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 struct process *get_process_by_name(const char *name)
57 struct process *p = first_process;
60 if (!strcmp(p->name, name))
67 static struct process *find_process(pid_t pid)
69 struct process *p = first_process;
80 /* Create a new process object and insert it into the process list */
81 static struct process *new_process(int p)
83 struct process *process;
84 process = (struct process *) malloc(sizeof(struct process));
86 // clean up memory first
87 memset(process, 0, sizeof(struct process));
89 /* Do stitching necessary for doubly linked list */
91 process->previous = 0;
92 process->next = first_process;
94 process->next->previous = process;
96 first_process = process;
99 process->time_stamp = 0;
100 process->previous_user_time = ULONG_MAX;
101 process->previous_kernel_time = ULONG_MAX;
103 process->previous_read_bytes = ULLONG_MAX;
104 process->previous_write_bytes = ULLONG_MAX;
106 process->counted = 1;
108 /* process_find_name(process); */
113 /******************************************
115 ******************************************/
117 /******************************************
118 * Extract information from /proc *
119 ******************************************/
121 /* These are the guts that extract information out of /proc.
122 * Anyone hoping to port wmtop should look here first. */
123 static int process_parse_stat(struct process *process)
125 struct information *cur = &info;
126 char line[BUFFER_LEN] = { 0 }, filename[BUFFER_LEN], procname[BUFFER_LEN];
128 unsigned long user_time = 0;
129 unsigned long kernel_time = 0;
134 char *lparen, *rparen;
136 snprintf(filename, sizeof(filename), PROCFS_TEMPLATE, process->pid);
138 ps = open(filename, O_RDONLY);
140 /* The process must have finished in the last few jiffies! */
144 /* Mark process as up-to-date. */
145 process->time_stamp = g_time;
147 rc = read(ps, line, sizeof(line));
153 /* Extract cpu times from data in /proc filesystem */
154 lparen = strchr(line, '(');
155 rparen = strrchr(line, ')');
156 if(!lparen || !rparen || rparen < lparen)
157 return 1; // this should not happen
159 rc = MIN((unsigned)(rparen - lparen - 1), sizeof(procname) - 1);
160 strncpy(procname, lparen + 1, rc);
162 rc = sscanf(rparen + 1, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %lu "
163 "%lu %*s %*s %*s %d %*s %*s %*s %u %u", &process->user_time,
164 &process->kernel_time, &nice_val, &process->vsize, &process->rss);
168 /* remove any "kdeinit: " */
169 if (procname == strstr(procname, "kdeinit")) {
170 snprintf(filename, sizeof(filename), PROCFS_CMDLINE_TEMPLATE,
173 ps = open(filename, O_RDONLY);
175 /* The process must have finished in the last few jiffies! */
179 endl = read(ps, line, sizeof(line));
182 /* null terminate the input */
184 /* account for "kdeinit: " */
185 if ((char *) line == strstr(line, "kdeinit: ")) {
186 r = ((char *) line) + 9;
193 while (*r && *r != ' ') {
202 process->name = strndup(procname, text_buffer_size);
203 process->rss *= getpagesize();
206 update_total_processes();
209 process->total_cpu_time = process->user_time + process->kernel_time;
210 process->totalmem = (float) (((float) process->rss / cur->memmax) / 10);
211 if (process->previous_user_time == ULONG_MAX) {
212 process->previous_user_time = process->user_time;
214 if (process->previous_kernel_time == ULONG_MAX) {
215 process->previous_kernel_time = process->kernel_time;
218 /* store the difference of the user_time */
219 user_time = process->user_time - process->previous_user_time;
220 kernel_time = process->kernel_time - process->previous_kernel_time;
222 /* backup the process->user_time for next time around */
223 process->previous_user_time = process->user_time;
224 process->previous_kernel_time = process->kernel_time;
226 /* store only the difference of the user_time here... */
227 process->user_time = user_time;
228 process->kernel_time = kernel_time;
234 static int process_parse_io(struct process *process)
236 static const char *read_bytes_str="read_bytes:";
237 static const char *write_bytes_str="write_bytes:";
239 char line[BUFFER_LEN] = { 0 }, filename[BUFFER_LEN];
243 unsigned long long read_bytes, write_bytes;
245 snprintf(filename, sizeof(filename), PROCFS_TEMPLATE_IO, process->pid);
247 ps = open(filename, O_RDONLY);
249 /* The process must have finished in the last few jiffies!
250 * Or, the kernel doesn't support I/O accounting.
255 rc = read(ps, line, sizeof(line));
261 pos = strstr(line, read_bytes_str);
263 /* these should not happen (unless the format of the file changes) */
266 pos += strlen(read_bytes_str);
267 process->read_bytes = strtoull(pos, &endpos, 10);
272 pos = strstr(line, write_bytes_str);
276 pos += strlen(write_bytes_str);
277 process->write_bytes = strtoull(pos, &endpos, 10);
282 if (process->previous_read_bytes == ULLONG_MAX) {
283 process->previous_read_bytes = process->read_bytes;
285 if (process->previous_write_bytes == ULLONG_MAX) {
286 process->previous_write_bytes = process->write_bytes;
289 /* store the difference of the byte counts */
290 read_bytes = process->read_bytes - process->previous_read_bytes;
291 write_bytes = process->write_bytes - process->previous_write_bytes;
293 /* backup the counts for next time around */
294 process->previous_read_bytes = process->read_bytes;
295 process->previous_write_bytes = process->write_bytes;
297 /* store only the difference here... */
298 process->read_bytes = read_bytes;
299 process->write_bytes = write_bytes;
305 /******************************************
306 * Get process structure for process pid *
307 ******************************************/
309 /* This function seems to hog all of the CPU time.
310 * I can't figure out why - it doesn't do much. */
311 static int calculate_stats(struct process *process)
315 /* compute each process cpu usage by reading /proc/<proc#>/stat */
316 rc = process_parse_stat(process);
319 /* rc = process_parse_statm(process); if (rc) return 1; */
322 rc = process_parse_io(process);
328 * Check name against the exclusion list
330 /* if (process->counted && exclusion_expression &&
331 * !regexec(exclusion_expression, process->name, 0, 0, 0))
332 * process->counted = 0; */
337 /******************************************
338 * Update process table *
339 ******************************************/
341 static int update_process_table(void)
344 struct dirent *entry;
346 if (!(dir = opendir("/proc"))) {
352 /* Get list of processes from /proc directory */
353 while ((entry = readdir(dir))) {
357 /* Problem reading list of processes */
362 if (sscanf(entry->d_name, "%d", &pid) > 0) {
365 p = find_process(pid);
367 p = new_process(pid);
370 /* compute each process cpu usage */
380 /******************************************
381 * Destroy and remove a process *
382 ******************************************/
384 static void delete_process(struct process *p)
386 #if defined(PARANOID)
387 assert(p->id == 0x0badfeed);
390 * Ensure that deleted processes aren't reused.
393 #endif /* defined(PARANOID) */
396 * Maintain doubly linked list.
399 p->next->previous = p->previous;
401 p->previous->next = p->next;
403 first_process = p->next;
411 /******************************************
412 * Strip dead process entries *
413 ******************************************/
415 static void process_cleanup(void)
418 struct process *p = first_process;
421 struct process *current = p;
423 #if defined(PARANOID)
424 assert(p->id == 0x0badfeed);
425 #endif /* defined(PARANOID) */
428 /* Delete processes that have died */
429 if (current->time_stamp != g_time) {
430 delete_process(current);
435 /******************************************
436 * Calculate cpu total *
437 ******************************************/
438 #define TMPL_SHORTPROC "%*s %llu %llu %llu %llu"
439 #define TMPL_LONGPROC "%*s %llu %llu %llu %llu %llu %llu %llu %llu"
441 static unsigned long long calc_cpu_total(void)
443 unsigned long long total = 0;
444 unsigned long long t = 0;
447 char line[BUFFER_LEN] = { 0 };
448 unsigned long long cpu = 0;
449 unsigned long long niceval = 0;
450 unsigned long long systemval = 0;
451 unsigned long long idle = 0;
452 unsigned long long iowait = 0;
453 unsigned long long irq = 0;
454 unsigned long long softirq = 0;
455 unsigned long long steal = 0;
456 const char *template =
457 KFLAG_ISSET(KFLAG_IS_LONGSTAT) ? TMPL_LONGPROC : TMPL_SHORTPROC;
459 ps = open("/proc/stat", O_RDONLY);
460 rc = read(ps, line, sizeof(line));
466 sscanf(line, template, &cpu, &niceval, &systemval, &idle, &iowait, &irq,
468 total = cpu + niceval + systemval + idle + iowait + irq + softirq + steal;
470 t = total - previous_total;
471 previous_total = total;
476 /******************************************
477 * Calculate each processes cpu *
478 ******************************************/
480 inline static void calc_cpu_each(unsigned long long total)
482 struct process *p = first_process;
485 p->amount = 100.0 * (cpu_separate ? info.cpu_count : 1) *
486 (p->user_time + p->kernel_time) / (float) total;
493 static void calc_io_each(void)
496 unsigned long long sum = 0;
498 for (p = first_process; p; p = p->next)
499 sum += p->read_bytes + p->write_bytes;
502 sum = 1; /* to avoid having NANs if no I/O occured */
503 for (p = first_process; p; p = p->next)
504 p->io_perc = 100.0 * (p->read_bytes + p->write_bytes) / (float) sum;
508 /******************************************
509 * Find the top processes *
510 ******************************************/
512 /* free a sp_process structure */
513 static void free_sp(struct sorted_process *sp)
518 /* create a new sp_process structure */
519 static struct sorted_process *malloc_sp(struct process *proc)
521 struct sorted_process *sp;
522 sp = malloc(sizeof(struct sorted_process));
529 /* cpu comparison function for insert_sp_element */
530 static int compare_cpu(struct process *a, struct process *b)
532 if (a->amount < b->amount) {
534 } else if (a->amount > b->amount) {
541 /* mem comparison function for insert_sp_element */
542 static int compare_mem(struct process *a, struct process *b)
544 if (a->totalmem < b->totalmem) {
546 } else if (a->totalmem > b->totalmem) {
553 /* CPU time comparision function for insert_sp_element */
554 static int compare_time(struct process *a, struct process *b)
556 return b->total_cpu_time - a->total_cpu_time;
560 /* I/O comparision function for insert_sp_element */
561 static int compare_io(struct process *a, struct process *b)
563 if (a->io_perc < b->io_perc) {
565 } else if (a->io_perc > b->io_perc) {
573 /* insert this process into the list in a sorted fashion,
574 * or destroy it if it doesn't fit on the list */
575 static int insert_sp_element(struct sorted_process *sp_cur,
576 struct sorted_process **p_sp_head, struct sorted_process **p_sp_tail,
577 int max_elements, int compare_funct(struct process *, struct process *))
580 struct sorted_process *sp_readthru = NULL, *sp_destroy = NULL;
581 int did_insert = 0, x = 0;
583 if (*p_sp_head == NULL) {
588 for (sp_readthru = *p_sp_head, x = 0;
589 sp_readthru != NULL && x < max_elements;
590 sp_readthru = sp_readthru->less, x++) {
591 if (compare_funct(sp_readthru->proc, sp_cur->proc) > 0 && !did_insert) {
592 /* sp_cur is bigger than sp_readthru
593 * so insert it before sp_readthru */
594 sp_cur->less = sp_readthru;
595 if (sp_readthru == *p_sp_head) {
596 /* insert as the new head of the list */
599 /* insert inside the list */
600 sp_readthru->greater->less = sp_cur;
601 sp_cur->greater = sp_readthru->greater;
603 sp_readthru->greater = sp_cur;
604 /* element was inserted, so increase the counter */
608 if (x < max_elements && sp_readthru == NULL && !did_insert) {
609 /* sp_cur is the smallest element and list isn't full,
610 * so insert at the end */
611 (*p_sp_tail)->less = sp_cur;
612 sp_cur->greater = *p_sp_tail;
615 } else if (x >= max_elements) {
616 /* We inserted an element and now the list is too big by one.
617 * Destroy the smallest element */
618 sp_destroy = *p_sp_tail;
619 *p_sp_tail = sp_destroy->greater;
620 (*p_sp_tail)->less = NULL;
624 /* sp_cur wasn't added to the sorted list, so destroy it */
630 /* copy the procs in the sorted list to the array, and destroy the list */
631 static void sp_acopy(struct sorted_process *sp_head, struct process **ar, int max_size)
633 struct sorted_process *sp_cur, *sp_tmp;
637 for (x = 0; x < max_size && sp_cur != NULL; x++) {
638 ar[x] = sp_cur->proc;
640 sp_cur = sp_cur->less;
645 /* ****************************************************************** *
646 * Get a sorted list of the top cpu hogs and top mem hogs. *
647 * Results are stored in the cpu,mem arrays in decreasing order[0-9]. *
648 * ****************************************************************** */
650 void process_find_top(struct process **cpu, struct process **mem,
651 struct process **ptime
653 , struct process **io
657 struct sorted_process *spc_head = NULL, *spc_tail = NULL, *spc_cur = NULL;
658 struct sorted_process *spm_head = NULL, *spm_tail = NULL, *spm_cur = NULL;
659 struct sorted_process *spt_head = NULL, *spt_tail = NULL, *spt_cur = NULL;
661 struct sorted_process *spi_head = NULL, *spi_tail = NULL, *spi_cur = NULL;
663 struct process *cur_proc = NULL;
664 unsigned long long total = 0;
666 if (!top_cpu && !top_mem && !top_time
674 total = calc_cpu_total(); /* calculate the total of the processor */
675 update_process_table(); /* update the table with process list */
676 calc_cpu_each(total); /* and then the percentage for each task */
677 process_cleanup(); /* cleanup list from exited processes */
679 calc_io_each(); /* percentage of I/O for each task */
682 cur_proc = first_process;
684 while (cur_proc != NULL) {
686 spc_cur = malloc_sp(cur_proc);
687 insert_sp_element(spc_cur, &spc_head, &spc_tail, MAX_SP,
691 spm_cur = malloc_sp(cur_proc);
692 insert_sp_element(spm_cur, &spm_head, &spm_tail, MAX_SP,
696 spt_cur = malloc_sp(cur_proc);
697 insert_sp_element(spt_cur, &spt_head, &spt_tail, MAX_SP,
702 spi_cur = malloc_sp(cur_proc);
703 insert_sp_element(spi_cur, &spi_head, &spi_tail, MAX_SP,
707 cur_proc = cur_proc->next;
711 sp_acopy(spc_head, cpu, MAX_SP);
713 sp_acopy(spm_head, mem, MAX_SP);
715 sp_acopy(spt_head, ptime, MAX_SP);
718 sp_acopy(spi_head, io,MAX_SP);