OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [userland/] [sash/] [ps.c] - Blame information for rev 1767

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/* ps.c:
2
 *
3
 * Copyright (C) 1998  Kenneth Albanowski <kjahds@kjahds.com>,
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 */
10
 
11
#include "sash.h"
12
 
13
#include <linux/autoconf.h>
14
 
15
#include <fcntl.h>
16
#include <sys/types.h>
17
 
18
#include <sys/stat.h>
19
#include <dirent.h>
20
#include <pwd.h>
21
#include <grp.h>
22
#include <time.h>
23
#include <linux/major.h>
24
#include <linux/types.h>
25
#include <sys/time.h>
26
#include <asm/param.h>
27
#ifdef EMBED
28
#include <mathf.h>
29
#endif
30
 
31
char psbuf[256];
32
char name[40];
33
int pid, state;
34
char statec;
35
int ppid, pgrp, session;
36
dev_t tty;
37
char tty_name[10];
38
 
39
char master[] = "pqrstuvwxyzabcde";
40
 
41
#define MAJOR(x) ((x) >> 8)
42
#define MINOR(x) ((x) & 0xff)
43
 
44
int port_xlate[16] = {1, 3, 5, 7,9 ,11,13,15,
45
                      2, 4, 6, 8,10,12,14,16};
46
 
47
void dev_to_name(dev_t dev, char * ttyname)
48
{
49
        strcpy(ttyname, "");
50
        if (MAJOR(dev) == 75)
51
                sprintf(ttyname,"X%d", MINOR(dev));
52
        else if (MAJOR(dev) == TTY_MAJOR)
53
                sprintf(ttyname,"S%d", MINOR(dev)-64);
54
        else if (MAJOR(dev) == PTY_SLAVE_MAJOR)
55
                sprintf(ttyname,"%c%x", master[MINOR(dev) / 16], MINOR(dev) & 0xf);
56
}
57
 
58
void
59
do_ps(argc, argv)
60
        char    **argv;
