Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 "G4TiMemory.hh"
35#include "G4UIcmdWithABool.hh"
36#include "G4UIcmdWithAString.hh"
37#include "G4UIdirectory.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("Control the output modes of the profiler.");
50
51#define CREATE_DIR(IDX, DIR, GUIDANCE) \
52 profileTypeDirs.at(IDX) = new G4UIdirectory(DIR); \
53 profileTypeDirs.at(IDX)->SetGuidance(GUIDANCE)
54
55 CREATE_DIR(Type::Run, "/profiler/run/", "Profiler controls at the G4Run level");
56 CREATE_DIR(Type::Event, "/profiler/event/", "Profiler controls at the G4Event level");
57 CREATE_DIR(Type::Track, "/profiler/track/", "Profiler controls at the G4Track level");
58 CREATE_DIR(Type::Step, "/profiler/step/", "Profiler controls at the G4Step level");
59 CREATE_DIR(Type::User, "/profiler/user/", "Profiler controls within user code");
60
61#define SET_ENABLED_CMD(IDX, CMD, CMDLINE, DEFAULT, GUIDANCE) \
62 profileEnableCmds.at(IDX).second = CMDLINE; \
63 profileEnableCmds.at(IDX).first = new G4UIcmdWithABool(CMD, this); \
64 profileEnableCmds.at(IDX).first->SetDefaultValue(DEFAULT); \
65 profileEnableCmds.at(IDX).first->SetGuidance(GUIDANCE); \
66 profileEnableCmds.at(IDX).first->AvailableForStates(G4State_PreInit, G4State_Idle)
67
68 SET_ENABLED_CMD(Type::Run, "/profiler/run/enable", "run", true, "Record metrics for each G4Run");
69 SET_ENABLED_CMD(Type::Event, "/profiler/event/enable", "event", true,
70 "Record metrics for each G4Event");
71 SET_ENABLED_CMD(Type::Track, "/profiler/track/enable", "track", false,
72 "Record metrics for each G4Track");
73 SET_ENABLED_CMD(Type::Step, "/profiler/step/enable", "step", false,
74 "Record metrics for each G4Step");
75 SET_ENABLED_CMD(Type::User, "/profiler/user/enable", "user", true,
76 "Record metrics for user specified profiling instances");
77
78#define SET_COMPONENTS_CMD(IDX, CMD, CMDLINE, DEFAULTS, GUIDANCE) \
79 profileCompCmds.at(IDX).second = CMDLINE; \
80 profileCompCmds.at(IDX).first = new G4UIcmdWithAString(CMD, this); \
81 profileCompCmds.at(IDX).first->SetDefaultValue(DEFAULTS); \
82 profileCompCmds.at(IDX).first->SetGuidance(GUIDANCE); \
83 profileCompCmds.at(IDX).first->AvailableForStates(G4State_PreInit, G4State_Idle)
84
85 G4String comps = "wall_clock, cpu_clock, cpu_util, peak_rss";
86
87 SET_COMPONENTS_CMD(Type::Run, "/profiler/run/components", "--run-components", comps,
88 "Measurment types to record for each G4Run (see `timemory-avail -s`)");
89 SET_COMPONENTS_CMD(Type::Event, "/profiler/event/components", "--event-components", comps,
90 "Measurment types to record for each G4Event (see `timemory-avail -s`)");
91 SET_COMPONENTS_CMD(Type::Track, "/profiler/track/components", "--track-components", comps,
92 "Measurment types to record for each G4Track (see `timemory-avail -s`)");
93 SET_COMPONENTS_CMD(Type::Step, "/profiler/step/components", "--step-components", comps,
94 "Measurment types to record for each G4Step (see `timemory-avail -s`)");
95 SET_COMPONENTS_CMD(Type::User, "/profiler/user/components", "--user-components", comps,
96 "Measurment types to record for user specified profiling "
97 "instances (see `timemory-avail -s`)");
98
99#define SET_OUTPUT_CMD(CMD, CMDLINE, DEFAULT, GUIDANCE) \
100 profileGeneralCmds.push_back({new G4UIcmdWithABool(CMD, this), CMDLINE}); \
101 profileGeneralCmds.back().first->SetDefaultValue(DEFAULT); \
102 profileGeneralCmds.back().first->SetGuidance(GUIDANCE); \
103 profileGeneralCmds.back().first->AvailableForStates(G4State_PreInit, G4State_Idle)
104
105 SET_OUTPUT_CMD("/profiler/output/dart", "--dart", false,
106 "Enabled Dart output (CTest/CDash data tracking)");
107 SET_OUTPUT_CMD("/profiler/output/json", "--json", true, "Enabled JSON output");
108 SET_OUTPUT_CMD("/profiler/output/text", "--text", true, "Enabled text output");
109 SET_OUTPUT_CMD("/profiler/output/cout", "--cout", false, "Enabled output to console");
110 SET_OUTPUT_CMD("/profiler/output/plot", "--plot", false, "Enabled plotting JSON output");
111
112 SET_OUTPUT_CMD("/profiler/tree", "--tree", true,
113 "Display the results as a call-stack hierarchy.");
114 SET_OUTPUT_CMD("/profiler/flat", "--flat", false, "Display the results as a flat call-stack");
115 SET_OUTPUT_CMD("/profiler/timeline", "--timeline", false,
116 "Do not merge duplicate entries at the same call-stack "
117 "position. May be combined with tree or flat profiles.");
118 SET_OUTPUT_CMD("/profiler/per_thread", "--per-thread", false,
119 "Display the results for each individual thread (default: aggregation)");
120 SET_OUTPUT_CMD("/profiler/per_event", "--per-event", false,
121 "Display the results for each individual G4event (default: aggregation)");
122}
123
124// --------------------------------------------------------------------
125
127{
128 delete profileDirectory;
129 delete profileOutputDirectory;
130 for (auto& itr : profileTypeDirs) {
131 delete itr;
132 }
133 for (auto& itr : profileEnableCmds) {
134 delete itr.first;
135 }
136 for (auto& itr : profileGeneralCmds) {
137 delete itr.first;
138 }
139 for (auto& itr : profileCompCmds) {
140 delete itr.first;
141 }
142}
143
144// --------------------------------------------------------------------
145
147{
148 for (size_t i = 0; i < static_cast<size_t>(G4ProfileType::TypeEnd); ++i) {
149 G4UIcmdWithABool* ui = profileEnableCmds.at(i).first;
150 if (command == ui) {
152 return;
153 }
154 }
155
156 // pass the commands to the timemory argparser
157 std::vector<std::string> command_line = {"G4ProfilerMessenger"};
158
159 for (auto& itr : profileGeneralCmds) {
160 G4UIcmdWithABool* ui = itr.first;
161 if (command == ui) {
162 command_line.push_back(itr.second.c_str());
163 command_line.push_back(value);
164 break;
165 }
166 }
167
168 for (auto& itr : profileCompCmds) {
169 G4UIcmdWithAString* ui = itr.first;
170 if (command == ui) {
171 command_line.push_back(itr.second);
172#if defined(GEANT4_USE_TIMEMORY)
173 for (auto vitr : tim::delimit(value, ", ;"))
174 command_line.push_back(vitr);
175#endif
176 break;
177 }
178 }
179
180 if (command_line.size() > 1) {
181 G4Profiler::Configure(command_line);
182 }
183}
184
185// --------------------------------------------------------------------
#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)
static G4bool GetNewBoolValue(const char *paramString)
void SetGuidance(const char *aGuidance)