| 1 | /* |
| 2 | * Copyright (C) 2018 Yusuke Suzuki <yusukesuzuki@slowstart.org>. |
| 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 <elf.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <mutex> |
| 34 | #include <sys/mman.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <sys/syscall.h> |
| 37 | #include <sys/types.h> |
| 38 | #include <unistd.h> |
| 39 | #include <wtf/DataLog.h> |
| 40 | #include <wtf/MonotonicTime.h> |
| 41 | #include <wtf/PageBlock.h> |
| 42 | #include <wtf/ProcessID.h> |
| 43 | |
| 44 | namespace JSC { |
| 45 | |
| 46 | namespace PerfLogInternal { |
| 47 | static constexpr bool verbose = false; |
| 48 | } // namespace PerfLogInternal |
| 49 | |
| 50 | namespace JITDump { |
| 51 | namespace Constants { |
| 52 | |
| 53 | // Perf jit-dump formats are specified here. |
| 54 | // https://raw.githubusercontent.com/torvalds/linux/master/tools/perf/Documentation/jitdump-specification.txt |
| 55 | |
| 56 | // The latest version 2, but it is too new at that time. |
| 57 | static constexpr uint32_t version = 1; |
| 58 | |
| 59 | #if CPU(LITTLE_ENDIAN) |
| 60 | static constexpr uint32_t magic = 0x4a695444; |
| 61 | #else |
| 62 | static constexpr uint32_t magic = 0x4454694a; |
| 63 | #endif |
| 64 | |
| 65 | #if CPU(X86) |
| 66 | static constexpr uint32_t elfMachine = EM_386; |
| 67 | #elif CPU(X86_64) |
| 68 | static constexpr uint32_t elfMachine = EM_X86_64; |
| 69 | #elif CPU(ARM64) |
| 70 | static constexpr uint32_t elfMachine = EM_AARCH64; |
| 71 | #elif CPU(ARM) |
| 72 | static constexpr uint32_t elfMachine = EM_ARM; |
| 73 | #elif CPU(MIPS) |
| 74 | #if CPU(LITTLE_ENDIAN) |
| 75 | static constexpr uint32_t elfMachine = EM_MIPS_RS3_LE; |
| 76 | #else |
| 77 | static constexpr uint32_t elfMachine = EM_MIPS; |
| 78 | #endif |
| 79 | #endif |
| 80 | |
| 81 | } // namespace Constants |
| 82 | |
| 83 | struct { |
| 84 | uint32_t { Constants::magic }; |
| 85 | uint32_t { Constants::version }; |
| 86 | uint32_t { sizeof(FileHeader) }; |
| 87 | uint32_t { Constants::elfMachine }; |
| 88 | uint32_t { 0 }; |
| 89 | uint32_t { 0 }; |
| 90 | uint64_t { 0 }; |
| 91 | uint64_t { 0 }; |
| 92 | }; |
| 93 | |
| 94 | enum class RecordType : uint32_t { |
| 95 | JITCodeLoad = 0, |
| 96 | JITCodeMove = 1, |
| 97 | JITCodeDebugInfo = 2, |
| 98 | JITCodeClose = 3, |
| 99 | JITCodeUnwindingInfo = 4, |
| 100 | }; |
| 101 | |
| 102 | struct { |
| 103 | RecordType { RecordType::JITCodeLoad }; |
| 104 | uint32_t { 0 }; |
| 105 | uint64_t { 0 }; |
| 106 | }; |
| 107 | |
| 108 | struct CodeLoadRecord { |
| 109 | RecordHeader { |
| 110 | RecordType::JITCodeLoad, |
| 111 | 0, |
| 112 | 0, |
| 113 | }; |
| 114 | uint32_t pid { 0 }; |
| 115 | uint32_t tid { 0 }; |
| 116 | uint64_t vma { 0 }; |
| 117 | uint64_t codeAddress { 0 }; |
| 118 | uint64_t codeSize { 0 }; |
| 119 | uint64_t codeIndex { 0 }; |
| 120 | }; |
| 121 | |
| 122 | } // namespace JITDump |
| 123 | |
| 124 | PerfLog& PerfLog::singleton() |
| 125 | { |
| 126 | static PerfLog* logger; |
| 127 | static std::once_flag onceKey; |
| 128 | std::call_once(onceKey, [] { |
| 129 | logger = new PerfLog; |
| 130 | }); |
| 131 | return *logger; |
| 132 | } |
| 133 | |
| 134 | static inline uint64_t generateTimestamp() |
| 135 | { |
| 136 | return MonotonicTime::now().secondsSinceEpoch().nanosecondsAs<uint64_t>(); |
| 137 | } |
| 138 | |
| 139 | static inline pid_t getCurrentThreadID() |
| 140 | { |
| 141 | return static_cast<pid_t>(syscall(__NR_gettid)); |
| 142 | } |
| 143 | |
| 144 | PerfLog::PerfLog() |
| 145 | { |
| 146 | { |
| 147 | std::array<char, 1024> filename; |
| 148 | snprintf(filename.data(), filename.size() - 1, "jit-%d.dump" , getCurrentProcessID()); |
| 149 | filename[filename.size() - 1] = '\0'; |
| 150 | m_fd = open(filename.data(), O_CREAT | O_TRUNC | O_RDWR, 0666); |
| 151 | RELEASE_ASSERT(m_fd != -1); |
| 152 | |
| 153 | // Linux perf command records this mmap operation in perf.data as a metadata to the JIT perf annotations. |
| 154 | // We do not use this mmap-ed memory region actually. |
| 155 | m_marker = mmap(nullptr, pageSize(), PROT_READ | PROT_EXEC, MAP_PRIVATE, m_fd, 0); |
| 156 | RELEASE_ASSERT(m_marker != MAP_FAILED); |
| 157 | |
| 158 | m_file = fdopen(m_fd, "wb" ); |
| 159 | RELEASE_ASSERT(m_file); |
| 160 | } |
| 161 | |
| 162 | JITDump::FileHeader ; |
| 163 | header.timestamp = generateTimestamp(); |
| 164 | header.pid = getCurrentProcessID(); |
| 165 | |
| 166 | auto locker = holdLock(m_lock); |
| 167 | write(locker, &header, sizeof(JITDump::FileHeader)); |
| 168 | flush(locker); |
| 169 | } |
| 170 | |
| 171 | void PerfLog::write(const AbstractLocker&, const void* data, size_t size) |
| 172 | { |
| 173 | size_t result = fwrite(data, 1, size, m_file); |
| 174 | RELEASE_ASSERT(result == size); |
| 175 | } |
| 176 | |
| 177 | void PerfLog::flush(const AbstractLocker&) |
| 178 | { |
| 179 | fflush(m_file); |
| 180 | } |
| 181 | |
| 182 | void PerfLog::log(CString&& name, const uint8_t* executableAddress, size_t size) |
| 183 | { |
| 184 | if (!size) { |
| 185 | dataLogLnIf(PerfLogInternal::verbose, "0 size record " , name, " " , RawPointer(executableAddress)); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | PerfLog& logger = singleton(); |
| 190 | auto locker = holdLock(logger.m_lock); |
| 191 | |
| 192 | JITDump::CodeLoadRecord record; |
| 193 | record.header.timestamp = generateTimestamp(); |
| 194 | record.header.totalSize = sizeof(JITDump::CodeLoadRecord) + (name.length() + 1) + size; |
| 195 | record.pid = getCurrentProcessID(); |
| 196 | record.tid = getCurrentThreadID(); |
| 197 | record.vma = bitwise_cast<uintptr_t>(executableAddress); |
| 198 | record.codeAddress = bitwise_cast<uintptr_t>(executableAddress); |
| 199 | record.codeSize = size; |
| 200 | record.codeIndex = logger.m_codeIndex++; |
| 201 | |
| 202 | logger.write(locker, &record, sizeof(JITDump::CodeLoadRecord)); |
| 203 | logger.write(locker, name.data(), name.length() + 1); |
| 204 | logger.write(locker, executableAddress, size); |
| 205 | logger.flush(locker); |
| 206 | |
| 207 | dataLogLnIf(PerfLogInternal::verbose, name, " [" , record.codeIndex, "] " , RawPointer(executableAddress), "-" , RawPointer(executableAddress + size), " " , size); |
| 208 | } |
| 209 | |
| 210 | } // namespace JSC |
| 211 | |
| 212 | #endif // ENABLE(ASSEMBLER) && OS(LINUX) |
| 213 | |