Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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/");
49 profileOutputDirectory->SetGuidance(
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;
151 delete profileOutputDirectory;
152 for(auto& itr : profileTypeDirs)
153 {
154 delete itr;
155 }
156 for(auto& itr : profileEnableCmds)
157 {
158 delete itr.first;
159 }
160 for(auto& itr : profileGeneralCmds)
161 {
162 delete itr.first;
163 }
164 for(auto& itr : profileCompCmds)
165 {
166 delete itr.first;
167 }
168}
169
170// --------------------------------------------------------------------
171
173{
174 for(size_t i = 0; i < static_cast<size_t>(G4ProfileType::TypeEnd); ++i)
175 {
176 G4UIcmdWithABool* ui = profileEnableCmds.at(i).first;
177 if(command == ui)
178 {
180 return;
181 }
182 }
183
184 // pass the commands to the timemory argparser
185 std::vector<std::string> command_line = { "G4ProfilerMessenger" };
186
187 for(auto& itr : profileGeneralCmds)
188 {
189 G4UIcmdWithABool* ui = itr.first;
190 if(command == ui)
191 {
192 command_line.push_back(itr.second);
193 command_line.push_back(value);
194 break;
195 }
196 }
197
198 for(auto& itr : profileCompCmds)
199 {
200 G4UIcmdWithAString* ui = itr.first;
201 if(command == ui)
202 {
203 command_line.push_back(itr.second);
204#if defined(GEANT4_USE_TIMEMORY)
205 for(auto vitr : tim::delimit(value, ", ;"))
206 command_line.push_back(vitr);
207#endif
208 break;
209 }
210 }
211
212 if(command_line.size() > 1)
213 {
214 G4Profiler::Configure(command_line);
215 }
216}
217
218// --------------------------------------------------------------------
#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) override
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:157