Skip to content

Commit

Permalink
add the option to save the profiling details
Browse files Browse the repository at this point in the history
  • Loading branch information
zoujh committed Jan 12, 2023
1 parent 966bba7 commit 6a16b2e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
12 changes: 10 additions & 2 deletions SniperUtil/SniperProfiling/SniperProfiling/SniperProfiling.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <vector>
#include <map>
#include <string>
#include <fstream>

namespace SnierProfilingNS
{
Expand All @@ -35,8 +36,8 @@ namespace sp = SnierProfilingNS;
class SniperProfiling : public SvcBase
{
public:
SniperProfiling(const std::string& name) : SvcBase(name) {}
~SniperProfiling() {}
SniperProfiling(const std::string& name);
virtual ~SniperProfiling();

bool initialize();
bool finalize();
Expand All @@ -50,6 +51,13 @@ class SniperProfiling : public SvcBase
sp::EndEvtHandler* m_endEvtHdl;
sp::BeginAlgHandler* m_beginAlgHdl;
sp::EndAlgHandler* m_endAlgHdl;

// for writing details to a text file
friend sp::EndEvtHandler;
void dumpDetails();

bool m_saveDetails;
std::ofstream* m_fDetails;
};

#endif
53 changes: 50 additions & 3 deletions SniperUtil/SniperProfiling/src/SniperProfiling.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,16 @@ bool sp::BeginEvtHandler::handle(Incident &incident)

struct sp::EndEvtHandler : public IIncidentHandler
{
EndEvtHandler(ExecUnit *domain, SniperTimer* evtTimer)
: IIncidentHandler(domain), h_evtTimer(evtTimer)
EndEvtHandler(ExecUnit *domain, SniperTimer* evtTimer, SniperProfiling* profiling)
: IIncidentHandler(domain),
h_evtTimer(evtTimer),
m_profiling(profiling)
{
m_name = "EndEvtHandler";
}

SniperTimer* h_evtTimer;
SniperProfiling* m_profiling;

bool handle(Incident &incident) override;
};
Expand All @@ -71,6 +74,10 @@ bool sp::EndEvtHandler::handle(Incident &incident)

LogDebug << "The event " << "tooks " << h_evtTimer->elapsed() << "ms" << std::endl;

if (m_profiling) {
m_profiling->dumpDetails();
}

return true;
}

Expand Down Expand Up @@ -127,6 +134,17 @@ bool sp::EndAlgHandler::handle(Incident &incident)
return true;
}

SniperProfiling::SniperProfiling(const std::string &name)
: SvcBase(name),
m_fDetails(nullptr)
{
declProp("SaveDetails", m_saveDetails = false);
}

SniperProfiling::~SniperProfiling()
{
}

bool SniperProfiling::initialize()
{
Task* taskPtr = dynamic_cast<Task*>(m_par);
Expand All @@ -150,7 +168,7 @@ bool SniperProfiling::initialize()
m_beginEvtHdl->regist("BeginEvent");

//create and regist the handler for EndEvent
m_endEvtHdl = new sp::EndEvtHandler(m_par, m_evtTimer);
m_endEvtHdl = new sp::EndEvtHandler(m_par, m_evtTimer, m_saveDetails ? this : nullptr);
m_endEvtHdl->regist("EndEvent");

//create and regist the handler for BeginAlg
Expand All @@ -167,11 +185,31 @@ bool SniperProfiling::initialize()

LogInfo << m_description << std::endl;

if ( m_saveDetails ) {
std::string fname = m_par->scope() + m_par->objName() + '_' + std::to_string(getpid()) + ".profiling";
auto pos = fname.find(':');
while (pos != fname.npos)
{
fname.replace(pos, 1, "_");
pos = fname.find(':');
}
m_fDetails = new std::ofstream(fname);
for (const auto &it : m_algName) {
*m_fDetails << std::left << std::setw(12) << it << " ";
}
*m_fDetails << "Event" << std::endl;
}

return true;
}

bool SniperProfiling::finalize()
{
if ( m_fDetails ) {
m_fDetails->close();
delete m_fDetails;
}

//unregist and delete the handlers
m_beginEvtHdl->unregist("BeginEvent");
m_endEvtHdl->unregist("EndEvent");
Expand Down Expand Up @@ -228,3 +266,12 @@ bool SniperProfiling::finalize()
LogInfo << "finalized successfully" << std::endl;
return true;
}

void SniperProfiling::dumpDetails()
{
for (const auto &it : m_algName)
{
*m_fDetails << std::setw(13) << m_algTimer[it]->elapsed();
}
*m_fDetails << std::setw(13) << m_evtTimer->elapsed() << std::endl;
}

0 comments on commit 6a16b2e

Please sign in to comment.