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