framework for --enable-ZAM-profiling configuration

profiles go to zprof.log rather than stdout
This commit is contained in:
Vern Paxson 2024-03-10 13:35:07 -07:00 committed by Tim Wojtulewicz
parent 8d762eea54
commit dc376953fa
6 changed files with 67 additions and 5 deletions

View file

@ -241,6 +241,9 @@
/* Spicy analyzers built in. */ /* Spicy analyzers built in. */
#cmakedefine01 USE_SPICY_ANALYZERS #cmakedefine01 USE_SPICY_ANALYZERS
/* Enable/disable ZAM profiling capability */
#cmakedefine ENABLE_ZAM_PROFILE
/* String with host architecture (e.g., "linux-x86_64") */ /* String with host architecture (e.g., "linux-x86_64") */
#define HOST_ARCHITECTURE "@HOST_ARCHITECTURE@" #define HOST_ARCHITECTURE "@HOST_ARCHITECTURE@"

6
configure vendored
View file

@ -65,6 +65,7 @@ Usage: $0 [OPTION]... [VAR=VALUE]...
--enable-static-binpac build binpac statically (ignored if --with-binpac is specified) --enable-static-binpac build binpac statically (ignored if --with-binpac is specified)
--enable-static-broker build Broker statically (ignored if --with-broker is specified) --enable-static-broker build Broker statically (ignored if --with-broker is specified)
--enable-werror build with -Werror --enable-werror build with -Werror
--enable-ZAM-profiling build with ZAM profiling enabled (--enable-debug implies this)
--disable-af-packet don't include native AF_PACKET support (Linux only) --disable-af-packet don't include native AF_PACKET support (Linux only)
--disable-archiver don't build or install zeek-archiver tool --disable-archiver don't build or install zeek-archiver tool
--disable-auxtools don't build or install auxiliary tools --disable-auxtools don't build or install auxiliary tools
@ -254,9 +255,11 @@ while [ $# -ne 0 ]; do
--enable-coverage) --enable-coverage)
append_cache_entry ENABLE_COVERAGE BOOL true append_cache_entry ENABLE_COVERAGE BOOL true
append_cache_entry ENABLE_DEBUG BOOL true append_cache_entry ENABLE_DEBUG BOOL true
append_cache_entry ENABLE_ZAM_PROFILE BOOL true
;; ;;
--enable-debug) --enable-debug)
append_cache_entry ENABLE_DEBUG BOOL true append_cache_entry ENABLE_DEBUG BOOL true
append_cache_entry ENABLE_ZAM_PROFILE BOOL true
;; ;;
--enable-fuzzers) --enable-fuzzers)
append_cache_entry ZEEK_ENABLE_FUZZERS BOOL true append_cache_entry ZEEK_ENABLE_FUZZERS BOOL true
@ -280,6 +283,9 @@ while [ $# -ne 0 ]; do
--enable-werror) --enable-werror)
append_cache_entry BUILD_WITH_WERROR BOOL true append_cache_entry BUILD_WITH_WERROR BOOL true
;; ;;
--enable-ZAM-profiling)
append_cache_entry ENABLE_ZAM_PROFILE BOOL true
;;
--disable-af-packet) --disable-af-packet)
append_cache_entry DISABLE_AF_PACKET BOOL true append_cache_entry DISABLE_AF_PACKET BOOL true
;; ;;

View file

