sm_io.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 2005 J. Martin Petersen
  3. * Copyright (c) 2012 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. * nr of reads : nr of writes : seeks (zero) : read bytes : written bytes
  33. */
  34. #include "conf.h"
  35. #include <sys/types.h>
  36. #include <sys/param.h>
  37. #include <stdlib.h>
  38. #include <ctype.h>
  39. #include <errno.h>
  40. #include <string.h>
  41. #include <fcntl.h>
  42. #ifdef HAS_RESOURCE_CPUSTATE
  43. #include <sys/resource.h>
  44. #else
  45. #include <sys/dkstat.h>
  46. #endif
  47. #include <devstat.h>
  48. #include "error.h"
  49. #include "symon.h"
  50. #include "diskbyname.h"
  51. static struct statinfo io_stats;
  52. static int io_numdevs = 0;
  53. void
  54. privinit_io()
  55. {
  56. /* EMPTY */
  57. }
  58. void
  59. init_io(struct stream *st)
  60. {
  61. unsigned int i;
  62. struct devstat *ds;
  63. char drivename[MAX_PATH_LEN];
  64. if (st->arg == NULL)
  65. fatal("io: need a <device>|<devicename> argument");
  66. if (diskbyname(st->arg, drivename, MAX_PATH_LEN) == 0)
  67. fatal("io: '%.200s' is not a <device>|<devicename>", st->arg);
  68. io_stats.dinfo = malloc(sizeof(struct devinfo));
  69. if (io_stats.dinfo == NULL) {
  70. fatal("io: could not allocate memory");
  71. }
  72. io_stats.dinfo->mem_ptr = NULL;
  73. gets_io();
  74. for (i = 0; i < io_numdevs; i++) {
  75. ds = &io_stats.dinfo->devices[i];
  76. if (strncmp(ds->device_name, st->arg, strlen(ds->device_name)) == 0 &&
  77. strlen(ds->device_name) < strlen(st->arg) &&
  78. isdigit(st->arg[strlen(ds->device_name)]) &&
  79. atoi(&st->arg[strlen(ds->device_name)]) == ds->unit_number) {
  80. info("started module io(%.200s)", st->arg);
  81. return;
  82. }
  83. }
  84. warning("io(%.200s): not found", st->arg);
  85. }
  86. void
  87. gets_io()
  88. {
  89. #if DEVSTAT_USER_API_VER >= 5
  90. io_numdevs = devstat_getnumdevs(NULL);
  91. #else
  92. io_numdevs = getnumdevs();
  93. #endif
  94. if (io_numdevs == -1) {
  95. fatal("io: numdevs error");
  96. }
  97. if (io_stats.dinfo->mem_ptr != NULL) {
  98. free(io_stats.dinfo->mem_ptr);
  99. }
  100. /* clear the devinfo struct, as getdevs expects it to be all zeroes */
  101. bzero(io_stats.dinfo, sizeof(struct devinfo));
  102. #if DEVSTAT_USER_API_VER >= 5
  103. devstat_getdevs(NULL, &io_stats);
  104. #else
  105. getdevs(&io_stats);
  106. #endif
  107. }
  108. int
  109. get_io(char *symon_buf, int maxlen, struct stream *st)
  110. {
  111. unsigned int i;
  112. struct devstat *ds;
  113. for (i = 0; i < io_numdevs; i++) {
  114. ds = &io_stats.dinfo->devices[i];
  115. if (strncmp(ds->device_name, st->arg, strlen(ds->device_name)) == 0 &&
  116. strlen(ds->device_name) < strlen(st->arg) &&
  117. isdigit(st->arg[strlen(ds->device_name)]) &&
  118. atoi(&st->arg[strlen(ds->device_name)]) == ds->unit_number) {
  119. #if DEVSTAT_USER_API_VER >= 5
  120. return snpack(symon_buf, maxlen, st->arg, MT_IO2,
  121. ds->operations[DEVSTAT_READ],
  122. ds->operations[DEVSTAT_WRITE],
  123. (uint64_t) 0, /* don't know how to find #seeks */
  124. ds->bytes[DEVSTAT_READ],
  125. ds->bytes[DEVSTAT_WRITE]);
  126. #else
  127. return snpack(symon_buf, maxlen, st->arg, MT_IO2,
  128. ds->num_reads,
  129. ds->num_writes,
  130. (uint64_t) 0, /* don't know how to find #seeks */
  131. ds->bytes_read,
  132. ds->bytes_written);
  133. #endif
  134. }
  135. }
  136. return 0;
  137. }