Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: multithreading support for the new profiler column #24061

Merged
merged 1 commit into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,26 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) GenO
b.write_string2('\n // V preincludes:\n', g.preincludes.str())
b.write_string2('\n// V cheaders:\n', g.cheaders.str())
if g.pcs_declarations.len > 0 {
g.pcs_declarations.writeln('double prof_measured_time = 0.0;') // does not work for multithreaded
g.pcs_declarations.writeln('// V profile thread local:')
g.pcs_declarations.writeln('#if defined(__cplusplus) && __cplusplus >= 201103L')
g.pcs_declarations.writeln('\t#define PROF_THREAD_LOCAL thread_local')
g.pcs_declarations.writeln('#elif defined(__GNUC__) && __GNUC__ < 5')
g.pcs_declarations.writeln('\t#define PROF_THREAD_LOCAL __thread')
g.pcs_declarations.writeln('#elif defined(_MSC_VER)')
g.pcs_declarations.writeln('\t#define PROF_THREAD_LOCAL __declspec(thread)')
g.pcs_declarations.writeln('#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)')
g.pcs_declarations.writeln('\t#define PROF_THREAD_LOCAL _Thread_local')
g.pcs_declarations.writeln('#endif')
g.pcs_declarations.writeln('#ifndef PROF_THREAD_LOCAL')
g.pcs_declarations.writeln('\t#if defined(__GNUC__)')
g.pcs_declarations.writeln('\t\t#define PROF_THREAD_LOCAL __thread')
g.pcs_declarations.writeln('\t#endif')
g.pcs_declarations.writeln('#endif')
g.pcs_declarations.writeln('#ifdef PROF_THREAD_LOCAL')
g.pcs_declarations.writeln('\tstatic PROF_THREAD_LOCAL double prof_measured_time = 0.0;')
g.pcs_declarations.writeln('#else')
g.pcs_declarations.writeln('\tdouble prof_measured_time = 0.0; // multithreaded: wrong values for func times without its children')
g.pcs_declarations.writeln('#endif')
b.write_string2('\n// V profile counters:\n', g.pcs_declarations.str())
}
b.write_string2('\n// V includes:\n', g.includes.str())
Expand Down
10 changes: 5 additions & 5 deletions vlib/v/gen/c/profile.v
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ fn (mut g Gen) profile_fn(fn_decl ast.FnDecl) {
}
g.writeln('\tdouble _PROF_FN_START = ${measure_fn_name}();')
g.writeln('\tdouble _PROF_PREV_MEASURED_TIME = prof_measured_time;')
g.writeln('\tif(v__profile_enabled) { ${fn_profile_counter_name_calls}++; } // ${fn_name}')
g.writeln('if(v__profile_enabled) { ${fn_profile_counter_name_calls}++; } // ${fn_name}')
g.writeln('')
g.defer_profile_code = '\tif(v__profile_enabled) { double _PROF_ELAPSED = ${measure_fn_name}() - _PROF_FN_START; '
g.defer_profile_code += '${fn_profile_counter_name} += _PROF_ELAPSED; '
g.defer_profile_code += '${fn_profile_counter_name}_only_current += _PROF_ELAPSED - (prof_measured_time - _PROF_PREV_MEASURED_TIME); '
g.defer_profile_code += 'prof_measured_time = _PROF_PREV_MEASURED_TIME + _PROF_ELAPSED; }'
g.defer_profile_code = '\tif(v__profile_enabled) { \n\t\tdouble _PROF_ELAPSED = ${measure_fn_name}() - _PROF_FN_START;\n'
g.defer_profile_code += '\t\t${fn_profile_counter_name} += _PROF_ELAPSED;\n'
g.defer_profile_code += '\t\t${fn_profile_counter_name}_only_current += _PROF_ELAPSED - (prof_measured_time - _PROF_PREV_MEASURED_TIME);\n'
g.defer_profile_code += '\t\tprof_measured_time = _PROF_PREV_MEASURED_TIME + _PROF_ELAPSED;\n\t}'
if should_restore_v__profile_enabled {
g.defer_profile_code += '\n\t\tv__profile_enabled = _prev_v__profile_enabled;'
}
Expand Down
Loading