index processes in a hash table for faster lookup
[monky] / src / top.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2005 Adi Zaimi, Dan Piponi <dan@tanelorn.demon.co.uk>,
13  *                                        Dave Clark <clarkd@skynet.ca>
14  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
15  *      (see AUTHORS)
16  * All rights reserved.
17  *
18  * This program is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  * You should have received a copy of the GNU General Public License
28  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29  *
30  */
31
32 #include "top.h"
33 #include "logging.h"
34
35 static unsigned long g_time = 0;
36 static unsigned long long previous_total = 0;
37 static struct process *first_process = 0;
38
39 /* a simple hash table to speed up find_process() */
40 struct proc_hash_entry {
41         struct proc_hash_entry *next;
42         struct process *proc;
43 };
44 static struct proc_hash_entry proc_hash_table[256];
45
46 static void hash_process(struct process *p)
47 {
48         struct proc_hash_entry *phe;
49         static char first_run = 1;
50
51         /* better make sure all next pointers are zero upon first access */
52         if (first_run) {
53                 memset(proc_hash_table, 0, sizeof(struct proc_hash_entry) * 256);
54                 first_run = 0;
55         }
56
57         /* get the bucket head */
58         phe = &proc_hash_table[p->pid % 256];
59
60         /* find the bucket's end */
61         while (phe->next)
62                 phe = phe->next;
63
64         /* append process */
65         phe->next = malloc(sizeof(struct proc_hash_entry));
66         memset(phe->next, 0, sizeof(struct proc_hash_entry));
67         phe->next->proc = p;
68 }
69
70 static void unhash_process(struct process *p)
71 {
72         struct proc_hash_entry *phe, *tmp;
73
74         /* get the bucket head */
75         phe = &proc_hash_table[p->pid % 256];
76
77         /* find the entry pointing to p and drop it */
78         while (phe->next) {
79                 if (phe->next->proc == p) {
80                         tmp = phe->next;
81                         phe->next = phe->next->next;
82                         free(tmp);
83                         return;
84                 }
85                 phe = phe->next;
86         }
87 }
88
89 static void __unhash_all_processes(struct proc_hash_entry *phe)
90 {
91         if (phe->next)
92                 __unhash_all_processes(phe->next);
93         free(phe->next);
94 }
95
96 static void unhash_all_processes(void)
97 {
98         int i;
99
100         for (i = 0; i < 256; i++) {
101                 __unhash_all_processes(&proc_hash_table[i]);
102                 proc_hash_table[i].next = NULL;
103         }
104 }
105
106 struct process *get_first_process(void)
107 {
108         return first_process;
109 }
110
111 void free_all_processes(void)
112 {
113         struct process *next = NULL, *pr = first_process;
114
115         while (pr) {
116                 next = pr->next;
117                 if (pr->name) {
118                         free(pr->name);
119                 }
120                 free(pr);
121                 pr = next;
122         }
123         first_process = NULL;
124
125         /* drop the whole hash table */
126         unhash_all_processes();
127 }
128
129 struct process *get_process_by_name(const char *name)
130 {
131         struct process *p = first_process;
132
133         while (p) {
134                 if (!strcmp(p->name, name))
135                         return p;
136                 p = p->next;
137         }
138         return 0;
139 }
140
141 static struct process *find_process(pid_t pid)
142 {
143         struct proc_hash_entry *phe;
144
145         phe = &proc_hash_table[pid % 256];
146         while (phe->next) {
147                 if (phe->next->proc->pid == pid)
148                         return phe->next->proc;
149                 phe = phe->next;
150         }
151         return 0;
152 }
153
154 /* Create a new process object and insert it into the process list */
155 static struct process *new_process(int p)
156 {
157         struct process *process;
158         process = (struct process *) malloc(sizeof(struct process));
159
160         // clean up memory first
161         memset(process, 0, sizeof(struct process));
162
163         /* Do stitching necessary for doubly linked list */
164         process->name = 0;
165         process->previous = 0;
166         process->next = first_process;
167         if (process->next) {
168                 process->next->previous = process;
169         }
170         first_process = process;
171
172         process->pid = p;
173         process->time_stamp = 0;
174         process->previous_user_time = ULONG_MAX;
175         process->previous_kernel_time = ULONG_MAX;
176 #ifdef IOSTATS
177         process->previous_read_bytes = ULLONG_MAX;
178         process->previous_write_bytes = ULLONG_MAX;
179 #endif /* IOSTATS */
180         process->counted = 1;
181
182         /* process_find_name(process); */
183
184         /* add the process to the hash table */
185         hash_process(process);
186
187         return process;
188 }
189
190 /******************************************
191  * Functions                                                      *
192  ******************************************/
193
194 /******************************************
195  * Extract information from /proc                 *
196  ******************************************/
197
198 /* These are the guts that extract information out of /proc.
199  * Anyone hoping to port wmtop should look here first. */
200 static int process_parse_stat(struct process *process)
201 {
202         struct information *cur = &info;
203         char line[BUFFER_LEN] = { 0 }, filename[BUFFER_LEN], procname[BUFFER_LEN];
204         int ps;
205         unsigned long user_time = 0;
206         unsigned long kernel_time = 0;
207         int rc;
208         char *r, *q;
209         int endl;
210         int nice_val;
211         char *lparen, *rparen;
212
213         snprintf(filename, sizeof(filename), PROCFS_TEMPLATE, process->pid);
214
215         ps = open(filename, O_RDONLY);
216         if (ps < 0) {
217                 /* The process must have finished in the last few jiffies! */
218                 return 1;
219         }
220
221         /* Mark process as up-to-date. */
222         process->time_stamp = g_time;
223
224         rc = read(ps, line, sizeof(line));
225         close(ps);
226         if (rc < 0) {
227                 return 1;
228         }
229
230         /* Extract cpu times from data in /proc filesystem */
231         lparen = strchr(line, '(');
232         rparen = strrchr(line, ')');
233         if(!lparen || !rparen || rparen < lparen)
234                 return 1; // this should not happen
235
236         rc = MIN((unsigned)(rparen - lparen - 1), sizeof(procname) - 1);
237         strncpy(procname, lparen + 1, rc);
238         procname[rc] = '\0';
239         rc = sscanf(rparen + 1, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %lu "
240                         "%lu %*s %*s %*s %d %*s %*s %*s %u %u", &process->user_time,
241                         &process->kernel_time, &nice_val, &process->vsize, &process->rss);
242         if (rc < 5) {
243                 NORM_ERR("scaning data for %s failed, got only %d fields", procname, rc);
244                 return 1;
245         }
246         /* remove any "kdeinit: " */
247         if (procname == strstr(procname, "kdeinit")) {
248                 snprintf(filename, sizeof(filename), PROCFS_CMDLINE_TEMPLATE,
249                                 process->pid);
250
251                 ps = open(filename, O_RDONLY);
252                 if (ps < 0) {
253                         /* The process must have finished in the last few jiffies! */
254                         return 1;
255                 }
256
257                 endl = read(ps, line, sizeof(line));
258                 close(ps);
259
260                 /* null terminate the input */
261                 line[endl] = 0;
262                 /* account for "kdeinit: " */
263                 if ((char *) line == strstr(line, "kdeinit: ")) {
264                         r = ((char *) line) + 9;
265                 } else {
266                         r = (char *) line;
267                 }
268
269                 q = procname;
270                 /* stop at space */
271                 while (*r && *r != ' ') {
272                         *q++ = *r++;
273                 }
274                 *q = 0;
275         }
276
277         if (process->name) {
278                 free(process->name);
279         }
280         process->name = strndup(procname, text_buffer_size);
281         process->rss *= getpagesize();
282
283         if (!cur->memmax) {
284                 update_total_processes();
285         }
286
287         process->total_cpu_time = process->user_time + process->kernel_time;
288         process->totalmem = (float) (((float) process->rss / cur->memmax) / 10);
289         if (process->previous_user_time == ULONG_MAX) {
290                 process->previous_user_time = process->user_time;
291         }
292         if (process->previous_kernel_time == ULONG_MAX) {
293                 process->previous_kernel_time = process->kernel_time;
294         }
295
296         /* strangely, the values aren't monotonous */
297         if (process->previous_user_time > process->user_time)
298                 process->previous_user_time = process->user_time;
299
300         if (process->previous_kernel_time > process->kernel_time)
301                 process->previous_kernel_time = process->kernel_time;
302
303         /* store the difference of the user_time */
304         user_time = process->user_time - process->previous_user_time;
305         kernel_time = process->kernel_time - process->previous_kernel_time;
306
307         /* backup the process->user_time for next time around */
308         process->previous_user_time = process->user_time;
309         process->previous_kernel_time = process->kernel_time;
310
311         /* store only the difference of the user_time here... */
312         process->user_time = user_time;
313         process->kernel_time = kernel_time;
314
315         return 0;
316 }
317
318 #ifdef IOSTATS
319 static int process_parse_io(struct process *process)
320 {
321         static const char *read_bytes_str="read_bytes:";
322         static const char *write_bytes_str="write_bytes:";
323
324         char line[BUFFER_LEN] = { 0 }, filename[BUFFER_LEN];
325         int ps;
326         int rc;
327         char *pos, *endpos;
328         unsigned long long read_bytes, write_bytes;
329
330         snprintf(filename, sizeof(filename), PROCFS_TEMPLATE_IO, process->pid);
331
332         ps = open(filename, O_RDONLY);
333         if (ps < 0) {
334                 /* The process must have finished in the last few jiffies!
335                  * Or, the kernel doesn't support I/O accounting.
336                  */
337                 return 1;
338         }
339
340         rc = read(ps, line, sizeof(line));
341         close(ps);
342         if (rc < 0) {
343                 return 1;
344         }
345
346         pos = strstr(line, read_bytes_str);
347         if (pos == NULL) {
348                 /* these should not happen (unless the format of the file changes) */
349                 return 1;
350         }
351         pos += strlen(read_bytes_str);
352         process->read_bytes = strtoull(pos, &endpos, 10);
353         if (endpos == pos) {
354                 return 1;
355         }
356
357         pos = strstr(line, write_bytes_str);
358         if (pos == NULL) {
359                 return 1;
360         }
361         pos += strlen(write_bytes_str);
362         process->write_bytes = strtoull(pos, &endpos, 10);
363         if (endpos == pos) {
364                 return 1;
365         }
366
367         if (process->previous_read_bytes == ULLONG_MAX) {
368                 process->previous_read_bytes = process->read_bytes;
369         }
370         if (process->previous_write_bytes == ULLONG_MAX) {
371                 process->previous_write_bytes = process->write_bytes;
372         }
373
374         /* store the difference of the byte counts */
375         read_bytes = process->read_bytes - process->previous_read_bytes;
376         write_bytes = process->write_bytes - process->previous_write_bytes;
377
378         /* backup the counts for next time around */
379         process->previous_read_bytes = process->read_bytes;
380         process->previous_write_bytes = process->write_bytes;
381
382         /* store only the difference here... */
383         process->read_bytes = read_bytes;
384         process->write_bytes = write_bytes;
385
386         return 0;
387 }
388 #endif /* IOSTATS */
389
390 /******************************************
391  * Get process structure for process pid  *
392  ******************************************/
393
394 /* This function seems to hog all of the CPU time.
395  * I can't figure out why - it doesn't do much. */
396 static int calculate_stats(struct process *process)
397 {
398         int rc;
399
400         /* compute each process cpu usage by reading /proc/<proc#>/stat */
401         rc = process_parse_stat(process);
402         if (rc) return 1;
403         /* rc = process_parse_statm(process); if (rc) return 1; */
404
405 #ifdef IOSTATS
406         rc = process_parse_io(process);
407         if (rc) return 1;
408 #endif /* IOSTATS */
409
410         /*
411          * Check name against the exclusion list
412          */
413         /* if (process->counted && exclusion_expression &&
414          * !regexec(exclusion_expression, process->name, 0, 0, 0))
415          * process->counted = 0; */
416
417         return 0;
418 }
419
420 /******************************************
421  * Update process table                                   *
422  ******************************************/
423
424 static int update_process_table(void)
425 {
426         DIR *dir;
427         struct dirent *entry;
428
429         if (!(dir = opendir("/proc"))) {
430                 return 1;
431         }
432
433         ++g_time;
434
435         /* Get list of processes from /proc directory */
436         while ((entry = readdir(dir))) {
437                 pid_t pid;
438
439                 if (!entry) {
440                         /* Problem reading list of processes */
441                         closedir(dir);
442                         return 1;
443                 }
444
445                 if (sscanf(entry->d_name, "%d", &pid) > 0) {
446                         struct process *p;
447
448                         p = find_process(pid);
449                         if (!p) {
450                                 p = new_process(pid);
451                         }
452
453                         /* compute each process cpu usage */
454                         calculate_stats(p);
455                 }
456         }
457
458         closedir(dir);
459
460         return 0;
461 }
462
463 /******************************************
464  * Destroy and remove a process           *
465  ******************************************/
466
467 static void delete_process(struct process *p)
468 {
469 #if defined(PARANOID)
470         assert(p->id == 0x0badfeed);
471
472         /*
473          * Ensure that deleted processes aren't reused.
474          */
475         p->id = 0x007babe;
476 #endif /* defined(PARANOID) */
477
478         /*
479          * Maintain doubly linked list.
480          */
481         if (p->next)
482                 p->next->previous = p->previous;
483         if (p->previous)
484                 p->previous->next = p->next;
485         else
486                 first_process = p->next;
487
488         if (p->name) {
489                 free(p->name);
490         }
491         /* remove the process from the hash table */
492         unhash_process(p);
493         free(p);
494 }
495
496 /******************************************
497  * Strip dead process entries                     *
498  ******************************************/
499
500 static void process_cleanup(void)
501 {
502
503         struct process *p = first_process;
504
505         while (p) {
506                 struct process *current = p;
507
508 #if defined(PARANOID)
509                 assert(p->id == 0x0badfeed);
510 #endif /* defined(PARANOID) */
511
512                 p = p->next;
513                 /* Delete processes that have died */
514                 if (current->time_stamp != g_time) {
515                         delete_process(current);
516                 }
517         }
518 }
519
520 /******************************************
521  * Calculate cpu total                                    *
522  ******************************************/
523 #define TMPL_SHORTPROC "%*s %llu %llu %llu %llu"
524 #define TMPL_LONGPROC "%*s %llu %llu %llu %llu %llu %llu %llu %llu"
525
526 static unsigned long long calc_cpu_total(void)
527 {
528         unsigned long long total = 0;
529         unsigned long long t = 0;
530         int rc;
531         int ps;
532         char line[BUFFER_LEN] = { 0 };
533         unsigned long long cpu = 0;
534         unsigned long long niceval = 0;
535         unsigned long long systemval = 0;
536         unsigned long long idle = 0;
537         unsigned long long iowait = 0;
538         unsigned long long irq = 0;
539         unsigned long long softirq = 0;
540         unsigned long long steal = 0;
541         const char *template =
542                 KFLAG_ISSET(KFLAG_IS_LONGSTAT) ? TMPL_LONGPROC : TMPL_SHORTPROC;
543
544         ps = open("/proc/stat", O_RDONLY);
545         rc = read(ps, line, sizeof(line));
546         close(ps);
547         if (rc < 0) {
548                 return 0;
549         }
550
551         sscanf(line, template, &cpu, &niceval, &systemval, &idle, &iowait, &irq,
552                         &softirq, &steal);
553         total = cpu + niceval + systemval + idle + iowait + irq + softirq + steal;
554
555         t = total - previous_total;
556         previous_total = total;
557
558         return t;
559 }
560
561 /******************************************
562  * Calculate each processes cpu                   *
563  ******************************************/
564
565 inline static void calc_cpu_each(unsigned long long total)
566 {
567         struct process *p = first_process;
568
569         while (p) {
570                 p->amount = 100.0 * (cpu_separate ? info.cpu_count : 1) *
571                         (p->user_time + p->kernel_time) / (float) total;
572
573                 p = p->next;
574         }
575 }
576
577 #ifdef IOSTATS
578 static void calc_io_each(void)
579 {
580         struct process *p;
581         unsigned long long sum = 0;
582
583         for (p = first_process; p; p = p->next)
584                 sum += p->read_bytes + p->write_bytes;
585
586         if(sum == 0)
587                 sum = 1; /* to avoid having NANs if no I/O occured */
588         for (p = first_process; p; p = p->next)
589                 p->io_perc = 100.0 * (p->read_bytes + p->write_bytes) / (float) sum;
590 }
591 #endif /* IOSTATS */
592
593 /******************************************
594  * Find the top processes                                 *
595  ******************************************/
596
597 /* free a sp_process structure */
598 static void free_sp(struct sorted_process *sp)
599 {
600         free(sp);
601 }
602
603 /* create a new sp_process structure */
604 static struct sorted_process *malloc_sp(struct process *proc)
605 {
606         struct sorted_process *sp;
607         sp = malloc(sizeof(struct sorted_process));
608         memset(sp, 0, sizeof(struct sorted_process));
609         sp->proc = proc;
610         return sp;
611 }
612
613 /* cpu comparison function for insert_sp_element */
614 static int compare_cpu(struct process *a, struct process *b)
615 {
616         if (a->amount < b->amount) {
617                 return 1;
618         } else if (a->amount > b->amount) {
619                 return -1;
620         } else {
621                 return 0;
622         }
623 }
624
625 /* mem comparison function for insert_sp_element */
626 static int compare_mem(struct process *a, struct process *b)
627 {
628         if (a->totalmem < b->totalmem) {
629                 return 1;
630         } else if (a->totalmem > b->totalmem) {
631                 return -1;
632         } else {
633                 return 0;
634         }
635 }
636
637 /* CPU time comparision function for insert_sp_element */
638 static int compare_time(struct process *a, struct process *b)
639 {
640         return b->total_cpu_time - a->total_cpu_time;
641 }
642
643 #ifdef IOSTATS
644 /* I/O comparision function for insert_sp_element */
645 static int compare_io(struct process *a, struct process *b)
646 {
647         if (a->io_perc < b->io_perc) {
648                 return 1;
649         } else if (a->io_perc > b->io_perc) {
650                 return -1;
651         } else {
652                 return 0;
653         }
654 }
655 #endif /* IOSTATS */
656
657 /* insert this process into the list in a sorted fashion,
658  * or destroy it if it doesn't fit on the list */
659 static int insert_sp_element(struct sorted_process *sp_cur,
660                 struct sorted_process **p_sp_head, struct sorted_process **p_sp_tail,
661                 int max_elements, int compare_funct(struct process *, struct process *))
662 {
663
664         struct sorted_process *sp_readthru = NULL, *sp_destroy = NULL;
665         int did_insert = 0, x = 0;
666
667         if (*p_sp_head == NULL) {
668                 *p_sp_head = sp_cur;
669                 *p_sp_tail = sp_cur;
670                 return 1;
671         }
672         for (sp_readthru = *p_sp_head, x = 0;
673                         sp_readthru != NULL && x < max_elements;
674                         sp_readthru = sp_readthru->less, x++) {
675                 if (compare_funct(sp_readthru->proc, sp_cur->proc) > 0 && !did_insert) {
676                         /* sp_cur is bigger than sp_readthru
677                          * so insert it before sp_readthru */
678                         sp_cur->less = sp_readthru;
679                         if (sp_readthru == *p_sp_head) {
680                                 /* insert as the new head of the list */
681                                 *p_sp_head = sp_cur;
682                         } else {
683                                 /* insert inside the list */
684                                 sp_readthru->greater->less = sp_cur;
685                                 sp_cur->greater = sp_readthru->greater;
686                         }
687                         sp_readthru->greater = sp_cur;
688                         /* element was inserted, so increase the counter */
689                         did_insert = ++x;
690                 }
691         }
692         if (x < max_elements && sp_readthru == NULL && !did_insert) {
693                 /* sp_cur is the smallest element and list isn't full,
694                  * so insert at the end */
695                 (*p_sp_tail)->less = sp_cur;
696                 sp_cur->greater = *p_sp_tail;
697                 *p_sp_tail = sp_cur;
698                 did_insert = x;
699         } else if (x >= max_elements) {
700                 /* We inserted an element and now the list is too big by one.
701                  * Destroy the smallest element */
702                 sp_destroy = *p_sp_tail;
703                 *p_sp_tail = sp_destroy->greater;
704                 (*p_sp_tail)->less = NULL;
705                 free_sp(sp_destroy);
706         }
707         if (!did_insert) {
708                 /* sp_cur wasn't added to the sorted list, so destroy it */
709                 free_sp(sp_cur);
710         }
711         return did_insert;
712 }
713
714 /* copy the procs in the sorted list to the array, and destroy the list */
715 static void sp_acopy(struct sorted_process *sp_head, struct process **ar, int max_size)
716 {
717         struct sorted_process *sp_cur, *sp_tmp;
718         int x;
719
720         sp_cur = sp_head;
721         for (x = 0; x < max_size && sp_cur != NULL; x++) {
722                 ar[x] = sp_cur->proc;
723                 sp_tmp = sp_cur;
724                 sp_cur = sp_cur->less;
725                 free_sp(sp_tmp);
726         }
727 }
728
729 /* ****************************************************************** *
730  * Get a sorted list of the top cpu hogs and top mem hogs.                        *
731  * Results are stored in the cpu,mem arrays in decreasing order[0-9]. *
732  * ****************************************************************** */
733
734 void process_find_top(struct process **cpu, struct process **mem,
735                 struct process **ptime
736 #ifdef IOSTATS
737                 , struct process **io
738 #endif /* IOSTATS */
739                 )
740 {
741         struct sorted_process *spc_head = NULL, *spc_tail = NULL, *spc_cur = NULL;
742         struct sorted_process *spm_head = NULL, *spm_tail = NULL, *spm_cur = NULL;
743         struct sorted_process *spt_head = NULL, *spt_tail = NULL, *spt_cur = NULL;
744 #ifdef IOSTATS
745         struct sorted_process *spi_head = NULL, *spi_tail = NULL, *spi_cur = NULL;
746 #endif /* IOSTATS */
747         struct process *cur_proc = NULL;
748         unsigned long long total = 0;
749
750         if (!top_cpu && !top_mem && !top_time
751 #ifdef IOSTATS
752                         && !top_io
753 #endif /* IOSTATS */
754                         && !top_running
755            ) {
756                 return;
757         }
758
759         total = calc_cpu_total();       /* calculate the total of the processor */
760         update_process_table();         /* update the table with process list */
761         calc_cpu_each(total);           /* and then the percentage for each task */
762         process_cleanup();                      /* cleanup list from exited processes */
763 #ifdef IOSTATS
764         calc_io_each();                 /* percentage of I/O for each task */
765 #endif /* IOSTATS */
766
767         cur_proc = first_process;
768
769         while (cur_proc != NULL) {
770                 if (top_cpu) {
771                         spc_cur = malloc_sp(cur_proc);
772                         insert_sp_element(spc_cur, &spc_head, &spc_tail, MAX_SP,
773                                         &compare_cpu);
774                 }
775                 if (top_mem) {
776                         spm_cur = malloc_sp(cur_proc);
777                         insert_sp_element(spm_cur, &spm_head, &spm_tail, MAX_SP,
778                                         &compare_mem);
779                 }
780                 if (top_time) {
781                         spt_cur = malloc_sp(cur_proc);
782                         insert_sp_element(spt_cur, &spt_head, &spt_tail, MAX_SP,
783                                         &compare_time);
784                 }
785 #ifdef IOSTATS
786                 if (top_io) {
787                         spi_cur = malloc_sp(cur_proc);
788                         insert_sp_element(spi_cur, &spi_head, &spi_tail, MAX_SP,
789                                         &compare_io);
790                 }
791 #endif /* IOSTATS */
792                 cur_proc = cur_proc->next;
793         }
794
795         if (top_cpu)    sp_acopy(spc_head, cpu,         MAX_SP);
796         if (top_mem)    sp_acopy(spm_head, mem,         MAX_SP);
797         if (top_time)   sp_acopy(spt_head, ptime,       MAX_SP);
798 #ifdef IOSTATS
799         if (top_io)             sp_acopy(spi_head, io,          MAX_SP);
800 #endif /* IOSTATS */
801 }
802
803 int parse_top_args(const char *s, const char *arg, struct text_object *obj)
804 {
805         char buf[64];
806         int n;
807
808         if (obj->data.top.was_parsed) {
809                 return 1;
810         }
811         obj->data.top.was_parsed = 1;
812
813         if (arg && !obj->data.top.s) {
814                 obj->data.top.s = strndup(arg, text_buffer_size);
815         }
816
817         if (s[3] == 0) {
818                 obj->type = OBJ_top;
819                 top_cpu = 1;
820         } else if (strcmp(&s[3], "_mem") == EQUAL) {
821                 obj->type = OBJ_top_mem;
822                 top_mem = 1;
823         } else if (strcmp(&s[3], "_time") == EQUAL) {
824                 obj->type = OBJ_top_time;
825                 top_time = 1;
826 #ifdef IOSTATS
827         } else if (strcmp(&s[3], "_io") == EQUAL) {
828                 obj->type = OBJ_top_io;
829                 top_io = 1;
830 #endif /* IOSTATS */
831         } else {
832 #ifdef IOSTATS
833                 NORM_ERR("Must be top, top_mem, top_time or top_io");
834 #else /* IOSTATS */
835                 NORM_ERR("Must be top, top_mem or top_time");
836 #endif /* IOSTATS */
837                 return 0;
838         }
839
840         if (!arg) {
841                 NORM_ERR("top needs arguments");
842                 return 0;
843         }
844
845         if (sscanf(arg, "%63s %i", buf, &n) == 2) {
846                 if (strcmp(buf, "name") == EQUAL) {
847                         obj->data.top.type = TOP_NAME;
848                 } else if (strcmp(buf, "cpu") == EQUAL) {
849                         obj->data.top.type = TOP_CPU;
850                 } else if (strcmp(buf, "pid") == EQUAL) {
851                         obj->data.top.type = TOP_PID;
852                 } else if (strcmp(buf, "mem") == EQUAL) {
853                         obj->data.top.type = TOP_MEM;
854                 } else if (strcmp(buf, "time") == EQUAL) {
855                         obj->data.top.type = TOP_TIME;
856                 } else if (strcmp(buf, "mem_res") == EQUAL) {
857                         obj->data.top.type = TOP_MEM_RES;
858                 } else if (strcmp(buf, "mem_vsize") == EQUAL) {
859                         obj->data.top.type = TOP_MEM_VSIZE;
860 #ifdef IOSTATS
861                 } else if (strcmp(buf, "io_read") == EQUAL) {
862                         obj->data.top.type = TOP_READ_BYTES;
863                 } else if (strcmp(buf, "io_write") == EQUAL) {
864                         obj->data.top.type = TOP_WRITE_BYTES;
865                 } else if (strcmp(buf, "io_perc") == EQUAL) {
866                         obj->data.top.type = TOP_IO_PERC;
867 #endif /* IOSTATS */
868                 } else {
869                         NORM_ERR("invalid type arg for top");
870 #ifdef IOSTATS
871                         NORM_ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize, "
872                                         "io_read, io_write, io_perc");
873 #else /* IOSTATS */
874                         NORM_ERR("must be one of: name, cpu, pid, mem, time, mem_res, mem_vsize");
875 #endif /* IOSTATS */
876                         return 0;
877                 }
878                 if (n < 1 || n > 10) {
879                         NORM_ERR("invalid num arg for top. Must be between 1 and 10.");
880                         return 0;
881                 } else {
882                         obj->data.top.num = n - 1;
883                 }
884         } else {
885                 NORM_ERR("invalid argument count for top");
886                 return 0;
887         }
888         return 1;
889 }
890
891