1
0
mirror of https://github.com/libuv/libuv synced 2025-03-28 21:13:16 +00:00

linux: make cpu_times consistently be milliseconds

On Linux, cpu_times have been calculated differently to all other
platforms for a while. Other platforms (FreeBSD, Mac, Windows) are all
in milliseconds, but Linux has been returning values ten times larger.

libuv has not previously documented what unit cpu_times uses, even
though NodeJS did - as milliseconds.

Here we're both documenting that the cpu_times are indeed in
milliseconds, and fixing the inconsistency on Linux.

Fixes: https://github.com/libuv/libuv/issues/2773
PR-URL: https://github.com/libuv/libuv/pull/2796
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This commit is contained in:
James Ross 2020-04-22 10:46:07 +02:00 committed by Ben Noordhuis
parent f52786b4ba
commit 37a8affbd3
3 changed files with 21 additions and 19 deletions

View File

@ -131,11 +131,11 @@ Data types
char* model;
int speed;
struct uv_cpu_times_s {
uint64_t user;
uint64_t nice;
uint64_t sys;
uint64_t idle;
uint64_t irq;
uint64_t user; /* milliseconds */
uint64_t nice; /* milliseconds */
uint64_t sys; /* milliseconds */
uint64_t idle; /* milliseconds */
uint64_t irq; /* milliseconds */
} cpu_times;
} uv_cpu_info_t;

View File

@ -1069,11 +1069,11 @@ UV_EXTERN int uv_cancel(uv_req_t* req);
struct uv_cpu_times_s {
uint64_t user;
uint64_t nice;
uint64_t sys;
uint64_t idle;
uint64_t irq;
uint64_t user; /* milliseconds */
uint64_t nice; /* milliseconds */
uint64_t sys; /* milliseconds */
uint64_t idle; /* milliseconds */
uint64_t irq; /* milliseconds */
};
struct uv_cpu_info_s {

View File

@ -768,7 +768,8 @@ static int read_times(FILE* statfile_fp,
unsigned int numcpus,
uv_cpu_info_t* ci) {
struct uv_cpu_times_s ts;
uint64_t clock_ticks;
unsigned int ticks;
unsigned int multiplier;
uint64_t user;
uint64_t nice;
uint64_t sys;
@ -779,9 +780,10 @@ static int read_times(FILE* statfile_fp,
uint64_t len;
char buf[1024];
clock_ticks = sysconf(_SC_CLK_TCK);
assert(clock_ticks != (uint64_t) -1);
assert(clock_ticks != 0);
ticks = (unsigned int)sysconf(_SC_CLK_TCK);
multiplier = ((uint64_t)1000L / ticks);
assert(ticks != (unsigned int) -1);
assert(ticks != 0);
rewind(statfile_fp);
@ -823,11 +825,11 @@ static int read_times(FILE* statfile_fp,
&irq))
abort();
ts.user = clock_ticks * user;
ts.nice = clock_ticks * nice;
ts.sys = clock_ticks * sys;
ts.idle = clock_ticks * idle;
ts.irq = clock_ticks * irq;
ts.user = user * multiplier;
ts.nice = nice * multiplier;
ts.sys = sys * multiplier;
ts.idle = idle * multiplier;
ts.irq = irq * multiplier;
ci[num++].cpu_times = ts;
}
assert(num == numcpus);