@ -192,7 +192,7 @@ static void print_analysis_help() {
fprintf(stderr, " no-ZAM-opt omit low-level ZAM optimization\n"); fprintf(stderr, " no-ZAM-opt omit low-level ZAM optimization\n");
fprintf(stderr, " optimize-all optimize all scripts, even inlined ones\n"); fprintf(stderr, " optimize-all optimize all scripts, even inlined ones\n");
fprintf(stderr, " optimize-AST optimize the (transformed) AST; implies xform\n"); fprintf(stderr, " optimize-AST optimize the (transformed) AST; implies xform\n");
fprintf(stderr, " profile-ZAM generate to stdout a ZAM execution profile; implies -O ZAM\n"); fprintf(stderr, " profile-ZAM generate to zprof.out a ZAM execution profile; implies -O ZAM\n");
fprintf(stderr, " report-recursive report on recursive functions and exit\n"); fprintf(stderr, " report-recursive report on recursive functions and exit\n");
fprintf(stderr, " xform transform scripts to \"reduced\" form\n"); fprintf(stderr, " xform transform scripts to \"reduced\" form\n");
@ -245,7 +245,7 @@ static void set_analysis_option(const char* opt, Options& opts) {
else if ( util::streq(opt, "optimize-AST") ) else if ( util::streq(opt, "optimize-AST") )
a_o.activate = a_o.optimize_AST = true; a_o.activate = a_o.optimize_AST = true;
else if ( util::streq(opt, "profile-ZAM") ) else if ( util::streq(opt, "profile-ZAM") )
a_o.activate = a_o.gen_ZAM_code = a_o.profile_ZAM = true; a_o.activate = a_o.profile_ZAM = true;
else if ( util::streq(opt, "report-C++") ) else if ( util::streq(opt, "report-C++") )
a_o.report_CPP = true; a_o.report_CPP = true;
else if ( util::streq(opt, "report-recursive") ) else if ( util::streq(opt, "report-recursive") )

View file

@ -20,6 +20,7 @@
#include "zeek/Val.h" #include "zeek/Val.h"
#include "zeek/module_util.h" #include "zeek/module_util.h"
#include "zeek/script_opt/IDOptInfo.h" #include "zeek/script_opt/IDOptInfo.h"
#include "zeek/script_opt/ScriptOpt.h"
#include "zeek/script_opt/StmtOptInfo.h" #include "zeek/script_opt/StmtOptInfo.h"
#include "zeek/script_opt/UsageAnalyzer.h" #include "zeek/script_opt/UsageAnalyzer.h"
@ -399,7 +400,10 @@ void add_type(ID* id, TypePtr t, std::unique_ptr<std::vector<AttrPtr>> attr) {
static std::set<std::string> all_module_names; static std::set<std::string> all_module_names;
void add_module(const char* module_name) { all_module_names.emplace(module_name); } void add_module(const char* module_name) {
all_module_names.emplace(module_name);
switch_to_module(module_name);
}
const std::set<std::string>& module_names() { return all_module_names; } const std::set<std::string>& module_names() { return all_module_names; }

View file

@ -17,6 +17,7 @@
#include "zeek/script_opt/UsageAnalyzer.h" #include "zeek/script_opt/UsageAnalyzer.h"
#include "zeek/script_opt/UseDefs.h" #include "zeek/script_opt/UseDefs.h"
#include "zeek/script_opt/ZAM/Compile.h" #include "zeek/script_opt/ZAM/Compile.h"
#include "zeek/script_opt/ZAM/Profile.h"
namespace zeek::detail { namespace zeek::detail {
@ -310,6 +311,22 @@ static void init_options() {
add_file_analysis_pattern(analysis_options, zo); add_file_analysis_pattern(analysis_options, zo);
} }
if ( analysis_options.profile_ZAM ) {
auto zsamp = getenv("ZEEK_ZAM_PROF_SAMPLING_RATE");
if ( zsamp ) {
analysis_options.profile_sampling_rate = atoi(zsamp);
if ( analysis_options.profile_sampling_rate == 0 ) {
fprintf(stderr, "bad ZAM sampling profile rate from $ZEEK_ZAM_PROF_SAMPLING_RATE: %s\n", zsamp);
analysis_options.profile_ZAM = false;
}
}
// If no ZAM generation options have been specified, default to
// the usual "-O ZAM" profile. But if they have, honor those.
if ( ! analysis_options.gen_ZAM_code )
analysis_options.gen_ZAM = true;
}
if ( analysis_options.gen_ZAM ) { if ( analysis_options.gen_ZAM ) {
analysis_options.gen_ZAM_code = true; analysis_options.gen_ZAM_code = true;
analysis_options.inliner = true; analysis_options.inliner = true;
@ -437,6 +454,19 @@ static void analyze_scripts_for_ZAM() {
auto pfs = std::make_shared<ProfileFuncs>(funcs, nullptr, true); auto pfs = std::make_shared<ProfileFuncs>(funcs, nullptr, true);
if ( analysis_options.profile_ZAM ) {
#ifdef ENABLE_ZAM_PROFILE
blocks = std::make_unique<BlockAnalyzer>(funcs);
const auto prof_filename = "zprof.out";
analysis_options.profile_file = fopen(prof_filename, "w");
if ( ! analysis_options.profile_file )
reporter->FatalError("cannot create ZAM profiling log %s", prof_filename);
#else
fprintf(stderr, "warning: zeek was not built with --enable-ZAM-profiling\n");
analysis_options.profile_ZAM = false;
#endif
}
bool report_recursive = analysis_options.report_recursive; bool report_recursive = analysis_options.report_recursive;
std::unique_ptr<Inliner> inl; std::unique_ptr<Inliner> inl;
if ( analysis_options.inliner ) if ( analysis_options.inliner )
@ -599,10 +629,19 @@ void profile_script_execution() {
if ( analysis_options.profile_ZAM ) { if ( analysis_options.profile_ZAM ) {
report_ZOP_profile(); report_ZOP_profile();
ProfMap module_prof;
for ( auto& f : funcs ) { for ( auto& f : funcs ) {
if ( f.Body()->Tag() == STMT_ZAM ) if ( f.Body()->Tag() == STMT_ZAM ) {
cast_intrusive<ZBody>(f.Body())->ProfileExecution(); auto zb = cast_intrusive<ZBody>(f.Body());
zb->ProfileExecution(module_prof);
}
} }
for ( auto& mp : module_prof )
if ( mp.second.first > 0 )
fprintf(analysis_options.profile_file, "module %s sampled CPU time %.06f, %d sampled instructions\n",
mp.first.c_str(), mp.second.second, static_cast<int>(mp.second.first));
} }
} }

View file

@ -75,6 +75,12 @@ struct AnalyOpt {
// Produce a profile of ZAM execution. // Produce a profile of ZAM execution.
bool profile_ZAM = false; bool profile_ZAM = false;
// ZAM profiling sampling rate. Set via ZEEK_ZAM_PROF_SAMPLING_RATE.
int profile_sampling_rate = 100;
// An associated file to which to write the profile.
FILE* profile_file = nullptr;
// If true, dump out transformed code: the results of reducing // If true, dump out transformed code: the results of reducing
// interpreted scripts, and, if optimize is set, of then optimizing // interpreted scripts, and, if optimize is set, of then optimizing
// them. // them.
@ -217,6 +223,10 @@ extern void analyze_global_stmts(Stmt* stmts);
// Returns the body and scope for the previously analyzed global statements. // Returns the body and scope for the previously analyzed global statements.
extern std::pair<StmtPtr, ScopePtr> get_global_stmts(); extern std::pair<StmtPtr, ScopePtr> get_global_stmts();
// Informs script optimization that parsing is switching to the given module.
// Used to associate module names with profiling information.
extern void switch_to_module(const char* module);
// Add a pattern to the "only_funcs" list. // Add a pattern to the "only_funcs" list.
extern void add_func_analysis_pattern(AnalyOpt& opts, const char* pat); extern void add_func_analysis_pattern(AnalyOpt& opts, const char* pat);