root/dmtcp_command.cpp
/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- main
1 /****************************************************************************
2 * Copyright (C) 2006-2010 by Jason Ansel, Kapil Arya, and Gene Cooperman *
3 * jansel@csail.mit.edu, kapil@ccs.neu.edu, gene@ccs.neu.edu *
4 * *
5 * This file is part of DMTCP. *
6 * *
7 * DMTCP is free software: you can redistribute it and/or *
8 * modify it under the terms of the GNU Lesser General Public License as *
9 * published by the Free Software Foundation, either version 3 of the *
10 * License, or (at your option) any later version. *
11 * *
12 * DMTCP is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Lesser General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with DMTCP:dmtcp/src. If not, see *
19 * <http://www.gnu.org/licenses/>. *
20 ****************************************************************************/
21
22 #include <stdio.h>
23
24 #include "coordinatorapi.h"
25 #include "util.h"
26
27 #define BINARY_NAME "dmtcp_command"
28
29 using namespace dmtcp;
30
31 // gcc-4.3.4 -Wformat=2 issues false positives for warnings unless the format
32 // string has at least one format specifier with corresponding format argument.
33 // Ubuntu 9.01 uses -Wformat=2 by default.
34 static const char* theUsage =
35 "Usage: dmtcp_command [OPTIONS] COMMAND [COMMAND...]\n"
36 "Send a command to the dmtcp_coordinator remotely.\n\n"
37 "Options:\n\n"
38 " -h, --coord-host HOSTNAME (environment variable DMTCP_COORD_HOST)\n"
39 " Hostname where dmtcp_coordinator is run (default: localhost)\n"
40 " -p, --coord-port PORT_NUM (environment variable DMTCP_COORD_PORT)\n"
41 " Port where dmtcp_coordinator is run (default: 7779)\n"
42 " --help\n"
43 " Print this message and exit.\n"
44 " --version\n"
45 " Print version information and exit.\n"
46 "\n"
47 "Commands for Coordinator:\n"
48 " -s, --status: Print status message\n"
49 " -c, --checkpoint: Checkpoint all nodes\n"
50 " -bc, --bcheckpoint: Checkpoint all nodes, blocking until done\n"
51 //" xc, -xc, --xcheckpoint : Checkpoint all nodes, kill all nodes when done\n"
52 " -i, --interval <val> Update ckpt interval to <val> seconds (0=never)\n"
53 " -k, --kill Kill all nodes\n"
54 " -q, --quit Kill all nodes and quit\n"
55 "\n"
56 HELP_AND_CONTACT_INFO
57 "\n"
58 ;
59
60
61 //shift args
62 #define shift argc--,argv++
63
64 int main ( int argc, char** argv )
65 {
66 string interval = "";
67 string request = "h";
68
69 initializeJalib();
70
71 // No need to initialize the log file.
72 // Util::initializeLogFile();
73
74 //process args
75 shift;
76 while(argc>0){
77 string s = argv[0];
78 if((s=="--help" || s=="-h") && argc==1){
79 printf("%s", theUsage);
80 return 1;
81 } else if ((s=="--version") && argc==1){
82 printf("%s", DMTCP_VERSION_AND_COPYRIGHT_INFO);
83 return 1;
84 }else if(argc>1 && (s == "-h" || s == "--coord-host" || s == "--host")){
85 setenv(ENV_VAR_NAME_HOST, argv[1], 1);
86 shift; shift;
87 } else if (argc>1 && (s == "-p" || s == "--coord-port" || s == "--port")) {
88 setenv(ENV_VAR_NAME_PORT, argv[1], 1);
89 shift; shift;
90 } else if (argv[0][0] == '-' && argv[0][1] == 'p' &&
91 isdigit(argv[0][2])) { // else if -p0, for example
92 setenv(ENV_VAR_NAME_PORT, argv[0]+2, 1);
93 shift;
94 }else if(s == "h" || s == "-h" || s == "--help" || s == "?"){
95 fprintf(stderr, theUsage, "");
96 return 1;
97 }else{ // else it's a request
98 char* cmd = argv[0];
99 //ignore leading dashes
100 while(*cmd == '-') cmd++;
101 s = cmd;
102
103 if((*cmd == 'b' || *cmd == 'x') && *(cmd+1) != 'c'){
104 // If blocking ckpt, next letter must be 'c'; else print the usage
105 fprintf(stderr, theUsage, "");
106 return 1;
107 } else if (*cmd == 's' || *cmd == 'i' || *cmd == 'c' || *cmd == 'b' ||
108 *cmd == 'x' || *cmd == 'k' || *cmd == 'q') {
109 request = s;
110 if (*cmd == 'i') {
111 if (isdigit(cmd[1])) { // if -i5, for example
112 interval = cmd+1;
113 } else { // else -i 5
114 if (argc == 1) {
115 fprintf(stderr, theUsage, "");
116 return 1;
117 }
118 interval = argv[1];
119 shift;
120 }
121 }
122 shift;
123 }else{
124 fprintf(stderr, theUsage, "");
125 return 1;
126 }
127 }
128 }
129
130 int coordCmdStatus = CoordCmdStatus::NOERROR;
131 int numPeers;
132 int isRunning;
133 int ckptInterval;
134 CoordinatorAPI coordinatorAPI;
135 char *cmd = (char *)request.c_str();
136 switch (*cmd) {
137 case 'h':
138 fprintf(stderr, theUsage, "");
139 return 1;
140 case 'i':
141 setenv(ENV_VAR_CKPT_INTR, interval.c_str(), 1);
142 coordinatorAPI.connectAndSendUserCommand(*cmd, &coordCmdStatus);
143 printf("Interval changed to %s\n", interval.c_str());
144 break;
145 case 'b':
146 case 'x':
147 // blocking prefix
148 coordinatorAPI.connectAndSendUserCommand(*cmd, &coordCmdStatus);
149 // actual command
150 coordinatorAPI.connectAndSendUserCommand(*(cmd+1), &coordCmdStatus);
151 break;
152 case 's':
153 coordinatorAPI.connectAndSendUserCommand(*cmd, &coordCmdStatus,
154 &numPeers, &isRunning, &ckptInterval);
155 case 'c':
156 case 'k':
157 case 'q':
158 coordinatorAPI.connectAndSendUserCommand(*cmd, &coordCmdStatus);
159 break;
160 }
161
162 //check for error
163 if (coordCmdStatus != CoordCmdStatus::NOERROR) {
164 switch(coordCmdStatus){
165 case CoordCmdStatus::ERROR_COORDINATOR_NOT_FOUND:
166 if (getenv("DMTCP_COORD_PORT") || getenv("DMTCP_PORT"))
167 fprintf(stderr, "Coordinator not found. Please check port and host.\n");
168 else
169 fprintf(stderr,
170 "Coordinator not found. Try specifying port with \'--port\'.\n");
171 break;
172 case CoordCmdStatus::ERROR_INVALID_COMMAND:
173 fprintf(stderr,
174 "Unknown command: %c, try 'dmtcp_command --help'\n", *cmd);
175 break;
176 case CoordCmdStatus::ERROR_NOT_RUNNING_STATE:
177 fprintf(stderr, "Error, computation not in running state."
178 " Either a checkpoint is\n"
179 " currently happening or there are no connected processes.\n");
180 break;
181 default:
182 fprintf(stderr, "Unknown error\n");
183 break;
184 }
185 return 2;
186 }
187
188 #define QUOTE(arg) #arg
189 #define STRINGIFY(arg) QUOTE(arg)
190 if(*cmd == 's'){
191 printf("Coordinator:\n");
192 char *host = getenv(ENV_VAR_NAME_HOST);
193 if (host == NULL) host = getenv("DMTCP_HOST"); // deprecated
194 printf(" Host: %s\n", (host ? host : "localhost"));
195 char *port = getenv(ENV_VAR_NAME_PORT);
196 if (port == NULL) port = getenv("DMTCP_PORT"); // deprecated
197 printf(" Port: %s\n",
198 (port ? port : STRINGIFY(DEFAULT_PORT) " (default port)"));
199 printf("Status...\n");
200 printf(" NUM_PEERS=%d\n", numPeers);
201 printf(" RUNNING=%s\n", (isRunning?"yes":"no"));
202 if (ckptInterval) {
203 printf(" CKPT_INTERVAL=%d\n", ckptInterval);
204 } else {
205 printf(" CKPT_INTERVAL=0 (checkpoint manually)\n");
206 }
207 }
208
209 return 0;
210 }
211