src: unify uptime base used across the code base

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: https://github.com/nodejs/node/pull/26016
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Joyee Cheung 2019-02-09 23:48:14 +08:00
parent 2ae45d3b17
commit fd0a861cdb
No known key found for this signature in database
GPG Key ID: 92B78A53C8303B8D
7 changed files with 25 additions and 25 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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

View File

@ -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,

View File

@ -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) \

View File

@ -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) {

View File

@ -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;