aboutsummaryrefslogtreecommitdiff
path: root/netcpu_kstat10.c
blob: 299e66d7abcfffca632fb18376c8cd8d51398ba9 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
char   netcpu_kstat10_id[]="\
@(#)netcpu_kstat10.c (c) Copyright 2005-2007, Hewlett-Packard Company Version 2.4.3";

#if HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>

#if HAVE_INTTYPES_H
# include <inttypes.h>
#else
# if HAVE_STDINT_H
#  include <stdint.h>
# endif
#endif

#if HAVE_UNISTD_H
# include <unistd.h>
#endif
#if HAVE_STRINGS_H
# include <strings.h>
#endif
#if STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# if HAVE_STDLIB_H
#  include <stdlib.h>
# endif
#endif

#include <errno.h>

#include <kstat.h>
#include <sys/sysinfo.h>

#include "netsh.h"
#include "netlib.h"

static kstat_ctl_t *kc = NULL;
static kid_t kcid = 0;

typedef struct cpu_time_counters {
  uint64_t idle;
  uint64_t user;
  uint64_t kernel;
  uint64_t interrupt;
} cpu_time_counters_t;

static cpu_time_counters_t starting_cpu_counters[MAXCPUS];
static cpu_time_counters_t ending_cpu_counters[MAXCPUS];
static cpu_time_counters_t delta_cpu_counters[MAXCPUS];
static cpu_time_counters_t corrected_cpu_counters[MAXCPUS];

static void
print_cpu_time_counters(char *name, int instance, cpu_time_counters_t *counters) 
{
  fprintf(where,"%s[%d]:\n",name,instance);
  fprintf(where,
	  "\t idle %llu\n",counters[instance].idle);
  fprintf(where,
	  "\t user %llu\n",counters[instance].user);
  fprintf(where,
	  "\t kernel %llu\n",counters[instance].kernel);
  fprintf(where,
	  "\t interrupt %llu\n",counters[instance].interrupt);
}

void
cpu_util_init(void) 
{
  kc = kstat_open();

  if (kc == NULL) {
    fprintf(where,
	    "cpu_util_init: kstat_open: errno %d %s\n",
	    errno,
	    strerror(errno));
    fflush(where);
    exit(-1);
  }
  return;
}

void
cpu_util_terminate(void)
{
  kstat_close(kc);
  return;
}

int
get_cpu_method(void)
{
  return KSTAT_10;
}

static void
print_unexpected_statistic_warning(char *who, char *what, char *why)
{
  if (why) {
    fprintf(where,
	    "WARNING! WARNING! WARNING! WARNING!\n");
    fprintf(where,
	    "%s found an unexpected %s statistic %.16s\n",
	    who,
	    why,
	    what);
  }
  else {
    fprintf(where,
	    "%s is ignoring statistic %.16s\n",
	    who,
	    what);
  }
}

static void
get_cpu_counters(int cpu_num, cpu_time_counters_t *counters)
{

  kstat_t *ksp;
  int found=0;
  kid_t nkcid;
  kstat_named_t *knp;
  int i;

  ksp = kstat_lookup(kc, "cpu", cpu_num, "sys");
  if ((ksp) && (ksp->ks_type == KSTAT_TYPE_NAMED)) {
    /* happiness and joy, keep going */
    nkcid = kstat_read(kc, ksp, NULL);
    if (nkcid != -1) {
      /* happiness and joy, keep going. we could consider adding a
	 "found < 3" to the end conditions, but then we wouldn't
	 search to the end and find that Sun added some nsec. we
	 probably want to see if they add an nsec. raj 2005-01-28 */
      for (i = ksp->ks_ndata, knp = ksp->ks_data;
	   i > 0;
	   knp++,i--) {
	/* we would be hosed if the same name could appear twice */
	if (!strcmp("cpu_nsec_idle",knp->name)) {
	  found++;
	  counters[cpu_num].idle = knp->value.ui64;
	}
	else if (!strcmp("cpu_nsec_user",knp->name)) {
	  found++;
	  counters[cpu_num].user = knp->value.ui64;
	}
	else if (!strcmp("cpu_nsec_kernel",knp->name)) {
	  found++;
	  counters[cpu_num].kernel = knp->value.ui64;
	}
	else if (strstr(knp->name,"nsec")) {
	  /* finding another nsec here means Sun have changed
	     something and we need to warn the user. raj 2005-01-28 */ 
	  print_unexpected_statistic_warning("get_cpu_counters",
					     knp->name,
					     "nsec");
	}
	else if (debug >=2) {

	  /* might want to tell people about what we are skipping.
	     however, only display other names debug >=2. raj
	     2005-01-28
	  */

	  print_unexpected_statistic_warning("get_cpu_counters",
					     knp->name,
					     NULL);
	}
      }
      if (3 == found) {
	/* happiness and joy */
	return;
      }
      else {
	fprintf(where,
		"get_cpu_counters could not find one or more of the expected counters!\n");
	fflush(where);
	exit(-1);
      }
    }
    else {
      /* the kstat_read returned an error or the chain changed */
      fprintf(where,
	      "get_cpu_counters: kstat_read failed or chain id changed %d %s\n",
	      errno,
	      strerror(errno));
      fflush(where);
      exit(-1);
    }
  }
  else {
    /* the lookup failed or found the wrong type */
    fprintf(where,
	    "get_cpu_counters: kstat_lookup failed for module 'cpu' instance %d name 'sys' and KSTAT_TYPE_NAMED: errno %d %s\n",
	    cpu_num,
	    errno,
	    strerror(errno));
    fflush(where);
    exit(-1);
  }
}

