sm_io.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* $Id: sm_io.c,v 1.5 2007/07/05 13:33:26 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2007 Willem Dijkstra
  4. * Copyright (c) 2004 Matthew Gream
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * - Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * - Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  22. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  24. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  28. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. */
  32. /*
  33. * Get current disk transfer statistics from kernel and return them in
  34. * symon_buf as
  35. *
  36. * total nr of transfers : total seeks : total bytes transferred
  37. */
  38. #include "conf.h"
  39. #include <sys/param.h>
  40. #include <sys/sysctl.h>
  41. #include <sys/disk.h>
  42. #ifdef HAS_HW_IOSTATS
  43. #include <sys/iostat.h>
  44. #endif
  45. #include <limits.h>
  46. #include <string.h>
  47. #include <stdlib.h>
  48. #include "error.h"
  49. #include "symon.h"
  50. #include "xmalloc.h"
  51. /* Globals for this module start with io_ */
  52. static int io_dks = 0;
  53. static int io_maxdks = 0;
  54. #ifndef HAS_HW_IOSTATS
  55. static struct disk_sysctl *io_dkstats = NULL;
  56. void
  57. gets_io()
  58. {
  59. int mib[3];
  60. size_t size;
  61. /* read size */
  62. mib[0] = CTL_HW;
  63. mib[1] = HW_DISKSTATS;
  64. mib[2] = sizeof (struct disk_sysctl);
  65. size = 0;
  66. if (sysctl(mib, 3, NULL, &size, NULL, 0) < 0) {
  67. fatal("%s:%d: io can't get hw.diskstats"
  68. __FILE__, __LINE__);
  69. }
  70. io_dks = size / sizeof (struct disk_sysctl);
  71. /* adjust buffer if necessary */
  72. if (io_dks > io_maxdks) {
  73. io_maxdks = io_dks;
  74. if (io_maxdks > SYMON_MAX_DOBJECTS) {
  75. fatal("%s:%d: dynamic object limit (%d) exceeded for diskstat structures",
  76. __FILE__, __LINE__, SYMON_MAX_DOBJECTS);
  77. }
  78. io_dkstats = xrealloc(io_dkstats, io_maxdks * sizeof(struct disk_sysctl));
  79. }
  80. /* read structure */
  81. size = io_maxdks * sizeof(struct disk_sysctl);
  82. if (sysctl(mib, 3, io_dkstats, &size, NULL, 0) < 0) {
  83. fatal("%s:%d: io can't get hw.diskstats"
  84. __FILE__, __LINE__);
  85. }
  86. }
  87. #else
  88. static struct io_sysctl *io_dkstats = NULL;
  89. void
  90. gets_io()
  91. {
  92. int mib[3];
  93. size_t size;
  94. /* read size */
  95. mib[0] = CTL_HW;
  96. mib[1] = HW_IOSTATS;
  97. mib[2] = sizeof(struct io_sysctl);
  98. size = 0;
  99. if (sysctl(mib, 3, NULL, &size, NULL, 0) < 0) {
  100. fatal("%s:%d: io can't get hw.iostats"
  101. __FILE__, __LINE__);
  102. }
  103. io_dks = size / sizeof (struct io_sysctl);
  104. /* adjust buffer if necessary */
  105. if (io_dks > io_maxdks) {
  106. io_maxdks = io_dks;
  107. if (io_maxdks > SYMON_MAX_DOBJECTS) {
  108. fatal("%s:%d: dynamic object limit (%d) exceeded for diskstat structures",
  109. __FILE__, __LINE__, SYMON_MAX_DOBJECTS);
  110. }
  111. io_dkstats = xrealloc(io_dkstats, io_maxdks * sizeof(struct io_sysctl));
  112. }
  113. /* read structure */
  114. size = io_maxdks * sizeof(struct io_sysctl);
  115. if (sysctl(mib, 3, io_dkstats, &size, NULL, 0) < 0) {
  116. fatal("%s:%d: io can't get hw.iostats"
  117. __FILE__, __LINE__);
  118. }
  119. }
  120. #endif
  121. void
  122. init_io(struct stream *st)
  123. {
  124. info("started module io(%.200s)", st->arg);
  125. }
  126. int
  127. get_io(char *symon_buf, int maxlen, struct stream *st)
  128. {
  129. int i;
  130. #ifndef HAS_HW_IOSTATS
  131. for (i = 0; i < io_maxdks; i++)
  132. if (strncmp(io_dkstats[i].dk_name, st->arg,
  133. sizeof(io_dkstats[i].dk_name)) == 0)
  134. return snpack(symon_buf, maxlen, st->arg, MT_IO2,
  135. io_dkstats[i].dk_rxfer,
  136. io_dkstats[i].dk_wxfer,
  137. io_dkstats[i].dk_seek,
  138. io_dkstats[i].dk_rbytes,
  139. io_dkstats[i].dk_wbytes);
  140. #else
  141. for (i = 0; i < io_maxdks; i++)
  142. if (strncmp(io_dkstats[i].name, st->arg,
  143. sizeof(io_dkstats[i].name)) == 0)
  144. return snpack(symon_buf, maxlen, st->arg, MT_IO2,
  145. io_dkstats[i].rxfer,
  146. io_dkstats[i].wxfer,
  147. io_dkstats[i].seek,
  148. io_dkstats[i].rbytes,
  149. io_dkstats[i].wbytes);
  150. #endif
  151. return 0;
  152. }