Old rstat.c
  1 /* 
  2  * rstat RPC code
  3  */
  4 /* John DiMarco, CSLab, University of Toronto <jdd@cs.toronto.edu> */
  5 
  6 #ifdef RSTAT
  7 
  8 /* LINTLIBRARY */
  9 #define MULTIPROCESSOR
 10 
 11 #include <sys/types.h>
 12 #include <sys/file.h>
 13 #include <stdio.h>
 14 #include <rpc/rpc.h>
 15 #include "rstat.h"
 16 
 17 /* most typical cpu states; for BSD systems */
 18 #define CP_USER         0
 19 #define CP_NICE         1
 20 #define CP_SYS          2
 21 #define CP_IDLE         3
 22 
 23 #define DISKSTATES 2 /* idle, transfer */
 24 
 25 int ncpu;       /* number of CPUs */
 26 int ndisk;      /* number of disks */
 27 
 28 #define BUFFSIZE 64
 29 
 30 #ifndef MAXHOSTNAMELEN
 31   /* Some U*x deviants managed to not have this in sys/param.h */
 32 # define MAXHOSTNAMELEN 64
 33 #endif
 34 
 35 static statstime *sts;
 36 
 37 static CLIENT *cl;
 38 
 39 extern void draw_bar(/*int bar_num, int *states, int num_states*/);
 40 
 41 extern char *rstathost;
 42 extern int cpuflag, diskflag;
 43 
 44 extern char *xmalloc(/* int nbytes */);
 45 extern void shorten(/* char *hname */);
 46 
 47 /* Called at the beginning to inquire how many bars are needed. */
 48 int
 49 rstat_num_bars()
 50 {
 51         int nbars=0;
 52 
 53         if(NULL==(cl=clnt_create(rstathost, RSTATPROG, RSTATVERS_TIME, "udp"))){
 54                 clnt_pcreateerror(rstathost);
 55                 exit(1);
 56         }
 57 
 58         if(cpuflag){
 59                 ncpu=1; /* rstat only supports uniprocessors */
 60                 nbars += ncpu;
 61         }
 62         if(diskflag){
 63                 ndisk=RSTAT_DK_NDRIVE;
 64                 nbars += ndisk;
 65         }
 66         return(nbars);
 67 }
 68 
 69 /*
 70  * Indicates how many levels each bar has.  For most machines, each bar will
 71  * have the same stuff.  But one can, for instance, display memory use on one
 72  * bar, processor levels on others, etc.
 73  */
 74 void
 75 rstat_bar_items(nbars, items)
 76 int nbars;
 77 int items[];    /* nbars items in this */
 78 {
 79     int i, n=0;
 80 
 81     if(cpuflag){
 82         for(i=0; i<ncpu; i++,n++){
 83                 items[n] = RSTAT_CPUSTATES;
 84         }
 85     }
 86     if(diskflag){
 87         for(i=0; i<ndisk; i++,n++){
 88                 items[n] = DISKSTATES;
 89         }
 90     }
 91 }
 92 
 93 /* Called after num_bars to ask for the bar names */
 94 char **
 95 rstat_label_bars(nbars)
 96 {
 97         char **names;
 98         int i, base;
 99         extern char *strcpy();
100         static char hname[MAXHOSTNAMELEN + 1];
101         char buf[MAXHOSTNAMELEN + 1 + BUFFSIZE];
102         char *cpname="";
103 
104         strcpy(hname, rstathost);
105         shorten(hname);
106         names = (char **) xmalloc(nbars * sizeof(char *));
107 
108         base=0; 
109 
110         if(cpuflag) {
111                 /* do cpu names */
112                 for(i=0; i<ncpu; i++) {
113                         (void) sprintf(buf, "%s%s%s%d", hname, 
114                                         hname[0]?" ":"", cpname, i);
115                         names[base+i] = strcpy(xmalloc(strlen(buf) + 1), buf);
116                 }
117                 base+=ncpu;
118         }
119         if(diskflag) {
120                 /* do disk names */
121                 for(i=0; i <ndisk; i++) {
122                         (void) sprintf(buf, "%s%sd%d", hname, hname[0]?" ":"", 
123                                         i);
124                         names[base+i] = strcpy(xmalloc(strlen(buf) + 1), buf);
125                 }
126                 base+=ndisk;
127         }
128         return names;
129 }
130 
131 /* 
132  *  Called after the bars are created to perform any machine dependent
133  *  initializations.
134  */
135 /* ARGSUSED */
136 void
137 rstat_init_bars(nbars)
138 int nbars;
139 {
140 }
141 
142 static void
143 display_cpu(base)
144 int base;
145 {
146         int states[RSTAT_CPUSTATES], i;
147         static long old[RSTAT_CPUSTATES];
148 
149         states[0] = (long)(sts->cp_time[CP_IDLE] - old[CP_IDLE]);
150         states[1] = (long)(sts->cp_time[CP_NICE] - old[CP_NICE]);
151         states[2] = (long)(sts->cp_time[CP_USER] - old[CP_USER]);
152         states[3] = (long)(sts->cp_time[CP_SYS] - old[CP_SYS]);
153         for(i=0;i<RSTAT_CPUSTATES;i++) old[i]=sts->cp_time[i];
154         draw_bar(base, states, RSTAT_CPUSTATES);
155 }
156 
157 static void
158 display_disk(base)
159 int base;
160 {
161         int states[2], i;
162         static long old[RSTAT_DK_NDRIVE];
163 #define MAXDISKXFER 100
164 
165         for(i=0;i<RSTAT_DK_NDRIVE;i++){
166                 long xfer;
167 
168                 xfer = sts->dk_xfer[i]-old[i];
169                 if(xfer>MAXDISKXFER) xfer=MAXDISKXFER;
170                 states[0] = MAXDISKXFER - xfer;
171                 states[1] = xfer;
172                 draw_bar(base+i, states, 2);
173                 old[i] = sts->dk_xfer[i];
174         }
175 }
176 
177 
178 /* 
179  *  This procedure gets called every interval to compute and display the
180  *  bars. It should call draw_bar() with the bar number, the array of
181  *  integer values to display in the bar, and the number of values in
182  *  the array.
183  */
184 /* ARGSUSED */
185 void
186 rstat_display_bars(nbars)
187 int nbars;
188 {
189         int n=0;
190         if(NULL==(sts=rstatproc_stats_3((void *)NULL, cl))){
191                 clnt_perror(cl, rstathost);
192                 exit(1);
193         }
194         if(cpuflag) {
195                 display_cpu(n);
196                 n+=ncpu;
197         }
198         if(diskflag) {
199                 display_disk(n);
200                 n+=ndisk;
201         }
202 }
203 
204 #endif /* RSTAT */