| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include <unistd.h> |
| 8 |
#include <sys/mman.h> |
| 9 |
#include <mach/mach_init.h> |
| 10 |
#include <mach-o/getsect.h> |
| 11 |
|
| 12 |
#include <AvailabilityMacros.h> |
| 13 |
|
| 14 |
#include <pthread.h> |
| 15 |
#include <semaphore.h> |
| 16 |
#include <signal.h> |
| 17 |
#include <libkern/OSAtomic.h> |
| 18 |
#include <mach/mach.h> |
| 19 |
#include <mach/semaphore.h> |
| 20 |
#include <mach/task.h> |
| 21 |
#include <mach/thread_act.h> |
| 22 |
#include <mach/vm_statistics.h> |
| 23 |
#include <sys/time.h> |
| 24 |
#include <sys/resource.h> |
| 25 |
#include <sys/syscall.h> |
| 26 |
#include <sys/types.h> |
| 27 |
#include <sys/sysctl.h> |
| 28 |
#include <stdarg.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
#include <errno.h> |
| 32 |
#include <math.h> |
| 33 |
|
| 34 |
// this port is based off of v8 svn revision 9837 |
| 35 |
|
| 36 |
namespace mozilla { |
| 37 |
namespace baseprofiler { |
| 38 |
|
| 39 |
static int64_t MicrosecondsSince1970() { |
| 40 |
struct timeval tv; |
| 41 |
gettimeofday(&tv, NULL); |
| 42 |
return int64_t(tv.tv_sec) * 1000000 + int64_t(tv.tv_usec); |
| 43 |
} |
| 44 |
|
| 45 |
void* GetStackTop(void* aGuess) { |
| 46 |
pthread_t thread = pthread_self(); |
| 47 |
return pthread_get_stackaddr_np(thread); |
| 48 |
} |
| 49 |
|
| 50 |
class PlatformData { |
| 51 |
public: |
| 52 |
explicit PlatformData(BaseProfilerThreadId aThreadId) |
| 53 |
: mProfiledThread(mach_thread_self()) {} |
| 54 |
|
| 55 |
~PlatformData() { |
| 56 |
// Deallocate Mach port for thread. |
| 57 |
mach_port_deallocate(mach_task_self(), mProfiledThread); |
| 58 |
} |
| 59 |
|
| 60 |
thread_act_t ProfiledThread() { return mProfiledThread; } |
| 61 |
|
| 62 |
private: |
| 63 |
// Note: for mProfiledThread Mach primitives are used instead of pthread's |
| 64 |
// because the latter doesn't provide thread manipulation primitives required. |
| 65 |
// For details, consult "Mac OS X Internals" book, Section 7.3. |
| 66 |
thread_act_t mProfiledThread; |
| 67 |
}; |
| 68 |
|
| 69 |
//////////////////////////////////////////////////////////////////////// |
| 70 |
// BEGIN Sampler target specifics |
| 71 |
|
| 72 |
Sampler::Sampler(PSLockRef aLock) {} |
| 73 |
|
| 74 |
void Sampler::Disable(PSLockRef aLock) {} |
| 75 |
|
| 76 |
template <typename Func> |
| 77 |
void Sampler::SuspendAndSampleAndResumeThread( |
| 78 |
PSLockRef aLock, const RegisteredThread& aRegisteredThread, |
| 79 |
const TimeStamp& aNow, const Func& aProcessRegs) { |
| 80 |
thread_act_t samplee_thread = |
| 81 |
aRegisteredThread.GetPlatformData()->ProfiledThread(); |
| 82 |
|
| 83 |
//----------------------------------------------------------------// |
| 84 |
// Suspend the samplee thread and get its context. |
| 85 |
|
| 86 |
// We're using thread_suspend on OS X because pthread_kill (which is what we |
| 87 |
// at one time used on Linux) has less consistent performance and causes |
| 88 |
// strange crashes, see bug 1166778 and bug 1166808. thread_suspend |
| 89 |
// is also just a lot simpler to use. |
| 90 |
|
| 91 |
if (KERN_SUCCESS != thread_suspend(samplee_thread)) { |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
//----------------------------------------------------------------// |
| 96 |
// Sample the target thread. |
| 97 |
|
| 98 |
// WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING |
| 99 |
// |
| 100 |
// The profiler's "critical section" begins here. We must be very careful |
| 101 |
// what we do here, or risk deadlock. See the corresponding comment in |
| 102 |
// platform-linux-android.cpp for details. |
| 103 |
|
| 104 |
#if defined(__x86_64__) |
| 105 |
thread_state_flavor_t flavor = x86_THREAD_STATE64; |
| 106 |
x86_thread_state64_t state; |
| 107 |
mach_msg_type_number_t count = x86_THREAD_STATE64_COUNT; |
| 108 |
# if __DARWIN_UNIX03 |
| 109 |
# define REGISTER_FIELD(name) __r##name |
| 110 |
# else |
| 111 |
# define REGISTER_FIELD(name) r##name |
| 112 |
# endif // __DARWIN_UNIX03 |
| 113 |
#elif defined(__aarch64__) |
| 114 |
thread_state_flavor_t flavor = ARM_THREAD_STATE64; |
| 115 |
arm_thread_state64_t state; |
| 116 |
mach_msg_type_number_t count = ARM_THREAD_STATE64_COUNT; |
| 117 |
# if __DARWIN_UNIX03 |
| 118 |
# define REGISTER_FIELD(name) __##name |
| 119 |
# else |
| 120 |
# define REGISTER_FIELD(name) name |
| 121 |
# endif // __DARWIN_UNIX03 |
| 122 |
#else |
| 123 |
# error "unknown architecture" |
| 124 |
#endif |
| 125 |
|
| 126 |
if (thread_get_state(samplee_thread, flavor, |
| 127 |
reinterpret_cast<natural_t*>(&state), |
| 128 |
&count) == KERN_SUCCESS) { |
| 129 |
Registers regs; |
| 130 |
#if defined(__x86_64__) |
| 131 |
regs.mPC = reinterpret_cast<Address>(state.REGISTER_FIELD(ip)); |
| 132 |
regs.mSP = reinterpret_cast<Address>(state.REGISTER_FIELD(sp)); |
| 133 |
regs.mFP = reinterpret_cast<Address>(state.REGISTER_FIELD(bp)); |
| 134 |
regs.mR10 = reinterpret_cast<Address>(state.REGISTER_FIELD(10)); |
| 135 |
regs.mR12 = reinterpret_cast<Address>(state.REGISTER_FIELD(12)); |
| 136 |
#elif defined(__aarch64__) |
| 137 |
regs.mPC = reinterpret_cast<Address>(state.REGISTER_FIELD(pc)); |
| 138 |
regs.mSP = reinterpret_cast<Address>(state.REGISTER_FIELD(sp)); |
| 139 |
regs.mFP = reinterpret_cast<Address>(state.REGISTER_FIELD(fp)); |
| 140 |
regs.mLR = reinterpret_cast<Address>(state.REGISTER_FIELD(lr)); |
| 141 |
regs.mR11 = reinterpret_cast<Address>(state.REGISTER_FIELD(x[11])); |
| 142 |
#else |
| 143 |
# error "unknown architecture" |
| 144 |
#endif |
| 145 |
|
| 146 |
aProcessRegs(regs, aNow); |
| 147 |
} |
| 148 |
|
| 149 |
#undef REGISTER_FIELD |
| 150 |
|
| 151 |
//----------------------------------------------------------------// |
| 152 |
// Resume the target thread. |
| 153 |
|
| 154 |
thread_resume(samplee_thread); |
| 155 |
|
| 156 |
// The profiler's critical section ends here. |
| 157 |
// |
| 158 |
// WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING |
| 159 |
} |
| 160 |
|
| 161 |
// END Sampler target specifics |
| 162 |
//////////////////////////////////////////////////////////////////////// |
| 163 |
|
| 164 |
//////////////////////////////////////////////////////////////////////// |
| 165 |
// BEGIN SamplerThread target specifics |
| 166 |
|
| 167 |
static void* ThreadEntry(void* aArg) { |
| 168 |
auto thread = static_cast<SamplerThread*>(aArg); |
| 169 |
thread->Run(); |
| 170 |
return nullptr; |
| 171 |
} |
| 172 |
|
| 173 |
SamplerThread::SamplerThread(PSLockRef aLock, uint32_t aActivityGeneration, |
| 174 |
double aIntervalMilliseconds, uint32_t aFeatures) |
| 175 |
: mSampler(aLock), |
| 176 |
mActivityGeneration(aActivityGeneration), |
| 177 |
mIntervalMicroseconds( |
| 178 |
std::max(1, int(floor(aIntervalMilliseconds * 1000 + 0.5)))), |
| 179 |
mThread{nullptr} { |
| 180 |
pthread_attr_t* attr_ptr = nullptr; |
| 181 |
if (pthread_create(&mThread, attr_ptr, ThreadEntry, this) != 0) { |
| 182 |
MOZ_CRASH("pthread_create failed"); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
SamplerThread::~SamplerThread() { pthread_join(mThread, nullptr); } |
| 187 |
|
| 188 |
void SamplerThread::SleepMicro(uint32_t aMicroseconds) { |
| 189 |
usleep(aMicroseconds); |
| 190 |
// FIXME: the OSX 10.12 page for usleep says "The usleep() function is |
| 191 |
// obsolescent. Use nanosleep(2) instead." This implementation could be |
| 192 |
// merged with the linux-android version. Also, this doesn't handle the |
| 193 |
// case where the usleep call is interrupted by a signal. |
| 194 |
} |
| 195 |
|
| 196 |
void SamplerThread::Stop(PSLockRef aLock) { mSampler.Disable(aLock); } |
| 197 |
|
| 198 |
// END SamplerThread target specifics |
| 199 |
//////////////////////////////////////////////////////////////////////// |
| 200 |
|
| 201 |
static void PlatformInit(PSLockRef aLock) {} |
| 202 |
|
| 203 |
// clang-format off |
| 204 |
#if defined(HAVE_NATIVE_UNWIND) |
| 205 |
// Derive the stack pointer from the frame pointer. The 0x10 offset is |
| 206 |
// 8 bytes for the previous frame pointer and 8 bytes for the return |
| 207 |
// address both stored on the stack after at the beginning of the current |
| 208 |
// frame. |
| 209 |
# define REGISTERS_SYNC_POPULATE(regs) \ |
| 210 |
regs.mSP = reinterpret_cast<Address>(__builtin_frame_address(0)) + 0x10; \ |
| 211 |
_Pragma("GCC diagnostic push") \ |
| 212 |
_Pragma("GCC diagnostic ignored \"-Wframe-address\"") \ |
| 213 |
regs.mFP = reinterpret_cast<Address>(__builtin_frame_address(1)); \ |
| 214 |
_Pragma("GCC diagnostic pop") \ |
| 215 |
regs.mPC = reinterpret_cast<Address>( \ |
| 216 |
__builtin_extract_return_addr(__builtin_return_address(0))); |
| 217 |
#endif |
| 218 |
// clang-format on |
| 219 |
|
| 220 |
} // namespace baseprofiler |
| 221 |
} // namespace mozilla |