mbuf.c (00925a8e849b375b464dd7b8f15b0a8dee7ec81a) mbuf.c (099a0e588cbe1bbc56a565bf57d722621b47a866)
1/*
2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. 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

--- 85 unchanged lines hidden (view full) ---

94/*
95 * Print mbuf statistics.
96 */
97void
98mbpr(u_long mbaddr, u_long mbtaddr __unused, u_long nmbcaddr, u_long nmbufaddr,
99 u_long mbhiaddr, u_long clhiaddr, u_long mbloaddr, u_long clloaddr,
100 u_long cpusaddr __unused, u_long pgsaddr, u_long mbpaddr)
101{
1/*
2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. 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

--- 85 unchanged lines hidden (view full) ---

94/*
95 * Print mbuf statistics.
96 */
97void
98mbpr(u_long mbaddr, u_long mbtaddr __unused, u_long nmbcaddr, u_long nmbufaddr,
99 u_long mbhiaddr, u_long clhiaddr, u_long mbloaddr, u_long clloaddr,
100 u_long cpusaddr __unused, u_long pgsaddr, u_long mbpaddr)
101{
102 int i, j, nmbufs, nmbclusters, page_size, num_objs;
102 int i, nmbclusters;
103 int nsfbufs, nsfbufspeak, nsfbufsused;
103 int nsfbufs, nsfbufspeak, nsfbufsused;
104 u_int mbuf_hiwm, clust_hiwm, mbuf_lowm, clust_lowm;
105 u_long totspace[2], totused[2];
106 u_long gentotnum, gentotfree, totnum, totfree;
107 u_long totmem, totmemalloced, totmemused;
108 short nmbtypes;
109 size_t mlen;
110 long *mbtypes = NULL;
111 struct mbstat *mbstat = NULL;
104 short nmbtypes;
105 size_t mlen;
106 long *mbtypes = NULL;
107 struct mbstat *mbstat = NULL;
112 struct mbpstat **mbpstat = NULL;
113 struct mbtypenames *mp;
114 bool *seen = NULL;
115
116 mlen = sizeof *mbstat;
117 if ((mbstat = malloc(mlen)) == NULL) {
118 warn("malloc: cannot allocate memory for mbstat");
119 goto err;
120 }
121
108 struct mbtypenames *mp;
109 bool *seen = NULL;
110
111 mlen = sizeof *mbstat;
112 if ((mbstat = malloc(mlen)) == NULL) {
113 warn("malloc: cannot allocate memory for mbstat");
114 goto err;
115 }
116
122 /*
123 * XXX: Unfortunately, for the time being, we have to fetch
124 * the total length of the per-CPU stats area via sysctl
125 * (regardless of whether we're looking at a core or not.
126 */
127 if (sysctlbyname("kern.ipc.mb_statpcpu", NULL, &mlen, NULL, 0) < 0) {
128 warn("sysctl: retrieving mb_statpcpu len");
129 goto err;
130 }
131 num_objs = (int)(mlen / sizeof(struct mbpstat));
132 if ((mbpstat = calloc(num_objs, sizeof(struct mbpstat *))) == NULL) {
133 warn("calloc: cannot allocate memory for mbpstats pointers");
134 goto err;
135 }
136 if ((mbpstat[0] = calloc(num_objs, sizeof(struct mbpstat))) == NULL) {
137 warn("calloc: cannot allocate memory for mbpstats");
138 goto err;
139 }
140
141 if (mbaddr) {
117 if (mbaddr) {
142 if (kread(mbpaddr, (char *)mbpstat[0], mlen))
143 goto err;
144 if (kread(mbaddr, (char *)mbstat, sizeof mbstat))
145 goto err;
146 if (kread(nmbcaddr, (char *)&nmbclusters, sizeof(int)))
147 goto err;
118 if (kread(mbaddr, (char *)mbstat, sizeof mbstat))
119 goto err;
120 if (kread(nmbcaddr, (char *)&nmbclusters, sizeof(int)))
121 goto err;
148 if (kread(nmbufaddr, (char *)&nmbufs, sizeof(int)))
149 goto err;
150 if (kread(mbhiaddr, (char *)&mbuf_hiwm, sizeof(u_int)))
151 goto err;
152 if (kread(clhiaddr, (char *)&clust_hiwm, sizeof(u_int)))
153 goto err;
154 if (kread(mbloaddr, (char *)&mbuf_lowm, sizeof(u_int)))
155 goto err;
156 if (kread(clloaddr, (char *)&clust_lowm, sizeof(u_int)))
157 goto err;
158 if (kread(pgsaddr, (char *)&page_size, sizeof(int)))
159 goto err;
160 } else {
122 } else {
161 if (sysctlbyname("kern.ipc.mb_statpcpu", mbpstat[0], &mlen,
162 NULL, 0) < 0) {
163 warn("sysctl: retrieving mb_statpcpu");
164 goto err;
165 }
166 mlen = sizeof *mbstat;
167 if (sysctlbyname("kern.ipc.mbstat", mbstat, &mlen, NULL, 0)
168 < 0) {
169 warn("sysctl: retrieving mbstat");
170 goto err;
171 }
172 mlen = sizeof(int);
173 if (sysctlbyname("kern.ipc.nmbclusters", &nmbclusters, &mlen,
174 NULL, 0) < 0) {
175 warn("sysctl: retrieving nmbclusters");
176 goto err;
177 }
123 mlen = sizeof *mbstat;
124 if (sysctlbyname("kern.ipc.mbstat", mbstat, &mlen, NULL, 0)
125 < 0) {
126 warn("sysctl: retrieving mbstat");
127 goto err;
128 }
129 mlen = sizeof(int);
130 if (sysctlbyname("kern.ipc.nmbclusters", &nmbclusters, &mlen,
131 NULL, 0) < 0) {
132 warn("sysctl: retrieving nmbclusters");
133 goto err;
134 }
178 mlen = sizeof(int);
179 if (sysctlbyname("kern.ipc.nmbufs", &nmbufs, &mlen, NULL, 0)
180 < 0) {
181 warn("sysctl: retrieving nmbufs");
182 goto err;
183 }
184 mlen = sizeof(u_int);
185 if (sysctlbyname("kern.ipc.mbuf_hiwm", &mbuf_hiwm, &mlen,
186 NULL, 0) < 0) {
187 warn("sysctl: retrieving mbuf_hiwm");
188 goto err;
189 }
190 mlen = sizeof(u_int);
191 if (sysctlbyname("kern.ipc.clust_hiwm", &clust_hiwm, &mlen,
192 NULL, 0) < 0) {
193 warn("sysctl: retrieving clust_hiwm");
194 goto err;
195 }
196 mlen = sizeof(u_int);
197 if (sysctlbyname("kern.ipc.mbuf_lowm", &mbuf_lowm, &mlen,
198 NULL, 0) < 0) {
199 warn("sysctl: retrieving mbuf_lowm");
200 goto err;
201 }
202 mlen = sizeof(u_int);
203 if (sysctlbyname("kern.ipc.clust_lowm", &clust_lowm, &mlen,
204 NULL, 0) < 0) {
205 warn("sysctl: retrieving clust_lowm");
206 goto err;
207 }
208 mlen = sizeof(int);
209 if (sysctlbyname("hw.pagesize", &page_size, &mlen, NULL, 0)
210 < 0) {
211 warn("sysctl: retrieving hw.pagesize");
212 goto err;
213 }
214 }
135 }
136 if (mbstat->m_mbufs < 0) mbstat->m_mbufs = 0; /* XXX */
137 if (mbstat->m_mclusts < 0) mbstat->m_mclusts = 0; /* XXX */
215
216 nmbtypes = mbstat->m_numtypes;
217 if ((seen = calloc(nmbtypes, sizeof(*seen))) == NULL) {
218 warn("calloc: cannot allocate memory for mbtypes seen flag");
219 goto err;
220 }
221 if ((mbtypes = calloc(nmbtypes, sizeof(long *))) == NULL) {
222 warn("calloc: cannot allocate memory for mbtypes");
223 goto err;
224 }
225
138
139 nmbtypes = mbstat->m_numtypes;
140 if ((seen = calloc(nmbtypes, sizeof(*seen))) == NULL) {
141 warn("calloc: cannot allocate memory for mbtypes seen flag");
142 goto err;
143 }
144 if ((mbtypes = calloc(nmbtypes, sizeof(long *))) == NULL) {
145 warn("calloc: cannot allocate memory for mbtypes");
146 goto err;
147 }
148
226 for (i = 0; i < num_objs; i++)
227 mbpstat[i] = mbpstat[0] + i;
228
229#undef MSIZE
230#define MSIZE (mbstat->m_msize)
231#undef MCLBYTES
232#define MCLBYTES (mbstat->m_mclbytes)
149#undef MSIZE
150#define MSIZE (mbstat->m_msize)
151#undef MCLBYTES
152#define MCLBYTES (mbstat->m_mclbytes)
233#define GENLST (num_objs - 1)
234
153
235 totnum = mbpstat[GENLST]->mb_mbbucks * mbstat->m_mbperbuck;
236 totfree = mbpstat[GENLST]->mb_mbfree;
237 for (j = 1; j < nmbtypes; j++)
238 mbtypes[j] += mbpstat[GENLST]->mb_mbtypes[j];
239 totspace[0] = mbpstat[GENLST]->mb_mbbucks * mbstat->m_mbperbuck * MSIZE;
240 for (i = 0; i < (num_objs - 1); i++) {
241 if (mbpstat[i]->mb_active == 0)
242 continue;
243 totspace[0] += mbpstat[i]->mb_mbbucks*mbstat->m_mbperbuck*MSIZE;
244 totnum += mbpstat[i]->mb_mbbucks * mbstat->m_mbperbuck;
245 totfree += mbpstat[i]->mb_mbfree;
246 for (j = 1; j < nmbtypes; j++)
247 mbtypes[j] += mbpstat[i]->mb_mbtypes[j];
248 }
249 totused[0] = totnum - totfree;
250 if (cflag) {
251 printf("mbuf usage:\n"
252 "\tTotal:\t\t%lu/%lu/%d (in use/in pool/max)\n",
253 totused[0], totnum, nmbufs);
254 gentotnum = mbpstat[GENLST]->mb_mbbucks * mbstat->m_mbperbuck;
255 gentotfree = mbpstat[GENLST]->mb_mbfree;
256 printf("\tGEN cache:\t%lu/%lu (in use/in pool)\n",
257 gentotnum - gentotfree, gentotnum);
258 } else {
259 /* XXX: peak is now wrong. */
260 printf("%lu/%lu/%d mbufs in use (current/peak/max):\n",
261 totused[0], totnum, nmbufs);
262 }
154 printf("%lu mbufs in use\n", mbstat->m_mbufs);
263
155
264 for (i = 0; cflag && i < (num_objs - 1); i++) {
265 if (mbpstat[i]->mb_active == 0)
266 continue;
267 printf("\tCPU #%d cache:\t%lu/%lu (in use/in pool)\n",
268 i,
269 (mbpstat[i]->mb_mbbucks * mbstat->m_mbperbuck -
270 mbpstat[i]->mb_mbfree),
271 (mbpstat[i]->mb_mbbucks * mbstat->m_mbperbuck));
272 }
273 if (cflag) {
274 printf("\tMbuf cache high watermark: %d\n", mbuf_hiwm);
275#ifdef NOTYET
276 printf("\tMbuf cache low watermark: %d\n", mbuf_lowm);
277#endif
278 }
279 for (mp = mbtypenames; mp->mt_name; mp++) {
280 if (mbtypes[mp->mt_type]) {
281 seen[mp->mt_type] = YES;
282 printf("\t %lu mbufs allocated to %s\n",
283 mbtypes[mp->mt_type], mp->mt_name);
284 }
285 }
286 for (i = 1; i < nmbtypes; i++) {
287 if (!seen[i] && mbtypes[i])
288 printf("\t %lu mbufs allocated to <mbuf type: %d>\n",
289 mbtypes[i], i);
290 }
156 for (mp = mbtypenames; mp->mt_name; mp++) {
157 if (mbtypes[mp->mt_type]) {
158 seen[mp->mt_type] = YES;
159 printf("\t %lu mbufs allocated to %s\n",
160 mbtypes[mp->mt_type], mp->mt_name);
161 }
162 }
163 for (i = 1; i < nmbtypes; i++) {
164 if (!seen[i] && mbtypes[i])
165 printf("\t %lu mbufs allocated to <mbuf type: %d>\n",
166 mbtypes[i], i);
167 }
291 if (cflag)
292 printf("\t%.1f%% of mbuf map consumed\n",
293 totspace[0] * 100.0 / (nmbufs * MSIZE));
294
168
295 totnum = mbpstat[GENLST]->mb_clbucks * mbstat->m_clperbuck;
296 totfree = mbpstat[GENLST]->mb_clfree;
297 totspace[1] = mbpstat[GENLST]->mb_clbucks*mbstat->m_clperbuck*MCLBYTES;
298 for (i = 0; i < (num_objs - 1); i++) {
299 if (mbpstat[i]->mb_active == 0)
300 continue;
301 totspace[1] += mbpstat[i]->mb_clbucks * mbstat->m_clperbuck
302 * MCLBYTES;
303 totnum += mbpstat[i]->mb_clbucks * mbstat->m_clperbuck;
304 totfree += mbpstat[i]->mb_clfree;
305 }
306 totused[1] = totnum - totfree;
307 if (cflag) {
308 printf("mbuf cluster usage:\n"
309 "\tTotal:\t\t%lu/%lu/%d (in use/in pool/max)\n",
310 totused[1], totnum, nmbclusters);
311 gentotnum = mbpstat[GENLST]->mb_clbucks * mbstat->m_clperbuck;
312 gentotfree = mbpstat[GENLST]->mb_clfree;
313 printf("\tGEN cache:\t%lu/%lu (in use/in pool)\n",
314 gentotnum - gentotfree, gentotnum);
315 } else {
316 /* XXX: peak is now wrong. */
317 printf("%lu/%lu/%d mbuf clusters in use (current/peak/max)\n",
318 totused[1], totnum, nmbclusters);
319 }
320 for (i = 0; cflag && i < (num_objs - 1); i++) {
321 if (mbpstat[i]->mb_active == 0)
322 continue;
323 printf("\tCPU #%d cache:\t%lu/%lu (in use/in pool)\n",
324 i,
325 (mbpstat[i]->mb_clbucks * mbstat->m_clperbuck -
326 mbpstat[i]->mb_clfree),
327 (mbpstat[i]->mb_clbucks * mbstat->m_clperbuck));
328 }
329 if (cflag) {
330 printf("\tCluster cache high watermark: %d\n", clust_hiwm);
331#ifdef NOTYET
332 printf("\tCluster cache low watermark: %d\n", clust_lowm);
333#endif
334 }
335 if (cflag)
336 printf("\t%.1f%% of cluster map consumed\n",
337 totspace[1] * 100.0 / (nmbclusters * MCLBYTES));
169 printf("%lu/%d mbuf clusters in use (current/max)\n",
170 mbstat->m_mclusts, nmbclusters);
171
338 mlen = sizeof(nsfbufs);
339 if (!sysctlbyname("kern.ipc.nsfbufs", &nsfbufs, &mlen, NULL, 0) &&
340 !sysctlbyname("kern.ipc.nsfbufsused", &nsfbufsused, &mlen, NULL,
341 0) &&
342 !sysctlbyname("kern.ipc.nsfbufspeak", &nsfbufspeak, &mlen, NULL,
343 0)) {
344 printf("%d/%d/%d sfbufs in use (current/peak/max)\n",
345 nsfbufsused, nsfbufspeak, nsfbufs);
346 }
172 mlen = sizeof(nsfbufs);
173 if (!sysctlbyname("kern.ipc.nsfbufs", &nsfbufs, &mlen, NULL, 0) &&
174 !sysctlbyname("kern.ipc.nsfbufsused", &nsfbufsused, &mlen, NULL,
175 0) &&
176 !sysctlbyname("kern.ipc.nsfbufspeak", &nsfbufspeak, &mlen, NULL,
177 0)) {
178 printf("%d/%d/%d sfbufs in use (current/peak/max)\n",
179 nsfbufsused, nsfbufspeak, nsfbufs);
180 }
347 totmem = nmbufs * MSIZE + nmbclusters * MCLBYTES;
348 totmemalloced = totspace[0] + totspace[1];
349 totmemused = totused[0] * MSIZE + totused[1] * MCLBYTES;
350 printf(
351 "%lu KBytes allocated to network (%.1f%% in use, %.1f%% wired)\n",
352 totmem / 1024, totmemused * 100.0 / totmem,
353 totmemalloced * 100.0 / totmem);
354 printf("%lu requests for memory denied\n", mbstat->m_drops);
355 printf("%lu requests for memory delayed\n", mbstat->m_wait);
181 printf("%lu KBytes allocated to network\n", (mbstat->m_mbufs * MSIZE +
182 mbstat->m_mclusts * MCLBYTES) / 1024);
356 printf("%lu requests for sfbufs denied\n", mbstat->sf_allocfail);
357 printf("%lu requests for sfbufs delayed\n", mbstat->sf_allocwait);
358 printf("%lu requests for I/O initiated by sendfile\n",
359 mbstat->sf_iocnt);
360 printf("%lu calls to protocol drain routines\n", mbstat->m_drain);
361
362err:
363 if (mbtypes != NULL)
364 free(mbtypes);
365 if (seen != NULL)
366 free(seen);
367 if (mbstat != NULL)
368 free(mbstat);
183 printf("%lu requests for sfbufs denied\n", mbstat->sf_allocfail);
184 printf("%lu requests for sfbufs delayed\n", mbstat->sf_allocwait);
185 printf("%lu requests for I/O initiated by sendfile\n",
186 mbstat->sf_iocnt);
187 printf("%lu calls to protocol drain routines\n", mbstat->m_drain);
188
189err:
190 if (mbtypes != NULL)
191 free(mbtypes);
192 if (seen != NULL)
193 free(seen);
194 if (mbstat != NULL)
195 free(mbstat);
369 if (mbpstat != NULL) {
370 if (mbpstat[0] != NULL)
371 free(mbpstat[0]);
372 free(mbpstat);
373 }
374}
196}