~ubuntu-branches/ubuntu/trusty/net-snmp/trusty

« back to all changes in this revision

Viewing changes to agent/mibgroup/hardware/cpu/cpu_sysctl.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2007-12-08 14:59:50 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20071208145950-u1tykhpw56nyzqik
Tags: 5.4.1~dfsg-4ubuntu1
* Merge from debian unstable.
* Remaining Ubuntu changes:
  - Remove stop links from rc0 and rc6
  - Munge Maintainer field as per spec.
* Ubuntu changes dropped:
  - Symlink common files between the packages, CDBS ought to handle that
    for us automatically.
* The latest Debian changes has dropped history from the changelog. Slot in
  the Ubuntu changes as best I can. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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-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/sched.h>
 
15
#include <sys/param.h>
 
16
#include <sys/sysctl.h>
 
17
#include <sys/vmmeter.h>
 
18
#ifdef HAVE_VM_VM_PARAM_H
 
19
#include <vm/vm_param.h>
 
20
#endif
 
21
#ifdef HAVE_VM_VM_EXTERN_H
 
22
#include <vm/vm_extern.h>
 
23
#endif
 
24
 
 
25
void _cpu_copy_stats( netsnmp_cpu_info *cpu );
 
26
 
 
27
    /*
 
28
     * Initialise the list of CPUs on the system
 
29
     *   (including descriptions)
 
30
     */
 
31
void init_cpu_sysctl( void ) {
 
32
    int               i, n;
 
33
    int               ncpu_mib[]  = { CTL_HW, HW_NCPU };
 
34
    int               model_mib[] = { CTL_HW, HW_MODEL };
 
35
    char              descr[ SNMP_MAXBUF ];
 
36
    netsnmp_cpu_info  *cpu = netsnmp_cpu_get_byIdx( -1, 1 );
 
37
    strcpy(cpu->name, "Overall CPU statistics");
 
38
 
 
39
    i = sizeof(n);
 
40
    sysctl(ncpu_mib, 2, &n, &i, NULL, 0);
 
41
    if ( n <= 0 )
 
42
        n = 1;   /* Single CPU system */
 
43
    i = sizeof(descr);
 
44
    sysctl(model_mib, 2, descr, &i, NULL, 0);
 
45
    for ( i = 0; i < n; i++ ) {
 
46
        cpu = netsnmp_cpu_get_byIdx( i, 1 );
 
47
        cpu->status = 2;  /* running */
 
48
        sprintf( cpu->name,  "cpu%d", i );
 
49
        sprintf( cpu->descr, "%s", descr );
 
50
    }
 
51
    cpu_num = n;
 
52
}
 
53
 
 
54
 
 
55
#define NETSNMP_CPU_STATS long
 
56
#if defined(KERN_CPUSTATS)                /* BSDi */
 
57
#define NETSNMP_KERN_CPU  KERN_CPUSTATS
 
58
#elif defined(KERN_CPTIME)                /* OpenBSD */
 
59
#define NETSNMP_KERN_CPU  KERN_CPTIME
 
60
#elif defined(KERN_CP_TIME)               /* NetBSD */
 
61
#define NETSNMP_KERN_CPU  KERN_CP_TIME
 
62
 
 
63
#if defined(netbsdelf3)
 
64
#undef  NETSNMP_CPU_STATS 
 
65
#define NETSNMP_CPU_STATS uint64_t
 
66
#endif
 
67
#else
 
68
#error "No CPU statistics sysctl token"
 
69
#endif
 
70
 
 
71
/*
 
72
   Need to check details before enabling this!
 
73
#if defined(KERN_MPCPUSTATS)
 
74
#define NETSNMP_KERN_MCPU  KERN_MPCPUSTATS
 
75
#define NETSNMP_KERN_MCPU_TYPE  struct mpcpustats
 
76
#elif defined(KERN_MP_CPUSTATS)
 
77
#define NETSNMP_KERN_MCPU  KERN_MP_CPUSTATS
 
78
#define NETSNMP_KERN_MCPU_TYPE  struct cpustats
 
79
#endif
 
80
 */
 
81
 
 
82
#if defined(VM_UVMEXP2) || defined(VM_UVMEXP) 
 
83
    #define NS_VM_INTR          intrs
 
84
    #define NS_VM_SWTCH         swtch
 
85
    #define NS_VM_PAGEIN        pageins
 
86
    #define NS_VM_PAGEOUT       pdpageouts
 
87
    #define NS_VM_SWAPIN        swapins
 
88
    #define NS_VM_SWAPOUT       swapouts
 
89
 
 
90
#if defined(VM_UVMEXP2)                   /* NetBSD 1.6+ */
 
91
#define NETSNMP_VM_STATS       VM_UVMEXP2
 
92
#define NETSNMP_VM_STATS_TYPE  struct uvmexp_sysctl
 
93
#else /* VM_UVMEXP */                     /* OpenBSD 3+, NetBSD 1.6+ */
 
94
#define NETSNMP_VM_STATS       VM_UVMEXP
 
95
#define NETSNMP_VM_STATS_TYPE  struct uvmexp
 
96
#endif  /* VM_UVMEXP2 || VM_UVMEXP */
 
