sm_mbuf.c 4.7 KB

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