sm_io.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 2001-2004 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 disk transfer statistics from kernel and return them in
  32. * symon_buf as
  33. *
  34. * total nr of transfers : total seeks : total bytes transferred
  35. */
  36. #include <sys/param.h>
  37. #include <sys/sysctl.h>
  38. #include <sys/disk.h>
  39. #include <limits.h>
  40. #include <string.h>
  41. #include "conf.h"
  42. #include "error.h"
  43. #include "symon.h"
  44. #include "xmalloc.h"
  45. /* Globals for this module start with io_ */
  46. static char *io_dkstr = NULL;
  47. static struct diskstats *io_dkstats = NULL;
  48. static char **io_dknames = NULL;
  49. static int io_dks = 0;
  50. static int io_maxdks = 0;
  51. static size_t io_maxstr = 0;
  52. void
  53. gets_io()
  54. {
  55. int mib[3];
  56. char *p;
  57. int dks;
  58. size_t size;
  59. size_t strsize;
  60. /* how much memory is needed */
  61. mib[0] = CTL_HW;
  62. mib[1] = HW_DISKCOUNT;
  63. size = sizeof(dks);
  64. if (sysctl(mib, 2, &dks, &size, NULL, 0) < 0) {
  65. fatal("%s:%d: sysctl failed: can't get hw.diskcount",
  66. __FILE__, __LINE__);
  67. }
  68. mib[0] = CTL_HW;
  69. mib[1] = HW_DISKNAMES;
  70. strsize = 0;
  71. if (sysctl(mib, 2, NULL, &strsize, NULL, 0) < 0) {
  72. fatal("%s:%d: sysctl failed: can't get hw.disknames",
  73. __FILE__, __LINE__);
  74. }
  75. /* increase buffers if necessary */
  76. if (dks > io_maxdks || strsize > io_maxstr) {
  77. io_maxdks = dks;
  78. io_maxstr = strsize;
  79. if (io_maxdks > SYMON_MAX_DOBJECTS) {
  80. fatal("%s:%d: dynamic object limit (%d) exceeded for diskstat structures",
  81. __FILE__, __LINE__, SYMON_MAX_DOBJECTS);
  82. }
  83. if (io_maxstr > SYMON_MAX_OBJSIZE) {
  84. fatal("%s:%d: string size exceeded (%d)",
  85. __FILE__, __LINE__, SYMON_MAX_OBJSIZE);
  86. }
  87. io_dkstats = xrealloc(io_dkstats, io_maxdks * sizeof(struct diskstats));
  88. io_dknames = xrealloc(io_dknames, io_maxdks * sizeof(char *));
  89. io_dkstr = xrealloc(io_dkstr, io_maxstr + 1);
  90. }
  91. /* read data in anger */
  92. mib[0] = CTL_HW;
  93. mib[1] = HW_DISKNAMES;
  94. if (sysctl(mib, 2, io_dkstr, &io_maxstr, NULL, 0) < 0) {
  95. fatal("%s:%d: io can't get hw.disknames"
  96. __FILE__, __LINE__);
  97. }
  98. io_dkstr[io_maxstr] = '\0';
  99. mib[0] = CTL_HW;
  100. mib[1] = HW_DISKSTATS;
  101. size = io_maxdks * sizeof(struct diskstats);
  102. if (sysctl(mib, 2, io_dkstats, &size, NULL, 0) < 0) {
  103. fatal("%s:%d: io can't get hw.diskstats"
  104. __FILE__, __LINE__);
  105. }
  106. p = io_dkstr;
  107. io_dks = 0;
  108. io_dknames[io_dks] = p;
  109. while ((*p != '\0') && ((p - io_dkstr) < io_maxstr)) {
  110. if ((*p == ',') && (*p+1 != '\0')) {
  111. *p = '\0';
  112. io_dks++; p++;
  113. io_dknames[io_dks] = p;
  114. }
  115. p++;
  116. }
  117. }
  118. void
  119. init_io(struct stream *st)
  120. {
  121. info("started module io(%.200s)", st->arg);
  122. }
  123. int
  124. get_io(char *symon_buf, int maxlen, struct stream *st)
  125. {
  126. int i;
  127. /* look for disk */
  128. for (i = 0; i <= io_dks; i++) {
  129. if (strncmp(io_dknames[i], st->arg,
  130. (io_dkstr + io_maxstr - io_dknames[i])) == 0)
  131. #ifdef HAS_IO2
  132. return snpack(symon_buf, maxlen, st->arg, MT_IO2,
  133. io_dkstats[i].ds_rxfer,
  134. io_dkstats[i].ds_wxfer,
  135. io_dkstats[i].ds_seek,
  136. io_dkstats[i].ds_rbytes,
  137. io_dkstats[i].ds_wbytes);
  138. #else
  139. return snpack(symon_buf, maxlen, st->arg, MT_IO1,
  140. io_dkstats[i].ds_xfer,
  141. io_dkstats[i].ds_seek,
  142. io_dkstats[i].ds_bytes);
  143. #endif
  144. }
  145. return 0;
  146. }