Wed, 31 Dec 2014 06:09:35 +0100
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 |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | /* Visual event tracer, creates a log of events on each thread for visualization */ |
michael@0 | 6 | |
michael@0 | 7 | /** |
michael@0 | 8 | * The event tracer code is by default disabled in both build and run time. |
michael@0 | 9 | * |
michael@0 | 10 | * To enable this code at build time, add --enable-visual-profiling to your |
michael@0 | 11 | * configure options. |
michael@0 | 12 | * |
michael@0 | 13 | * To enable this code at run time, export MOZ_TRACE_FILE env var with a |
michael@0 | 14 | * path to the file to write the log to. This is all you need to * produce |
michael@0 | 15 | * the log of all events instrumentation in the mozilla code. Check |
michael@0 | 16 | * MOZ_EVENT_TRACER_* macros below to add your own. |
michael@0 | 17 | * |
michael@0 | 18 | * To let the event tracer log only some events to save disk space, export |
michael@0 | 19 | * MOZ_PROFILING_EVENTS with comma separated list of event names you want |
michael@0 | 20 | * to record in the log. |
michael@0 | 21 | */ |
michael@0 | 22 | |
michael@0 | 23 | #ifndef VisualEventTracer_h___ |
michael@0 | 24 | #define VisualEventTracer_h___ |
michael@0 | 25 | |
michael@0 | 26 | #include <stdint.h> |
michael@0 | 27 | #include "mozilla/Attributes.h" |
michael@0 | 28 | #include "mozilla/GuardObjects.h" |
michael@0 | 29 | |
michael@0 | 30 | #ifdef MOZ_VISUAL_EVENT_TRACER |
michael@0 | 31 | #include "nsIVisualEventTracer.h" |
michael@0 | 32 | |
michael@0 | 33 | // Bind an object instance, usually |this|, to a name, usually URL or |
michael@0 | 34 | // host name, the instance deals with for its lifetime. The name string |
michael@0 | 35 | // is duplicated. |
michael@0 | 36 | // IMPORTANT: it is up to the caller to pass the correct static_cast |
michael@0 | 37 | // of the |instance| pointer to all these macros ; otherwise the linking |
michael@0 | 38 | // of events and objects will not work! |
michael@0 | 39 | // The name will show in details of the events on the timeline and also |
michael@0 | 40 | // will allow events filtering by host names, URLs etc. |
michael@0 | 41 | #define MOZ_EVENT_TRACER_NAME_OBJECT(instance, name) \ |
michael@0 | 42 | mozilla::eventtracer::Mark(mozilla::eventtracer::eName, instance, name) |
michael@0 | 43 | |
michael@0 | 44 | // The same as MOZ_EVENT_TRACER_NAME_OBJECT, just simplifies building |
michael@0 | 45 | // names when it consists of two parts |
michael@0 | 46 | #define MOZ_EVENT_TRACER_COMPOUND_NAME(instance, name, name2) \ |
michael@0 | 47 | mozilla::eventtracer::Mark(mozilla::eventtracer::eName, instance, name, name2) |
michael@0 | 48 | |
michael@0 | 49 | |
michael@0 | 50 | // Call the followings with the same |instance| reference as you have |
michael@0 | 51 | // previously called MOZ_EVENT_TRACER_NAME_OBJECT. |
michael@0 | 52 | // Let |name| be the name of the event happening, like "resolving", |
michael@0 | 53 | // "connecting", "loading" etc. |
michael@0 | 54 | |
michael@0 | 55 | // This will crate a single-point-in-time event marked with an arrow |
michael@0 | 56 | // on the timeline, this is a way to indicate an event without a duration. |
michael@0 | 57 | #define MOZ_EVENT_TRACER_MARK(instance, name) \ |
michael@0 | 58 | mozilla::eventtracer::Mark(mozilla::eventtracer::eShot, instance, name) |
michael@0 | 59 | |
michael@0 | 60 | // Following macros are used to log events with duration. |
michael@0 | 61 | // There always has to be complete WAIT,EXEC,DONE or EXEC,DONE |
michael@0 | 62 | // uninterrupted and non-interferring tuple(s) for an event to be correctly |
michael@0 | 63 | // shown on the timeline. Each can be called on any thread, the event tape is |
michael@0 | 64 | // finally displayed on the thread where it has been EXECuted. |
michael@0 | 65 | |
michael@0 | 66 | // Example of 3 phases usage for "HTTP request channel": |
michael@0 | 67 | // WAIT: we've just created the channel and called AsyncOpen on it |
michael@0 | 68 | // EXEC: we've just got first call to OnDataAvailable, so we are actually |
michael@0 | 69 | // receiving the content after some time like connection establising, |
michael@0 | 70 | // opening a cache entry, sending the GET request... |
michael@0 | 71 | // DONE: we've just got OnStopRequest call that indicates the content |
michael@0 | 72 | // has been completely delivered and the request is now finished |
michael@0 | 73 | |
michael@0 | 74 | // Indicate an event pending start, on the timeline this will |
michael@0 | 75 | // start the event's interval tape with a pale color, the time will |
michael@0 | 76 | // show in details. Usually used when an event is being posted or |
michael@0 | 77 | // we wait for a lock acquisition. |
michael@0 | 78 | // WAITING is not mandatory, some events don't have a pending phase. |
michael@0 | 79 | #define MOZ_EVENT_TRACER_WAIT(instance, name) \ |
michael@0 | 80 | mozilla::eventtracer::Mark(mozilla::eventtracer::eWait, instance, name) |
michael@0 | 81 | |
michael@0 | 82 | // Indicate start of an event actual execution, on the timeline this will |
michael@0 | 83 | // change the event's tape to a deeper color, the time will show in details. |
michael@0 | 84 | #define MOZ_EVENT_TRACER_EXEC(instance, name) \ |
michael@0 | 85 | mozilla::eventtracer::Mark(mozilla::eventtracer::eExec, instance, name) |
michael@0 | 86 | |
michael@0 | 87 | // Indicate the end of an event execution (the event has been done), |
michael@0 | 88 | // on the timeline this will end the event's tape and show the time in |
michael@0 | 89 | // event details. |
michael@0 | 90 | // NOTE: when the event duration is extremely short, like 1ms, it will convert |
michael@0 | 91 | // to an event w/o a duration - the same as MOZ_EVENT_TRACER_MARK would be used. |
michael@0 | 92 | #define MOZ_EVENT_TRACER_DONE(instance, name) \ |
michael@0 | 93 | mozilla::eventtracer::Mark(mozilla::eventtracer::eDone, instance, name) |
michael@0 | 94 | |
michael@0 | 95 | // The same meaning as the above macros, just for concurent events. |
michael@0 | 96 | // Concurent event means it can happen for the same instance on more |
michael@0 | 97 | // then just a single thread, e.g. a service method call, a global lock |
michael@0 | 98 | // acquisition, enter and release. |
michael@0 | 99 | #define MOZ_EVENT_TRACER_WAIT_THREADSAFE(instance, name) \ |
michael@0 | 100 | mozilla::eventtracer::Mark(mozilla::eventtracer::eWait | mozilla::eventtracer::eThreadConcurrent, instance, name) |
michael@0 | 101 | #define MOZ_EVENT_TRACER_EXEC_THREADSAFE(instance, name) \ |
michael@0 | 102 | mozilla::eventtracer::Mark(mozilla::eventtracer::eExec | mozilla::eventtracer::eThreadConcurrent, instance, name) |
michael@0 | 103 | #define MOZ_EVENT_TRACER_DONE_THREASAFE(instance, name) \ |
michael@0 | 104 | mozilla::eventtracer::Mark(mozilla::eventtracer::eDone | mozilla::eventtracer::eThreadConcurrent, instance, name) |
michael@0 | 105 | |
michael@0 | 106 | #else |
michael@0 | 107 | |
michael@0 | 108 | // MOZ_VISUAL_EVENT_TRACER disabled |
michael@0 | 109 | |
michael@0 | 110 | #define MOZ_EVENT_TRACER_NAME_OBJECT(instance, name) (void)0 |
michael@0 | 111 | #define MOZ_EVENT_TRACER_COMPOUND_NAME(instance, name, name2) (void)0 |
michael@0 | 112 | #define MOZ_EVENT_TRACER_MARK(instance, name) (void)0 |
michael@0 | 113 | #define MOZ_EVENT_TRACER_WAIT(instance, name) (void)0 |
michael@0 | 114 | #define MOZ_EVENT_TRACER_EXEC(instance, name) (void)0 |
michael@0 | 115 | #define MOZ_EVENT_TRACER_DONE(instance, name) (void)0 |
michael@0 | 116 | #define MOZ_EVENT_TRACER_WAIT_THREADSAFE(instance, name) (void)0 |
michael@0 | 117 | #define MOZ_EVENT_TRACER_EXEC_THREADSAFE(instance, name) (void)0 |
michael@0 | 118 | #define MOZ_EVENT_TRACER_DONE_THREASAFE(instance, name) (void)0 |
michael@0 | 119 | |
michael@0 | 120 | #endif |
michael@0 | 121 | |
michael@0 | 122 | |
michael@0 | 123 | namespace mozilla { |
michael@0 | 124 | namespace eventtracer { |
michael@0 | 125 | |
michael@0 | 126 | // Initialize the event tracer engine, called automatically on XPCOM startup. |
michael@0 | 127 | void Init(); |
michael@0 | 128 | |
michael@0 | 129 | // Shuts the event tracer engine down and closes the log file, called |
michael@0 | 130 | // automatically during XPCOM shutdown. |
michael@0 | 131 | void Shutdown(); |
michael@0 | 132 | |
michael@0 | 133 | enum MarkType { |
michael@0 | 134 | eNone, // No operation, ignored |
michael@0 | 135 | eName, // This is used to bind an object instance with a name |
michael@0 | 136 | |
michael@0 | 137 | eShot, // An event with no duration |
michael@0 | 138 | eWait, // Start of waiting for execution (lock acquire, post...) |
michael@0 | 139 | eExec, // Start of the execution it self |
michael@0 | 140 | eDone, // End of the execution |
michael@0 | 141 | eLast, // Sentinel |
michael@0 | 142 | |
michael@0 | 143 | // Flags |
michael@0 | 144 | |
michael@0 | 145 | // The same object can execute the same event on several threads concurently |
michael@0 | 146 | eThreadConcurrent = 0x10000 |
michael@0 | 147 | }; |
michael@0 | 148 | |
michael@0 | 149 | // Records an event on the calling thread. |
michael@0 | 150 | // @param aType |
michael@0 | 151 | // One of MarkType fields, can be bitwise or'ed with the flags. |
michael@0 | 152 | // @param aItem |
michael@0 | 153 | // Reference to the object we want to bind a name to or the event is |
michael@0 | 154 | // happening on. Can be actually anything, but valid poitners should |
michael@0 | 155 | // be used. |
michael@0 | 156 | // @param aText |
michael@0 | 157 | // Text of the name (for eName) or event's name for others. The string |
michael@0 | 158 | // is duplicated. |
michael@0 | 159 | // @param aText2 |
michael@0 | 160 | // Optional second part of the instnace name, or event name. |
michael@0 | 161 | // Event filtering does apply only to the first part (aText). |
michael@0 | 162 | void Mark(uint32_t aType, void * aItem, |
michael@0 | 163 | const char * aText, const char * aText2 = 0); |
michael@0 | 164 | |
michael@0 | 165 | |
michael@0 | 166 | // Helper guard object. Use to mark an event in the constructor and a different |
michael@0 | 167 | // event in the destructor. |
michael@0 | 168 | // |
michael@0 | 169 | // Example: |
michael@0 | 170 | // int class::func() |
michael@0 | 171 | // { |
michael@0 | 172 | // AutoEventTracer tracer(this, eventtracer::eExec, eventtracer::eDone, "func"); |
michael@0 | 173 | // |
michael@0 | 174 | // do_something_taking_a_long_time(); |
michael@0 | 175 | // } |
michael@0 | 176 | class MOZ_STACK_CLASS AutoEventTracer |
michael@0 | 177 | { |
michael@0 | 178 | public: |
michael@0 | 179 | AutoEventTracer(void * aInstance, |
michael@0 | 180 | uint32_t aTypeOn, // MarkType marked in constructor |
michael@0 | 181 | uint32_t aTypeOff, // MarkType marked in destructor |
michael@0 | 182 | const char * aName, |
michael@0 | 183 | const char * aName2 = 0 |
michael@0 | 184 | MOZ_GUARD_OBJECT_NOTIFIER_PARAM) |
michael@0 | 185 | : mInstance(aInstance) |
michael@0 | 186 | , mName(aName) |
michael@0 | 187 | , mName2(aName2) |
michael@0 | 188 | , mTypeOn(aTypeOn) |
michael@0 | 189 | , mTypeOff(aTypeOff) |
michael@0 | 190 | { |
michael@0 | 191 | MOZ_GUARD_OBJECT_NOTIFIER_INIT; |
michael@0 | 192 | |
michael@0 | 193 | ::mozilla::eventtracer::Mark(mTypeOn, mInstance, mName, mName2); |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | ~AutoEventTracer() |
michael@0 | 197 | { |
michael@0 | 198 | ::mozilla::eventtracer::Mark(mTypeOff, mInstance, mName, mName2); |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | private: |
michael@0 | 202 | void * mInstance; |
michael@0 | 203 | const char * mName; |
michael@0 | 204 | const char * mName2; |
michael@0 | 205 | uint32_t mTypeOn; |
michael@0 | 206 | uint32_t mTypeOff; |
michael@0 | 207 | |
michael@0 | 208 | MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER |
michael@0 | 209 | }; |
michael@0 | 210 | |
michael@0 | 211 | #ifdef MOZ_VISUAL_EVENT_TRACER |
michael@0 | 212 | |
michael@0 | 213 | // The scriptable class that drives the event tracer |
michael@0 | 214 | |
michael@0 | 215 | class VisualEventTracer : public nsIVisualEventTracer |
michael@0 | 216 | { |
michael@0 | 217 | NS_DECL_ISUPPORTS |
michael@0 | 218 | NS_DECL_NSIVISUALEVENTTRACER |
michael@0 | 219 | }; |
michael@0 | 220 | |
michael@0 | 221 | #define NS_VISUALEVENTTRACER_CID \ |
michael@0 | 222 | { 0xb9e5e102, 0xc2f4, 0x497a, \ |
michael@0 | 223 | { 0xa6, 0xe4, 0x54, 0xde, 0xf3, 0x71, 0xf3, 0x9d } } |
michael@0 | 224 | #define NS_VISUALEVENTTRACER_CONTRACTID "@mozilla.org/base/visual-event-tracer;1" |
michael@0 | 225 | #define NS_VISUALEVENTTRACER_CLASSNAME "Visual Event Tracer" |
michael@0 | 226 | |
michael@0 | 227 | #endif |
michael@0 | 228 | |
michael@0 | 229 | } // eventtracer |
michael@0 | 230 | } // mozilla |
michael@0 | 231 | |
michael@0 | 232 | #endif /* VisualEventTracer_h___ */ |