Geant4-11
G4ProfilerMessenger.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26// G4ProfilerMessenger implementation
27//
28// Author: J.Madsen, 12 November 2020
29// --------------------------------------------------------------------
30
32
33#include "G4Profiler.hh"
34#include "G4UIdirectory.hh"
35#include "G4UIcmdWithABool.hh"
36#include "G4UIcmdWithAString.hh"
37#include "G4TiMemory.hh"
38
39// --------------------------------------------------------------------
40
42
44{
45 profileDirectory = new G4UIdirectory("/profiler/");
46 profileDirectory->SetGuidance("Profiler controls.");
47
48 profileOutputDirectory = new G4UIdirectory("/profiler/output/");
50 "Control the output modes of the profiler.");
51
52#define CREATE_DIR(IDX, DIR, GUIDANCE) \
53 profileTypeDirs.at(IDX) = new G4UIdirectory(DIR); \
54 profileTypeDirs.at(IDX)->SetGuidance(GUIDANCE)
55
56 CREATE_DIR(Type::Run, "/profiler/run/",
57 "Profiler controls at the G4Run level");
58 CREATE_DIR(Type::Event, "/profiler/event/",
59 "Profiler controls at the G4Event level");
60 CREATE_DIR(Type::Track, "/profiler/track/",
61 "Profiler controls at the G4Track level");
62 CREATE_DIR(Type::Step, "/profiler/step/",
63 "Profiler controls at the G4Step level");
64 CREATE_DIR(Type::User, "/profiler/user/",
65 "Profiler controls within user code");
66
67#define SET_ENABLED_CMD(IDX, CMD, CMDLINE, DEFAULT, GUIDANCE) \
68 profileEnableCmds.at(IDX).second = CMDLINE; \
69 profileEnableCmds.at(IDX).first = new G4UIcmdWithABool(CMD, this); \
70 profileEnableCmds.at(IDX).first->SetDefaultValue(DEFAULT); \
71 profileEnableCmds.at(IDX).first->SetGuidance(GUIDANCE); \
72 profileEnableCmds.at(IDX).first->AvailableForStates(G4State_PreInit, \
73 G4State_Idle)
74
75 SET_ENABLED_CMD(Type::Run, "/profiler/run/enable", "run", true,
76 "Record metrics for each G4Run");
77 SET_ENABLED_CMD(Type::Event, "/profiler/event/enable", "event", true,
78 "Record metrics for each G4Event");
79 SET_ENABLED_CMD(Type::Track, "/profiler/track/enable", "track", false,
80 "Record metrics for each G4Track");
81 SET_ENABLED_CMD(Type::Step, "/profiler/step/enable", "step", false,
82 "Record metrics for each G4Step");
83 SET_ENABLED_CMD(Type::User, "/profiler/user/enable", "user", true,
84 "Record metrics for user specified profiling instances");
85
86#define SET_COMPONENTS_CMD(IDX, CMD, CMDLINE, DEFAULTS, GUIDANCE) \
87 profileCompCmds.at(IDX).second = CMDLINE; \
88 profileCompCmds.at(IDX).first = new G4UIcmdWithAString(CMD, this); \
89 profileCompCmds.at(IDX).first->SetDefaultValue(DEFAULTS); \
90 profileCompCmds.at(IDX).first->SetGuidance(GUIDANCE); \
91 profileCompCmds.at(IDX).first->AvailableForStates(G4State_PreInit, \
92 G4State_Idle)
93
94 G4String comps = "wall_clock, cpu_clock, cpu_util, peak_rss";
95
97 Type::Run, "/profiler/run/components", "--run-components", comps,
98 "Measurment types to record for each G4Run (see `timemory-avail -s`)");
100 Type::Event, "/profiler/event/components", "--event-components", comps,
101 "Measurment types to record for each G4Event (see `timemory-avail -s`)");
103 Type::Track, "/profiler/track/components", "--track-components", comps,
104 "Measurment types to record for each G4Track (see `timemory-avail -s`)");
106 Type::Step, "/profiler/step/components", "--step-components", comps,
107 "Measurment types to record for each G4Step (see `timemory-avail -s`)");
108 SET_COMPONENTS_CMD(Type::User, "/profiler/user/components",
109 "--user-components", comps,
110 "Measurment types to record for user specified profiling "
111 "instances (see `timemory-avail -s`)");
112
113#define SET_OUTPUT_CMD(CMD, CMDLINE, DEFAULT, GUIDANCE) \
114 profileGeneralCmds.push_back({ new G4UIcmdWithABool(CMD, this), CMDLINE }); \
115 profileGeneralCmds.back().first->SetDefaultValue(DEFAULT); \
116 profileGeneralCmds.back().first->SetGuidance(GUIDANCE); \
117 profileGeneralCmds.back().first->AvailableForStates(G4State_PreInit, \
118 G4State_Idle)
119
120 SET_OUTPUT_CMD("/profiler/output/dart", "--dart", false,
121 "Enabled Dart output (CTest/CDash data tracking)");
122 SET_OUTPUT_CMD("/profiler/output/json", "--json", true,
123 "Enabled JSON output");
124 SET_OUTPUT_CMD("/profiler/output/text", "--text", true,
125 "Enabled text output");
126 SET_OUTPUT_CMD("/profiler/output/cout", "--cout", false,
127 "Enabled output to console");
128 SET_OUTPUT_CMD("/profiler/output/plot", "--plot", false,
129 "Enabled plotting JSON output");
130
131 SET_OUTPUT_CMD("/profiler/tree", "--tree", true,
132 "Display the results as a call-stack hierarchy.");
133 SET_OUTPUT_CMD("/profiler/flat", "--flat", false,
134 "Display the results as a flat call-stack");
135 SET_OUTPUT_CMD("/profiler/timeline", "--timeline", false,
136 "Do not merge duplicate entries at the same call-stack "
137 "position. May be combined with tree or flat profiles.");
139 "/profiler/per_thread", "--per-thread", false,
140 "Display the results for each individual thread (default: aggregation)");
142 "/profiler/per_event", "--per-event", false,
143 "Display the results for each individual G4event (default: aggregation)");
144}
145
146// --------------------------------------------------------------------
147
149{
150 delete profileDirectory;
152 for(auto& itr : profileTypeDirs)
153 delete itr;
154 for(auto& itr : profileEnableCmds)
155 delete itr.first;
156 for(auto& itr : profileGeneralCmds)
157 delete itr.first;
158 for(auto& itr : profileCompCmds)
159 delete itr.first;
160}
161
162// --------------------------------------------------------------------
163
165{
166 for(size_t i = 0; i < static_cast<size_t>(G4ProfileType::TypeEnd); ++i)
167 {
168 G4UIcmdWithABool* ui = profileEnableCmds.at(i).first;
169 if(command == ui)
170 {
172 return;
173 }
174 }
175
176 // pass the commands to the timemory argparser
177 std::vector<std::string> command_line = { "G4ProfilerMessenger" };
178
179 for(auto& itr : profileGeneralCmds)
180 {
181 G4UIcmdWithABool* ui = itr.first;
182 if(command == ui)
183 {
184 command_line.push_back(itr.second);
185 command_line.push_back(value);
186 break;
187 }
188 }
189
190 for(auto& itr : profileCompCmds)
191 {
192 G4UIcmdWithAString* ui = itr.first;
193 if(command == ui)
194 {
195 command_line.push_back(itr.second);
196#if defined(GEANT4_USE_TIMEMORY)
197 for(auto vitr : tim::delimit(value, ", ;"))
198 command_line.push_back(vitr);
199#endif
200 break;
201 }
202 }
203
204 if(command_line.size() > 1)
205 G4Profiler::Configure(command_line);
206}
207
208// --------------------------------------------------------------------
#define SET_ENABLED_CMD(IDX, CMD, CMDLINE, DEFAULT, GUIDANCE)
#define SET_COMPONENTS_CMD(IDX, CMD, CMDLINE, DEFAULTS, GUIDANCE)
#define SET_OUTPUT_CMD(CMD, CMDLINE, DEFAULT, GUIDANCE)
#define CREATE_DIR(IDX, DIR, GUIDANCE)
void SetNewValue(G4UIcommand *, G4String)
directory_array profileTypeDirs
G4UIdirectory * profileDirectory
boolcmd_array profileEnableCmds
stringcmd_array profileCompCmds
boolcmd_vector profileGeneralCmds
G4UIdirectory * profileOutputDirectory
static void Configure(const std::vector< std::string > &args)
Definition: G4Profiler.cc:91
static void SetEnabled(size_t v, bool val)
Definition: G4Profiler.hh:113
static G4bool GetNewBoolValue(const char *paramString)
void SetGuidance(const char *aGuidance)
Definition: G4UIcommand.hh:156