1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #include "node_counters.h"
30 using v8::FunctionCallbackInfo;
31 using v8::FunctionTemplate;
32 using v8::GCCallbackFlags;
33 using v8::GCEpilogueCallback;
34 using v8::GCPrologueCallback;
37 using v8::HandleScope;
43 static uint64_t counter_gc_start_time;
44 static uint64_t counter_gc_end_time;
47 void COUNTER_NET_SERVER_CONNECTION(const FunctionCallbackInfo<Value>&) {
48 NODE_COUNT_SERVER_CONN_OPEN();
52 void COUNTER_NET_SERVER_CONNECTION_CLOSE(const FunctionCallbackInfo<Value>&) {
53 NODE_COUNT_SERVER_CONN_CLOSE();
57 void COUNTER_HTTP_SERVER_REQUEST(const FunctionCallbackInfo<Value>&) {
58 NODE_COUNT_HTTP_SERVER_REQUEST();
62 void COUNTER_HTTP_SERVER_RESPONSE(const FunctionCallbackInfo<Value>&) {
63 NODE_COUNT_HTTP_SERVER_RESPONSE();
67 void COUNTER_HTTP_CLIENT_REQUEST(const FunctionCallbackInfo<Value>&) {
68 NODE_COUNT_HTTP_CLIENT_REQUEST();
72 void COUNTER_HTTP_CLIENT_RESPONSE(const FunctionCallbackInfo<Value>&) {
73 NODE_COUNT_HTTP_CLIENT_RESPONSE();
77 static void counter_gc_start(GCType type, GCCallbackFlags flags) {
78 counter_gc_start_time = NODE_COUNT_GET_GC_RAWTIME();
82 static void counter_gc_done(GCType type, GCCallbackFlags flags) {
83 uint64_t endgc = NODE_COUNT_GET_GC_RAWTIME();
85 uint64_t totalperiod = endgc - counter_gc_end_time;
86 uint64_t gcperiod = endgc - counter_gc_start_time;
88 if (totalperiod > 0) {
89 unsigned int percent = static_cast<unsigned int>(
90 (gcperiod * 100) / totalperiod);
92 NODE_COUNT_GC_PERCENTTIME(percent);
93 counter_gc_end_time = endgc;
99 void InitPerfCounters(Handle<Object> target) {
100 HandleScope scope(node_isolate);
104 void (*func)(const FunctionCallbackInfo<Value>&);
106 #define NODE_PROBE(name) #name, name
107 { NODE_PROBE(COUNTER_NET_SERVER_CONNECTION) },
108 { NODE_PROBE(COUNTER_NET_SERVER_CONNECTION_CLOSE) },
109 { NODE_PROBE(COUNTER_HTTP_SERVER_REQUEST) },
110 { NODE_PROBE(COUNTER_HTTP_SERVER_RESPONSE) },
111 { NODE_PROBE(COUNTER_HTTP_CLIENT_REQUEST) },
112 { NODE_PROBE(COUNTER_HTTP_CLIENT_RESPONSE) }
116 for (int i = 0; i < ARRAY_SIZE(tab); i++) {
117 Local<String> key = OneByteString(node_isolate, tab[i].name);
118 Local<Value> val = FunctionTemplate::New(tab[i].func)->GetFunction();
119 target->Set(key, val);
122 // Only Windows performance counters supported
123 // To enable other OS, use conditional compilation here
124 InitPerfCountersWin32();
126 // init times for GC percent calculation and hook callbacks
127 counter_gc_start_time = NODE_COUNT_GET_GC_RAWTIME();
128 counter_gc_end_time = counter_gc_start_time;
130 v8::V8::AddGCPrologueCallback(counter_gc_start);
131 v8::V8::AddGCEpilogueCallback(counter_gc_done);
135 void TermPerfCounters(Handle<Object> target) {
136 // Only Windows performance counters supported
137 // To enable other OS, use conditional compilation here
138 TermPerfCountersWin32();