1 /*
2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3 * 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
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * $Id: vm_meter.c,v 1.1 94/10/19 17:37:26 bill Exp $
34 */
35
36 #include "sys/param.h"
37 #include "sys/errno.h"
38 #include "proc.h"
39 #include "kernel.h" /* time */
40 #include "vmmeter.h"
41
42 #include "vm.h"
43 #include "vmspace.h"
44
45 #include "prototypes.h"
46
47 fixpt_t averunnable[3]; /* load average, of runnable procs */
48
49 int maxslp = MAXSLP;
50 int saferss = SAFERSS;
51
52
53 void
vmmeter()54 vmmeter()
55 {
56 register unsigned *cp, *rp, *sp;
57
58 if (time.tv_sec % 5 == 0)
59 vmtotal();
60 if (proc0.p_slptime > maxslp/2)
61 wakeup((caddr_t)&proc0);
62 }
63
64 void
vmtotal()65 vmtotal()
66 {
67 register struct proc *p;
68 int nrun = 0;
69 register struct vmspace *vms;
70
71 total.t_vm = 0;
72 total.t_avm = 0;
73 total.t_rm = 0;
74 total.t_arm = 0;
75 total.t_rq = 0;
76 total.t_dw = 0;
77 total.t_pw = 0;
78 total.t_sl = 0;
79 total.t_sw = 0;
80 total.t_free = vm_page_free_count;
81 for (p = allproc; p != NULL; p = p->p_nxt) {
82 if (p->p_flag & SSYS)
83 continue;
84 vms = p->p_vmspace;
85 total.t_vm = vms->vm_tsize + vms->vm_dsize + vms->vm_ssize;
86 if (p->p_stat) {
87 if (p->p_flag & SLOAD)
88 total.t_avm = vms->vm_tsize + vms->vm_dsize
89 + vms->vm_ssize;
90 switch (p->p_stat) {
91
92 case SSLEEP:
93 if (p->p_pri <= PUSER && p->p_slptime == 0)
94 nrun++;
95 /* fall through */
96 case SSTOP:
97 if (p->p_flag & SPAGE)
98 total.t_pw++;
99 else
100 if (p->p_flag & SLOAD) {
101 if (p->p_pri <= PUSER)
102 total.t_dw++;
103 else if (p->p_slptime < maxslp)
104 total.t_sl++;
105 } else if (p->p_slptime < maxslp)
106 total.t_sw++;
107 if (p->p_slptime < maxslp)
108 goto active;
109 break;
110
111 case SRUN:
112 case SIDL:
113 nrun++;
114 if (p->p_flag & SLOAD)
115 total.t_rq++;
116 else
117 total.t_sw++;
118 active:
119 break;
120 }
121 }
122 }
123 loadav(averunnable, nrun);
124 }
125
126 /*
127 * Constants for averages over 1, 5, and 15 minutes
128 * when sampling at 5 second intervals.
129 */
130 static fixpt_t cexp[3] = {
131 0.9200444146293232 * FSCALE, /* exp(-1/12) */
132 0.9834714538216174 * FSCALE, /* exp(-1/60) */
133 0.9944598480048967 * FSCALE, /* exp(-1/180) */
134 };
135
136 /*
137 * Compute a tenex style load average of a quantity on
138 * 1, 5 and 15 minute intervals.
139 */
140 void
loadav(fixpt_t * avg,int n)141 loadav(fixpt_t *avg, int n)
142 {
143 register int i;
144
145 for (i = 0; i < 3; i++)
146 avg[i] = (cexp[i] * avg[i] + n * FSCALE * (FSCALE - cexp[i]))
147 >> FSHIFT;
148 }
149