| 1 |
/* |
| 2 |
* sysctl() interface |
| 3 |
* e.g. BSD/OS, NetBSD, OpenBSD, later Darwin releases |
| 4 |
*/ |
| 5 |
#include <net-snmp/net-snmp-config.h> |
| 6 |
#include <net-snmp/net-snmp-features.h> |
| 7 |
#include <net-snmp/net-snmp-includes.h> |
| 8 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 9 |
#include <net-snmp/agent/hardware/cpu.h> |
| 10 |
|
| 11 |
#include <stdlib.h> |
| 12 |
#include <unistd.h> |
| 13 |
#include <sys/types.h> |
| 14 |
|
| 15 |
#if defined(freebsd3) |
| 16 |
#include <sys/resource.h> |
| 17 |
#if !defined(CPUSTATES) |
| 18 |
#include <sys/dkstat.h> |
| 19 |
#endif |
| 20 |
#else |
| 21 |
#include <sys/sched.h> |
| 22 |
#endif |
| 23 |
|
| 24 |
#include <sys/param.h> |
| 25 |
#include <sys/sysctl.h> |
| 26 |
#include <sys/vmmeter.h> |
| 27 |
#ifdef HAVE_VM_VM_PARAM_H |
| 28 |
#include <vm/vm_param.h> |
| 29 |
#endif |
| 30 |
#ifdef HAVE_VM_VM_EXTERN_H |
| 31 |
#include <vm/vm_extern.h> |
| 32 |
#endif |
| 33 |
#ifdef HAVE_UVM_UVM_EXTERN_H |
| 34 |
#include <uvm/uvm_extern.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
netsnmp_feature_require(hardware_cpu_copy_stats); |
| 38 |
|
| 39 |
void _cpu_copy_stats( netsnmp_cpu_info *cpu ); |
| 40 |
|
| 41 |
/* |
| 42 |
* Initialise the list of CPUs on the system |
| 43 |
* (including descriptions) |
| 44 |
*/ |
| 45 |
void init_cpu_sysctl( void ) { |
| 46 |
int i, n; |
| 47 |
size_t siz; |
| 48 |
int ncpu_mib[] = { CTL_HW, HW_NCPU }; |
| 49 |
#if !(defined(__NetBSD__) && ( defined(__i386__) || defined(__x86_64__) ) ) |
| 50 |
int model_mib[] = { CTL_HW, HW_MODEL }; |
| 51 |
#endif |
| 52 |
char descr[ SNMP_MAXBUF ]; |
| 53 |
netsnmp_cpu_info *cpu = netsnmp_cpu_get_byIdx( -1, 1 ); |
| 54 |
strcpy(cpu->name, "Overall CPU statistics"); |
| 55 |
|
| 56 |
siz = sizeof(n); |
| 57 |
sysctl(ncpu_mib, 2, &n, &siz, NULL, 0); |
| 58 |
if ( n <= 0 ) |
| 59 |
n = 1; /* Single CPU system */ |
| 60 |
siz = sizeof(descr); |
| 61 |
#if defined(__NetBSD__) && ( defined(__i386__) || defined(__x86_64__) ) |
| 62 |
sysctlbyname("machdep.cpu_brand", descr, &siz, NULL, 0); |
| 63 |
#else |
| 64 |
sysctl(model_mib, 2, descr, &siz, NULL, 0); |
| 65 |
#endif |
| 66 |
for ( i = 0; i < n; i++ ) { |
| 67 |
cpu = netsnmp_cpu_get_byIdx( i, 1 ); |
| 68 |
cpu->status = 2; /* running */ |
| 69 |
sprintf( cpu->name, "cpu%d", i ); |
| 70 |
sprintf( cpu->descr, "%s", descr ); |
| 71 |
} |
| 72 |
cpu_num = n; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
#if defined(__NetBSD__) |
| 77 |
#define NETSNMP_CPU_STATS uint64_t |
| 78 |
#else |
| 79 |
#define NETSNMP_CPU_STATS long |
| 80 |
#endif |
| 81 |
|
| 82 |
#if defined(__NetBSD__) |
| 83 |
#define NETSNMP_KERN_CPU KERN_CP_TIME |
| 84 |
#define NETSNMP_KERN_MCPU |
| 85 |
#define NETSNMP_KERN_MCPU_TYPE NETSNMP_CPU_STATS |
| 86 |
#elif defined(KERN_CPUSTATS) /* BSDi */ |
| 87 |
#define NETSNMP_KERN_CPU KERN_CPUSTATS |
| 88 |
#elif defined(KERN_CPTIME2) /* OpenBSD */ |
| 89 |
#define NETSNMP_KERN_CPU KERN_CPTIME |
| 90 |
#define NETSNMP_KERN_MCPU |
| 91 |
#define NETSNMP_KERN_MCPU_TYPE NETSNMP_CPU_STATS |
| 92 |
#elif defined(KERN_CPTIME) /* OpenBSD */ |
| 93 |
#define NETSNMP_KERN_CPU KERN_CPTIME |
| 94 |
|
| 95 |
#elif defined(freebsd3) |
| 96 |
#define NETSNMP_KERN_MCPU 1 /* Enable support for multi-cpu stats. Valid for FreeBSD >=6.4, >=7.1, >=8.0 and beyond */ |
| 97 |
#define NETSNMP_KERN_MCPU_TYPE NETSNMP_CPU_STATS |
| 98 |
|
| 99 |
#else |
| 100 |
#error "No CPU statistics sysctl token" |
| 101 |
#endif |
| 102 |
|
| 103 |
/* |
| 104 |
Need to check details before enabling this! |
| 105 |
#if defined(KERN_MPCPUSTATS) |
| 106 |
#define NETSNMP_KERN_MCPU KERN_MPCPUSTATS |
| 107 |
#define NETSNMP_KERN_MCPU_TYPE struct mpcpustats |
| 108 |
#elif defined(KERN_MP_CPUSTATS) |
| 109 |
#define NETSNMP_KERN_MCPU KERN_MP_CPUSTATS |
| 110 |
#define NETSNMP_KERN_MCPU_TYPE struct cpustats |
| 111 |
#endif |
| 112 |
*/ |
| 113 |
|
| 114 |
#if defined(VM_UVMEXP2) || defined(VM_UVMEXP) |
| 115 |
#define NS_VM_INTR intrs |
| 116 |
#define NS_VM_SWTCH swtch |
| 117 |
#define NS_VM_PAGEIN pageins |
| 118 |
#define NS_VM_PAGEOUT pdpageouts |
| 119 |
#ifdef HAVE_STRUCT_UVMEXP_PGSWAPIN |
| 120 |
#define NS_VM_SWAPIN pgswapin |
| 121 |
#define NS_VM_SWAPOUT pgswapout |
| 122 |
#else |
| 123 |
#define NS_VM_SWAPIN swapins |
| 124 |
#define NS_VM_SWAPOUT swapouts |
| 125 |
#endif |
| 126 |
|
| 127 |
#if defined(VM_UVMEXP2) /* NetBSD 1.6+ */ |
| 128 |
#define NETSNMP_VM_STATS VM_UVMEXP2 |
| 129 |
#define NETSNMP_VM_STATS_TYPE struct uvmexp_sysctl |
| 130 |
#else /* VM_UVMEXP */ /* OpenBSD 3+, NetBSD 1.6+ */ |
| 131 |
#define NETSNMP_VM_STATS VM_UVMEXP |
| 132 |
#define NETSNMP_VM_STATS_TYPE struct uvmexp |
| 133 |
#endif /* VM_UVMEXP2 || VM_UVMEXP */ |
| 134 |
|
| 135 |
#elif defined(freebsd3) /* FreeBSD */ |
| 136 |
#if __FreeBSD_version >= 1200028 |
| 137 |
#define VMMETER_TYPE uint64_t |
| 138 |
#else |
| 139 |
#define VMMETER_TYPE u_int |
| 140 |
#endif |
| 141 |
struct __vmmeter { |
| 142 |
VMMETER_TYPE v_intr; |
| 143 |
VMMETER_TYPE v_swtch; |
| 144 |
VMMETER_TYPE v_swappgsin; |
| 145 |
VMMETER_TYPE v_swappgsout; |
| 146 |
VMMETER_TYPE v_swapin; |
| 147 |
VMMETER_TYPE v_swapout; |
| 148 |
}; |
| 149 |
#define NETSNMP_VM_STATS VM_METER |
| 150 |
#define NETSNMP_VM_STATS_TYPE struct __vmmeter |
| 151 |
#define NS_VM_INTR v_intr |
| 152 |
#define NS_VM_SWTCH v_swtch |
| 153 |
#define NS_VM_PAGEIN v_swappgsin |
| 154 |
#define NS_VM_PAGEOUT v_swappgsout |
| 155 |
#define NS_VM_SWAPIN v_swapin |
| 156 |
#define NS_VM_SWAPOUT v_swapout |
| 157 |
|
| 158 |
#elif defined(VM_METER) /* OpenBSD, NetBSD */ |
| 159 |
#define NETSNMP_VM_STATS VM_METER |
| 160 |
#define NETSNMP_VM_STATS_TYPE struct vmtotal |
| 161 |
|
| 162 |
#elif defined(VM_CNT) /* BSDi */ |
| 163 |
#define NETSNMP_VM_STATS VM_CNT |
| 164 |
#define NETSNMP_VM_STATS_TYPE struct vmmeter |
| 165 |
#define NS_VM_INTR v_intr |
| 166 |
#define NS_VM_SWTCH v_swtch |
| 167 |
#undef NS_VM_PAGEIN |
| 168 |
#undef NS_VM_PAGEOUT |
| 169 |
#define NS_VM_SWAPIN v_swpin |
| 170 |
#define NS_VM_SWAPOUT v_swpout |
| 171 |
#endif |
| 172 |
|
| 173 |
|
| 174 |
/* |
| 175 |
* Load the latest CPU usage statistics |
| 176 |
*/ |
| 177 |
int netsnmp_cpu_arch_load( netsnmp_cache *cache, void *magic ) { |
| 178 |
|
| 179 |
/* |
| 180 |
* Strictly speaking, BSDi ought to use |
| 181 |
* "struct cpustats cpu_stats" |
| 182 |
* but this array was used in the previous code, and |
| 183 |
* is correct for the {Open,Net}BSD versions too. |
| 184 |
* Don't fight it, Dave - go with the flow.... |
| 185 |
*/ |
| 186 |
NETSNMP_CPU_STATS cpu_stats[CPUSTATES]; |
| 187 |
size_t cpu_size = sizeof(cpu_stats); |
| 188 |
#if !defined(freebsd3) && !defined(__NetBSD__) |
| 189 |
int cpu_mib[] = { CTL_KERN, NETSNMP_KERN_CPU }; |
| 190 |
#endif |
| 191 |
#ifdef freebsd3 |
| 192 |
static int cp_times = -1; |
| 193 |
#endif |
| 194 |
#ifdef NETSNMP_KERN_MCPU |
| 195 |
#ifdef KERN_CPTIME2 |
| 196 |
int mcpu_mib[] = { CTL_KERN, KERN_CPTIME2, 0 }; |
| 197 |
#endif |
| 198 |
int i; |
| 199 |
int act_cpu = cpu_num; |
| 200 |
NETSNMP_KERN_MCPU_TYPE *mcpu_stats; |
| 201 |
size_t mcpu_size; |
| 202 |
#endif |
| 203 |
NETSNMP_VM_STATS_TYPE mem_stats; |
| 204 |
netsnmp_cpu_info *cpu = netsnmp_cpu_get_byIdx( -1, 0 ); |
| 205 |
|
| 206 |
#if defined(freebsd3) || defined(__NetBSD__) |
| 207 |
sysctlbyname("kern.cp_time", cpu_stats, &cpu_size, NULL, 0); |
| 208 |
#else |
| 209 |
sysctl(cpu_mib, 2, cpu_stats, &cpu_size, NULL, 0); |
| 210 |
#endif |
| 211 |
cpu->user_ticks = cpu_stats[CP_USER]; |
| 212 |
cpu->nice_ticks = cpu_stats[CP_NICE]; |
| 213 |
cpu->sys2_ticks = cpu_stats[CP_SYS]+cpu_stats[CP_INTR]; |
| 214 |
cpu->kern_ticks = cpu_stats[CP_SYS]; |
| 215 |
cpu->idle_ticks = cpu_stats[CP_IDLE]; |
| 216 |
cpu->intrpt_ticks = cpu_stats[CP_INTR]; |
| 217 |
/* wait_ticks, sirq_ticks unused */ |
| 218 |
|
| 219 |
#ifdef freebsd3 |
| 220 |
#define GET_VM_STATS(space, name) sysctlbyname("vm.stats." #space "." #name, &mem_stats.name, &len, NULL, 0) |
| 221 |
{ |
| 222 |
size_t len; |
| 223 |
|
| 224 |
len = sizeof(VMMETER_TYPE); |
| 225 |
GET_VM_STATS(sys, v_intr); |
| 226 |
GET_VM_STATS(sys, v_swtch); |
| 227 |
GET_VM_STATS(vm, v_swappgsin); |
| 228 |
GET_VM_STATS(vm, v_swappgsout); |
| 229 |
GET_VM_STATS(vm, v_swapin); |
| 230 |
GET_VM_STATS(vm, v_swapout); |
| 231 |
} |
| 232 |
#undef GET_VM_STATS |
| 233 |
#else |
| 234 |
{ |
| 235 |
static const int mem_mib[] = { CTL_VM, NETSNMP_VM_STATS }; |
| 236 |
size_t mem_size = sizeof(mem_stats); |
| 237 |
|
| 238 |
sysctl(mem_mib, sizeof(mem_mib) / sizeof(mem_mib[0]), &mem_stats, |
| 239 |
&mem_size, NULL, 0); |
| 240 |
netsnmp_assert(mem_size == sizeof(mem_stats)); |
| 241 |
} |
| 242 |
#endif |
| 243 |
/* |
| 244 |
* Interrupt/Context Switch statistics |
| 245 |
* XXX - Do these really belong here ? |
| 246 |
*/ |
| 247 |
cpu->nInterrupts = mem_stats.NS_VM_INTR; |
| 248 |
cpu->nCtxSwitches = mem_stats.NS_VM_SWTCH; |
| 249 |
cpu->swapIn = mem_stats.NS_VM_SWAPIN; |
| 250 |
cpu->swapOut = mem_stats.NS_VM_SWAPOUT; |
| 251 |
#ifdef NS_VM_PAGEIN |
| 252 |
cpu->pageIn = mem_stats.NS_VM_PAGEIN; |
| 253 |
#endif |
| 254 |
#ifdef NS_VM_PAGEOUT |
| 255 |
cpu->pageOut = mem_stats.NS_VM_PAGEOUT; |
| 256 |
#endif |
| 257 |
|
| 258 |
#ifdef NETSNMP_KERN_MCPU |
| 259 |
#if defined(KERN_CPTIME2) |
| 260 |
mcpu_size = cpu_num*sizeof(cpu_stats); |
| 261 |
mcpu_stats = malloc(mcpu_size); |
| 262 |
#elif defined(__NetBSD__) |
| 263 |
mcpu_size = cpu_num*sizeof(cpu_stats); |
| 264 |
mcpu_stats = malloc(mcpu_size); |
| 265 |
sysctlbyname("kern.cp_time", mcpu_stats, &mcpu_size, NULL, 0); |
| 266 |
#elif defined(freebsd3) |
| 267 |
if (cp_times == -1) { |
| 268 |
int ret = sysctlbyname("kern.cp_times", NULL, &mcpu_size, NULL, 0); |
| 269 |
cp_times = ret == -1 ? 0 : 1; |
| 270 |
} |
| 271 |
if (cp_times) { |
| 272 |
sysctlbyname("kern.cp_times", NULL, &mcpu_size, NULL, 0); |
| 273 |
mcpu_stats = malloc(mcpu_size); |
| 274 |
sysctlbyname("kern.cp_times", mcpu_stats, &mcpu_size, NULL, 0); |
| 275 |
} |
| 276 |
else { |
| 277 |
mcpu_size = sizeof(cpu_stats); |
| 278 |
mcpu_stats = malloc(mcpu_size); |
| 279 |
sysctlbyname("kern.cp_time", mcpu_stats, &mcpu_size, NULL, 0); |
| 280 |
act_cpu = 1; |
| 281 |
} |
| 282 |
#endif |
| 283 |
for ( i = 0; i < act_cpu; i++ ) { |
| 284 |
cpu = netsnmp_cpu_get_byIdx( i, 0 ); |
| 285 |
/* XXX - per-CPU statistics - mcpu_mib[i].??? */ |
| 286 |
#ifdef KERN_CPTIME2 |
| 287 |
mcpu_mib[2] = i; |
| 288 |
sysctl(mcpu_mib, 3, mcpu_stats+i*CPUSTATES, &mcpu_size, NULL, 0); |
| 289 |
#endif |
| 290 |
/* Almost copy & paste of previous cpu stats stuff :) */ |
| 291 |
cpu->user_ticks = mcpu_stats[(i*CPUSTATES)+CP_USER]; |
| 292 |
cpu->nice_ticks = mcpu_stats[(i*CPUSTATES)+CP_NICE]; |
| 293 |
cpu->sys2_ticks = mcpu_stats[(i*CPUSTATES)+CP_SYS]+mcpu_stats[(i*CPUSTATES)+CP_INTR]; |
| 294 |
cpu->kern_ticks = mcpu_stats[(i*CPUSTATES)+CP_SYS]; |
| 295 |
cpu->idle_ticks = mcpu_stats[(i*CPUSTATES)+CP_IDLE]; |
| 296 |
cpu->intrpt_ticks = mcpu_stats[(i*CPUSTATES)+CP_INTR]; |
| 297 |
/* wait_ticks, sirq_ticks unused */ |
| 298 |
|
| 299 |
/* |
| 300 |
* Interrupt/Context Switch statistics |
| 301 |
* XXX - Do these really belong here ? |
| 302 |
*/ |
| 303 |
|
| 304 |
/* There's no real need to execute another sysctl() |
| 305 |
* |
| 306 |
* sysctl(mem_mib, 2, &mem_stats, &mem_size, NULL, 0); |
| 307 |
*/ |
| 308 |
cpu->nInterrupts = mem_stats.NS_VM_INTR; |
| 309 |
cpu->nCtxSwitches = mem_stats.NS_VM_SWTCH; |
| 310 |
cpu->swapIn = mem_stats.NS_VM_SWAPIN; |
| 311 |
cpu->swapOut = mem_stats.NS_VM_SWAPOUT; |
| 312 |
#ifdef NS_VM_PAGEIN |
| 313 |
cpu->pageIn = mem_stats.NS_VM_PAGEIN; |
| 314 |
#endif |
| 315 |
#ifdef NS_VM_PAGEOUT |
| 316 |
cpu->pageOut = mem_stats.NS_VM_PAGEOUT; |
| 317 |
#endif |
| 318 |
} |
| 319 |
free(mcpu_stats); |
| 320 |
#else /* NETSNMP_KERN_MCPU */ |
| 321 |
/* Copy "overall" figures to cpu0 entry */ |
| 322 |
_cpu_copy_stats( cpu ); |
| 323 |
#endif /* NETSNMP_KERN_MCPU */ |
| 324 |
|
| 325 |
return 0; |
| 326 |
} |