Skip to content

Commit

Permalink
src: unify uptime base used across the code base
Browse files Browse the repository at this point in the history
This patch joins `per_process::prog_start_time` (a double)
and `performance::performance_node_start` (a uint64_t) into a
`per_process::node_start_time` (a uint64_t) which gets initialized
in `node::Start()`.

PR-URL: #26016
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
joyeecheung authored and rvagg committed Feb 28, 2019
1 parent 230e98b commit 82df851
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 25 deletions.
5 changes: 2 additions & 3 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 3 additions & 6 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion src/node_perf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion src/node_perf_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down
11 changes: 7 additions & 4 deletions src/node_process_methods.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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. */
Expand Down Expand Up @@ -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) {
Expand Down
21 changes: 12 additions & 9 deletions src/node_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 82df851

Please sign in to comment.