sm_io.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* $Id: sm_io.c,v 1.1 2005/03/20 16:17:22 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. * Non re-entrant code
  35. */
  36. #include "conf.h"
  37. #include <sys/types.h>
  38. #include <sys/param.h>
  39. #include <stdlib.h>
  40. #include <ctype.h>
  41. #include <errno.h>
  42. #include <string.h>
  43. #include <fcntl.h>
  44. #ifdef HAS_RESOURCE_CPUSTATE
  45. #include <sys/resource.h>
  46. #else
  47. #include <sys/dkstat.h>
  48. #endif
  49. #include <devstat.h>
  50. #include "error.h"
  51. #include "symon.h"
  52. static struct statinfo io_stats;
  53. static int io_numdevs = 0;
  54. void
  55. privinit_io()
  56. {
  57. }
  58. void
  59. init_io(char *s)
  60. {
  61. io_stats.dinfo = malloc(sizeof(struct devinfo));
  62. if (io_stats.dinfo == NULL) {
  63. fatal("io: could not allocate memory");
  64. }
  65. io_stats.dinfo->mem_ptr = NULL;
  66. info("started module io(%.200s)",s);
  67. }
  68. void
  69. gets_io()
  70. {
  71. #if DEVSTAT_USER_API_VER == 5
  72. io_numdevs = devstat_getnumdevs(NULL);
  73. #else
  74. io_numdevs = getnumdevs();
  75. #endif
  76. if (io_numdevs == -1) {
  77. fatal("io: numdevs error");
  78. }
  79. if (io_stats.dinfo->mem_ptr != NULL) {
  80. free(io_stats.dinfo->mem_ptr);
  81. }
  82. /* clear the devinfo struct, as getdevs expects it to be all zeroes */
  83. bzero(io_stats.dinfo, sizeof(struct devinfo));
  84. #if DEVSTAT_USER_API_VER == 5
  85. devstat_getdevs(NULL, &io_stats);
  86. #else
  87. getdevs(&io_stats);
  88. #endif
  89. }
  90. int
  91. get_io(char *symon_buf, int maxlen, char *dev)
  92. {
  93. unsigned int i;
  94. struct devstat *ds;
  95. for (i=0; i < io_numdevs; i++) {
  96. ds = &io_stats.dinfo->devices[i];
  97. if (strncmp(ds->device_name, dev, strlen(ds->device_name)) == 0 &&
  98. strlen(ds->device_name) < strlen(dev) &&
  99. isdigit(dev[strlen(ds->device_name)]) &&
  100. atoi(&dev[strlen(ds->device_name)]) == ds->unit_number) {
  101. #if DEVSTAT_USER_API_VER == 5
  102. return snpack(symon_buf, maxlen, dev, MT_IO2,
  103. ds->operations[DEVSTAT_READ],
  104. ds->operations[DEVSTAT_WRITE],
  105. (uint64_t) 0, /* don't know how to find #seeks */
  106. ds->bytes[DEVSTAT_READ],
  107. ds->bytes[DEVSTAT_WRITE]);
  108. #else
  109. return snpack(symon_buf, maxlen, dev, MT_IO2,
  110. ds->num_reads,
  111. ds->num_writes,
  112. (uint64_t) 0, /* don't know how to find #seeks */
  113. ds->bytes_read,
  114. ds->bytes_written);
  115. #endif
  116. }
  117. }
  118. return 0;
  119. }