*
* Copyright (c) 2005 Adi Zaimi, Dan Piponi <dan@tanelorn.demon.co.uk>,
* Dave Clark <clarkd@skynet.ca>
- * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
+ * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
* (see AUTHORS)
* All rights reserved.
*
struct process *p = first_process;
while (p) {
- if (!strcmp(p->name, name))
+ if (p->name && !strcmp(p->name, name))
return p;
p = p->next;
}
* 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];
+ char state[4];
int ps;
unsigned long user_time = 0;
unsigned long kernel_time = 0;
rc = MIN((unsigned)(rparen - lparen - 1), sizeof(procname) - 1);
strncpy(procname, lparen + 1, rc);
procname[rc] = '\0';
- rc = sscanf(rparen + 1, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %lu "
- "%lu %*s %*s %*s %d %*s %*s %*s %u %u", &process->user_time,
+ rc = sscanf(rparen + 1, "%3s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %lu "
+ "%lu %*s %*s %*s %d %*s %*s %*s %u %u", state, &process->user_time,
&process->kernel_time, &nice_val, &process->vsize, &process->rss);
- if (rc < 5) {
+ if (rc < 6) {
NORM_ERR("scaning data for %s failed, got only %d fields", procname, rc);
return 1;
}
+
+ if(state[0]=='R')
+ ++ info.run_procs;
+
/* remove any "kdeinit: " */
if (procname == strstr(procname, "kdeinit")) {
snprintf(filename, sizeof(filename), PROCFS_CMDLINE_TEMPLATE,
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;
}
return 1;
}
+ info.run_procs = 0;
++g_time;
/* Get list of processes from /proc directory */
/* 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;
#endif /* IOSTATS */
}
+struct top_data {
+ int num;
+ int type;
+ int was_parsed;
+ char *s;
+};
+
int parse_top_args(const char *s, const char *arg, struct text_object *obj)
{
+ struct top_data *td;
char buf[64];
int n;
- if (obj->data.top.was_parsed) {
- return 1;
+ if (!arg) {
+ NORM_ERR("top needs arguments");
+ return 0;
}
- obj->data.top.was_parsed = 1;
- if (arg && !obj->data.top.s) {
- obj->data.top.s = strndup(arg, text_buffer_size);
+ if (obj->data.opaque) {
+ return 1;
}
if (s[3] == 0) {
return 0;
}
- if (!arg) {
- NORM_ERR("top needs arguments");
- return 0;
- }
+ obj->data.opaque = td = malloc(sizeof(struct top_data));
+ memset(td, 0, sizeof(struct top_data));
+ td->s = strndup(arg, text_buffer_size);
if (sscanf(arg, "%63s %i", buf, &n) == 2) {
if (strcmp(buf, "name") == EQUAL) {
- obj->data.top.type = TOP_NAME;
+ td->type = TOP_NAME;
} else if (strcmp(buf, "cpu") == EQUAL) {
- obj->data.top.type = TOP_CPU;
+ td->type = TOP_CPU;
} else if (strcmp(buf, "pid") == EQUAL) {
- obj->data.top.type = TOP_PID;
+ td->type = TOP_PID;
} else if (strcmp(buf, "mem") == EQUAL) {
- obj->data.top.type = TOP_MEM;
+ td->type = TOP_MEM;
} else if (strcmp(buf, "time") == EQUAL) {
- obj->data.top.type = TOP_TIME;
+ td->type = TOP_TIME;
} else if (strcmp(buf, "mem_res") == EQUAL) {
- obj->data.top.type = TOP_MEM_RES;
+ td->type = TOP_MEM_RES;
} else if (strcmp(buf, "mem_vsize") == EQUAL) {
- obj->data.top.type = TOP_MEM_VSIZE;
+ td->type = TOP_MEM_VSIZE;
#ifdef IOSTATS
} else if (strcmp(buf, "io_read") == EQUAL) {
- obj->data.top.type = TOP_READ_BYTES;
+ td->type = TOP_READ_BYTES;
} else if (strcmp(buf, "io_write") == EQUAL) {
- obj->data.top.type = TOP_WRITE_BYTES;
+ td->type = TOP_WRITE_BYTES;
} else if (strcmp(buf, "io_perc") == EQUAL) {
- obj->data.top.type = TOP_IO_PERC;
+ td->type = TOP_IO_PERC;
#endif /* IOSTATS */
} else {
NORM_ERR("invalid type arg for top");
#else /* IOSTATS */
NORM_ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize");
#endif /* IOSTATS */
+ free(td->s);
+ free(obj->data.opaque);
return 0;
}
if (n < 1 || n > 10) {
NORM_ERR("invalid num arg for top. Must be between 1 and 10.");
+ free(td->s);
+ free(obj->data.opaque);
return 0;
} else {
- obj->data.top.num = n - 1;
+ td->num = n - 1;
}
} else {
NORM_ERR("invalid argument count for top");
+ free(td->s);
+ free(obj->data.opaque);
return 0;
}
return 1;
}
+static char *format_time(unsigned long timeval, const int width)
+{
+ char buf[10];
+ unsigned long nt; // narrow time, for speed on 32-bit
+ unsigned cc; // centiseconds
+ unsigned nn; // multi-purpose whatever
+
+ nt = timeval;
+ cc = nt % 100; // centiseconds past second
+ nt /= 100; // total seconds
+ nn = nt % 60; // seconds past the minute
+ nt /= 60; // total minutes
+ if (width >= snprintf(buf, sizeof buf, "%lu:%02u.%02u",
+ nt, nn, cc)) {
+ return strndup(buf, text_buffer_size);
+ }
+ if (width >= snprintf(buf, sizeof buf, "%lu:%02u", nt, nn)) {
+ return strndup(buf, text_buffer_size);
+ }
+ nn = nt % 60; // minutes past the hour
+ nt /= 60; // total hours
+ if (width >= snprintf(buf, sizeof buf, "%lu,%02u", nt, nn)) {
+ return strndup(buf, text_buffer_size);
+ }
+ nn = nt; // now also hours
+ if (width >= snprintf(buf, sizeof buf, "%uh", nn)) {
+ return strndup(buf, text_buffer_size);
+ }
+ nn /= 24; // now days
+ if (width >= snprintf(buf, sizeof buf, "%ud", nn)) {
+ return strndup(buf, text_buffer_size);
+ }
+ nn /= 7; // now weeks
+ if (width >= snprintf(buf, sizeof buf, "%uw", nn)) {
+ return strndup(buf, text_buffer_size);
+ }
+ // well shoot, this outta' fit...
+ return strndup("<inf>", text_buffer_size);
+}
+
+static unsigned int top_name_width = 15;
+/* return zero on success, non-zero otherwise */
+int set_top_name_width(const char *s)
+{
+ if (!s)
+ return 0;
+ return !(sscanf(s, "%u", &top_name_width) == 1);
+}
+
+void print_top(struct text_object *obj, char *p, int p_max_size)
+{
+ struct information *cur = &info;
+ struct top_data *td = obj->data.opaque;
+ struct process **needed = 0;
+ int width;
+
+ if (!td)
+ return;
+
+ switch (obj->type) {
+ case OBJ_top:
+ needed = cur->cpu;
+ break;
+ case OBJ_top_mem:
+ needed = cur->memu;
+ break;
+ case OBJ_top_time:
+ needed = cur->time;
+ break;
+#ifdef IOSTATS
+ case OBJ_top_io:
+ needed = cur->io;
+ break;
+#endif /* IOSTATS */
+ default:
+ return;
+ }
+
+
+ if (needed[td->num]) {
+ char *timeval;
+
+ switch (td->type) {
+ case TOP_NAME:
+ if (needed[td->num]->name) {
+ width = MIN(p_max_size, (int)top_name_width + 1);
+ snprintf(p, width + 1, "%-*s", width,
+ needed[td->num]->name);
+ }
+ break;
+ case TOP_CPU:
+ width = MIN(p_max_size, 7);
+ snprintf(p, width, "%6.2f",
+ needed[td->num]->amount);
+ break;
+ case TOP_PID:
+ width = MIN(p_max_size, 6);
+ snprintf(p, width, "%5i",
+ 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. */
+ width = MIN(p_max_size, 7);
+ snprintf(p, width, "%6.2f",
+ (float) ((float)needed[td->num]->rss / cur->memmax) / 10);
+ break;
+ case TOP_TIME:
+ width = MIN(p_max_size, 10);
+ timeval = format_time(
+ needed[td->num]->total_cpu_time, 9);
+ snprintf(p, width, "%9s", timeval);
+ free(timeval);
+ break;
+ case TOP_MEM_RES:
+ human_readable(needed[td->num]->rss,
+ p, p_max_size);
+ break;
+ case TOP_MEM_VSIZE:
+ human_readable(needed[td->num]->vsize,
+ p, p_max_size);
+ break;
+#ifdef IOSTATS
+ case TOP_READ_BYTES:
+ human_readable(needed[td->num]->read_bytes / update_interval,
+ p, p_max_size);
+ break;
+ case TOP_WRITE_BYTES:
+ human_readable(needed[td->num]->write_bytes / update_interval,
+ p, p_max_size);
+ break;
+ case TOP_IO_PERC:
+ width = MIN(p_max_size, 7);
+ snprintf(p, width, "%6.2f",
+ needed[td->num]->io_perc);
+ break;
+#endif
+ }
+ }
+}
+
+void free_top(struct text_object *obj, int internal)
+{
+ struct top_data *td = obj->data.opaque;
+
+ if (info.first_process && !internal) {
+ free_all_processes();
+ info.first_process = NULL;
+ }
+
+ if (!td)
+ return;
+ if (td->s)
+ free(td->s);
+ free(obj->data.opaque);
+ obj->data.opaque = NULL;
+}