OLD | NEW |
1 // Copyright 2007-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2007-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 13 matching lines...) Expand all Loading... |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #ifndef V8_COUNTERS_H_ | 28 #ifndef V8_COUNTERS_H_ |
29 #define V8_COUNTERS_H_ | 29 #define V8_COUNTERS_H_ |
30 | 30 |
31 namespace v8 { | 31 namespace v8 { |
32 namespace internal { | 32 namespace internal { |
33 | 33 |
| 34 class StatsTableData { |
| 35 CounterLookupCallback lookup_function_; |
| 36 CreateHistogramCallback create_histogram_function_; |
| 37 AddHistogramSampleCallback add_histogram_sample_function_; |
| 38 StatsTableData(); |
| 39 |
| 40 friend class StatsTable; |
| 41 friend class V8Context; |
| 42 DISALLOW_COPY_AND_ASSIGN(StatsTableData); |
| 43 }; |
| 44 |
34 // StatsCounters is an interface for plugging into external | 45 // StatsCounters is an interface for plugging into external |
35 // counters for monitoring. Counters can be looked up and | 46 // counters for monitoring. Counters can be looked up and |
36 // manipulated by name. | 47 // manipulated by name. |
37 | 48 |
38 class StatsTable : public AllStatic { | 49 class StatsTable : public AllStatic { |
39 public: | 50 public: |
40 // Register an application-defined function where | 51 // Register an application-defined function where |
41 // counters can be looked up. | 52 // counters can be looked up. |
42 static void SetCounterFunction(CounterLookupCallback f) { | 53 static void SetCounterFunction(CounterLookupCallback f) { |
43 lookup_function_ = f; | 54 v8_context()->stats_table_data_.lookup_function_ = f; |
44 } | 55 } |
45 | 56 |
46 // Register an application-defined function to create | 57 // Register an application-defined function to create |
47 // a histogram for passing to the AddHistogramSample function | 58 // a histogram for passing to the AddHistogramSample function |
48 static void SetCreateHistogramFunction(CreateHistogramCallback f) { | 59 static void SetCreateHistogramFunction(CreateHistogramCallback f) { |
49 create_histogram_function_ = f; | 60 v8_context()->stats_table_data_.create_histogram_function_ = f; |
50 } | 61 } |
51 | 62 |
52 // Register an application-defined function to add a sample | 63 // Register an application-defined function to add a sample |
53 // to a histogram created with CreateHistogram function | 64 // to a histogram created with CreateHistogram function |
54 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback f) { | 65 static void SetAddHistogramSampleFunction(AddHistogramSampleCallback f) { |
55 add_histogram_sample_function_ = f; | 66 v8_context()->stats_table_data_.add_histogram_sample_function_ = f; |
56 } | 67 } |
57 | 68 |
58 static bool HasCounterFunction() { | 69 static bool HasCounterFunction() { |
59 return lookup_function_ != NULL; | 70 return v8_context()->stats_table_data_.lookup_function_ != NULL; |
60 } | 71 } |
61 | 72 |
62 // Lookup the location of a counter by name. If the lookup | 73 // Lookup the location of a counter by name. If the lookup |
63 // is successful, returns a non-NULL pointer for writing the | 74 // is successful, returns a non-NULL pointer for writing the |
64 // value of the counter. Each thread calling this function | 75 // value of the counter. Each thread calling this function |
65 // may receive a different location to store it's counter. | 76 // may receive a different location to store it's counter. |
66 // The return value must not be cached and re-used across | 77 // The return value must not be cached and re-used across |
67 // threads, although a single thread is free to cache it. | 78 // threads, although a single thread is free to cache it. |
68 static int *FindLocation(const char* name) { | 79 static int *FindLocation(const char* name) { |
69 if (!lookup_function_) return NULL; | 80 CounterLookupCallback lookup_function = |
70 return lookup_function_(name); | 81 v8_context()->stats_table_data_.lookup_function_; |
| 82 if (!lookup_function) return NULL; |
| 83 return lookup_function(name); |
71 } | 84 } |
72 | 85 |
73 // Create a histogram by name. If the create is successful, | 86 // Create a histogram by name. If the create is successful, |
74 // returns a non-NULL pointer for use with AddHistogramSample | 87 // returns a non-NULL pointer for use with AddHistogramSample |
75 // function. min and max define the expected minimum and maximum | 88 // function. min and max define the expected minimum and maximum |
76 // sample values. buckets is the maximum number of buckets | 89 // sample values. buckets is the maximum number of buckets |
77 // that the samples will be grouped into. | 90 // that the samples will be grouped into. |
78 static void* CreateHistogram(const char* name, | 91 static void* CreateHistogram(const char* name, |
79 int min, | 92 int min, |
80 int max, | 93 int max, |
81 size_t buckets) { | 94 size_t buckets) { |
82 if (!create_histogram_function_) return NULL; | 95 CreateHistogramCallback create_histogram_function = |
83 return create_histogram_function_(name, min, max, buckets); | 96 v8_context()->stats_table_data_.create_histogram_function_; |
| 97 if (!create_histogram_function) return NULL; |
| 98 return create_histogram_function(name, min, max, buckets); |
84 } | 99 } |
85 | 100 |
86 // Add a sample to a histogram created with the CreateHistogram | 101 // Add a sample to a histogram created with the CreateHistogram |
87 // function. | 102 // function. |
88 static void AddHistogramSample(void* histogram, int sample) { | 103 static void AddHistogramSample(void* histogram, int sample) { |
89 if (!add_histogram_sample_function_) return; | 104 AddHistogramSampleCallback add_histogram_sample_function = |
90 return add_histogram_sample_function_(histogram, sample); | 105 v8_context()->stats_table_data_.add_histogram_sample_function_; |
| 106 if (!add_histogram_sample_function) return; |
| 107 return add_histogram_sample_function(histogram, sample); |
91 } | 108 } |
92 | |
93 private: | |
94 static CounterLookupCallback lookup_function_; | |
95 static CreateHistogramCallback create_histogram_function_; | |
96 static AddHistogramSampleCallback add_histogram_sample_function_; | |
97 }; | 109 }; |
98 | 110 |
99 // StatsCounters are dynamically created values which can be tracked in | 111 // StatsCounters are dynamically created values which can be tracked in |
100 // the StatsTable. They are designed to be lightweight to create and | 112 // the StatsTable. They are designed to be lightweight to create and |
101 // easy to use. | 113 // easy to use. |
102 // | 114 // |
103 // Internally, a counter represents a value in a row of a StatsTable. | 115 // Internally, a counter represents a value in a row of a StatsTable. |
104 // The row has a 32bit value for each process/thread in the table and also | 116 // The row has a 32bit value for each process/thread in the table and also |
105 // a name (stored in the table metadata). Since the storage location can be | 117 // a name (stored in the table metadata). Since the storage location can be |
106 // thread-specific, this class cannot be shared across threads. | 118 // thread-specific, this class cannot be shared across threads. |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 timer_->Stop(); | 242 timer_->Stop(); |
231 } | 243 } |
232 private: | 244 private: |
233 HistogramTimer* timer_; | 245 HistogramTimer* timer_; |
234 }; | 246 }; |
235 | 247 |
236 | 248 |
237 } } // namespace v8::internal | 249 } } // namespace v8::internal |
238 | 250 |
239 #endif // V8_COUNTERS_H_ | 251 #endif // V8_COUNTERS_H_ |
OLD | NEW |