sm_io.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (c) 2007-2012 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 disk transfer statistics from kernel and return them in
  33. * symon_buf as
  34. *
  35. * total nr of transfers : total seeks : total bytes transferred
  36. */
  37. #include "conf.h"
  38. #include <sys/param.h>
  39. #include <sys/sysctl.h>
  40. #include <sys/disk.h>
  41. #ifdef HAS_HW_IOSTATS
  42. #include <sys/iostat.h>
  43. #endif
  44. #include <limits.h>
  45. #include <string.h>
  46. #include <stdlib.h>
  47. #include "error.h"
  48. #include "symon.h"
  49. #include "xmalloc.h"
  50. /* Globals for this module start with io_ */
  51. static int io_dks = 0;
  52. static int io_maxdks = 0;
  53. #ifndef HAS_HW_IOSTATS
  54. static struct disk_sysctl *io_dkstats = NULL;
  55. void
  56. gets_io()
  57. {
  58. int mib[3];
  59. size_t size;
  60. /* read size */
  61. mib[0] = CTL_HW;
  62. mib[1] = HW_DISKSTATS;
  63. mib[2] = sizeof (struct disk_sysctl);
  64. size = 0;
  65. if (sysctl(mib, 3, NULL, &size, NULL, 0) < 0) {
  66. fatal("%s:%d: io can't get hw.diskstats"
  67. __FILE__, __LINE__);
  68. }
  69. io_dks = size / sizeof (struct disk_sysctl);
  70. /* adjust buffer if necessary */
  71. if (io_dks > io_maxdks) {
  72. io_maxdks = io_dks;
  73. if (io_maxdks > SYMON_MAX_DOBJECTS) {
  74. fatal("%s:%d: dynamic object limit (%d) exceeded for diskstat structures",
  75. __FILE__, __LINE__, SYMON_MAX_DOBJECTS);
  76. }
  77. io_dkstats = xrealloc(io_dkstats, io_maxdks * sizeof(struct disk_sysctl));
  78. }
  79. /* read structure */
  80. size = io_maxdks * sizeof(struct disk_sysctl);
  81. if (sysctl(mib, 3, io_dkstats, &size, NULL, 0) < 0) {
  82. fatal("%s:%d: io can't get hw.diskstats"
  83. __FILE__, __LINE__);
  84. }
  85. }
  86. #else
  87. static struct io_sysctl *io_dkstats = NULL;
  88. void
  89. gets_io()
  90. {
  91. int mib[3];
  92. size_t size;
  93. /* read size */
  94. mib[0] = CTL_HW;
  95. mib[1] = HW_IOSTATS;
  96. mib[2] = sizeof(struct io_sysctl);
  97. size = 0;
  98. if (sysctl(mib, 3, NULL, &size, NULL, 0) < 0) {
  99. fatal("%s:%d: io can't get hw.iostats"
  100. __FILE__, __LINE__);
  101. }
  102. io_dks = size / sizeof (struct io_sysctl);
  103. /* adjust buffer if necessary */
  104. if (io_dks > io_maxdks) {
  105. io_maxdks = io_dks;
  106. if (io_maxdks > SYMON_MAX_DOBJECTS) {
  107. fatal("%s:%d: dynamic object limit (%d) exceeded for diskstat structures",
  108. __FILE__, __LINE__, SYMON_MAX_DOBJECTS);
  109. }
  110. io_dkstats = xrealloc(io_dkstats, io_maxdks * sizeof(struct io_sysctl));
  111. }
  112. /* read structure */
  113. size = io_maxdks * sizeof(struct io_sysctl);
  114. if (sysctl(mib, 3, io_dkstats, &size, NULL, 0) < 0) {
  115. fatal("%s:%d: io can't get hw.iostats"
  116. __FILE__, __LINE__);
  117. }
  118. }
  119. #endif
  120. void
  121. init_io(struct stream *st)
  122. {
  123. info("started module io(%.200s)", st->arg);
  124. }
  125. int
  126. get_io(char *symon_buf, int maxlen, struct stream *st)
  127. {
  128. int i;
  129. #ifndef HAS_HW_IOSTATS
  130. for (i = 0; i < io_maxdks; i++)
  131. if (strncmp(io_dkstats[i].dk_name, st->arg,
  132. sizeof(io_dkstats[i].dk_name)) == 0)
  133. return snpack(symon_buf, maxlen, st->arg, MT_IO2,
  134. io_dkstats[i].dk_rxfer,
  135. io_dkstats[i].dk_wxfer,
  136. io_dkstats[i].dk_seek,
  137. io_dkstats[i].dk_rbytes,
  138. io_dkstats[i].dk_wbytes);
  139. #else
  140. for (i = 0; i < io_maxdks; i++)
  141. if (strncmp(io_dkstats[i].name, st->arg,
  142. sizeof(io_dkstats[i].name)) == 0)
  143. return snpack(symon_buf, maxlen, st->arg, MT_IO2,
  144. io_dkstats[i].rxfer,
  145. io_dkstats[i].wxfer,
  146. io_dkstats[i].seek,
  147. io_dkstats[i].rbytes,
  148. io_dkstats[i].wbytes);
  149. #endif
  150. return 0;
  151. }