ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/sgps/sgps.c
Revision: 1.1
Committed: Mon Apr 4 14:10:08 2005 UTC (19 years, 7 months ago) by pajs
Content type: text/plain
Branch: MAIN
Log Message:
Initial version. Only compiled on solaris - but seems to work.

Makes use of efA flags, and also a 'o' flag for sorting..

ps -o cpu
ps -o mem

will sort by that type.

Need to do the -n flag still (not look up names - although, is there
much point to that?)

File Contents

# User Rev Content
1 pajs 1.1 #include <stdio.h>
2     #include <stdlib.h>
3     #include <unistd.h>
4     #include <strings.h>
5     #include <pwd.h>
6     #include "statgrab.h"
7    
8     char *size_conv(long long number){
9     char type[] = {'B', 'K', 'M', 'G', 'T'};
10     int x=0;
11     static char string[10];
12    
13     for(;x<5;x++){
14     if( (number/1024) < (100)) {
15     break;
16     }
17     number = (number/1024);
18     }
19    
20     snprintf(string, 10, "%lld%c", number, type[x]);
21     return string;
22    
23     }
24    
25     char *username(uid_t uid){
26     static struct passwd *pass;
27    
28     pass = getpwuid(uid);
29     if(pass == NULL){
30     return "NULL";
31     }
32    
33     return pass->pw_name;
34     }
35    
36     char *proc_state(sg_process_state state){
37     static char *pstat = "Unk";
38    
39     if(state == SG_PROCESS_STATE_RUNNING){
40     pstat="Run";
41     }
42    
43     if(state == SG_PROCESS_STATE_SLEEPING){
44     pstat="Sleep";
45     }
46    
47     if(state == SG_PROCESS_STATE_STOPPED){
48     pstat="Stop";
49     }
50    
51     if(state == SG_PROCESS_STATE_ZOMBIE){
52     pstat="Zomb";
53     }
54    
55     if(state == SG_PROCESS_STATE_UNKNOWN){
56     pstat="Unk";
57     }
58    
59     return pstat;
60     }
61    
62     int main(int argc, char **argv){
63     extern char *optarg;
64     int c;
65     int x;
66     int full=0;
67     int detailed=0;
68     int nolookup=0;
69     char *user = NULL;
70    
71     int (*sortby_ptr)(const void *va, const void *vb);
72    
73     int entries;
74     extern char *optarg;
75    
76    
77     sg_process_stats *sg_proc = NULL;
78     /* Default behaviour - sort by pid */
79     sortby_ptr = sg_process_compare_pid;
80    
81     while ((c = getopt(argc, argv, "neAfu:o:")) != -1){
82     switch (c){
83     case 'e':
84     case 'A':
85     full=1;
86     break;
87     case 'f':
88     detailed=1;
89     break;
90     case 'n':
91     nolookup=1;
92     break;
93     case 'u':
94     user = strdup(optarg);
95     if (user == NULL) exit(1);
96     break;
97     case 'o':
98     if(!strncasecmp(optarg, "cpu", 3)){
99     sortby_ptr = sg_process_compare_cpu;
100     break;
101     }
102     if(!strncasecmp(optarg, "size", 3)){
103     sortby_ptr = sg_process_compare_size;
104     break;
105     }
106     if(!strncasecmp(optarg, "pid", 3)){
107     sortby_ptr = sg_process_compare_pid;
108     break;
109     }
110     if(!strncasecmp(optarg, "mem", 3)){
111     sortby_ptr = sg_process_compare_res;
112     break;
113     }
114     if(!strncasecmp(optarg, "res", 3)){
115     sortby_ptr = sg_process_compare_res;
116     break;
117     }
118     if(!strncasecmp(optarg, "uid", 3)){
119     sortby_ptr = sg_process_compare_uid;
120     break;
121     }
122     if(!strncasecmp(optarg, "gid", 3)){
123     sortby_ptr = sg_process_compare_gid;
124     break;
125     }
126     default:
127     sortby_ptr = sg_process_compare_cpu;
128     break;
129     }
130     }
131    
132     /* Get the procs - and sort them */
133     sg_proc = sg_get_process_stats(&entries);
134     qsort(sg_proc, entries, sizeof *sg_proc, sortby_ptr);
135    
136     if (full){
137     printf("%9s %6s %6s %6s %7s %7s %4s %s\n", "User", "pid", "parent", "state", "size", "res", "CPU", "Process");
138     }else{
139     printf("%6s %7s %4s %s\n", "pid", "size", "CPU", "Process");
140     }
141    
142     for(x=0; x<entries; x++){
143     char *proc_out=NULL;
144     if (detailed) {
145     proc_out = sg_proc->proctitle;
146     }else{
147     proc_out = sg_proc->process_name;
148     }
149    
150     if(full){
151     printf("%9s %6d %6d %6s %7s %7s %2.2f %s\n", username(sg_proc->uid), (int)sg_proc->pid, (int)sg_proc->parent, proc_state(sg_proc->state), size_conv(sg_proc->proc_size), size_conv(sg_proc->proc_resident), sg_proc->cpu_percent, proc_out);
152     }else{
153     printf("%6d %7s %2.2f %s\n", (int)sg_proc->pid, size_conv(sg_proc->proc_size), sg_proc->cpu_percent, proc_out);
154     }
155    
156     sg_proc++;
157     }
158    
159     return 0;
160     }