1 # Debugging/profiling for rbot
3 # (c) 2006 Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
4 # Licensed under GPL V2.
6 class DebugPlugin < Plugin
7 BotConfig.register BotConfigIntegerValue.new('debug.interval',
8 :default => 10, :validate => Proc.new{|v| v > 0},
9 :desc => "Number of seconds between memory profile dumps")
10 BotConfig.register BotConfigBooleanValue.new('debug.dump_strings',
12 :desc => "Set to true if you want the profiler to dump strings, false otherwise")
13 BotConfig.register BotConfigStringValue.new('debug.logdir',
15 :desc => "Directory where profile/string dumps are to be stored")
22 @file = File.open("#{@bot.botclass}/#{@bot.config['debug.logdir']}/memory_profiler.log",'w')
23 @thread = @bot.timer.add(@bot.config['debug.interval']) {
30 ObjectSpace.each_object do |o|
31 @curr[o.class] += 1 #Marshal.dump(o).size rescue 1
32 if @bot.config['debug.dump_strings'] and o.class == String
37 if @bot.config['debug.dump_strings']
38 File.open("#{@bot.botclass}/#{@bot.config['debug.logdir']}/memory_profiler_strings.log.#{Time.now.to_i}",'w') do |f|
39 curr_strings.sort.each do |s|
47 (@curr.keys + @delta.keys).uniq.each do |k,v|
48 @delta[k] = @curr[k]-@prev[k]
52 @delta.sort_by { |k,v| -v.abs }[0..19].sort_by { |k,v| -v}.each do |k,v|
53 @file.printf "%+5d: %s (%d)\n", v, k.name, @curr[k] unless v == 0
61 rescue Exception => err
62 error "** memory_profiler error: #{err}"
65 @bot.timer.block(@thread)
68 def help( plugin, topic="" )
69 "debug start => start the periodic profiler; " + \
70 "debug stop => stops the periodic profiler; " + \
71 "debug dumpstrings => dump all of the strings"
74 def start_it(m, params)
76 @bot.timer.unblock(@thread)
77 m.reply "profile dump started"
78 rescue Exception => err
79 m.reply "couldn't start profile dump"
80 error "couldn't start profile dump: #{err}"
84 def stop_it(m, params)
86 @bot.timer.block(@thread)
87 m.reply "profile dump stop"
88 rescue Exception => err
89 m.reply "couldn't stop profile dump"
90 error "couldn't stop profile dump: #{err}"
94 def dump_strings(m, params)
97 m.reply "Dumping strings ..."
100 ObjectSpace.each_object do |o|
106 File.open("#{@bot.botclass}/#{@bot.config['debug.logdir']}/memory_profiler_strings.log.#{Time.now.to_i}",'w') do |f|
107 curr_strings.sort.each do |s|
113 rescue Exception => err
114 m.reply "dumping strings failed"
115 error "dumping strings failed: #{err}"
122 plugin = DebugPlugin.new
123 plugin.register( "debug" )
124 plugin.default_auth( 'start', false )
125 plugin.default_auth( 'stop', false )
126 plugin.default_auth( 'dumpstrings', false )
128 plugin.map 'debug start', :action => 'start_it'
129 plugin.map 'debug stop', :action => 'stop_it'
130 plugin.map 'debug dumpstrings', :action => 'dump_strings'