xref: /titanic_41/usr/src/lib/libast/common/vmalloc/vmstat.c (revision 67e96bdfbca5b789a8c03fa34eaaa6c9aec7a629)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #if defined(_UWIN) && defined(_BLD_ast)
23 
24 void _STUB_vmstat(){}
25 
26 #else
27 
28 #include	"vmhdr.h"
29 
30 /*	Get statistics from a region.
31 **
32 **	Written by Kiem-Phong Vo, kpv@research.att.com, 01/16/94.
33 */
34 
35 #if __STD_C
36 int vmstat(Vmalloc_t* vm, Vmstat_t* st)
37 #else
38 int vmstat(vm, st)
39 Vmalloc_t*	vm;
40 Vmstat_t*	st;
41 #endif
42 {
43 	reg Seg_t*	seg;
44 	reg Block_t	*b, *endb;
45 	reg size_t	s = 0;
46 	reg Vmdata_t*	vd = vm->data;
47 
48 	if(!st)
49 		return -1;
50 	if(!(vd->mode&VM_TRUST))
51 	{	if(ISLOCK(vd,0))
52 			return -1;
53 		SETLOCK(vd,0);
54 	}
55 
56 	st->n_busy = st->n_free = 0;
57 	st->s_busy = st->s_free = st->m_busy = st->m_free = 0;
58 	st->n_seg = 0;
59 	st->extent = 0;
60 
61 	if(vd->mode&VM_MTLAST)
62 		st->n_busy = 0;
63 	else if((vd->mode&VM_MTPOOL) && (s = vd->pool) > 0)
64 	{	s = ROUND(s,ALIGN);
65 		for(b = vd->free; b; b = SEGLINK(b))
66 			st->n_free += 1;
67 	}
68 
69 	for(seg = vd->seg; seg; seg = seg->next)
70 	{	st->n_seg += 1;
71 		st->extent += seg->extent;
72 
73 		b = SEGBLOCK(seg);
74 		endb = BLOCK(seg->baddr);
75 
76 		if(vd->mode&(VM_MTDEBUG|VM_MTBEST|VM_MTPROFILE))
77 		{	while(b < endb)
78 			{	s = SIZE(b)&~BITS;
79 				if(ISJUNK(SIZE(b)) || !ISBUSY(SIZE(b)))
80 				{	if(s > st->m_free)
81 						st->m_free = s;
82 					st->s_free += s;
83 					st->n_free += 1;
84 				}
85 				else	/* get the real size */
86 				{	if(vd->mode&VM_MTDEBUG)
87 						s = DBSIZE(DB2DEBUG(DATA(b)));
88 					else if(vd->mode&VM_MTPROFILE)
89 						s = PFSIZE(DATA(b));
90 					if(s > st->m_busy)
91 						st->m_busy = s;
92 					st->s_busy += s;
93 					st->n_busy += 1;
94 				}
95 
96 				b = (Block_t*)((Vmuchar_t*)DATA(b) + (SIZE(b)&~BITS) );
97 			}
98 		}
99 		else if(vd->mode&VM_MTLAST)
100 		{	if((s = seg->free ? (SIZE(seg->free) + sizeof(Head_t)) : 0) > 0)
101 			{	st->s_free += s;
102 				st->n_free += 1;
103 			}
104 			if((s = ((char*)endb - (char*)b) - s) > 0)
105 			{	st->s_busy += s;
106 				st->n_busy += 1;
107 			}
108 		}
109 		else if((vd->mode&VM_MTPOOL) && s > 0)
110 		{	if(seg->free)
111 				st->n_free += (SIZE(seg->free)+sizeof(Head_t))/s;
112 			st->n_busy += ((seg->baddr - (Vmuchar_t*)b) - sizeof(Head_t))/s;
113 		}
114 	}
115 
116 	if((vd->mode&VM_MTPOOL) && s > 0)
117 	{	st->n_busy -= st->n_free;
118 		if(st->n_busy > 0)
119 			st->s_busy = (st->m_busy = vd->pool)*st->n_busy;
120 		if(st->n_free > 0)
121 			st->s_free = (st->m_free = vd->pool)*st->n_free;
122 	}
123 
124 	CLRLOCK(vd,0);
125 	return 0;
126 }
127 
128 #endif
129