sm_mem.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* $Id: sm_mem.c,v 1.4 2005/03/20 16:17:22 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2004 Matthew Gream
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * - Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following
  14. * disclaimer in the documentation and/or other materials provided
  15. * with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. /*
  32. * Get current memory statistics in bytes; reports them back in symon_buf as
  33. *
  34. * real active : real total : free : [swap used : swap total]
  35. *
  36. * This code is not re-entrant.
  37. */
  38. #include "conf.h"
  39. #include <sys/param.h>
  40. #include <sys/sysctl.h>
  41. #include <sys/vmmeter.h>
  42. #include <vm/vm_param.h>
  43. #include <unistd.h>
  44. #include <stdlib.h>
  45. #include <stdio.h>
  46. #include <string.h>
  47. #include <limits.h>
  48. #include "error.h"
  49. #include "symon.h"
  50. #include "xmalloc.h"
  51. #define pagetob(size) ((size) << me_pageshift)
  52. /* Globals for this module all start with me_ */
  53. static int me_pagesize;
  54. static int me_pageshift;
  55. static char me_vmnswp_mib_str[] = "vm.nswapdev";
  56. static int me_vmnswp_mib_nam[CTL_MAXNAME];
  57. static size_t me_vmnswp_mib_len = 0;
  58. static char me_vmiswp_mib_str[] = "vm.swap_info";
  59. static int me_vmiswp_mib_nam[CTL_MAXNAME];
  60. static size_t me_vmiswp_mib_len = 0;
  61. static int me_vm_mib[] = {CTL_VM, VM_TOTAL};
  62. static long me_stats[5];
  63. static struct vmtotal me_vmtotal;
  64. static size_t me_vmsize = sizeof(struct vmtotal);
  65. /* Prepare mem module for first use */
  66. void
  67. init_mem(char *s)
  68. {
  69. me_pagesize = sysconf(_SC_PAGESIZE);
  70. me_pageshift = 0;
  71. while (me_pagesize > 1) {
  72. me_pageshift++;
  73. me_pagesize >>= 1;
  74. }
  75. me_vmnswp_mib_len = CTL_MAXNAME;
  76. if (sysctlnametomib(me_vmnswp_mib_str, me_vmnswp_mib_nam, &me_vmnswp_mib_len) < 0) {
  77. warning("sysctlnametomib for nswapdev failed");
  78. me_vmnswp_mib_len = 0;
  79. }
  80. me_vmiswp_mib_len = CTL_MAXNAME;
  81. if (sysctlnametomib(me_vmiswp_mib_str, me_vmiswp_mib_nam, &me_vmiswp_mib_len) < 0) {
  82. warning("sysctlnametomib for swap_info failed");
  83. me_vmiswp_mib_len = 0;
  84. }
  85. info("started module mem(%.200s)", s);
  86. }
  87. /* Get memory statistics */
  88. int
  89. get_mem(char *symon_buf, int maxlen, char *s)
  90. {
  91. #ifdef HAS_XSWDEV
  92. int i;
  93. int vmnswp_dat, vmnswp_siz;
  94. #endif
  95. if (sysctl(me_vm_mib, 2, &me_vmtotal, &me_vmsize, NULL, 0) < 0) {
  96. warning("%s:%d: sysctl failed", __FILE__, __LINE__);
  97. bzero(&me_vmtotal, sizeof(me_vmtotal));
  98. }
  99. /* convert memory stats to Kbytes */
  100. me_stats[0] = pagetob(me_vmtotal.t_arm);
  101. me_stats[1] = pagetob(me_vmtotal.t_rm);
  102. me_stats[2] = pagetob(me_vmtotal.t_free);
  103. #ifdef HAS_XSWDEV
  104. vmnswp_siz = sizeof (int);
  105. if (sysctl(me_vmnswp_mib_nam, me_vmnswp_mib_len, &vmnswp_dat, (void *)&vmnswp_siz, NULL, 0) < 0) {
  106. warning("%s:%d: sysctl nswapdev failed", __FILE__, __LINE__);
  107. vmnswp_dat = 0;
  108. }
  109. for (i = 0; i < vmnswp_dat; i++) {
  110. struct xswdev vmiswp_dat;
  111. int vmiswp_siz;
  112. me_vmiswp_mib_nam[me_vmiswp_mib_len] = i;
  113. if (sysctl(me_vmiswp_mib_nam, me_vmiswp_mib_len + 1, &vmiswp_dat, (void *)&vmiswp_siz, NULL, 0) < 0)
  114. continue;
  115. me_stats[3] += (vmiswp_dat.xsw_used * DEV_BSIZE);
  116. me_stats[4] += (vmiswp_dat.xsw_nblks * DEV_BSIZE);
  117. }
  118. #else
  119. me_stats[3] = me_stats[4] = 0;
  120. #endif
  121. return snpack(symon_buf, maxlen, s, MT_MEM,
  122. me_stats[0], me_stats[1], me_stats[2],
  123. me_stats[3], me_stats[4]);
  124. }