diff -ru /usr/src/usr.bin/netstat.old/mbuf.c /usr/src/usr.bin/netstat/mbuf.c --- /usr/src/usr.bin/netstat.old/mbuf.c Wed May 23 17:27:24 2001 +++ /usr/src/usr.bin/netstat/mbuf.c Fri May 25 16:59:28 2001 @@ -40,6 +40,11 @@ #endif /* not lint */ #include + +/* This is total crap. */ +#undef MAXCPU +#define MAXCPU 16 + #include #include #include @@ -53,8 +58,6 @@ #define YES 1 typedef int bool; -struct mbstat mbstat; - static struct mbtypenames { int mt_type; char *mt_name; @@ -98,105 +101,124 @@ void mbpr() { - u_long totmem, totpossible, totmbufs; - register int i; - struct mbtypenames *mp; - int name[3], nmbclusters, nmbufs, nmbcnt, nmbtypes; - size_t nmbclen, nmbuflen, nmbcntlen, mbstatlen, mbtypeslen; - u_long *mbtypes; - bool *seen; /* "have we seen this type yet?" */ - - mbtypes = NULL; - seen = NULL; - - name[0] = CTL_KERN; - name[1] = KERN_IPC; - name[2] = KIPC_MBSTAT; - mbstatlen = sizeof mbstat; - if (sysctl(name, 3, &mbstat, &mbstatlen, 0, 0) < 0) { - warn("sysctl: retrieving mbstat"); + int i; + int nmbclusters, nmbufs, ncpu, page_size; + u_int mbuf_limit, clust_limit; + u_long totspace, totnum, totfree; + size_t mlen; + struct mbstat *mbstat = NULL; + + if (sysctlbyname("kern.ipc.mbstat", NULL, &mlen, NULL, 0) < 0) { + warn("sysctl: retrieving mbstat len"); goto err; } - if (sysctlbyname("kern.ipc.mbtypes", NULL, &mbtypeslen, NULL, 0) < 0) { - warn("sysctl: retrieving mbtypes length"); + if ((mbstat = malloc(mlen)) == NULL) { + warn("malloc: cannot allocate memory for mbstat"); goto err; } - if ((mbtypes = malloc(mbtypeslen)) == NULL) { - warn("malloc: %lu bytes for mbtypes", (u_long)mbtypeslen); + + if (sysctlbyname("kern.ipc.mbstat", mbstat, &mlen, NULL, 0) < 0) { + warn("sysctl: retrieving mbstat"); goto err; } - if (sysctlbyname("kern.ipc.mbtypes", mbtypes, &mbtypeslen, NULL, - 0) < 0) { - warn("sysctl: retrieving mbtypes"); + + mlen = sizeof (int); + if (sysctlbyname("kern.ipc.nmbclusters", &nmbclusters, &mlen, NULL, 0) + < 0) { + warn("sysctl: retrieving nmbclusters"); goto err; } - nmbtypes = mbtypeslen / sizeof(*mbtypes); - if ((seen = calloc(nmbtypes, sizeof(*seen))) == NULL) { - warn("calloc"); + mlen = sizeof (int); + if (sysctlbyname("kern.ipc.nmbufs", &nmbufs, &mlen, NULL, 0) < 0) { + warn("sysctl: retrieving nmbufs"); goto err; } - - name[2] = KIPC_NMBCLUSTERS; - nmbclen = sizeof(int); - if (sysctl(name, 3, &nmbclusters, &nmbclen, 0, 0) < 0) { - warn("sysctl: retrieving nmbclusters"); + + mlen = sizeof (u_int); + if (sysctlbyname("kern.ipc.mbuf_limit", &mbuf_limit,&mlen,NULL,0) < 0) { + warn("sysctl: retrieving mbuf_limit"); goto err; } - nmbuflen = sizeof(int); - if (sysctlbyname("kern.ipc.nmbufs", &nmbufs, &nmbuflen, 0, 0) < 0) { - warn("sysctl: retrieving nmbufs"); + mlen = sizeof (u_int); + if (sysctlbyname("kern.ipc.clust_limit",&clust_limit,&mlen,NULL,0) <0) { + warn("sysctl: retrieving clust_limit"); goto err; } - nmbcntlen = sizeof(int); - if (sysctlbyname("kern.ipc.nmbcnt", &nmbcnt, &nmbcntlen, 0, 0) < 0) { - warn("sysctl: retrieving nmbcnt"); + mlen = sizeof (int); + if (sysctlbyname("hw.ncpu", &ncpu, &mlen, NULL, 0) < 0) { + warn("sysctl: retrieving hw.ncpu"); + goto err; + } + + mlen = sizeof (int); + if (sysctlbyname("hw.pagesize", &page_size, &mlen, NULL, 0) < 0) { + warn("sysctl: retrieving hw.pagesize"); goto err; } #undef MSIZE -#define MSIZE (mbstat.m_msize) +#define MSIZE (mbstat->m_msize) #undef MCLBYTES -#define MCLBYTES (mbstat.m_mclbytes) - - totmbufs = 0; - for (mp = mbtypenames; mp->mt_name; mp++) - totmbufs += mbtypes[mp->mt_type]; - printf("%lu/%lu/%u mbufs in use (current/peak/max):\n", totmbufs, - mbstat.m_mbufs, nmbufs); - for (mp = mbtypenames; mp->mt_name; mp++) - if (mbtypes[mp->mt_type]) { - seen[mp->mt_type] = YES; - printf("\t%lu mbufs allocated to %s\n", - mbtypes[mp->mt_type], mp->mt_name); - } - seen[MT_FREE] = YES; - for (i = 0; i < nmbtypes; i++) - if (!seen[i] && mbtypes[i]) { - printf("\t%lu mbufs allocated to \n", - mbtypes[i], i); - } - printf("%lu/%lu/%u mbuf clusters in use (current/peak/max)\n", - mbstat.m_clusters - mbstat.m_clfree, mbstat.m_clusters, - nmbclusters); - printf("%lu/%lu m_ext reference counters (in use/allocated)\n", - mbstat.m_refcnt - mbstat.m_refree, mbstat.m_refcnt); - totmem = mbstat.m_mbufs * MSIZE + mbstat.m_clusters * MCLBYTES + - mbstat.m_refcnt * sizeof(union mext_refcnt); - totpossible = nmbclusters * MCLBYTES + nmbufs * MSIZE + - nmbcnt * sizeof(union mext_refcnt); - printf("%lu Kbytes allocated to network (%lu%% of mb_map in use)\n", - totmem / 1024, (totmem * 100) / totpossible); - printf("%lu requests for memory denied\n", mbstat.m_drops); - printf("%lu requests for memory delayed\n", mbstat.m_wait); - printf("%lu calls to protocol drain routines\n", mbstat.m_drain); +#define MCLBYTES (mbstat->m_mclbytes) +#undef PAGE_SIZE +#define PAGE_SIZE (page_size) +#define MBPERPG (PAGE_SIZE / MSIZE) +#define CLPERPG (PAGE_SIZE / MCLBYTES) + + printf("mbuf usage:\n"); + printf("\tGEN list:\t%lu/%lu (in use/total)\n", (mbstat->m_mbpgs[NCPU] * + MBPERPG - mbstat->m_mbfree[NCPU]), (mbstat->m_mbpgs[NCPU] * + MBPERPG)); + totnum = mbstat->m_mbpgs[NCPU] * MBPERPG; + totfree = mbstat->m_mbfree[NCPU]; + totspace = mbstat->m_mbpgs[NCPU] * PAGE_SIZE; + for (i = 0; i < ncpu; i++) { + printf("\tCPU #%d list:\t%lu/%lu (in use/total)\n", i, + (mbstat->m_mbpgs[i] * MBPERPG - mbstat->m_mbfree[i]), + (mbstat->m_mbpgs[i] * MBPERPG)); + totspace += mbstat->m_mbpgs[i] * PAGE_SIZE; + totnum += mbstat->m_mbpgs[i] * MBPERPG; + totfree += mbstat->m_mbfree[i]; + } + printf("\tTotal:\t\t%lu/%lu (in use/total)\n", (totnum - totfree), + totnum); + printf("\tMaximum number allowed on each CPU list: %d\n", mbuf_limit); + printf("\tMaximum possible: %d\n", nmbufs); + printf("\t%lu%% of mbuf map consumed\n", ((totspace * 100) / (nmbufs + * MSIZE))); + + printf("mbuf cluster usage:\n"); + printf("\tGEN list:\t%lu/%lu (in use/total)\n", (mbstat->m_clpgs[NCPU] * + CLPERPG - mbstat->m_clfree[NCPU]), (mbstat->m_clpgs[NCPU] * + CLPERPG)); + totnum = mbstat->m_clpgs[NCPU] * CLPERPG; + totfree = mbstat->m_clfree[NCPU]; + totspace = mbstat->m_clpgs[NCPU] * PAGE_SIZE; + for (i = 0; i < ncpu; i++) { + printf("\tCPU #%d list:\t%lu/%lu (in use/total)\n", i, + (mbstat->m_clpgs[i] * CLPERPG - mbstat->m_clfree[i]), + (mbstat->m_clpgs[i] * CLPERPG)); + totspace += mbstat->m_clpgs[i] * PAGE_SIZE; + totnum += mbstat->m_clpgs[i] * CLPERPG; + totfree += mbstat->m_clfree[i]; + } + printf("\tTotal:\t\t%lu/%lu (in use/total)\n", (totnum - totfree), + totnum); + printf("\tMaximum number allowed on each CPU list: %d\n", clust_limit); + printf("\tMaximum possible: %d\n", nmbclusters); + printf("\t%lu%% of cluster map consumed\n", ((totspace * 100) / + (nmbclusters * MCLBYTES))); + + printf("%lu requests for memory denied\n", mbstat->m_drops); + printf("%lu requests for memory delayed\n", mbstat->m_wait); + printf("%lu calls to protocol drain routines\n", mbstat->m_drain); err: - if (mbtypes != NULL) - free(mbtypes); - if (seen != NULL) - free(seen); + if (mbstat != NULL) + free(mbstat); + return; }