sm_mem.c 3.9 KB

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