xpcom/threads/ThreadStackHelper.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef mozilla_ThreadStackHelper_h
michael@0 7 #define mozilla_ThreadStackHelper_h
michael@0 8
michael@0 9 #include "mozilla/ThreadHangStats.h"
michael@0 10
michael@0 11 #include "GeckoProfiler.h"
michael@0 12
michael@0 13 #include <stddef.h>
michael@0 14
michael@0 15 #if defined(XP_LINUX)
michael@0 16 #include <signal.h>
michael@0 17 #include <semaphore.h>
michael@0 18 #include <sys/types.h>
michael@0 19 #elif defined(XP_WIN)
michael@0 20 #include <windows.h>
michael@0 21 #elif defined(XP_MACOSX)
michael@0 22 #include <mach/mach.h>
michael@0 23 #endif
michael@0 24
michael@0 25 namespace mozilla {
michael@0 26
michael@0 27 /**
michael@0 28 * ThreadStackHelper is used to retrieve the profiler pseudo-stack of a
michael@0 29 * thread, as an alternative of using the profiler to take a profile.
michael@0 30 * The target thread first declares an ThreadStackHelper instance;
michael@0 31 * then another thread can call ThreadStackHelper::GetStack to retrieve
michael@0 32 * the pseudo-stack of the target thread at that instant.
michael@0 33 *
michael@0 34 * Only non-copying labels are included in the stack, which means labels
michael@0 35 * with custom text and markers are not included.
michael@0 36 */
michael@0 37 class ThreadStackHelper
michael@0 38 {
michael@0 39 public:
michael@0 40 typedef Telemetry::HangHistogram::Stack Stack;
michael@0 41
michael@0 42 private:
michael@0 43 #ifdef MOZ_ENABLE_PROFILER_SPS
michael@0 44 const PseudoStack* const mPseudoStack;
michael@0 45 #endif
michael@0 46 Stack mStackBuffer;
michael@0 47 size_t mMaxStackSize;
michael@0 48
michael@0 49 bool PrepareStackBuffer(Stack& aStack);
michael@0 50 void FillStackBuffer();
michael@0 51
michael@0 52 public:
michael@0 53 /**
michael@0 54 * Initialize ThreadStackHelper. Must be called from main thread.
michael@0 55 */
michael@0 56 static void Startup();
michael@0 57 /**
michael@0 58 * Uninitialize ThreadStackHelper. Must be called from main thread.
michael@0 59 */
michael@0 60 static void Shutdown();
michael@0 61
michael@0 62 /**
michael@0 63 * Create a ThreadStackHelper instance targeting the current thread.
michael@0 64 */
michael@0 65 ThreadStackHelper();
michael@0 66
michael@0 67 ~ThreadStackHelper();
michael@0 68
michael@0 69 /**
michael@0 70 * Retrieve the current pseudostack of the thread associated
michael@0 71 * with this ThreadStackHelper.
michael@0 72 *
michael@0 73 * @param aStack Stack instance to be filled.
michael@0 74 */
michael@0 75 void GetStack(Stack& aStack);
michael@0 76
michael@0 77 #if defined(XP_LINUX)
michael@0 78 private:
michael@0 79 static int sInitialized;
michael@0 80 static sem_t sSem;
michael@0 81 static struct sigaction sOldSigAction;
michael@0 82 static ThreadStackHelper* sCurrent;
michael@0 83
michael@0 84 static void SigAction(int aSignal, siginfo_t* aInfo, void* aContext);
michael@0 85
michael@0 86 pid_t mThreadID;
michael@0 87
michael@0 88 #elif defined(XP_WIN)
michael@0 89 private:
michael@0 90 bool mInitialized;
michael@0 91 HANDLE mThreadID;
michael@0 92
michael@0 93 #elif defined(XP_MACOSX)
michael@0 94 private:
michael@0 95 thread_act_t mThreadID;
michael@0 96
michael@0 97 #endif
michael@0 98 };
michael@0 99
michael@0 100 } // namespace mozilla
michael@0 101
michael@0 102 #endif // mozilla_ThreadStackHelper_h

mercurial