summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPatrick Mueller <pmuellr@nodesource.com>2016-04-05 09:17:48 -0400
committerJames M Snell <jasnell@gmail.com>2016-04-29 16:07:06 -0700
commit52cb4104020723106409349b4965d6614721074f (patch)
tree9e614bf2b4df71fbc0a4147a695bf6d8a5f1d079 /src
parent706778a90247937af0af0ae668fabf03e142640b (diff)
downloadandroid-node-v8-52cb4104020723106409349b4965d6614721074f.tar.gz
android-node-v8-52cb4104020723106409349b4965d6614721074f.tar.bz2
android-node-v8-52cb4104020723106409349b4965d6614721074f.zip
process: add process.cpuUsage() - implementation, doc, tests
Add process.cpuUsage() method that returns the user and system CPU time usage of the current process PR-URL: https://github.com/nodejs/node/pull/6157 Reviewed-By: Robert Lindstaedt <robert.lindstaedt@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Diffstat (limited to 'src')
-rw-r--r--src/node.cc35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/node.cc b/src/node.cc
index 479130230b..6f155a64a3 100644
--- a/src/node.cc
+++ b/src/node.cc
@@ -106,6 +106,7 @@ using v8::Boolean;
using v8::Context;
using v8::EscapableHandleScope;
using v8::Exception;
+using v8::Float64Array;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
@@ -2220,6 +2221,38 @@ void Hrtime(const FunctionCallbackInfo<Value>& args) {
fields[2] = t % NANOS_PER_SEC;
}
+// Microseconds in a second, as a float, used in CPUUsage() below
+#define MICROS_PER_SEC 1e6
+
+// CPUUsage use libuv's uv_getrusage() this-process resource usage accessor,
+// to access ru_utime (user CPU time used) and ru_stime (system CPU time used),
+// which are uv_timeval_t structs (long tv_sec, long tv_usec).
+// Returns those values as Float64 microseconds in the elements of the array
+// passed to the function.
+void CPUUsage(const FunctionCallbackInfo<Value>& args) {
+ uv_rusage_t rusage;
+
+ // Call libuv to get the values we'll return.
+ int err = uv_getrusage(&rusage);
+ if (err) {
+ // On error, return the strerror version of the error code.
+ Local<String> errmsg = OneByteString(args.GetIsolate(), uv_strerror(err));
+ args.GetReturnValue().Set(errmsg);
+ return;
+ }
+
+ // Get the double array pointer from the Float64Array argument.
+ CHECK(args[0]->IsFloat64Array());
+ Local<Float64Array> array = args[0].As<Float64Array>();
+ CHECK_EQ(array->Length(), 2);
+ Local<ArrayBuffer> ab = array->Buffer();
+ double* fields = static_cast<double*>(ab->GetContents().Data());
+
+ // Set the Float64Array elements to be user / system values in microseconds.
+ fields[0] = MICROS_PER_SEC * rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec;
+ fields[1] = MICROS_PER_SEC * rusage.ru_stime.tv_sec + rusage.ru_stime.tv_usec;
+}
+
extern "C" void node_module_register(void* m) {
struct node_module* mp = reinterpret_cast<struct node_module*>(m);
@@ -3212,6 +3245,8 @@ void SetupProcessObject(Environment* env,
env->SetMethod(process, "hrtime", Hrtime);
+ env->SetMethod(process, "cpuUsage", CPUUsage);
+
env->SetMethod(process, "dlopen", DLOpen);
env->SetMethod(process, "uptime", Uptime);