sm_io.c 4.4 KB

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