sm_mem.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2001-2008 Willem Dijkstra
  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. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following
  13. * disclaimer in the documentation and/or other materials provided
  14. * with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  26. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. */
  30. /*
  31. * Get current memory statistics in bytes; reports them back in symon_buf as
  32. *
  33. * real active : real total : free : [swap used : swap total]
  34. */
  35. #include <sys/param.h>
  36. #include <sys/sysctl.h>
  37. #include <sys/swap.h>
  38. #include <unistd.h>
  39. #include <stdlib.h>
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <limits.h>
  43. #include "error.h"
  44. #include "symon.h"
  45. #include "xmalloc.h"
  46. #define pagetob(size) (((u_int64_t)size) << me_pageshift)
  47. /* Globals for this module all start with me_ */
  48. static u_int64_t me_pageshift;
  49. static u_int64_t me_stats[5];
  50. static int me_vm_mib[] = {CTL_VM, VM_METER};
  51. static struct vmtotal me_vmtotal;
  52. static size_t me_vmsize;
  53. static u_int64_t me_pagesize;
  54. static u_int64_t me_nswap;
  55. struct swapent *me_swdev = NULL;
  56. void
  57. init_mem(struct stream *st)
  58. {
  59. me_pagesize = sysconf(_SC_PAGESIZE);
  60. me_pageshift = 0;
  61. while (me_pagesize > 1) {
  62. me_pageshift++;
  63. me_pagesize >>= 1;
  64. }
  65. /* get total -- systemwide main memory usage structure */
  66. me_vmsize = sizeof(me_vmtotal);
  67. /* determine number of swap entries */
  68. me_nswap = swapctl(SWAP_NSWAP, 0, 0);
  69. if (me_swdev) {
  70. xfree(me_swdev);
  71. }
  72. if (me_nswap != 0) {
  73. me_swdev = xmalloc(me_nswap * sizeof(*me_swdev));
  74. } else {
  75. me_swdev = NULL;
  76. }
  77. if (me_swdev == NULL && me_nswap != 0) {
  78. me_nswap = 0;
  79. }
  80. if (st != NULL) {
  81. info("started module mem(%.200s)", st->arg);
  82. }
  83. }
  84. void
  85. gets_mem()
  86. {
  87. int i, rnswap;
  88. if (sysctl(me_vm_mib, 2, &me_vmtotal, &me_vmsize, NULL, 0) < 0) {
  89. warning("%s:%d: sysctl failed", __FILE__, __LINE__);
  90. bzero(&me_vmtotal, sizeof(me_vmtotal));
  91. }
  92. /* convert memory stats to Kbytes */
  93. me_stats[0] = pagetob(me_vmtotal.t_arm);
  94. me_stats[1] = pagetob(me_vmtotal.t_rm);
  95. me_stats[2] = pagetob(me_vmtotal.t_free);
  96. rnswap = swapctl(SWAP_STATS, me_swdev, me_nswap);
  97. if (rnswap == -1) {
  98. /* A swap device may have been added; increase and retry */
  99. init_mem(NULL);
  100. rnswap = swapctl(SWAP_STATS, me_swdev, me_nswap);
  101. }
  102. me_stats[3] = me_stats[4] = 0;
  103. if (rnswap == me_nswap) { /* Read swap successfully */
  104. /* Total things up */
  105. for (i = 0; i < me_nswap; i++) {
  106. if (me_swdev[i].se_flags & SWF_ENABLE) {
  107. me_stats[3] += (me_swdev[i].se_inuse * DEV_BSIZE);
  108. me_stats[4] += (me_swdev[i].se_nblks * DEV_BSIZE);
  109. }
  110. }
  111. }
  112. }
  113. int
  114. get_mem(char *symon_buf, int maxlen, struct stream *st)
  115. {
  116. return snpack(symon_buf, maxlen, st->arg, MT_MEM2,
  117. me_stats[0], me_stats[1], me_stats[2],
  118. me_stats[3], me_stats[4]);
  119. }