top: drop problematic field totalmem
authorPhil Sutter <phil@nwl.cc>
Tue, 3 Nov 2009 22:11:14 +0000 (23:11 +0100)
committerPhil Sutter <phil@nwl.cc>
Tue, 3 Nov 2009 22:24:09 +0000 (23:24 +0100)
The field totalmem was formerly used to hold the percentage of used mem
by a process. So at update time, the field info.memmax was being
addressed, which is potentially being updated at the same time, As all
updating routines potentially run in parallel. Though there is an
(quite) easy fix for this: calculate the percentage upon object
printing. This works because conky synchronises all update routines
right before printing the result. (To omit locking on it's own.)

src/freebsd.c
src/openbsd.c
src/top.c
src/top.h

index 736b394..8d13962 100644 (file)
@@ -724,9 +724,9 @@ int comparecpu(const void *a, const void *b)
 
 int comparemem(const void *a, const void *b)
 {
-       if (((const struct process *)a)->totalmem > ((const struct process *)b)->totalmem) {
+       if (((const struct process *)a)->rss > ((const struct process *)b)->rss) {
                return -1;
-       } else if (((const struct process *)a)->totalmem < ((const struct process *)b)->totalmem) {
+       } else if (((const struct process *)a)->rss < ((const struct process *)b)->rss) {
                return 1;
        } else {
                return 0;
@@ -756,8 +756,6 @@ proc_find_top(struct process **cpu, struct process **mem)
                        processes[j].pid = p[i].ki_pid;
                        processes[j].name = strndup(p[i].ki_comm, text_buffer_size);
                        processes[j].amount = 100.0 * p[i].ki_pctcpu / FSCALE;
-                       processes[j].totalmem = (float) (p[i].ki_rssize /
-                               (float) total_pages) * 100.0;
                        processes[j].vsize = p[i].ki_size;
                        processes[j].rss = (p[i].ki_rssize * getpagesize());
                        j++;
@@ -771,7 +769,6 @@ proc_find_top(struct process **cpu, struct process **mem)
                tmp = malloc(sizeof(struct process));
                tmp->pid = processes[i].pid;
                tmp->amount = processes[i].amount;
-               tmp->totalmem = processes[i].totalmem;
                tmp->name = strndup(processes[i].name, text_buffer_size);
                tmp->rss = processes[i].rss;
                tmp->vsize = processes[i].vsize;
@@ -791,7 +788,6 @@ proc_find_top(struct process **cpu, struct process **mem)
                tmp = malloc(sizeof(struct process));
                tmp->pid = processes[i].pid;
                tmp->amount = processes[i].amount;
-               tmp->totalmem = processes[i].totalmem;
                tmp->name = strndup(processes[i].name, text_buffer_size);
                tmp->rss = processes[i].rss;
                tmp->vsize = processes[i].vsize;
@@ -807,8 +803,8 @@ proc_find_top(struct process **cpu, struct process **mem)
 #if defined(FREEBSD_DEBUG)
        printf("=====\nmem\n");
        for (i = 0; i < 10; i++) {
-               printf("%d: %s(%d) %.2f %ld %ld\n", i, mem[i]->name,
-                               mem[i]->pid, mem[i]->totalmem, mem[i]->vsize, mem[i]->rss);
+               printf("%d: %s(%d) %ld %ld\n", i, mem[i]->name,
+                               mem[i]->pid, mem[i]->vsize, mem[i]->rss);
        }
 #endif
 
index b6fba3d..e2f6cc2 100644 (file)
@@ -738,11 +738,11 @@ int comparecpu(const void *a, const void *b)
 
 int comparemem(const void *a, const void *b)
 {
-       if (((struct process *) a)->totalmem > ((struct process *) b)->totalmem) {
+       if (((struct process *) a)->rss > ((struct process *) b)->rss) {
                return -1;
        }
 
-       if (((struct process *) a)->totalmem < ((struct process *) b)->totalmem) {
+       if (((struct process *) a)->rss < ((struct process *) b)->rss) {
                return 1;
        }
 
@@ -783,8 +783,6 @@ inline void proc_find_top(struct process **cpu, struct process **mem)
                        processes[j].pid = p[i].p_pid;
                        processes[j].name = strndup(p[i].p_comm, text_buffer_size);
                        processes[j].amount = 100.0 * p[i].p_pctcpu / FSCALE;
-                       processes[j].totalmem = (float) (p[i].p_vm_rssize /
-                                       (float) total_pages) * 100.0;
                        j++;
                }
        }
@@ -796,7 +794,6 @@ inline void proc_find_top(struct process **cpu, struct process **mem)
                tmp = malloc(sizeof(struct process));
                tmp->pid = processes[i].pid;
                tmp->amount = processes[i].amount;
-               tmp->totalmem = processes[i].totalmem;
                tmp->name = strndup(processes[i].name, text_buffer_size);
 
                ttmp = mem[i];
@@ -814,7 +811,6 @@ inline void proc_find_top(struct process **cpu, struct process **mem)
                tmp = malloc(sizeof(struct process));
                tmp->pid = processes[i].pid;
                tmp->amount = processes[i].amount;
-               tmp->totalmem = processes[i].totalmem;
                tmp->name = strndup(processes[i].name, text_buffer_size);
 
                ttmp = cpu[i];
index 76236ea..ff5fa00 100644 (file)
--- a/src/top.c
+++ b/src/top.c
@@ -199,7 +199,6 @@ static struct process *new_process(int p)
  * Anyone hoping to port wmtop should look here first. */
 static int process_parse_stat(struct process *process)
 {
-       struct information *cur = &info;
        char line[BUFFER_LEN] = { 0 }, filename[BUFFER_LEN], procname[BUFFER_LEN];
        int ps;
        unsigned long user_time = 0;
@@ -280,12 +279,7 @@ static int process_parse_stat(struct process *process)
        process->name = strndup(procname, text_buffer_size);
        process->rss *= getpagesize();
 
-       if (!cur->memmax) {
-               update_total_processes();
-       }
-
        process->total_cpu_time = process->user_time + process->kernel_time;
-       process->totalmem = (float) (((float) process->rss / cur->memmax) / 10);
        if (process->previous_user_time == ULONG_MAX) {
                process->previous_user_time = process->user_time;
        }
@@ -625,9 +619,9 @@ static int compare_cpu(struct process *a, struct process *b)
 /* mem comparison function for insert_sp_element */
 static int compare_mem(struct process *a, struct process *b)
 {
-       if (a->totalmem < b->totalmem) {
+       if (a->rss < b->rss) {
                return 1;
-       } else if (a->totalmem > b->totalmem) {
+       } else if (a->rss > b->rss) {
                return -1;
        } else {
                return 0;
@@ -978,8 +972,10 @@ void print_top(struct text_object *obj, char *p, int top_name_width)
                                                needed[td->num]->pid);
                                break;
                        case TOP_MEM:
+                               /* Calculate a percentage of residential mem from total mem available.
+                                * Since rss is bytes and memmax kilobytes, dividing by 10 suffices here. */
                                snprintf(p, 7, "%6.2f",
-                                               needed[td->num]->totalmem);
+                                               (float) ((float)needed[td->num]->rss / cur->memmax) / 10);
                                break;
                        case TOP_TIME:
                                timeval = format_time(
index e5773d1..ae05f0a 100644 (file)
--- a/src/top.h
+++ b/src/top.h
@@ -123,7 +123,6 @@ struct process {
        unsigned int time_stamp;
        unsigned int counted;
        unsigned int changed;
-       float totalmem;
 };
 
 struct sorted_process {