Skip to content

Commit 22414eb

Browse files
authored
cgen: support measuring programs, that use multiple threads in the new profiler column (turn prof_measured_time into a thread local, for the supported C compilers) (#24061)
1 parent 8a23ea6 commit 22414eb

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

vlib/v/gen/c/cgen.v

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,26 @@ 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
577+
g.pcs_declarations.writeln('// V profile thread local:')
578+
g.pcs_declarations.writeln('#if defined(__cplusplus) && __cplusplus >= 201103L')
579+
g.pcs_declarations.writeln('\t#define PROF_THREAD_LOCAL thread_local')
580+
g.pcs_declarations.writeln('#elif defined(__GNUC__) && __GNUC__ < 5')
581+
g.pcs_declarations.writeln('\t#define PROF_THREAD_LOCAL __thread')
582+
g.pcs_declarations.writeln('#elif defined(_MSC_VER)')
583+
g.pcs_declarations.writeln('\t#define PROF_THREAD_LOCAL __declspec(thread)')
584+
g.pcs_declarations.writeln('#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)')
585+
g.pcs_declarations.writeln('\t#define PROF_THREAD_LOCAL _Thread_local')
586+
g.pcs_declarations.writeln('#endif')
587+
g.pcs_declarations.writeln('#ifndef PROF_THREAD_LOCAL')
588+
g.pcs_declarations.writeln('\t#if defined(__GNUC__)')
589+
g.pcs_declarations.writeln('\t\t#define PROF_THREAD_LOCAL __thread')
590+
g.pcs_declarations.writeln('\t#endif')
591+
g.pcs_declarations.writeln('#endif')
592+
g.pcs_declarations.writeln('#ifdef PROF_THREAD_LOCAL')
593+
g.pcs_declarations.writeln('\tstatic PROF_THREAD_LOCAL double prof_measured_time = 0.0;')
594+
g.pcs_declarations.writeln('#else')
595+
g.pcs_declarations.writeln('\tdouble prof_measured_time = 0.0; // multithreaded: wrong values for func times without its children')
596+
g.pcs_declarations.writeln('#endif')
578597
b.write_string2('\n// V profile counters:\n', g.pcs_declarations.str())
579598
}
580599
b.write_string2('\n// V includes:\n', g.includes.str())

vlib/v/gen/c/profile.v

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ fn (mut g Gen) profile_fn(fn_decl ast.FnDecl) {
3333
}
3434
g.writeln('\tdouble _PROF_FN_START = ${measure_fn_name}();')
3535
g.writeln('\tdouble _PROF_PREV_MEASURED_TIME = prof_measured_time;')
36-
g.writeln('\tif(v__profile_enabled) { ${fn_profile_counter_name_calls}++; } // ${fn_name}')
36+
g.writeln('if(v__profile_enabled) { ${fn_profile_counter_name_calls}++; } // ${fn_name}')
3737
g.writeln('')
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; }'
38+
g.defer_profile_code = '\tif(v__profile_enabled) { \n\t\tdouble _PROF_ELAPSED = ${measure_fn_name}() - _PROF_FN_START;\n'
39+
g.defer_profile_code += '\t\t${fn_profile_counter_name} += _PROF_ELAPSED;\n'
40+
g.defer_profile_code += '\t\t${fn_profile_counter_name}_only_current += _PROF_ELAPSED - (prof_measured_time - _PROF_PREV_MEASURED_TIME);\n'
41+
g.defer_profile_code += '\t\tprof_measured_time = _PROF_PREV_MEASURED_TIME + _PROF_ELAPSED;\n\t}'
4242
if should_restore_v__profile_enabled {
4343
g.defer_profile_code += '\n\t\tv__profile_enabled = _prev_v__profile_enabled;'
4444
}

0 commit comments

Comments
 (0)