sm_mbuf.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* $Id: sm_mbuf.c,v 1.5 2005/10/18 19:58:06 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2004 Matthew Gream
  4. * Copyright (c) 2003 Daniel Hartmeier
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * - Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * - Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  22. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  24. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  28. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. */
  32. #include <sys/param.h>
  33. #include <sys/mbuf.h>
  34. #include <sys/sysctl.h>
  35. #include <errno.h>
  36. #include <string.h>
  37. #include <unistd.h>
  38. #include "error.h"
  39. #include "symon.h"
  40. static char mbstat_mib_str[] = "kern.ipc.mbstat";
  41. static int mbstat_mib[CTL_MAXNAME];
  42. static size_t mbstat_len = 0;
  43. void
  44. init_mbuf(struct stream *st)
  45. {
  46. mbstat_len = CTL_MAXNAME;
  47. if (sysctlnametomib(mbstat_mib_str, mbstat_mib, &mbstat_len) < 0) {
  48. warning("sysctlnametomib for mbuf failed");
  49. mbstat_len = 0;
  50. }
  51. info("started module mbuf(%.200s)", st->arg);
  52. }
  53. int
  54. get_mbuf(char *symon_buf, int maxlen, struct stream *st)
  55. {
  56. struct mbstat mbstat;
  57. #ifdef KERN_POOL
  58. int npools;
  59. struct pool pool, mbpool, mclpool;
  60. char name[32];
  61. int flag = 0;
  62. int page_size = getpagesize();
  63. #endif
  64. size_t size;
  65. int totmem, totused, totmbufs, totpct;
  66. u_int32_t stats[15];
  67. if (!mbstat_len)
  68. return 0;
  69. size = sizeof(mbstat);
  70. if (sysctl(mbstat_mib, mbstat_len, &mbstat, &size, NULL, 0) < 0) {
  71. warning("mbuf(%.200s) failed (sysctl() %.200s)", st->arg, strerror(errno));
  72. return (0);
  73. }
  74. #ifdef KERN_POOL
  75. mib[0] = CTL_KERN;
  76. mib[1] = KERN_POOL;
  77. mib[2] = KERN_POOL_NPOOLS;
  78. size = sizeof(npools);
  79. if (sysctl(mib, 3, &npools, &size, NULL, 0) < 0) {
  80. warning("mbuf(%.200s) failed (sysctl() %.200s)", st->arg, strerror(errno));
  81. return (0);
  82. }
  83. for (i = 1; npools; ++i) {
  84. mib[0] = CTL_KERN;
  85. mib[1] = KERN_POOL;
  86. mib[2] = KERN_POOL_POOL;
  87. mib[3] = i;
  88. size = sizeof(pool);
  89. if (sysctl(mib, 4, &pool, &size, NULL, 0) < 0) {
  90. warning("mbuf(%.200s) failed (sysctl() %.200s)", st->arg, strerror(errno));
  91. return (0);
  92. }
  93. npools--;
  94. mib[2] = KERN_POOL_NAME;
  95. size = sizeof(name);
  96. if (sysctl(mib, 4, name, &size, NULL, 0) < 0) {
  97. warning("mbuf(%.200s) failed (sysctl() %.200s)", st->arg, strerror(errno));
  98. return (0);
  99. }
  100. if (!strcmp(name, "mbpl")) {
  101. bcopy(&pool, &mbpool, sizeof(pool));
  102. flag |= 1;
  103. } else if (!strcmp(name, "mclpl")) {
  104. bcopy(&pool, &mclpool, sizeof(pool));
  105. flag |= 2;
  106. }
  107. if (flag == 3)
  108. break;
  109. }
  110. if (flag != 3) {
  111. warning("mbuf(%.200s) failed (flag != 3)", st->arg);
  112. return (0);
  113. }
  114. #endif
  115. totmbufs = 0; /* XXX: collect mb_statpcpu and add together */
  116. #ifdef KERN_POOL
  117. totmem = (mbpool.pr_npages + mclpool.pr_npages) * page_size;
  118. totused = (mbpool.pr_nget - mbpool.pr_nput) * mbpool.pr_size +
  119. (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
  120. #else
  121. totmem = 0;
  122. totused = 0;
  123. #endif
  124. totpct = (totmem == 0) ? 0 : ((totused * 100) / totmem);
  125. stats[0] = totmbufs;
  126. stats[1] = 0; /*mbstat.m_mtypes[MT_DATA];*/
  127. stats[2] = 0; /*mbstat.m_mtypes[MT_OOBDATA];*/
  128. stats[3] = 0; /*mbstat.m_mtypes[MT_CONTROL];*/
  129. stats[4] = 0; /*mbstat.m_mtypes[MT_HEADER];*/
  130. stats[5] = 0; /*mbstat.m_mtypes[MT_FTABLE];*/
  131. stats[6] = 0; /*mbstat.m_mtypes[MT_SONAME];*/
  132. stats[7] = 0; /*mbstat.m_mtypes[MT_SOOPTS];*/
  133. #ifdef KERN_POOL
  134. stats[8] = mclpool.pr_nget - mclpool.pr_nput;
  135. stats[9] = mclpool.pr_npages * mclpool.pr_itemsperpage;
  136. #else
  137. stats[8] = 0;
  138. stats[9] = 0;
  139. #endif
  140. stats[10] = totmem;
  141. stats[11] = totpct;
  142. #ifdef HAS_MBUF_SFALLOCFAIL
  143. stats[12] = mbstat.sf_allocfail;
  144. stats[13] = mbstat.sf_allocwait;
  145. #else
  146. #ifdef HAS_MBUF_MDROPS
  147. stats[12] = mbstat.m_drops;
  148. stats[13] = mbstat.m_wait;
  149. #else
  150. stats[12] = 0;
  151. stats[13] = 0;
  152. #endif
  153. #endif
  154. stats[14] = mbstat.m_drain;
  155. return snpack(symon_buf, maxlen, st->arg, MT_MBUF,
  156. stats[0],
  157. stats[1],
  158. stats[2],
  159. stats[3],
  160. stats[4],
  161. stats[5],
  162. stats[6],
  163. stats[7],
  164. stats[8],
  165. stats[9],
  166. stats[10],
  167. stats[11],
  168. stats[12],
  169. stats[13],
  170. stats[14]);
  171. }