sm_mbuf.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* $Id: sm_mbuf.c,v 1.3 2005/01/15 17:31:11 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. /* Prepare if module for first use */
  44. void
  45. init_mbuf(char *s)
  46. {
  47. mbstat_len = CTL_MAXNAME;
  48. if (sysctlnametomib(mbstat_mib_str, mbstat_mib, &mbstat_len) < 0) {
  49. warning("sysctlnametomib for mbuf failed");
  50. mbstat_len = 0;
  51. }
  52. info("started module mbuf(%.200s)", s);
  53. }
  54. /* Get mbuf statistics */
  55. int
  56. get_mbuf(char *symon_buf, int maxlen, char *arg)
  57. {
  58. struct mbstat mbstat;
  59. #ifdef KERN_POOL
  60. int npools;
  61. struct pool pool, mbpool, mclpool;
  62. char name[32];
  63. int flag = 0;
  64. int page_size = getpagesize();
  65. #endif
  66. size_t size;
  67. int totmem, totused, totmbufs, totpct;
  68. u_int32_t stats[15];
  69. if (!mbstat_len)
  70. return 0;
  71. size = sizeof(mbstat);
  72. if (sysctl(mbstat_mib, mbstat_len, &mbstat, &size, NULL, 0) < 0) {
  73. warning("mbuf(%.200s) failed (sysctl() %.200s)", arg, strerror(errno));
  74. return (0);
  75. }
  76. #ifdef KERN_POOL
  77. mib[0] = CTL_KERN;
  78. mib[1] = KERN_POOL;
  79. mib[2] = KERN_POOL_NPOOLS;
  80. size = sizeof(npools);
  81. if (sysctl(mib, 3, &npools, &size, NULL, 0) < 0) {
  82. warning("mbuf(%.200s) failed (sysctl() %.200s)", arg, strerror(errno));
  83. return (0);
  84. }
  85. for (i = 1; npools; ++i) {
  86. mib[0] = CTL_KERN;
  87. mib[1] = KERN_POOL;
  88. mib[2] = KERN_POOL_POOL;
  89. mib[3] = i;
  90. size = sizeof(pool);
  91. if (sysctl(mib, 4, &pool, &size, NULL, 0) < 0) {
  92. warning("mbuf(%.200s) failed (sysctl() %.200s)", arg, strerror(errno));
  93. return (0);
  94. }
  95. npools--;
  96. mib[2] = KERN_POOL_NAME;
  97. size = sizeof(name);
  98. if (sysctl(mib, 4, name, &size, NULL, 0) < 0) {
  99. warning("mbuf(%.200s) failed (sysctl() %.200s)", arg, strerror(errno));
  100. return (0);
  101. }
  102. if (!strcmp(name, "mbpl")) {
  103. bcopy(&pool, &mbpool, sizeof(pool));
  104. flag |= 1;
  105. } else if (!strcmp(name, "mclpl")) {
  106. bcopy(&pool, &mclpool, sizeof(pool));
  107. flag |= 2;
  108. }
  109. if (flag == 3)
  110. break;
  111. }
  112. if (flag != 3) {
  113. warning("mbuf(%.200s) failed (flag != 3)", arg);
  114. return (0);
  115. }
  116. #endif
  117. totmbufs = 0; /* XXX: collect mb_statpcpu and add together */
  118. #ifdef KERN_POOL
  119. totmem = (mbpool.pr_npages + mclpool.pr_npages) * page_size;
  120. totused = (mbpool.pr_nget - mbpool.pr_nput) * mbpool.pr_size +
  121. (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
  122. #else
  123. totmem = 0;
  124. totused = 0;
  125. #endif
  126. totpct = (totmem == 0) ? 0 : ((totused * 100) / totmem);
  127. stats[0] = totmbufs;
  128. stats[1] = 0; /*mbstat.m_mtypes[MT_DATA];*/
  129. stats[2] = 0; /*mbstat.m_mtypes[MT_OOBDATA];*/
  130. stats[3] = 0; /*mbstat.m_mtypes[MT_CONTROL];*/
  131. stats[4] = 0; /*mbstat.m_mtypes[MT_HEADER];*/
  132. stats[5] = 0; /*mbstat.m_mtypes[MT_FTABLE];*/
  133. stats[6] = 0; /*mbstat.m_mtypes[MT_SONAME];*/
  134. stats[7] = 0; /*mbstat.m_mtypes[MT_SOOPTS];*/
  135. #ifdef KERN_POOL
  136. stats[8] = mclpool.pr_nget - mclpool.pr_nput;
  137. stats[9] = mclpool.pr_npages * mclpool.pr_itemsperpage;
  138. #else
  139. stats[8] = 0;
  140. stats[9] = 0;
  141. #endif
  142. stats[10] = totmem;
  143. stats[11] = totpct;
  144. #ifdef HAS_MBUF_SFALLOCFAIL
  145. stats[12] = mbstat.sf_allocfail;
  146. stats[13] = mbstat.sf_allocwait;
  147. #else
  148. #ifdef HAS_MBUF_MDROPS
  149. stats[12] = mbstat.m_drops;
  150. stats[13] = mbstat.m_wait;
  151. #else
  152. stats[12] = 0;
  153. stats[13] = 0;
  154. #endif
  155. #endif
  156. stats[14] = mbstat.m_drain;
  157. return snpack(symon_buf, maxlen, arg, MT_MBUF,
  158. stats[0],
  159. stats[1],
  160. stats[2],
  161. stats[3],
  162. stats[4],
  163. stats[5],
  164. stats[6],
  165. stats[7],
  166. stats[8],
  167. stats[9],
  168. stats[10],
  169. stats[11],
  170. stats[12],
  171. stats[13],
  172. stats[14]);
  173. }