sm_io.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* $Id: sm_io.c,v 1.3 2005/10/18 19:58:06 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2005 J. Martin Petersen
  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. static struct statinfo io_stats;
  51. static int io_numdevs = 0;
  52. void
  53. privinit_io()
  54. {
  55. /* EMPTY */
  56. }
  57. void
  58. init_io(struct stream *st)
  59. {
  60. io_stats.dinfo = malloc(sizeof(struct devinfo));
  61. if (io_stats.dinfo == NULL) {
  62. fatal("io: could not allocate memory");
  63. }
  64. io_stats.dinfo->mem_ptr = NULL;
  65. info("started module io(%.200s)", st->arg);
  66. }
  67. void
  68. gets_io()
  69. {
  70. #if DEVSTAT_USER_API_VER == 5
  71. io_numdevs = devstat_getnumdevs(NULL);
  72. #else
  73. io_numdevs = getnumdevs();
  74. #endif
  75. if (io_numdevs == -1) {
  76. fatal("io: numdevs error");
  77. }
  78. if (io_stats.dinfo->mem_ptr != NULL) {
  79. free(io_stats.dinfo->mem_ptr);
  80. }
  81. /* clear the devinfo struct, as getdevs expects it to be all zeroes */
  82. bzero(io_stats.dinfo, sizeof(struct devinfo));
  83. #if DEVSTAT_USER_API_VER == 5
  84. devstat_getdevs(NULL, &io_stats);
  85. #else
  86. getdevs(&io_stats);
  87. #endif
  88. }
  89. int
  90. get_io(char *symon_buf, int maxlen, struct stream *st)
  91. {
  92. unsigned int i;
  93. struct devstat *ds;
  94. for (i=0; i < io_numdevs; i++) {
  95. ds = &io_stats.dinfo->devices[i];
  96. if (strncmp(ds->device_name, st->arg, strlen(ds->device_name)) == 0 &&
  97. strlen(ds->device_name) < strlen(st->arg) &&
  98. isdigit(st->arg[strlen(ds->device_name)]) &&
  99. atoi(&st->arg[strlen(ds->device_name)]) == ds->unit_number) {
  100. #if DEVSTAT_USER_API_VER == 5
  101. return snpack(symon_buf, maxlen, st->arg, MT_IO2,
  102. ds->operations[DEVSTAT_READ],
  103. ds->operations[DEVSTAT_WRITE],
  104. (uint64_t) 0, /* don't know how to find #seeks */
  105. ds->bytes[DEVSTAT_READ],
  106. ds->bytes[DEVSTAT_WRITE]);
  107. #else
  108. return snpack(symon_buf, maxlen, st->arg, MT_IO2,
  109. ds->num_reads,
  110. ds->num_writes,
  111. (uint64_t) 0, /* don't know how to find #seeks */
  112. ds->bytes_read,
  113. ds->bytes_written);
  114. #endif
  115. }
  116. }
  117. return 0;
  118. }