Deleted Added
full compact
geom_stats.c (110545) geom_stats.c (110603)
1/*-
2 * Copyright (c) 2003 Poul-Henning Kamp
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The names of the authors may not be used to endorse or promote
14 * products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
1/*-
2 * Copyright (c) 2003 Poul-Henning Kamp
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The names of the authors may not be used to endorse or promote
14 * products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: head/lib/libgeom/geom_stats.c 110545 2003-02-08 15:15:56Z phk $
29 * $FreeBSD: head/lib/libgeom/geom_stats.c 110603 2003-02-10 00:11:43Z phk $
30 */
31
32#include <paths.h>
33#include <errno.h>
34#include <fcntl.h>
35#include <stdlib.h>
36#include <string.h>
37#include <unistd.h>
38#include <libgeom.h>
39
40#include <sys/mman.h>
41#include <sys/time.h>
42#include <sys/types.h>
43
44#include <geom/geom_stats.h>
30 */
31
32#include <paths.h>
33#include <errno.h>
34#include <fcntl.h>
35#include <stdlib.h>
36#include <string.h>
37#include <unistd.h>
38#include <libgeom.h>
39
40#include <sys/mman.h>
41#include <sys/time.h>
42#include <sys/types.h>
43
44#include <geom/geom_stats.h>
45#if 0
46#include <stdio.h>
47#include <stdint.h>
48#include <err.h>
49#endif
50
51/************************************************************/
52static uint npages, pagesize, spp;
53static int statsfd = -1;
54static u_char *statp;
55
56void
57geom_stats_close(void)
58{
59 if (statsfd == -1)
60 return;
61 munmap(statp, npages *pagesize);
62 statp = NULL;
63 close (statsfd);
64 statsfd = -1;
65}
66
67void
68geom_stats_resync(void)
69{
70 void *p;
71
72 if (statsfd == -1)
73 return;
74 for (;;) {
75 p = mmap(statp, (npages + 1) * pagesize,
76 PROT_READ, 0, statsfd, 0);
77 if (p == MAP_FAILED)
78 break;
79 else
80 statp = p;
81 npages++;
82 }
83}
84
85int
86geom_stats_open(void)
87{
88 int error;
89 void *p;
90
91 if (statsfd != -1)
92 return (EBUSY);
93 statsfd = open(_PATH_DEV GEOM_STATS_DEVICE, O_RDONLY);
94 if (statsfd < 0)
95 return (errno);
96 pagesize = getpagesize();
97 spp = pagesize / sizeof(struct g_stat);
98 p = mmap(NULL, pagesize, PROT_READ, 0, statsfd, 0);
99 if (p == MAP_FAILED) {
100 error = errno;
101 close(statsfd);
102 statsfd = -1;
103 errno = error;
104 return (error);
105 }
106 statp = p;
107 npages = 1;
108 geom_stats_resync();
109 return (0);
110}
111
112struct snapshot {
113 u_char *ptr;
114 uint pages;
115 uint pagesize;
116 uint perpage;
117 struct timespec time;
118 /* used by getnext: */
119 uint u, v;
120};
121
122void *
123geom_stats_snapshot_get(void)
124{
125 struct snapshot *sp;
126
127 sp = malloc(sizeof *sp);
128 if (sp == NULL)
129 return (NULL);
130 memset(sp, 0, sizeof *sp);
131 sp->ptr = malloc(pagesize * npages);
132 if (sp->ptr == NULL) {
133 free(sp);
134 return (NULL);
135 }
136 memset(sp->ptr, 0, pagesize * npages); /* page in, cache */
137 clock_gettime(CLOCK_REALTIME, &sp->time);
138 memset(sp->ptr, 0, pagesize * npages); /* page in, cache */
139 memcpy(sp->ptr, statp, pagesize * npages);
140 sp->pages = npages;
141 sp->perpage = spp;
142 sp->pagesize = pagesize;
143 return (sp);
144}
145
146void
147geom_stats_snapshot_free(void *arg)
148{
149 struct snapshot *sp;
150
151 sp = arg;
152 free(sp->ptr);
153 free(sp);
154}
155
156void
157geom_stats_snapshot_timestamp(void *arg, struct timespec *tp)
158{
159 struct snapshot *sp;
160
161 sp = arg;
162 *tp = sp->time;
163}
164
165void
166geom_stats_snapshot_reset(void *arg)
167{
168 struct snapshot *sp;
169
170 sp = arg;
171 sp->u = sp->v = 0;
172}
173
174struct g_stat *
175geom_stats_snapshot_next(void *arg)
176{
177 struct g_stat *gsp;
178 struct snapshot *sp;
179
180 sp = arg;
181 gsp = (struct g_stat *)
182 (sp->ptr + sp->u * pagesize + sp->v * sizeof *gsp);
183 if (++sp->v >= sp->perpage) {
184 if (++sp->u >= sp->pages)
185 return (NULL);
186 else
187 sp->v = 0;
188 }
189 return (gsp);
190}
45
46/************************************************************/
47static uint npages, pagesize, spp;
48static int statsfd = -1;
49static u_char *statp;
50
51void
52geom_stats_close(void)
53{
54 if (statsfd == -1)
55 return;
56 munmap(statp, npages *pagesize);
57 statp = NULL;
58 close (statsfd);
59 statsfd = -1;
60}
61
62void
63geom_stats_resync(void)
64{
65 void *p;
66
67 if (statsfd == -1)
68 return;
69 for (;;) {
70 p = mmap(statp, (npages + 1) * pagesize,
71 PROT_READ, 0, statsfd, 0);
72 if (p == MAP_FAILED)
73 break;
74 else
75 statp = p;
76 npages++;
77 }
78}
79
80int
81geom_stats_open(void)
82{
83 int error;
84 void *p;
85
86 if (statsfd != -1)
87 return (EBUSY);
88 statsfd = open(_PATH_DEV GEOM_STATS_DEVICE, O_RDONLY);
89 if (statsfd < 0)
90 return (errno);
91 pagesize = getpagesize();
92 spp = pagesize / sizeof(struct g_stat);
93 p = mmap(NULL, pagesize, PROT_READ, 0, statsfd, 0);
94 if (p == MAP_FAILED) {
95 error = errno;
96 close(statsfd);
97 statsfd = -1;
98 errno = error;
99 return (error);
100 }
101 statp = p;
102 npages = 1;
103 geom_stats_resync();
104 return (0);
105}
106
107struct snapshot {
108 u_char *ptr;
109 uint pages;
110 uint pagesize;
111 uint perpage;
112 struct timespec time;
113 /* used by getnext: */
114 uint u, v;
115};
116
117void *
118geom_stats_snapshot_get(void)
119{
120 struct snapshot *sp;
121
122 sp = malloc(sizeof *sp);
123 if (sp == NULL)
124 return (NULL);
125 memset(sp, 0, sizeof *sp);
126 sp->ptr = malloc(pagesize * npages);
127 if (sp->ptr == NULL) {
128 free(sp);
129 return (NULL);
130 }
131 memset(sp->ptr, 0, pagesize * npages); /* page in, cache */
132 clock_gettime(CLOCK_REALTIME, &sp->time);
133 memset(sp->ptr, 0, pagesize * npages); /* page in, cache */
134 memcpy(sp->ptr, statp, pagesize * npages);
135 sp->pages = npages;
136 sp->perpage = spp;
137 sp->pagesize = pagesize;
138 return (sp);
139}
140
141void
142geom_stats_snapshot_free(void *arg)
143{
144 struct snapshot *sp;
145
146 sp = arg;
147 free(sp->ptr);
148 free(sp);
149}
150
151void
152geom_stats_snapshot_timestamp(void *arg, struct timespec *tp)
153{
154 struct snapshot *sp;
155
156 sp = arg;
157 *tp = sp->time;
158}
159
160void
161geom_stats_snapshot_reset(void *arg)
162{
163 struct snapshot *sp;
164
165 sp = arg;
166 sp->u = sp->v = 0;
167}
168
169struct g_stat *
170geom_stats_snapshot_next(void *arg)
171{
172 struct g_stat *gsp;
173 struct snapshot *sp;
174
175 sp = arg;
176 gsp = (struct g_stat *)
177 (sp->ptr + sp->u * pagesize + sp->v * sizeof *gsp);
178 if (++sp->v >= sp->perpage) {
179 if (++sp->u >= sp->pages)
180 return (NULL);
181 else
182 sp->v = 0;
183 }
184 return (gsp);
185}