sm_mem.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2005-2007 Harm Schotanus
  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 <errno.h>
  36. #include <fcntl.h>
  37. #include <inttypes.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <unistd.h>
  41. #include "error.h"
  42. #include "conf.h"
  43. #include "symon.h"
  44. #include "xmalloc.h"
  45. #define ktob(size) ((size) << 10)
  46. /* Globals for this module all start with me_ */
  47. static void *me_buf = NULL;
  48. static u_int64_t me_size = 0;
  49. static u_int64_t me_maxsize = 0;
  50. static u_int64_t me_stats[5];
  51. void
  52. init_mem(struct stream *st)
  53. {
  54. if (me_buf == NULL) {
  55. me_maxsize = SYMON_MAX_OBJSIZE;
  56. me_buf = xmalloc(me_maxsize);
  57. }
  58. info("started module mem(%.200s)", st->arg);
  59. }
  60. void
  61. gets_mem()
  62. {
  63. int fd;
  64. if ((fd = open("/proc/meminfo", O_RDONLY)) < 0) {
  65. warning("cannot access /proc/meminfo: %.200s", strerror(errno));
  66. }
  67. bzero(me_buf, me_maxsize);
  68. me_size = read(fd, me_buf, me_maxsize);
  69. close(fd);
  70. if (me_size == me_maxsize) {
  71. /* buffer is too small to hold all memory data */
  72. me_maxsize += SYMON_MAX_OBJSIZE;
  73. if (me_maxsize > SYMON_MAX_OBJSIZE * SYMON_MAX_DOBJECTS) {
  74. fatal("%s:%d: dynamic object limit (%d) exceeded for cp data",
  75. __FILE__, __LINE__, SYMON_MAX_OBJSIZE * SYMON_MAX_DOBJECTS);
  76. }
  77. me_buf = xrealloc(me_buf, me_maxsize);
  78. gets_mem();
  79. return;
  80. }
  81. if (me_size == -1) {
  82. warning("could not read if statistics from /proc/meminfo: %.200s", strerror(errno));
  83. }
  84. }
  85. u_int64_t
  86. mem_getitem(char *name)
  87. {
  88. u_int64_t stat;
  89. char *line;
  90. if (me_size <= 0) {
  91. return 0;
  92. }
  93. if ((line = strstr(me_buf, name)) == NULL) {
  94. warning("could not find %s in /proc/meminfo", name);
  95. return 0;
  96. }
  97. line += strlen(name);
  98. if (1 < sscanf(line, ": %" SCNu64 " Kb", &stat)) {
  99. warning("could not parse memory statistics");
  100. return 0;
  101. } else {
  102. return stat;
  103. }
  104. }
  105. int
  106. get_mem(char *symon_buf, int maxlen, struct stream *st)
  107. {
  108. me_stats[0] = ktob(mem_getitem("Active"));
  109. me_stats[1] = ktob(mem_getitem("MemTotal"));
  110. me_stats[2] = ktob(mem_getitem("MemFree"));
  111. me_stats[1] -= me_stats[2];
  112. me_stats[3] = ktob(mem_getitem("SwapFree"));
  113. me_stats[4] = ktob(mem_getitem("SwapTotal"));
  114. me_stats[3] = me_stats[4] - me_stats[3];
  115. return snpack(symon_buf, maxlen, st->arg, MT_MEM2,
  116. me_stats[0], me_stats[1], me_stats[2],
  117. me_stats[3], me_stats[4]);
  118. }