xref: /freebsd/tools/test/stress2/tools/leaks.sh (revision 9768746b)
1#!/bin/sh
2
3#
4# Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
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# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29# Script to catch possible leaks in vm, malloc and mbufs
30# Report values growing in 10 consecuitive samples
31
32i=0
33while true; do
34   # Check for leaks in vm.zone
35
36# ITEM            SIZE     LIMIT     USED    FREE  REQUESTS
37#
38# UMA Kegs:        140,        0,      66,      6,       66
39# UMA Zones:       120,        0,      66,     24,       66
40
41   vmstat -z | sed '1,3d;s/://g' | \
42   sed 's/  */ /g;s/\([0-9]\)  *\([0-9]\)/\1,\2/g;s/ \([0-9]\)/,\1/;s/^ *//' | \
43   awk -F, '
44/^..*$/{
45	gsub("^ *", "", $1);
46	size=$4;
47	printf "vmstat -z %s,%s\n", $1, size;
48}
49'
50   # vmstat -m
51
52   #          Type InUse MemUse HighUse Requests  Size(s)
53   #        DEVFS3   168    21K       -      169  128
54   #        DEVFS1   157    40K       -      157  256
55   #         DEVFS    12     1K       -       13  16,128
56	vmstat -m | \
57	sed '1,1d;s/K .*//;s/ [0-9][0-9]* / /;s/  */ /g;s/^ *//;s/ /_/g;s/_\([0-9][0-9]*$\)/ \1/' | \
58	awk '{printf "vmstat -m %s, %d\n", $1, $2}'
59
60   # Check for leaks in mbufs
61
62# $ netstat -m
63# 1233/597/1830 mbufs in use (current/cache/total)
64# 1232/196/1428/8896 mbuf clusters in use (current/cache/total/max)
65# 1232/74 mbuf+clusters out of packet secondary zone in use (current/cache)
66# 0/0/0/0 4k (page size) jumbo clusters in use (current/cache/total/max)
67# 0/0/0/0 9k jumbo clusters in use (current/cache/total/max)
68# 0/0/0/0 16k jumbo clusters in use (current/cache/total/max)
69# 2772K/541K/3313K bytes allocated to network (current/cache/total)
70# 508/7778/5734 requests for mbufs denied (mbufs/clusters/mbuf+clusters)
71# 0/0/0 requests for jumbo clusters denied (4k/9k/16k)
72# 0/6/2480 sfbufs in use (current/peak/max)
73# 0 requests for sfbufs denied
74# 0 requests for sfbufs delayed
75# 0 requests for I/O initiated by sendfile
76# 251 calls to protocol drain routines
77
78   netstat -m | head -10 | sed 's#/# #g;s/k / /;s/K / /' | awk '
79/mbufs /     {mbufs=$1};
80/ clusters/  {clusters=$2};
81/sfbufs in use/    {sfbufs=$3};
82/allocated/ {allocated=$1}
83END {
84	print "mbufs,", mbufs;
85	print "clusters,", clusters;
86	print "sfbufs,", sfbufs;
87	print "allocatedToNetwork,", allocated;
88}
89'
90   sysctl vm.kvm_free | tail -1 | sed 's/:/,/'
91   sysctl vm.swap_reserved | tail -1 | sed 's/:/,/'
92   sleep 10
93done | awk -F, '
94{
95# Pairs of "name, value" are passed to this awk script
96	name=$1;
97	size=$2;
98#	print "name, size :", name, size;
99	if (NF != 2)
100		print "Number of fields for ", name, "is ", NF;
101	if (size > s[name]) {
102		n[name]++;
103		if (n[name] > 50) {
104			cmd="date '+%T'";
105			cmd | getline t;
106			close(cmd);
107			printf "%s \"%s\" may be leaking, used %d\r\n", t, name, size;
108			n[name] = 0;
109		}
110		s[name] = size;
111	}
112}'
113