summaryrefslogtreecommitdiff
path: root/src/pmdas/netbsd/disk.c
blob: b188ae46c37a10841dbd011a478a3a37fdcb8e01 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
 * NetBSD Kernel PMDA - disk metrics
 *
 * Copyright (c) 2012,2013 Ken McDonell.  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.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "pmapi.h"
#include "impl.h"
#include "pmda.h"
#include "netbsd.h"

#if 0
#include <devstat.h>
struct devinfo	devinfo = { 0 };
struct statinfo	statinfo;
#endif

void
refresh_disk_metrics(void)
{
#if 0
    static int	init_done = 0;
    int		i;
    int		sts;

    if (!init_done) {
	sts = devstat_checkversion(NULL);
	if (sts != 0) {
	    fprintf(stderr, "refresh_disk_metrics: devstat_checkversion: failed! %s\n", devstat_errbuf);
	    exit(1);
	}
	statinfo.dinfo = &devinfo;
	init_done = 1;
    }

    sts = devstat_getdevs(NULL, &statinfo);
    if (sts < 0) {
	fprintf(stderr, "refresh_disk_metrics: devstat_getdevs: %s\n", strerror(errno));
	exit(1);
    }
    else if (sts == 1) {
	/*
	 * First call, else devstat[] list has changed
	 */
	struct devstat	*dsp;
	char		iname[DEVSTAT_NAME_LEN+6];
	pmdaCacheOp(indomtab[DISK_INDOM].it_indom, PMDA_CACHE_INACTIVE);
	for (i = 0; i < devinfo.numdevs; i++) {
	    dsp = &devinfo.devices[i];
	    /*
	     * Skip entries that are not interesting ... only include
	     * "da" (direct access) disks at this stage
	     */
	    if (strcmp(dsp->device_name, "da") != 0)
		continue;
	    snprintf(iname, sizeof(iname), "%s%d", dsp->device_name, dsp->unit_number);
	    sts = pmdaCacheLookupName(indomtab[DISK_INDOM].it_indom, iname, NULL, NULL);
	    if (sts == PMDA_CACHE_ACTIVE) {
		int	j;
		fprintf(stderr, "refresh_disk_metrics: Warning: duplicate name (%s) in disk indom\n", iname);
		for (j = 0; j < devinfo.numdevs; j++) {
		    dsp = &devinfo.devices[j];
		    fprintf(stderr, "  devinfo[%d]: %s%d\n", j, dsp->device_name, dsp->unit_number);
		}
		continue;
	    }
	    else {
		/* new entry or reactivate an existing one */
		pmdaCacheStore(indomtab[DISK_INDOM].it_indom, PMDA_CACHE_ADD, iname, (void *)dsp);
	    }
	}
    }
#endif

}

int
do_disk_metrics(pmdaMetric *mdesc, unsigned int inst, pmAtomValue *atom)
{
#if 0
    struct devstat	*dsp;
    int			sts;

    if (inst != PM_IN_NULL) {
	/*
	 * per-disk metrics
	 */
	sts = pmdaCacheLookup(indomtab[DISK_INDOM].it_indom, inst, NULL, (void **)&dsp);
	if (sts == PMDA_CACHE_ACTIVE) {
	    sts = 1;
	    /* cluster and domain already checked, just need item ... */
	    switch (pmid_item(mdesc->m_desc.pmid)) {
		case 0:		/* disk.dev.read */
		    atom->ull = dsp->operations[DEVSTAT_READ];
		    break;

		case 1:		/* disk.dev.write */
		    atom->ull = dsp->operations[DEVSTAT_WRITE];
		    break;

		case 2:		/* disk.dev.total */
		    atom->ull = dsp->operations[DEVSTAT_READ] + dsp->operations[DEVSTAT_WRITE];
		    break;

		case 3:		/* disk.dev.read_bytes */
		    atom->ull = dsp->bytes[DEVSTAT_READ];
		    break;

		case 4:		/* disk.dev.write_bytes */
		    atom->ull = dsp->bytes[DEVSTAT_WRITE];
		    break;

		case 5:		/* disk.dev.total_bytes */
		    atom->ull = dsp->bytes[DEVSTAT_READ] + dsp->bytes[DEVSTAT_WRITE];
		    break;

		case 12:	/* disk.dev.blkread */
		    atom->ull = dsp->block_size == 0 ? 0 : dsp->bytes[DEVSTAT_READ] / dsp->block_size;
		    break;

		case 13:	/* disk.dev.blkwrite */
		    atom->ull = dsp->block_size == 0 ? 0 : dsp->bytes[DEVSTAT_WRITE] / dsp->block_size;
		    break;

		case 14:	/* disk.dev.blktotal */
		    atom->ull = dsp->block_size == 0 ? 0 : (dsp->bytes[DEVSTAT_READ] + dsp->bytes[DEVSTAT_WRITE]) / dsp->block_size;
		    break;

		default:
		    sts = PM_ERR_PMID;
		    break;
	    }
	}
	else
	    sts = 0;
    }
    else {
	/*
	 * all-disk summary metrics
	 */
	int	i;
	atom->ull = 0;
	sts = 1;
	pmdaCacheOp(indomtab[DISK_INDOM].it_indom, PMDA_CACHE_WALK_REWIND);
	while (sts == 1 && (i = pmdaCacheOp(indomtab[DISK_INDOM].it_indom, PMDA_CACHE_WALK_NEXT)) >= 0) {
	    int		lsts;
	    lsts = pmdaCacheLookup(indomtab[DISK_INDOM].it_indom, i, NULL, (void **)&dsp);
	    if (lsts == PMDA_CACHE_ACTIVE) {
		/* cluster and domain already checked, just need item ... */
		switch (pmid_item(mdesc->m_desc.pmid)) {
		    case 6:		/* disk.all.read */
			atom->ull += dsp->operations[DEVSTAT_READ];
			break;

		    case 7:		/* disk.all.write */
			atom->ull += dsp->operations[DEVSTAT_WRITE];
			break;

		    case 8:		/* disk.all.total */
			atom->ull += dsp->operations[DEVSTAT_READ] + dsp->operations[DEVSTAT_WRITE];
			break;

		    case 9:		/* disk.all.read_bytes */
			atom->ull += dsp->bytes[DEVSTAT_READ];
			break;

		    case 10:		/* disk.all.write_bytes */
			atom->ull += dsp->bytes[DEVSTAT_WRITE];
			break;

		    case 11:		/* disk.all.total_bytes */
			atom->ull += dsp->bytes[DEVSTAT_READ] + dsp->bytes[DEVSTAT_WRITE];
			break;

		    case 15:		/* disk.all.blkread */
			atom->ull += dsp->block_size == 0 ? 0 : dsp->bytes[DEVSTAT_READ] / dsp->block_size;
			break;

		    case 16:		/* disk.all.blkwrite */
			atom->ull += dsp->block_size == 0 ? 0 : dsp->bytes[DEVSTAT_WRITE] / dsp->block_size;
			break;

		    case 17:		/* disk.all.blktotal */
			atom->ull += dsp->block_size == 0 ? 0 : (dsp->bytes[DEVSTAT_READ] + dsp->bytes[DEVSTAT_WRITE]) / dsp->block_size;
			break;

		    default:
			sts = PM_ERR_PMID;
			break;
		}
	    }
	}
	if (i < 0 && i != -1)
	    /* not end of indom from cache walk, some other error */
	    sts = i;
    }

    return sts;
#else
    return PM_ERR_PMID;
#endif
}