OBJ_if_mounted,
OBJ_if_running,
OBJ_top,
+ OBJ_top_mem,
OBJ_tail,
OBJ_kernel,
OBJ_loadavg,
}
else {
obj->data.top.num = n-1;
+ top_cpu = 1;
}
}
else {
return;
}
END
- OBJ(addr, INFO_NET)
+ OBJ(top_mem, INFO_TOP)
+ char buf[64];
+ int n;
+ if(!arg) {
+ ERR("top_mem needs arguments");
+ obj->type = OBJ_text;
+ obj->data.s = strdup("${top_mem}");
+ return;
+ }
+ if(sscanf(arg, "%63s %i", buf, &n) == 2) {
+ if(strcmp(buf, "name") == 0) {
+ obj->data.top.type = TOP_NAME;
+ }
+ else if(strcmp(buf, "cpu") == 0) {
+ obj->data.top.type = TOP_CPU;
+ }
+ else if(strcmp(buf, "pid") == 0) {
+ obj->data.top.type = TOP_PID;
+ }
+ else if(strcmp(buf, "mem") == 0) {
+ obj->data.top.type = TOP_MEM;
+ }
+ else
+ {
+ ERR("invalid arg for top");
+ return;
+ }
+ if(n < 1 || n > 10) {
+ CRIT_ERR("invalid arg for top");
+ return;
+ }
+ else {
+ obj->data.top.num = n-1;
+ top_mem = 1;
+ }
+ }
+ else {
+ ERR("invalid args given for top");
+ return;
+ }
+ END
+ OBJ(addr, INFO_NET)
obj->data.net = get_net_stat(arg);
END
OBJ(linkstatus, INFO_WIFI)
if (obj->data.top.type == TOP_NAME && obj->data.top.num >= 0 && obj->data.top.num < 10){
// if we limit the buffer and add a bunch of space after, it stops the thing from
// moving other shit around, which is really fucking annoying
- snprintf(p, 17, "%s ", cur->tops[obj->data.top.num]->name);
+ snprintf(p, 17, "%s ", cur->cpu[obj->data.top.num]->name);
}
else if (obj->data.top.type == TOP_CPU && obj->data.top.num >= 0 && obj->data.top.num < 10) {
- snprintf(p, 7, "%3.2f ", cur->tops[obj->data.top.num]->amount);
+ snprintf(p, 7, "%3.2f ", cur->cpu[obj->data.top.num]->amount);
}
else if (obj->data.top.type == TOP_PID && obj->data.top.num >= 0 && obj->data.top.num < 10) {
- snprintf(p, 8, "%i ", cur->tops[obj->data.top.num]->pid);
+ snprintf(p, 8, "%i ", cur->cpu[obj->data.top.num]->pid);
}
else if (obj->data.top.type == TOP_MEM && obj->data.top.num >= 0 && obj->data.top.num < 10) {
- snprintf(p, 7, "%3.2f ", cur->tops[obj->data.top.num]->totalmem);
+ snprintf(p, 7, "%3.2f ", cur->cpu[obj->data.top.num]->totalmem);
}
}
+ OBJ(top_mem) {
+ if (obj->data.top.type == TOP_NAME && obj->data.top.num >= 0 && obj->data.top.num < 10){
+ // if we limit the buffer and add a bunch of space after, it stops the thing from
+ // moving other shit around, which is really fucking annoying
+ snprintf(p, 17, "%s ", cur->memu[obj->data.top.num]->name);
+ }
+ else if (obj->data.top.type == TOP_CPU && obj->data.top.num >= 0 && obj->data.top.num < 10) {
+ snprintf(p, 7, "%3.2f ", cur->memu[obj->data.top.num]->amount);
+ }
+ else if (obj->data.top.type == TOP_PID && obj->data.top.num >= 0 && obj->data.top.num < 10) {
+ snprintf(p, 8, "%i ", cur->memu[obj->data.top.num]->pid);
+ }
+ else if (obj->data.top.type == TOP_MEM && obj->data.top.num >= 0 && obj->data.top.num < 10) {
+ snprintf(p, 7, "%3.2f ", cur->memu[obj->data.top.num]->totalmem);
+ }
+ }
static void main_loop()
{
Region region = XCreateRegion();
- info.looped = -1;
+ info.looped = 0;
while (total_run_times == 0 || info.looped < total_run_times-1) {
info.looped++;
XFlush(display);
info.cpu_avg_samples = 2;
info.net_avg_samples = 2;
info.memmax = 0;
+ top_cpu = 0;
+ top_mem = 0;
#ifdef MPD
strcpy(info.mpd.host, "localhost");
info.mpd.port = 6600;
info.mpd.status = "Checking status...";
#endif
out_to_console = 0;
- top_sort_cpu = 1;
use_spacer = 0;
default_fg_color = WhitePixel(display, screen);
default_bg_color = BlackPixel(display, screen);
string_to_bool
(value);
}
- CONF("top_sort_cpu") {
- top_sort_cpu
- =
- string_to_bool
- (value);
- }
CONF("use_spacer") {
use_spacer
=
}
/*
- * Create a new process object and insert it into the process list
- */
+* Create a new process object and insert it into the process list
+*/
static struct process *new_process(int p)
{
struct process *process;
process = malloc(sizeof(struct process));
/*
- * Do stitching necessary for doubly linked list
- */
+ * Do stitching necessary for doubly linked list
+ */
process->name = 0;
process->previous = 0;
process->next = first_process;
process->previous_kernel_time = INT_MAX;
process->counted = 1;
-/* process_find_name(process);*/
+ /* process_find_name(process);*/
return process;
}
/*inline void draw_processes(void);*/
static int calc_cpu_total(void);
static void calc_cpu_each(int);
-void process_find_top(struct process **);
/******************************************/
/******************************************/
/*
- * These are the guts that extract information out of /proc.
- * Anyone hoping to port wmtop should look here first.
- */
+* These are the guts that extract information out of /proc.
+* Anyone hoping to port wmtop should look here first.
+*/
static int process_parse_stat(struct process *process)
{
struct information *cur;
++g_time;
/*
- * Get list of processes from /proc directory
- */
+ * Get list of processes from /proc directory
+ */
while ((entry = readdir(dir))) {
pid_t pid;
if (!entry) {
/*
- * Problem reading list of processes
- */
+ * Problem reading list of processes
+ */
closedir(dir);
return 1;
}
/******************************************/
/*
- * This function seems to hog all of the CPU time. I can't figure out why - it
- * doesn't do much.
- */
+* This function seems to hog all of the CPU time. I can't figure out why - it
+* doesn't do much.
+*/
static int calculate_cpu(struct process *process)
{
int rc;
return 1;*/
/*
- * Check name against the exclusion list
- */
+ * Check name against the exclusion list
+ */
if (process->counted && exclusion_expression
- && !regexec(exclusion_expression, process->name, 0, 0, 0))
- process->counted = 0;
+ && !regexec(exclusion_expression, process->name, 0, 0, 0))
+ process->counted = 0;
return 0;
}
p = p->next;
/*
- * Delete processes that have died
- */
+ * Delete processes that have died
+ */
if (current->time_stamp != g_time)
delete_process(current);
}
assert(p->id == 0x0badfeed);
/*
- * Ensure that deleted processes aren't reused.
- */
+ * Ensure that deleted processes aren't reused.
+ */
p->id = 0x007babe;
#endif /* defined(PARANOID) */
/*
- * Maintain doubly linked list.
- */
+ * Maintain doubly linked list.
+ */
if (p->next)
p->next->previous = p->previous;
if (p->previous)
struct process *p = first_process;
while (p) {
/*p->amount = total ?
- (100.0 * (float) (p->user_time + p->kernel_time) /
- total) : 0;*/
- p->amount = (100.0 * (p->user_time + p->kernel_time) / total);
+ (100.0 * (float) (p->user_time + p->kernel_time) /
+ total) : 0;*/
+ p->amount = (100.0 * (p->user_time + p->kernel_time) / total);
/* if (p->amount > 100)
- p->amount = 0;*/
+ p->amount = 0;*/
p = p->next;
}
}
/******************************************/
/*
- * Result is stored in decreasing order in best[0-9].
- */
+* Result is stored in decreasing order in best[0-9].
+*/
static struct process **sorttmp;
+static size_t sorttmp_size = 10;
-inline void process_find_top(struct process **best)
+inline void process_find_top(struct process **cpu, struct process **mem)
{
struct process *pr;
if (sorttmp == NULL) {
- sorttmp = malloc(sizeof(struct process)*10);
+ sorttmp = malloc(sizeof(struct process)*sorttmp_size);
assert(sorttmp != NULL);
}
int total;
- int i, max;
+ unsigned int i, max;
total = calc_cpu_total(); /* calculate the total of the processor */
process_cleanup(); /* cleanup list from exited processes */
/*
- * this is really ugly,
- * not to mention probably not too efficient.
- * the main problem is that there could be any number of processes,
- * however we have to use a fixed size for the "best" array.
- * right now i can't think of a better way to do this,
- * although i'm sure there is one.
- * Perhaps just using a linked list would be more effecient?
- * I'm too fucking lazy to do that right now.
- */
- pr = first_process;
- i = 0;
- while(pr) {
- if(i<300 && pr->counted) {
- sorttmp[i] = pr;
- i++;
+ * this is really ugly,
+ * not to mention probably not too efficient.
+ * the main problem is that there could be any number of processes,
+ * however we have to use a fixed size for the "best" array.
+ * right now i can't think of a better way to do this,
+ * although i'm sure there is one.
+ * Perhaps just using a linked list would be more effecient?
+ * I'm too fucking lazy to do that right now.
+ */
+ if(top_cpu) {
+ pr = first_process;
+ i = 0;
+ while(pr) {
+ if(i<sorttmp_size && pr->counted) {
+ sorttmp[i] = pr;
+ i++;
+ }
+ else if (i == sorttmp_size && pr->counted) {
+ sorttmp_size++;
+ sorttmp = realloc(sorttmp, sizeof(struct process)*sorttmp_size);
+ sorttmp[i] = pr;
+ i++;
+ }
+ pr = pr->next;
}
- else if (i>299) {
- /*ERR("too many processes, you will get innaccurate results from top");*/
- break;
+ if (i+1 < sorttmp_size) {
+ sorttmp = realloc(sorttmp, sizeof(struct process)*sorttmp_size);
}
- pr = pr->next;
- }
- max = i;
- if(top_sort_cpu) {
- for(i=0;i<max-1;i++)
- {
- while (sorttmp[i+1]->amount > sorttmp[i]->amount)
+ max = i;
+ for(i=0;i<max-1;i++)
{
- pr = sorttmp[i];
- sorttmp[i] = sorttmp[i+1];
- sorttmp[i+1] = pr;
- if (i>0)
- i--;
- else
- break;
- }
+ while (sorttmp[i+1]->amount > sorttmp[i]->amount)
+ {
+ pr = sorttmp[i];
+ sorttmp[i] = sorttmp[i+1];
+ sorttmp[i+1] = pr;
+ if (i>0)
+ i--;
+ else
+ break;
+ }
- }
- for(i=max;i>1;i--);
- {
- while (sorttmp[i]->amount > sorttmp[i-1]->amount)
+ }
+ for(i=max;i>1;i--);
{
- pr = sorttmp[i];
- sorttmp[i] = sorttmp[i-1];
- sorttmp[i-1] = pr;
- if (i<max)
- i++;
- else
- break;
+ while (sorttmp[i]->amount > sorttmp[i-1]->amount)
+ {
+ pr = sorttmp[i];
+ sorttmp[i] = sorttmp[i-1];
+ sorttmp[i-1] = pr;
+ if (i<max)
+ i++;
+ else
+ break;
+ }
}
- }
- for(i=0;i<10;i++)
- {
- best[i] = sorttmp[i];
+ for(i=0;i<10;i++)
+ {
+ cpu[i] = sorttmp[i];
+ }
}
- }
- else {
+ if (top_mem) {
+ pr = first_process;
+ i = 0;
+ while(pr) {
+ if(i<sorttmp_size && pr->counted) {
+ sorttmp[i] = pr;
+ i++;
+ }
+ else if (i == sorttmp_size && pr->counted) {
+ sorttmp_size++;
+ sorttmp = realloc(sorttmp, sizeof(struct process)*sorttmp_size);
+ sorttmp[i] = pr;
+ i++;
+ }
+ pr = pr->next;
+ }
+ if (i+1 < sorttmp_size) {
+ sorttmp = realloc(sorttmp, sizeof(struct process)*sorttmp_size);
+ } max = i;
for(i=0;i<max-1;i++)
{
while (sorttmp[i+1]->totalmem > sorttmp[i]->totalmem)
}
for(i=0;i<10;i++)
{
- best[i] = sorttmp[i];
+ mem[i] = sorttmp[i];
}
}