Browse Source

Parse -t nthreads option (and improved argument parsing in general)

Ian Goldberg 1 year ago
parent
commit
bccaf75ceb
4 changed files with 46 additions and 15 deletions
  1. 6 5
      App/launch
  2. 1 1
      App/start.cpp
  3. 1 1
      App/start.hpp
  4. 38 8
      App/teems.cpp

+ 6 - 5
App/launch

@@ -19,16 +19,17 @@ PUBKEYS = "pubkeys.yaml"
 # The TEEMS binary
 TEEMS = "./teems"
 
-def launch(node, manifest, config):
+def launch(node, manifest, config, cmd):
     manifestdata = manifest[node]
     cmdline = ''
     if 'launchprefix' in manifestdata:
         cmdline = manifestdata['launchprefix'] + ' '
-    cmdline += TEEMS + " %s %s" % (manifestdata['sprvfile'], node)
+    cmdline += TEEMS + " -k %s -n %s" % (manifestdata['sprvfile'], node)
     if 'args' in manifestdata:
         cmdline += ' ' + manifestdata['args']
-    proc = subprocess.Popen(shlex.split(cmdline), stdin=subprocess.PIPE,
-        stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=0)
+    proc = subprocess.Popen(shlex.split(cmdline) + cmd,
+        stdin=subprocess.PIPE, stdout=subprocess.PIPE,
+        stderr=subprocess.STDOUT, bufsize=0)
     proc.stdin.write(config.encode('utf-8'))
     while True:
         line = proc.stdout.readline()
@@ -90,7 +91,7 @@ if __name__ == "__main__":
         if node == "params":
             continue
         thread = threading.Thread(target=launch,
-            args=(node, manifest, config))
+            args=(node, manifest, config, args.cmd))
         thread.start()
         threadlist.append(thread)
 

+ 1 - 1
App/start.cpp

@@ -4,7 +4,7 @@
 
 // Once all the networking is set up, start doing whatever we were asked
 // to do on the command line
-void start(NetIO &netio, int argc, char **argv)
+void start(NetIO &netio, char **args)
 {
     printf("Reading\n");
     for (nodenum_t node_num = 0; node_num < netio.num_nodes; ++node_num) {

+ 1 - 1
App/start.hpp

@@ -5,6 +5,6 @@
 
 // Once all the networking is set up, start doing whatever we were asked
 // to do on the command line
-void start(NetIO &netio, int argc, char **argv);
+void start(NetIO &netio, char **args);
 
 #endif

+ 38 - 8
App/teems.cpp

@@ -129,8 +129,9 @@ static void usage(const char *argv0)
 {
     fprintf(stderr, "Usage: %s --gen sealedprivkeyfile pubkeyfile\n",
         argv0);
-    fprintf(stderr, "or     %s sealedprivkeyfile myname [args] < config.json\n",
+    fprintf(stderr, "or     %s -k sealedprivkeyfile -n myname [-t nthreads] [command] < config.json\n",
         argv0);
+    exit(1);
 }
 
 int main(int argc, char **argv)
@@ -150,7 +151,6 @@ int main(int argc, char **argv)
     if (argc > 1 && !strcmp(argv[1], "--gen")) {
         if (argc != 4) {
             usage(argv[0]);
-            exit(1);
         }
 
         const char *sealedprivkeyfile = argv[2];
@@ -173,13 +173,43 @@ int main(int argc, char **argv)
         exit(1);
     }
 
-    if (argc < 3) {
-        usage(argv[0]);
-        exit(1);
+    const char *sealedprivkeyfile = NULL;
+    const char *myname = NULL;
+    uint16_t nthreads = 1;
+
+    const char *progname = argv[0];
+    ++argv;
+    // Parse options
+    while (*argv && (*argv)[0] == '-') {
+        if (!strcmp(*argv, "--")) {
+            argv += 1;
+            break;
+        } else if (!strcmp(*argv, "-k")) {
+            if (argv[1] == NULL) {
+                usage(progname);
+            }
+            sealedprivkeyfile = argv[1];
+            argv += 2;
+        } else if (!strcmp(*argv, "-n")) {
+            if (argv[1] == NULL) {
+                usage(progname);
+            }
+            myname = argv[1];
+            argv += 2;
+        } else if (!strcmp(*argv, "-t")) {
+            if (argv[1] == NULL) {
+                usage(progname);
+            }
+            nthreads = uint16_t(atoi(argv[1]));
+            argv += 2;
+        } else {
+            usage(progname);
+        }
     }
 
-    const char *sealedprivkeyfile = argv[1];
-    std::string myname(argv[2]);
+    if (sealedprivkeyfile == NULL || myname == NULL) {
+        usage(progname);
+    }
 
     // Read the config.json from the first line of stdin.  We have to do
     // this before outputting anything to avoid potential deadlock with
@@ -215,7 +245,7 @@ int main(int argc, char **argv)
     boost::asio::post(io_context, [&]{
         // Start enclave-to-enclave communications
         ecall_comms_start();
-        start(netio, argc, argv);
+        start(netio, argv);
     });
 
     // Start another thread; one will perform the work and the other