1 char netcpu_sysctl_id[]="\ 2 @(#)netcpu_sysctl.c Version 2.6.0"; 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 #ifdef __NetBSD__ 37 #define CP_TIME_TYPE uint64_t 38 #else 39 #define CP_TIME_TYPE long 40 #endif 41 42 #include <errno.h> 43 44 /* need to have some sort of check for sys/sysctl.h versus sysctl.h */ 45 #include <sys/sysctl.h> 46 47 48 /* this has been liberally cut and pasted from <sys/resource.h> on 49 FreeBSD. in general, this would be a bad idea, but I don't want to 50 have to do a _KERNEL define to get these and that is what 51 sys/resource.h seems to want. raj 2002-03-03 */ 52 #define CP_USER 0 53 #define CP_NICE 1 54 #define CP_SYS 2 55 #define CP_INTR 3 56 #define CP_IDLE 4 57 #define CPUSTATES 5 58 59 60 #include "netsh.h" 61 #include "netlib.h" 62 63 static CP_TIME_TYPE lib_start_count[CPUSTATES]; 64 static CP_TIME_TYPE lib_end_count[CPUSTATES]; 65 66 void 67 cpu_util_init(void) 68 { 69 return; 70 } 71 72 void 73 cpu_util_terminate(void) 74 { 75 return; 76 } 77 78 int 79 get_cpu_method(void) 80 { 81 return SYSCTL; 82 } 83 84 static void 85 get_cpu_time(CP_TIME_TYPE *cpu_time) 86 { 87 size_t cpu_time_len = CPUSTATES * sizeof (cpu_time[0]); 88 89 if (sysctlbyname("kern.cp_time", cpu_time, &cpu_time_len, NULL, 0) == -1) { 90 fprintf (stderr, "Cannot get CPU time!\n"); 91 exit (1); 92 } 93 } 94 95 /* calibrate_sysctl - perform the idle rate calculation using the 96 sysctl call - typically on BSD */ 97 98 float 99 calibrate_idle_rate(int iterations, int interval) 100 { 101 return sysconf (_SC_CLK_TCK); 102 } 103 104 float 105 calc_cpu_util_internal(float elapsed_time) 106 { 107 CP_TIME_TYPE sum_idle, sum_busy; 108 int i; 109 110 memset(&lib_local_cpu_stats, 0, sizeof(lib_local_cpu_stats)); 111 112 for (sum_busy = 0, i = 0; i < CPUSTATES; i++) { 113 if (i != CP_IDLE) 114 sum_busy += lib_end_count[i] - lib_start_count[i]; 115 } 116 117 sum_idle = lib_end_count[CP_IDLE] - lib_start_count[CP_IDLE]; 118 lib_local_cpu_stats.cpu_util = (float)sum_busy / (float)(sum_busy + sum_idle); 119 lib_local_cpu_stats.cpu_util *= 100.0; 120 121 return lib_local_cpu_stats.cpu_util; 122 123 } 124 void 125 cpu_start_internal(void) 126 { 127 get_cpu_time(lib_start_count); 128 } 129 130 void 131 cpu_stop_internal(void) 132 { 133 get_cpu_time(lib_end_count); 134 } 135