61
{
62
        int i;
63
        int h;
64
        int max;
65
        FILE * f;
66
        DIR * d;
67
        unsigned long bytes, sbytes;
68
        struct dirent * de;
69
        char *ext;
70
        int l;
71
        time_t time_now;
72
        long uptime_secs;
73
        float idle_secs;
74
        float seconds, start, total_time;
75
        int utime, stime, start_time;
76
        int pcpu;
77
        /*extern int _vfprintf_fp_ref, _vfscanf_fp_ref;*/
78
 
79
#if 0
80
        fclose(stdin);
81
#endif 
82
 
83
        printf("  PID PORT STAT SIZE SHARED %%CPU COMMAND\n"/*, _vfprintf_fp_ref, _vfscanf_fp_ref*/);
84
 
85
        h = open("/proc/uptime", O_RDONLY);
86
 
87
        if (h==-1) {
88
                perror("Unable to open /proc/uptime\n");
89
                return;
90
        }
91
 
92
        l = read(h, psbuf, 255);
93
 
94
        close(h);
95
 
96
 
97
        if (l<=0) {
98
                perror("Unable to read uptime");
99
                return;
100
        }
101
 
102
 
103
        psbuf[l] = '\0';
104
        psbuf[255] = '\0';
105
 
106
        ext = psbuf;
107
 
108
 
109
        uptime_secs = atol(ext);
110
 
111
 
112
        time_now = time(0);
113
 
114
        d = opendir("/proc");
115
        if (!d)
116
                return;
117
 
118
        while (de = readdir(d)) {
119
 
120
 
121
                for(i=0;i<strlen(de->d_name);i++)
122
                        if (!isdigit(de->d_name[i]))
123
                                goto next;
124
 
125
                sprintf(psbuf, "/proc/%s/stat", de->d_name);
126
 
127
                h = open(psbuf, O_RDONLY);
128
 
129
                if (h==-1)
130
                        continue;
131
 
132
                l = read(h, psbuf, 255);
133
                if (l<=0) {
134
                        perror("Unable to read status");
135
                        close(h);
136
                        continue;
137
                }
138
 
139
                psbuf[l] = '\0';
140
                psbuf[255] = '\0';
141
 
142
                ext = strrchr(psbuf, ')');
143
                ext[0] = '\0';
144
 
145
                statec = ext[2];
146
 
147
                ext += 4;
148
 
149
                ppid = atoi(ext);
150
                ext = strchr(ext, ' ')+1;
151
 
152
                pgrp = atoi(ext);
153
                ext = strchr(ext, ' ')+1;
154
 
155
                session = atoi(ext);
156
                ext = strchr(ext, ' ')+1;
157
 
158
                tty = atoi(ext);
159
                ext = strchr(ext, ' ')+1;
160
 
161
                //printf("1|%s\n", ext);
162
                //tpgid
163
                ext = strchr(ext, ' ')+1;
164
 
165
                //printf("2|%s\n", ext);
166
                //flags
167
                ext = strchr(ext, ' ')+1;
168
 
169
                //printf("3|%s\n", ext);
170
                //min_flt
171
                ext = strchr(ext, ' ')+1;
172
 
173
                //printf("4|%s\n", ext);
174
                //cmin_flt
175
                ext = strchr(ext, ' ')+1;
176
 
177
                //printf("5|%s\n", ext);
178
                //maj_flt
179
                ext = strchr(ext, ' ')+1;
180
 
181
                //printf("6|%s\n", ext);
182
                //cmaj_flt
183
                ext = strchr(ext, ' ')+1;
184
 
185
                //printf("7|%s\n", ext);
186
                utime = atoi(ext);
187
                ext = strchr(ext, ' ')+1;
188
 
189
                //printf("8|%s\n", ext);
190
                stime = atoi(ext);
191
                ext = strchr(ext, ' ')+1;
192
 
193
                //printf("9|%s\n", ext);
194
                //cutime
195
                ext = strchr(ext, ' ')+1;
196
 
197
                //printf("10|%s\n", ext);
198
                //cstime
199
                ext = strchr(ext, ' ')+1;
200
 
201
                //priority
202
                ext = strchr(ext, ' ')+1;
203
 
204
                //nice
205
                ext = strchr(ext, ' ')+1;
206
 
207
                //timeout
208
                ext = strchr(ext, ' ')+1;
209
 
210
                //it_real_value
211
                ext = strchr(ext, ' ')+1;
212
 
213
                start_time = atoi(ext);
214
 
215
                ext = strchr(psbuf, '(');
216
                ext++;
217
                strcpy(name, ext);
218
 
219
                pid = atoi(psbuf);
220
 
221
 
222
                state = statec;
223
 
224
                close(h);
225
 
226
                dev_to_name(tty, tty_name);
227
 
228
                bytes = 0;
229
                sbytes = 0;
230
                sprintf(psbuf, "/proc/%s/status", de->d_name);
231
 
232
                f = fopen(psbuf, "r");
233
 
234
                if (f) {
235
                        while (fgets(psbuf, 250, f)) {
236
                                if (strncmp(psbuf, "Mem:", 4) == 0) {
237
                                        bytes = atol(psbuf+5);
238
                                } else if (strncmp(psbuf, "Shared:", 7) == 0) {
239
                                        sbytes = atol(psbuf+8);
240
                                }
241
                        }
242
                        fclose(f);
243
                }
244
 
245
                bytes /= 1024;
246
                sbytes /= 1024;
247
 
248
                seconds = ((uptime_secs * (long)HZ) - start_time) / HZ;
249
 
250
                /*printf("seconds=%s\n", gcvt(seconds, 15, psbuf));*/
251
 
252
                start = time_now - seconds;
253
 
254
                /*
255
                printf("1\n");
256
 
257
                gcvt(start, 15, psbuf);
258
 
259
                printf("2\n");
260
 
261
                printf("start=%s\n", psbuf);
262
 
263
                printf("utime=%d, stime=%d. start_time=%d\n", utime, stime, start_time);
264
                */
265
 
266
                total_time = (utime + stime);
267
 
268
                /*printf("total_time=%s\n", gcvt(total_time, 15, psbuf));*/
269
 
270
                pcpu =  seconds ?
271
                        (total_time * 10.0f * 100.0f / (float)HZ) / seconds :
272
                        0;
273
                if (pcpu > 999) pcpu = 999;
274
 
275
 
276
                sprintf(psbuf, "/proc/%s/cmdline", de->d_name);
277
                h = open(psbuf, O_RDONLY);
278
 
279
                if (h == -1) {
280
                        perror("Unable to open cmdline");
281
                        continue;
282
                }
283
 
284
                l = read(h, psbuf, 255);
285
                if (l < 0) {
286
                        perror("Unable to read cmdline");
287
                        close(h);
288
                        continue;
289
                }
290
 
291
                close(h);
292
 
293
                psbuf[255] = psbuf[l] = '\0';
294
 
295
 
296
                printf("%5d %4s %c    %3ldK   %3ldK %2u.%u %s\n", pid, tty_name, state,
297
                        bytes, sbytes,
298
                         pcpu / 10, pcpu % 10,
299
                         /*(int)seconds / 60, (int)seconds % 60,*/
300
                         l ? psbuf : name);
301
        next:
302
        }
303
 
304
        closedir(d);
305
}
306
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.