otsdaq  3.11.00
Iterator.h
1 #ifndef _ots_Iterator_h
2 #define _ots_Iterator_h
3 
4 #include <atomic> //for std::atomic
5 #include <memory> //for std::shared_ptr
6 #include <mutex> //for std::mutex
7 #include <string>
8 #include "otsdaq/TablePlugins/IterateTable.h"
9 #include "otsdaq/XmlUtilities/HttpXmlDocument.h"
10 
11 #include "otsdaq/ConfigurationInterface/ConfigurationManagerRW.h"
12 
13 // clang-format off
14 
15 namespace ots
16 {
17 class GatewaySupervisor;
18 class ConfigurationManagerRW;
19 
20 class Iterator
21 {
22  friend class GatewaySupervisor;
23 
24  public:
25  Iterator(GatewaySupervisor* supervisor);
26  ~Iterator(void);
27 
28  static const std::string RESERVED_GEN_PLAN_NAME;
29 
31  struct OutputFile
32  {
33  std::string subsystem;
34  std::string path;
35  std::string label;
36  std::string iconName;
37  };
38 
39  void playIterationPlan (HttpXmlDocument& xmldoc, const std::string& planName);
40  void playGeneratedIterationPlan (HttpXmlDocument& xmldoc, const std::string& parametersCSV);
41  void playGeneratedIterationPlan (HttpXmlDocument& xmldoc, const std::string& fsmName, const std::string& configAlias, uint64_t durationSeconds = -1, unsigned int numberOfRuns = 1, bool keepConfiguration = false, const std::string& logEntry = "");
42  void pauseIterationPlan (HttpXmlDocument& xmldoc);
43  void haltIterationPlan (HttpXmlDocument& xmldoc);
45 
46 
47  bool handleCommandRequest (HttpXmlDocument& xmldoc, const std::string& command, const std::string& parameter);
48 
49  bool isIteratorBusy (void) const { return iteratorBusy_; }
50  private:
51 
52  void playIterationPlanPrivate (HttpXmlDocument& xmldoc, const std::string& planName);
53  static std::vector<
54  IterateTable::Command> generateIterationPlan (const std::string& fsmName, const std::string& configAlias, uint64_t durationSeconds = -1, unsigned int numberOfRuns = 1);
55 
57  struct IteratorWorkLoopStruct
58  {
59  IteratorWorkLoopStruct(Iterator* iterator, ConfigurationManagerRW* cfgMgr)
60  : theIterator_(iterator)
61  , cfgMgr_(cfgMgr)
62  , originalTrackChanges_(false)
63  , running_(false)
64  , commandBusy_(false)
65  , doPauseAction_(false)
66  , doHaltAction_(false)
67  , doResumeAction_(false)
68  , commandIndex_((unsigned int)-1)
69  {
70  }
71 
72  Iterator* theIterator_;
73  ConfigurationManagerRW* cfgMgr_;
74  bool originalTrackChanges_;
75  std::string originalConfigGroup_;
76  TableGroupKey originalConfigKey_;
77 
78  bool running_, commandBusy_;
79  bool doPauseAction_, doHaltAction_, doResumeAction_;
80  bool onlyConfigIfNotConfigured_ = false;
81 
82  std::string activePlan_;
83  std::vector<IterateTable::Command> commands_;
84  std::vector<unsigned int> commandIterations_;
85  unsigned int commandIndex_;
86  std::vector<unsigned int> stepIndexStack_;
87  std::vector<std::string> stepLabelStack_;
88  time_t originalDurationInSeconds_;
89 
90  // associated with FSM
91  std::string fsmName_, fsmRunAlias_;
92  unsigned int fsmNextRunNumber_;
93  bool runIsDone_;
94  bool waitIsDone_;
95 
96  std::vector<std::string> fsmCommandParameters_;
97  std::vector<bool> targetsDone_;
98 
99  bool remoteConfigureQueued_ = false;
100 
101  std::vector<OutputFile> outputFiles_;
102  std::string macroArgsSummary_;
103 
106  {
107  std::atomic<bool> done{false}, abort{false};
108  std::mutex mutex;
109  std::string error;
110  uint64_t iterationsDone = 0;
111  uint64_t iterationsTotal = 0;
112  int progress = 0;
113  std::vector<std::pair<std::string /*path*/, std::string /*"arg = val, ..."*/>>
115  };
116  std::shared_ptr<RemoteMacroRun> remoteMacroRun_;
117 
118  }; // end declaration of iterator workloop members
119 
120  static void IteratorWorkLoop(Iterator* iterator);
121  static void startCommand(IteratorWorkLoopStruct* iteratorStruct);
122  static bool checkCommand(IteratorWorkLoopStruct* iteratorStruct);
123 
124  static void startCommandChooseFSM(IteratorWorkLoopStruct* iteratorStruct, const std::string& fsmName);
125 
126  static bool commandSkipsIfConfigured(IteratorWorkLoopStruct* iteratorStruct);
127  static void startCommandConfigureActive(IteratorWorkLoopStruct* iteratorStruct);
128  static void startCommandConfigureAlias(IteratorWorkLoopStruct* iteratorStruct, const std::string& systemAlias);
129  static void startCommandConfigureGroup(IteratorWorkLoopStruct* iteratorStruct);
130  static bool checkCommandConfigure(IteratorWorkLoopStruct* iteratorStruct);
131 
132  static void startCommandModifyActive(IteratorWorkLoopStruct* iteratorStruct);
133 
134  static void startCommandMacro(IteratorWorkLoopStruct* iteratorStruct, bool isFEMacro);
135  static bool checkCommandMacro(IteratorWorkLoopStruct* iteratorStruct, bool isFEMacro);
136 
137  static void startCommandBeginLabel(IteratorWorkLoopStruct* iteratorStruct);
138  static void startCommandRepeatLabel(IteratorWorkLoopStruct* iteratorStruct);
139 
140  static void startCommandRun(IteratorWorkLoopStruct* iteratorStruct);
141  static bool checkCommandRun(IteratorWorkLoopStruct* iteratorStruct);
142  static std::string buildIteratorLogEntry(IteratorWorkLoopStruct* iteratorStruct);
143 
144  static void startCommandWait(IteratorWorkLoopStruct* iteratorStruct);
145  static bool checkCommandWait(IteratorWorkLoopStruct* iteratorStruct);
146 
147  static void startCommandFSMTransition(IteratorWorkLoopStruct* iteratorStruct, const std::string& transitionCommand);
148  static bool checkCommandFSMTransition(IteratorWorkLoopStruct* iteratorStruct, const std::string& finalState);
149 
150  static void startRemoteCommandFSMTransition(IteratorWorkLoopStruct* iteratorStruct, const std::string& transitionCommand, const std::string& targetSubsystem);
151  static bool checkRemoteCommandFSMTransition(IteratorWorkLoopStruct* iteratorStruct, const std::string& finalState, const std::string& targetSubsystem);
152 
153  static void startRemoteCommandConfigure(IteratorWorkLoopStruct* iteratorStruct, const std::string& systemAlias, const std::string& targetSubsystem);
154  static bool checkRemoteCommandConfigure(IteratorWorkLoopStruct* iteratorStruct, const std::string& targetSubsystem);
155 
156  static void startRemoteCommandMacro(IteratorWorkLoopStruct* iteratorStruct, bool isFEMacro);
158  static std::string feMacroArgBaseName(const std::string& argName);
159  static bool checkRemoteCommandMacro(IteratorWorkLoopStruct* iteratorStruct, bool isFEMacro);
164  struct MacroLoopSpec
165  {
166  struct Arg
167  {
168  std::string name;
169  enum { LONG, DOUBLE, STRING } type = LONG;
170  long lInit = 0, lStep = 0;
171  double dInit = 0, dStep = 0;
172  std::string sVal;
173  };
174  std::vector<unsigned long> dimIterations;
175  std::vector<std::vector<Arg>> dimArgs;
176  std::vector<std::string> argNames;
177  uint64_t totalIterations = 1;
178  };
180  static bool isConstantMacroStep(const std::string& step);
181  static MacroLoopSpec parseMacroLoopSpec(const std::string& inputArgs);
183  static std::vector<std::pair<std::string, std::string>> macroLoopIteration(const MacroLoopSpec& spec, uint64_t index);
185  static std::string macroArgsSummary(const std::string& inputArgs);
187  static std::string macroArgValueForLabel(const std::string& value);
188 
190  static unsigned int getStepIndexForLabel(IteratorWorkLoopStruct* iteratorStruct, const std::string& label);
192  static std::string applyStepIndexToMacroArgs(IteratorWorkLoopStruct* iteratorStruct, const std::string& inputArgs, const std::string& labelsStr);
193 
196  static size_t findRemoteGatewayApp(IteratorWorkLoopStruct* iteratorStruct, const std::string& targetSubsystem);
197  static void queueRemoteGatewayCommand(IteratorWorkLoopStruct* iteratorStruct, size_t remoteAppIndex, const std::string& command, const std::string& statusLabel);
198  static std::string buildRemoteConfigureCommand(IteratorWorkLoopStruct* iteratorStruct, const std::string& systemAlias);
199 
202  static std::string findRemoteCodeEditorIconName(GatewaySupervisor* supervisor, const std::string& targetSubsystem);
203 
204  static bool haltIterator(Iterator* iterator,
205  IteratorWorkLoopStruct* iteratorStruct = 0,
206  bool doNotHaltFSM = false);
207 
208  std::mutex accessMutex_;
209  volatile bool workloopRunning_;
210  volatile bool activePlanIsRunning_;
211  std::atomic<bool> iteratorBusy_;
212  volatile bool commandPlay_, commandPause_,
213  commandHalt_;
216  std::string activePlanName_, lastStartedPlanName_, lastFinishedPlanName_;
217  std::vector<OutputFile> lastPlanOutputFiles_;
218  volatile unsigned int activeCommandIndex_, activeCommandIteration_, activeNumberOfCommands_;
219  std::string activeCommandType_;
220 
221  volatile uint64_t genPlanDurationSeconds_ = -1;
222  volatile unsigned int genPlanNumberOfRuns_ = 1;
223  std::string genFsmName_, genConfigAlias_, genLogEntry_;
224  bool genKeepConfiguration_ = false;
225 
226  std::vector<unsigned int> depthIterationStack_;
227  volatile time_t activeCommandStartTime_;
228  std::string lastFsmName_;
229  std::string errorMessage_;
230 
231  GatewaySupervisor* theSupervisor_;
232 
233  template<class T>
234  static void helpCommandModifyActive(IteratorWorkLoopStruct* iteratorStruct, const T& setValue, bool doTrackGroupChanges);
235 };
236 
237 #include "otsdaq/GatewaySupervisor/Iterator.icc" //for template definitions
238 
239 } // namespace ots
240 
241 // clang-format on
242 
243 #endif
void getIterationPlanStatus(HttpXmlDocument &xmldoc)
return state machine and iterator status
Definition: Iterator.cc:2831
defines used also by OtsConfigurationWizardSupervisor
state shared with the background thread driving a remote FE macro over MacroMaker UDP
Definition: Iterator.h:106
std::vector< std::pair< std::string, std::string > > outputFiles
"Filename" reported by the remote MacroMaker per iteration, with that iteration's inputs
Definition: Iterator.h:114
int progress
percent of current iteration, from remote
Definition: Iterator.h:112
std::mutex mutex
guards error, iterationsDone, progress, outputFiles
Definition: Iterator.h:108
an output file written by a macro command during a plan (local FE or remote MacroMaker)
Definition: Iterator.h:32
std::string iconName
desktop icon name of the subsystem's Code Editor (remote only), for open-by-name
Definition: Iterator.h:36
std::string label
e.g. "DTC Read on Calo14_DTC0"
Definition: Iterator.h:35
std::string subsystem
"" for Self, else remote subsystem name
Definition: Iterator.h:33
std::string path
as reported by the writer (e.g. "$OTSDAQ_DATA//macroOutput_..txt" or absolute)
Definition: Iterator.h:34