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 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
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 perf_jsperf_h |
michael@0 | 7 | #define perf_jsperf_h |
michael@0 | 8 | |
michael@0 | 9 | #include "jstypes.h" |
michael@0 | 10 | |
michael@0 | 11 | #include "js/TypeDecls.h" |
michael@0 | 12 | #include "js/Utility.h" |
michael@0 | 13 | |
michael@0 | 14 | namespace JS { |
michael@0 | 15 | |
michael@0 | 16 | /* |
michael@0 | 17 | * JS::PerfMeasurement is a generic way to access detailed performance |
michael@0 | 18 | * measurement APIs provided by your operating system. The details of |
michael@0 | 19 | * exactly how this works and what can be measured are highly |
michael@0 | 20 | * system-specific, but this interface is (one hopes) implementable |
michael@0 | 21 | * on top of all of them. |
michael@0 | 22 | * |
michael@0 | 23 | * To use this API, create a PerfMeasurement object, passing its |
michael@0 | 24 | * constructor a bitmask indicating which events you are interested |
michael@0 | 25 | * in. Thereafter, Start() zeroes all counters and starts timing; |
michael@0 | 26 | * Stop() stops timing again; and the counters for the events you |
michael@0 | 27 | * requested are available as data values after calling Stop(). The |
michael@0 | 28 | * object may be reused for many measurements. |
michael@0 | 29 | */ |
michael@0 | 30 | class JS_FRIEND_API(PerfMeasurement) |
michael@0 | 31 | { |
michael@0 | 32 | protected: |
michael@0 | 33 | // Implementation-specific data, if any. |
michael@0 | 34 | void* impl; |
michael@0 | 35 | |
michael@0 | 36 | public: |
michael@0 | 37 | /* |
michael@0 | 38 | * Events that may be measured. Taken directly from the list of |
michael@0 | 39 | * "generalized hardware performance event types" in the Linux |
michael@0 | 40 | * perf_event API, plus some of the "software events". |
michael@0 | 41 | */ |
michael@0 | 42 | enum EventMask { |
michael@0 | 43 | CPU_CYCLES = 0x00000001, |
michael@0 | 44 | INSTRUCTIONS = 0x00000002, |
michael@0 | 45 | CACHE_REFERENCES = 0x00000004, |
michael@0 | 46 | CACHE_MISSES = 0x00000008, |
michael@0 | 47 | BRANCH_INSTRUCTIONS = 0x00000010, |
michael@0 | 48 | BRANCH_MISSES = 0x00000020, |
michael@0 | 49 | BUS_CYCLES = 0x00000040, |
michael@0 | 50 | PAGE_FAULTS = 0x00000080, |
michael@0 | 51 | MAJOR_PAGE_FAULTS = 0x00000100, |
michael@0 | 52 | CONTEXT_SWITCHES = 0x00000200, |
michael@0 | 53 | CPU_MIGRATIONS = 0x00000400, |
michael@0 | 54 | |
michael@0 | 55 | ALL = 0x000007ff, |
michael@0 | 56 | NUM_MEASURABLE_EVENTS = 11 |
michael@0 | 57 | }; |
michael@0 | 58 | |
michael@0 | 59 | /* |
michael@0 | 60 | * Bitmask of events that will be measured when this object is |
michael@0 | 61 | * active (between Start() and Stop()). This may differ from the |
michael@0 | 62 | * bitmask passed to the constructor if the platform does not |
michael@0 | 63 | * support measuring all of the requested events. |
michael@0 | 64 | */ |
michael@0 | 65 | const EventMask eventsMeasured; |
michael@0 | 66 | |
michael@0 | 67 | /* |
michael@0 | 68 | * Counters for each measurable event. |
michael@0 | 69 | * Immediately after one of these objects is created, all of the |
michael@0 | 70 | * counters for enabled events will be zero, and all of the |
michael@0 | 71 | * counters for disabled events will be uint64_t(-1). |
michael@0 | 72 | */ |
michael@0 | 73 | uint64_t cpu_cycles; |
michael@0 | 74 | uint64_t instructions; |
michael@0 | 75 | uint64_t cache_references; |
michael@0 | 76 | uint64_t cache_misses; |
michael@0 | 77 | uint64_t branch_instructions; |
michael@0 | 78 | uint64_t branch_misses; |
michael@0 | 79 | uint64_t bus_cycles; |
michael@0 | 80 | uint64_t page_faults; |
michael@0 | 81 | uint64_t major_page_faults; |
michael@0 | 82 | uint64_t context_switches; |
michael@0 | 83 | uint64_t cpu_migrations; |
michael@0 | 84 | |
michael@0 | 85 | /* |
michael@0 | 86 | * Prepare to measure the indicated set of events. If not all of |
michael@0 | 87 | * the requested events can be measured on the current platform, |
michael@0 | 88 | * then the eventsMeasured bitmask will only include the subset of |
michael@0 | 89 | * |toMeasure| corresponding to the events that can be measured. |
michael@0 | 90 | */ |
michael@0 | 91 | PerfMeasurement(EventMask toMeasure); |
michael@0 | 92 | |
michael@0 | 93 | /* Done with this set of measurements, tear down OS-level state. */ |
michael@0 | 94 | ~PerfMeasurement(); |
michael@0 | 95 | |
michael@0 | 96 | /* Start a measurement cycle. */ |
michael@0 | 97 | void start(); |
michael@0 | 98 | |
michael@0 | 99 | /* |
michael@0 | 100 | * End a measurement cycle, and for each enabled counter, add the |
michael@0 | 101 | * number of measured events of that type to the appropriate |
michael@0 | 102 | * visible variable. |
michael@0 | 103 | */ |
michael@0 | 104 | void stop(); |
michael@0 | 105 | |
michael@0 | 106 | /* Reset all enabled counters to zero. */ |
michael@0 | 107 | void reset(); |
michael@0 | 108 | |
michael@0 | 109 | /* |
michael@0 | 110 | * True if this platform supports measuring _something_, i.e. it's |
michael@0 | 111 | * not using the stub implementation. |
michael@0 | 112 | */ |
michael@0 | 113 | static bool canMeasureSomething(); |
michael@0 | 114 | }; |
michael@0 | 115 | |
michael@0 | 116 | /* Inject a Javascript wrapper around the above C++ class into the |
michael@0 | 117 | * Javascript object passed as an argument (this will normally be a |
michael@0 | 118 | * global object). The JS-visible API is identical to the C++ API. |
michael@0 | 119 | */ |
michael@0 | 120 | extern JS_FRIEND_API(JSObject*) |
michael@0 | 121 | RegisterPerfMeasurement(JSContext *cx, JS::HandleObject global); |
michael@0 | 122 | |
michael@0 | 123 | /* |
michael@0 | 124 | * Given a Value which contains an instance of the aforementioned |
michael@0 | 125 | * wrapper class, extract the C++ object. Returns nullptr if the |
michael@0 | 126 | * Value is not an instance of the wrapper. |
michael@0 | 127 | */ |
michael@0 | 128 | extern JS_FRIEND_API(PerfMeasurement*) |
michael@0 | 129 | ExtractPerfMeasurement(Value wrapper); |
michael@0 | 130 | |
michael@0 | 131 | } // namespace JS |
michael@0 | 132 | |
michael@0 | 133 | #endif /* perf_jsperf_h */ |