uvm_meter.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* $OpenBSD: uvm_meter.c,v 1.36 2015/03/14 03:38:53 jsg Exp $ */
  2. /* $NetBSD: uvm_meter.c,v 1.21 2001/07/14 06:36:03 matt Exp $ */
  3. /*
  4. * Copyright (c) 1997 Charles D. Cranor and Washington University.
  5. * Copyright (c) 1982, 1986, 1989, 1993
  6. * The Regents of the University of California.
  7. *
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the University nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * @(#)vm_meter.c 8.4 (Berkeley) 1/4/94
  35. * from: Id: uvm_meter.c,v 1.1.2.1 1997/08/14 19:10:35 chuck Exp
  36. */
  37. #include <sys/param.h>
  38. #include <sys/systm.h>
  39. #include <sys/kernel.h>
  40. #include <sys/proc.h>
  41. #include <sys/sysctl.h>
  42. #include <sys/vmmeter.h>
  43. #include <uvm/uvm.h>
  44. #ifdef UVM_SWAP_ENCRYPT
  45. #include <uvm/uvm_swap.h>
  46. #include <uvm/uvm_swap_encrypt.h>
  47. #endif
  48. /*
  49. * The time for a process to be blocked before being very swappable.
  50. * This is a number of seconds which the system takes as being a non-trivial
  51. * amount of real time. You probably shouldn't change this;
  52. * it is used in subtle ways (fractions and multiples of it are, that is, like
  53. * half of a ``long time'', almost a long time, etc.)
  54. * It is related to human patience and other factors which don't really
  55. * change over time.
  56. */
  57. #define MAXSLP 20
  58. int maxslp = MAXSLP; /* patchable ... */
  59. struct loadavg averunnable;
  60. /*
  61. * constants for averages over 1, 5, and 15 minutes when sampling at
  62. * 5 second intervals.
  63. */
  64. static fixpt_t cexp[3] = {
  65. 0.9200444146293232 * FSCALE, /* exp(-1/12) */
  66. 0.9834714538216174 * FSCALE, /* exp(-1/60) */
  67. 0.9944598480048967 * FSCALE, /* exp(-1/180) */
  68. };
  69. static void uvm_loadav(struct loadavg *);
  70. void uvm_total(struct vmtotal *);
  71. /*
  72. * uvm_meter: calculate load average and wake up the swapper (if needed)
  73. */
  74. void
  75. uvm_meter(void)
  76. {
  77. if ((time_second % 5) == 0)
  78. uvm_loadav(&averunnable);
  79. if (proc0.p_slptime > (maxslp / 2))
  80. wakeup(&proc0);
  81. }
  82. /*
  83. * uvm_loadav: compute a tenex style load average of a quantity on
  84. * 1, 5, and 15 minute intervals.
  85. */
  86. static void
  87. uvm_loadav(struct loadavg *avg)
  88. {
  89. CPU_INFO_ITERATOR cii;
  90. struct cpu_info *ci;
  91. int i, nrun;
  92. struct proc *p;
  93. int nrun_cpu[MAXCPUS];
  94. nrun = 0;
  95. memset(nrun_cpu, 0, sizeof(nrun_cpu));
  96. LIST_FOREACH(p, &allproc, p_list) {
  97. switch (p->p_stat) {
  98. case SSLEEP:
  99. if (p->p_priority > PZERO || p->p_slptime > 1)
  100. continue;
  101. /* FALLTHROUGH */
  102. case SRUN:
  103. case SONPROC:
  104. if (p == p->p_cpu->ci_schedstate.spc_idleproc)
  105. continue;
  106. case SIDL:
  107. nrun++;
  108. if (p->p_cpu)
  109. nrun_cpu[CPU_INFO_UNIT(p->p_cpu)]++;
  110. }
  111. }
  112. for (i = 0; i < 3; i++) {
  113. avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
  114. nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
  115. }
  116. CPU_INFO_FOREACH(cii, ci) {
  117. struct schedstate_percpu *spc = &ci->ci_schedstate;
  118. if (nrun_cpu[CPU_INFO_UNIT(ci)] == 0)
  119. continue;
  120. spc->spc_ldavg = (cexp[0] * spc->spc_ldavg +
  121. nrun_cpu[CPU_INFO_UNIT(ci)] * FSCALE *
  122. (FSCALE - cexp[0])) >> FSHIFT;
  123. }
  124. }
  125. /*
  126. * uvm_sysctl: sysctl hook into UVM system.
  127. */
  128. int
  129. uvm_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
  130. size_t newlen, struct proc *p)
  131. {
  132. struct process *pr = p->p_p;
  133. struct vmtotal vmtotals;
  134. int rv, t;
  135. switch (name[0]) {
  136. case VM_SWAPENCRYPT:
  137. #ifdef UVM_SWAP_ENCRYPT
  138. return (swap_encrypt_ctl(name + 1, namelen - 1, oldp, oldlenp,
  139. newp, newlen, p));
  140. #else
  141. return (EOPNOTSUPP);
  142. #endif
  143. default:
  144. /* all sysctl names at this level are terminal */
  145. if (namelen != 1)
  146. return (ENOTDIR); /* overloaded */
  147. break;
  148. }
  149. switch (name[0]) {
  150. case VM_LOADAVG:
  151. return (sysctl_rdstruct(oldp, oldlenp, newp, &averunnable,
  152. sizeof(averunnable)));
  153. case VM_METER:
  154. uvm_total(&vmtotals);
  155. return (sysctl_rdstruct(oldp, oldlenp, newp, &vmtotals,
  156. sizeof(vmtotals)));
  157. case VM_UVMEXP:
  158. return (sysctl_rdstruct(oldp, oldlenp, newp, &uvmexp,
  159. sizeof(uvmexp)));
  160. case VM_NKMEMPAGES:
  161. return (sysctl_rdint(oldp, oldlenp, newp, nkmempages));
  162. case VM_PSSTRINGS:
  163. return (sysctl_rdstruct(oldp, oldlenp, newp, &pr->ps_strings,
  164. sizeof(pr->ps_strings)));
  165. case VM_ANONMIN:
  166. t = uvmexp.anonminpct;
  167. rv = sysctl_int(oldp, oldlenp, newp, newlen, &t);
  168. if (rv) {
  169. return rv;
  170. }
  171. if (t + uvmexp.vtextminpct + uvmexp.vnodeminpct > 95 || t < 0) {
  172. return EINVAL;
  173. }
  174. uvmexp.anonminpct = t;
  175. uvmexp.anonmin = t * 256 / 100;
  176. return rv;
  177. case VM_VTEXTMIN:
  178. t = uvmexp.vtextminpct;
  179. rv = sysctl_int(oldp, oldlenp, newp, newlen, &t);
  180. if (rv) {
  181. return rv;
  182. }
  183. if (uvmexp.anonminpct + t + uvmexp.vnodeminpct > 95 || t < 0) {
  184. return EINVAL;
  185. }
  186. uvmexp.vtextminpct = t;
  187. uvmexp.vtextmin = t * 256 / 100;
  188. return rv;
  189. case VM_VNODEMIN:
  190. t = uvmexp.vnodeminpct;
  191. rv = sysctl_int(oldp, oldlenp, newp, newlen, &t);
  192. if (rv) {
  193. return rv;
  194. }
  195. if (uvmexp.anonminpct + uvmexp.vtextminpct + t > 95 || t < 0) {
  196. return EINVAL;
  197. }
  198. uvmexp.vnodeminpct = t;
  199. uvmexp.vnodemin = t * 256 / 100;
  200. return rv;
  201. case VM_MAXSLP:
  202. return (sysctl_rdint(oldp, oldlenp, newp, maxslp));
  203. case VM_USPACE:
  204. return (sysctl_rdint(oldp, oldlenp, newp, USPACE));
  205. default:
  206. return (EOPNOTSUPP);
  207. }
  208. /* NOTREACHED */
  209. }
  210. /*
  211. * uvm_total: calculate the current state of the system.
  212. */
  213. void
  214. uvm_total(struct vmtotal *totalp)
  215. {
  216. struct proc *p;
  217. #if 0
  218. struct vm_map_entry * entry;
  219. struct vm_map *map;
  220. int paging;
  221. #endif
  222. memset(totalp, 0, sizeof *totalp);
  223. /* calculate process statistics */
  224. LIST_FOREACH(p, &allproc, p_list) {
  225. if (p->p_flag & P_SYSTEM)
  226. continue;
  227. switch (p->p_stat) {
  228. case 0:
  229. continue;
  230. case SSLEEP:
  231. case SSTOP:
  232. if (p->p_priority <= PZERO)
  233. totalp->t_dw++;
  234. else if (p->p_slptime < maxslp)
  235. totalp->t_sl++;
  236. if (p->p_slptime >= maxslp)
  237. continue;
  238. break;
  239. case SRUN:
  240. case SIDL:
  241. case SONPROC:
  242. totalp->t_rq++;
  243. if (p->p_stat == SIDL)
  244. continue;
  245. break;
  246. }
  247. /*
  248. * note active objects
  249. */
  250. #if 0
  251. /*
  252. * XXXCDC: BOGUS! rethink this. in the mean time
  253. * don't do it.
  254. */
  255. paging = 0;
  256. vm_map_lock(map);
  257. for (map = &p->p_vmspace->vm_map, entry = map->header.next;
  258. entry != &map->header; entry = entry->next) {
  259. if (entry->is_a_map || entry->is_sub_map ||
  260. entry->object.uvm_obj == NULL)
  261. continue;
  262. /* XXX how to do this with uvm */
  263. }
  264. vm_map_unlock(map);
  265. if (paging)
  266. totalp->t_pw++;
  267. #endif
  268. }
  269. /*
  270. * Calculate object memory usage statistics.
  271. */
  272. totalp->t_free = uvmexp.free;
  273. totalp->t_vm = uvmexp.npages - uvmexp.free + uvmexp.swpginuse;
  274. totalp->t_avm = uvmexp.active + uvmexp.swpginuse; /* XXX */
  275. totalp->t_rm = uvmexp.npages - uvmexp.free;
  276. totalp->t_arm = uvmexp.active;
  277. totalp->t_vmshr = 0; /* XXX */
  278. totalp->t_avmshr = 0; /* XXX */
  279. totalp->t_rmshr = 0; /* XXX */
  280. totalp->t_armshr = 0; /* XXX */
  281. }