1# See the file LICENSE for redistribution information.
2#
3# Copyright (c) 1996-2009 Oracle.  All rights reserved.
4#
5# $Id$
6#
7# Deadlock Test 5.
8# Test out the minlocks, maxlocks, and minwrites options
9# to the deadlock detector.
10proc dead005 { { procs "4 6 10" } \
11    {tests "maxlocks maxwrites minlocks minwrites" } { tnum "005" } } {
12	source ./include.tcl
13
14	foreach t $tests {
15		puts "Dead$tnum.$t: deadlock detection tests"
16		env_cleanup $testdir
17
18		# Create the environment.
19		set env [berkdb_env -create -mode 0644 -lock -home $testdir]
20		error_check_good lock_env:open [is_valid_env $env] TRUE
21		case $t {
22			maxlocks { set to m }
23			maxwrites { set to W }
24			minlocks { set to n }
25			minwrites { set to w }
26		}
27		foreach n $procs {
28			set dpid [exec $util_path/db_deadlock -v -t 0.100000 \
29			    -h $testdir -a $to >& $testdir/dd.out &]
30			sentinel_init
31			set pidlist ""
32
33			# Fire off the tests
34			puts "\tDead$tnum: $t test with $n procs"
35			for { set i 0 } { $i < $n } { incr i } {
36				set locker [$env lock_id]
37				puts "$tclsh_path $test_path/wrap.tcl \
38				    $testdir/dead$tnum.log.$i \
39				    ddscript.tcl $testdir $t $locker $i $n"
40				set p [exec $tclsh_path \
41					$test_path/wrap.tcl \
42					ddscript.tcl $testdir/dead$tnum.log.$i \
43					$testdir $t $locker $i $n &]
44				lappend pidlist $p
45			}
46			watch_procs $pidlist 5
47
48			# Now check output
49			set dead 0
50			set clean 0
51			set other 0
52			for { set i 0 } { $i < $n } { incr i } {
53				set did [open $testdir/dead$tnum.log.$i]
54				while { [gets $did val] != -1 } {
55					switch $val {
56						DEADLOCK { incr dead }
57						1 { incr clean }
58						default { incr other }
59					}
60				}
61				close $did
62			}
63			tclkill $dpid
64			puts "\tDead$tnum: dead check..."
65			dead_check $t $n 0 $dead $clean $other
66			# Now verify that the correct participant
67			# got deadlocked.
68			switch $t {
69				maxlocks {set f [expr $n - 1]}
70				maxwrites {set f 2}
71				minlocks {set f 0}
72				minwrites {set f 1}
73			}
74			set did [open $testdir/dead$tnum.log.$f]
75			error_check_bad file:$t [gets $did val] -1
76			error_check_good read($f):$t $val DEADLOCK
77			close $did
78		}
79		error_check_good lock_env:close [$env close] 0
80		# Windows needs files closed before deleting them, so pause
81		tclsleep 2
82		fileremove -f $testdir/dd.out
83		# Remove log files
84		for { set i 0 } { $i < $n } { incr i } {
85			fileremove -f $testdir/dead001.log.$i
86		}
87	}
88}
89