1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-12-27 20:44:56 +00:00

${top} improvements for FreeBSD

- Displaying of CPU time of the processes now works
- ${top_time} (sorting by CPU time) works
- Using memcpy() instead of many sequential assignments (more clear,
  less lines, maybe faster, less effort to change something)

Signed-off-by: Alexander Graf <agraf@znc.in>
Signed-off-by: Pavel Labath <pavelo@centrum.sk>
This commit is contained in:
Alexander Graf 2010-05-23 23:51:29 +02:00 committed by Pavel Labath
parent 53b167a84a
commit b0e5e2cd22

View File

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