sm_mbuf.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (c) 2003 Daniel Hartmeier
  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. #include <sys/param.h>
  31. #include <sys/queue.h>
  32. #include <sys/mbuf.h>
  33. #include <sys/sysctl.h>
  34. #include <sys/errno.h>
  35. #include <sys/pool.h>
  36. #include <errno.h>
  37. #include <string.h>
  38. #include <unistd.h>
  39. #include "conf.h"
  40. #include "error.h"
  41. #include "symon.h"
  42. #ifndef HAS_KERN_MBSTAT
  43. void
  44. init_mbuf(struct stream *st)
  45. {
  46. fatal("mbuf module requires system upgrade (sysctl.h/KERN_MBSTAT)");
  47. }
  48. int
  49. get_mbuf(char *symon_buf, int maxlen, struct stream *st)
  50. {
  51. fatal("mbuf module requires system upgrade (sysctl.h/KERN_MBSTAT)");
  52. }
  53. #else
  54. void
  55. init_mbuf(struct stream *st)
  56. {
  57. info("started module mbuf(%.200s)", st->arg);
  58. }
  59. int
  60. get_mbuf(char *symon_buf, int maxlen, struct stream *st)
  61. {
  62. struct mbstat mbstat;
  63. int npools;
  64. struct kinfo_pool pool, mbpool, mclpool;
  65. int mib[4];
  66. size_t size;
  67. int i;
  68. char name[32];
  69. int flag = 0;
  70. int nmbtypes = sizeof(mbstat.m_mtypes) / sizeof(short);
  71. int page_size = getpagesize();
  72. int totmem, totused, totmbufs, totpct;
  73. u_int32_t stats[15];
  74. totmem = totused = 0;
  75. mib[0] = CTL_KERN;
  76. mib[1] = KERN_MBSTAT;
  77. size = sizeof(mbstat);
  78. if (sysctl(mib, 2, &mbstat, &size, NULL, 0) < 0) {
  79. warning("mbuf(%.200s) failed (sysctl() %.200s)", st->arg, strerror(errno));
  80. return 0;
  81. }
  82. mib[0] = CTL_KERN;
  83. mib[1] = KERN_POOL;
  84. mib[2] = KERN_POOL_NPOOLS;
  85. size = sizeof(npools);
  86. if (sysctl(mib, 3, &npools, &size, NULL, 0) < 0) {
  87. warning("mbuf(%.200s) failed (sysctl() %.200s)", st->arg, strerror(errno));
  88. return 0;
  89. }
  90. for (i = 1; npools; ++i) {
  91. mib[0] = CTL_KERN;
  92. mib[1] = KERN_POOL;
  93. mib[2] = KERN_POOL_POOL;
  94. mib[3] = i;
  95. size = sizeof(pool);
  96. if (sysctl(mib, 4, &pool, &size, NULL, 0) < 0) {
  97. warning("mbuf(%.200s) failed (sysctl() %.200s)", st->arg, strerror(errno));
  98. return 0;
  99. }
  100. npools--;
  101. mib[2] = KERN_POOL_NAME;
  102. size = sizeof(name);
  103. if (sysctl(mib, 4, name, &size, NULL, 0) < 0) {
  104. warning("mbuf(%.200s) failed (sysctl() %.200s)", st->arg, strerror(errno));
  105. return (0);
  106. }
  107. if (!strcmp(name, "mbpl") || !strcmp(name, "mbufpl")) {
  108. bcopy(&pool, &mbpool, sizeof(pool));
  109. flag |= (1 << 0);
  110. } else if (!strcmp(name, "mclpl")) {
  111. bcopy(&pool, &mclpool, sizeof(pool));
  112. totmem += mclpool.pr_npages * page_size;
  113. totused += (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
  114. flag |= (1 << 1);
  115. } else if (!strcmp(name, "mcl2k")) {
  116. bcopy(&pool, &mclpool, sizeof(pool));
  117. totmem += mclpool.pr_npages * page_size;
  118. totused += (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
  119. flag |= (1 << 2);
  120. } else if (!strcmp(name, "mcl4k")) {
  121. bcopy(&pool, &mclpool, sizeof(pool));
  122. totmem += mclpool.pr_npages * page_size;
  123. totused += (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
  124. flag |= (1 << 3);
  125. } else if (!strcmp(name, "mcl8k")) {
  126. bcopy(&pool, &mclpool, sizeof(pool));
  127. totmem += mclpool.pr_npages * page_size;
  128. totused += (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
  129. flag |= (1 << 4);
  130. } else if (!strcmp(name, "mcl9k")) {
  131. bcopy(&pool, &mclpool, sizeof(pool));
  132. totmem += mclpool.pr_npages * page_size;
  133. totused += (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
  134. flag |= (1 << 5);
  135. } else if (!strcmp(name, "mcl12k")) {
  136. bcopy(&pool, &mclpool, sizeof(pool));
  137. totmem += mclpool.pr_npages * page_size;
  138. totused += (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
  139. flag |= (1 << 6);
  140. } else if (!strcmp(name, "mcl16k")) {
  141. bcopy(&pool, &mclpool, sizeof(pool));
  142. totmem += mclpool.pr_npages * page_size;
  143. totused += (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
  144. flag |= (1 << 7);
  145. } else if (!strcmp(name, "mcl64k")) {
  146. bcopy(&pool, &mclpool, sizeof(pool));
  147. totmem += mclpool.pr_npages * page_size;
  148. totused += (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
  149. flag |= (1 << 8);
  150. }
  151. if (flag == 3 || flag == 509)
  152. break;
  153. }
  154. /* Check pre/post h2k8 mcpl */
  155. if ((flag != 3) && (flag != 509)) {
  156. warning("mbuf(%.200s) failed (%d)", st->arg, flag);
  157. return 0;
  158. }
  159. totmbufs = 0;
  160. for (i = 0; i < nmbtypes; ++i)
  161. totmbufs += mbstat.m_mtypes[i];
  162. totmem += mbpool.pr_npages * page_size;
  163. totused += (mbpool.pr_nget - mbpool.pr_nput) * mbpool.pr_size;
  164. totpct = (totmem == 0) ? 0 : ((totused * 100) / totmem);
  165. stats[0] = totmbufs;
  166. stats[1] = mbstat.m_mtypes[MT_DATA];
  167. stats[2] = mbstat.m_mtypes[MT_OOBDATA];
  168. stats[3] = mbstat.m_mtypes[MT_CONTROL];
  169. stats[4] = mbstat.m_mtypes[MT_HEADER];
  170. stats[5] = mbstat.m_mtypes[MT_FTABLE];
  171. stats[6] = mbstat.m_mtypes[MT_SONAME];
  172. stats[7] = mbstat.m_mtypes[MT_SOOPTS];
  173. stats[8] = mclpool.pr_nget - mclpool.pr_nput;
  174. stats[9] = mclpool.pr_npages * mclpool.pr_itemsperpage;
  175. stats[10] = totmem;
  176. stats[11] = totpct;
  177. stats[12] = mbstat.m_drops;
  178. stats[13] = mbstat.m_wait;
  179. stats[14] = mbstat.m_drain;
  180. return snpack(symon_buf, maxlen, st->arg, MT_MBUF,
  181. stats[0],
  182. stats[1],
  183. stats[2],
  184. stats[3],
  185. stats[4],
  186. stats[5],
  187. stats[6],
  188. stats[7],
  189. stats[8],
  190. stats[9],
  191. stats[10],
  192. stats[11],
  193. stats[12],
  194. stats[13],
  195. stats[14]);
  196. }
  197. #endif /* HAS_KERN_MBSTAT */