sm_mbuf.c 5.1 KB

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