sm_cpu.c 5.3 KB

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