sm_cpu.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2014 Willem Dijkstra
  3. * Copyright (c) 2004 Matthew Gream
  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 cpu statistics in percentages (total of all counts = 100.0)
  33. * and returns them in symon_buf as
  34. *
  35. * user : nice : system : interrupt : idle
  36. *
  37. * This code is not re-entrant.
  38. *
  39. * This module uses the sysctl interface and can run as any user.
  40. */
  41. #include "conf.h"
  42. #include <sys/param.h>
  43. #include <sys/dkstat.h>
  44. #include <sys/sysctl.h>
  45. #include <errno.h>
  46. #include <stdlib.h>
  47. #include <limits.h>
  48. #include "error.h"
  49. #include "percentages.h"
  50. #include "symon.h"
  51. #include "sylimits.h"
  52. #include "xmalloc.h"
  53. /* Globals for this module all start with cp_ */
  54. #ifdef HAS_CP_TIMES
  55. static char cp_time_mib_str[] = "kern.cp_times";
  56. #else
  57. static char cp_time_mib_str[] = "kern.cp_time";
  58. #endif
  59. static int cp_time_mib[CTL_MAXNAME];
  60. static size_t cp_time_len = 0;
  61. static void *cp_buf;
  62. static size_t cp_size = 0;
  63. void
  64. init_cpu(struct stream *st)
  65. {
  66. char buf[SYMON_MAX_OBJSIZE];
  67. const char *errstr;
  68. if (cp_time_len == 0) {
  69. cp_time_len = CTL_MAXNAME;
  70. if (sysctlnametomib(cp_time_mib_str, cp_time_mib, &cp_time_len) < 0) {
  71. warning("sysctlnametomib for cpu failed");
  72. cp_time_len = 0;
  73. }
  74. if ((sysctl(cp_time_mib, cp_time_len, NULL, &cp_size, NULL, 0) != -1) &&
  75. (errno == ENOMEM))
  76. fatal("cpu(%.200s): failed to determine sysctl buffer len", st->arg);
  77. if (cp_size > SYMON_MAX_OBJSIZE)
  78. fatal("cpu(%.200s): sysctl buffer too large", st->arg);
  79. cp_buf = xmalloc(cp_size);
  80. gets_cpu();
  81. }
  82. st->parg.cp.id = strtonum(st->arg, 0, SYMON_MAXCPUID, &errstr);
  83. if (errstr != NULL)
  84. fatal("cpu(%.200s) is invalid: %.200s", st->arg, errstr);
  85. get_cpu(buf, sizeof(buf), st);
  86. info("started module cpu(%.200s)", st->arg);
  87. }
  88. void
  89. gets_cpu()
  90. {
  91. if (sysctl(cp_time_mib, cp_time_len, cp_buf, &cp_size, NULL, 0) < 0) {
  92. warning("%s:%d: sysctl failed", __FILE__, __LINE__);
  93. return;
  94. }
  95. }
  96. int
  97. get_cpu(char *symon_buf, int maxlen, struct stream *st)
  98. {
  99. int i;
  100. long *time1;
  101. if (!cp_time_len)
  102. return 0;
  103. time1 = (long *)(cp_buf + (sizeof(long) * CPUSTATES * st->parg.cp.id));
  104. if (((void *)time1 - cp_buf) > cp_size)
  105. warning("cpu(%d): not in sysctl buffer", st->parg.cp.id);
  106. /* convert cp_time counts to percentages */
  107. for (i = 0; i < CPUSTATES; i++)
  108. st->parg.cp.time2[i] = (int64_t) time1[i];
  109. (void)percentages(CPUSTATES, st->parg.cp.states, st->parg.cp.time2, st->parg.cp.old, st->parg.cp.diff);
  110. return snpack(symon_buf, maxlen, st->arg, MT_CPU,
  111. (double) (st->parg.cp.states[CP_USER] / 10.0),
  112. (double) (st->parg.cp.states[CP_NICE] / 10.0),
  113. (double) (st->parg.cp.states[CP_SYS] / 10.0),
  114. (double) (st->parg.cp.states[CP_INTR] / 10.0),
  115. (double) (st->parg.cp.states[CP_IDLE] / 10.0));
  116. }