Over a limit
A script that runs over its compute limit or its memory limit stops with an error that catch and try cannot catch. Limits defines each limit. This page finds the cause of such a stop with --report and a probe.
Run the commands of this page in the folder of the repository.
The bug: a loop that does not end
examples/debug/over-limit.tcl counts the words of three lines. The second line is empty.
proc countWords {lines} {
set n 0
set i 0
while {$i < [llength $lines]} {
set line [lindex $lines $i]
if {$line eq ""} continue
incr n [llength $line]
incr i
}
return $n
}
puts "words: [countWords {{one two} {} {three four five}}]"
Run it with a compute limit and --report:
ztcl --compute 1000000 --report examples/debug/over-limit.tcl
examples/debug/over-limit.tcl:18: compute limit reached
report compute=1000000 mem=212744 peak=212744
gc collections=0
The script used each of its 1,000,000 compute units, and the exit code is 1. The memory counts of the report differ between builds: the numbers here are an example. The error gives the line of the command at the top level, line 18, and no stack. A probe gives the values inside the proc. Put it on line 6 of countWords, the if, and capture i and line:
ztcl debug --compute 1000000 --last 3 \
-p "-proc countWords -line 6 -capture {i line}" \
examples/debug/over-limit.tcl
status: error
error: compute limit reached
code: NONE
probes:
p2 line countWords line 6: 166665 hits, 166665 records, 0 dropped, 0 errors
records (the last 3 of 1000; --detail records), oldest first:
#166662 p2 proc=countWords line=6 i=1 line=
#166663 p2 proc=countWords line=6 i=1 line=
#166664 p2 proc=countWords line=6 i=1 line=
Line 6 ran 166,665 times, and i stays 1 at the empty line. continue skips the incr i at the end of the body, so the loop does not end.
The fix
foreach has no index to forget. examples/debug/over-limit-fixed.tcl has this proc:
proc countWords {lines} {
set n 0
foreach line $lines {
incr n [llength $line]
}
return $n
}
ztcl --compute 1000000 --report examples/debug/over-limit-fixed.tcl prints words: 5 and a report such as report compute=15 mem=187624 peak=187624. Use the compute units of the report to choose a limit for the script, with room for larger inputs.
The bug: a cache that grows
examples/debug/over-memory.tcl keeps each result of lookup in the array cache and drops none:
proc lookup {key} {
global cache
if {![info exists cache($key)]} {
set cache($key) [string repeat $key 40]
}
return $cache($key)
}
for {set i 0} {$i < 100000} {incr i} {
lookup item-$i
}
ztcl --mem 10000000 --report examples/debug/over-memory.tcl
examples/debug/over-memory.tcl:14: the script used too much memory
report compute=164210 mem=10001248 peak=10868480
gc collections=3
The numbers of this report are an example: the compute units count the passes before the limit, so they change with the memory counts of the build.
A probe can capture the result of a command. This probe captures the size of the cache at each call of lookup:
ztcl debug --mem 10000000 --last 2 \
-p "-proc lookup -capture {{[array size ::cache]}}" \
examples/debug/over-memory.tcl
records (the last 2 of 1000; --detail records), oldest first:
#18243 p2 proc=lookup words=lookup item-18243 [array size ::cache]=18243
#18244 p2 proc=lookup words=lookup item-18244 [array size ::cache]=18244
The cache has one entry for each call, so it grows until the memory limit stops the script. The number of the last record is one less than the hits of the probe, for example 18,244 for 18,245 hits. Give the cache a limit of entries, or remove the entries that the script does not use again.
The report
--report writes these lines to stderr at the end of the run, also after an error:
| Line | What it has |
|---|---|
report compute=N mem=N peak=N |
The compute units that the script used, the memory in use at the end in bytes, and the most memory in use at one time. |
gc collections=N |
The count of the passes of the collector. |
--time adds the run time of each script, for a slow script with no limit (A slow proc).
tests/debug-examples.sh runs the examples of this page and checks these lines.