sm_io.c 4.5 KB

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