1262595Smarkj#
2262595Smarkj# CDDL HEADER START
3262595Smarkj#
4262595Smarkj# The contents of this file are subject to the terms of the
5262595Smarkj# Common Development and Distribution License (the "License").
6262595Smarkj# You may not use this file except in compliance with the License.
7262595Smarkj#
8262595Smarkj# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9262595Smarkj# or http://www.opensolaris.org/os/licensing.
10262595Smarkj# See the License for the specific language governing permissions
11262595Smarkj# and limitations under the License.
12262595Smarkj#
13262595Smarkj# When distributing Covered Code, include this CDDL HEADER in each
14262595Smarkj# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15262595Smarkj# If applicable, add the following below this CDDL HEADER, with the
16262595Smarkj# fields enclosed by brackets "[]" replaced with your own identifying
17262595Smarkj# information: Portions Copyright [yyyy] [name of copyright owner]
18262595Smarkj#
19262595Smarkj# CDDL HEADER END
20262595Smarkj#
21262595Smarkj
22262595Smarkj#
23262595Smarkj# Copyright (c) 2012, Joyent, Inc. All rights reserved.
24262595Smarkj#
25262595Smarkj
26262595Smarkjlet j=8
27262595Smarkj
28262595Smarkjenable()
29262595Smarkj{
30262595Smarkj	prog=/var/tmp/dtest.$$.d
31262595Smarkj	err=/var/tmp/dtest.$$.err
32262595Smarkj
33262595Smarkj	nawk -v nprobes=$1 'BEGIN { \
34262595Smarkj		for (i = 0; i < nprobes - 1; i++) { 		\
35262595Smarkj			printf("dtrace:::BEGIN,\n");		\
36262595Smarkj		}						\
37262595Smarkj								\
38262595Smarkj		printf("dtrace:::BEGIN { exit(0); }\n");	\
39262595Smarkj	}' /dev/null > $prog
40262595Smarkj
41262595Smarkj	dtrace -qs $prog > /dev/null 2> $err
42262595Smarkj
43262595Smarkj	if [[ "$?" -eq 0 ]]; then
44262595Smarkj		return 0
45262595Smarkj	else
46262595Smarkj		if ! grep "DIF program exceeds maximum program size" $err \
47262595Smarkj		    1> /dev/null 2>&1 ; then 
48262595Smarkj			echo "failed to enable $prog: `cat $err`"
49262595Smarkj			exit 1
50262595Smarkj		fi
51262595Smarkj
52262595Smarkj		return 1
53262595Smarkj	fi
54262595Smarkj}
55262595Smarkj
56262595Smarkj#
57262595Smarkj# First, establish an upper bound
58262595Smarkj#
59262595Smarkjlet upper=1
60262595Smarkj
61262595Smarkjwhile enable $upper ; do
62262595Smarkj	let lower=upper
63262595Smarkj	let upper=upper+upper
64262595Smarkj	echo success at $lower, raised to $upper
65262595Smarkjdone
66262595Smarkj
67262595Smarkj#
68262595Smarkj# Now search for the highest value that can be enabled
69262595Smarkj#
70262595Smarkjwhile [[ "$lower" -lt "$upper" ]]; do
71262595Smarkj	let guess=$(((lower + upper) / 2))
72262595Smarkj	echo "lower is $lower; upper is $upper; guess is $guess\c"
73262595Smarkj
74262595Smarkj	if enable $guess ; then
75262595Smarkj		if [[ $((upper - lower)) -le 2 ]]; then
76262595Smarkj			let upper=guess
77262595Smarkj		fi
78262595Smarkj
79262595Smarkj		echo " (success)"
80262595Smarkj		let lower=guess
81262595Smarkj	else
82262595Smarkj		echo " (failure)"
83262595Smarkj		let upper=guess
84262595Smarkj	fi
85262595Smarkjdone
86262595Smarkj
87262595Smarkjlet expected=10000
88262595Smarkj
89262595Smarkjif [[ "$lower" -lt "$expected" ]]; then
90262595Smarkj	echo "expected support for enablings of at least $expected probes; \c"
91262595Smarkj	echo "found $lower"
92262595Smarkj	exit 1
93262595Smarkjfi
94262595Smarkj
95262595Smarkjecho "maximum supported enabled probes found to be $lower"
96262595Smarkjexit 0
97262595Smarkj
98