MIDAS
Loading...
Searching...
No Matches
mtransition.cxx File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <signal.h>
#include <unistd.h>
#include "midas.h"
Include dependency graph for mtransition.cxx:

Go to the source code of this file.

Functions

int read_state (HNDLE hDB)
 
void usage ()
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char argv[] 
)

Definition at line 61 of file mtransition.cxx.

62{
63 int status;
64 bool verbose = false;
65 int debug_flag = 0;
66 bool force = false;
67 bool multithread = false;
68 bool asyncmultithread = false;
69 std::string host_name;
70 std::string exp_name;
71
74
75#ifndef OS_WINNT
77#endif
78
79 /* get default from environment */
81
82 /* parse command line parameters */
83
84 if (argc < 2)
85 usage(); // does not return
86
87 for (int i = 1; i < argc; i++) {
88 if (argv[i][0] == '-') {
89 if (argv[i][1] == 'v')
90 verbose = true;
91 else if (argv[i][1] == 'f')
92 force = true;
93 else if (argv[i][1] == 'a')
94 asyncmultithread = true;
95 else if (argv[i][1] == 'm')
96 multithread = true;
97 else if (argv[i][1] == 'd' && i < argc-1)
98 debug_flag = strtoul(argv[++i], NULL, 0);
99 else if (argv[i][1] == 'e' && i < argc-1)
100 exp_name = argv[++i];
101 else if (argv[i][1] == 'h' && i < argc-1)
102 host_name = argv[++i];
103 else
104 usage(); // does not return
105 }
106 }
107
108 if (debug_flag)
109 verbose = true;
110 else if (verbose)
111 debug_flag = 1;
112
113 /* do not produce a startup message */
115
116 status = cm_connect_experiment1(host_name.c_str(), exp_name.c_str(), "mtransition", NULL,
119 if (status != CM_SUCCESS) {
120 fprintf(stderr,"Error: Cannot connect to experiment \'%s\' on host \'%s\', status %d\n",
121 exp_name.c_str(),
122 host_name.c_str(),
123 status);
124 exit(1);
125 }
126
127 if (verbose)
128 printf("Connected to experiment \'%s\' on host \'%s\'\n", exp_name.c_str(), host_name.c_str());
129
130 HNDLE hDB;
131
133 assert(status == CM_SUCCESS);
134
135 int tr_flag = TR_SYNC;
136 if (multithread)
140
141 for (int i=1; i<argc; i++) {
142
143 if (argv[i][0] == '-') {
144
145 // skip command line switches
146
147 if (argv[i][1] == 'd')
148 i++;
149 else if (argv[i][1] == 'h')
150 i++;
151 if (argv[i][1] == 'e')
152 i++;
153
154 continue;
155
156 } else if (strcmp(argv[i], "START") == 0) {
157
158 /* start */
159
160 /* check if run is already started */
161 int state = read_state(hDB);
162
163 if (!force && state == STATE_RUNNING) {
164 printf("START: Run is already started\n");
167 exit(1);
168 } else if (!force && state == STATE_PAUSED) {
169 printf("START: Run is paused, please use \"RESUME\"\n");
172 exit(1);
173 }
174
175 /* get present run number */
176 int old_run_number = 0;
177 int size = sizeof(old_run_number);
178 status = db_get_value(hDB, 0, "/Runinfo/Run number", &old_run_number, &size, TID_INT, FALSE);
179 assert(status == DB_SUCCESS);
180 assert(old_run_number >= 0);
181
183
184 if (i+1 < argc) {
185 if (isdigit(argv[i+1][0])) {
187 i++;
188 }
189 }
190
191 if (verbose)
192 printf("Starting run %d\n", new_run_number);
193
194 char str[256];
195 status = cm_transition(TR_START, new_run_number, str, sizeof(str), tr_flag, debug_flag);
196 if (status != CM_SUCCESS) {
197 /* in case of error, reset run number */
198 status = db_set_value(hDB, 0, "/Runinfo/Run number", &old_run_number, sizeof(old_run_number), 1, TID_INT);
199 assert(status == DB_SUCCESS);
200
201 printf("START: cm_transition status %d, message \'%s\'\n", status, str);
202 }
203
204 } else if (strcmp(argv[i], "STOP") == 0) {
205
206 /* check if run is stopped */
207
208 int state = read_state(hDB);
209
210 if (state == STATE_STOPPED) {
211 printf("Run is already stopped, stopping again.\n");
212 }
213
214 char str[256];
215 status = cm_transition(TR_STOP, 0, str, sizeof(str), tr_flag, debug_flag);
216
217 if (status != CM_SUCCESS)
218 printf("STOP: cm_transition status %d, message \'%s\'\n", status, str);
219
220 } else if (strcmp(argv[i], "PAUSE") == 0) {
221
222 /* check if run is started */
223
224 int state = read_state(hDB);
225
226 if (!force && state == STATE_PAUSED) {
227 printf("PAUSE: Run is already paused\n");
228 continue;
229 }
230
231 if (state != STATE_RUNNING) {
232 printf("PAUSE: Run is not started\n");
235 exit(1);
236 }
237
238 char str[256];
239 status = cm_transition(TR_PAUSE, 0, str, sizeof(str), tr_flag, debug_flag);
240
241 if (status != CM_SUCCESS)
242 printf("PAUSE: cm_transition status %d, message \'%s\'\n", status, str);
243
244 } else if (strcmp(argv[i], "RESUME") == 0) {
245
246 int state = read_state(hDB);
247
248 if (!force && state == STATE_RUNNING) {
249 printf("RESUME: Run is already running\n");
250 continue;
251 } else if (!force && state != STATE_PAUSED) {
252 printf("RESUME: Run is not paused\n");
255 exit(1);
256 }
257
258 char str[256];
259 status = cm_transition(TR_RESUME, 0, str, sizeof(str), tr_flag, debug_flag);
260 if (status != CM_SUCCESS)
261 printf("RESUME: cm_transition status %d, message \'%s\'\n", status, str);
262
263 } else if (strcmp(argv[i], "STARTABORT") == 0) {
264
265 char str[256];
266 status = cm_transition(TR_STARTABORT, 0, str, sizeof(str), tr_flag, debug_flag);
267 if (status != CM_SUCCESS)
268 printf("STARTABORT: cm_transition status %d, message \'%s\'\n", status, str);
269
270 } else if (strcmp(argv[i], "DELAY") == 0) {
271
272 if (argv[i+1] == NULL) {
273 fprintf(stderr,"Command DELAY requires an argument\n");
274 usage(); // does not return
275 }
276
277 const char* arg = argv[++i];
278 int delay = 0;
279
280 if (isdigit(arg[0])) {
281 delay = atoi(arg);
282 } else if (arg[0] == '/') {
283 int size = sizeof(delay);
284 status = db_get_value(hDB, 0, arg, &delay, &size, TID_INT, FALSE);
285 if (status != DB_SUCCESS) {
286 fprintf(stderr,"DELAY: Cannot read ODB variable \'%s\', status %d\n", arg, status);
289 exit(1);
290 }
291 }
292
293 if (verbose)
294 printf("DELAY \'%s\' %d sec\n", arg, delay);
295
296 status = ss_sleep(delay*1000);
297 assert(status == SS_SUCCESS);
298
299 } else if (strcmp(argv[i], "IF") == 0) {
300
301 if (argv[i+1] == NULL) {
302 fprintf(stderr,"Command IF requires an argument\n");
303 usage(); // does not return
304 }
305
306 const char* arg = argv[++i];
307 int value = 0;
308
309 if (isdigit(arg[0])) {
310 value = atoi(arg);
311 } else if (arg[0] == '/') {
312 int size = sizeof(value);
313 status = db_get_value(hDB, 0, arg, &value, &size, TID_BOOL, FALSE);
314 if (status == DB_TYPE_MISMATCH) {
315 status = db_get_value(hDB, 0, arg, &value, &size, TID_INT, FALSE);
316 }
317 if (status != DB_SUCCESS) {
318 fprintf(stderr,"IF: Cannot read ODB variable \'%s\', status %d\n", arg, status);
321 exit(1);
322 }
323 }
324
325 if (verbose)
326 printf("IF \'%s\' value %d\n", arg, value);
327
328 if (!value) {
331 exit(0);
332 }
333
334 } else {
335
336 fprintf(stderr,"Unknown command \'%s\'\n", argv[i]);
337 usage(); // does not return
338
339 }
340 }
341
342 while (1) {
344 if (status == CM_SUCCESS)
345 break;
346 printf("waiting for transition to finish!\n");
347 ::sleep(1);
348 }
349
350 /* do not produce a shutdown message */
352
354
355 return 0;
356}
#define FALSE
Definition cfortran.h:309
INT cm_get_experiment_database(HNDLE *hDB, HNDLE *hKeyClient)
Definition midas.cxx:3011
INT cm_transition(INT transition, INT run_number, char *errstr, INT errstr_size, INT async_flag, INT debug_flag)
Definition midas.cxx:5286
INT cm_connect_experiment1(const char *host_name, const char *default_exp_name, const char *client_name, void(*func)(char *), INT odb_size, DWORD watchdog_timeout)
Definition midas.cxx:2297
INT cm_disconnect_experiment(void)
Definition midas.cxx:2846
INT cm_get_environment(char *host_name, int host_name_size, char *exp_name, int exp_name_size)
Definition midas.cxx:2134
INT cm_transition_cleanup()
Definition midas.cxx:5267
#define CM_SUCCESS
Definition midas.h:582
#define DB_SUCCESS
Definition midas.h:631
#define DB_TYPE_MISMATCH
Definition midas.h:645
#define SS_SUCCESS
Definition midas.h:663
#define TR_RESUME
Definition midas.h:408
#define TR_PAUSE
Definition midas.h:407
#define TID_BOOL
Definition midas.h:340
#define TR_START
Definition midas.h:405
#define TR_SYNC
Definition midas.h:358
#define TR_ASYNC
Definition midas.h:359
#define TR_MTHREAD
Definition midas.h:361
#define TR_STARTABORT
Definition midas.h:409
#define STATE_STOPPED
Definition midas.h:305
#define STATE_PAUSED
Definition midas.h:306
#define STATE_RUNNING
Definition midas.h:307
#define TID_INT
Definition midas.h:338
#define TR_STOP
Definition midas.h:406
#define MT_ERROR
Definition midas.h:542
INT ss_sleep(INT millisec)
Definition system.cxx:3628
INT cm_set_msg_print(INT system_mask, INT user_mask, int(*func)(const char *))
Definition midas.cxx:647
INT db_get_value(HNDLE hDB, HNDLE hKeyRoot, const char *key_name, void *data, INT *buf_size, DWORD type, BOOL create)
Definition odb.cxx:5415
INT db_set_value(HNDLE hDB, HNDLE hKeyRoot, const char *key_name, const void *data, INT data_size, INT num_values, DWORD type)
Definition odb.cxx:5261
char exp_name[NAME_LENGTH]
Definition mana.cxx:243
HNDLE hDB
main ODB handle
Definition mana.cxx:207
BOOL verbose
Definition mana.cxx:255
char host_name[HOST_NAME_LENGTH]
Definition mana.cxx:242
INT i
Definition mdump.cxx:32
INT HNDLE
Definition midas.h:132
#define DEFAULT_WATCHDOG_TIMEOUT
Definition midas.h:290
#define DEFAULT_ODB_SIZE
Definition midas.h:270
#define sleep(ms)
void usage()
int read_state(HNDLE hDB)
double value[100]
Definition odbhist.cxx:42
char str[256]
Definition odbhist.cxx:33
DWORD status
Definition odbhist.cxx:39
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
Definition rmidas.h:24
Here is the call graph for this function:

◆ read_state()

int read_state ( HNDLE  hDB)

Definition at line 26 of file mtransition.cxx.

27{
28 /* check if run is stopped */
29 int state = STATE_STOPPED;
30 int size = sizeof(state);
31 int status = db_get_value(hDB, 0, "/Runinfo/State", &state, &size, TID_INT, FALSE);
32 assert(status == DB_SUCCESS);
33 return state;
34}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ usage()

void usage ( )

Definition at line 36 of file mtransition.cxx.

37{
38 fprintf(stderr, "Usage: mtransition [-v] [-d debug_flag] [-f] [-h Hostname] [-e Experiment] commands...\n");
39 fprintf(stderr, "Options:\n");
40 fprintf(stderr, " -v - report activity\n");
41 fprintf(stderr, " -d debug_flag - passed through cm_transition(debug_flag): 0=normal, 1=printf, 2=cm_msg(MINFO)\n");
42 fprintf(stderr, " -f - force new state regardless of current state\n");
43 fprintf(stderr, " -m - multithread transition\n");
44 fprintf(stderr, " -a - async multithread transition\n");
45 fprintf(stderr, " -h Hostname - connect to mserver on remote host\n");
46 fprintf(stderr, " -e Experiment - connect to non-default experiment\n");
47 fprintf(stderr, "Commands:\n");
48 fprintf(stderr, " START [runno] - start a new run\n");
49 fprintf(stderr, " STOP - stop the run\n");
50 fprintf(stderr, " PAUSE - pause the run\n");
51 fprintf(stderr, " RESUME - resume the run\n");
52 fprintf(stderr, " STARTABORT - cleanup after failed START\n");
53 fprintf(stderr, " DELAY 100 - sleep for 100 seconds\n");
54 fprintf(stderr, " DELAY \"/logger/Auto restart delay\" - sleep for time specified in ODB variable\n");
55 fprintf(stderr, " IF \"/logger/Auto restart\" - continue only if ODB variable is set to TRUE\n");
58 exit (1);
59}
Here is the call graph for this function:
Here is the caller graph for this function: