1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above
9 //       copyright notice, this list of conditions and the following
10 //       disclaimer in the documentation and/or other materials provided
11 //       with the distribution.
12 //     * Neither the name of Google Inc. nor the names of its
13 //       contributors may be used to endorse or promote products derived
14 //       from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 #ifndef V8_PERF_JIT_H_
29 #define V8_PERF_JIT_H_
30 
31 #include "src/v8.h"
32 
33 namespace v8 {
34 namespace internal {
35 
36 // TODO(jarin) For now, we disable perf integration on Android because of a
37 // build problem - when building the snapshot with AOSP, librt is not
38 // available, so we cannot use the clock_gettime function. To fix this, we
39 // should thread through the V8_LIBRT_NOT_AVAILABLE flag here and only disable
40 // the perf integration when this flag is present (the perf integration is not
41 // needed when generating snapshot, so it is fine to ifdef it away).
42 
43 #if V8_OS_LINUX
44 
45 // Linux perf tool logging support
46 class PerfJitLogger : public CodeEventLogger {
47  public:
48   PerfJitLogger();
49   virtual ~PerfJitLogger();
50 
51   virtual void CodeMoveEvent(Address from, Address to);
52   virtual void CodeDeleteEvent(Address from);
CodeDisableOptEvent(Code * code,SharedFunctionInfo * shared)53   virtual void CodeDisableOptEvent(Code* code, SharedFunctionInfo* shared) {}
54   virtual void SnapshotPositionEvent(Address addr, int pos);
55 
56  private:
57   uint64_t GetTimestamp();
58   virtual void LogRecordedBuffer(Code* code, SharedFunctionInfo* shared,
59                                  const char* name, int length);
60 
61   // Extension added to V8 log file name to get the low-level log name.
62   static const char kFilenameFormatString[];
63   static const int kFilenameBufferPadding;
64 
65   // File buffer size of the low-level log. We don't use the default to
66   // minimize the associated overhead.
67   static const int kLogBufferSize = 2 * MB;
68 
69   void LogWriteBytes(const char* bytes, int size);
70   void LogWriteHeader();
71 
72   static const uint32_t kElfMachIA32 = 3;
73   static const uint32_t kElfMachX64 = 62;
74   static const uint32_t kElfMachARM = 40;
75   static const uint32_t kElfMachMIPS = 10;
76 
GetElfMach()77   uint32_t GetElfMach() {
78 #if V8_TARGET_ARCH_IA32
79     return kElfMachIA32;
80 #elif V8_TARGET_ARCH_X64
81     return kElfMachX64;
82 #elif V8_TARGET_ARCH_ARM
83     return kElfMachARM;
84 #elif V8_TARGET_ARCH_MIPS
85     return kElfMachMIPS;
86 #else
87     UNIMPLEMENTED();
88     return 0;
89 #endif
90   }
91 
92   FILE* perf_output_handle_;
93   uint64_t code_index_;
94 };
95 
96 #else
97 
98 // PerfJitLogger is only implemented on Linux
99 class PerfJitLogger : public CodeEventLogger {
100  public:
101   virtual void CodeMoveEvent(Address from, Address to) { UNIMPLEMENTED(); }
102 
103   virtual void CodeDeleteEvent(Address from) { UNIMPLEMENTED(); }
104 
105   virtual void CodeDisableOptEvent(Code* code, SharedFunctionInfo* shared) {
106     UNIMPLEMENTED();
107   }
108 
109   virtual void SnapshotPositionEvent(Address addr, int pos) { UNIMPLEMENTED(); }
110 
111   virtual void LogRecordedBuffer(Code* code, SharedFunctionInfo* shared,
112                                  const char* name, int length) {
113     UNIMPLEMENTED();
114   }
115 };
116 
117 #endif  // V8_OS_LINUX
118 }
119 }  // namespace v8::internal
120 #endif
121