Skip to content

Commit 82df851

Browse files
joyeecheungrvagg
authored andcommitted
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: #26016 Reviewed-By: Anna Henningsen <[email protected]>
1 parent 230e98b commit 82df851

7 files changed

+25
-25
lines changed

src/env.cc

+2-3
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,8 @@ Environment::Environment(IsolateData* isolate_data,
228228
performance_state_.reset(new performance::performance_state(isolate()));
229229
performance_state_->Mark(
230230
performance::NODE_PERFORMANCE_MILESTONE_ENVIRONMENT);
231-
performance_state_->Mark(
232-
performance::NODE_PERFORMANCE_MILESTONE_NODE_START,
233-
performance::performance_node_start);
231+
performance_state_->Mark(performance::NODE_PERFORMANCE_MILESTONE_NODE_START,
232+
per_process::node_start_time);
234233
performance_state_->Mark(
235234
performance::NODE_PERFORMANCE_MILESTONE_V8_START,
236235
performance::performance_v8_start);

src/node.cc

+3-6
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ unsigned int reverted_cve = 0;
145145
bool v8_initialized = false;
146146

147147
// node_internals.h
148-
// process-relative uptime base, initialized at start-up
149-
double prog_start_time;
148+
// process-relative uptime base in nanoseconds, initialized in node::Start()
149+
uint64_t node_start_time;
150150
// Tells whether --prof is passed.
151151
bool v8_is_profiling = false;
152152

@@ -611,9 +611,6 @@ int ProcessGlobalArgs(std::vector<std::string>* args,
611611
int Init(std::vector<std::string>* argv,
612612
std::vector<std::string>* exec_argv,
613613
std::vector<std::string>* errors) {
614-
// Initialize prog_start_time to get relative uptime.
615-
per_process::prog_start_time = static_cast<double>(uv_now(uv_default_loop()));
616-
617614
// Register built-in modules
618615
binding::RegisterBuiltinModules();
619616

@@ -891,7 +888,7 @@ inline int Start(uv_loop_t* event_loop,
891888
int Start(int argc, char** argv) {
892889
atexit([] () { uv_tty_reset_mode(); });
893890
PlatformInit();
894-
performance::performance_node_start = PERFORMANCE_NOW();
891+
per_process::node_start_time = uv_hrtime();
895892

896893
CHECK_GT(argc, 0);
897894

src/node_internals.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class NativeModuleLoader;
5555

5656
namespace per_process {
5757
extern Mutex env_var_mutex;
58-
extern double prog_start_time;
58+
extern uint64_t node_start_time;
5959
extern bool v8_is_profiling;
6060
} // namespace per_process
6161

src/node_perf.cc

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ using v8::Value;
4646
const uint64_t timeOrigin = PERFORMANCE_NOW();
4747
// https://w3c.github.io/hr-time/#dfn-time-origin-timestamp
4848
const double timeOriginTimestamp = GetCurrentTimeInMicroseconds();
49-
uint64_t performance_node_start;
5049
uint64_t performance_v8_start;
5150

5251
void performance_state::Mark(enum PerformanceMilestone milestone,

src/node_perf_common.h

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ namespace performance {
1818

1919
// These occur before the environment is created. Cache them
2020
// here and add them to the milestones when the env is init'd.
21-
extern uint64_t performance_node_start;
2221
extern uint64_t performance_v8_start;
2322

2423
#define NODE_PERFORMANCE_MILESTONES(V) \

src/node_process_methods.cc

+7-4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ using v8::Isolate;
4444
using v8::Local;
4545
using v8::Name;
4646
using v8::NewStringType;
47+
using v8::Number;
4748
using v8::Object;
4849
using v8::String;
4950
using v8::Uint32;
@@ -58,6 +59,8 @@ Mutex umask_mutex;
5859
#define MICROS_PER_SEC 1e6
5960
// used in Hrtime() below
6061
#define NANOS_PER_SEC 1000000000
62+
// Used in Uptime()
63+
#define NANOS_PER_MICROS 1e3
6164

6265
#ifdef _WIN32
6366
/* MAX_PATH is in characters, not bytes. Make sure we have enough headroom. */
@@ -239,12 +242,12 @@ static void Umask(const FunctionCallbackInfo<Value>& args) {
239242

240243
static void Uptime(const FunctionCallbackInfo<Value>& args) {
241244
Environment* env = Environment::GetCurrent(args);
242-
double uptime;
243245

244246
uv_update_time(env->event_loop());
245-
uptime = uv_now(env->event_loop()) - per_process::prog_start_time;
246-
247-
args.GetReturnValue().Set(uptime / 1000);
247+
double uptime =
248+
static_cast<double>(uv_hrtime() - per_process::node_start_time);
249+
Local<Number> result = Number::New(env->isolate(), uptime / NANOS_PER_MICROS);
250+
args.GetReturnValue().Set(result);
248251
}
249252

250253
static void GetActiveRequests(const FunctionCallbackInfo<Value>& args) {

src/node_report.cc

+12-9
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
extern char** environ;
4848
#endif
4949

50+
constexpr int NANOS_PER_SEC = 1000 * 1000 * 1000;
51+
constexpr double SEC_PER_MICROS = 1e-6;
52+
5053
namespace report {
5154
using node::arraysize;
5255
using node::Environment;
@@ -489,20 +492,19 @@ static void PrintGCStatistics(JSONWriter* writer, Isolate* isolate) {
489492
#ifndef _WIN32
490493
// Report resource usage (Linux/OSX only).
491494
static void PrintResourceUsage(JSONWriter* writer) {
492-
time_t current_time; // current time absolute
493-
time(&current_time);
494-
size_t boot_time = static_cast<time_t>(node::per_process::prog_start_time /
495-
(1000 * 1000 * 1000));
496-
auto uptime = difftime(current_time, boot_time);
495+
// Get process uptime in seconds
496+
uint64_t uptime =
497+
(uv_hrtime() - node::per_process::node_start_time) / (NANOS_PER_SEC);
497498
if (uptime == 0) uptime = 1; // avoid division by zero.
498499

499500
// Process and current thread usage statistics
500501
struct rusage stats;
501502
writer->json_objectstart("resourceUsage");
502503
if (getrusage(RUSAGE_SELF, &stats) == 0) {
503-
double user_cpu = stats.ru_utime.tv_sec + 0.000001 * stats.ru_utime.tv_usec;
504+
double user_cpu =
505+
stats.ru_utime.tv_sec + SEC_PER_MICROS * stats.ru_utime.tv_usec;
504506
double kernel_cpu =
505-
stats.ru_utime.tv_sec + 0.000001 * stats.ru_utime.tv_usec;
507+
stats.ru_utime.tv_sec + SEC_PER_MICROS * stats.ru_utime.tv_usec;
506508
writer->json_keyvalue("userCpuSeconds", user_cpu);
507509
writer->json_keyvalue("kernelCpuSeconds", kernel_cpu);
508510
double cpu_abs = user_cpu + kernel_cpu;
@@ -522,9 +524,10 @@ static void PrintResourceUsage(JSONWriter* writer) {
522524
#ifdef RUSAGE_THREAD
523525
if (getrusage(RUSAGE_THREAD, &stats) == 0) {
524526
writer->json_objectstart("uvthreadResourceUsage");
525-
double user_cpu = stats.ru_utime.tv_sec + 0.000001 * stats.ru_utime.tv_usec;
527+
double user_cpu =
528+
stats.ru_utime.tv_sec + SEC_PER_MICROS * stats.ru_utime.tv_usec;
526529
double kernel_cpu =
527-
stats.ru_utime.tv_sec + 0.000001 * stats.ru_utime.tv_usec;
530+
stats.ru_utime.tv_sec + SEC_PER_MICROS * stats.ru_utime.tv_usec;
528531
writer->json_keyvalue("userCpuSeconds", user_cpu);
529532
writer->json_keyvalue("kernelCpuSeconds", kernel_cpu);
530533
double cpu_abs = user_cpu + kernel_cpu;

0 commit comments

Comments
 (0)