Home | History | Annotate | Download | only in netperf
      1 char   netcpu_sysctl_id[]="\
      2 @(#)netcpu_sysctl.c  Version 2.4.3";
      3 
      4 #if HAVE_CONFIG_H
      5 # include <config.h>
      6 #endif
      7 
      8 #include <stdio.h>
      9 #include <unistd.h>
     10 
     11 #if HAVE_INTTYPES_H
     12 # include <inttypes.h>
     13 #else
     14 # if HAVE_STDINT_H
     15 #  include <stdint.h>
     16 # endif
     17 #endif
     18 
     19 #if TIME_WITH_SYS_TIME
     20 # include <sys/time.h>
     21 # include <time.h>
     22 #else
     23 # if HAVE_SYS_TIME_H
     24 #  include <sys/time.h>
     25 # else
     26 #  include <time.h>
     27 # endif
     28 #endif
     29 #if HAVE_LIMITS_H
     30 # include <limits.h>
     31 # ifndef LONG_LONG_MAX
     32 #  define LONG_LONG_MAX LLONG_MAX
     33 # endif /* LONG_LONG_MAX */
     34 #endif
     35 
     36 
     37 #include <errno.h>
     38 
     39 /* need to have some sort of check for sys/sysctl.h versus sysctl.h */
     40 #include <sys/sysctl.h>
     41 
     42 
     43 /* this has been liberally cut and pasted from <sys/resource.h> on
     44    FreeBSD. in general, this would be a bad idea, but I don't want to
     45    have to do a _KERNEL define to get these and that is what
     46    sys/resource.h seems to want. raj 2002-03-03 */
     47 #define CP_USER         0
     48 #define CP_NICE         1
     49 #define CP_SYS          2
     50 #define CP_INTR         3
     51 #define CP_IDLE         4
     52 #define CPUSTATES       5
     53 
     54 
     55 #include "netsh.h"
     56 #include "netlib.h"
     57 
     58 static long lib_start_count[CPUSTATES];
     59 static long lib_end_count[CPUSTATES];
     60 
     61 void
     62 cpu_util_init(void)
     63 {
     64   return;
     65 }
     66 
     67 void
     68 cpu_util_terminate(void)
     69 {
     70   return;
     71 }
     72 
     73 int
     74 get_cpu_method(void)
     75 {
     76   return SYSCTL;
     77 }
     78 
     79 static void
     80 get_cpu_time(long *cpu_time)
     81 {
     82   size_t cpu_time_len = CPUSTATES * sizeof (cpu_time[0]);
     83 
     84   if (sysctlbyname("kern.cp_time", cpu_time, &cpu_time_len, NULL, 0) == -1) {
     85       fprintf (stderr, "Cannot get CPU time!\n");
     86       exit (1);
     87   }
     88 }
     89 
     90 /* calibrate_sysctl  - perform the idle rate calculation using the
     91    sysctl call - typically on BSD */
     92 
     93 float
     94 calibrate_idle_rate(int iterations, int interval)
     95 {
     96   return sysconf (_SC_CLK_TCK);
     97 }
     98 
     99 float
    100 calc_cpu_util_internal(float elapsed_time)
    101 {
    102   long sum_idle, sum_busy;
    103   int i;
    104 
    105   for (sum_busy = 0, i = 0; i < CPUSTATES; i++) {
    106     if (i != CP_IDLE)
    107       sum_busy += lib_end_count[i] - lib_start_count[i];
    108   }
    109 
    110   sum_idle = lib_end_count[CP_IDLE] - lib_start_count[CP_IDLE];
    111   lib_local_cpu_util = (float)sum_busy / (float)(sum_busy + sum_idle);
    112   lib_local_cpu_util *= 100.0;
    113 
    114   return lib_local_cpu_util;
    115 
    116 }
    117 void
    118 cpu_start_internal(void)
    119 {
    120   get_cpu_time(lib_start_count);
    121 }
    122 
    123 void
    124 cpu_stop_internal(void)
    125 {
    126   get_cpu_time(lib_end_count);
    127 }
    128