xref: /onnv-gate/usr/src/cmd/sendmail/db/mp/mp_pr.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*-
2*0Sstevel@tonic-gate  * See the file LICENSE for redistribution information.
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * Copyright (c) 1996, 1997, 1998
5*0Sstevel@tonic-gate  *	Sleepycat Software.  All rights reserved.
6*0Sstevel@tonic-gate  */
7*0Sstevel@tonic-gate #include "config.h"
8*0Sstevel@tonic-gate 
9*0Sstevel@tonic-gate #ifndef lint
10*0Sstevel@tonic-gate static const char sccsid[] = "@(#)mp_pr.c	10.30 (Sleepycat) 10/1/98";
11*0Sstevel@tonic-gate #endif /* not lint */
12*0Sstevel@tonic-gate 
13*0Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES
14*0Sstevel@tonic-gate #include <sys/types.h>
15*0Sstevel@tonic-gate 
16*0Sstevel@tonic-gate #include <errno.h>
17*0Sstevel@tonic-gate #include <stdio.h>
18*0Sstevel@tonic-gate #include <string.h>
19*0Sstevel@tonic-gate #include <unistd.h>
20*0Sstevel@tonic-gate #endif
21*0Sstevel@tonic-gate 
22*0Sstevel@tonic-gate #include "db_int.h"
23*0Sstevel@tonic-gate #include "db_page.h"
24*0Sstevel@tonic-gate #include "shqueue.h"
25*0Sstevel@tonic-gate #include "db_shash.h"
26*0Sstevel@tonic-gate #include "mp.h"
27*0Sstevel@tonic-gate #include "db_auto.h"
28*0Sstevel@tonic-gate #include "db_ext.h"
29*0Sstevel@tonic-gate #include "common_ext.h"
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate static void __memp_pbh __P((DB_MPOOL *, BH *, size_t *, FILE *));
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate /*
34*0Sstevel@tonic-gate  * memp_stat --
35*0Sstevel@tonic-gate  *	Display MPOOL statistics.
36*0Sstevel@tonic-gate  */
37*0Sstevel@tonic-gate int
memp_stat(dbmp,gspp,fspp,db_malloc)38*0Sstevel@tonic-gate memp_stat(dbmp, gspp, fspp, db_malloc)
39*0Sstevel@tonic-gate 	DB_MPOOL *dbmp;
40*0Sstevel@tonic-gate 	DB_MPOOL_STAT **gspp;
41*0Sstevel@tonic-gate 	DB_MPOOL_FSTAT ***fspp;
42*0Sstevel@tonic-gate 	void *(*db_malloc) __P((size_t));
43*0Sstevel@tonic-gate {
44*0Sstevel@tonic-gate 	DB_MPOOL_FSTAT **tfsp;
45*0Sstevel@tonic-gate 	MPOOLFILE *mfp;
46*0Sstevel@tonic-gate 	size_t len, nlen;
47*0Sstevel@tonic-gate 	int ret;
48*0Sstevel@tonic-gate 	char *name;
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate 	MP_PANIC_CHECK(dbmp);
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 	/* Allocate space for the global statistics. */
53*0Sstevel@tonic-gate 	if (gspp != NULL) {
54*0Sstevel@tonic-gate 		*gspp = NULL;
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate 		if ((ret = __os_malloc(sizeof(**gspp), db_malloc, gspp)) != 0)
57*0Sstevel@tonic-gate 			return (ret);
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate 		LOCKREGION(dbmp);
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 		/* Copy out the global statistics. */
62*0Sstevel@tonic-gate 		**gspp = dbmp->mp->stat;
63*0Sstevel@tonic-gate 		(*gspp)->st_hash_buckets = dbmp->mp->htab_buckets;
64*0Sstevel@tonic-gate 		(*gspp)->st_region_wait =
65*0Sstevel@tonic-gate 		    dbmp->mp->rlayout.lock.mutex_set_wait;
66*0Sstevel@tonic-gate 		(*gspp)->st_region_nowait =
67*0Sstevel@tonic-gate 		    dbmp->mp->rlayout.lock.mutex_set_nowait;
68*0Sstevel@tonic-gate 		(*gspp)->st_refcnt = dbmp->mp->rlayout.refcnt;
69*0Sstevel@tonic-gate 		(*gspp)->st_regsize = dbmp->mp->rlayout.size;
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 		UNLOCKREGION(dbmp);
72*0Sstevel@tonic-gate 	}
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 	if (fspp != NULL) {
75*0Sstevel@tonic-gate 		*fspp = NULL;
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 		LOCKREGION(dbmp);
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 		/* Count the MPOOLFILE structures. */
80*0Sstevel@tonic-gate 		for (len = 0,
81*0Sstevel@tonic-gate 		    mfp = SH_TAILQ_FIRST(&dbmp->mp->mpfq, __mpoolfile);
82*0Sstevel@tonic-gate 		    mfp != NULL;
83*0Sstevel@tonic-gate 		    ++len, mfp = SH_TAILQ_NEXT(mfp, q, __mpoolfile))
84*0Sstevel@tonic-gate 			;
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 		UNLOCKREGION(dbmp);
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 		if (len == 0)
89*0Sstevel@tonic-gate 			return (0);
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 		/* Allocate space for the pointers. */
92*0Sstevel@tonic-gate 		len = (len + 1) * sizeof(DB_MPOOL_FSTAT *);
93*0Sstevel@tonic-gate 		if ((ret = __os_malloc(len, db_malloc, fspp)) != 0)
94*0Sstevel@tonic-gate 			return (ret);
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 		LOCKREGION(dbmp);
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 		/* Build each individual entry. */
99*0Sstevel@tonic-gate 		for (tfsp = *fspp,
100*0Sstevel@tonic-gate 		    mfp = SH_TAILQ_FIRST(&dbmp->mp->mpfq, __mpoolfile);
101*0Sstevel@tonic-gate 		    mfp != NULL;
102*0Sstevel@tonic-gate 		    ++tfsp, mfp = SH_TAILQ_NEXT(mfp, q, __mpoolfile)) {
103*0Sstevel@tonic-gate 			name = __memp_fns(dbmp, mfp);
104*0Sstevel@tonic-gate 			nlen = strlen(name);
105*0Sstevel@tonic-gate 			len = sizeof(DB_MPOOL_FSTAT) + nlen + 1;
106*0Sstevel@tonic-gate 			if ((ret = __os_malloc(len, db_malloc, tfsp)) != 0)
107*0Sstevel@tonic-gate 				return (ret);
108*0Sstevel@tonic-gate 			**tfsp = mfp->stat;
109*0Sstevel@tonic-gate 			(*tfsp)->file_name = (char *)
110*0Sstevel@tonic-gate 			    (u_int8_t *)*tfsp + sizeof(DB_MPOOL_FSTAT);
111*0Sstevel@tonic-gate 			memcpy((*tfsp)->file_name, name, nlen + 1);
112*0Sstevel@tonic-gate 		}
113*0Sstevel@tonic-gate 		*tfsp = NULL;
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 		UNLOCKREGION(dbmp);
116*0Sstevel@tonic-gate 	}
117*0Sstevel@tonic-gate 	return (0);
118*0Sstevel@tonic-gate }
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate /*
121*0Sstevel@tonic-gate  * __memp_fn --
122*0Sstevel@tonic-gate  *	On errors we print whatever is available as the file name.
123*0Sstevel@tonic-gate  *
124*0Sstevel@tonic-gate  * PUBLIC: char * __memp_fn __P((DB_MPOOLFILE *));
125*0Sstevel@tonic-gate  */
126*0Sstevel@tonic-gate char *
__memp_fn(dbmfp)127*0Sstevel@tonic-gate __memp_fn(dbmfp)
128*0Sstevel@tonic-gate 	DB_MPOOLFILE *dbmfp;
129*0Sstevel@tonic-gate {
130*0Sstevel@tonic-gate 	return (__memp_fns(dbmfp->dbmp, dbmfp->mfp));
131*0Sstevel@tonic-gate }
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate /*
134*0Sstevel@tonic-gate  * __memp_fns --
135*0Sstevel@tonic-gate  *	On errors we print whatever is available as the file name.
136*0Sstevel@tonic-gate  *
137*0Sstevel@tonic-gate  * PUBLIC: char * __memp_fns __P((DB_MPOOL *, MPOOLFILE *));
138*0Sstevel@tonic-gate  *
139*0Sstevel@tonic-gate  */
140*0Sstevel@tonic-gate char *
__memp_fns(dbmp,mfp)141*0Sstevel@tonic-gate __memp_fns(dbmp, mfp)
142*0Sstevel@tonic-gate 	DB_MPOOL *dbmp;
143*0Sstevel@tonic-gate 	MPOOLFILE *mfp;
144*0Sstevel@tonic-gate {
145*0Sstevel@tonic-gate 	if (mfp->path_off == 0)
146*0Sstevel@tonic-gate 		return ((char *)"temporary");
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	return ((char *)R_ADDR(dbmp, mfp->path_off));
149*0Sstevel@tonic-gate }
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate #define	FMAP_ENTRIES	200			/* Files we map. */
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate #define	MPOOL_DUMP_HASH	0x01			/* Debug hash chains. */
154*0Sstevel@tonic-gate #define	MPOOL_DUMP_LRU	0x02			/* Debug LRU chains. */
155*0Sstevel@tonic-gate #define	MPOOL_DUMP_MEM	0x04			/* Debug region memory. */
156*0Sstevel@tonic-gate #define	MPOOL_DUMP_ALL	0x07			/* Debug all. */
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate /*
160*0Sstevel@tonic-gate  * __memp_dump_region --
161*0Sstevel@tonic-gate  *	Display MPOOL structures.
162*0Sstevel@tonic-gate  *
163*0Sstevel@tonic-gate  * PUBLIC: void __memp_dump_region __P((DB_MPOOL *, char *, FILE *));
164*0Sstevel@tonic-gate  */
165*0Sstevel@tonic-gate void
__memp_dump_region(dbmp,area,fp)166*0Sstevel@tonic-gate __memp_dump_region(dbmp, area, fp)
167*0Sstevel@tonic-gate 	DB_MPOOL *dbmp;
168*0Sstevel@tonic-gate 	char *area;
169*0Sstevel@tonic-gate 	FILE *fp;
170*0Sstevel@tonic-gate {
171*0Sstevel@tonic-gate 	BH *bhp;
172*0Sstevel@tonic-gate 	DB_HASHTAB *htabp;
173*0Sstevel@tonic-gate 	DB_MPOOLFILE *dbmfp;
174*0Sstevel@tonic-gate 	MPOOL *mp;
175*0Sstevel@tonic-gate 	MPOOLFILE *mfp;
176*0Sstevel@tonic-gate 	size_t bucket, fmap[FMAP_ENTRIES + 1];
177*0Sstevel@tonic-gate 	u_int32_t flags;
178*0Sstevel@tonic-gate 	int cnt;
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	/* Make it easy to call from the debugger. */
181*0Sstevel@tonic-gate 	if (fp == NULL)
182*0Sstevel@tonic-gate 		fp = stderr;
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	for (flags = 0; *area != '\0'; ++area)
185*0Sstevel@tonic-gate 		switch (*area) {
186*0Sstevel@tonic-gate 		case 'A':
187*0Sstevel@tonic-gate 			LF_SET(MPOOL_DUMP_ALL);
188*0Sstevel@tonic-gate 			break;
189*0Sstevel@tonic-gate 		case 'h':
190*0Sstevel@tonic-gate 			LF_SET(MPOOL_DUMP_HASH);
191*0Sstevel@tonic-gate 			break;
192*0Sstevel@tonic-gate 		case 'l':
193*0Sstevel@tonic-gate 			LF_SET(MPOOL_DUMP_LRU);
194*0Sstevel@tonic-gate 			break;
195*0Sstevel@tonic-gate 		case 'm':
196*0Sstevel@tonic-gate 			LF_SET(MPOOL_DUMP_MEM);
197*0Sstevel@tonic-gate 			break;
198*0Sstevel@tonic-gate 		}
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	LOCKREGION(dbmp);
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	mp = dbmp->mp;
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	/* Display MPOOL structures. */
205*0Sstevel@tonic-gate 	(void)fprintf(fp, "%s\nPool (region addr 0x%lx, alloc addr 0x%lx)\n",
206*0Sstevel@tonic-gate 	    DB_LINE, (u_long)dbmp->reginfo.addr, (u_long)dbmp->addr);
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 	/* Display the MPOOLFILE structures. */
209*0Sstevel@tonic-gate 	cnt = 0;
210*0Sstevel@tonic-gate 	for (mfp = SH_TAILQ_FIRST(&dbmp->mp->mpfq, __mpoolfile);
211*0Sstevel@tonic-gate 	    mfp != NULL; mfp = SH_TAILQ_NEXT(mfp, q, __mpoolfile), ++cnt) {
212*0Sstevel@tonic-gate 		(void)fprintf(fp, "file #%d: %s: refs %lu, type %ld, %s\n",
213*0Sstevel@tonic-gate 		    cnt + 1, __memp_fns(dbmp, mfp), (u_long)mfp->ref,
214*0Sstevel@tonic-gate 		    (long)mfp->ftype,
215*0Sstevel@tonic-gate 		    F_ISSET(mfp, MP_CAN_MMAP) ? "mmap" : "read/write");
216*0Sstevel@tonic-gate 		    if (cnt < FMAP_ENTRIES)
217*0Sstevel@tonic-gate 			fmap[cnt] = R_OFFSET(dbmp, mfp);
218*0Sstevel@tonic-gate 	}
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 	for (dbmfp = TAILQ_FIRST(&dbmp->dbmfq);
221*0Sstevel@tonic-gate 	    dbmfp != NULL; dbmfp = TAILQ_NEXT(dbmfp, q), ++cnt) {
222*0Sstevel@tonic-gate 		(void)fprintf(fp, "file #%d: %s: fd: %d: per-process, %s\n",
223*0Sstevel@tonic-gate 		    cnt + 1, __memp_fn(dbmfp), dbmfp->fd,
224*0Sstevel@tonic-gate 		    F_ISSET(dbmfp, MP_READONLY) ? "readonly" : "read/write");
225*0Sstevel@tonic-gate 		    if (cnt < FMAP_ENTRIES)
226*0Sstevel@tonic-gate 			fmap[cnt] = R_OFFSET(dbmp, mfp);
227*0Sstevel@tonic-gate 	}
228*0Sstevel@tonic-gate 	if (cnt < FMAP_ENTRIES)
229*0Sstevel@tonic-gate 		fmap[cnt] = INVALID;
230*0Sstevel@tonic-gate 	else
231*0Sstevel@tonic-gate 		fmap[FMAP_ENTRIES] = INVALID;
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 	/* Display the hash table list of BH's. */
234*0Sstevel@tonic-gate 	if (LF_ISSET(MPOOL_DUMP_HASH)) {
235*0Sstevel@tonic-gate 		(void)fprintf(fp,
236*0Sstevel@tonic-gate 	    "%s\nBH hash table (%lu hash slots)\npageno, file, ref, address\n",
237*0Sstevel@tonic-gate 		    DB_LINE, (u_long)mp->htab_buckets);
238*0Sstevel@tonic-gate 		for (htabp = dbmp->htab,
239*0Sstevel@tonic-gate 		    bucket = 0; bucket < mp->htab_buckets; ++htabp, ++bucket) {
240*0Sstevel@tonic-gate 			if (SH_TAILQ_FIRST(&dbmp->htab[bucket], __bh) != NULL)
241*0Sstevel@tonic-gate 				(void)fprintf(fp, "%lu:\n", (u_long)bucket);
242*0Sstevel@tonic-gate 			for (bhp = SH_TAILQ_FIRST(&dbmp->htab[bucket], __bh);
243*0Sstevel@tonic-gate 			    bhp != NULL; bhp = SH_TAILQ_NEXT(bhp, hq, __bh))
244*0Sstevel@tonic-gate 				__memp_pbh(dbmp, bhp, fmap, fp);
245*0Sstevel@tonic-gate 		}
246*0Sstevel@tonic-gate 	}
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 	/* Display the LRU list of BH's. */
249*0Sstevel@tonic-gate 	if (LF_ISSET(MPOOL_DUMP_LRU)) {
250*0Sstevel@tonic-gate 		(void)fprintf(fp, "%s\nBH LRU list\n", DB_LINE);
251*0Sstevel@tonic-gate 		(void)fprintf(fp, "pageno, file, ref, address\n");
252*0Sstevel@tonic-gate 		for (bhp = SH_TAILQ_FIRST(&dbmp->mp->bhq, __bh);
253*0Sstevel@tonic-gate 		    bhp != NULL; bhp = SH_TAILQ_NEXT(bhp, q, __bh))
254*0Sstevel@tonic-gate 			__memp_pbh(dbmp, bhp, fmap, fp);
255*0Sstevel@tonic-gate 	}
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 	if (LF_ISSET(MPOOL_DUMP_MEM))
258*0Sstevel@tonic-gate 		__db_shalloc_dump(dbmp->addr, fp);
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 	UNLOCKREGION(dbmp);
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate 	/* Flush in case we're debugging. */
263*0Sstevel@tonic-gate 	(void)fflush(fp);
264*0Sstevel@tonic-gate }
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate /*
267*0Sstevel@tonic-gate  * __memp_pbh --
268*0Sstevel@tonic-gate  *	Display a BH structure.
269*0Sstevel@tonic-gate  */
270*0Sstevel@tonic-gate static void
__memp_pbh(dbmp,bhp,fmap,fp)271*0Sstevel@tonic-gate __memp_pbh(dbmp, bhp, fmap, fp)
272*0Sstevel@tonic-gate 	DB_MPOOL *dbmp;
273*0Sstevel@tonic-gate 	BH *bhp;
274*0Sstevel@tonic-gate 	size_t *fmap;
275*0Sstevel@tonic-gate 	FILE *fp;
276*0Sstevel@tonic-gate {
277*0Sstevel@tonic-gate 	static const FN fn[] = {
278*0Sstevel@tonic-gate 		{ BH_CALLPGIN,	"callpgin" },
279*0Sstevel@tonic-gate 		{ BH_DIRTY,	"dirty" },
280*0Sstevel@tonic-gate 		{ BH_DISCARD,	"discard" },
281*0Sstevel@tonic-gate 		{ BH_LOCKED,	"locked" },
282*0Sstevel@tonic-gate 		{ BH_TRASH,	"trash" },
283*0Sstevel@tonic-gate 		{ BH_WRITE,	"write" },
284*0Sstevel@tonic-gate 		{ 0 },
285*0Sstevel@tonic-gate 	};
286*0Sstevel@tonic-gate 	int i;
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate 	for (i = 0; i < FMAP_ENTRIES; ++i)
289*0Sstevel@tonic-gate 		if (fmap[i] == INVALID || fmap[i] == bhp->mf_offset)
290*0Sstevel@tonic-gate 			break;
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	if (fmap[i] == INVALID)
293*0Sstevel@tonic-gate 		(void)fprintf(fp, "  %4lu, %lu, %2lu, %lu",
294*0Sstevel@tonic-gate 		    (u_long)bhp->pgno, (u_long)bhp->mf_offset,
295*0Sstevel@tonic-gate 		    (u_long)bhp->ref, (u_long)R_OFFSET(dbmp, bhp));
296*0Sstevel@tonic-gate 	else
297*0Sstevel@tonic-gate 		(void)fprintf(fp, "  %4lu,   #%d,  %2lu, %lu",
298*0Sstevel@tonic-gate 		    (u_long)bhp->pgno, i + 1,
299*0Sstevel@tonic-gate 		    (u_long)bhp->ref, (u_long)R_OFFSET(dbmp, bhp));
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 	__db_prflags(bhp->flags, fn, fp);
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 	(void)fprintf(fp, "\n");
304*0Sstevel@tonic-gate }
305