sm_cpu.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 cpu statistics in percentages (total of all counts = 100.0)
  32. * and returns them in symon_buf as
  33. *
  34. * user : nice : system : interrupt : idle
  35. */
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #include <fcntl.h>
  39. #include <ctype.h>
  40. #include <errno.h>
  41. #include <inttypes.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <strings.h>
  45. #include <unistd.h>
  46. #include "conf.h"
  47. #include "error.h"
  48. #include "percentages.h"
  49. #include "symon.h"
  50. #include "xmalloc.h"
  51. /* Globals for this module all start with cp_ */
  52. static void *cp_buf = NULL;
  53. static int cp_size = 0;
  54. static int cp_maxsize = 0;
  55. void
  56. init_cpu(struct stream *st)
  57. {
  58. char buf[SYMON_MAX_OBJSIZE];
  59. if (cp_buf == NULL) {
  60. cp_maxsize = SYMON_MAX_OBJSIZE;
  61. cp_buf = xmalloc(cp_maxsize);
  62. }
  63. if (st->arg != NULL && isdigit(*st->arg)) {
  64. snprintf(st->parg.cp.name, sizeof(st->parg.cp.name), "cpu%s", st->arg);
  65. } else {
  66. snprintf(st->parg.cp.name, sizeof(st->parg.cp.name), "cpu");
  67. }
  68. gets_cpu();
  69. get_cpu(buf, sizeof(buf), st);
  70. info("started module cpu(%.200s)", st->arg);
  71. }
  72. void
  73. gets_cpu()
  74. {
  75. int fd;
  76. if ((fd = open("/proc/stat", O_RDONLY)) < 0) {
  77. warning("cannot access /proc/stat: %.200s", strerror(errno));
  78. return;
  79. }
  80. bzero(cp_buf, cp_maxsize);
  81. cp_size = read(fd, cp_buf, cp_maxsize);
  82. close(fd);
  83. if (cp_size == cp_maxsize) {
  84. /* buffer is too small to hold all interface data */
  85. cp_maxsize += SYMON_MAX_OBJSIZE;
  86. if (cp_maxsize > SYMON_MAX_OBJSIZE * SYMON_MAX_DOBJECTS) {
  87. fatal("%s:%d: dynamic object limit (%d) exceeded for cp data",
  88. __FILE__, __LINE__, SYMON_MAX_OBJSIZE * SYMON_MAX_DOBJECTS);
  89. }
  90. cp_buf = xrealloc(cp_buf, cp_maxsize);
  91. gets_cpu();
  92. return;
  93. }
  94. if (cp_size == -1) {
  95. warning("could not read if statistics from /proc/stat: %.200s", strerror(errno));
  96. }
  97. }
  98. int
  99. get_cpu(char *symon_buf, int maxlen, struct stream *st)
  100. {
  101. char *line;
  102. if (cp_size <= 0) {
  103. return 0;
  104. }
  105. if ((line = strstr(cp_buf, st->parg.cp.name)) == NULL) {
  106. warning("could not find %s", st->parg.cp.name);
  107. return 0;
  108. }
  109. line += strlen(st->parg.cp.name);
  110. if (CPUSTATES > sscanf(line, "%" SCNd64 " %" SCNd64 " %" SCNd64 " %" SCNd64 " %" SCNd64 " %" SCNd64 " %" SCNd64 " %" SCNd64 "\n",
  111. &st->parg.cp.time[CP_USER],
  112. &st->parg.cp.time[CP_NICE],
  113. &st->parg.cp.time[CP_SYS],
  114. &st->parg.cp.time[CP_IDLE],
  115. &st->parg.cp.time[CP_IOWAIT],
  116. &st->parg.cp.time[CP_HARDIRQ],
  117. &st->parg.cp.time[CP_SOFTIRQ],
  118. &st->parg.cp.time[CP_STEAL])) {
  119. /* /proc/stat might not support steal */
  120. st->parg.cp.time[CP_STEAL] = 0;
  121. if ((CPUSTATES - 1) > sscanf(line, "%" SCNd64 " %" SCNd64 " %" SCNd64 " %" SCNd64 " %" SCNd64 " %" SCNd64 " %" SCNd64 "\n",
  122. &st->parg.cp.time[CP_USER],
  123. &st->parg.cp.time[CP_NICE],
  124. &st->parg.cp.time[CP_SYS],
  125. &st->parg.cp.time[CP_IDLE],
  126. &st->parg.cp.time[CP_IOWAIT],
  127. &st->parg.cp.time[CP_HARDIRQ],
  128. &st->parg.cp.time[CP_SOFTIRQ])) {
  129. warning("could not parse cpu statistics for %.200s", &st->parg.cp.name);
  130. return 0;
  131. }
  132. }
  133. percentages(CPUSTATES, st->parg.cp.states, st->parg.cp.time,
  134. st->parg.cp.old, st->parg.cp.diff);
  135. return snpack(symon_buf, maxlen, st->arg, MT_CPU,
  136. (double) (st->parg.cp.states[CP_USER] / 10.0),
  137. (double) (st->parg.cp.states[CP_NICE] / 10.0),
  138. (double) (st->parg.cp.states[CP_SYS] / 10.0),
  139. (double) (st->parg.cp.states[CP_IOWAIT] +
  140. st->parg.cp.states[CP_HARDIRQ] +
  141. st->parg.cp.states[CP_SOFTIRQ] +
  142. st->parg.cp.states[CP_STEAL]) / 10.0,
  143. (double) (st->parg.cp.states[CP_IDLE] / 10.0));
  144. }