sm_mem.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* $Id: sm_mem.c,v 1.7 2008/01/30 14:29:31 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2005-2007 Harm Schotanus
  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 <stdlib.h>
  37. #include <string.h>
  38. #include <errno.h>
  39. #include <fcntl.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, ": %llu 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. }