1 /*
2  * Copyright (c) 2005 J. Martin Petersen
3  * Copyright (c) 2012 Willem Dijkstra
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  *    - Redistributions of source code must retain the above copyright
11  *      notice, this list of conditions and the following disclaimer.
12  *    - Redistributions in binary form must reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials provided
15  *      with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  */
31 
32 /*
33  * nr of reads : nr of writes : seeks (zero) : read bytes : written bytes
34  */
35 
36 #include "conf.h"
37 
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <stdlib.h>
41 #include <ctype.h>
42 
43 #include <errno.h>
44 #include <string.h>
45 #include <fcntl.h>
46 
47 #ifdef HAS_RESOURCE_CPUSTATE
48 #include <sys/resource.h>
49 #else
50 #include <sys/dkstat.h>
51 #endif
52 
53 #include <devstat.h>
54 
55 #include "error.h"
56 #include "symon.h"
57 #include "diskname.h"
58 
59 static struct statinfo io_stats;
60 static int io_numdevs = 0;
61 
62 void
privinit_io()63 privinit_io()
64 {
65     /* EMPTY */
66 }
67 
68 void
init_io(struct stream * st)69 init_io(struct stream *st)
70 {
71     struct disknamectx c;
72     unsigned int i;
73     struct devstat *ds;
74     char drivename[MAX_PATH_LEN];
75 
76     if (st->arg == NULL)
77         fatal("io: need a <device>|<devicename> argument");
78 
79     io_stats.dinfo = malloc(sizeof(struct devinfo));
80     if (io_stats.dinfo == NULL) {
81         fatal("io: could not allocate memory");
82     }
83 
84     io_stats.dinfo->mem_ptr = NULL;
85 
86     initdisknamectx(&c, st->arg, drivename, MAX_PATH_LEN);
87 
88     gets_io();
89 
90     while (nextdiskname(&c)) {
91         for (i = 0; i < io_numdevs; i++) {
92             ds = &io_stats.dinfo->devices[i];
93 
94             if (strncmp(ds->device_name, drivename, strlen(ds->device_name)) == 0 &&
95                 strlen(ds->device_name) < strlen(drivename) &&
96                 isdigit(st->arg[strlen(ds->device_name)]) &&
97                 atoi(&drivename[strlen(ds->device_name)]) == ds->unit_number) {
98                 if (strcmp(st->arg, drivename) == 0)
99                     info("started module io(%.200s)", st->arg);
100                 else
101                     info("started module io(%.200s = %.200s)", st->arg, drivename);
102                 return;
103             }
104         }
105     }
106 
107     warning("io(%.200s): not found", st->arg);
108 }
109 
110 void
gets_io()111 gets_io()
112 {
113 #if DEVSTAT_USER_API_VER >= 5
114     io_numdevs = devstat_getnumdevs(NULL);
115 #else
116     io_numdevs = getnumdevs();
117 #endif
118 
119     if (io_numdevs == -1) {
120         fatal("io: numdevs error");
121     }
122 
123     if (io_stats.dinfo->mem_ptr != NULL) {
124         free(io_stats.dinfo->mem_ptr);
125     }
126 
127     /* clear the devinfo struct, as getdevs expects it to be all zeroes */
128     bzero(io_stats.dinfo, sizeof(struct devinfo));
129 
130 #if DEVSTAT_USER_API_VER >= 5
131     devstat_getdevs(NULL, &io_stats);
132 #else
133     getdevs(&io_stats);
134 #endif
135 }
136 
137 int
get_io(char * symon_buf,int maxlen,struct stream * st)138 get_io(char *symon_buf, int maxlen, struct stream *st)
139 {
140     unsigned int i;
141     struct devstat *ds;
142 
143     for (i = 0; i < io_numdevs; i++) {
144         ds = &io_stats.dinfo->devices[i];
145 
146         if (strncmp(ds->device_name, st->arg, strlen(ds->device_name)) == 0 &&
147             strlen(ds->device_name) < strlen(st->arg) &&
148             isdigit(st->arg[strlen(ds->device_name)]) &&
149             atoi(&st->arg[strlen(ds->device_name)]) == ds->unit_number) {
150 #if DEVSTAT_USER_API_VER >= 5
151             return snpack(symon_buf, maxlen, st->arg, MT_IO2,
152                           ds->operations[DEVSTAT_READ],
153                           ds->operations[DEVSTAT_WRITE],
154                           (uint64_t) 0, /* don't know how to find #seeks */
155                           ds->bytes[DEVSTAT_READ],
156                           ds->bytes[DEVSTAT_WRITE]);
157 
158 #else
159             return snpack(symon_buf, maxlen, st->arg, MT_IO2,
160                           ds->num_reads,
161                           ds->num_writes,
162                           (uint64_t) 0, /* don't know how to find #seeks */
163                           ds->bytes_read,
164                           ds->bytes_written);
165 #endif
166         }
167     }
168 
169     return 0;
170 }
171