Changeset: 997c7811bf34 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=997c7811bf34
Modified Files:
        clients/mapiclient/stethoscope.c
Branch: profiler
Log Message:

Changes in stethoscope:
- Always either write to stdout or to specified file
- Add json flag that, when set, makes stethoscope output json
- Fix help messages


diffs (255 lines):

diff --git a/clients/mapiclient/stethoscope.c b/clients/mapiclient/stethoscope.c
--- a/clients/mapiclient/stethoscope.c
+++ b/clients/mapiclient/stethoscope.c
@@ -69,10 +69,12 @@
 
 static stream *conn = NULL;
 static char hostname[128];
-static char *basefilename = "stethoscope";
+static char *filename = NULL;
 static int beat = 50;
+static int json = 0;
 static Mapi dbh;
 static MapiHdl hdl = NULL;
+static FILE *trace = NULL;
 
 /*
  * Tuple level reformatting
@@ -80,51 +82,56 @@ static MapiHdl hdl = NULL;
 
 static void
 renderEvent(EventRecord *ev){
+       FILE *s;
+       if(trace != NULL) 
+               s = trace;
+       else
+               s = stdout;
        if( ev->eventnr ==0 && ev->version){
-               printf("[ ");
-               printf("0,      ");
-               printf("0,      ");
-               printf("\"\",   " );
-               printf("0,      ");
-               printf("\"system\",     "); 
-               printf("0,      ");
-               printf("0,      ");
-               printf("0,      ");
-               printf("0,      ");
-               printf("0,      ");
-               printf("0,      ");
-               printf("\"");
-               printf("version:%s, release:%s, threads:%s, memory:%s, host:%s, 
oid:%d, package:%s ", 
+               fprintf(s, "[ ");
+               fprintf(s, "0,  ");
+               fprintf(s, "0,  ");
+               fprintf(s, "\"\",       " );
+               fprintf(s, "0,  ");
+               fprintf(s, "\"system\", "); 
+               fprintf(s, "0,  ");
+               fprintf(s, "0,  ");
+               fprintf(s, "0,  ");
+               fprintf(s, "0,  ");
+               fprintf(s, "0,  ");
+               fprintf(s, "0,  ");
+               fprintf(s, "\"");
+               fprintf(s, "version:%s, release:%s, threads:%s, memory:%s, 
host:%s, oid:%d, package:%s ", 
                        ev->version, ev->release, ev->threads, ev->memory, 
ev->host, ev->oid, ev->package);
-               printf("\"      ]\n");
+               fprintf(s, "\"  ]\n");
                return ;
        }
        if( ev->eventnr < 0)
                return;
-       printf("[ ");
-       printf(LLFMT",  ", ev->eventnr);
-       printf("\"%s\", ", ev->clk);
+       fprintf(s, "[ ");
+       fprintf(s, LLFMT",      ", ev->eventnr);
+       fprintf(s, "\"%s\",     ", ev->clk);
        if( ev->function && *ev->function)
-               printf("\"%s[%d]%d\",   ", ev->function, ev->pc, ev->tag);
+               fprintf(s, "\"%s[%d]%d\",       ", ev->function, ev->pc, 
ev->tag);
        else
-               printf("\"\",   ");
-       printf("%d,     ", ev->thread);
+               fprintf(s, "\"\",       ");
+       fprintf(s, "%d, ", ev->thread);
        switch(ev->state){
-       case MDB_START: printf("\"start\",      "); break;
-       case MDB_DONE: printf("\"done \",       "); break;
-       case MDB_WAIT: printf("\"wait \",       "); break;
-       case MDB_PING: printf("\"ping \",       "); break;
-       case MDB_SYSTEM: printf("\"system\",    "); 
+       case MDB_START: fprintf(s, "\"start\",  "); break;
+       case MDB_DONE: fprintf(s, "\"done \",   "); break;
+       case MDB_WAIT: fprintf(s, "\"wait \",   "); break;
+       case MDB_PING: fprintf(s, "\"ping \",   "); break;
+       case MDB_SYSTEM: fprintf(s, "\"system\",        "); 
        }
-       printf(LLFMT",  ", ev->ticks);
-       printf(LLFMT",  ", ev->rss);
-       printf(LLFMT",  ", ev->size);
-       printf(LLFMT",  ", ev->inblock);
-       printf(LLFMT",  ", ev->oublock);
-       printf(LLFMT",  ", ev->majflt);
-       printf(LLFMT",  ", ev->swaps);
-       printf(LLFMT",  ", ev->csw);
-       printf("\"%s\"  ]\n", ev->stmt);
+       fprintf(s, LLFMT",      ", ev->ticks);
+       fprintf(s, LLFMT",      ", ev->rss);
+       fprintf(s, LLFMT",      ", ev->size);
+       fprintf(s, LLFMT",      ", ev->inblock);
+       fprintf(s, LLFMT",      ", ev->oublock);
+       fprintf(s, LLFMT",      ", ev->majflt);
+       fprintf(s, LLFMT",      ", ev->swaps);
+       fprintf(s, LLFMT",      ", ev->csw);
+       fprintf(s, "\"%s\"      ]\n", ev->stmt);
 }
 
 static void
@@ -133,10 +140,13 @@ usageStethoscope(void)
     fprintf(stderr, "stethoscope [options] \n");
     fprintf(stderr, "  -d | --dbname=<database_name>\n");
     fprintf(stderr, "  -u | --user=<user>\n");
+    fprintf(stderr, "  -P | --password=<password>\n");
     fprintf(stderr, "  -p | --port=<portnr>\n");
     fprintf(stderr, "  -h | --host=<hostname>\n");
+    fprintf(stderr, "  -j | --json\n");
     fprintf(stderr, "  -o | --output=<file>\n");
        fprintf(stderr, "  -b | --beat=<delay> in milliseconds (default 50)\n");
+       fprintf(stderr, "  -D | --debug");
     fprintf(stderr, "  -? | --help\n");
        exit(-1);
 }
@@ -152,6 +162,8 @@ stopListening(int i)
                doQ("profiler.stop();");
 stop_disconnect:
        // show follow up action only once
+       if(trace)
+               fclose(trace);
        if(dbh)
                mapi_disconnect(dbh);
        exit(0);
@@ -169,17 +181,17 @@ main(int argc, char **argv)
        char *user = NULL;
        char *password = NULL;
        char buf[BUFSIZ], *buffer, *e, *response;
-       int line = 0, done =0;
-       FILE *trace = NULL;
+       int done = 0;
        EventRecord *ev = malloc(sizeof(EventRecord));
 
-       static struct option long_options[10] = {
+       static struct option long_options[11] = {
                { "dbname", 1, 0, 'd' },
                { "user", 1, 0, 'u' },
                { "port", 1, 0, 'p' },
                { "password", 1, 0, 'P' },
                { "host", 1, 0, 'h' },
                { "help", 0, 0, '?' },
+               { "json", 0, 0, 'j'},
                { "output", 1, 0, 'o' },
                { "debug", 0, 0, 'D' },
                { "beat", 1, 0, 'b' },
@@ -197,7 +209,7 @@ main(int argc, char **argv)
 
        while (1) {
                int option_index = 0;
-               int c = getopt_long(argc, argv, "d:u:p:P:h:?o:Db:",
+               int c = getopt_long(argc, argv, "d:u:p:P:h:?jo:Db:",
                                        long_options, &option_index);
                if (c == -1)
                        break;
@@ -232,11 +244,12 @@ main(int argc, char **argv)
                case 'h':
                        host = optarg;
                        break;
+               case 'j':
+                       json = 1;
+                       break;
                case 'o':
-                       basefilename = strdup(optarg);
-                       if( strstr(basefilename,".trace"))
-                               *strstr(basefilename,".trace") = 0;
-                       printf("-- Output directed towards %s\n", basefilename);
+                       filename = strdup(optarg);
+                       printf("-- Output directed towards %s\n", filename);
                        break;
                case '?':
                        usageStethoscope();
@@ -256,7 +269,7 @@ main(int argc, char **argv)
        }
 
        if(debug)
-               printf("stethoscope -d %s -o %s\n",dbname,basefilename);
+               printf("stethoscope -d %s -o %s\n",dbname,filename);
 
        if (dbname != NULL && strncmp(dbname, "mapi:monetdb://", 15) == 0) {
                uri = dbname;
@@ -308,10 +321,14 @@ main(int argc, char **argv)
                fprintf(stderr,"--%s\n",buf);
        doQ(buf);
 
-       snprintf(buf,BUFSIZ,"%s.trace",basefilename);
-       trace = fopen(buf,"w");
-       if( trace == NULL)
-               fprintf(stderr,"Could not create trace file\n");
+       if(filename != NULL) {
+               trace = fopen(filename,"w");
+
+               if( trace == NULL) {
+                       fprintf(stderr,"Could not create file '%s', printing to 
stdout instead...\n", filename);
+                       filename = NULL;
+               }
+       }
 
        len = 0;
        buflen = BUFSIZ;
@@ -323,24 +340,26 @@ main(int argc, char **argv)
        conn = mapi_get_from(dbh);
        while ((n = mnstr_read(conn, buffer + len, 1, buflen - len-1)) >= 0) {
                buffer[len + n] = 0;
-               if(debug)
-                       printf("%s",buffer);
-               if( trace) 
-                       fprintf(trace,"%s",buffer);
                response = buffer;
+               if(json) {
+                       if(trace != NULL)
+                               fprintf(trace, "%s", response);
+                       else
+                               printf("%s", response);
+               }
                while ((e = strchr(response, '\n')) != NULL) {
                        *e = 0;
-                       //printf("%s\n", response);
-                       done= keyvalueparser(response,ev);
-                       if( done== 1){
-                               renderEvent(ev);
-                               resetEventRecord(ev);
-                       }
-                       if (++line % 200) {
-                               line = 0;
+                       if(!json) {
+                               //printf("%s\n", response);
+                               done= keyvalueparser(response,ev);
+                               if( done== 1){
+                                       renderEvent(ev);
+                                       resetEventRecord(ev);
+                               }
                        }
                        response = e + 1;
                }
+
                /* handle the case that the line is too long to
                 * fit in the buffer */
                if( response == buffer){
@@ -370,6 +389,8 @@ main(int argc, char **argv)
 stop_disconnect:
        if(dbh)
                mapi_disconnect(dbh);
+       if(trace)
+               fclose(trace);
        printf("-- connection with server %s closed\n", uri ? uri : host);
        return 0;
 }
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to