Ron M <rm_rails / cheapcomplexdevices.com> wrote:
> Robert Klemme wrote:
>> Ron M <rm_rails / cheapcomplexdevices.com> wrote:
>> 
>>> Is there a way of displaying the stack trace of a running ruby
>>> [like] Java  QUIT signal
>>> [or] the "pstack" program gives you the same capabilities for C
>>> programs. 
>> 
>> I prefer to use real profiling tools instead of looking at a single
>> stacktrace.  In Java that would be -Xrunhprof, OptimizeIt et al.  In
>> Ruby you can use -r profile for profiling.  Also, there's module
>> Benchmark which is less intrusive than the profiler.  Finally there's
>> set_trace_func which can be used to generate call stacks for each
>> thread and print them to console on a signal similarly to your Java
>> approach. 
>> 
>> HTH
> 
> These all work fine for relatively short-lived programs; but
> do little to help in a progam that runs for days and only gets
> slow after many hours of running.
> 
> On short runs (few hours) my program works fine (processes
> dozens of records per second) but after half a day or so
> it slows to taking a few seconds per record.  "-r profile"
> and set_trace_func are pretty intrusive and I fear my
> program that runs for days would take weeks.    Benchmark
> has the disadvantage of needing to know what piece of code
> to measure; rather than exposing the slow part.
> 
> But the biggest advantage in my mind of sampling instantaneous
> snapshots of the stack is that it works on any program witout
> modification (well, linux's ptrace is most useful if you didn't
> strip signals); and it's  almost totally non-intrusive, and does
> not skew the timing results at all.

How about this?


Signal.trap "INT" do
  fork do
    ObjectSpace.each_object(Thread) do |th|
      th.raise Exception, "Stack Dump" unless Thread.current == th
    end
    raise Exception, "Stack Dump"
  end
end

Thread.new do
  10.times do
    puts "."
    sleep 5
  end
end

10.times do
  puts "."
  sleep 5
end


This is non intrusive.

    robert