Merge remote-tracking branch 'ry/v0.10'
[platform/upstream/nodejs.git] / src / node_counters.cc
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
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:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
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.
21
22 #include "node_counters.h"
23 #include "uv.h"
24
25 #include <string.h>
26
27
28 namespace node {
29
30 using v8::FunctionCallbackInfo;
31 using v8::FunctionTemplate;
32 using v8::GCCallbackFlags;
33 using v8::GCEpilogueCallback;
34 using v8::GCPrologueCallback;
35 using v8::GCType;
36 using v8::Handle;
37 using v8::HandleScope;
38 using v8::Local;
39 using v8::Object;
40 using v8::String;
41 using v8::Value;
42
43 static uint64_t counter_gc_start_time;
44 static uint64_t counter_gc_end_time;
45
46
47 void COUNTER_NET_SERVER_CONNECTION(const FunctionCallbackInfo<Value>&) {
48   NODE_COUNT_SERVER_CONN_OPEN();
49 }
50
51
52 void COUNTER_NET_SERVER_CONNECTION_CLOSE(const FunctionCallbackInfo<Value>&) {
53   NODE_COUNT_SERVER_CONN_CLOSE();
54 }
55
56
57 void COUNTER_HTTP_SERVER_REQUEST(const FunctionCallbackInfo<Value>&) {
58   NODE_COUNT_HTTP_SERVER_REQUEST();
59 }
60
61
62 void COUNTER_HTTP_SERVER_RESPONSE(const FunctionCallbackInfo<Value>&) {
63   NODE_COUNT_HTTP_SERVER_RESPONSE();
64 }
65
66
67 void COUNTER_HTTP_CLIENT_REQUEST(const FunctionCallbackInfo<Value>&) {
68   NODE_COUNT_HTTP_CLIENT_REQUEST();
69 }
70
71
72 void COUNTER_HTTP_CLIENT_RESPONSE(const FunctionCallbackInfo<Value>&) {
73   NODE_COUNT_HTTP_CLIENT_RESPONSE();
74 }
75
76
77 static void counter_gc_start(GCType type, GCCallbackFlags flags) {
78   counter_gc_start_time = NODE_COUNT_GET_GC_RAWTIME();
79 }
80
81
82 static void counter_gc_done(GCType type, GCCallbackFlags flags) {
83   uint64_t endgc = NODE_COUNT_GET_GC_RAWTIME();
84   if (endgc != 0) {
85     uint64_t totalperiod = endgc - counter_gc_end_time;
86     uint64_t gcperiod = endgc - counter_gc_start_time;
87
88     if (totalperiod > 0) {
89       unsigned int percent = static_cast<unsigned int>(
90           (gcperiod * 100) / totalperiod);
91
92       NODE_COUNT_GC_PERCENTTIME(percent);
93       counter_gc_end_time = endgc;
94     }
95   }
96 }
97
98
99 void InitPerfCounters(Handle<Object> target) {
100   HandleScope scope(node_isolate);
101
102   static struct {
103     const char* name;
104     void (*func)(const FunctionCallbackInfo<Value>&);
105   } tab[] = {
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) }
113 #undef NODE_PROBE
114   };
115
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);
120   }
121
122   // Only Windows performance counters supported
123   // To enable other OS, use conditional compilation here
124   InitPerfCountersWin32();
125
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;
129
130   v8::V8::AddGCPrologueCallback(counter_gc_start);
131   v8::V8::AddGCEpilogueCallback(counter_gc_done);
132 }
133
134
135 void TermPerfCounters(Handle<Object> target) {
136   // Only Windows performance counters supported
137   // To enable other OS, use conditional compilation here
138   TermPerfCountersWin32();
139 }
140
141 }  // namespace node