97
 
 
98
#elif defined(VM_METER)                   /* OpenBSD, NetBSD, FreeBSD */
 
99
#define NETSNMP_VM_STATS       VM_METER
 
100
#define NETSNMP_VM_STATS_TYPE  struct vmtotal
 
101
 
 
102
#elif defined(VM_CNT)                     /* BSDi */
 
103
#define NETSNMP_VM_STATS       VM_CNT
 
104
#define NETSNMP_VM_STATS_TYPE  struct vmmeter
 
105
    #define NS_VM_INTR          v_intr
 
106
    #define NS_VM_SWTCH         v_swtch
 
107
    #undef  NS_VM_PAGEIN
 
108
    #undef  NS_VM_PAGEOUT
 
109
    #define NS_VM_SWAPIN        v_swpin
 
110
    #define NS_VM_SWAPOUT       v_swpout
 
111
#endif
 
112
 
 
113
 
 
114
    /*
 
115
     * Load the latest CPU usage statistics
 
116
     */
 
117
int netsnmp_cpu_arch_load( netsnmp_cache *cache, void *magic ) {
 
118
    int                     i;
 
119
 
 
120
    /*
 
121
     * Strictly speaking, BSDi ought to use
 
122
     *    "struct cpustats  cpu_stats"
 
123
     * but this array was used in the previous code, and
 
124
     *   is correct for the {Open,Net}BSD versions too.
 
125
     * Don't fight it, Dave - go with the flow....
 
126
     */
 
127
    NETSNMP_CPU_STATS cpu_stats[CPUSTATES];
 
128
    int            cpu_mib[] = { CTL_KERN, NETSNMP_KERN_CPU };
 
129
    int            cpu_size  = sizeof(cpu_stats);
 
130
#ifdef NETSNMP_KERN_MCPU 
 
131
    NETSNMP_KERN_MCPU_TYPE *mcpu_stats;
 
132
    int            mcpu_mib[] = { CTL_KERN, NETSNMP_KERN_MCPU };
 
133
    int            mcpu_size  = sizeof(NETSNMP_KERN_MCPU_TYPE);
 
134
#endif
 
135
    NETSNMP_VM_STATS_TYPE mem_stats;
 
136
    int            mem_mib[] = { CTL_VM, NETSNMP_VM_STATS };
 
137
    int            mem_size  = sizeof(NETSNMP_VM_STATS_TYPE);
 
138
    netsnmp_cpu_info *cpu = netsnmp_cpu_get_byIdx( -1, 0 );
 
139
 
 
140
    sysctl(cpu_mib, 2,  cpu_stats, &cpu_size, NULL, 0);
 
141
    cpu->user_ticks = (unsigned long)cpu_stats[CP_USER];
 
142
    cpu->nice_ticks = (unsigned long)cpu_stats[CP_NICE];
 
143
    cpu->sys2_ticks = (unsigned long)cpu_stats[CP_SYS]+cpu_stats[CP_INTR];
 
144
    cpu->kern_ticks = (unsigned long)cpu_stats[CP_SYS];
 
145
    cpu->idle_ticks = (unsigned long)cpu_stats[CP_IDLE];
 
146
    cpu->intrpt_ticks = (unsigned long)cpu_stats[CP_INTR];
 
147
        /* wait_ticks, sirq_ticks unused */
 
148
    
 
149
        /*
 
150
         * Interrupt/Context Switch statistics
 
151
         *   XXX - Do these really belong here ?
 
152
         */
 
153
    sysctl(mem_mib, 2, &mem_stats, &mem_size, NULL, 0);
 
154
    cpu->nInterrupts  = (unsigned long)mem_stats.NS_VM_INTR;
 
155
    cpu->nCtxSwitches = (unsigned long)mem_stats.NS_VM_SWTCH;
 
156
    cpu->swapIn       = (unsigned long)mem_stats.NS_VM_SWAPIN;
 
157
    cpu->swapOut      = (unsigned long)mem_stats.NS_VM_SWAPOUT;
 
158
#ifdef NS_VM_PAGEIN
 
159
    cpu->pageIn       = (unsigned long)mem_stats.NS_VM_PAGEIN;
 
160
#endif
 
161
#ifdef NS_VM_PAGEOUT
 
162
    cpu->pageOut      = (unsigned long)mem_stats.NS_VM_PAGEOUT;
 
163
#endif
 
164
 
 
165
#ifdef NETSNMP_KERN_MCPU
 
166
    mcpu_stats = malloc(cpu_num*sizeof(NETSNMP_KERN_MCPU_TYPE));
 
167
    sysctl(mcpu_mib, 2, mcpu_stats,
 
168
           cpu_num*sizeof(NETSNMP_KERN_MCPU_TYPE), NULL, 0);
 
169
    for ( i = 0; i < cpu_num; i++ ) {
 
170
        cpu = netsnmp_cpu_get_byIdx( i, 0 );
 
171
        /* XXX - per-CPU statistics - mcpu_mib[i].??? */
 
172
    }
 
173
#else
 
174
        /* Copy "overall" figures to cpu0 entry */
 
175
    _cpu_copy_stats( cpu );
 
176
#endif
 
177
 
 
178
    return 0;
 
179
}