ZAM classes in support of finer-grained profiling

This commit is contained in:
Vern Paxson 2024-03-10 13:31:14 -07:00 committed by Tim Wojtulewicz
parent 037f76e384
commit 8d762eea54
3 changed files with 98 additions and 0 deletions

View file

@ -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

View file

@ -0,0 +1,53 @@
// See the file "COPYING" in the main distribution directory for copyright.
#include "zeek/script_opt/ZAM/Profile.h"
#include <unordered_map>
#include <unordered_set>
#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<Location> _loc, std::shared_ptr<ZAMLocInfo> _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

View file

@ -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<Location> _loc, std::shared_ptr<ZAMLocInfo> _parent);
const std::string& FuncName() const { return func_name; }
const Location* Loc() const { return loc.get(); }
std::shared_ptr<Location> LocPtr() const { return loc; }
std::shared_ptr<ZAMLocInfo> Parent() { return parent; }
// Add this location's modules to the target set.
void AddInModules(std::set<std::string>& 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<std::string> modules;
std::shared_ptr<Location> loc;
std::shared_ptr<ZAMLocInfo> parent;
};
// Reports a profile of the different ZAM operations (instructions)
// that executed.
extern void report_ZOP_profile();
} // namespace zeek::detail