sm_mbuf.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* $Id: sm_mbuf.c,v 1.1 2003/06/20 08:42:03 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 <sys/errno.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. #include "error.h"
  38. #include "symon.h"
  39. #include "conf.h"
  40. #ifndef HAS_KERN_MBSTAT
  41. void
  42. init_mbuf(char *s)
  43. {
  44. fatal("mbuf module requires system upgrade (sysctl.h/KERN_MBSTAT)");
  45. }
  46. int
  47. get_mbuf(char *symon_buf, int maxlen, char *arg)
  48. {
  49. fatal("mbuf module requires system upgrade (sysctl.h/KERN_MBSTAT)");
  50. }
  51. #else
  52. /* Prepare if module for first use */
  53. void
  54. init_mbuf(char *s)
  55. {
  56. info("started module mbuf(%s)", s);
  57. }
  58. /* Get mbuf statistics */
  59. int
  60. get_mbuf(char *symon_buf, int maxlen, char *arg)
  61. {
  62. struct mbstat mbstat;
  63. int npools;
  64. struct pool pool, mbpool, mclpool;
  65. int mib[4];
  66. size_t size;
  67. int i;
  68. char name[32];
  69. int flag = 0;
  70. int nmbtypes = sizeof(mbstat.m_mtypes) / sizeof(short);
  71. int page_size = getpagesize();
  72. int totmem, totused, totmbufs, totpct;
  73. u_int32_t stats[15];
  74. mib[0] = CTL_KERN;
  75. mib[1] = KERN_MBSTAT;
  76. size = sizeof(mbstat);
  77. if (sysctl(mib, 2, &mbstat, &size, NULL, 0) < 0) {
  78. warning("mbuf(%s) failed (sysctl() %s)", arg, strerror(errno));
  79. return (0);
  80. }
  81. mib[0] = CTL_KERN;
  82. mib[1] = KERN_POOL;
  83. mib[2] = KERN_POOL_NPOOLS;
  84. size = sizeof(npools);
  85. if (sysctl(mib, 3, &npools, &size, NULL, 0) < 0) {
  86. warning("mbuf(%s) failed (sysctl() %s)", arg, strerror(errno));
  87. return (0);
  88. }
  89. for (i = 1; npools; ++i) {
  90. mib[0] = CTL_KERN;
  91. mib[1] = KERN_POOL;
  92. mib[2] = KERN_POOL_POOL;
  93. mib[3] = i;
  94. size = sizeof(pool);
  95. if (sysctl(mib, 4, &pool, &size, NULL, 0) < 0) {
  96. warning("mbuf(%s) failed (sysctl() %s)", arg, strerror(errno));
  97. return (0);
  98. }
  99. npools--;
  100. mib[2] = KERN_POOL_NAME;
  101. size = sizeof(name);
  102. if (sysctl(mib, 4, name, &size, NULL, 0) < 0) {
  103. warning("mbuf(%s) failed (sysctl() %s)", arg, strerror(errno));
  104. return (0);
  105. }
  106. if (!strcmp(name, "mbpl")) {
  107. bcopy(&pool, &mbpool, sizeof(pool));
  108. flag |= 1;
  109. } else if (!strcmp(name, "mclpl")) {
  110. bcopy(&pool, &mclpool, sizeof(pool));
  111. flag |= 2;
  112. }
  113. if (flag == 3)
  114. break;
  115. }
  116. if (flag != 3) {
  117. warning("mbuf(%s) failed (flag != 3)", arg);
  118. return (0);
  119. }
  120. totmbufs = 0;
  121. for (i = 0; i < nmbtypes; ++i)
  122. totmbufs += mbstat.m_mtypes[i];
  123. totmem = (mbpool.pr_npages + mclpool.pr_npages) * page_size;
  124. totused = (mbpool.pr_nget - mbpool.pr_nput) * mbpool.pr_size +
  125. (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size;
  126. totpct = (totmem == 0) ? 0 : ((totused * 100) / totmem);
  127. stats[0] = totmbufs;
  128. stats[1] = mbstat.m_mtypes[MT_DATA];
  129. stats[2] = mbstat.m_mtypes[MT_OOBDATA];
  130. stats[3] = mbstat.m_mtypes[MT_CONTROL];
  131. stats[4] = mbstat.m_mtypes[MT_HEADER];
  132. stats[5] = mbstat.m_mtypes[MT_FTABLE];
  133. stats[6] = mbstat.m_mtypes[MT_SONAME];
  134. stats[7] = mbstat.m_mtypes[MT_SOOPTS];
  135. stats[8] = mclpool.pr_nget - mclpool.pr_nput;
  136. stats[9] = mclpool.pr_npages * mclpool.pr_itemsperpage;
  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, 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. }
  159. #endif /* HAS_KERN_MBSTAT */