41
41
42
42
struct cpu_info {
43
43
long unsigned utime , ntime , stime , itime ;
44
+ long unsigned iowtime , irqtime , sirqtime ;
44
45
};
45
46
47
+ #define PROC_NAME_LEN 64
48
+ #define THREAD_NAME_LEN 32
49
+
46
50
struct proc_info {
47
51
struct proc_info * next ;
48
52
pid_t pid ;
49
53
pid_t tid ;
50
54
uid_t uid ;
51
55
gid_t gid ;
52
- char name [256 ];
56
+ char name [PROC_NAME_LEN ];
57
+ char tname [THREAD_NAME_LEN ];
53
58
char state ;
54
59
long unsigned utime ;
55
60
long unsigned stime ;
@@ -69,7 +74,7 @@ struct proc_list {
69
74
#define die (...) { fprintf(stderr, __VA_ARGS__); exit(EXIT_FAILURE); }
70
75
71
76
#define INIT_PROCS 50
72
- #define THREAD_MULT 4
77
+ #define THREAD_MULT 8
73
78
static struct proc_info * * old_procs , * * new_procs ;
74
79
static int num_old_procs , num_new_procs ;
75
80
static struct proc_info * free_procs ;
@@ -228,7 +233,8 @@ static void read_procs(void) {
228
233
229
234
file = fopen ("/proc/stat" , "r" );
230
235
if (!file ) die ("Could not open /proc/stat.\n" );
231
- fscanf (file , "cpu %lu %lu %lu %lu" , & new_cpu .utime , & new_cpu .ntime , & new_cpu .stime , & new_cpu .itime );
236
+ fscanf (file , "cpu %lu %lu %lu %lu %lu %lu %lu" , & new_cpu .utime , & new_cpu .ntime , & new_cpu .stime ,
237
+ & new_cpu .itime , & new_cpu .iowtime , & new_cpu .irqtime , & new_cpu .sirqtime );
232
238
fclose (file );
233
239
234
240
proc_num = 0 ;
@@ -237,7 +243,9 @@ static void read_procs(void) {
237
243
continue ;
238
244
239
245
pid = atoi (pid_dir -> d_name );
240
-
246
+
247
+ struct proc_info cur_proc ;
248
+
241
249
if (!threads ) {
242
250
proc = alloc_proc ();
243
251
@@ -254,6 +262,12 @@ static void read_procs(void) {
254
262
255
263
proc -> num_threads = 0 ;
256
264
} else {
265
+ sprintf (filename , "/proc/%d/cmdline" , pid );
266
+ read_cmdline (filename , & cur_proc );
267
+
268
+ sprintf (filename , "/proc/%d/status" , pid );
269
+ read_status (filename , & cur_proc );
270
+
257
271
proc = NULL ;
258
272
}
259
273
@@ -275,11 +289,9 @@ static void read_procs(void) {
275
289
sprintf (filename , "/proc/%d/task/%d/stat" , pid , tid );
276
290
read_stat (filename , proc );
277
291
278
- sprintf (filename , "/proc/%d/task/%d/cmdline" , pid , tid );
279
- read_cmdline (filename , proc );
280
-
281
- sprintf (filename , "/proc/%d/task/%d/status" , pid , tid );
282
- read_status (filename , proc );
292
+ strcpy (proc -> name , cur_proc .name );
293
+ proc -> uid = cur_proc .uid ;
294
+ proc -> gid = cur_proc .gid ;
283
295
284
296
add_proc (proc_num ++ , proc );
285
297
} else {
@@ -315,8 +327,9 @@ static int read_stat(char *filename, struct proc_info *proc) {
315
327
if (!open_paren || !close_paren ) return 1 ;
316
328
317
329
* open_paren = * close_paren = '\0' ;
318
- strcpy (proc -> name , open_paren + 1 );
319
-
330
+ strncpy (proc -> tname , open_paren + 1 , THREAD_NAME_LEN );
331
+ proc -> tname [THREAD_NAME_LEN - 1 ] = 0 ;
332
+
320
333
/* Scan rest of string. */
321
334
sscanf (close_paren + 1 , " %c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
322
335
"%lu %lu %*d %*d %*d %*d %*d %*d %*d %lu %ld" ,
@@ -347,8 +360,11 @@ static int read_cmdline(char *filename, struct proc_info *proc) {
347
360
if (!file ) return 1 ;
348
361
fgets (line , MAX_LINE , file );
349
362
fclose (file );
350
- if (strlen (line ) > 0 )
351
- strcpy (proc -> name , line );
363
+ if (strlen (line ) > 0 ) {
364
+ strncpy (proc -> name , line , PROC_NAME_LEN );
365
+ proc -> name [PROC_NAME_LEN - 1 ] = 0 ;
366
+ } else
367
+ proc -> name [0 ] = 0 ;
352
368
return 0 ;
353
369
}
354
370
@@ -391,16 +407,34 @@ static void print_procs(void) {
391
407
}
392
408
}
393
409
394
- total_delta_time = (new_cpu .utime + new_cpu .ntime + new_cpu .stime + new_cpu .itime )
395
- - (old_cpu .utime + old_cpu .ntime + old_cpu .stime + old_cpu .itime );
410
+ total_delta_time = (new_cpu .utime + new_cpu .ntime + new_cpu .stime + new_cpu .itime
411
+ + new_cpu .iowtime + new_cpu .irqtime + new_cpu .sirqtime )
412
+ - (old_cpu .utime + old_cpu .ntime + old_cpu .stime + old_cpu .itime
413
+ + old_cpu .iowtime + old_cpu .irqtime + old_cpu .sirqtime );
396
414
397
415
qsort (new_procs , num_new_procs , sizeof (struct proc_info * ), proc_cmp );
398
416
399
417
printf ("\n\n\n" );
418
+ printf ("User %ld%%, System %ld%%, IOW %ld%%, IRQ %ld%%\n" ,
419
+ ((new_cpu .utime + new_cpu .ntime ) - (old_cpu .utime + old_cpu .ntime )) * 100 / total_delta_time ,
420
+ ((new_cpu .stime ) - (old_cpu .stime )) * 100 / total_delta_time ,
421
+ ((new_cpu .iowtime ) - (old_cpu .iowtime )) * 100 / total_delta_time ,
422
+ ((new_cpu .irqtime + new_cpu .sirqtime )
423
+ - (old_cpu .irqtime + old_cpu .sirqtime )) * 100 / total_delta_time );
424
+ printf ("User %ld + Nice %ld + Sys %ld + Idle %ld + IOW %ld + IRQ %ld + SIRQ %ld = %ld\n" ,
425
+ new_cpu .utime - old_cpu .utime ,
426
+ new_cpu .ntime - old_cpu .ntime ,
427
+ new_cpu .stime - old_cpu .stime ,
428
+ new_cpu .itime - old_cpu .itime ,
429
+ new_cpu .iowtime - old_cpu .iowtime ,
430
+ new_cpu .irqtime - old_cpu .irqtime ,
431
+ new_cpu .sirqtime - old_cpu .sirqtime ,
432
+ total_delta_time );
433
+ printf ("\n" );
400
434
if (!threads )
401
435
printf ("%5s %4s %1s %5s %7s %7s %-8s %s\n" , "PID" , "CPU%" , "S" , "#THR" , "VSS" , "RSS" , "UID" , "Name" );
402
436
else
403
- printf ("%5s %5s %4s %1s %7s %7s %-8s %s\n" , "PID" , "TID" , "CPU%" , "S" , "VSS" , "RSS" , "UID" , "Name " );
437
+ printf ("%5s %5s %4s %1s %7s %7s %-8s %-15s % s\n" , "PID" , "TID" , "CPU%" , "S" , "VSS" , "RSS" , "UID" , "Thread" , "Proc " );
404
438
405
439
for (i = 0 ; i < num_new_procs ; i ++ ) {
406
440
proc = new_procs [i ];
@@ -423,10 +457,10 @@ static void print_procs(void) {
423
457
}
424
458
if (!threads )
425
459
printf ("%5d %3ld%% %c %5d %6ldK %6ldK %-8.8s %s\n" , proc -> pid , proc -> delta_time * 100 / total_delta_time , proc -> state , proc -> num_threads ,
426
- proc -> vss / 1024 , proc -> rss * getpagesize () / 1024 , user_str , proc -> name );
460
+ proc -> vss / 1024 , proc -> rss * getpagesize () / 1024 , user_str , proc -> name [ 0 ] != 0 ? proc -> name : proc -> tname );
427
461
else
428
- printf ("%5d %5d %3ld%% %c %6ldK %6ldK %-8.8s %s\n" , proc -> pid , proc -> tid , proc -> delta_time * 100 / total_delta_time , proc -> state ,
429
- proc -> vss / 1024 , proc -> rss * getpagesize () / 1024 , user_str , proc -> name );
462
+ printf ("%5d %5d %3ld%% %c %6ldK %6ldK %-8.8s %-15s % s\n" , proc -> pid , proc -> tid , proc -> delta_time * 100 / total_delta_time , proc -> state ,
463
+ proc -> vss / 1024 , proc -> rss * getpagesize () / 1024 , user_str , proc -> tname , proc -> name );
430
464
}
431
465
}
432
466
0 commit comments