sm_io.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "diskname.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. struct disknamectx c;
  62. unsigned int i;
  63. struct devstat *ds;
  64. char drivename[MAX_PATH_LEN];
  65. if (st->arg == NULL)
  66. fatal("io: need a <device>|<devicename> argument");
  67. io_stats.dinfo = malloc(sizeof(struct devinfo));
  68. if (io_stats.dinfo == NULL) {
  69. fatal("io: could not allocate memory");
  70. }
  71. io_stats.dinfo->mem_ptr = NULL;
  72. initdisknamectx(&c, st->arg, drivename, MAX_PATH_LEN);
  73. gets_io();
  74. while (nextdiskname(&c)) {
  75. for (i = 0; i < io_numdevs; i++) {
  76. ds = &io_stats.dinfo->devices[i];
  77. if (strncmp(ds->device_name, drivename, strlen(ds->device_name)) == 0 &&
  78. strlen(ds->device_name) < strlen(drivename) &&
  79. isdigit(st->arg[strlen(ds->device_name)]) &&
  80. atoi(&drivename[strlen(ds->device_name)]) == ds->unit_number) {
  81. if (strcmp(st->arg, drivename) == 0)
  82. info("started module io(%.200s)", st->arg);
  83. else
  84. info("started module io(%.200s = %.200s)", st->arg, drivename);
  85. return;
  86. }
  87. }
  88. }
  89. warning("io(%.200s): not found", st->arg);
  90. }
  91. void
  92. gets_io()
  93. {
  94. #if DEVSTAT_USER_API_VER >= 5
  95. io_numdevs = devstat_getnumdevs(NULL);
  96. #else
  97. io_numdevs = getnumdevs();
  98. #endif
  99. if (io_numdevs == -1) {
  100. fatal("io: numdevs error");
  101. }
  102. if (io_stats.dinfo->mem_ptr != NULL) {
  103. free(io_stats.dinfo->mem_ptr);
  104. }
  105. /* clear the devinfo struct, as getdevs expects it to be all zeroes */
  106. bzero(io_stats.dinfo, sizeof(struct devinfo));
  107. #if DEVSTAT_USER_API_VER >= 5
  108. devstat_getdevs(NULL, &io_stats);
  109. #else
  110. getdevs(&io_stats);
  111. #endif
  112. }
  113. int
  114. get_io(char *symon_buf, int maxlen, struct stream *st)
  115. {
  116. unsigned int i;
  117. struct devstat *ds;
  118. for (i = 0; i < io_numdevs; i++) {
  119. ds = &io_stats.dinfo->devices[i];
  120. if (strncmp(ds->device_name, st->arg, strlen(ds->device_name)) == 0 &&
  121. strlen(ds->device_name) < strlen(st->arg) &&
  122. isdigit(st->arg[strlen(ds->device_name)]) &&
  123. atoi(&st->arg[strlen(ds->device_name)]) == ds->unit_number) {
  124. #if DEVSTAT_USER_API_VER >= 5
  125. return snpack(symon_buf, maxlen, st->arg, MT_IO2,
  126. ds->operations[DEVSTAT_READ],
  127. ds->operations[DEVSTAT_WRITE],
  128. (uint64_t) 0, /* don't know how to find #seeks */
  129. ds->bytes[DEVSTAT_READ],
  130. ds->bytes[DEVSTAT_WRITE]);
  131. #else
  132. return snpack(symon_buf, maxlen, st->arg, MT_IO2,
  133. ds->num_reads,
  134. ds->num_writes,
  135. (uint64_t) 0, /* don't know how to find #seeks */
  136. ds->bytes_read,
  137. ds->bytes_written);
  138. #endif
  139. }
  140. }
  141. return 0;
  142. }