xref: /dragonfly/test/stress/stress2/tools/leaks2.sh (revision 5fb3968e)
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# $FreeBSD$
29#
30
31# Script to catch possible leaks in vm, malloc and mbufs
32# This version looks for increse in minimum values
33
34i=0
35while true; do
36   # Check for leaks in vm.zone
37
38# ITEM            SIZE     LIMIT     USED    FREE  REQUESTS
39#
40# UMA Kegs:        140,        0,      66,      6,       66
41# UMA Zones:       120,        0,      66,     24,       66
42
43
44   vmstat -z | sed '1,3d;s/://g' | \
45   sed 's/  */ /g;s/\([0-9]\)  *\([0-9]\)/\1,\2/g;s/ \([0-9]\)/,\1/;s/^ *//' | \
46   awk -F, '
47/^..*$/{
48	gsub("^ *", "", $1);
49	size=$4;
50	printf "vmstat -z %s,%s\n", $1, size;
51}
52'
53   # Check for leaks in mbufs
54
55# $ netstat -m
56# 1233/597/1830 mbufs in use (current/cache/total)
57# 1232/196/1428/8896 mbuf clusters in use (current/cache/total/max)
58# 1232/74 mbuf+clusters out of packet secondary zone in use (current/cache)
59# 0/0/0/0 4k (page size) jumbo clusters in use (current/cache/total/max)
60# 0/0/0/0 9k jumbo clusters in use (current/cache/total/max)
61# 0/0/0/0 16k jumbo clusters in use (current/cache/total/max)
62# 2772K/541K/3313K bytes allocated to network (current/cache/total)
63# 508/7778/5734 requests for mbufs denied (mbufs/clusters/mbuf+clusters)
64# 0/0/0 requests for jumbo clusters denied (4k/9k/16k)
65# 0/6/2480 sfbufs in use (current/peak/max)
66# 0 requests for sfbufs denied
67# 0 requests for sfbufs delayed
68# 0 requests for I/O initiated by sendfile
69# 251 calls to protocol drain routines
70
71
72   netstat -m | head -10 | sed 's#/# #g;s/k / /;s/K / /' | awk '
73/mbufs /     {mbufs=$1};
74/ clusters/  {clusters=$2};
75/sfbufs in use/    {sfbufs=$3};
76/allocated/ {allocated=$1}
77END {
78	print "mbufs,", mbufs;
79	print "clusters,", clusters;
80	print "sfbufs,", sfbufs;
81	print "allocatedToNetwork,", allocated;
82}
83'
84#   sysctl vm.kvm_free | tail -1 | sed 's/:/,/' # Need used here!
85   sleep 1
86done | awk -F, '
87{
88# Pairs of "name, value" are passed to this awk script
89	name=$1
90	size=$2
91	if (NF != 2)
92		print "Number of fields for ", name, "is ", NF
93	if (size == s[name])
94		next;
95#	print "name, size, old minimum :", name, size, s[name]
96
97	if ((size - 10 < s[name]) || (f[name] == 0)) {
98#		print "Initial value / new minimum", n[name], s[name], size
99		n[name] = 0
100		if (f[name] == 0)
101			f[name] = 1
102		if (f[name] != 2)
103			s[name] = size
104	}
105
106	if (++n[name] > 120) {
107		cmd="date '+%T'"
108		cmd | getline t
109		close(cmd)
110#		if  (++w[name] > 4) {
111			printf "%s \"%s\" may be leaking, size %d\n", t, name, size
112			f[name] = 2
113#		}
114		n[name] = 0
115		s[name] = size
116	}
117}'
118