From: Alexander Graf Date: Fri, 28 May 2010 15:28:21 +0000 (+0200) Subject: ${top} fixes for FreeBSD X-Git-Url: http://vcs.maemo.org/git/?p=monky;a=commitdiff_plain;h=10bd81e77c3d1476c70ec8de4f23416ff7a2515b ${top} fixes for FreeBSD ${top}, ${top_mem} and ${top_time} are now recognized as variables. The parts of code doing this were in a #ifdef __linux__. The total CPU time of processes (${top} with "time" argument) is now printable, and ${top_time} works. Signed-off-by: Alexander Graf --- diff --git a/src/conky.c b/src/conky.c index f52d7b8..a289172 100644 --- a/src/conky.c +++ b/src/conky.c @@ -2180,7 +2180,6 @@ void generate_text_internal(char *p, int p_max_size, /* we have four different types of top (top, top_mem, * top_time and top_io). To avoid having almost-same code four * times, we have this special handler. */ -#ifdef __linux__ break; case OBJ_top: case OBJ_top_mem: @@ -2189,7 +2188,6 @@ void generate_text_internal(char *p, int p_max_size, case OBJ_top_io: #endif print_top(obj, p, p_max_size); -#endif /* __linux__ */ OBJ(tail) { print_tailhead("tail", obj, p, p_max_size); } @@ -4226,9 +4224,7 @@ static void set_default_configurations(void) #ifdef IOSTATS top_io = 0; #endif -#ifdef __linux__ top_running = 0; -#endif #ifdef MPD mpd_env_host = getenv("MPD_HOST"); mpd_env_port = getenv("MPD_PORT"); diff --git a/src/core.c b/src/core.c index 90ed09b..e0e7218 100644 --- a/src/core.c +++ b/src/core.c @@ -541,6 +541,11 @@ struct text_object *construct_text_object(const char *s, const char *arg, long parse_platform_sensor(obj, arg); END OBJ_ARG(hwmon, 0, "hwmon needs argumanets") parse_hwmon_sensor(obj, arg); + END OBJ(addr, &update_net_stats) + parse_net_stat_arg(obj, arg, free_at_crash); + END OBJ(addrs, &update_net_stats) + parse_net_stat_arg(obj, arg, free_at_crash); +#endif /* __linux__ */ END /* we have four different types of top (top, top_mem, top_time and top_io). To * avoid having almost-same code four times, we have this special @@ -551,12 +556,8 @@ struct text_object *construct_text_object(const char *s, const char *arg, long if (!parse_top_args(s, arg, obj)) { return NULL; } - } else OBJ(addr, &update_net_stats) - parse_net_stat_arg(obj, arg, free_at_crash); - END OBJ(addrs, &update_net_stats) - parse_net_stat_arg(obj, arg, free_at_crash); -#endif /* __linux__ */ - END OBJ_ARG(tail, 0, "tail needs arguments") + } else + OBJ_ARG(tail, 0, "tail needs arguments") init_tailhead("tail", arg, obj, free_at_crash); END OBJ_ARG(head, 0, "head needs arguments") init_tailhead("head", arg, obj, free_at_crash); @@ -786,9 +787,9 @@ struct text_object *construct_text_object(const char *s, const char *arg, long obj->sub = malloc(sizeof(struct text_object)); extract_variable_text_internal(obj->sub, arg); END OBJ(processes, &update_total_processes) -#ifdef __linux__ END OBJ(running_processes, &update_top) top_running = 1; +#ifdef __linux__ END OBJ(threads, &update_threads) END OBJ(running_threads, &update_stat) #else diff --git a/src/freebsd.c b/src/freebsd.c index 368c05a..a8cd8b1 100644 --- a/src/freebsd.c +++ b/src/freebsd.c @@ -67,7 +67,7 @@ #endif __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; @@ -590,7 +590,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; } @@ -726,8 +726,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; @@ -751,6 +758,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++; } } @@ -760,11 +770,8 @@ proc_find_top(struct process **cpu, struct process **mem) struct process *tmp, *ttmp; tmp = malloc(sizeof(struct process)); - tmp->pid = processes[i].pid; - tmp->amount = processes[i].amount; + memcpy(tmp, &processes[i], sizeof(struct process)); tmp->name = strndup(processes[i].name, text_buffer_size); - tmp->rss = processes[i].rss; - tmp->vsize = processes[i].vsize; ttmp = mem[i]; mem[i] = tmp; @@ -779,11 +786,8 @@ proc_find_top(struct process **cpu, struct process **mem) struct process *tmp, *ttmp; tmp = malloc(sizeof(struct process)); - tmp->pid = processes[i].pid; - tmp->amount = processes[i].amount; + memcpy(tmp, &processes[i], sizeof(struct process)); tmp->name = strndup(processes[i].name, text_buffer_size); - tmp->rss = processes[i].rss; - tmp->vsize = processes[i].vsize; ttmp = cpu[i]; cpu[i] = tmp; @@ -793,6 +797,22 @@ 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 = malloc(sizeof(struct process)); + memcpy(tmp, &processes[i], sizeof(struct process)); + tmp->name = strndup(processes[i].name, text_buffer_size); + + 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++) {