summaryrefslogtreecommitdiff
path: root/src/pmdas/darwin/kernel.c
blob: 57921c900b8088ffa77c2b91e5bf0a13c0a2c72e (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * Copyright (c) 2004 Silicon Graphics, Inc.  All Rights Reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */
#include <sys/time.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/utsname.h>
#include <sys/sysctl.h>
#include <sys/mount.h>
#include <mach/mach.h>
#include "pmapi.h"
#include "impl.h"
#include "pmda.h"

extern mach_port_t	mach_host;
extern int		mach_hertz;

int
refresh_vmstat(struct vm_statistics *vmstat)
{
    int error, info = HOST_VM_INFO;
    natural_t count = HOST_VM_INFO_COUNT;

    error = host_statistics(mach_host, info, (host_info_t)vmstat, &count);
    return (error != KERN_SUCCESS) ? -oserror() : 0;
}

int
refresh_cpuload(struct host_cpu_load_info *cpuload)
{
    int error, info = HOST_CPU_LOAD_INFO;
    natural_t count = HOST_CPU_LOAD_INFO_COUNT;

    error = host_statistics(mach_host, info, (host_info_t)cpuload, &count);
    return (error != KERN_SUCCESS) ? -oserror() : 0;
}

int
refresh_uname(struct utsname *utsname)
{
    return (uname(utsname) == -1) ? -oserror() : 0;
}

int
refresh_hertz(unsigned int *hertz)
{
    int			mib[2] = { CTL_KERN, KERN_CLOCKRATE };
    size_t		size = sizeof(struct clockinfo);
    struct clockinfo	clockrate;

    if (sysctl(mib, 2, &clockrate, &size, NULL, 0) == -1)
	return -oserror();
    *hertz = clockrate.hz;
    return 0;
}

int
refresh_loadavg(float *loadavg)
{
    int			mib[2] = { CTL_VM, VM_LOADAVG };
    size_t		size = sizeof(struct loadavg);
    struct loadavg	loadavgs;

    if (sysctl(mib, 2, &loadavgs, &size, NULL, 0) == -1)
	return -oserror();
    loadavg[0] = (float)loadavgs.ldavg[0] / (float)loadavgs.fscale;
    loadavg[1] = (float)loadavgs.ldavg[1] / (float)loadavgs.fscale;
    loadavg[2] = (float)loadavgs.ldavg[2] / (float)loadavgs.fscale;
    return 0;
}

int
refresh_uptime(unsigned int *uptime)
{
    static struct timeval	boottime;
    struct timeval		timediff;

    if (!boottime.tv_sec) {
	int	mib[2] = { CTL_KERN, KERN_BOOTTIME };
	size_t	size = sizeof(struct timeval);

	if (sysctl(mib, 2, &boottime, &size, NULL, 0) == -1)
	    return -oserror();
    }

    __pmtimevalNow(&timediff);
    timediff.tv_usec -= boottime.tv_usec;
    if (timediff.tv_usec < 0) {
	timediff.tv_usec += 1000000;
	timediff.tv_sec--;
    }
    timediff.tv_sec -= boottime.tv_sec;

    *uptime = timediff.tv_sec;
    return 0;
}

int
refresh_cpus(struct processor_cpu_load_info **cpuload, pmdaIndom *indom)
{
    natural_t ncpu, icount;
    processor_info_array_t iarray;
    struct processor_cpu_load_info *cpuinfo;
    int error, i, info = PROCESSOR_CPU_LOAD_INFO;

    error = host_processor_info(mach_host, info, &ncpu, &iarray, &icount);
    if (error != KERN_SUCCESS)
	return -oserror();

    cpuinfo = (struct processor_cpu_load_info *)iarray;
    if (ncpu != indom->it_numinst) {
	char	name[16];	/* 8 is real max atm, but be conservative */

	error = -ENOMEM;
	i = sizeof(unsigned long) * CPU_STATE_MAX * ncpu;
	if ((*cpuload = realloc(*cpuload, i)) == NULL)
	    goto vmdealloc;

	i = sizeof(pmdaInstid) * ncpu;
	if ((indom->it_set = realloc(indom->it_set, i)) == NULL) {
	    free(*cpuload);
	    *cpuload = NULL;
	    indom->it_numinst = 0;
	    goto vmdealloc;
	}

	for (i = 0; i < ncpu; i++) {
	    snprintf(name, sizeof(name), "cpu%d", i);
	    indom->it_set[i].i_name = strdup(name);
	    indom->it_set[i].i_inst = i;
	}
	indom->it_numinst = ncpu;
    }

    error = 0;
    for (i = 0; i < ncpu; i++)
	memcpy(&(*cpuload)[i], &cpuinfo[i],
		sizeof(unsigned long) * CPU_STATE_MAX);

vmdealloc:
    vm_deallocate(mach_host, (vm_address_t)iarray, icount);
    return error;
}

int
refresh_filesys(struct statfs **filesys, pmdaIndom *indom)
{
    int	i, count = getmntinfo(filesys, MNT_NOWAIT);

    if (count < 0) {
	indom->it_numinst = 0;
	indom->it_set = NULL;
	return -oserror();
    }
    if (count > 0 && count != indom->it_numinst) {
	i = sizeof(pmdaInstid) * count;
	if ((indom->it_set = realloc(indom->it_set, i)) == NULL) {
	    indom->it_numinst = 0;
	    return -ENOMEM;
	}
    }
    for (i = 0; i < count; i++) {
	indom->it_set[i].i_name = (*filesys)[i].f_mntfromname;
	indom->it_set[i].i_inst = i;
    }
    indom->it_numinst = count;
    return 0;
}

#if 0
int
refresh_hinv()
{
sysctl...
hw.machine = Power Macintosh
hw.model = PowerMac4,2
hw.busfrequency = 99837332
hw.cpufrequency = 700000000
hw.cachelinesize = 32
hw.l1icachesize = 32768
hw.l1dcachesize = 32768
hw.l2settings = 2147483648
hw.l2cachesize = 262144
}
#endif