summaryrefslogtreecommitdiff
path: root/agent/mibgroup/hardware/cpu/cpu_kerndata.c
blob: 9a9c4a4d6f187385c89a7cbde370799b3d8f4535 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 *   getkerndata() interface
 *     e.g. Dynix
 */
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include <net-snmp/agent/hardware/cpu.h>

#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

#include <sys/sysctl.h>
#include <sys/vmmeter.h>
#include <vm/vm_param.h>
#include <vm/vm_extern.h>


    /*
     * Initialise the list of CPUs on the system
     *   (including descriptions)
     */
void init_cpu_nlist( void ) {
    int                i;
    netsnmp_cpu_info  *cpu = netsnmp_cpu_get_byIdx( -1, 1 );
    strcpy(cpu->name, "Overall CPU statistics");

    cpu_num = tmp_ctl( TMP_NENG, 0 );
    if ( cpu_num <= 0 )
         cpu_num = 1;  /* Single CPU system */
    for ( i=0; i < cpu_num; i++ ) {
        cpu = netsnmp_cpu_get_byIdx( i, 1 );
        sprintf( cpu->name, "cpu%d", i );
    }
}


    /*
     * Load the latest CPU usage statistics
     */
int netsnmp_cpu_arch_load( netsnmp_cache *cache, void *magic ) {
    int               i;
    exp_vmmeter_t    *vminfo;
    size_t            vminfo_size;
    netsnmp_cpu_info *cpu = netsnmp_cpu_get_byIdx( -1, 0 );
    netsnmp_cpu_info *cpu2;

        /* Clear overall stats, ready for summing individual CPUs */
    cpu->user_ticks = 0;
    cpu->idle_ticks = 0;
    cpu->kern_ticks = 0;
    cpu->wait_ticks = 0;
    cpu->sys2_ticks = 0;
    cpu->swapIn       = 0;
    cpu->swapOut      = 0;
    cpu->nInterrupts  = 0;
    cpu->nCtxSwitches = 0;

    vminfo_size = cpu_num * sizeof(exp_vmmeter_t);
    vminfo      = (exp_vmmeter_t *)malloc( vminfo_size );
    getkerninfo(VMMETER_DATAID, vminfo, vminfo_size);

    for ( i=0; i < cpu_num; i++ ) {
        cpu2 = netsnmp_cpu_get_byIdx( i, 0 );

        cpu2->user_ticks = (unsigned long)vminfo[i].v_time[V_CPU_USER];
        cpu2->idle_ticks = (unsigned long)vminfo[i].v_time[V_CPU_IDLE];
        cpu2->kern_ticks = (unsigned long)vminfo[i].v_time[V_CPU_KERNEL];
        cpu2->wait_ticks = (unsigned long)vminfo[i].v_time[V_CPU_STREAM];
        cpu2->sys2_ticks = (unsigned long)vminfo[i].v_time[V_CPU_KERNEL]+
                                          vminfo[i].v_time[V_CPU_STREAM];
            /* nice_ticks, intrpt_ticks, sirq_ticks unused */

            /* sum these for the overall stats */
        cpu->user_ticks += (unsigned long)vminfo[i].v_time[V_CPU_USER];
        cpu->idle_ticks += (unsigned long)vminfo[i].v_time[V_CPU_IDLE];
        cpu->kern_ticks += (unsigned long)vminfo[i].v_time[V_CPU_KERNEL];
        cpu->wait_ticks += (unsigned long)vminfo[i].v_time[V_CPU_STREAM];
        cpu->sys2_ticks += (unsigned long)vminfo[i].v_time[V_CPU_KERNEL]+
                                          vminfo[i].v_time[V_CPU_STREAM];

            /*
             * Interrupt/Context Switch statistics
             *   XXX - Do these really belong here ?
             */
        cpu->swapIn       += (unsigned long)vminfo[i].v_swpin;
        cpu->swapOut      += (unsigned long)vminfo[i].v_swpout;
        cpu->pageIn       += (unsigned long)vminfo[i].v_phread;
        cpu->pageOut      += (unsigned long)vminfo[i].v_phwrite;
        cpu->nInterrupts  += (unsigned long)vminfo[i].v_intr;
        cpu->nCtxSwitches += (unsigned long)vminfo[i].v_swtch;
    }
    return 0;
}