From 22414eb9023131ff79745f880ac77a1cf9a8f95d Mon Sep 17 00:00:00 2001 From: "Eliyaan (Nopana)" <103932369+Eliyaan@users.noreply.github.com> Date: Fri, 28 Mar 2025 11:41:23 +0100 Subject: [PATCH] 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) --- vlib/v/gen/c/cgen.v | 21 ++++++++++++++++++++- vlib/v/gen/c/profile.v | 10 +++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index a0228714c..1b03aa3a5 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -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()) diff --git a/vlib/v/gen/c/profile.v b/vlib/v/gen/c/profile.v index e17075274..2da6d75c9 100644 --- a/vlib/v/gen/c/profile.v +++ b/vlib/v/gen/c/profile.v @@ -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;' } -- 2.39.5