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-2007 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;
112 char line[BUFFER_LEN] = { 0 }, filename[BUFFER_LEN], procname[BUFFER_LEN];
114 unsigned long user_time = 0;
115 unsigned long kernel_time = 0;
118 char deparenthesised_name[BUFFER_LEN];
122 snprintf(filename, sizeof(filename), PROCFS_TEMPLATE, process->pid);
124 ps = open(filename, O_RDONLY);
126 /* The process must have finished in the last few jiffies! */
130 /* Mark process as up-to-date. */
131 process->time_stamp = g_time;
133 rc = read(ps, line, sizeof(line));
139 /* Extract cpu times from data in /proc filesystem */
140 rc = sscanf(line, "%*s %s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %lu "
141 "%lu %*s %*s %*s %d %*s %*s %*s %u %u", procname, &process->user_time,
142 &process->kernel_time, &nice_val, &process->vsize, &process->rss);
146 /* Remove parentheses from the process name stored in /proc/ under Linux */
148 /* remove any "kdeinit: " */
149 if (r == strstr(r, "kdeinit")) {
150 snprintf(filename, sizeof(filename), PROCFS_CMDLINE_TEMPLATE,
153 ps = open(filename, O_RDONLY);
155 /* The process must have finished in the last few jiffies! */
159 endl = read(ps, line, sizeof(line));
162 /* null terminate the input */
164 /* account for "kdeinit: " */
165 if ((char *) line == strstr(line, "kdeinit: ")) {
166 r = ((char *) line) + 9;
171 q = deparenthesised_name;
173 while (*r && *r != ' ') {
178 q = deparenthesised_name;
179 while (*r && *r != ')') {
188 process->name = strdup(deparenthesised_name);
189 process->rss *= getpagesize();
192 update_total_processes();
195 process->total_cpu_time = process->user_time + process->kernel_time;
196 process->totalmem = (float) (((float) process->rss / cur->memmax) / 10);
197 if (process->previous_user_time == ULONG_MAX) {
198 process->previous_user_time = process->user_time;
200 if (process->previous_kernel_time == ULONG_MAX) {
201 process->previous_kernel_time = process->kernel_time;
204 /* store the difference of the user_time */
205 user_time = process->user_time - process->previous_user_time;
206 kernel_time = process->kernel_time - process->previous_kernel_time;
208 /* backup the process->user_time for next time around */
209 process->previous_user_time = process->user_time;
210 process->previous_kernel_time = process->kernel_time;
212 /* store only the difference of the user_time here... */
213 process->user_time = user_time;
214 process->kernel_time = kernel_time;
219 /******************************************
220 * Get process structure for process pid *
221 ******************************************/
223 /* This function seems to hog all of the CPU time.
224 * I can't figure out why - it doesn't do much. */
225 static int calculate_cpu(struct process *process)
229 /* compute each process cpu usage by reading /proc/<proc#>/stat */
230 rc = process_parse_stat(process);
233 /* rc = process_parse_statm(process); if (rc) return 1; */
236 * Check name against the exclusion list
238 /* if (process->counted && exclusion_expression &&
239 * !regexec(exclusion_expression, process->name, 0, 0, 0))
240 * process->counted = 0; */
245 /******************************************
246 * Update process table *
247 ******************************************/
249 static int update_process_table(void)
252 struct dirent *entry;
254 if (!(dir = opendir("/proc"))) {
260 /* Get list of processes from /proc directory */
261 while ((entry = readdir(dir))) {
265 /* Problem reading list of processes */
270 if (sscanf(entry->d_name, "%d", &pid) > 0) {
273 p = find_process(pid);
275 p = new_process(pid);
278 /* compute each process cpu usage */
288 /******************************************
289 * Destroy and remove a process *
290 ******************************************/
292 static void delete_process(struct process *p)
294 #if defined(PARANOID)
295 assert(p->id == 0x0badfeed);
298 * Ensure that deleted processes aren't reused.
301 #endif /* defined(PARANOID) */
304 * Maintain doubly linked list.
307 p->next->previous = p->previous;
309 p->previous->next = p->next;
311 first_process = p->next;
319 /******************************************
320 * Strip dead process entries *
321 ******************************************/
323 static void process_cleanup(void)
326 struct process *p = first_process;
329 struct process *current = p;
331 #if defined(PARANOID)
332 assert(p->id == 0x0badfeed);
333 #endif /* defined(PARANOID) */
336 /* Delete processes that have died */
337 if (current->time_stamp != g_time) {
338 delete_process(current);
343 /******************************************
344 * Calculate cpu total *
345 ******************************************/
346 #define TMPL_SHORTPROC "%*s %llu %llu %llu %llu"
347 #define TMPL_LONGPROC "%*s %llu %llu %llu %llu %llu %llu %llu %llu"
349 static unsigned long long calc_cpu_total(void)
351 unsigned long long total = 0;
352 unsigned long long t = 0;
355 char line[BUFFER_LEN] = { 0 };
356 unsigned long long cpu = 0;
357 unsigned long long nice = 0;
358 unsigned long long system = 0;
359 unsigned long long idle = 0;
360 unsigned long long iowait = 0;
361 unsigned long long irq = 0;
362 unsigned long long softirq = 0;
363 unsigned long long steal = 0;
364 const char *template =
365 KFLAG_ISSET(KFLAG_IS_LONGSTAT) ? TMPL_LONGPROC : TMPL_SHORTPROC;
367 ps = open("/proc/stat", O_RDONLY);
368 rc = read(ps, line, sizeof(line));
374 sscanf(line, template, &cpu, &nice, &system, &idle, &iowait, &irq,
376 total = cpu + nice + system + idle + iowait + irq + softirq + steal;
378 t = total - previous_total;
379 previous_total = total;
384 /******************************************
385 * Calculate each processes cpu *
386 ******************************************/
388 inline static void calc_cpu_each(unsigned long long total)
390 struct process *p = first_process;
393 p->amount = 100.0 * (cpu_separate ? info.cpu_count : 1) *
394 (p->user_time + p->kernel_time) / (float) total;
400 /******************************************
401 * Find the top processes *
402 ******************************************/
404 /* free a sp_process structure */
405 void free_sp(struct sorted_process *sp)
410 /* create a new sp_process structure */
411 struct sorted_process *malloc_sp(struct process *proc)
413 struct sorted_process *sp;
414 sp = malloc(sizeof(struct sorted_process));
421 /* cpu comparison function for insert_sp_element */
422 int compare_cpu(struct process *a, struct process *b)
424 if (a->amount < b->amount) {
426 } else if (a->amount > b->amount) {
433 /* mem comparison function for insert_sp_element */
434 int compare_mem(struct process *a, struct process *b)
436 if (a->totalmem < b->totalmem) {
438 } else if (a->totalmem > b->totalmem) {
445 /* insert this process into the list in a sorted fashion,
446 * or destroy it if it doesn't fit on the list */
447 int insert_sp_element(struct sorted_process *sp_cur,
448 struct sorted_process **p_sp_head, struct sorted_process **p_sp_tail,
449 int max_elements, int compare_funct(struct process *, struct process *))
452 struct sorted_process *sp_readthru = NULL, *sp_destroy = NULL;
453 int did_insert = 0, x = 0;
455 if (*p_sp_head == NULL) {
460 for (sp_readthru = *p_sp_head, x = 0;
461 sp_readthru != NULL && x < max_elements;
462 sp_readthru = sp_readthru->less, x++) {
463 if (compare_funct(sp_readthru->proc, sp_cur->proc) > 0 && !did_insert) {
464 /* sp_cur is bigger than sp_readthru
465 * so insert it before sp_readthru */
466 sp_cur->less = sp_readthru;
467 if (sp_readthru == *p_sp_head) {
468 /* insert as the new head of the list */
471 /* insert inside the list */
472 sp_readthru->greater->less = sp_cur;
473 sp_cur->greater = sp_readthru->greater;
475 sp_readthru->greater = sp_cur;
476 /* element was inserted, so increase the counter */
480 if (x < max_elements && sp_readthru == NULL && !did_insert) {
481 /* sp_cur is the smallest element and list isn't full,
482 * so insert at the end */
483 (*p_sp_tail)->less = sp_cur;
484 sp_cur->greater = *p_sp_tail;
487 } else if (x >= max_elements) {
488 /* We inserted an element and now the list is too big by one.
489 * Destroy the smallest element */
490 sp_destroy = *p_sp_tail;
491 *p_sp_tail = sp_destroy->greater;
492 (*p_sp_tail)->less = NULL;
496 /* sp_cur wasn't added to the sorted list, so destroy it */
502 /* copy the procs in the sorted list to the array, and destroy the list */
503 void sp_acopy(struct sorted_process *sp_head, struct process **ar, int max_size)
505 struct sorted_process *sp_cur, *sp_tmp;
509 for (x = 0; x < max_size && sp_cur != NULL; x++) {
510 ar[x] = sp_cur->proc;
512 sp_cur = sp_cur->less;
517 /* ****************************************************************** *
518 * Get a sorted list of the top cpu hogs and top mem hogs. *
519 * Results are stored in the cpu,mem arrays in decreasing order[0-9]. *
520 * ****************************************************************** */
522 inline void process_find_top(struct process **cpu, struct process **mem)
524 struct sorted_process *spc_head = NULL, *spc_tail = NULL, *spc_cur = NULL;
525 struct sorted_process *spm_head = NULL, *spm_tail = NULL, *spm_cur = NULL;
526 struct process *cur_proc = NULL;
527 unsigned long long total = 0;
529 if (!top_cpu && !top_mem) {
533 total = calc_cpu_total(); /* calculate the total of the processor */
534 update_process_table(); /* update the table with process list */
535 calc_cpu_each(total); /* and then the percentage for each task */
536 process_cleanup(); /* cleanup list from exited processes */
538 cur_proc = first_process;
540 while (cur_proc != NULL) {
542 spc_cur = malloc_sp(cur_proc);
543 insert_sp_element(spc_cur, &spc_head, &spc_tail, MAX_SP,
547 spm_cur = malloc_sp(cur_proc);
548 insert_sp_element(spm_cur, &spm_head, &spm_tail, MAX_SP,
551 cur_proc = cur_proc->next;
553 sp_acopy(spc_head, cpu, MAX_SP);
554 sp_acopy(spm_head, mem, MAX_SP);