Skip to content

Commit a03d62e

Browse files
authored
cgen: add a -profile report column, to show only the func time, *excluding* the accumulated children calls time (usable through ./v -profile - run a.v |sort -nk3 for example) (#24056)
1 parent c22a9eb commit a03d62e

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

‎vlib/v/gen/c/cgen.v‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) GenO
574574
b.write_string2('\n // V preincludes:\n', g.preincludes.str())
575575
b.write_string2('\n// V cheaders:\n', g.cheaders.str())
576576
if g.pcs_declarations.len > 0 {
577+
g.pcs_declarations.writeln('double prof_measured_time = 0.0;') // does not work for multithreaded
577578
b.write_string2('\n// V profile counters:\n', g.pcs_declarations.str())
578579
}
579580
b.write_string2('\n// V includes:\n', g.includes.str())

‎vlib/v/gen/c/profile.v‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ fn (mut g Gen) profile_fn(fn_decl ast.FnDecl) {
3232
g.writeln('\tv__profile_enabled = true;')
3333
}
3434
g.writeln('\tdouble _PROF_FN_START = ${measure_fn_name}();')
35+
g.writeln('\tdouble _PROF_PREV_MEASURED_TIME = prof_measured_time;')
3536
g.writeln('\tif(v__profile_enabled) { ${fn_profile_counter_name_calls}++; } // ${fn_name}')
3637
g.writeln('')
37-
g.defer_profile_code = '\tif(v__profile_enabled) { ${fn_profile_counter_name} += ${measure_fn_name}() - _PROF_FN_START; }'
38+
g.defer_profile_code = '\tif(v__profile_enabled) { double _PROF_ELAPSED = ${measure_fn_name}() - _PROF_FN_START; '
39+
g.defer_profile_code += '${fn_profile_counter_name} += _PROF_ELAPSED; '
40+
g.defer_profile_code += '${fn_profile_counter_name}_only_current += _PROF_ELAPSED - (prof_measured_time - _PROF_PREV_MEASURED_TIME); '
41+
g.defer_profile_code += 'prof_measured_time = _PROF_PREV_MEASURED_TIME + _PROF_ELAPSED; }'
3842
if should_restore_v__profile_enabled {
3943
g.defer_profile_code += '\n\t\tv__profile_enabled = _prev_v__profile_enabled;'
4044
}
41-
g.pcs_declarations.writeln('double ${fn_profile_counter_name} = 0.0; u64 ${fn_profile_counter_name_calls} = 0;')
45+
g.pcs_declarations.writeln('double ${fn_profile_counter_name} = 0.0; double ${fn_profile_counter_name}_only_current = 0.0; u64 ${fn_profile_counter_name_calls} = 0;')
4246
g.pcs << ProfileCounterMeta{
4347
fn_name: cfn_name
4448
vpc_name: fn_profile_counter_name
@@ -49,7 +53,7 @@ fn (mut g Gen) profile_fn(fn_decl ast.FnDecl) {
4953

5054
pub fn (mut g Gen) gen_vprint_profile_stats() {
5155
g.pcs_declarations.writeln('void vprint_profile_stats(){')
52-
fstring := '"%14llu %14.3fms %14.0fns %s \\n"'
56+
fstring := '"%14llu %14.3fms %14.3fms %14.0fns %s \\n"'
5357
g.pcs_declarations.writeln('\tf64 f = 1.0;')
5458
if g.pref.os == .windows {
5559
// QueryPerformanceCounter() / QueryPerformanceFrequency()
@@ -60,13 +64,13 @@ pub fn (mut g Gen) gen_vprint_profile_stats() {
6064
}
6165
if g.pref.profile_file == '-' {
6266
for pc_meta in g.pcs {
63-
g.pcs_declarations.writeln('\tif (${pc_meta.vpc_calls}) printf(${fstring}, ${pc_meta.vpc_calls}, (${pc_meta.vpc_name}*f)/1000000.0, (${pc_meta.vpc_name}*f)/${pc_meta.vpc_calls}, "${pc_meta.fn_name}" );')
67+
g.pcs_declarations.writeln('\tif (${pc_meta.vpc_calls}) printf(${fstring}, ${pc_meta.vpc_calls}, (${pc_meta.vpc_name}*f)/1000000.0, (${pc_meta.vpc_name}_only_current*f)/1000000.0, (${pc_meta.vpc_name}*f)/${pc_meta.vpc_calls}, "${pc_meta.fn_name}" );')
6468
}
6569
} else {
6670
g.pcs_declarations.writeln('\tFILE * fp;')
6771
g.pcs_declarations.writeln('\tfp = fopen ("${g.pref.profile_file}", "w+");')
6872
for pc_meta in g.pcs {
69-
g.pcs_declarations.writeln('\tif (${pc_meta.vpc_calls}) fprintf(fp, ${fstring}, ${pc_meta.vpc_calls}, (${pc_meta.vpc_name}*f)/1000000.0, (${pc_meta.vpc_name}*f)/${pc_meta.vpc_calls}, "${pc_meta.fn_name}" );')
73+
g.pcs_declarations.writeln('\tif (${pc_meta.vpc_calls}) fprintf(fp, ${fstring}, ${pc_meta.vpc_calls}, (${pc_meta.vpc_name}*f)/1000000.0, (${pc_meta.vpc_name}_only_current*f)/1000000.0, (${pc_meta.vpc_name}*f)/${pc_meta.vpc_calls}, "${pc_meta.fn_name}" );')
7074
}
7175
g.pcs_declarations.writeln('\tfclose(fp);')
7276
}

0 commit comments

Comments
 (0)