add improved argument parsing. also add helper messages
authorFilippos Giannakos <philipgian@grnet.gr>
Thu, 22 Nov 2012 11:18:00 +0000 (13:18 +0200)
committerFilippos Giannakos <philipgian@grnet.gr>
Thu, 22 Nov 2012 11:18:00 +0000 (13:18 +0200)
xseg/peers/user/dummy.c
xseg/peers/user/monitor.c
xseg/peers/user/mt-mapperd.c
xseg/peers/user/mt-pfiled.c
xseg/peers/user/mt-sosd.c
xseg/peers/user/mt-vlmcd.c
xseg/peers/user/peer.c
xseg/peers/user/peer.h
xseg/sys/user/xseg_user.c

index d96b603..fd512b6 100644 (file)
@@ -8,6 +8,12 @@
 #include <sys/util.h>
 
 struct timespec delay = {0, 4000000};
+
+void custom_peer_usage()
+{
+       fprintf(stderr, "Custom peer options: \n"
+                       "none \n\n");
+}
 int custom_peer_init(struct peerd *peer, int argc, char *argv[])
 {
        return 0;
index 3959bb3..75b044a 100644 (file)
@@ -18,7 +18,10 @@ struct monitor_io {
        void *src_priv;
 };
 
-
+void custom_peer_usage()
+{
+       return;
+}
 
 static int forward(struct peerd *peer, struct peer_req *pr)
 {
index 83a81b2..c9f2465 100644 (file)
@@ -179,6 +179,16 @@ struct mapperd *mapper;
 
 void print_map(struct map *m);
 
+
+void custom_peer_usage()
+{
+       fprintf(stderr, "Custom peer options: \n"
+                       "-bp  : port for block blocker(!)\n"
+                       "-mbp : port for map blocker\n"
+                       "\n");
+}
+
+
 /*
  * Helper functions
  */
index 1ee7647..446452b 100644 (file)
  * Globals, holding command-line arguments
  */
 
-void usage(char *argv0)
+void custom_peer_usage(char *argv0)
 {
-       fprintf(stderr,
-                       "Usage: %s [-p PORT] [-g XSEG_SPEC] [-n NR_OPS] [-v] "
-                       "--pithos PATH --archip VPATH --prefix PREFIX\n\n"
-                       "where:\n"
-                       "\tPATH: path to pithos data blocks\n"
-                       "\tVPATH: path to modified volume blocks\n"
-                       "\tPREFIX: Common prefix of Archipelagos objects to be"
-                       "striped during filesystem hierarchy creation\n"
-                       "\tPORT: xseg port to listen for requests on\n"
-                       "\tXSEG_SPEC: xseg spec as 'type:name:nr_ports:nr_requests:"
-                       "request_size:extra_size:page_shift'\n"
-                       "\tNR_OPS: number of outstanding xseg requests\n"
-                       "\t-v: verbose mode\n",
-                       argv0);
-
+       fprintf(stderr, "Custom peer options:\n"
+               "--pithos PATH --archip VPATH --prefix PREFIX\n\n"
+               "where:\n"
+               "\tPATH: path to pithos data blocks\n"
+               "\tVPATH: path to modified volume blocks\n"
+               "\tPREFIX: Common prefix of Archipelagos objects to be"
+               "striped during filesystem hierarchy creation\n"
+              );
 }
 
 /* fdcache_node flags */
@@ -700,6 +693,7 @@ int custom_peer_init(struct peerd *peer, int argc, char *argv[])
        pfiled->vpath[0] = 0;
        pfiled->path[0] = 0;
        pfiled->handled_reqs = 0;
+       /*
        for (i = 0; i < argc; i++) {
                if (!strcmp(argv[i], "--pithos") && (i+1) < argc){
                        strncpy(pfiled->path, argv[i+1], MAX_PATH_SIZE);
@@ -720,9 +714,17 @@ int custom_peer_init(struct peerd *peer, int argc, char *argv[])
                        continue;
                }
        }
+       */
+       BEGIN_READ_ARGS(argc, argv);
+       READ_ARG_STRING("--pithos", pfiled->path, MAX_PATH_SIZE);
+       READ_ARG_STRING("--archip", pfiled->vpath, MAX_PATH_SIZE);
+       READ_ARG_STRING("--prefix", pfiled->prefix, MAX_PREFIX_LEN);
+       END_READ_ARGS();
+
 
        pfiled->prefix_len = strlen(pfiled->prefix);
 
+       //TODO test path exist
        pfiled->path_len = strlen(pfiled->path);
        if (!pfiled->path_len){
                XSEGLOG2(&lc, E, "Pithos path was not provided");
index 28a25a3..bffc26b 100644 (file)
 #define MAX_POOL_NAME 64
 #define MAX_OBJ_NAME XSEG_MAX_TARGETLEN
 
+void custom_peer_usage()
+{
+       fprintf(stderr, "Custom peer options:\n"
+               "--pool: Rados pool to connect\n"
+               "\n");
+}
+
 enum rados_state {
        ACCEPTED = 0,
        PENDING = 1,
index 8b85089..56aee24 100644 (file)
@@ -28,6 +28,14 @@ struct vlmc_io {
        unsigned long breq_len, breq_cnt;
 };
 
+void custom_peer_usage()
+{
+       fprintf(stderr, "Custom peer options: \n"
+                       "-mp : mapper port\n"
+                       "-bp : blocker port for blocks\n"
+                       "\n");
+}
+
 static inline void __set_vio_state(struct vlmc_io *vio, enum io_state_enum state)
 {
 //     xlock_acquire(&vio->lock, 1);
index 2f3e3a6..451ea41 100644 (file)
@@ -3,24 +3,30 @@
 #include <stdlib.h>
 #include <sys/types.h>
 #include <unistd.h>
-#include <xseg/xseg.h>
-#include <peer.h>
 #include <sys/syscall.h>
 #include <sys/time.h>
 #include <signal.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <errno.h>
+
 #ifdef MT
 #include <pthread.h>
 #endif
 
+#include <xseg/xseg.h>
+#include <peer.h>
+
 #ifdef MT
 #define PEER_TYPE "pthread"
 #else
 #define PEER_TYPE "posix"
 #endif
 
+//FIXME this should not be defined here probably
+#define MAX_SPEC_LEN 128
+#define MAX_PIDFILE_LEN 512
+
 volatile unsigned int terminated = 0;
 unsigned int verbose = 0;
 struct log_ctx lc;
@@ -104,7 +110,7 @@ void signal_handler(int signal)
 void renew_logfile(int signal)
 {
        XSEGLOG2(&lc, I, "Caught signal. Renewing logfile");
-       renew_logctx(&lc, NULL, lc.log_level, NULL, REOPEN_FILE);
+       renew_logctx(&lc, NULL, verbose, NULL, REOPEN_FILE);
 }
 
 static int setup_signals(struct peerd *peer)
@@ -639,29 +645,60 @@ int pidfile_open(char *path, pid_t *old_pid)
        return fd;
 }
 
+void usage(char *argv0)
+{
+       fprintf(stderr, "Usage: %s [general options] [custom peer options]\n\n", argv0);
+       fprintf(stderr, "General peer options:\n"
+               "  Option      | Default | \n"
+               "  --------------------------------------------\n"
+               "    -g        | None    | Segment spec to join\n"
+               "    -sp       | NoPort  | Start portno to bind\n"
+               "    -ep       | NoPort  | End portno to bind\n"
+               "    -p        | NoPort  | Portno to bind\n"
+               "    -n        | 16      | Number of ops\n"
+               "    -v        | 0       | Verbosity level\n"
+               "    -l        | None    | Logfile \n"
+               "    -d        | No      | Daemonize \n"
+               "    --pidfile | None    | Pidfile \n"
+#ifdef MT
+               "    -t        | No      | Number of threads \n"
+#endif
+               "\n"
+              );
+       custom_peer_usage();
+}
+
 int main(int argc, char *argv[])
 {
        struct peerd *peer = NULL;
        //parse args
-       char *spec = "";
-       int i, r, daemonize = 0;
+       int r;
        long portno_start = -1, portno_end = -1, portno = -1;
+
        //set defaults here
+       int daemonize = 0, help = 0;
        uint32_t nr_ops = 16;
-       uint32_t nr_threads = 16 ;
+       uint32_t nr_threads = 1;
        unsigned int debug_level = 0;
        uint32_t defer_portno = NoPort;
-       char *logfile = NULL;
-       char *pidfile = NULL;
        pid_t old_pid;
        int pid_fd = -1;
 
+       char spec[MAX_SPEC_LEN + 1];
+       char logfile[MAX_LOGFILE_LEN + 1];
+       char pidfile[MAX_PIDFILE_LEN + 1];
+
+       logfile[0] = 0;
+       pidfile[0] = 0;
+       spec[0] = 0;
+
        //capture here -g spec, -n nr_ops, -p portno, -t nr_threads -v verbose level
        // -dp xseg_portno to defer blocking requests
        // -l log file ?
        //TODO print messages on arg parsing error
        //TODO string checking
 
+       /*
        for (i = 1; i < argc; i++) {
                if (!strcmp(argv[i], "-g") && i + 1 < argc) {
                        spec = argv[i+1];
@@ -723,6 +760,30 @@ int main(int argc, char *argv[])
                }
 
        }
+       */
+       BEGIN_READ_ARGS(argc, argv);
+       READ_ARG_STRING("-g", spec, MAX_SPEC_LEN);
+       READ_ARG_ULONG("-sp", portno_start);
+       READ_ARG_ULONG("-ep", portno_end);
+       READ_ARG_ULONG("-p", portno);
+       READ_ARG_ULONG("-n", nr_ops);
+       READ_ARG_ULONG("-v", debug_level);
+#ifdef MT
+       READ_ARG_ULONG("-t", nr_threads);
+#endif
+//     READ_ARG_ULONG("-dp", defer_portno);
+       READ_ARG_STRING("-l", logfile, MAX_LOGFILE_LEN);
+       READ_ARG_BOOL("-d", daemonize);
+       READ_ARG_BOOL("-h", help);
+       READ_ARG_BOOL("--help", help);
+       READ_ARG_STRING("--pidfile", pidfile, MAX_PIDFILE_LEN);
+       END_READ_ARGS();
+
+       if (help){
+               usage(argv[0]);
+               return 0;
+       }
+
        r = init_logctx(&lc, argv[0], debug_level, logfile,
                        REDIRECT_STDOUT|REDIRECT_STDERR);
        if (r < 0){
@@ -731,7 +792,7 @@ int main(int argc, char *argv[])
        }
        XSEGLOG2(&lc, D, "Main thread has tid %ld.\n", syscall(SYS_gettid));
 
-       if (pidfile){
+       if (pidfile[0]){
                pid_fd = pidfile_open(pidfile, &old_pid);
                if (pid_fd < 0) {
                        if (old_pid) {
@@ -760,7 +821,6 @@ int main(int argc, char *argv[])
                portno_end = portno;
        }
 
-       //TODO err check
        peer = peerd_init(nr_ops, spec, portno_start, portno_end, nr_threads, defer_portno);
        if (!peer){
                r = -1;
@@ -771,6 +831,7 @@ int main(int argc, char *argv[])
        if (r < 0)
                goto out;
 #ifdef MT
+       //TODO err check
        peerd_start_threads(peer);
 #endif
 
index 297e477..2fe9c59 100644 (file)
@@ -5,6 +5,40 @@
 #include <st.h>
 #endif
 
+
+#define BEGIN_READ_ARGS(__ac, __av)                                    \
+       int __argc = __ac;                                              \
+       char **__argv = __av;                                           \
+       int __i;                                                        \
+       for (__i = 0; __i < __argc; __i++) {
+
+#define END_READ_ARGS()                                                        \
+       }
+
+#define READ_ARG_ULONG(__name, __var)                                  \
+       if (!strcmp(__argv[__i], __name) && __i + 1 < __argc){  \
+               __var = strtoul(__argv[__i+1], NULL, 10);               \
+               __i += 1;                                               \
+               continue;                                               \
+       }
+
+#define READ_ARG_STRING(__name, __var, __max_len)                      \
+       if (!strcmp(__argv[__i], __name) && __i + 1 < __argc){  \
+               strncpy(__var, __argv[__i+1], __max_len);               \
+               __var[__max_len] = 0;                           \
+               __i += 1;                                               \
+               continue;                                               \
+       }
+
+#define READ_ARG_BOOL(__name, __var)                                   \
+       if (!strcmp(__argv[__i], __name)){                              \
+               __var = 1;                                              \
+               continue;                                               \
+       }
+
+
+
+
 /* main peer structs */
 struct peer_req {
        struct peerd *peer;
@@ -80,4 +114,4 @@ int custom_peer_init(struct peerd *peer, int argc, char *argv[]);
 int dispatch(struct peerd *peer, struct peer_req *pr, struct xseg_request *req,
                enum dispatch_reason reason);
 
-void usage();
+void custom_peer_usage();
index 11d8ede..21ae94f 100644 (file)
@@ -98,7 +98,7 @@ int __renew_logctx(struct log_ctx *lc, char *peer_name,
        }
 
        lc->log_level = log_level;
-       if (logfile) {
+       if (logfile && logfile[0]) {
                strncpy(lc->filename, logfile, MAX_LOGFILE_LEN);
                lc->filename[MAX_LOGFILE_LEN - 1] = 0;
        }
@@ -141,7 +141,7 @@ int __init_logctx(struct log_ctx *lc, char *peer_name,
        }
 
        lc->log_level = log_level;
-       if (!logfile) {
+       if (!logfile || !logfile[0]) {
                lc->logfile = stderr;
                return 0;
        }