diff --git a/src/freebsd.cc b/src/freebsd.cc index 59d96d9a..5cae8f6d 100644 --- a/src/freebsd.cc +++ b/src/freebsd.cc @@ -70,7 +70,7 @@ kvm_t *kd; __attribute__((gnu_inline)) inline void -proc_find_top(struct process **cpu, struct process **mem); +proc_find_top(struct process **cpu, struct process **mem, struct process **time); static short cpu_setup = 0; @@ -597,7 +597,7 @@ char get_freq(char *p_client_buffer, size_t client_buffer_size, const char *p_fo int update_top(void) { - proc_find_top(info.cpu, info.memu); + proc_find_top(info.cpu, info.memu, info.time); return 0; } @@ -733,8 +733,15 @@ int comparemem(const void *a, const void *b) } } +int comparetime(const void *va, const void *vb) +{ + struct process *a = (struct process *)va, *b = (struct process *)vb; + + return b->total_cpu_time - a->total_cpu_time; +} + __attribute__((gnu_inline)) inline void -proc_find_top(struct process **cpu, struct process **mem) +proc_find_top(struct process **cpu, struct process **mem, struct process **time) { struct kinfo_proc *p; int n_processes; @@ -758,6 +765,9 @@ proc_find_top(struct process **cpu, struct process **mem) processes[j].amount = 100.0 * p[i].ki_pctcpu / FSCALE; processes[j].vsize = p[i].ki_size; processes[j].rss = (p[i].ki_rssize * getpagesize()); + /* ki_runtime is in microseconds, total_cpu_time in centiseconds. + * Therefore we divide by 10000. */ + processes[j].total_cpu_time = p[i].ki_runtime / 10000; j++; } } @@ -767,11 +777,7 @@ proc_find_top(struct process **cpu, struct process **mem) struct process *tmp, *ttmp; tmp = (process *) malloc(sizeof(struct process)); - tmp->pid = processes[i].pid; - tmp->amount = processes[i].amount; - tmp->name = strndup(processes[i].name, text_buffer_size); - tmp->rss = processes[i].rss; - tmp->vsize = processes[i].vsize; + memcpy(tmp, &processes[i], sizeof(struct process)); ttmp = mem[i]; mem[i] = tmp; @@ -786,11 +792,7 @@ proc_find_top(struct process **cpu, struct process **mem) struct process *tmp, *ttmp; tmp = (process *) malloc(sizeof(struct process)); - tmp->pid = processes[i].pid; - tmp->amount = processes[i].amount; - tmp->name = strndup(processes[i].name, text_buffer_size); - tmp->rss = processes[i].rss; - tmp->vsize = processes[i].vsize; + memcpy(tmp, &processes[i], sizeof(struct process)); ttmp = cpu[i]; cpu[i] = tmp; @@ -800,6 +802,21 @@ proc_find_top(struct process **cpu, struct process **mem) } } + qsort(processes, j - 1, sizeof(struct process), comparetime); + for (i = 0; i < 10 && i < n_processes; i++) { + struct process *tmp, *ttmp; + + tmp = (process *) malloc(sizeof(struct process)); + memcpy(tmp, &processes[i], sizeof(struct process)); + + ttmp = time[i]; + time[i] = tmp; + if (ttmp != NULL) { + free(ttmp->name); + free(ttmp); + } + } + #if defined(FREEBSD_DEBUG) printf("=====\nmem\n"); for (i = 0; i < 10; i++) {