summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/env.cc5
-rw-r--r--src/node.cc9
-rw-r--r--src/node_internals.h2
-rw-r--r--src/node_perf.cc1
-rw-r--r--src/node_perf_common.h1
-rw-r--r--src/node_process_methods.cc11
-rw-r--r--src/node_report.cc21
7 files changed, 25 insertions, 25 deletions
diff --git a/src/env.cc b/src/env.cc
index 94fe595ac5..ce2911251e 100644
--- a/src/env.cc
+++ b/src/env.cc
@@ -228,9 +228,8 @@ Environment::Environment(IsolateData* isolate_data,
performance_state_.reset(new performance::performance_state(isolate()));
performance_state_->Mark(
performance::NODE_PERFORMANCE_MILESTONE_ENVIRONMENT);
- performance_state_->Mark(
- performance::NODE_PERFORMANCE_MILESTONE_NODE_START,
- performance::performance_node_start);
+ performance_state_->Mark(performance::NODE_PERFORMANCE_MILESTONE_NODE_START,
+ per_process::node_start_time);
performance_state_->Mark(
performance::NODE_PERFORMANCE_MILESTONE_V8_START,
performance::performance_v8_start);
diff --git a/src/node.cc b/src/node.cc
index 82282e202b..b3195b7365 100644
--- a/src/node.cc
+++ b/src/node.cc
@@ -145,8 +145,8 @@ unsigned int reverted_cve = 0;
bool v8_initialized = false;
// node_internals.h
-// process-relative uptime base, initialized at start-up
-double prog_start_time;
+// process-relative uptime base in nanoseconds, initialized in node::Start()
+uint64_t node_start_time;
// Tells whether --prof is passed.
bool v8_is_profiling = false;
@@ -611,9 +611,6 @@ int ProcessGlobalArgs(std::vector<std::string>* args,
int Init(std::vector<std::string>* argv,
std::vector<std::string>* exec_argv,
std::vector<std::string>* errors) {
- // Initialize prog_start_time to get relative uptime.
- per_process::prog_start_time = static_cast<double>(uv_now(uv_default_loop()));
-
// Register built-in modules
binding::RegisterBuiltinModules();
@@ -891,7 +888,7 @@ inline int Start(uv_loop_t* event_loop,
int Start(int argc, char** argv) {
atexit([] () { uv_tty_reset_mode(); });
PlatformInit();
- performance::performance_node_start = PERFORMANCE_NOW();
+ per_process::node_start_time = uv_hrtime();
CHECK_GT(argc, 0);
diff --git a/src/node_internals.h b/src/node_internals.h
index 01ebd8b40a..367df26c0e 100644
--- a/src/node_internals.h
+++ b/src/node_internals.h
@@ -55,7 +55,7 @@ class NativeModuleLoader;
namespace per_process {
extern Mutex env_var_mutex;
-extern double prog_start_time;
+extern uint64_t node_start_time;
extern bool v8_is_profiling;
} // namespace per_process
diff --git a/src/node_perf.cc b/src/node_perf.cc
index b9c0183a83..f63dc6abf0 100644
--- a/src/node_perf.cc
+++ b/src/node_perf.cc
@@ -46,7 +46,6 @@ using v8::Value;
const uint64_t timeOrigin = PERFORMANCE_NOW();
// https://w3c.github.io/hr-time/#dfn-time-origin-timestamp
const double timeOriginTimestamp = GetCurrentTimeInMicroseconds();
-uint64_t performance_node_start;
uint64_t performance_v8_start;
void performance_state::Mark(enum PerformanceMilestone milestone,
diff --git a/src/node_perf_common.h b/src/node_perf_common.h
index 1c4cf01c87..5c972c9841 100644
--- a/src/node_perf_common.h
+++ b/src/node_perf_common.h
@@ -18,7 +18,6 @@ namespace performance {
// These occur before the environment is created. Cache them
// here and add them to the milestones when the env is init'd.
-extern uint64_t performance_node_start;
extern uint64_t performance_v8_start;
#define NODE_PERFORMANCE_MILESTONES(V) \
diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc
index be91a11f56..dfcc6641a1 100644
--- a/src/node_process_methods.cc
+++ b/src/node_process_methods.cc
@@ -44,6 +44,7 @@ using v8::Isolate;
using v8::Local;
using v8::Name;
using v8::NewStringType;
+using v8::Number;
using v8::Object;
using v8::String;
using v8::Uint32;
@@ -58,6 +59,8 @@ Mutex umask_mutex;
#define MICROS_PER_SEC 1e6
// used in Hrtime() below
#define NANOS_PER_SEC 1000000000
+// Used in Uptime()
+#define NANOS_PER_MICROS 1e3
#ifdef _WIN32
/* MAX_PATH is in characters, not bytes. Make sure we have enough headroom. */
@@ -239,12 +242,12 @@ static void Umask(const FunctionCallbackInfo<Value>& args) {
static void Uptime(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
- double uptime;
uv_update_time(env->event_loop());
- uptime = uv_now(env->event_loop()) - per_process::prog_start_time;
-
- args.GetReturnValue().Set(uptime / 1000);
+ double uptime =
+ static_cast<double>(uv_hrtime() - per_process::node_start_time);
+ Local<Number> result = Number::New(env->isolate(), uptime / NANOS_PER_MICROS);
+ args.GetReturnValue().Set(result);
}
static void GetActiveRequests(const FunctionCallbackInfo<Value>& args) {
diff --git a/src/node_report.cc b/src/node_report.cc
index d4e332f607..d4f59de84d 100644
--- a/src/node_report.cc
+++ b/src/node_report.cc
@@ -47,6 +47,9 @@
extern char** environ;
#endif
+constexpr int NANOS_PER_SEC = 1000 * 1000 * 1000;
+constexpr double SEC_PER_MICROS = 1e-6;
+
namespace report {
using node::arraysize;
using node::Environment;
@@ -489,20 +492,19 @@ static void PrintGCStatistics(JSONWriter* writer, Isolate* isolate) {
#ifndef _WIN32
// Report resource usage (Linux/OSX only).
static void PrintResourceUsage(JSONWriter* writer) {
- time_t current_time; // current time absolute
- time(&current_time);
- size_t boot_time = static_cast<time_t>(node::per_process::prog_start_time /
- (1000 * 1000 * 1000));
- auto uptime = difftime(current_time, boot_time);
+ // Get process uptime in seconds
+ uint64_t uptime =
+ (uv_hrtime() - node::per_process::node_start_time) / (NANOS_PER_SEC);
if (uptime == 0) uptime = 1; // avoid division by zero.
// Process and current thread usage statistics
struct rusage stats;
writer->json_objectstart("resourceUsage");
if (getrusage(RUSAGE_SELF, &stats) == 0) {
- double user_cpu = stats.ru_utime.tv_sec + 0.000001 * stats.ru_utime.tv_usec;
+ double user_cpu =
+ stats.ru_utime.tv_sec + SEC_PER_MICROS * stats.ru_utime.tv_usec;
double kernel_cpu =
- stats.ru_utime.tv_sec + 0.000001 * stats.ru_utime.tv_usec;
+ stats.ru_utime.tv_sec + SEC_PER_MICROS * stats.ru_utime.tv_usec;
writer->json_keyvalue("userCpuSeconds", user_cpu);
writer->json_keyvalue("kernelCpuSeconds", kernel_cpu);
double cpu_abs = user_cpu + kernel_cpu;
@@ -522,9 +524,10 @@ static void PrintResourceUsage(JSONWriter* writer) {
#ifdef RUSAGE_THREAD
if (getrusage(RUSAGE_THREAD, &stats) == 0) {
writer->json_objectstart("uvthreadResourceUsage");
- double user_cpu = stats.ru_utime.tv_sec + 0.000001 * stats.ru_utime.tv_usec;
+ double user_cpu =
+ stats.ru_utime.tv_sec + SEC_PER_MICROS * stats.ru_utime.tv_usec;
double kernel_cpu =
- stats.ru_utime.tv_sec + 0.000001 * stats.ru_utime.tv_usec;
+ stats.ru_utime.tv_sec + SEC_PER_MICROS * stats.ru_utime.tv_usec;
writer->json_keyvalue("userCpuSeconds", user_cpu);
writer->json_keyvalue("kernelCpuSeconds", kernel_cpu);
double cpu_abs = user_cpu + kernel_cpu;