znmeb / profiling-ruby-interpreters

This is a project to profile Ruby interpreters on Linux systems

profiling-ruby-interpreters / rake_methods.rb
100644 95 lines (84 sloc) 3.042 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# helper methods to keep from cluttering up the Rakefile
 
KERNEL = "/usr/src/linux-2.6.25-gentoo-r6/vmlinux"
LIBC="/lib64/libc-2.8.so"
 
# patch the shared library linking flags
def patch_ldshared(target)
  # TODO: rewrite using "patch"!
  # as currently shipped, non-standard "CFLAGS" don't get propagated during
  # shared library builds. There are two places we need to hack to get this
  # to work: #{target}/lib/mkmf.rb and #{target}/Makefile.in
  k1 = '$(LDSHARED) #{OUTFLAG}$@ $(OBJS)'
  k2 = '$(LDSHARED) ${CFLAGS} #{OUTFLAG}$@ $(OBJS)'
  cp "#{target}/lib/mkmf.rb", "."
  sh "sed 's/#{k1}/#{k2}/' mkmf.rb > #{target}/lib/mkmf.rb"
  print `diff mkmf.rb #{target}/lib/mkmf.rb`
 
  k3 = 'LDSHARED = @LIBRUBY_LDSHARED@'
  k4 = 'LDSHARED = @LIBRUBY_LDSHARED@ @CFLAGS@'
  cp "#{target}/Makefile.in", "."
  sh "sed 's/#{k3}/#{k4}/' Makefile.in > #{target}/Makefile.in"
  print `diff Makefile.in #{target}/Makefile.in`
end
 
# check out and configure a Ruby source tree
def checkout(repos, target)
  sh "svn co #{repos} #{target}"
  patch_ldshared target
 
  # set up for builds
  cd "#{target}"
  cflags = "-g3 -march=athlon64 -pipe -O3"
  config_flags = "--enable-pthread --disable-shared --disable-install-doc --prefix=#{HERE}/#{target}.inst"
  sh "export CFLAGS=\'#{cflags}\'; autoconf; ./configure #{config_flags}"
  cd ".."
end
 
# build a C Ruby (MRI or KRI) and capture profiles of testing and benchmarks
def build_ruby(target)
  mkdir_p "#{target}.inst"
  cd target
  sh "make 2>&1 | tee ../make.log"
  #sh "make test 2>&1 | tee ../test.log"
  sh "make install 2>&1 | tee ../#{target}_inst.log"
  cd ".."
end
 
# Run benchmark suite
def run_benchmark_suite(target, path, executable, blacklist)
  mkdir_p "#{target}_profiles"
  cd "ruby-benchmark-suite"
  here = Dir.getwd
  Find.find(here) do |filename|
    basename = File.basename(filename)
    dirname = File.dirname(filename)
    next if basename !~ /^bm_.+\.rb$/
    next if blacklist.include? basename
    puts "Benchmark #{basename}"
    cd dirname
    start_oprofile
    sh "unset RUBYOPT; " +
      "export JAVA_TOOL_OPTIONS='-agentpath:/usr/local/lib64/oprofile/libjvmti_oprofile.so'; " +
      "export PATH=\"#{path}:$PATH\"; " +
      "ulimit -s 32768; " +
      "/usr/bin/time #{executable} #{filename} > /dev/null"
    stop_oprofile "#{basename}_benchmark_suite"
    cd here
    sh "opreport -l session:#{basename}_benchmark_suite > ../#{target}_profiles/#{basename}_symbols.opreport"
  end
  cd ".."
end
 
def start_oprofile
  sh "sudo opcontrol --reset"
  sh "sudo opcontrol --start"
  sh "sudo opcontrol --status"
end
 
def stop_oprofile(session)
  sh "sudo opcontrol --shutdown"
  sh "sudo rm -rf /var/lib/oprofile/samples/#{session}"
  sh "sudo opcontrol --save #{session}"
  sh "sudo opcontrol --status"
end
 
# factored pet store start task
def start_pet_store(target)
  path = "#{Dir.getwd}/#{target}/bin"
  ruby = "#{path}/ruby"
  script = "script/server -e production"
  cd "pet_store"
  sh "unset RUBYOPT; export PATH=#{path}:$PATH; #{ruby} #{script}"
  cd ".."
end