static void
get_interrupt_counters(int cpu_num, cpu_time_counters_t *counters)
{
  kstat_t *ksp;
  int found=0;
  kid_t nkcid;
  kstat_named_t *knp;
  int i;

  ksp = kstat_lookup(kc, "cpu", cpu_num, "intrstat");

  counters[cpu_num].interrupt = 0;
  if ((ksp) && (ksp->ks_type == KSTAT_TYPE_NAMED)) {
    /* happiness and joy, keep going */
    nkcid = kstat_read(kc, ksp, NULL);
    if (nkcid != -1) {
      /* happiness and joy, keep going. we could consider adding a
	 "found < 15" to the end conditions, but then we wouldn't
	 search to the end and find that Sun added some "time." we
	 probably want to see if they add a "nsec." raj 2005-01-28 */
      for (i = ksp->ks_ndata, knp = ksp->ks_data;
	   i > 0;
	   knp++,i--) {
	if (strstr(knp->name,"time")) {
	  found++;
	  counters[cpu_num].interrupt += knp->value.ui64;
	}
	else if (debug >=2) {

	  /* might want to tell people about what we are skipping.
	     however, only display other names debug >=2. raj
	     2005-01-28
	  */

	  print_unexpected_statistic_warning("get_cpu_counters",
					     knp->name,
					     NULL);
	}
      }
      if (15 == found) {
	/* happiness and joy */
	return;
      }
      else {
	fprintf(where,
		"get_cpu_counters could not find one or more of the expected counters!\n");
	fflush(where);
	exit(-1);
      }
    }
    else {
      /* the kstat_read returned an error or the chain changed */
      fprintf(where,
	      "get_cpu_counters: kstat_read failed or chain id changed %d %s\n",
	      errno,
	      strerror(errno));
      fflush(where);
      exit(-1);
    }
  }
  else {
    /* the lookup failed or found the wrong type */
    fprintf(where,
	    "get_cpu_counters: kstat_lookup failed for module 'cpu' instance %d class 'intrstat' and KSTAT_TYPE_NAMED: errno %d %s\n",
	    cpu_num,
	    errno,
	    strerror(errno));
    fflush(where);
    exit(-1);
  }

}

static void
get_cpu_time_counters(cpu_time_counters_t *counters)
{

  int i;

  for (i = 0; i < lib_num_loc_cpus; i++){
    get_cpu_counters(i, counters);
    get_interrupt_counters(i, counters);
  }

  return;
}

/* the kstat10 mechanism, since it is based on actual nanosecond
   counters is not going to use a comparison to an idle rate. so, the
   calibrate_idle_rate routine will be rather simple :) raj 2005-01-28
   */ 

float
calibrate_idle_rate(int iterations, int interval)
{
  return 0.0;
}

