js/src/devtools/Instruments.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "Instruments.h"
michael@0 6
michael@0 7 #ifdef __APPLE__
michael@0 8
michael@0 9 #include <dlfcn.h>
michael@0 10 #include <CoreFoundation/CoreFoundation.h>
michael@0 11 #include <unistd.h>
michael@0 12
michael@0 13 // There are now 2 paths to the DTPerformanceSession framework. We try to load
michael@0 14 // the one contained in /Applications/Xcode.app first, falling back to the one
michael@0 15 // contained in /Library/Developer/4.0/Instruments.
michael@0 16 #define DTPerformanceLibraryPath "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/DTPerformanceSession.framework/Versions/Current/DTPerformanceSession"
michael@0 17 #define OldDTPerformanceLibraryPath "/Library/Developer/4.0/Instruments/Frameworks/DTPerformanceSession.framework/Versions/Current/DTPerformanceSession"
michael@0 18
michael@0 19 extern "C" {
michael@0 20
michael@0 21 typedef CFTypeRef DTPerformanceSessionRef;
michael@0 22
michael@0 23 #define DTPerformanceSession_TimeProfiler "com.apple.instruments.dtps.timeprofiler"
michael@0 24 // DTPerformanceSession_Option_SamplingInterval is measured in microseconds
michael@0 25 #define DTPerformanceSession_Option_SamplingInterval "com.apple.instruments.dtps.option.samplinginterval"
michael@0 26
michael@0 27 typedef void (*dtps_errorcallback_t)(CFStringRef, CFErrorRef);
michael@0 28 typedef DTPerformanceSessionRef (*DTPerformanceSessionCreateFunction)(CFStringRef, CFStringRef, CFDictionaryRef, CFErrorRef*);
michael@0 29 typedef bool (*DTPerformanceSessionAddInstrumentFunction)(DTPerformanceSessionRef, CFStringRef, CFDictionaryRef, dtps_errorcallback_t, CFErrorRef*);
michael@0 30 typedef bool (*DTPerformanceSessionIsRecordingFunction)(DTPerformanceSessionRef);
michael@0 31 typedef bool (*DTPerformanceSessionStartFunction)(DTPerformanceSessionRef, CFArrayRef, CFErrorRef*);
michael@0 32 typedef bool (*DTPerformanceSessionStopFunction)(DTPerformanceSessionRef, CFArrayRef, CFErrorRef*);
michael@0 33 typedef bool (*DTPerformanceSessionSaveFunction)(DTPerformanceSessionRef, CFStringRef, CFErrorRef*);
michael@0 34
michael@0 35 } // extern "C"
michael@0 36
michael@0 37 namespace Instruments {
michael@0 38
michael@0 39 static const int kSamplingInterval = 20; // microseconds
michael@0 40
michael@0 41 template<typename T>
michael@0 42 class AutoReleased
michael@0 43 {
michael@0 44 public:
michael@0 45 AutoReleased(T aTypeRef) : mTypeRef(aTypeRef)
michael@0 46 {
michael@0 47 }
michael@0 48 ~AutoReleased()
michael@0 49 {
michael@0 50 if (mTypeRef) {
michael@0 51 CFRelease(mTypeRef);
michael@0 52 }
michael@0 53 }
michael@0 54
michael@0 55 operator T()
michael@0 56 {
michael@0 57 return mTypeRef;
michael@0 58 }
michael@0 59
michael@0 60 private:
michael@0 61 T mTypeRef;
michael@0 62 };
michael@0 63
michael@0 64 #define DTPERFORMANCE_SYMBOLS \
michael@0 65 SYMBOL(DTPerformanceSessionCreate) \
michael@0 66 SYMBOL(DTPerformanceSessionAddInstrument) \
michael@0 67 SYMBOL(DTPerformanceSessionIsRecording) \
michael@0 68 SYMBOL(DTPerformanceSessionStart) \
michael@0 69 SYMBOL(DTPerformanceSessionStop) \
michael@0 70 SYMBOL(DTPerformanceSessionSave)
michael@0 71
michael@0 72 #define SYMBOL(_sym) \
michael@0 73 _sym##Function _sym = nullptr;
michael@0 74
michael@0 75 DTPERFORMANCE_SYMBOLS
michael@0 76
michael@0 77 #undef SYMBOL
michael@0 78
michael@0 79 void*
michael@0 80 LoadDTPerformanceLibraries(bool dontLoad)
michael@0 81 {
michael@0 82 int flags = RTLD_LAZY | RTLD_LOCAL | RTLD_NODELETE;
michael@0 83 if (dontLoad) {
michael@0 84 flags |= RTLD_NOLOAD;
michael@0 85 }
michael@0 86
michael@0 87 void *DTPerformanceLibrary = dlopen(DTPerformanceLibraryPath, flags);
michael@0 88 if (!DTPerformanceLibrary) {
michael@0 89 DTPerformanceLibrary = dlopen(OldDTPerformanceLibraryPath, flags);
michael@0 90 }
michael@0 91 return DTPerformanceLibrary;
michael@0 92 }
michael@0 93
michael@0 94 bool
michael@0 95 LoadDTPerformanceLibrary()
michael@0 96 {
michael@0 97 void *DTPerformanceLibrary = LoadDTPerformanceLibraries(true);
michael@0 98 if (!DTPerformanceLibrary) {
michael@0 99 DTPerformanceLibrary = LoadDTPerformanceLibraries(false);
michael@0 100 if (!DTPerformanceLibrary) {
michael@0 101 return false;
michael@0 102 }
michael@0 103 }
michael@0 104
michael@0 105 #define SYMBOL(_sym) \
michael@0 106 _sym = reinterpret_cast<_sym##Function>(dlsym(DTPerformanceLibrary, #_sym)); \
michael@0 107 if (!_sym) { \
michael@0 108 dlclose(DTPerformanceLibrary); \
michael@0 109 DTPerformanceLibrary = nullptr; \
michael@0 110 return false; \
michael@0 111 }
michael@0 112
michael@0 113 DTPERFORMANCE_SYMBOLS
michael@0 114
michael@0 115 #undef SYMBOL
michael@0 116
michael@0 117 dlclose(DTPerformanceLibrary);
michael@0 118
michael@0 119 return true;
michael@0 120 }
michael@0 121
michael@0 122 static DTPerformanceSessionRef gSession;
michael@0 123
michael@0 124 bool
michael@0 125 Error(CFErrorRef error)
michael@0 126 {
michael@0 127 if (gSession) {
michael@0 128 CFErrorRef unused = nullptr;
michael@0 129 DTPerformanceSessionStop(gSession, nullptr, &unused);
michael@0 130 CFRelease(gSession);
michael@0 131 gSession = nullptr;
michael@0 132 }
michael@0 133 #ifdef DEBUG
michael@0 134 AutoReleased<CFDataRef> data =
michael@0 135 CFStringCreateExternalRepresentation(nullptr,
michael@0 136 CFErrorCopyDescription(error),
michael@0 137 kCFStringEncodingUTF8, '?');
michael@0 138 if (data != nullptr) {
michael@0 139 printf("%.*s\n\n", (int)CFDataGetLength(data), CFDataGetBytePtr(data));
michael@0 140 }
michael@0 141 #endif
michael@0 142 return false;
michael@0 143 }
michael@0 144
michael@0 145 bool
michael@0 146 Start(pid_t pid)
michael@0 147 {
michael@0 148 if (gSession) {
michael@0 149 return false;
michael@0 150 }
michael@0 151
michael@0 152 if (!LoadDTPerformanceLibrary()) {
michael@0 153 return false;
michael@0 154 }
michael@0 155
michael@0 156 AutoReleased<CFStringRef> process =
michael@0 157 CFStringCreateWithFormat(kCFAllocatorDefault, nullptr, CFSTR("%d"), pid);
michael@0 158 if (!process) {
michael@0 159 return false;
michael@0 160 }
michael@0 161 CFErrorRef error = nullptr;
michael@0 162 gSession = DTPerformanceSessionCreate(nullptr, process, nullptr, &error);
michael@0 163 if (!gSession) {
michael@0 164 return Error(error);
michael@0 165 }
michael@0 166
michael@0 167 AutoReleased<CFNumberRef> interval =
michael@0 168 CFNumberCreate(0, kCFNumberIntType, &kSamplingInterval);
michael@0 169 if (!interval) {
michael@0 170 return false;
michael@0 171 }
michael@0 172 CFStringRef keys[1] = { CFSTR(DTPerformanceSession_Option_SamplingInterval) };
michael@0 173 CFNumberRef values[1] = { interval };
michael@0 174 AutoReleased<CFDictionaryRef> options =
michael@0 175 CFDictionaryCreate(kCFAllocatorDefault, (const void**)keys,
michael@0 176 (const void**)values, 1, &kCFTypeDictionaryKeyCallBacks,
michael@0 177 &kCFTypeDictionaryValueCallBacks);
michael@0 178 if (!options) {
michael@0 179 return false;
michael@0 180 }
michael@0 181
michael@0 182 if (!DTPerformanceSessionAddInstrument(gSession,
michael@0 183 CFSTR(DTPerformanceSession_TimeProfiler),
michael@0 184 options, nullptr, &error)) {
michael@0 185 return Error(error);
michael@0 186 }
michael@0 187
michael@0 188 return Resume();
michael@0 189 }
michael@0 190
michael@0 191 void
michael@0 192 Pause()
michael@0 193 {
michael@0 194 if (gSession && DTPerformanceSessionIsRecording(gSession)) {
michael@0 195 CFErrorRef error = nullptr;
michael@0 196 if (!DTPerformanceSessionStop(gSession, nullptr, &error)) {
michael@0 197 Error(error);
michael@0 198 }
michael@0 199 }
michael@0 200 }
michael@0 201
michael@0 202 bool
michael@0 203 Resume()
michael@0 204 {
michael@0 205 if (!gSession) {
michael@0 206 return false;
michael@0 207 }
michael@0 208
michael@0 209 CFErrorRef error = nullptr;
michael@0 210 return DTPerformanceSessionStart(gSession, nullptr, &error) ||
michael@0 211 Error(error);
michael@0 212 }
michael@0 213
michael@0 214 void
michael@0 215 Stop(const char* profileName)
michael@0 216 {
michael@0 217 Pause();
michael@0 218
michael@0 219 CFErrorRef error = nullptr;
michael@0 220 AutoReleased<CFStringRef> name =
michael@0 221 CFStringCreateWithFormat(kCFAllocatorDefault, nullptr, CFSTR("%s%s"),
michael@0 222 "/tmp/", profileName ? profileName : "mozilla");
michael@0 223 if (!DTPerformanceSessionSave(gSession, name, &error)) {
michael@0 224 Error(error);
michael@0 225 return;
michael@0 226 }
michael@0 227
michael@0 228 CFRelease(gSession);
michael@0 229 gSession = nullptr;
michael@0 230 }
michael@0 231
michael@0 232 } // namespace Instruments
michael@0 233
michael@0 234 #endif /* __APPLE__ */

mercurial