diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index febe2d3e4e..10dcb2a3d5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -424,6 +424,7 @@ set(MAIN_SRCS script_opt/ZAM/Expr.cc script_opt/ZAM/Inst-Gen.cc script_opt/ZAM/Low-Level.cc + script_opt/ZAM/Profile.cc script_opt/ZAM/Stmt.cc script_opt/ZAM/Support.cc script_opt/ZAM/Vars.cc diff --git a/src/script_opt/ZAM/Profile.cc b/src/script_opt/ZAM/Profile.cc new file mode 100644 index 0000000000..074e78a518 --- /dev/null +++ b/src/script_opt/ZAM/Profile.cc @@ -0,0 +1,53 @@ +// See the file "COPYING" in the main distribution directory for copyright. + +#include "zeek/script_opt/ZAM/Profile.h" + +#include +#include + +#include "zeek/Obj.h" +#include "zeek/script_opt/ProfileFunc.h" +#include "zeek/script_opt/ZAM/ZBody.h" + +namespace zeek::detail { + +ZAMLocInfo::ZAMLocInfo(std::string _func_name, std::shared_ptr _loc, std::shared_ptr _parent) + : loc(std::move(_loc)), parent(std::move(_parent)) { + func_name = func_name_at_loc(_func_name, loc.get()); + + auto main_module = func_name.find("::"); + if ( main_module != std::string::npos ) + modules.insert(func_name.substr(0, main_module)); + + if ( parent ) + parent->AddInModules(modules); +} + +std::string ZAMLocInfo::Describe(bool include_lines) const { + std::string desc; + + if ( blocks ) { + desc = blocks->GetDesc(loc.get()); + if ( parent ) + desc = parent->Describe(false) + ";" + desc; + } + else { + if ( parent ) { + desc = parent->Describe(false); + if ( func_name != parent->FuncName() ) + desc += ";" + func_name; + } + else + desc = func_name; + + if ( include_lines ) { + desc += ";" + func_name + ":" + std::to_string(loc->first_line); + if ( loc->last_line > loc->first_line ) + desc += "-" + std::to_string(loc->last_line); + } + } + + return desc; +} + +} // namespace zeek::detail diff --git a/src/script_opt/ZAM/Profile.h b/src/script_opt/ZAM/Profile.h new file mode 100644 index 0000000000..031ade736a --- /dev/null +++ b/src/script_opt/ZAM/Profile.h @@ -0,0 +1,44 @@ +// See the file "COPYING" in the main distribution directory for copyright. + +// Classes for profiling ZAM execution. + +#pragma once + +#include "zeek/script_opt/ProfileFunc.h" +#include "zeek/util.h" + +namespace zeek::detail { + +class ZAMLocInfo { +public: + // A generalization of the notion of "Location" that includes associating + // with the location a function name, a static parent (i.e., one we can + // determine at compile time, reflecting an outer block or inlining), and + // a group of modules. It's a group of modules rather than a single one + // because of event handler coalescence. + ZAMLocInfo(std::string _func_name, std::shared_ptr _loc, std::shared_ptr _parent); + + const std::string& FuncName() const { return func_name; } + const Location* Loc() const { return loc.get(); } + std::shared_ptr LocPtr() const { return loc; } + std::shared_ptr Parent() { return parent; } + + // Add this location's modules to the target set. + void AddInModules(std::set& target) const { target.insert(modules.begin(), modules.end()); } + + // If include_lines is true, then in the description we include line + // number information, otherwise we omit them. + std::string Describe(bool include_lines) const; + +private: + std::string func_name; + std::set modules; + std::shared_ptr loc; + std::shared_ptr parent; +}; + +// Reports a profile of the different ZAM operations (instructions) +// that executed. +extern void report_ZOP_profile(); + +} // namespace zeek::detail