float
calc_cpu_util_internal(float elapsed_time)
{
  int i;
  float correction_factor;
  float actual_rate;
  
  uint64_t total_cpu_nsec;

  /* multiply by 100 and divide by total and you get whole
     percentages. multiply by 1000 and divide by total and you get
     tenths of percentages.  multiply by 10000 and divide by total and
     you get hundredths of percentages. etc etc etc raj 2005-01-28 */

#define CALC_PERCENT 100
#define CALC_TENTH_PERCENT 1000
#define CALC_HUNDREDTH_PERCENT 10000
#define CALC_THOUSANDTH_PERCENT 100000
#define CALC_ACCURACY CALC_THOUSANDTH_PERCENT
  
  uint64_t fraction_idle;
  uint64_t fraction_user;
  uint64_t fraction_kernel;
  uint64_t fraction_interrupt;

  uint64_t interrupt_idle;
  uint64_t interrupt_user;
  uint64_t interrupt_kernel;

  lib_local_cpu_util = (float)0.0;

  /* It is possible that the library measured a time other than */
  /* the one that the user want for the cpu utilization */
  /* calculations - for example, tests that were ended by */
  /* watchdog timers such as the udp stream test. We let these */
  /* tests tell up what the elapsed time should be. */
  
  if (elapsed_time != 0.0) {
    correction_factor = (float) 1.0 + 
      ((lib_elapsed - elapsed_time) / elapsed_time);
  }
  else {
    correction_factor = (float) 1.0;
  }

  for (i = 0; i < lib_num_loc_cpus; i++) {

    /* this is now the fun part.  we have the nanoseconds _allegedly_
       spent in user, idle and kernel.  We also have nanoseconds spent
       servicing interrupts.  Sadly, in the developer's finite wisdom,
       the interrupt time accounting is in parallel with the other
       accounting. this means that time accounted in user, kernel or
       idle will also include time spent in interrupt.  for netperf's
       porpoises we do not really care about that for user and kernel,
       but we certainly do care for idle.  the $64B question becomes -
       how to "correct" for this?

       we could just subtract interrupt time from idle.  that has the
       virtue of simplicity and also "punishes" Sun for doing
       something that seems to be so stupid.  however, we probably
       have to be "fair" even to the allegedly stupid so the other
       mechanism, suggested by a Sun engineer is to subtract interrupt
       time from each of user, kernel and idle in proportion to their
       numbers.  then we sum the corrected user, kernel and idle along
       with the interrupt time and use that to calculate a new idle
       percentage and thus a CPU util percentage.

       that is what we will attempt to do here.  raj 2005-01-28 

       of course, we also have to wonder what we should do if there is
       more interrupt time than the sum of user, kernel and idle.
       that is a theoretical possibility I suppose, but for the
       time-being, one that we will blythly ignore, except perhaps for
       a quick check. raj 2005-01-31 
    */
    
    /* we ass-u-me that these counters will never wrap during a
       netperf run.  this may not be a particularly safe thing to
       do. raj 2005-01-28 */
    delta_cpu_counters[i].idle = ending_cpu_counters[i].idle -
      starting_cpu_counters[i].idle;
    delta_cpu_counters[i].user = ending_cpu_counters[i].user -
      starting_cpu_counters[i].user;
    delta_cpu_counters[i].kernel = ending_cpu_counters[i].kernel -
      starting_cpu_counters[i].kernel;
    delta_cpu_counters[i].interrupt = ending_cpu_counters[i].interrupt -
      starting_cpu_counters[i].interrupt;
    
    if (debug) {
      print_cpu_time_counters("delta_cpu_counters",i,delta_cpu_counters);
    }

    /* for this summation, we do not include interrupt time */
    total_cpu_nsec = 
      delta_cpu_counters[i].idle +
      delta_cpu_counters[i].user +
      delta_cpu_counters[i].kernel;

    if (debug) {
      fprintf(where,"total_cpu_nsec %llu\n",total_cpu_nsec);
    }

    if (delta_cpu_counters[i].interrupt > total_cpu_nsec) {
      /* we are not in Kansas any more Toto, and I am not quite sure
	 the best way to get our tails out of here so let us just
	 punt. raj 2005-01-31 */
      fprintf(where,
	      "WARNING! WARNING! WARNING! WARNING! WARNING! \n");
      fprintf(where,
	      "calc_cpu_util_internal: more interrupt time than others combined!\n");
      fprintf(where,
	      "\tso CPU util cannot be estimated\n");
      fprintf(where,
	      "\t delta[%d].interrupt %llu\n",i,delta_cpu_counters[i].interrupt);
      fprintf(where,
	      "\t delta[%d].idle %llu\n",i,delta_cpu_counters[i].idle);
      fprintf(where,
	      "\t delta[%d].user %llu\n",i,delta_cpu_counters[i].user);
      fprintf(where,
	      "\t delta[%d].kernel %llu\n",i,delta_cpu_counters[i].kernel);
      fflush(where);
      
      lib_local_cpu_util = -1.0;
      lib_local_per_cpu_util[i] = -1.0;
      return -1.0;
    }

    /* and now some fun with integer math.  i initially tried to
       promote things to long doubled but that didn't seem to result
       in happiness and joy. raj 2005-01-28 */

    fraction_idle = 
      (delta_cpu_counters[i].idle * CALC_ACCURACY) / total_cpu_nsec;

    fraction_user = 
      (delta_cpu_counters[i].user * CALC_ACCURACY) / total_cpu_nsec;

    fraction_kernel = 
      (delta_cpu_counters[i].kernel * CALC_ACCURACY) / total_cpu_nsec;

    /* ok, we have our fractions, now we want to take that fraction of
       the interrupt time and subtract that from the bucket. */

    interrupt_idle =  ((delta_cpu_counters[i].interrupt * fraction_idle) / 
		       CALC_ACCURACY);

    interrupt_user = ((delta_cpu_counters[i].interrupt * fraction_user) / 
		      CALC_ACCURACY);

    interrupt_kernel = ((delta_cpu_counters[i].interrupt * fraction_kernel) / 
			CALC_ACCURACY);

    if (debug) {
      fprintf(where,
	      "\tfraction_idle %llu interrupt_idle %llu\n",
	      fraction_idle,
	      interrupt_idle);
      fprintf(where,
	      "\tfraction_user %llu interrupt_user %llu\n",
	      fraction_user,
	      interrupt_user);
      fprintf(where,"\tfraction_kernel %llu interrupt_kernel %llu\n",
	      fraction_kernel,
	      interrupt_kernel);
    }

    corrected_cpu_counters[i].idle = delta_cpu_counters[i].idle - 
      interrupt_idle;

    corrected_cpu_counters[i].user = delta_cpu_counters[i].user - 
      interrupt_user;

    corrected_cpu_counters[i].kernel = delta_cpu_counters[i].kernel - 
      interrupt_kernel;

    corrected_cpu_counters[i].interrupt = delta_cpu_counters[i].interrupt;
		  
    if (debug) {
      print_cpu_time_counters("corrected_cpu_counters",
			      i,
			      corrected_cpu_counters);
    }

    /* I was going to checkfor going less than zero, but since all the
       calculations are in unsigned quantities that would seem to be a
       triffle silly... raj 2005-01-28 */

    /* ok, now we sum the numbers again, this time including interrupt
       */

    total_cpu_nsec = 
      corrected_cpu_counters[i].idle +
      corrected_cpu_counters[i].user +
      corrected_cpu_counters[i].kernel +
      corrected_cpu_counters[i].interrupt;

    /* and recalculate our fractions we are really only going to use
       fraction_idle, but lets calculate the rest just for the heck of
       it. one day we may want to display them. raj 2005-01-28 */
    
    /* multiply by 100 and divide by total and you get whole
       percentages. multiply by 1000 and divide by total and you get
       tenths of percentages.  multiply by 10000 and divide by total
       and you get hundredths of percentages. etc etc etc raj
       2005-01-28 */
    fraction_idle = 
      (corrected_cpu_counters[i].idle * CALC_ACCURACY) / total_cpu_nsec;

    fraction_user = 
      (corrected_cpu_counters[i].user * CALC_ACCURACY) / total_cpu_nsec;

    fraction_kernel = 
      (corrected_cpu_counters[i].kernel * CALC_ACCURACY) / total_cpu_nsec;

    fraction_interrupt = 
      (corrected_cpu_counters[i].interrupt * CALC_ACCURACY) / total_cpu_nsec;

    if (debug) {
      fprintf(where,"\tfraction_idle %lu\n",fraction_idle);
      fprintf(where,"\tfraction_user %lu\n",fraction_user);
      fprintf(where,"\tfraction_kernel %lu\n",fraction_kernel);
      fprintf(where,"\tfraction_interrupt %lu\n",fraction_interrupt);
    }

    /* and finally, what is our CPU utilization? */
    lib_local_per_cpu_util[i] = 100.0 - (((float)fraction_idle / 
					  (float)CALC_ACCURACY) * 100.0);
    if (debug) {
      fprintf(where,
	      "lib_local_per_cpu_util[%d] %g\n",
	      i,
	      lib_local_per_cpu_util[i]);
    }
    lib_local_cpu_util += lib_local_per_cpu_util[i];
  }
  /* we want the average across all n processors */
  lib_local_cpu_util /= (float)lib_num_loc_cpus;
  
  lib_local_cpu_util *= correction_factor;
  return lib_local_cpu_util;


}

void
cpu_start_internal(void)
{
  get_cpu_time_counters(starting_cpu_counters);
  return;
}

void
cpu_stop_internal(void)
{
  get_cpu_time_counters(ending_cpu_counters);
}