Skip to content

Commit

Permalink
Enhance --stats option
Browse files Browse the repository at this point in the history
  • Loading branch information
makenowjust authored and asterite committed Dec 6, 2016
1 parent 2e9c9b5 commit 858b3e1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
38 changes: 25 additions & 13 deletions src/compiler/crystal/command.cr
Expand Up @@ -52,6 +52,7 @@ class Crystal::Command

def initialize(@options : Array(String))
@color = true
@stats = false
end

def run
Expand Down Expand Up @@ -172,7 +173,9 @@ class Crystal::Command

private def hierarchy
config, result = compile_no_codegen "tool hierarchy", hierarchy: true, top_level: true
Crystal.print_hierarchy result.program, config.hierarchy_exp, config.output_format
Crystal.timing("Tool (hierarchy)", @stats, delay: true) do
Crystal.print_hierarchy result.program, config.hierarchy_exp, config.output_format
end
end

private def run_command(single_file = false)
Expand All @@ -190,7 +193,9 @@ class Crystal::Command

private def types
config, result = compile_no_codegen "tool types"
Crystal.print_types result.node
Crystal.timing("Tool (types)", @stats, delay: true) do
Crystal.print_types result.node
end
end

private def compile_no_codegen(command, wants_doc = false, hierarchy = false, cursor_command = false, top_level = false)
Expand All @@ -202,15 +207,17 @@ class Crystal::Command
end

private def execute(output_filename, run_args)
begin
Process.run(output_filename, args: run_args, input: true, output: true, error: true) do |process|
# Ignore the signal so we don't exit the running process
# (the running process can still handle this signal)
Signal::INT.ignore # do
status = Crystal.timing("Execute", @stats, delay: true) do
begin
Process.run(output_filename, args: run_args, input: true, output: true, error: true) do |process|
# Ignore the signal so we don't exit the running process
# (the running process can still handle this signal)
Signal::INT.ignore # do
end
$?
ensure
File.delete(output_filename) rescue nil
end
status = $?
ensure
File.delete(output_filename) rescue nil
end

if status.normal_exit?
Expand Down Expand Up @@ -351,9 +358,14 @@ class Crystal::Command
opts.on("--release", "Compile in release mode") do
compiler.release = true
end
opts.on("-s", "--stats", "Enable statistics output") do
compiler.stats = true
end
end

opts.on("-s", "--stats", "Enable statistics output") do
@stats = true
compiler.stats = true
end

unless no_codegen
opts.on("--single-module", "Generate a single LLVM module") do
compiler.single_module = true
end
Expand Down
4 changes: 3 additions & 1 deletion src/compiler/crystal/command/cursor.cr
Expand Up @@ -45,7 +45,9 @@ class Crystal::Command

file = File.expand_path(file)

result = yield Location.new(file, line_number, column_number), config, result
result = Crystal.timing("Tool (#{command.split(' ')[1]})", @stats) do
yield Location.new(file, line_number, column_number), config, result
end

case format
when "json"
Expand Down
10 changes: 7 additions & 3 deletions src/compiler/crystal/util.cr
Expand Up @@ -23,15 +23,19 @@ module Crystal
exit(exit_code) if exit_code
end

def self.timing(label, stats)
def self.timing(label, stats, delay = false)
if stats
print "%-34s" % "#{label}:"
print "%-34s" % "#{label}:" unless delay
time = Time.now
value = yield
elapsed_time = Time.now - time
LibGC.get_heap_usage_safe(out heap_size, out free_bytes, out unmapped_bytes, out bytes_since_gc, out total_bytes)
mb = heap_size / 1024.0 / 1024.0
puts " %s (%7.2fMB)" % {elapsed_time, mb}
if delay
puts "%-34s %s (%7.2fMB)" % {"#{label}:", elapsed_time, mb}
else
puts " %s (%7.2fMB)" % {elapsed_time, mb}
end
value
else
yield
Expand Down

0 comments on commit 858b3e1

Please sign in to comment.