File: ttest.c

package info (click to toggle)
gkrelltop 2.2.13-1.3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 308 kB
  • sloc: sh: 2,805; ansic: 1,823; makefile: 168
file content (149 lines) | stat: -rw-r--r-- 3,790 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* test the top_three functionality
 * will print in the cmdline every second a list of
 * three best processes 
 */

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <dirent.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include <math.h>
#include <limits.h>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <regex.h>
#include "gkrelltop.h"
#include <sys/time.h> //gettimeofday


#define SLEEPLENGTH 700000

void onexit_cleanup(void);

void onexit(int sig)
{
  perror("\nAttempting to cleanup\n");
  onexit_cleanup();
  exit(0);
}

int  show_nice_processes;
extern int pluginMode;

struct process *best[3] = { 0, 0, 0 };

int main (int argc, char ** argv)
{
  int n = 0;
  int i;
  char str [256];

  if(argc > 1) {
      if(strncmp(argv[1], "mem", 2) == 0)
          pluginMode = mem;
      else if(strncmp(argv[1], "io", 2) == 0)
          pluginMode = io;
  }


  /* catch when user presses ctrl-c SIGINT and perform cleanup */
  if(signal(SIGINT, onexit) == SIG_ERR) {
      perror("\nSignal not divertible; no cleanup upon exit\n");
  }

  show_nice_processes = 1;
  printf("start of the program, show_nice_processes=%d (%p)\n", 
          show_nice_processes, &show_nice_processes);

  int u_secs1 = 0; 
  int u_secs2 = 0;

  while (1)    //sorry
  { 
    /*
     * Find the top three!
     */

    //find the delays of the times, and see which method is better, 
    //if needed, call multiple times in a loop

    int i;
    struct timeval start;
    gettimeofday(&start, 0);

    //for(i=0;i<20;i++) {
    best[0] = best[1] = best[2] = NULL; //freeing occurrs in top_three.c
    n = gkrelltop_process_find_top_three(best);
    //}

    struct timeval end;
    gettimeofday(&end, 0);
    u_secs1 += (end.tv_sec - start.tv_sec) * 1000000 +
              (end.tv_usec - start.tv_usec);

    start = end;

    //for(i=0;i<20;i++) {
    best[0] = best[1] = best[2] = NULL; //freeing occurrs in top_three.c
    n = working_gkrelltop_process_find_top_three(best);
    //}

    gettimeofday(&end, 0);
    u_secs2 += (end.tv_sec - start.tv_sec) * 1000000 +
              (end.tv_usec - start.tv_usec);

    printf("u_secs1 = %d   u_secs2 = %d\n", u_secs1, u_secs2);

    if (n > 3) {
        /* cant have more than 3, some error occured */
        return 0;
    }

    if(n > 0) {
        printf("N(%d): ", n);

        for (i = 0; i < n; i++) {
            if(pluginMode == cpu)
                snprintf(str, sizeof(str), "[%-5.10s(%4.2f%c %5d)]", 
                        best[i]->name, best[i]->amount, '%', best[i]->pid);
            else if(pluginMode == mem)
                snprintf(str, sizeof(str), "[%-5.10s(%.2f%c %dM %5d)]", 
                        best[i]->name, best[i]->amount, '%', 
                        best[i]->rss/MEG, best[i]->pid);
            else if(pluginMode == io)
                snprintf(str, sizeof(str), "[%-5.10s(%.0f %.0fK %.0fK %5d)]", 
                    best[i]->name, best[i]->amount, 
                    (float)(best[i]->io_read - best[i]->previous_io_read)/KIL,
                    (float)(best[i]->io_write - best[i]->previous_io_write)/KIL,
                    best[i]->pid);

            printf("%s  ",str);
        }

        for(; i < 3; i++) {
          if (best[i]) {
            printf("WARNING: SHOULD NEVER GET HERE!!\n");
            snprintf(str, sizeof(str), "%d - %-10.10s(%c %.2f %5d)", i+1, 
                    best[i]->name,'%',best[i]->amount,best[i]->pid);
            printf("%s\t",str);
          }
        }

        printf("\n"); //15 CR, 12 linefeed
    }

    usleep(SLEEPLENGTH); /* microsleep in microseeconds */
  }

  return 0;
}