xref: /titanic_44/usr/src/lib/libast/common/vmalloc/vmstat.c (revision 726fad2a65f16c200a03969c29cb5c86c2d427db)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
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 ? vm->data : Vmregion->data;
47 	reg int		inuse;
48 
49 	SETINUSE(vd, inuse);
50 	if(!st)
51 	{	CLRINUSE(vd, inuse);
52 		return inuse ? 1 : 0;
53 	}
54 	if(!(vd->mode&VM_TRUST))
55 	{	if(ISLOCK(vd,0))
56 		{	CLRINUSE(vd, inuse);
57 			return -1;
58 		}
59 		SETLOCK(vd,0);
60 	}
61 
62 	st->n_busy = st->n_free = 0;
63 	st->s_busy = st->s_free = st->m_busy = st->m_free = 0;
64 	st->n_seg = 0;
65 	st->extent = 0;
66 
67 	if(vd->mode&VM_MTLAST)
68 		st->n_busy = 0;
69 	else if((vd->mode&VM_MTPOOL) && (s = vd->pool) > 0)
70 	{	s = ROUND(s,ALIGN);
71 		for(b = vd->free; b; b = SEGLINK(b))
72 			st->n_free += 1;
73 	}
74 
75 	for(seg = vd->seg; seg; seg = seg->next)
76 	{	st->n_seg += 1;
77 		st->extent += seg->extent;
78 
79 		b = SEGBLOCK(seg);
80 		endb = BLOCK(seg->baddr);
81 
82 		if(vd->mode&(VM_MTDEBUG|VM_MTBEST|VM_MTPROFILE))
83 		{	while(b < endb)
84 			{	s = SIZE(b)&~BITS;
85 				if(ISJUNK(SIZE(b)) || !ISBUSY(SIZE(b)))
86 				{	if(s > st->m_free)
87 						st->m_free = s;
88 					st->s_free += s;
89 					st->n_free += 1;
90 				}
91 				else	/* get the real size */
92 				{	if(vd->mode&VM_MTDEBUG)
93 						s = DBSIZE(DB2DEBUG(DATA(b)));
94 					else if(vd->mode&VM_MTPROFILE)
95 						s = PFSIZE(DATA(b));
96 					if(s > st->m_busy)
97 						st->m_busy = s;
98 					st->s_busy += s;
99 					st->n_busy += 1;
100 				}
101 
102 				b = (Block_t*)((Vmuchar_t*)DATA(b) + (SIZE(b)&~BITS) );
103 			}
104 		}
105 		else if(vd->mode&VM_MTLAST)
106 		{	if((s = seg->free ? (SIZE(seg->free) + sizeof(Head_t)) : 0) > 0)
107 			{	st->s_free += s;
108 				st->n_free += 1;
109 			}
110 			if((s = ((char*)endb - (char*)b) - s) > 0)
111 			{	st->s_busy += s;
112 				st->n_busy += 1;
113 			}
114 		}
115 		else if((vd->mode&VM_MTPOOL) && s > 0)
116 		{	if(seg->free)
117 				st->n_free += (SIZE(seg->free)+sizeof(Head_t))/s;
118 			st->n_busy += ((seg->baddr - (Vmuchar_t*)b) - sizeof(Head_t))/s;
119 		}
120 	}
121 
122 	if((vd->mode&VM_MTPOOL) && s > 0)
123 	{	st->n_busy -= st->n_free;
124 		if(st->n_busy > 0)
125 			st->s_busy = (st->m_busy = vd->pool)*st->n_busy;
126 		if(st->n_free > 0)
127 			st->s_free = (st->m_free = vd->pool)*st->n_free;
128 	}
129 
130 	CLRLOCK(vd,0);
131 	CLRINUSE(vd, inuse);
132 	return inuse ? 1 : 0;
133 }
134 
135 #endif
136