sm_io.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* $Id: sm_io.c,v 1.3 2005/10/18 19:58:09 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2004 Matthew Gream
  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. * Get current disk transfer statistics from kernel and return them in
  33. * symon_buf as
  34. *
  35. * total nr of transfers : total seeks : total bytes transferred
  36. */
  37. #include <sys/param.h>
  38. #include <sys/sysctl.h>
  39. #include <sys/disk.h>
  40. #include <limits.h>
  41. #include <string.h>
  42. #include <stdlib.h>
  43. #include "error.h"
  44. #include "symon.h"
  45. #include "xmalloc.h"
  46. /* Globals for this module start with io_ */
  47. static struct disk_sysctl *io_dkstats = NULL;
  48. static int io_dks = 0;
  49. static int io_maxdks = 0;
  50. void
  51. gets_io()
  52. {
  53. int mib[3];
  54. size_t size;
  55. /* read size */
  56. mib[0] = CTL_HW;
  57. mib[1] = HW_DISKSTATS;
  58. mib[2] = sizeof (struct disk_sysctl);
  59. size = 0;
  60. if (sysctl(mib, 3, NULL, &size, NULL, 0) < 0) {
  61. fatal("%s:%d: io can't get hw.diskstats"
  62. __FILE__, __LINE__);
  63. }
  64. io_dks = size / sizeof (struct disk_sysctl);
  65. /* adjust buffer if necessary */
  66. if (io_dks > io_maxdks) {
  67. io_maxdks = io_dks;
  68. if (io_maxdks > SYMON_MAX_DOBJECTS) {
  69. fatal("%s:%d: dynamic object limit (%d) exceeded for diskstat structures",
  70. __FILE__, __LINE__, SYMON_MAX_DOBJECTS);
  71. }
  72. io_dkstats = xrealloc(io_dkstats, io_maxdks * sizeof(struct disk_sysctl));
  73. }
  74. /* read structure */
  75. size = io_maxdks * sizeof(struct disk_sysctl);
  76. if (sysctl(mib, 3, io_dkstats, &size, NULL, 0) < 0) {
  77. fatal("%s:%d: io can't get hw.diskstats"
  78. __FILE__, __LINE__);
  79. }
  80. }
  81. void
  82. init_io(struct stream *st)
  83. {
  84. info("started module io(%.200s)", st->arg);
  85. }
  86. int
  87. get_io(char *symon_buf, int maxlen, struct stream *st)
  88. {
  89. int i;
  90. for (i = 0; i < io_maxdks; i++)
  91. if (strncmp(io_dkstats[i].dk_name, st->arg,
  92. sizeof(io_dkstats[i].dk_name)) == 0)
  93. return snpack(symon_buf, maxlen, st->arg, MT_IO2,
  94. io_dkstats[i].dk_rxfer,
  95. io_dkstats[i].dk_wxfer,
  96. io_dkstats[i].dk_seek,
  97. io_dkstats[i].dk_rbytes,
  98. io_dkstats[i].dk_wbytes);
  99. return 0;
  100. }