sm_mem.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* $Id: sm_mem.c,v 1.4 2008/02/20 08:17:25 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2008 Willem Dijkstra
  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. #include <sys/param.h>
  37. #include <sys/sysctl.h>
  38. #include <sys/swap.h>
  39. #include <unistd.h>
  40. #include <stdlib.h>
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include <limits.h>
  44. #include "error.h"
  45. #include "symon.h"
  46. #include "xmalloc.h"
  47. #define pagetob(size) (((u_int64_t)size) << me_pageshift)
  48. /* Globals for this module all start with me_ */
  49. static u_int64_t me_pageshift;
  50. static u_int64_t me_stats[5];
  51. static int me_vm_mib[] = {CTL_VM, VM_METER};
  52. static struct vmtotal me_vmtotal;
  53. static size_t me_vmsize;
  54. static u_int64_t me_pagesize;
  55. static u_int64_t me_nswap;
  56. struct swapent *me_swdev = NULL;
  57. void
  58. init_mem(struct stream *st)
  59. {
  60. me_pagesize = sysconf(_SC_PAGESIZE);
  61. me_pageshift = 0;
  62. while (me_pagesize > 1) {
  63. me_pageshift++;
  64. me_pagesize >>= 1;
  65. }
  66. /* get total -- systemwide main memory usage structure */
  67. me_vmsize = sizeof(me_vmtotal);
  68. /* determine number of swap entries */
  69. me_nswap = swapctl(SWAP_NSWAP, 0, 0);
  70. if (me_swdev) {
  71. xfree(me_swdev);
  72. }
  73. if (me_nswap != 0) {
  74. me_swdev = xmalloc(me_nswap * sizeof(*me_swdev));
  75. } else {
  76. me_swdev = NULL;
  77. }
  78. if (me_swdev == NULL && me_nswap != 0) {
  79. me_nswap = 0;
  80. }
  81. if (st != NULL) {
  82. info("started module mem(%.200s)", st->arg);
  83. }
  84. }
  85. void
  86. gets_mem()
  87. {
  88. int i, rnswap;
  89. if (sysctl(me_vm_mib, 2, &me_vmtotal, &me_vmsize, NULL, 0) < 0) {
  90. warning("%s:%d: sysctl failed", __FILE__, __LINE__);
  91. bzero(&me_vmtotal, sizeof(me_vmtotal));
  92. }
  93. /* convert memory stats to Kbytes */
  94. me_stats[0] = pagetob(me_vmtotal.t_arm);
  95. me_stats[1] = pagetob(me_vmtotal.t_rm);
  96. me_stats[2] = pagetob(me_vmtotal.t_free);
  97. rnswap = swapctl(SWAP_STATS, me_swdev, me_nswap);
  98. if (rnswap == -1) {
  99. /* A swap device may have been added; increase and retry */
  100. init_mem(NULL);
  101. rnswap = swapctl(SWAP_STATS, me_swdev, me_nswap);
  102. }
  103. me_stats[3] = me_stats[4] = 0;
  104. if (rnswap == me_nswap) { /* Read swap successfully */
  105. /* Total things up */
  106. for (i = 0; i < me_nswap; i++) {
  107. if (me_swdev[i].se_flags & SWF_ENABLE) {
  108. me_stats[3] += (me_swdev[i].se_inuse * DEV_BSIZE);
  109. me_stats[4] += (me_swdev[i].se_nblks * DEV_BSIZE);
  110. }
  111. }
  112. }
  113. }
  114. int
  115. get_mem(char *symon_buf, int maxlen, struct stream *st)
  116. {
  117. return snpack(symon_buf, maxlen, st->arg, MT_MEM2,
  118. me_stats[0], me_stats[1], me_stats[2],
  119. me_stats[3], me_stats[4]);
  120. }