1/*
2 * Copyright (C) 2018 Yusuke Suzuki <[email protected]>.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "PerfLog.h"
28
29#if ENABLE(ASSEMBLER) && OS(LINUX)
30
31#include <array>
32#include <elf.h>
33#include <fcntl.h>
34#include <mutex>
35#include <sys/mman.h>
36#include <sys/stat.h>
37#include <sys/syscall.h>
38#include <sys/types.h>
39#include <unistd.h>
40#include <wtf/DataLog.h>
41#include <wtf/MonotonicTime.h>
42#include <wtf/PageBlock.h>
43#include <wtf/ProcessID.h>
44
45namespace JSC {
46
47namespace PerfLogInternal {
48static constexpr bool verbose = false;
49} // namespace PerfLogInternal
50
51namespace JITDump {
52namespace Constants {
53
54// Perf jit-dump formats are specified here.
55// https://raw.githubusercontent.com/torvalds/linux/master/tools/perf/Documentation/jitdump-specification.txt
56
57// The latest version 2, but it is too new at that time.
58static constexpr uint32_t version = 1;
59
60#if CPU(LITTLE_ENDIAN)
61static constexpr uint32_t magic = 0x4a695444;
62#else
63static constexpr uint32_t magic = 0x4454694a;
64#endif
65
66#if CPU(X86)
67static constexpr uint32_t elfMachine = EM_386;
68#elif CPU(X86_64)
69static constexpr uint32_t elfMachine = EM_X86_64;
70#elif CPU(ARM64)
71static constexpr uint32_t elfMachine = EM_AARCH64;
72#elif CPU(ARM)
73static constexpr uint32_t elfMachine = EM_ARM;
74#elif CPU(MIPS)
75#if CPU(LITTLE_ENDIAN)
76static constexpr uint32_t elfMachine = EM_MIPS_RS3_LE;
77#else
78static constexpr uint32_t elfMachine = EM_MIPS;
79#endif
80#endif
81
82} // namespace Constants
83
84struct FileHeader {
85 uint32_t magic { Constants::magic };
86 uint32_t version { Constants::version };
87 uint32_t totalSize { sizeof(FileHeader) };
88 uint32_t elfMachine { Constants::elfMachine };
89 uint32_t padding1 { 0 };
90 uint32_t pid { 0 };
91 uint64_t timestamp { 0 };
92 uint64_t flags { 0 };
93};
94
95enum class RecordType : uint32_t {
96 JITCodeLoad = 0,
97 JITCodeMove = 1,
98 JITCodeDebugInfo = 2,
99 JITCodeClose = 3,
100 JITCodeUnwindingInfo = 4,
101};
102
103struct RecordHeader {
104 RecordType type { RecordType::JITCodeLoad };
105 uint32_t totalSize { 0 };
106 uint64_t timestamp { 0 };
107};
108
109struct CodeLoadRecord {
110 RecordHeader header {
111 RecordType::JITCodeLoad,
112 0,
113 0,
114 };
115 uint32_t pid { 0 };
116 uint32_t tid { 0 };
117 uint64_t vma { 0 };
118 uint64_t codeAddress { 0 };
119 uint64_t codeSize { 0 };
120 uint64_t codeIndex { 0 };
121};
122
123} // namespace JITDump
124
125PerfLog& PerfLog::singleton()
126{
127 static PerfLog* logger;
128 static std::once_flag onceKey;
129 std::call_once(onceKey, [] {
130 logger = new PerfLog;
131 });
132 return *logger;
133}
134
135static inline uint64_t generateTimestamp()
136{
137 return MonotonicTime::now().secondsSinceEpoch().nanosecondsAs<uint64_t>();
138}
139
140static inline pid_t getCurrentThreadID()
141{
142 return static_cast<pid_t>(syscall(__NR_gettid));
143}
144
145PerfLog::PerfLog()
146{
147 {
148 std::array<char, 1024> filename;
149 snprintf(filename.data(), filename.size() - 1, "jit-%d.dump", getCurrentProcessID());
150 filename[filename.size() - 1] = '\0';
151 m_fd = open(filename.data(), O_CREAT | O_TRUNC | O_RDWR, 0666);
152 RELEASE_ASSERT(m_fd != -1);
153
154 // Linux perf command records this mmap operation in perf.data as a metadata to the JIT perf annotations.
155 // We do not use this mmap-ed memory region actually.
156 m_marker = mmap(nullptr, pageSize(), PROT_READ | PROT_EXEC, MAP_PRIVATE, m_fd, 0);
157 RELEASE_ASSERT(m_marker != MAP_FAILED);
158
159 m_file = fdopen(m_fd, "wb");
160 RELEASE_ASSERT(m_file);
161 }
162
163 JITDump::FileHeader header;
164 header.timestamp = generateTimestamp();
165 header.pid = getCurrentProcessID();
166
167 auto locker = holdLock(m_lock);
168 write(locker, &header, sizeof(JITDump::FileHeader));
169 flush(locker);
170}
171
172void PerfLog::write(const AbstractLocker&, const void* data, size_t size)
173{
174 size_t result = fwrite(data, 1, size, m_file);
175 RELEASE_ASSERT(result == size);
176}
177
178void PerfLog::flush(const AbstractLocker&)
179{
180 fflush(m_file);
181}
182
183void PerfLog::log(CString&& name, const uint8_t* executableAddress, size_t size)
184{
185 if (!size) {
186 dataLogLnIf(PerfLogInternal::verbose, "0 size record ", name, " ", RawPointer(executableAddress));
187 return;
188 }
189
190 PerfLog& logger = singleton();
191 auto locker = holdLock(logger.m_lock);
192
193 JITDump::CodeLoadRecord record;
194 record.header.timestamp = generateTimestamp();
195 record.header.totalSize = sizeof(JITDump::CodeLoadRecord) + (name.length() + 1) + size;
196 record.pid = getCurrentProcessID();
197 record.tid = getCurrentThreadID();
198 record.vma = bitwise_cast<uintptr_t>(executableAddress);
199 record.codeAddress = bitwise_cast<uintptr_t>(executableAddress);
200 record.codeSize = size;
201 record.codeIndex = logger.m_codeIndex++;
202
203 logger.write(locker, &record, sizeof(JITDump::CodeLoadRecord));
204 logger.write(locker, name.data(), name.length() + 1);
205 logger.write(locker, executableAddress, size);
206 logger.flush(locker);
207
208 dataLogLnIf(PerfLogInternal::verbose, name, " [", record.codeIndex, "] ", RawPointer(executableAddress), "-", RawPointer(executableAddress + size), " ", size);
209}
210
211} // namespace JSC
212
213#endif // ENABLE(ASSEMBLER) && OS(LINUX)
214