| 1 |
/* |
| 2 |
* perfstat() interface |
| 3 |
* e.g. AIX |
| 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 |
#include <sys/protosw.h> |
| 16 |
#include <libperfstat.h> |
| 17 |
|
| 18 |
netsnmp_feature_require(hardware_cpu_copy_stats); |
| 19 |
void _cpu_copy_stats( netsnmp_cpu_info *cpu ); |
| 20 |
|
| 21 |
/* |
| 22 |
* Initialise the list of CPUs on the system |
| 23 |
* (including descriptions) |
| 24 |
*/ |
| 25 |
void init_cpu_perfstat( void ) { |
| 26 |
int i; |
| 27 |
perfstat_id_t name; |
| 28 |
perfstat_cpu_t *cs2; |
| 29 |
netsnmp_cpu_info *cpu = netsnmp_cpu_get_byIdx( -1, 1 ); |
| 30 |
strcpy(cpu->name, "Overall CPU statistics"); |
| 31 |
|
| 32 |
cpu_num = perfstat_cpu( NULL, NULL, sizeof(perfstat_cpu_t), 0 ); |
| 33 |
cs2 = (perfstat_cpu_t*)malloc( cpu_num*sizeof(perfstat_cpu_t)); |
| 34 |
|
| 35 |
strcpy( name.name, ""); |
| 36 |
if (perfstat_cpu(&name, cs2, sizeof(perfstat_cpu_t), cpu_num) > 0) { |
| 37 |
for ( i = 0; i < cpu_num; i++ ) { |
| 38 |
cpu = netsnmp_cpu_get_byIdx( i, 1 ); |
| 39 |
sprintf( cpu->name, cs2[i].name); |
| 40 |
} |
| 41 |
} |
| 42 |
free(cs2); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
/* |
| 47 |
* Load the latest CPU usage statistics |
| 48 |
*/ |
| 49 |
int netsnmp_cpu_arch_load( netsnmp_cache *cache, void *magic ) { |
| 50 |
int i,n; |
| 51 |
perfstat_id_t name; |
| 52 |
perfstat_cpu_total_t cs; |
| 53 |
perfstat_cpu_t *cs2; |
| 54 |
perfstat_memory_total_t ms; |
| 55 |
netsnmp_cpu_info *cpu = netsnmp_cpu_get_byIdx( -1, 0 ); |
| 56 |
|
| 57 |
if (perfstat_cpu_total((perfstat_id_t *)NULL, &cs, |
| 58 |
sizeof(perfstat_cpu_total_t), 1) > 0) { |
| 59 |
|
| 60 |
/* Returns 'u_longlong_t' statistics */ |
| 61 |
cpu->user_ticks = (unsigned long long)cs.user / cs.ncpus; |
| 62 |
cpu->sys_ticks = ((unsigned long long)cs.sys + (unsigned long long)cs.wait) / cs.ncpus; |
| 63 |
cpu->kern_ticks = (unsigned long long)cs.sys / cs.ncpus; |
| 64 |
cpu->wait_ticks = (unsigned long long)cs.wait / cs.ncpus; |
| 65 |
cpu->idle_ticks = (unsigned long long)cs.idle / cs.ncpus; |
| 66 |
/* intrpt_ticks, sirq_ticks, nice_ticks unused */ |
| 67 |
|
| 68 |
/* |
| 69 |
* Interrupt/Context Switch statistics |
| 70 |
* XXX - Do these really belong here ? |
| 71 |
*/ |
| 72 |
cpu->pageIn = (unsigned long long)cs.sysread; |
| 73 |
cpu->pageOut = (unsigned long long)cs.syswrite; |
| 74 |
cpu->nInterrupts = (unsigned long long)cs.devintrs + cs.softintrs; |
| 75 |
cpu->nCtxSwitches = (unsigned long long)cs.pswitch; |
| 76 |
} |
| 77 |
|
| 78 |
if (perfstat_memory_total((perfstat_id_t *)NULL, &ms, |
| 79 |
sizeof(perfstat_memory_total_t), 1) > 0) { |
| 80 |
cpu->swapIn = (unsigned long long)ms.pgspins; |
| 81 |
cpu->swapOut = (unsigned long long)ms.pgspouts; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
/* |
| 86 |
* Per-CPU statistics |
| 87 |
*/ |
| 88 |
n = cs.ncpus; /* XXX - Compare against cpu_num */ |
| 89 |
cs2 = (perfstat_cpu_t*)malloc( n*sizeof(perfstat_cpu_t)); |
| 90 |
strcpy( name.name, ""); |
| 91 |
if (perfstat_cpu(&name, cs2, sizeof(perfstat_cpu_t), n) > 0) { |
| 92 |
for ( i = 0; i < n; i++ ) { |
| 93 |
cpu = netsnmp_cpu_get_byIdx( i, 1 ); |
| 94 |
cpu->user_ticks = (unsigned long long)cs2[i].user; |
| 95 |
cpu->sys_ticks = (unsigned long long)cs2[i].sys + (unsigned long long)cs2[i].wait; |
| 96 |
cpu->kern_ticks = (unsigned long long)cs2[i].sys; |
| 97 |
cpu->wait_ticks = (unsigned long long)cs2[i].wait; |
| 98 |
cpu->idle_ticks = (unsigned long long)cs2[i].idle; |
| 99 |
cpu->pageIn = (unsigned long long)cs2[i].sysread; |
| 100 |
cpu->pageOut = (unsigned long long)cs2[i].syswrite; |
| 101 |
cpu->nCtxSwitches = (unsigned long long)cs2[i].pswitch; |
| 102 |
/* Interrupt stats only apply overall, not per-CPU */ |
| 103 |
} |
| 104 |
} else { |
| 105 |
_cpu_copy_stats( cpu ); |
| 106 |
} |
| 107 |
free(cs2); |
| 108 |
|
| 109 |
return 0; |
| 110 |
} |