Permalink
Browse files

sunos.c: return CPU time information in ms, not ticks

  • Loading branch information...
1 parent 38032ec commit e79d4e75f314d341d245ba9cb704ac8bff60c1c7 @tfeb tfeb committed Dec 31, 2012
Showing with 16 additions and 4 deletions.
  1. +16 −4 src/unix/sunos.c
View
@@ -476,6 +476,11 @@ uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
kstat_t *ksp;
kstat_named_t *knp;
uv_cpu_info_t* cpu_info;
+ unsigned long clock_ticks;
+
+ clock_ticks = sysconf(_SC_CLK_TCK);
+ assert(clock_ticks != (unsigned long) -1);
+ assert(clock_ticks != 0);
if ((kc = kstat_open()) == NULL) {
return uv__new_sys_error(errno);
@@ -528,21 +533,28 @@ uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
cpu_info->cpu_times.idle = 0;
cpu_info->cpu_times.irq = 0;
} else {
+ /* conversions to ms below have good rounding but potential
+ * overflow */
knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"cpu_ticks_user");
assert(knp->data_type == KSTAT_DATA_UINT64);
- cpu_info->cpu_times.user = knp->value.ui64;
+ cpu_info->cpu_times.user = (1000 * knp->value.ui64 + clock_ticks/2)
+ / clock_ticks;
knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"cpu_ticks_kernel");
assert(knp->data_type == KSTAT_DATA_UINT64);
- cpu_info->cpu_times.sys = knp->value.ui64;
+ cpu_info->cpu_times.sys = (1000 * knp->value.ui64 + clock_ticks/2)
+ / clock_ticks;
knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"cpu_ticks_idle");
assert(knp->data_type == KSTAT_DATA_UINT64);
- cpu_info->cpu_times.idle = knp->value.ui64;
+ cpu_info->cpu_times.idle = (1000 * knp->value.ui64 + clock_ticks/2)
+ / clock_ticks;
knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"intr");
assert(knp->data_type == KSTAT_DATA_UINT64);
- cpu_info->cpu_times.irq = knp->value.ui64;
+ cpu_info->cpu_times.irq = (1000 * knp->value.ui64 + clock_ticks/2)
+ / clock_ticks;
+
cpu_info->cpu_times.nice = 0;
}

0 comments on commit e79d4e7

Please sign in to comment.