~bhaveekdesai/ubuntu/maverick/latencytop/bug-735262

« back to all changes in this revision

Viewing changes to latencytop.c

  • Committer: Bazaar Package Importer
  • Author(s): Giacomo Catenazzi
  • Date: 2008-04-15 08:08:15 UTC
  • Revision ID: james.westby@ubuntu.com-20080415080815-lamon64lszr79vzc
Tags: 0.3-4
* Ooops, finally merge all changes of previous versions.
* Merging again some patches from git, so kernel support is enabled
  automatically at program start (Closes: #475548)
* Don't clean up screen after writing errors (Closes: #469418)

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
#include <string.h>
31
31
#include <sys/types.h>
32
32
#include <dirent.h>
 
33
#include <ncursesw/ncurses.h>
33
34
 
34
35
#include <glib.h>
35
36
 
47
48
int firsttime = 1;
48
49
int noui; 
49
50
int dump_unknown;
 
51
static void disable_sysctl(void);
 
52
 
50
53
 
51
54
static void add_to_global(struct latency_line *line)
52
55
{
95
98
        size_t dummy;
96
99
        file = fopen("/proc/latency_stats","r+");
97
100
        if (!file) {
98
 
                cleanup_curses() ;
99
 
                fprintf(stderr, "Please enable the CONFIG_LATENCYTOP configuration in your kernel.\n");
100
 
                fprintf(stderr, "Exiting...\n");
101
 
                exit(EXIT_FAILURE);
 
101
                cleanup_curses();
 
102
                disable_sysctl();
 
103
                fputs(  "Are you root?\n"
 
104
                        "Please enable the CONFIG_LATENCYTOP configuration in your kernel.\n"
 
105
                        "Exiting...\n", stderr);
 
106
                _exit(EXIT_FAILURE);
102
107
        }
103
108
        /* wipe first line */
104
109
        ln = NULL;
161
166
 
162
167
void sort_list(void)
163
168
{
 
169
        GList *entry;
 
170
        struct latency_line *line;
 
171
 
 
172
        total_time = 0;
164
173
        lines = g_list_sort(lines, comparef);
 
174
        entry = g_list_first(lines);
 
175
        while (entry) {
 
176
                line = entry->data;
 
177
                entry = g_list_next(entry);
 
178
                total_time = total_time + line->time;
 
179
        }
165
180
}
166
181
 
167
182
 
292
307
                fclose(file);
293
308
        }
294
309
        /* 100 usec minimum */
295
 
        if (process->maxdelay > 0.1 && !firsttime) {
296
 
                struct latency_line *ln;
 
310
        if (!firsttime) {
 
311
                struct latency_line *ln, *ln2;
297
312
                        
298
313
                ln = malloc(sizeof(struct latency_line));
 
314
                ln2 = malloc(sizeof(struct latency_line));
299
315
                memset(ln, 0, sizeof(struct latency_line));
300
 
                ln->count = 1;
301
 
 
302
 
                ln->time = process->maxdelay * 1000;    
303
 
                ln->max = ln->time;
 
316
                
 
317
                if (process->delaycount)
 
318
                        ln->count = process->delaycount;
 
319
                else
 
320
                        ln->count = 1;
 
321
                if (process->totaldelay > 0.00001)
 
322
                        ln->time = process->totaldelay * 1000;
 
323
                else
 
324
                        ln->time = process->maxdelay * 1000;    
 
325
                ln->max = process->maxdelay * 1000;    
304
326
                strcpy(ln->reason, "Scheduler: waiting for cpu");
305
327
                if (ln->max > process->max)
306
328
                        process->max = ln->max;
 
329
                memcpy(ln2, ln, sizeof(struct latency_line));
307
330
                add_to_process(process, ln);
 
331
                add_to_global(ln2);
308
332
                process->used = 1;
309
333
        }
310
334
        closedir(dir);
388
412
                                        sscanf(q+1,"%lf", &d);
389
413
                                        process->maxdelay = d;
390
414
                                }
 
415
                                if (strstr(line, "se.wait_sum") && q) {
 
416
                                        sscanf(q+1,"%lf", &d);
 
417
                                        process->totaldelay = d;
 
418
                                }
 
419
                                if (strstr(line, "se.wait_count") && q) {
 
420
                                        sscanf(q+1,"%lf", &d);
 
421
                                        process->delaycount = d;
 
422
                                }
391
423
                                free(line);
392
424
                                line = NULL;
393
425
                        }
443
475
 
444
476
}
445
477
 
 
478
static void enable_sysctl(void)
 
479
{
 
480
        FILE *file;
 
481
        file = fopen("/proc/sys/kernel/latencytop", "w");
 
482
        if (!file) {
 
483
                perror("Error writing to /proc/sys/kernel/latencytop");
 
484
                return;
 
485
        }
 
486
        if (putc('1', file) == EOF) {
 
487
                perror("Error writing to /proc/sys/kernel/latencytop");
 
488
                return;
 
489
        }
 
490
        fclose(file);
 
491
}
 
492
 
 
493
static void disable_sysctl(void)
 
494
{
 
495
        FILE *file;
 
496
        file = fopen("/proc/sys/kernel/latencytop", "w");
 
497
        if (!file) {
 
498
                perror("Error writing to /proc/sys/kernel/latencytop");
 
499
                return;
 
500
        }
 
501
        if (putc('0', file) == EOF) {
 
502
                perror("Error writing to /proc/sys/kernel/latencytop");
 
503
                return;
 
504
        }
 
505
        fclose(file);
 
506
}
446
507
 
447
508
int main(int argc, char **argv)
448
509
{
449
 
        init_translations("latencytop.trans");
 
510
        int ret = 1;
 
511
        enable_sysctl();
 
512
        atexit(disable_sysctl);
 
513
        init_translations("/usr/share/misc/latencytop.trans");
450
514
        if (argc>1 && strcmp(argv[1],"-d")==0) {
451
515
                parse_global_list();
452
516
                sort_list();
458
522
                dump_unknown = 1;
459
523
        }
460
524
        initialize_curses();
461
 
        while (1 + argc || argv==NULL) {
 
525
        while (ret) {
462
526
                parse_processes();
463
527
                prune_unused_procs();
464
528
                parse_global_list();
465
529
                sort_list();
466
530
                if (!total_time)
467
531
                        total_time = 1;
468
 
                update_display(30);
 
532
                ret = update_display(30);
469
533
                delete_list();
470
534
                firsttime = 0;
471
535
                if (noui)
472
536
                        fprintf(stderr, ".");
473
537
        }
 
538
        prune_unused_procs();
 
539
        delete_list();
474
540
        return EXIT_SUCCESS;
475
541
}