From 229d2fb667832dc0842cea01a139d2c137227b58 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 5 Dec 2021 11:55:41 +0200 Subject: [PATCH] v.util.timers: improve tracing by supporting `-d trace_timers_creation` --- cmd/v/v.v | 2 +- vlib/v/checker/checker.v | 4 ++-- vlib/v/gen/c/cgen.v | 9 ++++++--- vlib/v/parser/parser.v | 2 +- vlib/v/util/timers.v | 31 ++++++++++++++++++++----------- 5 files changed, 30 insertions(+), 18 deletions(-) diff --git a/cmd/v/v.v b/cmd/v/v.v index 66b3543834..10c29f121d 100644 --- a/cmd/v/v.v +++ b/cmd/v/v.v @@ -50,7 +50,7 @@ fn main() { $if time_v ? { timers_should_print = true } - mut timers := util.new_timers(timers_should_print) + mut timers := util.new_timers(should_print: timers_should_print, label: 'main') timers.start('v total') defer { timers.show('v total') diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index b728153538..95148f7dde 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -101,7 +101,7 @@ mut: vweb_gen_types []ast.Type // vweb route checks prevent_sum_type_unwrapping_once bool // needed for assign new values to sum type, stopping unwrapping then loop_label string // set when inside a labelled for loop - timers &util.Timers = util.new_timers(false) + timers &util.Timers = util.get_timers() comptime_fields_type map[string]ast.Type fn_scope &ast.Scope = voidptr(0) main_fn_decl_node ast.FnDecl @@ -123,7 +123,7 @@ pub fn new_checker(table &ast.Table, pref &pref.Preferences) &Checker { return &Checker{ table: table pref: pref - timers: util.new_timers(timers_should_print) + timers: util.new_timers(should_print: timers_should_print, label: 'checker') match_exhaustive_cutoff_limit: pref.checker_match_exhaustive_cutoff_limit } } diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 8ed8d8d3cd..8bf8cef9eb 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -175,7 +175,7 @@ mut: returned_var_name string // to detect that a var doesn't need to be freed since it's being returned branch_parent_pos int // used in BranchStmt (continue/break) for autofree stop position infix_left_var_name string // a && if expr - timers &util.Timers = util.new_timers(false) + timers &util.Timers = util.get_timers() force_main_console bool // true when [console] used on fn main() as_cast_type_names map[string]string // table for type name lookup in runtime (for __as_cast) obf_table map[string]string @@ -240,7 +240,7 @@ pub fn gen(files []&ast.File, table &ast.Table, pref &pref.Preferences) string { indent: -1 module_built: module_built timers_should_print: timers_should_print - timers: util.new_timers(timers_should_print) + timers: util.new_timers(should_print: timers_should_print, label: 'global_cgen') inner_loop: &ast.EmptyStmt{} field_data_type: ast.Type(table.find_type_idx('FieldData')) init: strings.new_builder(100) @@ -525,7 +525,10 @@ fn cgen_process_one_file_cb(p &pool.PoolProcessor, idx int, wid int) &Gen { fn_decl: 0 indent: -1 module_built: global_g.module_built - timers: util.new_timers(global_g.timers_should_print) + timers: util.new_timers( + should_print: global_g.timers_should_print + label: 'cgen_process_one_file_cb idx: $idx, wid: $wid' + ) inner_loop: &ast.EmptyStmt{} field_data_type: ast.Type(global_g.table.find_type_idx('FieldData')) array_sort_fn: global_g.array_sort_fn diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 0fbf91e99b..218572a055 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -360,7 +360,7 @@ fn (mut q Queue) run() { } */ pub fn parse_files(paths []string, table &ast.Table, pref &pref.Preferences) []&ast.File { - mut timers := util.new_timers(false) + mut timers := util.new_timers(should_print: false, label: 'parse_files: $paths') $if time_parsing ? { timers.should_print = true } diff --git a/vlib/v/util/timers.v b/vlib/v/util/timers.v index 6b4e163657..c384e4815a 100644 --- a/vlib/v/util/timers.v +++ b/vlib/v/util/timers.v @@ -1,12 +1,16 @@ // Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved. // Use of this source code is governed by an MIT license // that can be found in the LICENSE file. +[has_globals] module util import time +__global g_timers = new_timers(should_print: false, label: 'g_timers') + [heap] pub struct Timers { + label string pub mut: swatches map[string]time.StopWatch should_print bool @@ -14,18 +18,26 @@ pub mut: already_shown []string } -pub fn new_timers(should_print bool) &Timers { +[params] +pub struct TimerParams { + should_print bool + label string +} + +pub fn new_timers(params TimerParams) &Timers { + $if trace_timers_creation ? { + eprintln('>>>> new_timers, should_print: $params.should_print | label: $params.label') + } return &Timers{ + label: params.label swatches: map[string]time.StopWatch{} - should_print: should_print + should_print: params.should_print already_shown: []string{cap: 100} } } -const timers = new_timers(false) - pub fn get_timers() &Timers { - return util.timers + return g_timers } pub fn timing_start(label string) { @@ -34,18 +46,15 @@ pub fn timing_start(label string) { } pub fn timing_measure(label string) { - mut t := get_timers() - t.show(label) + g_timers.show(label) } pub fn timing_measure_cumulative(label string) { - mut t := get_timers() - t.measure_cumulative(label) + g_timers.measure_cumulative(label) } pub fn timing_set_should_print(should_print bool) { - mut t := util.timers - t.should_print = should_print + g_timers.should_print = should_print } pub fn (mut t Timers) start(name string) {