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;
92 /* process_find_name(process); */
97 /******************************************
99 ******************************************/
101 /******************************************
102 * Extract information from /proc *
103 ******************************************/
105 /* These are the guts that extract information out of /proc.
106 * Anyone hoping to port wmtop should look here first. */
107 static int process_parse_stat(struct process *process)
109 struct information *cur = &info;
110 char line[BUFFER_LEN] = { 0 }, filename[BUFFER_LEN], procname[BUFFER_LEN];
112 unsigned long user_time = 0;
113 unsigned long kernel_time = 0;
116 char deparenthesised_name[BUFFER_LEN];
120 snprintf(filename, sizeof(filename), PROCFS_TEMPLATE, process->pid);
122 ps = open(filename, O_RDONLY);
124 /* The process must have finished in the last few jiffies! */
128 /* Mark process as up-to-date. */
129 process->time_stamp = g_time;
131 rc = read(ps, line, sizeof(line));
137 /* Extract cpu times from data in /proc filesystem */
138 rc = sscanf(line, "%*s %s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %lu "
139 "%lu %*s %*s %*s %d %*s %*s %*s %u %u", procname, &process->user_time,
140 &process->kernel_time, &nice_val, &process->vsize, &process->rss);
144 /* Remove parentheses from the process name stored in /proc/ under Linux */
146 /* remove any "kdeinit: " */
147 if (r == strstr(r, "kdeinit")) {
148 snprintf(filename, sizeof(filename), PROCFS_CMDLINE_TEMPLATE,
151 ps = open(filename, O_RDONLY);
153 /* The process must have finished in the last few jiffies! */
157 endl = read(ps, line, sizeof(line));
160 /* null terminate the input */
162 /* account for "kdeinit: " */
163 if ((char *) line == strstr(line, "kdeinit: ")) {
164 r = ((char *) line) + 9;
169 q = deparenthesised_name;
171 while (*r && *r != ' ') {
176 q = deparenthesised_name;
177 while (*r && *r != ')') {
186 process->name = strndup(deparenthesised_name, text_buffer_size);
187 process->rss *= getpagesize();
190 update_total_processes();
193 process->total_cpu_time = process->user_time + process->kernel_time;
194 process->totalmem = (float) (((float) process->rss / cur->memmax) / 10);
195 if (process->previous_user_time == ULONG_MAX) {
196 process->previous_user_time = process->user_time;
198 if (process->previous_kernel_time == ULONG_MAX) {
199 process->previous_kernel_time = process->kernel_time;
202 /* store the difference of the user_time */
203 user_time = process->user_time - process->previous_user_time;
204 kernel_time = process->kernel_time - process->previous_kernel_time;
206 /* backup the process->user_time for next time around */
207 process->previous_user_time = process->user_time;
208 process->previous_kernel_time = process->kernel_time;
210 /* store only the difference of the user_time here... */
211 process->user_time = user_time;
212 process->kernel_time = kernel_time;
217 /******************************************
218 * Get process structure for process pid *
219 ******************************************/
221 /* This function seems to hog all of the CPU time.
222 * I can't figure out why - it doesn't do much. */
223 static int calculate_cpu(struct process *process)
227 /* compute each process cpu usage by reading /proc/<proc#>/stat */
228 rc = process_parse_stat(process);
231 /* rc = process_parse_statm(process); if (rc) return 1; */
234 * Check name against the exclusion list
236 /* if (process->counted && exclusion_expression &&
237 * !regexec(exclusion_expression, process->name, 0, 0, 0))
238 * process->counted = 0; */
243 /******************************************
244 * Update process table *
245 ******************************************/
247 static int update_process_table(void)
250 struct dirent *entry;
252 if (!(dir = opendir("/proc"))) {
258 /* Get list of processes from /proc directory */
259 while ((entry = readdir(dir))) {
263 /* Problem reading list of processes */
268 if (sscanf(entry->d_name, "%d", &pid) > 0) {
271 p = find_process(pid);
273 p = new_process(pid);
276 /* compute each process cpu usage */
286 /******************************************
287 * Destroy and remove a process *
288 ******************************************/
290 static void delete_process(struct process *p)
292 #if defined(PARANOID)
293 assert(p->id == 0x0badfeed);
296 * Ensure that deleted processes aren't reused.
299 #endif /* defined(PARANOID) */
302 * Maintain doubly linked list.
305 p->next->previous = p->previous;
307 p->previous->next = p->next;
309 first_process = p->next;
317 /******************************************
318 * Strip dead process entries *
319 ******************************************/
321 static void process_cleanup(void)
324 struct process *p = first_process;
327 struct process *current = p;
329 #if defined(PARANOID)
330 assert(p->id == 0x0badfeed);
331 #endif /* defined(PARANOID) */
334 /* Delete processes that have died */
335 if (current->time_stamp != g_time) {
336 delete_process(current);
341 /******************************************
342 * Calculate cpu total *
343 ******************************************/
344 #define TMPL_SHORTPROC "%*s %llu %llu %llu %llu"
345 #define TMPL_LONGPROC "%*s %llu %llu %llu %llu %llu %llu %llu %llu"
347 static unsigned long long calc_cpu_total(void)
349 unsigned long long total = 0;
350 unsigned long long t = 0;
353 char line[BUFFER_LEN] = { 0 };
354 unsigned long long cpu = 0;
355 unsigned long long niceval = 0;
356 unsigned long long systemval = 0;
357 unsigned long long idle = 0;
358 unsigned long long iowait = 0;
359 unsigned long long irq = 0;
360 unsigned long long softirq = 0;
361 unsigned long long steal = 0;
362 const char *template =
363 KFLAG_ISSET(KFLAG_IS_LONGSTAT) ? TMPL_LONGPROC : TMPL_SHORTPROC;
365 ps = open("/proc/stat", O_RDONLY);
366 rc = read(ps, line, sizeof(line));
372 sscanf(line, template, &cpu, &niceval, &systemval, &idle, &iowait, &irq,
374 total = cpu + niceval + systemval + idle + iowait + irq + softirq + steal;
376 t = total - previous_total;
377 previous_total = total;
382 /******************************************
383 * Calculate each processes cpu *
384 ******************************************/
386 inline static void calc_cpu_each(unsigned long long total)
388 struct process *p = first_process;
391 p->amount = 100.0 * (cpu_separate ? info.cpu_count : 1) *
392 (p->user_time + p->kernel_time) / (float) total;
398 /******************************************
399 * Find the top processes *
400 ******************************************/
402 /* free a sp_process structure */
403 static void free_sp(struct sorted_process *sp)
408 /* create a new sp_process structure */
409 static struct sorted_process *malloc_sp(struct process *proc)
411 struct sorted_process *sp;
412 sp = malloc(sizeof(struct sorted_process));
419 /* cpu comparison function for insert_sp_element */
420 static int compare_cpu(struct process *a, struct process *b)
422 if (a->amount < b->amount) {
424 } else if (a->amount > b->amount) {
431 /* mem comparison function for insert_sp_element */
432 static int compare_mem(struct process *a, struct process *b)
434 if (a->totalmem < b->totalmem) {
436 } else if (a->totalmem > b->totalmem) {
443 /* CPU time comparision function for insert_sp_element */
444 static int compare_time(struct process *a, struct process *b)
446 return b->total_cpu_time - a->total_cpu_time;
449 /* insert this process into the list in a sorted fashion,
450 * or destroy it if it doesn't fit on the list */
451 static int insert_sp_element(struct sorted_process *sp_cur,
452 struct sorted_process **p_sp_head, struct sorted_process **p_sp_tail,
453 int max_elements, int compare_funct(struct process *, struct process *))
456 struct sorted_process *sp_readthru = NULL, *sp_destroy = NULL;
457 int did_insert = 0, x = 0;
459 if (*p_sp_head == NULL) {
464 for (sp_readthru = *p_sp_head, x = 0;
465 sp_readthru != NULL && x < max_elements;
466 sp_readthru = sp_readthru->less, x++) {
467 if (compare_funct(sp_readthru->proc, sp_cur->proc) > 0 && !did_insert) {
468 /* sp_cur is bigger than sp_readthru
469 * so insert it before sp_readthru */
470 sp_cur->less = sp_readthru;
471 if (sp_readthru == *p_sp_head) {
472 /* insert as the new head of the list */
475 /* insert inside the list */
476 sp_readthru->greater->less = sp_cur;
477 sp_cur->greater = sp_readthru->greater;
479 sp_readthru->greater = sp_cur;
480 /* element was inserted, so increase the counter */
484 if (x < max_elements && sp_readthru == NULL && !did_insert) {
485 /* sp_cur is the smallest element and list isn't full,
486 * so insert at the end */
487 (*p_sp_tail)->less = sp_cur;
488 sp_cur->greater = *p_sp_tail;
491 } else if (x >= max_elements) {
492 /* We inserted an element and now the list is too big by one.
493 * Destroy the smallest element */
494 sp_destroy = *p_sp_tail;
495 *p_sp_tail = sp_destroy->greater;
496 (*p_sp_tail)->less = NULL;
500 /* sp_cur wasn't added to the sorted list, so destroy it */
506 /* copy the procs in the sorted list to the array, and destroy the list */
507 static void sp_acopy(struct sorted_process *sp_head, struct process **ar, int max_size)
509 struct sorted_process *sp_cur, *sp_tmp;
513 for (x = 0; x < max_size && sp_cur != NULL; x++) {
514 ar[x] = sp_cur->proc;
516 sp_cur = sp_cur->less;
521 /* ****************************************************************** *
522 * Get a sorted list of the top cpu hogs and top mem hogs. *
523 * Results are stored in the cpu,mem arrays in decreasing order[0-9]. *
524 * ****************************************************************** */
526 void process_find_top(struct process **cpu, struct process **mem,
527 struct process **ptime)
529 struct sorted_process *spc_head = NULL, *spc_tail = NULL, *spc_cur = NULL;
530 struct sorted_process *spm_head = NULL, *spm_tail = NULL, *spm_cur = NULL;
531 struct sorted_process *spt_head = NULL, *spt_tail = NULL, *spt_cur = NULL;
532 struct process *cur_proc = NULL;
533 unsigned long long total = 0;
535 if (!top_cpu && !top_mem && !top_time) {
539 total = calc_cpu_total(); /* calculate the total of the processor */
540 update_process_table(); /* update the table with process list */
541 calc_cpu_each(total); /* and then the percentage for each task */
542 process_cleanup(); /* cleanup list from exited processes */
544 cur_proc = first_process;
546 while (cur_proc != NULL) {
548 spc_cur = malloc_sp(cur_proc);
549 insert_sp_element(spc_cur, &spc_head, &spc_tail, MAX_SP,
553 spm_cur = malloc_sp(cur_proc);
554 insert_sp_element(spm_cur, &spm_head, &spm_tail, MAX_SP,
558 spt_cur = malloc_sp(cur_proc);
559 insert_sp_element(spt_cur, &spt_head, &spt_tail, MAX_SP,
562 cur_proc = cur_proc->next;
566 sp_acopy(spc_head, cpu, MAX_SP);
568 sp_acopy(spm_head, mem, MAX_SP);
570 sp_acopy(spt_head, ptime, MAX_SP);