Thanks Matthew - very cool idea. I'll try to apply it to my code base and see how it works.<br><br>Thanks,<br>yc<br><br><div class="gmail_quote">On Fri, Apr 17, 2009 at 7:03 PM, Matthew Flatt <span dir="ltr"><<a href="mailto:mflatt@cs.utah.edu">mflatt@cs.utah.edu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="im">At Fri, 17 Apr 2009 18:33:12 -0700, YC wrote:<br>
> what are some approaches to determine where in code the thread execution (or<br>
> multiple threads) hangs?<br>
<br>
</div>Here's a function that creates a thread to watch a different thread.<br>
Every `delay' seconds, it grabs a stack trace of the thread `t':<br>
<br>
(define (watch-thread t delay)<br>
;; create a new thread to report snapshots of t:<br>
(thread (lambda ()<br>
(let loop ()<br>
(sleep delay)<br>
(let ([trace (continuation-mark-set->context<br>
(continuation-marks t))])<br>
(unless (null? trace)<br>
(for-each print-location trace)<br>
(newline)))<br>
(loop)))))<br>
<br>
Maybe something like that would help you.<br>
<br>
Here's a basic print-location function to use with watch-thread:<br>
<br>
(define (print-location i)<br>
(let ([id (car i)]<br>
[src (cdr i)])<br>
(cond<br>
[src (printf "~a:~a~a\n"<br>
(srcloc-source src)<br>
(if (srcloc-line src)<br>
(format "~a:~a"<br>
(srcloc-line src)<br>
(srcloc-column src))<br>
(srcloc-position src))<br>
(if id<br>
(format ": ~a" id)<br>
""))]<br>
[id (printf "~a\n" id)]<br>
[else (printf "???\n")])))<br>
<br>
For example, the following shows where all the time goes when you load<br>
the implementation of the main PLT Scheme reference:<br>
<br>
(let ([watcher-t (watch-thread (current-thread) 0.1)])<br>
(dynamic-require '(lib "scribblings/reference/reference.scrbl") #f)<br>
(kill-thread watcher-t))<br>
<br>
This is a noisy way to monitor a thread, though. Instead of dumping each<br>
stack trace, you might just save it to check later when the thread<br>
seems to be stuck.<br>
<br>
The gprof-like profiler that Eli mentioned on plt-dev works in a<br>
similar way:<br>
<br>
<a href="http://list.cs.brown.edu/pipermail/plt-dev/2009-April/000543.html" target="_blank">http://list.cs.brown.edu/pipermail/plt-dev/2009-April/000543.html</a><br>
<br>
</blockquote></div><br>