sm_io.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2005 J. Martin Petersen
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following
  13. * disclaimer in the documentation and/or other materials provided
  14. * with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  26. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. */
  30. /*
  31. * nr of reads : nr of writes : seeks (zero) : read bytes : written bytes
  32. */
  33. #include "conf.h"
  34. #include <sys/types.h>
  35. #include <sys/param.h>
  36. #include <stdlib.h>
  37. #include <ctype.h>
  38. #include <errno.h>
  39. #include <string.h>
  40. #include <fcntl.h>
  41. #ifdef HAS_RESOURCE_CPUSTATE
  42. #include <sys/resource.h>
  43. #else
  44. #include <sys/dkstat.h>
  45. #endif
  46. #include <devstat.h>
  47. #include "error.h"
  48. #include "symon.h"
  49. static struct statinfo io_stats;
  50. static int io_numdevs = 0;
  51. void
  52. privinit_io()
  53. {
  54. /* EMPTY */
  55. }
  56. void
  57. init_io(struct stream *st)
  58. {
  59. io_stats.dinfo = malloc(sizeof(struct devinfo));
  60. if (io_stats.dinfo == NULL) {
  61. fatal("io: could not allocate memory");
  62. }
  63. io_stats.dinfo->mem_ptr = NULL;
  64. info("started module io(%.200s)", st->arg);
  65. }
  66. void
  67. gets_io()
  68. {
  69. #if DEVSTAT_USER_API_VER >= 5
  70. io_numdevs = devstat_getnumdevs(NULL);
  71. #else
  72. io_numdevs = getnumdevs();
  73. #endif
  74. if (io_numdevs == -1) {
  75. fatal("io: numdevs error");
  76. }
  77. if (io_stats.dinfo->mem_ptr != NULL) {
  78. free(io_stats.dinfo->mem_ptr);
  79. }
  80. /* clear the devinfo struct, as getdevs expects it to be all zeroes */
  81. bzero(io_stats.dinfo, sizeof(struct devinfo));
  82. #if DEVSTAT_USER_API_VER >= 5
  83. devstat_getdevs(NULL, &io_stats);
  84. #else
  85. getdevs(&io_stats);
  86. #endif
  87. }
  88. int
  89. get_io(char *symon_buf, int maxlen, struct stream *st)
  90. {
  91. unsigned int i;
  92. struct devstat *ds;
  93. for (i=0; i < io_numdevs; i++) {
  94. ds = &io_stats.dinfo->devices[i];
  95. if (strncmp(ds->device_name, st->arg, strlen(ds->device_name)) == 0 &&
  96. strlen(ds->device_name) < strlen(st->arg) &&
  97. isdigit(st->arg[strlen(ds->device_name)]) &&
  98. atoi(&st->arg[strlen(ds->device_name)]) == ds->unit_number) {
  99. #if DEVSTAT_USER_API_VER >= 5
  100. return snpack(symon_buf, maxlen, st->arg, MT_IO2,
  101. ds->operations[DEVSTAT_READ],
  102. ds->operations[DEVSTAT_WRITE],
  103. (uint64_t) 0, /* don't know how to find #seeks */
  104. ds->bytes[DEVSTAT_READ],
  105. ds->bytes[DEVSTAT_WRITE]);
  106. #else
  107. return snpack(symon_buf, maxlen, st->arg, MT_IO2,
  108. ds->num_reads,
  109. ds->num_writes,
  110. (uint64_t) 0, /* don't know how to find #seeks */
  111. ds->bytes_read,
  112. ds->bytes_written);
  113. #endif
  114. }
  115. }
  116. return 0;
  117. }