1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2005 Adi Zaimi, Dan Piponi <dan@tanelorn.demon.co.uk>,
10 * Dave Clark <clarkd@skynet.ca>
11 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
13 * All rights reserved.
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 static unsigned long g_time = 0;
32 static unsigned long long previous_total = 0;
33 static struct process *first_process = 0;
35 struct process *get_first_process(void)
40 void free_all_processes(void)
42 struct process *next = NULL, *pr = first_process;
55 static struct process *find_process(pid_t pid)
57 struct process *p = first_process;
68 /* Create a new process object and insert it into the process list */
69 static struct process *new_process(int p)
71 struct process *process;
72 process = (struct process *) malloc(sizeof(struct process));
74 // clean up memory first
75 memset(process, 0, sizeof(struct process));
77 /* Do stitching necessary for doubly linked list */
79 process->previous = 0;
80 process->next = first_process;
82 process->next->previous = process;
84 first_process = process;
87 process->time_stamp = 0;
88 process->previous_user_time = ULONG_MAX;
89 process->previous_kernel_time = ULONG_MAX;
91 process->previous_read_bytes = ULLONG_MAX;
92 process->previous_write_bytes = ULLONG_MAX;
96 /* process_find_name(process); */
101 /******************************************
103 ******************************************/
105 /******************************************
106 * Extract information from /proc *
107 ******************************************/
109 /* These are the guts that extract information out of /proc.
110 * Anyone hoping to port wmtop should look here first. */
111 static int process_parse_stat(struct process *process)
113 struct information *cur = &info;
114 char line[BUFFER_LEN] = { 0 }, filename[BUFFER_LEN], procname[BUFFER_LEN];
116 unsigned long user_time = 0;
117 unsigned long kernel_time = 0;
122 char *lparen, *rparen;
124 snprintf(filename, sizeof(filename), PROCFS_TEMPLATE, process->pid);
126 ps = open(filename, O_RDONLY);
128 /* The process must have finished in the last few jiffies! */
132 /* Mark process as up-to-date. */
133 process->time_stamp = g_time;
135 rc = read(ps, line, sizeof(line));
141 /* Extract cpu times from data in /proc filesystem */
142 lparen = strchr(line, '(');
143 rparen = strrchr(line, ')');
144 if(!lparen || !rparen || rparen < lparen)
145 return 1; // this should not happen
147 rc = MIN((unsigned)(rparen - lparen - 1), sizeof(procname) - 1);
148 strncpy(procname, lparen + 1, rc);
150 rc = sscanf(rparen + 1, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %lu "
151 "%lu %*s %*s %*s %d %*s %*s %*s %u %u", &process->user_time,
152 &process->kernel_time, &nice_val, &process->vsize, &process->rss);
156 /* remove any "kdeinit: " */
157 if (procname == strstr(procname, "kdeinit")) {
158 snprintf(filename, sizeof(filename), PROCFS_CMDLINE_TEMPLATE,
161 ps = open(filename, O_RDONLY);
163 /* The process must have finished in the last few jiffies! */
167 endl = read(ps, line, sizeof(line));
170 /* null terminate the input */
172 /* account for "kdeinit: " */
173 if ((char *) line == strstr(line, "kdeinit: ")) {
174 r = ((char *) line) + 9;
181 while (*r && *r != ' ') {
190 process->name = strndup(procname, text_buffer_size);
191 process->rss *= getpagesize();
194 update_total_processes();
197 process->total_cpu_time = process->user_time + process->kernel_time;
198 process->totalmem = (float) (((float) process->rss / cur->memmax) / 10);
199 if (process->previous_user_time == ULONG_MAX) {
200 process->previous_user_time = process->user_time;
202 if (process->previous_kernel_time == ULONG_MAX) {
203 process->previous_kernel_time = process->kernel_time;
206 /* store the difference of the user_time */
207 user_time = process->user_time - process->previous_user_time;
208 kernel_time = process->kernel_time - process->previous_kernel_time;
210 /* backup the process->user_time for next time around */
211 process->previous_user_time = process->user_time;
212 process->previous_kernel_time = process->kernel_time;
214 /* store only the difference of the user_time here... */
215 process->user_time = user_time;
216 process->kernel_time = kernel_time;
222 static int process_parse_io(struct process *process)
224 static const char *read_bytes_str="read_bytes:";
225 static const char *write_bytes_str="write_bytes:";
227 char line[BUFFER_LEN] = { 0 }, filename[BUFFER_LEN];
231 unsigned long long read_bytes, write_bytes;
233 snprintf(filename, sizeof(filename), PROCFS_TEMPLATE_IO, process->pid);
235 ps = open(filename, O_RDONLY);
237 /* The process must have finished in the last few jiffies!
238 * Or, the kernel doesn't support I/O accounting.
243 rc = read(ps, line, sizeof(line));
249 pos = strstr(line, read_bytes_str);
251 /* these should not happen (unless the format of the file changes) */
254 pos += strlen(read_bytes_str);
255 process->read_bytes = strtoull(pos, &endpos, 10);
260 pos = strstr(line, write_bytes_str);
264 pos += strlen(write_bytes_str);
265 process->write_bytes = strtoull(pos, &endpos, 10);
270 if (process->previous_read_bytes == ULLONG_MAX) {
271 process->previous_read_bytes = process->read_bytes;
273 if (process->previous_write_bytes == ULLONG_MAX) {
274 process->previous_write_bytes = process->write_bytes;
277 /* store the difference of the byte counts */
278 read_bytes = process->read_bytes - process->previous_read_bytes;
279 write_bytes = process->write_bytes - process->previous_write_bytes;
281 /* backup the counts for next time around */
282 process->previous_read_bytes = process->read_bytes;
283 process->previous_write_bytes = process->write_bytes;
285 /* store only the difference here... */
286 process->read_bytes = read_bytes;
287 process->write_bytes = write_bytes;
293 /******************************************
294 * Get process structure for process pid *
295 ******************************************/
297 /* This function seems to hog all of the CPU time.
298 * I can't figure out why - it doesn't do much. */
299 static int calculate_stats(struct process *process)
303 /* compute each process cpu usage by reading /proc/<proc#>/stat */
304 rc = process_parse_stat(process);
307 /* rc = process_parse_statm(process); if (rc) return 1; */
310 rc = process_parse_io(process);
316 * Check name against the exclusion list
318 /* if (process->counted && exclusion_expression &&
319 * !regexec(exclusion_expression, process->name, 0, 0, 0))
320 * process->counted = 0; */
325 /******************************************
326 * Update process table *
327 ******************************************/
329 static int update_process_table(void)
332 struct dirent *entry;
334 if (!(dir = opendir("/proc"))) {
340 /* Get list of processes from /proc directory */
341 while ((entry = readdir(dir))) {
345 /* Problem reading list of processes */
350 if (sscanf(entry->d_name, "%d", &pid) > 0) {
353 p = find_process(pid);
355 p = new_process(pid);
358 /* compute each process cpu usage */
368 /******************************************
369 * Destroy and remove a process *
370 ******************************************/
372 static void delete_process(struct process *p)
374 #if defined(PARANOID)
375 assert(p->id == 0x0badfeed);
378 * Ensure that deleted processes aren't reused.
381 #endif /* defined(PARANOID) */
384 * Maintain doubly linked list.
387 p->next->previous = p->previous;
389 p->previous->next = p->next;
391 first_process = p->next;
399 /******************************************
400 * Strip dead process entries *
401 ******************************************/
403 static void process_cleanup(void)
406 struct process *p = first_process;
409 struct process *current = p;
411 #if defined(PARANOID)
412 assert(p->id == 0x0badfeed);
413 #endif /* defined(PARANOID) */
416 /* Delete processes that have died */
417 if (current->time_stamp != g_time) {
418 delete_process(current);
423 /******************************************
424 * Calculate cpu total *
425 ******************************************/
426 #define TMPL_SHORTPROC "%*s %llu %llu %llu %llu"
427 #define TMPL_LONGPROC "%*s %llu %llu %llu %llu %llu %llu %llu %llu"
429 static unsigned long long calc_cpu_total(void)
431 unsigned long long total = 0;
432 unsigned long long t = 0;
435 char line[BUFFER_LEN] = { 0 };
436 unsigned long long cpu = 0;
437 unsigned long long niceval = 0;
438 unsigned long long systemval = 0;
439 unsigned long long idle = 0;
440 unsigned long long iowait = 0;
441 unsigned long long irq = 0;
442 unsigned long long softirq = 0;
443 unsigned long long steal = 0;
444 const char *template =
445 KFLAG_ISSET(KFLAG_IS_LONGSTAT) ? TMPL_LONGPROC : TMPL_SHORTPROC;
447 ps = open("/proc/stat", O_RDONLY);
448 rc = read(ps, line, sizeof(line));
454 sscanf(line, template, &cpu, &niceval, &systemval, &idle, &iowait, &irq,
456 total = cpu + niceval + systemval + idle + iowait + irq + softirq + steal;
458 t = total - previous_total;
459 previous_total = total;
464 /******************************************
465 * Calculate each processes cpu *
466 ******************************************/
468 inline static void calc_cpu_each(unsigned long long total)
470 struct process *p = first_process;
473 p->amount = 100.0 * (cpu_separate ? info.cpu_count : 1) *
474 (p->user_time + p->kernel_time) / (float) total;
481 static void calc_io_each(void)
484 unsigned long long sum = 0;
486 for (p = first_process; p; p = p->next)
487 sum += p->read_bytes + p->write_bytes;
490 sum = 1; /* to avoid having NANs if no I/O occured */
491 for (p = first_process; p; p = p->next)
492 p->io_perc = 100.0 * (p->read_bytes + p->write_bytes) / (float) sum;
496 /******************************************
497 * Find the top processes *
498 ******************************************/
500 /* free a sp_process structure */
501 static void free_sp(struct sorted_process *sp)
506 /* create a new sp_process structure */
507 static struct sorted_process *malloc_sp(struct process *proc)
509 struct sorted_process *sp;
510 sp = malloc(sizeof(struct sorted_process));
517 /* cpu comparison function for insert_sp_element */
518 static int compare_cpu(struct process *a, struct process *b)
520 if (a->amount < b->amount) {
522 } else if (a->amount > b->amount) {
529 /* mem comparison function for insert_sp_element */
530 static int compare_mem(struct process *a, struct process *b)
532 if (a->totalmem < b->totalmem) {
534 } else if (a->totalmem > b->totalmem) {
541 /* CPU time comparision function for insert_sp_element */
542 static int compare_time(struct process *a, struct process *b)
544 return b->total_cpu_time - a->total_cpu_time;
548 /* I/O comparision function for insert_sp_element */
549 static int compare_io(struct process *a, struct process *b)
551 if (a->io_perc < b->io_perc) {
553 } else if (a->io_perc > b->io_perc) {
561 /* insert this process into the list in a sorted fashion,
562 * or destroy it if it doesn't fit on the list */
563 static int insert_sp_element(struct sorted_process *sp_cur,
564 struct sorted_process **p_sp_head, struct sorted_process **p_sp_tail,
565 int max_elements, int compare_funct(struct process *, struct process *))
568 struct sorted_process *sp_readthru = NULL, *sp_destroy = NULL;
569 int did_insert = 0, x = 0;
571 if (*p_sp_head == NULL) {
576 for (sp_readthru = *p_sp_head, x = 0;
577 sp_readthru != NULL && x < max_elements;
578 sp_readthru = sp_readthru->less, x++) {
579 if (compare_funct(sp_readthru->proc, sp_cur->proc) > 0 && !did_insert) {
580 /* sp_cur is bigger than sp_readthru
581 * so insert it before sp_readthru */
582 sp_cur->less = sp_readthru;
583 if (sp_readthru == *p_sp_head) {
584 /* insert as the new head of the list */
587 /* insert inside the list */
588 sp_readthru->greater->less = sp_cur;
589 sp_cur->greater = sp_readthru->greater;
591 sp_readthru->greater = sp_cur;
592 /* element was inserted, so increase the counter */
596 if (x < max_elements && sp_readthru == NULL && !did_insert) {
597 /* sp_cur is the smallest element and list isn't full,
598 * so insert at the end */
599 (*p_sp_tail)->less = sp_cur;
600 sp_cur->greater = *p_sp_tail;
603 } else if (x >= max_elements) {
604 /* We inserted an element and now the list is too big by one.
605 * Destroy the smallest element */
606 sp_destroy = *p_sp_tail;
607 *p_sp_tail = sp_destroy->greater;
608 (*p_sp_tail)->less = NULL;
612 /* sp_cur wasn't added to the sorted list, so destroy it */
618 /* copy the procs in the sorted list to the array, and destroy the list */
619 static void sp_acopy(struct sorted_process *sp_head, struct process **ar, int max_size)
621 struct sorted_process *sp_cur, *sp_tmp;
625 for (x = 0; x < max_size && sp_cur != NULL; x++) {
626 ar[x] = sp_cur->proc;
628 sp_cur = sp_cur->less;
633 /* ****************************************************************** *
634 * Get a sorted list of the top cpu hogs and top mem hogs. *
635 * Results are stored in the cpu,mem arrays in decreasing order[0-9]. *
636 * ****************************************************************** */
638 void process_find_top(struct process **cpu, struct process **mem,
639 struct process **ptime
641 , struct process **io
645 struct sorted_process *spc_head = NULL, *spc_tail = NULL, *spc_cur = NULL;
646 struct sorted_process *spm_head = NULL, *spm_tail = NULL, *spm_cur = NULL;
647 struct sorted_process *spt_head = NULL, *spt_tail = NULL, *spt_cur = NULL;
649 struct sorted_process *spi_head = NULL, *spi_tail = NULL, *spi_cur = NULL;
651 struct process *cur_proc = NULL;
652 unsigned long long total = 0;
654 if (!top_cpu && !top_mem && !top_time
662 total = calc_cpu_total(); /* calculate the total of the processor */
663 update_process_table(); /* update the table with process list */
664 calc_cpu_each(total); /* and then the percentage for each task */
665 process_cleanup(); /* cleanup list from exited processes */
667 calc_io_each(); /* percentage of I/O for each task */
670 cur_proc = first_process;
672 while (cur_proc != NULL) {
674 spc_cur = malloc_sp(cur_proc);
675 insert_sp_element(spc_cur, &spc_head, &spc_tail, MAX_SP,
679 spm_cur = malloc_sp(cur_proc);
680 insert_sp_element(spm_cur, &spm_head, &spm_tail, MAX_SP,
684 spt_cur = malloc_sp(cur_proc);
685 insert_sp_element(spt_cur, &spt_head, &spt_tail, MAX_SP,
690 spi_cur = malloc_sp(cur_proc);
691 insert_sp_element(spi_cur, &spi_head, &spi_tail, MAX_SP,
695 cur_proc = cur_proc->next;
699 sp_acopy(spc_head, cpu, MAX_SP);
701 sp_acopy(spm_head, mem, MAX_SP);
703 sp_acopy(spt_head, ptime, MAX_SP);
706 sp_acopy(spi_head, io,MAX_SP);