sm_mbuf.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/mbuf.h>
  32. #include <sys/sysctl.h>
  33. #include <sys/errno.h>
  34. #include <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. totmem = totused = 0;
  73. mib[0] = CTL_KERN;
  74. mib[1] = KERN_MBSTAT;
  75. size = sizeof(mbstat);
  76. if (sysctl(mib, 2, &mbstat, &size, NULL, 0) < 0) {
  77. warning("mbuf(%.200s) failed (sysctl() %.200s)", st->arg, strerror(errno));
  78. return 0;
  79. }
  80. mib[0] = CTL_KERN;
  81. mib[1] = KERN_POOL;
  82. mib[2] = KERN_POOL_NPOOLS;
  83. size = sizeof(npools);
  84. if (sysctl(mib, 3, &npools, &size, NULL, 0) < 0) {
  85. warning("mbuf(%.200s) failed (sysctl() %.200s)", st->arg, strerror(errno));
  86. return 0;
  87. }
  88. for (i = 1; npools; ++i) {
  89. mib[0] = CTL_KERN;
  90. mib[1] = KERN_POOL;
  91. mib[2] = KERN_POOL_POOL;
  92. mib[3] = i;
  93. size = sizeof(pool);
  94. if (sysctl(mib, 4, &pool, &size, NULL, 0) < 0) {
  95. warning("mbuf(%.200s) failed (sysctl() %.200s)", st->arg, strerror(errno));
  96. return 0;
  97. }
  98. npools--;
  99. mib[2] = KERN_POOL_NAME;
  100. size = sizeof(name);
  101. if (sysctl(mib, 4, name, &size, NULL, 0) < 0) {
  102. warning("mbuf(%.200s) failed (sysctl() %.200s)", st->arg, strerror(errno));
  103. return (0);
  104. }
  105. if (!strcmp(name, "mbpl")) {
  106. bcopy(&pool, &mbpool, sizeof(pool));
  107. flag |= (1 << 0);
  108. } else if (!strcmp(name, "mclpl")) {
  109. bcopy(&pool, &mclpool, sizeof(pool));
  110. totmem += mclpool.pr_npages * page_size;
  111. totused += (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
  112. flag |= (1 << 1);
  113. } else if (!strcmp(name, "mcl2k")) {
  114. bcopy(&pool, &mclpool, sizeof(pool));
  115. totmem += mclpool.pr_npages * page_size;
  116. totused += (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
  117. flag |= (1 << 2);
  118. } else if (!strcmp(name, "mcl4k")) {
  119. bcopy(&pool, &mclpool, sizeof(pool));
  120. totmem += mclpool.pr_npages * page_size;
  121. totused += (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
  122. flag |= (1 << 3);
  123. } else if (!strcmp(name, "mcl8k")) {
  124. bcopy(&pool, &mclpool, sizeof(pool));
  125. totmem += mclpool.pr_npages * page_size;
  126. totused += (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
  127. flag |= (1 << 4);
  128. } else if (!strcmp(name, "mcl9k")) {
  129. bcopy(&pool, &mclpool, sizeof(pool));
  130. totmem += mclpool.pr_npages * page_size;
  131. totused += (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
  132. flag |= (1 << 5);
  133. } else if (!strcmp(name, "mcl12k")) {
  134. bcopy(&pool, &mclpool, sizeof(pool));
  135. totmem += mclpool.pr_npages * page_size;
  136. totused += (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
  137. flag |= (1 << 6);
  138. } else if (!strcmp(name, "mcl16k")) {
  139. bcopy(&pool, &mclpool, sizeof(pool));
  140. totmem += mclpool.pr_npages * page_size;
  141. totused += (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
  142. flag |= (1 << 7);
  143. } else if (!strcmp(name, "mcl64k")) {
  144. bcopy(&pool, &mclpool, sizeof(pool));
  145. totmem += mclpool.pr_npages * page_size;
  146. totused += (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
  147. flag |= (1 << 8);
  148. }
  149. if (flag == 3 || flag == 509)
  150. break;
  151. }
  152. /* Check pre/post h2k8 mcpl */
  153. if ((flag != 3) && (flag != 509)) {
  154. warning("mbuf(%.200s) failed (%d)", st->arg, flag);
  155. return 0;
  156. }
  157. totmbufs = 0;
  158. for (i = 0; i < nmbtypes; ++i)
  159. totmbufs += mbstat.m_mtypes[i];
  160. totmem += mbpool.pr_npages * page_size;
  161. totused += (mbpool.pr_nget - mbpool.pr_nput) * mbpool.pr_size;
  162. totpct = (totmem == 0) ? 0 : ((totused * 100) / totmem);
  163. stats[0] = totmbufs;
  164. stats[1] = mbstat.m_mtypes[MT_DATA];
  165. stats[2] = mbstat.m_mtypes[MT_OOBDATA];
  166. stats[3] = mbstat.m_mtypes[MT_CONTROL];
  167. stats[4] = mbstat.m_mtypes[MT_HEADER];
  168. stats[5] = mbstat.m_mtypes[MT_FTABLE];
  169. stats[6] = mbstat.m_mtypes[MT_SONAME];
  170. stats[7] = mbstat.m_mtypes[MT_SOOPTS];
  171. stats[8] = mclpool.pr_nget - mclpool.pr_nput;
  172. stats[9] = mclpool.pr_npages * mclpool.pr_itemsperpage;
  173. stats[10] = totmem;
  174. stats[11] = totpct;
  175. stats[12] = mbstat.m_drops;
  176. stats[13] = mbstat.m_wait;
  177. stats[14] = mbstat.m_drain;
  178. return snpack(symon_buf, maxlen, st->arg, MT_MBUF,
  179. stats[0],
  180. stats[1],
  181. stats[2],
  182. stats[3],
  183. stats[4],
  184. stats[5],
  185. stats[6],
  186. stats[7],
  187. stats[8],
  188. stats[9],
  189. stats[10],
  190. stats[11],
  191. stats[12],
  192. stats[13],
  193. stats[14]);
  194. }
  195. #endif /* HAS_KERN_MBSTAT */