Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
node_counters.cc
Go to the documentation of this file.
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 #include "env.h"
25 #include "env-inl.h"
26 
27 #include <string.h>
28 
29 
30 namespace node {
31 
32 using v8::FunctionCallbackInfo;
33 using v8::FunctionTemplate;
34 using v8::GCCallbackFlags;
35 using v8::GCType;
36 using v8::HandleScope;
37 using v8::Isolate;
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>&) {
49 }
50 
51 
52 void COUNTER_NET_SERVER_CONNECTION_CLOSE(const FunctionCallbackInfo<Value>&) {
54 }
55 
56 
57 void COUNTER_HTTP_SERVER_REQUEST(const FunctionCallbackInfo<Value>&) {
59 }
60 
61 
62 void COUNTER_HTTP_SERVER_RESPONSE(const FunctionCallbackInfo<Value>&) {
64 }
65 
66 
67 void COUNTER_HTTP_CLIENT_REQUEST(const FunctionCallbackInfo<Value>&) {
69 }
70 
71 
72 void COUNTER_HTTP_CLIENT_RESPONSE(const FunctionCallbackInfo<Value>&) {
74 }
75 
76 
77 static void counter_gc_start(Isolate* isolate,
78  GCType type,
79  GCCallbackFlags flags) {
80  counter_gc_start_time = NODE_COUNT_GET_GC_RAWTIME();
81 }
82 
83 
84 static void counter_gc_done(Isolate* isolate,
85  GCType type,
86  GCCallbackFlags flags) {
87  uint64_t endgc = NODE_COUNT_GET_GC_RAWTIME();
88  if (endgc != 0) {
89  uint64_t totalperiod = endgc - counter_gc_end_time;
90  uint64_t gcperiod = endgc - counter_gc_start_time;
91 
92  if (totalperiod > 0) {
93  unsigned int percent = static_cast<unsigned int>(
94  (gcperiod * 100) / totalperiod);
95 
97  counter_gc_end_time = endgc;
98  }
99  }
100 }
101 
102 
103 void InitPerfCounters(Environment* env, Local<Object> target) {
104  HandleScope scope(env->isolate());
105 
106  static struct {
107  const char* name;
108  void (*func)(const FunctionCallbackInfo<Value>&);
109  } tab[] = {
110 #define NODE_PROBE(name) #name, name
117 #undef NODE_PROBE
118  };
119 
120  for (size_t i = 0; i < arraysize(tab); i++) {
121  Local<String> key = OneByteString(env->isolate(), tab[i].name);
122  Local<Value> val = env->NewFunctionTemplate(tab[i].func)->GetFunction();
123  target->Set(key, val);
124  }
125 
126  // Only Windows performance counters supported
127  // To enable other OS, use conditional compilation here
129 
130  // init times for GC percent calculation and hook callbacks
131  counter_gc_start_time = NODE_COUNT_GET_GC_RAWTIME();
132  counter_gc_end_time = counter_gc_start_time;
133 
134  env->isolate()->AddGCPrologueCallback(counter_gc_start);
135  env->isolate()->AddGCEpilogueCallback(counter_gc_done);
136 }
137 
138 
139 void TermPerfCounters(Local<Object> target) {
140  // Only Windows performance counters supported
141  // To enable other OS, use conditional compilation here
143 }
144 
145 } // namespace node
this func
Definition: v8ustack.d:371
void NODE_COUNT_HTTP_SERVER_RESPONSE()
#define NODE_PROBE(name)
void TermPerfCounters(Local< Object > target)
void NODE_COUNT_SERVER_CONN_OPEN()
void NODE_COUNT_HTTP_CLIENT_RESPONSE()
void NODE_COUNT_HTTP_SERVER_REQUEST()
void NODE_COUNT_SERVER_CONN_CLOSE()
void COUNTER_HTTP_SERVER_RESPONSE(const FunctionCallbackInfo< Value > &)
void COUNTER_HTTP_CLIENT_RESPONSE(const FunctionCallbackInfo< Value > &)
void NODE_COUNT_GC_PERCENTTIME(unsigned int percent)
void COUNTER_NET_SERVER_CONNECTION(const FunctionCallbackInfo< Value > &)
void InitPerfCounters(Environment *env, Local< Object > target)
void COUNTER_HTTP_SERVER_REQUEST(const FunctionCallbackInfo< Value > &)
void COUNTER_HTTP_CLIENT_REQUEST(const FunctionCallbackInfo< Value > &)
void COUNTER_NET_SERVER_CONNECTION_CLOSE(const FunctionCallbackInfo< Value > &)
uint64_t NODE_COUNT_GET_GC_RAWTIME()
void NODE_COUNT_HTTP_CLIENT_REQUEST()