Debian Codegraph @

agent/mibgroup/hardware/cpu/cpu_kerndata.c

raw
1 /*
2 * getkerndata() interface
3 * e.g. Dynix
4 */
5 #include <net-snmp/net-snmp-config.h>
6 #include <net-snmp/net-snmp-includes.h>
7 #include <net-snmp/agent/net-snmp-agent-includes.h>
8 #include <net-snmp/agent/hardware/cpu.h>
9
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <sys/types.h>
13
14 #include <sys/sysctl.h>
15 #include <sys/vmmeter.h>
16 #include <vm/vm_param.h>
17 #include <vm/vm_extern.h>
18
19
20 /*
21 * Initialise the list of CPUs on the system
22 * (including descriptions)
23 */
24 void init_cpu_nlist( void ) {
25 int i;
26 netsnmp_cpu_info *cpu = netsnmp_cpu_get_byIdx( -1, 1 );
27 strcpy(cpu->name, "Overall CPU statistics");
28
29 cpu_num = tmp_ctl( TMP_NENG, 0 );
30 if ( cpu_num <= 0 )
31 cpu_num = 1; /* Single CPU system */
32 for ( i=0; i < cpu_num; i++ ) {
33 cpu = netsnmp_cpu_get_byIdx( i, 1 );
34 sprintf( cpu->name, "cpu%d", i );
35 }
36 }
37
38
39 /*
40 * Load the latest CPU usage statistics
41 */
42 int netsnmp_cpu_arch_load( netsnmp_cache *cache, void *magic ) {
43 int i;
44 exp_vmmeter_t *vminfo;
45 size_t vminfo_size;
46 netsnmp_cpu_info *cpu = netsnmp_cpu_get_byIdx( -1, 0 );
47 netsnmp_cpu_info *cpu2;
48
49 /* Clear overall stats, ready for summing individual CPUs */
50 cpu->user_ticks = 0;
51 cpu->idle_ticks = 0;
52 cpu->kern_ticks = 0;
53 cpu->wait_ticks = 0;
54 cpu->sys2_ticks = 0;
55 cpu->swapIn = 0;
56 cpu->swapOut = 0;
57 cpu->nInterrupts = 0;
58 cpu->nCtxSwitches = 0;
59
60 vminfo_size = cpu_num * sizeof(exp_vmmeter_t);
61 vminfo = (exp_vmmeter_t *)malloc( vminfo_size );
62 getkerninfo(VMMETER_DATAID, vminfo, vminfo_size);
63
64 for ( i=0; i < cpu_num; i++ ) {
65 cpu2 = netsnmp_cpu_get_byIdx( i, 0 );
66
67 cpu2->user_ticks = (unsigned long long)vminfo[i].v_time[V_CPU_USER];
68 cpu2->idle_ticks = (unsigned long long)vminfo[i].v_time[V_CPU_IDLE];
69 cpu2->kern_ticks = (unsigned long long)vminfo[i].v_time[V_CPU_KERNEL];
70 cpu2->wait_ticks = (unsigned long long)vminfo[i].v_time[V_CPU_STREAM];
71 cpu2->sys2_ticks = (unsigned long long)vminfo[i].v_time[V_CPU_KERNEL]+
72 vminfo[i].v_time[V_CPU_STREAM];
73 /* nice_ticks, intrpt_ticks, sirq_ticks unused */
74
75 /* sum these for the overall stats */
76 cpu->user_ticks += (unsigned long long)vminfo[i].v_time[V_CPU_USER];
77 cpu->idle_ticks += (unsigned long long)vminfo[i].v_time[V_CPU_IDLE];
78 cpu->kern_ticks += (unsigned long long)vminfo[i].v_time[V_CPU_KERNEL];
79 cpu->wait_ticks += (unsigned long long)vminfo[i].v_time[V_CPU_STREAM];
80 cpu->sys2_ticks += (unsigned long long)vminfo[i].v_time[V_CPU_KERNEL]+
81 vminfo[i].v_time[V_CPU_STREAM];
82
83 /*
84 * Interrupt/Context Switch statistics
85 * XXX - Do these really belong here ?
86 */
87 cpu->swapIn += (unsigned long long)vminfo[i].v_swpin;
88 cpu->swapOut += (unsigned long long)vminfo[i].v_swpout;
89 cpu->pageIn += (unsigned long long)vminfo[i].v_phread;
90 cpu->pageOut += (unsigned long long)vminfo[i].v_phwrite;
91 cpu->nInterrupts += (unsigned long long)vminfo[i].v_intr;
92 cpu->nCtxSwitches += (unsigned long long)vminfo[i].v_swtch;
93 }
94 return 0;
95 }