Old s-mach.c
  1 /*
  2  * System dependent file for Mach
  3  */
  4 
  5 /* 
  6  * Melinda Shore, mt Xinu, 3/21/90 
  7  */
  8 
  9 #include <mach.h>
 10 
 11 #define NSTATES 3
 12 
 13 extern char *xmalloc(/* int nbytes */);
 14 extern kern_return_t host_info();
 15 extern task_t task_self();
 16 extern kern_return_t slot_info();
 17 int *cpu_ids;
 18 machine_slot_data_t **last_sinfo, **cur_sinfo, **tmp_sinfo;
 19 task_t self;
 20 
 21 /* 
 22  * Called at the beginning to enquire how many bars are needed,
 23  * and to determine which cpu slots are filled
 24  */
 25 
 26 int
 27 num_bars()
 28 {
 29     machine_info_data_t minfo;
 30     machine_slot_data_t sinfo;
 31     int i, j = 0, result, ncpus;
 32 
 33     ncpus = 0;
 34     self = task_self();
 35     if ((result = host_info(self, &minfo)) != KERN_SUCCESS)  {
 36         mach_error("xcpustate", result);
 37         exit(1);
 38     }
 39     cpu_ids = (int *)xmalloc(minfo.max_cpus * sizeof (int));
 40     for (i = 0 ; i < minfo.max_cpus ; i++)  {
 41         if ((result = slot_info(self, i, &sinfo)) != KERN_SUCCESS)
 42             mach_error("xcpustate", result);
 43         else
 44             if (sinfo.is_cpu && sinfo.running)  {
 45                 ncpus++;
 46                 cpu_ids[j++] = i;
 47             }
 48     }
 49     return ncpus;
 50 }
 51 
 52 /*
 53  * Indicates how many levels each bar has.  For most machines, each bar will
 54  * have the same stuff.  But one can, for instance, display memory use on one
 55  * bar, processor levels on others, etc.
 56  */
 57 void
 58 bar_items(nbars, items)
 59 int nbars;
 60 int items[];    /* nbars items in this */
 61 {
 62     int i;
 63 
 64     for(i = 0; i < nbars; i++)
 65         items[i] = NSTATES;
 66 }
 67 
 68 /* Called after num_bars to ask for the bar names */
 69 /* ARGSUSED */
 70 char **
 71 label_bars(nbars)
 72 {
 73     char **names;
 74     int i;
 75 
 76     names = (char **)xmalloc(nbars * sizeof (char *));
 77     for (i = 0 ; i < nbars ; i++)  {
 78         names[i] = xmalloc(10);
 79         (void) sprintf(names[i], "Slot %d", cpu_ids[i]);
 80     }
 81     return names;
 82 }
 83 
 84 /* 
 85  *  Called after the bars are created to perform any machine dependent
 86  *  initializations.
 87  */
 88 /* ARGSUSED */
 89 void
 90 init_bars(nbars)
 91 int nbars;
 92 {
 93     int i;
 94     kern_return_t result;
 95 
 96     cur_sinfo = (machine_slot_data_t **)xmalloc(nbars *
 97                                                sizeof (machine_slot_data_t *));
 98     last_sinfo = (machine_slot_data_t **)xmalloc(nbars *
 99                                                sizeof (machine_slot_data_t *));
100     for (i = 0 ; i < nbars ; i++)  {
101         cur_sinfo[i] = (machine_slot_data_t *)xmalloc(sizeof
102                                                    (machine_slot_data_t));
103         last_sinfo[i] = (machine_slot_data_t *)xmalloc(sizeof
104                                                    (machine_slot_data_t));
105         if ((result = slot_info(self, cpu_ids[i], cur_sinfo[i])) !=
106             KERN_SUCCESS)  {
107             mach_error("xcpustate", result);
108             exit(1);
109         }
110     }            
111 }
112 
113 /* 
114  *  This procedure gets called every interval to compute and display the
115  *  bars. It should call draw_bar() with the bar number, the array of
116  *  integer values to display in the bar, and the number of values in
117  *  the array.
118  */
119 /* ARGSUSED */
120 void
121 display_bars(nbars)
122 {
123     int states[CPU_STATE_MAX];
124     int nstates;
125     int i;
126     extern void draw_bar(/*int bar_num, int *states, int num_states*/);
127     kern_return_t result;
128     
129     for (i = 0 ; i < nbars ; i++)  {
130         if ((result = slot_info(self, cpu_ids[i], cur_sinfo[i])) != 
131             KERN_SUCCESS)
132             mach_error("xcpustate", result);
133 #define delta(cpustate) ((int) (cur_sinfo[i]->cpu_ticks[(cpustate)] - \
134     last_sinfo[i]->cpu_ticks[(cpustate)]))
135 
136         nstates = 0;
137         states[nstates++] = delta(CPU_STATE_USER);
138         states[nstates++] = delta(CPU_STATE_SYSTEM);
139         states[nstates++] = delta(CPU_STATE_IDLE);
140         draw_bar(i, states, nstates);
141     }
142     tmp_sinfo = last_sinfo;
143     last_sinfo = cur_sinfo;
144     cur_sinfo = tmp_sinfo;
145 }