2020-04-27 08:38:47 +02:00
|
|
|
module gen
|
|
|
|
|
|
|
|
pub struct ProfileCounterMeta{
|
|
|
|
fn_name string
|
|
|
|
vpc_name string
|
|
|
|
vpc_calls string
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut g Gen) profile_fn(fn_name string, is_main bool){
|
|
|
|
if is_main {
|
|
|
|
g.writeln('')
|
|
|
|
g.writeln('\tatexit(vprint_profile_stats);')
|
|
|
|
g.writeln('')
|
|
|
|
}
|
2020-05-05 16:19:25 +02:00
|
|
|
if fn_name.starts_with('time.vpc_now') {
|
2020-04-27 08:38:47 +02:00
|
|
|
g.defer_profile_code = ''
|
|
|
|
} else {
|
2020-05-05 16:19:25 +02:00
|
|
|
measure_fn_name := if g.pref.os == .mac { 'time__vpc_now_darwin' } else { 'time__vpc_now' }
|
2020-04-27 08:38:47 +02:00
|
|
|
fn_profile_counter_name := 'vpc_${g.last_fn_c_name}'
|
|
|
|
fn_profile_counter_name_calls := '${fn_profile_counter_name}_calls'
|
|
|
|
g.writeln('')
|
2020-05-05 16:19:25 +02:00
|
|
|
g.writeln('\tdouble _PROF_FN_START = ${measure_fn_name}(); ${fn_profile_counter_name_calls}++; // $fn_name')
|
2020-04-27 08:38:47 +02:00
|
|
|
g.writeln('')
|
2020-05-05 16:19:25 +02:00
|
|
|
g.defer_profile_code = '\t${fn_profile_counter_name} += ${measure_fn_name}() - _PROF_FN_START;'
|
2020-04-27 08:38:47 +02:00
|
|
|
g.pcs_declarations.writeln('double ${fn_profile_counter_name} = 0.0; u64 ${fn_profile_counter_name_calls} = 0;')
|
|
|
|
g.pcs << ProfileCounterMeta{ g.last_fn_c_name, fn_profile_counter_name, fn_profile_counter_name_calls }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut g Gen) gen_vprint_profile_stats() {
|
|
|
|
g.pcs_declarations.writeln('void vprint_profile_stats(){')
|
2020-04-27 11:45:19 +02:00
|
|
|
fstring := '"%14llu %14.3fms %14.0fns %s \\n"'
|
2020-04-27 08:38:47 +02:00
|
|
|
if g.pref.profile_file == '-' {
|
|
|
|
for pc_meta in g.pcs {
|
2020-04-27 11:45:19 +02:00
|
|
|
g.pcs_declarations.writeln('\tif (${pc_meta.vpc_calls}) printf($fstring, ${pc_meta.vpc_calls}, ${pc_meta.vpc_name}/1000000.0, ${pc_meta.vpc_name}/${pc_meta.vpc_calls}, "${pc_meta.fn_name}" );')
|
2020-04-27 08:38:47 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
g.pcs_declarations.writeln('\tFILE * fp;')
|
|
|
|
g.pcs_declarations.writeln('\tfp = fopen ("${g.pref.profile_file}", "w+");')
|
|
|
|
for pc_meta in g.pcs {
|
2020-04-27 11:45:19 +02:00
|
|
|
g.pcs_declarations.writeln('\tif (${pc_meta.vpc_calls}) fprintf(fp, $fstring, ${pc_meta.vpc_calls}, ${pc_meta.vpc_name}/1000000.0, ${pc_meta.vpc_name}/${pc_meta.vpc_calls}, "${pc_meta.fn_name}" );')
|
2020-04-27 08:38:47 +02:00
|
|
|
}
|
|
|
|
g.pcs_declarations.writeln('\tfclose(fp);')
|
|
|
|
}
|
|
|
|
g.pcs_declarations.writeln('}')
|
|
|
|
}
|