Old s-bsdi.c
1 /*
2 * System dependent file for BSDI and possibly BSD4.4
3 */
4 /* Andy Beals, Telebit <asb@telebit.com> */
5 /* LINTLIBRARY */
6 #include <sys/param.h>
7 #include <sys/cpustats.h>
8 #include <sys/types.h>
9 #include <sys/kinfo.h>
10 #include <sys/kinfo_proc.h>
11 #include <sys/sysinfo.h>
12 #include <nlist.h>
13
14 #ifndef MAXHOSTNAMELEN
15 /* Some U*x deviants managed to not have this in sys/param.h */
16 # define MAXHOSTNAMELEN 64
17 #endif
18
19 extern char *xmalloc(/* int nbytes */);
20
21 extern int open(), read();
22 extern long lseek();
23
24 long cp_time[CPUSTATES];
25 long cp_old[CPUSTATES];
26
27 #define NPROCS 1
28
29 /* Called at the beginning to inquire how many bars are needed. */
30 int
31 num_bars()
32 {
33 return NPROCS;
34 }
35
36 /* Called after num_bars to ask for the bar names */
37 /* ARGSUSED */
38 char **
39 label_bars(nbars)
40 {
41 static char *name[NPROCS];
42 static char hname[MAXHOSTNAMELEN];
43
44 name[0] = hname;
45 if (gethostname(name[0], MAXHOSTNAMELEN) < 0) {
46 perror("gethostname");
47 *name[0] = '\0';
48 }
49 return name;
50 }
51
52 /*
53 * Called after the bars are created to perform any machine dependent
54 * initializations.
55 */
56 /* ARGSUSED */
57 void
58 init_bars(nbars)
59 int nbars;
60 {
61 struct cpustats s;
62 int t = sizeof s, j;
63
64 if(getkerninfo(KINFO_CPU, &s, &t, 0) == -1) {
65 perror("getkerninfo");
66 for(j=0;j<CPUSTATES;j++) cp_old[j] = 0;
67 } else {
68 for(j=0;j<CPUSTATES;j++) cp_old[j] = s.cp_time[j];
69 }
70 }
71
72 /*
73 * This procedure gets called every interval to compute and display the
74 * bars. It should call draw_bar() with the bar number, the array of
75 * integer values to display in the bar, and the number of values in
76 * the array.
77 */
78 /* ARGSUSED */
79 void
80 display_bars(nbars)
81 {
82 int states[CPUSTATES];
83 int nstates;
84 int i;
85 extern void draw_bar(/*int bar_num, int *states, int num_states*/);
86 struct cpustats s;
87
88 i=sizeof s;
89 if(getkerninfo(KINFO_CPU, &s, &i, 0) == -1) {
90 perror("getkerninfo");
91 } else {
92 for(i=0;i<CPUSTATES;i++) cp_time[i] = s.cp_time[i];
93 }
94
95 #define delta(cpustate) ((int) (cp_time[(cpustate)] - cp_old[(cpustate)]))
96
97 nstates = 0;
98 states[nstates++] = delta(CP_IDLE);
99 states[nstates++] = delta(CP_USER);
100 states[nstates++] = delta(CP_NICE);
101 states[nstates++] = delta(CP_SYS);
102 draw_bar(0, states, nstates);
103 for (i = 0; i < CPUSTATES; i ++)
104 cp_old[i] = cp_time[i];
105 }