tools/profiler/platform-macos.cc

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include <dlfcn.h>
michael@0 6 #include <unistd.h>
michael@0 7 #include <sys/mman.h>
michael@0 8 #include <mach/mach_init.h>
michael@0 9 #include <mach-o/dyld.h>
michael@0 10 #include <mach-o/getsect.h>
michael@0 11
michael@0 12 #include <AvailabilityMacros.h>
michael@0 13
michael@0 14 #include <pthread.h>
michael@0 15 #include <semaphore.h>
michael@0 16 #include <signal.h>
michael@0 17 #include <libkern/OSAtomic.h>
michael@0 18 #include <mach/mach.h>
michael@0 19 #include <mach/semaphore.h>
michael@0 20 #include <mach/task.h>
michael@0 21 #include <mach/vm_statistics.h>
michael@0 22 #include <sys/time.h>
michael@0 23 #include <sys/resource.h>
michael@0 24 #include <sys/types.h>
michael@0 25 #include <sys/sysctl.h>
michael@0 26 #include <stdarg.h>
michael@0 27 #include <stdlib.h>
michael@0 28 #include <string.h>
michael@0 29 #include <errno.h>
michael@0 30 #include <math.h>
michael@0 31
michael@0 32 #include "nsThreadUtils.h"
michael@0 33
michael@0 34 #include "platform.h"
michael@0 35 #include "TableTicker.h"
michael@0 36 #include "UnwinderThread2.h" /* uwt__register_thread_for_profiling */
michael@0 37
michael@0 38 // this port is based off of v8 svn revision 9837
michael@0 39
michael@0 40 // XXX: this is a very stubbed out implementation
michael@0 41 // that only supports a single Sampler
michael@0 42 struct SamplerRegistry {
michael@0 43 static void AddActiveSampler(Sampler *sampler) {
michael@0 44 ASSERT(!SamplerRegistry::sampler);
michael@0 45 SamplerRegistry::sampler = sampler;
michael@0 46 }
michael@0 47 static void RemoveActiveSampler(Sampler *sampler) {
michael@0 48 SamplerRegistry::sampler = NULL;
michael@0 49 }
michael@0 50 static Sampler *sampler;
michael@0 51 };
michael@0 52
michael@0 53 Sampler *SamplerRegistry::sampler = NULL;
michael@0 54
michael@0 55 // 0 is never a valid thread id on MacOSX since a ptread_t is
michael@0 56 // a pointer.
michael@0 57 static const pthread_t kNoThread = (pthread_t) 0;
michael@0 58
michael@0 59 void OS::Startup() {
michael@0 60 }
michael@0 61
michael@0 62 void OS::Sleep(int milliseconds) {
michael@0 63 usleep(1000 * milliseconds);
michael@0 64 }
michael@0 65
michael@0 66 void OS::SleepMicro(int microseconds) {
michael@0 67 usleep(microseconds);
michael@0 68 }
michael@0 69
michael@0 70 Thread::Thread(const char* name)
michael@0 71 : stack_size_(0) {
michael@0 72 set_name(name);
michael@0 73 }
michael@0 74
michael@0 75
michael@0 76 Thread::~Thread() {
michael@0 77 }
michael@0 78
michael@0 79
michael@0 80 static void SetThreadName(const char* name) {
michael@0 81 // pthread_setname_np is only available in 10.6 or later, so test
michael@0 82 // for it at runtime.
michael@0 83 int (*dynamic_pthread_setname_np)(const char*);
michael@0 84 *reinterpret_cast<void**>(&dynamic_pthread_setname_np) =
michael@0 85 dlsym(RTLD_DEFAULT, "pthread_setname_np");
michael@0 86 if (!dynamic_pthread_setname_np)
michael@0 87 return;
michael@0 88
michael@0 89 // Mac OS X does not expose the length limit of the name, so hardcode it.
michael@0 90 static const int kMaxNameLength = 63;
michael@0 91 USE(kMaxNameLength);
michael@0 92 ASSERT(Thread::kMaxThreadNameLength <= kMaxNameLength);
michael@0 93 dynamic_pthread_setname_np(name);
michael@0 94 }
michael@0 95
michael@0 96
michael@0 97 static void* ThreadEntry(void* arg) {
michael@0 98 Thread* thread = reinterpret_cast<Thread*>(arg);
michael@0 99
michael@0 100 thread->thread_ = pthread_self();
michael@0 101 SetThreadName(thread->name());
michael@0 102 ASSERT(thread->thread_ != kNoThread);
michael@0 103 thread->Run();
michael@0 104 return NULL;
michael@0 105 }
michael@0 106
michael@0 107
michael@0 108 void Thread::set_name(const char* name) {
michael@0 109 strncpy(name_, name, sizeof(name_));
michael@0 110 name_[sizeof(name_) - 1] = '\0';
michael@0 111 }
michael@0 112
michael@0 113
michael@0 114 void Thread::Start() {
michael@0 115 pthread_attr_t* attr_ptr = NULL;
michael@0 116 pthread_attr_t attr;
michael@0 117 if (stack_size_ > 0) {
michael@0 118 pthread_attr_init(&attr);
michael@0 119 pthread_attr_setstacksize(&attr, static_cast<size_t>(stack_size_));
michael@0 120 attr_ptr = &attr;
michael@0 121 }
michael@0 122 pthread_create(&thread_, attr_ptr, ThreadEntry, this);
michael@0 123 ASSERT(thread_ != kNoThread);
michael@0 124 }
michael@0 125
michael@0 126 void Thread::Join() {
michael@0 127 pthread_join(thread_, NULL);
michael@0 128 }
michael@0 129
michael@0 130 class PlatformData : public Malloced {
michael@0 131 public:
michael@0 132 PlatformData() : profiled_thread_(mach_thread_self())
michael@0 133 {
michael@0 134 profiled_pthread_ = pthread_from_mach_thread_np(profiled_thread_);
michael@0 135 }
michael@0 136
michael@0 137 ~PlatformData() {
michael@0 138 // Deallocate Mach port for thread.
michael@0 139 mach_port_deallocate(mach_task_self(), profiled_thread_);
michael@0 140 }
michael@0 141
michael@0 142 thread_act_t profiled_thread() { return profiled_thread_; }
michael@0 143 pthread_t profiled_pthread() { return profiled_pthread_; }
michael@0 144
michael@0 145 private:
michael@0 146 // Note: for profiled_thread_ Mach primitives are used instead of PThread's
michael@0 147 // because the latter doesn't provide thread manipulation primitives required.
michael@0 148 // For details, consult "Mac OS X Internals" book, Section 7.3.
michael@0 149 thread_act_t profiled_thread_;
michael@0 150 // we also store the pthread because Mach threads have no concept of stack
michael@0 151 // and we want to be able to get the stack size when we need to unwind the
michael@0 152 // stack using frame pointers.
michael@0 153 pthread_t profiled_pthread_;
michael@0 154 };
michael@0 155
michael@0 156 /* static */ PlatformData*
michael@0 157 Sampler::AllocPlatformData(int aThreadId)
michael@0 158 {
michael@0 159 return new PlatformData;
michael@0 160 }
michael@0 161
michael@0 162 /* static */ void
michael@0 163 Sampler::FreePlatformData(PlatformData* aData)
michael@0 164 {
michael@0 165 delete aData;
michael@0 166 }
michael@0 167
michael@0 168 class SamplerThread : public Thread {
michael@0 169 public:
michael@0 170 explicit SamplerThread(double interval)
michael@0 171 : Thread("SamplerThread")
michael@0 172 , intervalMicro_(floor(interval * 1000 + 0.5))
michael@0 173 {
michael@0 174 if (intervalMicro_ <= 0) {
michael@0 175 intervalMicro_ = 1;
michael@0 176 }
michael@0 177 }
michael@0 178
michael@0 179 static void AddActiveSampler(Sampler* sampler) {
michael@0 180 mozilla::MutexAutoLock lock(*Sampler::sRegisteredThreadsMutex);
michael@0 181 SamplerRegistry::AddActiveSampler(sampler);
michael@0 182 if (instance_ == NULL) {
michael@0 183 instance_ = new SamplerThread(sampler->interval());
michael@0 184 instance_->Start();
michael@0 185 }
michael@0 186 }
michael@0 187
michael@0 188 static void RemoveActiveSampler(Sampler* sampler) {
michael@0 189 mozilla::MutexAutoLock lock(*Sampler::sRegisteredThreadsMutex);
michael@0 190 instance_->Join();
michael@0 191 //XXX: unlike v8 we need to remove the active sampler after doing the Join
michael@0 192 // because we drop the sampler immediately
michael@0 193 SamplerRegistry::RemoveActiveSampler(sampler);
michael@0 194 delete instance_;
michael@0 195 instance_ = NULL;
michael@0 196 }
michael@0 197
michael@0 198 // Implement Thread::Run().
michael@0 199 virtual void Run() {
michael@0 200 while (SamplerRegistry::sampler->IsActive()) {
michael@0 201 if (!SamplerRegistry::sampler->IsPaused()) {
michael@0 202 mozilla::MutexAutoLock lock(*Sampler::sRegisteredThreadsMutex);
michael@0 203 std::vector<ThreadInfo*> threads =
michael@0 204 SamplerRegistry::sampler->GetRegisteredThreads();
michael@0 205 for (uint32_t i = 0; i < threads.size(); i++) {
michael@0 206 ThreadInfo* info = threads[i];
michael@0 207
michael@0 208 // This will be null if we're not interested in profiling this thread.
michael@0 209 if (!info->Profile())
michael@0 210 continue;
michael@0 211
michael@0 212 PseudoStack::SleepState sleeping = info->Stack()->observeSleeping();
michael@0 213 if (sleeping == PseudoStack::SLEEPING_AGAIN) {
michael@0 214 info->Profile()->DuplicateLastSample();
michael@0 215 //XXX: This causes flushes regardless of jank-only mode
michael@0 216 info->Profile()->flush();
michael@0 217 continue;
michael@0 218 }
michael@0 219
michael@0 220 ThreadProfile* thread_profile = info->Profile();
michael@0 221
michael@0 222 SampleContext(SamplerRegistry::sampler, thread_profile);
michael@0 223 }
michael@0 224 }
michael@0 225 OS::SleepMicro(intervalMicro_);
michael@0 226 }
michael@0 227 }
michael@0 228
michael@0 229 void SampleContext(Sampler* sampler, ThreadProfile* thread_profile) {
michael@0 230 thread_act_t profiled_thread =
michael@0 231 thread_profile->GetPlatformData()->profiled_thread();
michael@0 232
michael@0 233 TickSample sample_obj;
michael@0 234 TickSample* sample = &sample_obj;
michael@0 235
michael@0 236 if (KERN_SUCCESS != thread_suspend(profiled_thread)) return;
michael@0 237
michael@0 238 #if V8_HOST_ARCH_X64
michael@0 239 thread_state_flavor_t flavor = x86_THREAD_STATE64;
michael@0 240 x86_thread_state64_t state;
michael@0 241 mach_msg_type_number_t count = x86_THREAD_STATE64_COUNT;
michael@0 242 #if __DARWIN_UNIX03
michael@0 243 #define REGISTER_FIELD(name) __r ## name
michael@0 244 #else
michael@0 245 #define REGISTER_FIELD(name) r ## name
michael@0 246 #endif // __DARWIN_UNIX03
michael@0 247 #elif V8_HOST_ARCH_IA32
michael@0 248 thread_state_flavor_t flavor = i386_THREAD_STATE;
michael@0 249 i386_thread_state_t state;
michael@0 250 mach_msg_type_number_t count = i386_THREAD_STATE_COUNT;
michael@0 251 #if __DARWIN_UNIX03
michael@0 252 #define REGISTER_FIELD(name) __e ## name
michael@0 253 #else
michael@0 254 #define REGISTER_FIELD(name) e ## name
michael@0 255 #endif // __DARWIN_UNIX03
michael@0 256 #else
michael@0 257 #error Unsupported Mac OS X host architecture.
michael@0 258 #endif // V8_HOST_ARCH
michael@0 259
michael@0 260 if (thread_get_state(profiled_thread,
michael@0 261 flavor,
michael@0 262 reinterpret_cast<natural_t*>(&state),
michael@0 263 &count) == KERN_SUCCESS) {
michael@0 264 sample->pc = reinterpret_cast<Address>(state.REGISTER_FIELD(ip));
michael@0 265 sample->sp = reinterpret_cast<Address>(state.REGISTER_FIELD(sp));
michael@0 266 sample->fp = reinterpret_cast<Address>(state.REGISTER_FIELD(bp));
michael@0 267 sample->timestamp = mozilla::TimeStamp::Now();
michael@0 268 sample->threadProfile = thread_profile;
michael@0 269 sampler->Tick(sample);
michael@0 270 }
michael@0 271 thread_resume(profiled_thread);
michael@0 272 }
michael@0 273
michael@0 274 int intervalMicro_;
michael@0 275 //RuntimeProfilerRateLimiter rate_limiter_;
michael@0 276
michael@0 277 static SamplerThread* instance_;
michael@0 278
michael@0 279 DISALLOW_COPY_AND_ASSIGN(SamplerThread);
michael@0 280 };
michael@0 281
michael@0 282 #undef REGISTER_FIELD
michael@0 283
michael@0 284 SamplerThread* SamplerThread::instance_ = NULL;
michael@0 285
michael@0 286 Sampler::Sampler(double interval, bool profiling, int entrySize)
michael@0 287 : // isolate_(isolate),
michael@0 288 interval_(interval),
michael@0 289 profiling_(profiling),
michael@0 290 paused_(false),
michael@0 291 active_(false),
michael@0 292 entrySize_(entrySize) /*,
michael@0 293 samples_taken_(0)*/ {
michael@0 294 }
michael@0 295
michael@0 296
michael@0 297 Sampler::~Sampler() {
michael@0 298 ASSERT(!IsActive());
michael@0 299 }
michael@0 300
michael@0 301
michael@0 302 void Sampler::Start() {
michael@0 303 ASSERT(!IsActive());
michael@0 304 SetActive(true);
michael@0 305 SamplerThread::AddActiveSampler(this);
michael@0 306 }
michael@0 307
michael@0 308
michael@0 309 void Sampler::Stop() {
michael@0 310 ASSERT(IsActive());
michael@0 311 SetActive(false);
michael@0 312 SamplerThread::RemoveActiveSampler(this);
michael@0 313 }
michael@0 314
michael@0 315 pthread_t
michael@0 316 Sampler::GetProfiledThread(PlatformData* aData)
michael@0 317 {
michael@0 318 return aData->profiled_pthread();
michael@0 319 }
michael@0 320
michael@0 321 #include <sys/syscall.h>
michael@0 322 pid_t gettid()
michael@0 323 {
michael@0 324 return (pid_t) syscall(SYS_thread_selfid);
michael@0 325 }
michael@0 326
michael@0 327 /* static */ Thread::tid_t
michael@0 328 Thread::GetCurrentId()
michael@0 329 {
michael@0 330 return gettid();
michael@0 331 }
michael@0 332
michael@0 333 bool Sampler::RegisterCurrentThread(const char* aName,
michael@0 334 PseudoStack* aPseudoStack,
michael@0 335 bool aIsMainThread, void* stackTop)
michael@0 336 {
michael@0 337 if (!Sampler::sRegisteredThreadsMutex)
michael@0 338 return false;
michael@0 339
michael@0 340
michael@0 341 mozilla::MutexAutoLock lock(*Sampler::sRegisteredThreadsMutex);
michael@0 342
michael@0 343 int id = gettid();
michael@0 344 for (uint32_t i = 0; i < sRegisteredThreads->size(); i++) {
michael@0 345 ThreadInfo* info = sRegisteredThreads->at(i);
michael@0 346 if (info->ThreadId() == id) {
michael@0 347 // Thread already registered. This means the first unregister will be
michael@0 348 // too early.
michael@0 349 ASSERT(false);
michael@0 350 return false;
michael@0 351 }
michael@0 352 }
michael@0 353
michael@0 354 set_tls_stack_top(stackTop);
michael@0 355
michael@0 356 ThreadInfo* info = new ThreadInfo(aName, id,
michael@0 357 aIsMainThread, aPseudoStack, stackTop);
michael@0 358
michael@0 359 if (sActiveSampler) {
michael@0 360 sActiveSampler->RegisterThread(info);
michael@0 361 }
michael@0 362
michael@0 363 sRegisteredThreads->push_back(info);
michael@0 364
michael@0 365 uwt__register_thread_for_profiling(stackTop);
michael@0 366 return true;
michael@0 367 }
michael@0 368
michael@0 369 void Sampler::UnregisterCurrentThread()
michael@0 370 {
michael@0 371 if (!Sampler::sRegisteredThreadsMutex)
michael@0 372 return;
michael@0 373
michael@0 374 tlsStackTop.set(nullptr);
michael@0 375
michael@0 376 mozilla::MutexAutoLock lock(*Sampler::sRegisteredThreadsMutex);
michael@0 377
michael@0 378 int id = gettid();
michael@0 379
michael@0 380 for (uint32_t i = 0; i < sRegisteredThreads->size(); i++) {
michael@0 381 ThreadInfo* info = sRegisteredThreads->at(i);
michael@0 382 if (info->ThreadId() == id) {
michael@0 383 delete info;
michael@0 384 sRegisteredThreads->erase(sRegisteredThreads->begin() + i);
michael@0 385 break;
michael@0 386 }
michael@0 387 }
michael@0 388 }
michael@0 389
michael@0 390 void TickSample::PopulateContext(void* aContext)
michael@0 391 {
michael@0 392 // Note that this asm changes if PopulateContext's parameter list is altered
michael@0 393 #if defined(SPS_PLAT_amd64_darwin)
michael@0 394 asm (
michael@0 395 // Compute caller's %rsp by adding to %rbp:
michael@0 396 // 8 bytes for previous %rbp, 8 bytes for return address
michael@0 397 "leaq 0x10(%%rbp), %0\n\t"
michael@0 398 // Dereference %rbp to get previous %rbp
michael@0 399 "movq (%%rbp), %1\n\t"
michael@0 400 :
michael@0 401 "=r"(sp),
michael@0 402 "=r"(fp)
michael@0 403 );
michael@0 404 #elif defined(SPS_PLAT_x86_darwin)
michael@0 405 asm (
michael@0 406 // Compute caller's %esp by adding to %ebp:
michael@0 407 // 4 bytes for aContext + 4 bytes for return address +
michael@0 408 // 4 bytes for previous %ebp
michael@0 409 "leal 0xc(%%ebp), %0\n\t"
michael@0 410 // Dereference %ebp to get previous %ebp
michael@0 411 "movl (%%ebp), %1\n\t"
michael@0 412 :
michael@0 413 "=r"(sp),
michael@0 414 "=r"(fp)
michael@0 415 );
michael@0 416 #else
michael@0 417 # error "Unsupported architecture"
michael@0 418 #endif
michael@0 419 pc = reinterpret_cast<Address>(__builtin_extract_return_addr(
michael@0 420 __builtin_return_address(0)));
michael@0 421 }
michael@0 422

mercurial