Old s-bsd.c
  1 /*
  2  * System dependent file for BSD derivatives that have _cp_time in their
  3  * kernels to hold the CPU states. Seen to work on SunOS and Ultrix.
  4  */
  5 /* Chris Siebenmann <cks@sys.toronto.edu>, University of Toronto */
  6 /* LINTLIBRARY */
  7 #include <sys/param.h>
  8 #include <sys/dk.h>
  9 #include <nlist.h>
 10 
 11 #ifndef MAXHOSTNAMELEN
 12   /* Some U*x deviants managed to not have this in sys/param.h */
 13 # define MAXHOSTNAMELEN 64
 14 #endif
 15 
 16 extern char *xmalloc(/* int nbytes */);
 17 extern void shorten(/* char *hname */);
 18 
 19 extern int open(), read();
 20 extern long lseek();
 21 
 22 extern char *kernelSymbols;
 23 
 24 long    cp_time[CPUSTATES];
 25 long    cp_old[CPUSTATES];
 26 int     kmem;                   /* file descriptor of /dev/kmem. */
 27 struct nlist    nl[] = {
 28 #define X_CP_TIME       0
 29         { "_cp_time" },
 30         { "" },
 31 };
 32 
 33 #define NPROCS 1
 34 
 35 /* Called at the beginning to inquire how many bars are needed. */
 36 int
 37 num_bars()
 38 {
 39         return NPROCS;
 40 }
 41 
 42 /*
 43  * Indicates how many levels each bar has.  For most machines, each bar will
 44  * have the same stuff.  But one can, for instance, display memory use on one
 45  * bar, processor levels on others, etc.
 46  */
 47 void
 48 bar_items(nbars, items)
 49 int nbars;
 50 int items[];    /* nbars items in this */
 51 {
 52     items[0] = CPUSTATES;
 53 }
 54 
 55 /* Called after num_bars to ask for the bar names */
 56 /* ARGSUSED */
 57 char **
 58 label_bars(nbars)
 59 {
 60         static char hname[MAXHOSTNAMELEN + 1];
 61         static char *names[NPROCS];
 62 
 63         names[0] = hname;
 64         hname[MAXHOSTNAMELEN] = '\0';
 65         if (gethostname(hname, MAXHOSTNAMELEN) < 0) {
 66                 perror("gethostname");
 67                 *hname = '\0';
 68         }
 69         shorten(hname);
 70         return names;
 71 }
 72 
 73 /* 
 74  *  Called after the bars are created to perform any machine dependent
 75  *  initializations.
 76  */
 77 /* ARGSUSED */
 78 void
 79 init_bars(nbars)
 80 int nbars;
 81 {
 82         if ((kmem = open("/dev/kmem", 0)) < 0) {
 83                 perror("/dev/kmem");
 84                 exit(1);
 85         }
 86         (void)nlist(kernelSymbols?kernelSymbols:"/vmunix", nl);
 87         if (lseek(kmem, (long) nl[X_CP_TIME].n_value, 0) !=
 88             (long) nl[X_CP_TIME].n_value)
 89                 perror("lseek");
 90         if (read(kmem, (char *) cp_old, sizeof(cp_old)) != sizeof(cp_old))
 91                 perror("read");
 92 }
 93 
 94 /* 
 95  *  This procedure gets called every interval to compute and display the
 96  *  bars. It should call draw_bar() with the bar number, the array of
 97  *  integer values to display in the bar, and the number of values in
 98  *  the array.
 99  */
100 /* ARGSUSED */
101 void
102 display_bars(nbars)
103 {
104         int     states[CPUSTATES];
105         int     nstates;
106         int     i;
107         extern void draw_bar(/*int bar_num, int *states, int num_states*/);
108         
109         if (lseek(kmem, (long) nl[X_CP_TIME].n_value, 0) !=
110             (long) nl[X_CP_TIME].n_value)
111                 perror("lseek");
112         if (read(kmem, (char *) cp_time, sizeof(cp_time)) !=
113             sizeof(cp_time))
114                 perror("read");
115         
116         
117 #define delta(cpustate) ((int) (cp_time[(cpustate)] - cp_old[(cpustate)]))
118 
119         nstates = 0;
120         states[nstates++] = delta(CP_IDLE);
121         states[nstates++] = delta(CP_USER);
122         states[nstates++] = delta(CP_NICE);
123         states[nstates++] = delta(CP_SYS);
124         draw_bar(0, states, nstates);
125         for (i = 0; i < CPUSTATES; i ++)
126                 cp_old[i] = cp_time[i];
127 }