sm_mbuf.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* $Id: sm_mbuf.c,v 1.6 2005/10/18 19:58:11 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2003 Daniel Hartmeier
  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. #include <sys/param.h>
  32. #include <sys/mbuf.h>
  33. #include <sys/sysctl.h>
  34. #include <sys/errno.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. #include "conf.h"
  38. #include "error.h"
  39. #include "symon.h"
  40. #ifndef HAS_KERN_MBSTAT
  41. void
  42. init_mbuf(struct stream *st)
  43. {
  44. fatal("mbuf module requires system upgrade (sysctl.h/KERN_MBSTAT)");
  45. }
  46. int
  47. get_mbuf(char *symon_buf, int maxlen, struct stream *st)
  48. {
  49. fatal("mbuf module requires system upgrade (sysctl.h/KERN_MBSTAT)");
  50. }
  51. #else
  52. void
  53. init_mbuf(struct stream *st)
  54. {
  55. info("started module mbuf(%.200s)", st->arg);
  56. }
  57. int
  58. get_mbuf(char *symon_buf, int maxlen, struct stream *st)
  59. {
  60. struct mbstat mbstat;
  61. int npools;
  62. struct pool pool, mbpool, mclpool;
  63. int mib[4];
  64. size_t size;
  65. int i;
  66. char name[32];
  67. int flag = 0;
  68. int nmbtypes = sizeof(mbstat.m_mtypes) / sizeof(short);
  69. int page_size = getpagesize();
  70. int totmem, totused, totmbufs, totpct;
  71. u_int32_t stats[15];
  72. mib[0] = CTL_KERN;
  73. mib[1] = KERN_MBSTAT;
  74. size = sizeof(mbstat);
  75. if (sysctl(mib, 2, &mbstat, &size, NULL, 0) < 0) {
  76. warning("mbuf(%.200s) failed (sysctl() %.200s)", st->arg, strerror(errno));
  77. return 0;
  78. }
  79. mib[0] = CTL_KERN;
  80. mib[1] = KERN_POOL;
  81. mib[2] = KERN_POOL_NPOOLS;
  82. size = sizeof(npools);
  83. if (sysctl(mib, 3, &npools, &size, NULL, 0) < 0) {
  84. warning("mbuf(%.200s) failed (sysctl() %.200s)", st->arg, strerror(errno));
  85. return 0;
  86. }
  87. for (i = 1; npools; ++i) {
  88. mib[0] = CTL_KERN;
  89. mib[1] = KERN_POOL;
  90. mib[2] = KERN_POOL_POOL;
  91. mib[3] = i;
  92. size = sizeof(pool);
  93. if (sysctl(mib, 4, &pool, &size, NULL, 0) < 0) {
  94. warning("mbuf(%.200s) failed (sysctl() %.200s)", st->arg, strerror(errno));
  95. return 0;
  96. }
  97. npools--;
  98. mib[2] = KERN_POOL_NAME;
  99. size = sizeof(name);
  100. if (sysctl(mib, 4, name, &size, NULL, 0) < 0) {
  101. warning("mbuf(%.200s) failed (sysctl() %.200s)", st->arg, strerror(errno));
  102. return (0);
  103. }
  104. if (!strcmp(name, "mbpl")) {
  105. bcopy(&pool, &mbpool, sizeof(pool));
  106. flag |= 1;
  107. } else if (!strcmp(name, "mclpl")) {
  108. bcopy(&pool, &mclpool, sizeof(pool));
  109. flag |= 2;
  110. }
  111. if (flag == 3)
  112. break;
  113. }
  114. if (flag != 3) {
  115. warning("mbuf(%.200s) failed (flag != 3)", st->arg);
  116. return 0;
  117. }
  118. totmbufs = 0;
  119. for (i = 0; i < nmbtypes; ++i)
  120. totmbufs += mbstat.m_mtypes[i];
  121. totmem = (mbpool.pr_npages + mclpool.pr_npages) * page_size;
  122. totused = (mbpool.pr_nget - mbpool.pr_nput) * mbpool.pr_size +
  123. (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
  124. totpct = (totmem == 0) ? 0 : ((totused * 100) / totmem);
  125. stats[0] = totmbufs;
  126. stats[1] = mbstat.m_mtypes[MT_DATA];
  127. stats[2] = mbstat.m_mtypes[MT_OOBDATA];
  128. stats[3] = mbstat.m_mtypes[MT_CONTROL];
  129. stats[4] = mbstat.m_mtypes[MT_HEADER];
  130. stats[5] = mbstat.m_mtypes[MT_FTABLE];
  131. stats[6] = mbstat.m_mtypes[MT_SONAME];
  132. stats[7] = mbstat.m_mtypes[MT_SOOPTS];
  133. stats[8] = mclpool.pr_nget - mclpool.pr_nput;
  134. stats[9] = mclpool.pr_npages * mclpool.pr_itemsperpage;
  135. stats[10] = totmem;
  136. stats[11] = totpct;
  137. stats[12] = mbstat.m_drops;
  138. stats[13] = mbstat.m_wait;
  139. stats[14] = mbstat.m_drain;
  140. return snpack(symon_buf, maxlen, st->arg, MT_MBUF,
  141. stats[0],
  142. stats[1],
  143. stats[2],
  144. stats[3],
  145. stats[4],
  146. stats[5],
  147. stats[6],
  148. stats[7],
  149. stats[8],
  150. stats[9],
  151. stats[10],
  152. stats[11],
  153. stats[12],
  154. stats[13],
  155. stats[14]);
  156. }
  157. #endif /* HAS_KERN_MBSTAT */