diff --git a/src/engine/bindjam.cpp b/src/engine/bindjam.cpp index feedd52ba0..5701e17f38 100644 --- a/src/engine/bindjam.cpp +++ b/src/engine/bindjam.cpp @@ -35,6 +35,7 @@ Distributed under the Boost Software License, Version 1.0. #include "mod_string.h" #include "mod_sysinfo.h" #include "mod_version.h" +#include "mod_bind_benchmark.h" #include #include @@ -850,7 +851,8 @@ void bind_jam(FRAME * f) .bind(version_module()) .bind(db_module()) .bind(command_db_module()) - .bind(b2::args::args_module()); + .bind(b2::args::args_module()) + .bind(benchmark_module()); } }} // namespace b2::jam diff --git a/src/engine/build.bat b/src/engine/build.bat index f231248455..d19b6f5e08 100644 --- a/src/engine/build.bat +++ b/src/engine/build.bat @@ -195,6 +195,7 @@ set B2_SOURCES=%B2_SOURCES% mod_string.cpp set B2_SOURCES=%B2_SOURCES% mod_summary.cpp set B2_SOURCES=%B2_SOURCES% mod_sysinfo.cpp set B2_SOURCES=%B2_SOURCES% mod_version.cpp +set B2_SOURCES=%B2_SOURCES% mod_bind_benchmark.cpp set B2_CXXFLAGS=%B2_CXXFLAGS% -DNDEBUG diff --git a/src/engine/build.sh b/src/engine/build.sh index be0e1ff5f6..7f337c0280 100755 --- a/src/engine/build.sh +++ b/src/engine/build.sh @@ -486,6 +486,7 @@ mod_string.cpp \ mod_summary.cpp \ mod_sysinfo.cpp \ mod_version.cpp \ +mod_bind_benchmark.cpp \ " if test_true ${B2_DEBUG_OPT} ; then B2_CXXFLAGS="${B2_CXXFLAGS_DEBUG}" diff --git a/src/engine/builtins.cpp b/src/engine/builtins.cpp index ee3a87ff87..ae41527dc6 100644 --- a/src/engine/builtins.cpp +++ b/src/engine/builtins.cpp @@ -511,6 +511,8 @@ void load_builtins() init_property_set(); init_sequence(); init_order(); + void init_benchmark(); + init_benchmark(); } diff --git a/src/engine/mod_bind_benchmark.cpp b/src/engine/mod_bind_benchmark.cpp new file mode 100644 index 0000000000..15f20875bd --- /dev/null +++ b/src/engine/mod_bind_benchmark.cpp @@ -0,0 +1,93 @@ +/* +Copyright 2026 Paolo Pastori +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE.txt or https://www.bfgroup.xyz/b2/LICENSE.txt) +*/ + +#include "startup.h" +#include "native.h" +#include "output.h" + +#include +#include + + +/* + * Report collected data on stderr every 1< +struct WatchedCounter +{ + static_assert(shift < 32, ""); + static constexpr size_t mask = 1 << shift; + bool last_masked = false; + size_t counter = 0; + clock_t t0; + size_t exit_count; + + /* + * Exit program after num reports. + */ + WatchedCounter(size_t num = 1) : exit_count(num << shift) { t0 = clock(); } + void tick() + { + if (bool(++counter & mask) != last_masked) + { + last_masked = !last_masked; + clock_t t1 = clock(); + double dur = 1000.0 * (t1 - t0) / CLOCKS_PER_SEC; + err_printf("%ld\t%.3f\n", counter, dur); + } + if (counter == exit_count) b2::clean_exit(EXIT_SUCCESS); + } +}; + + +/* + * Legacy binding style. + */ +LIST * native_timed(FRAME * frame, int flags) +{ + //out_puts("LEGACY TIMED\n"); + static WatchedCounter<13> wc(2); + wc.tick(); + return L0; +} + +void init_benchmark() +{ + //char const * args[] = { "any", "*", 0 }; // only used to check for call syntax + char const * * args = nullptr; // do not care of args + declare_native_rule( + "benchmark", + "timed", + args, + native_timed, + 1 + ); +} + + +/* + * New binding style. + */ +namespace b2 { + +// NOTE: returning void seems slower +//void timed_no_args() +value_ref timed_no_args() +{ + //out_puts("NEW BINDING TIMED\n"); + static WatchedCounter<13> wc(2); + wc.tick(); + return value_ref(); +} + +value_ref timed_any_args(list_cref args) +{ + static WatchedCounter<13> wc(2); + wc.tick(); + return value_ref(); +} + +} // namespace b2 diff --git a/src/engine/mod_bind_benchmark.h b/src/engine/mod_bind_benchmark.h new file mode 100644 index 0000000000..5b8b296439 --- /dev/null +++ b/src/engine/mod_bind_benchmark.h @@ -0,0 +1,28 @@ +/* +Copyright 2026 Paolo Pastori +Distributed under the Boost Software License, Version 1.0. +(See accompanying file LICENSE.txt or https://www.bfgroup.xyz/b2/LICENSE.txt) +*/ + +namespace b2 { + +//void timed_no_args(); +value_ref timed_no_args(); +value_ref timed_any_args(list_cref args); + +/* + * New binding style. + */ +struct benchmark_module : b2::bind::module_ +{ + const char * module_name = "benchmark"; + + template + void def(Binder & binder) + { + //binder.def(&b2::timed_no_args, "timed"); + binder.def(&b2::timed_any_args, "timed"); + binder.loaded(); + } +}; +} // namespace b2