Include git commit ID in the program and output it with the result
authorMikko Rasa <mikko.rasa@movial.fi>
Thu, 26 Mar 2009 09:30:13 +0000 (11:30 +0200)
committerMikko Rasa <mikko.rasa@movial.fi>
Thu, 26 Mar 2009 09:30:13 +0000 (11:30 +0200)
Compute CRC32 of the benchmark and output it with the result as well
Add command line switches to control verbosity and output file

Makefile
main.c

index 26dedff..26dc86d 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,2 +1,4 @@
+VERSION=\"mx11mark-$(shell git rev-parse --short HEAD)\"
+
 mx11mark: main.c
-       $(CC) -o $@ $< $(shell pkg-config --cflags --libs xrender xft) -lm -ggdb -Wall -Wextra
+       $(CC) -o $@ $< -DVERSION=$(VERSION) $(shell pkg-config --cflags --libs xrender xft) -lm -ggdb -Wall -Wextra
diff --git a/main.c b/main.c
index c2dc6a6..ee10cb2 100644 (file)
--- a/main.c
+++ b/main.c
 #include <X11/extensions/Xrender.h>
 #include <X11/Xft/Xft.h>
 
+#ifndef VERSION
+#define VERSION "mx11mark-unknown"
+#endif
+
 typedef enum eMeasureType
 {
        NORMAL,
@@ -39,6 +43,7 @@ typedef struct sResult
        unsigned error_margin;
 } Result;
 
+void update_crc32(unsigned *crc32, const char *data, unsigned size);
 Result measure(void (*func)(), unsigned batch, unsigned time, MeasureType type);
 void sync();
 void nop();
@@ -67,6 +72,7 @@ XftDraw *ftdraw;
 char *text;
 unsigned textlen;
 int copymode;
+int verbosity;
 
 int main(int argc, char **argv)
 {
@@ -79,17 +85,36 @@ int main(int argc, char **argv)
        unsigned batch=3000;
        unsigned time=1000;
        MeasureType type=NORMAL;
+       char *in_fn=NULL;
+       char *out_fn="result.csv";
        FILE *in=NULL, *out;
        float total=0;
        float weight=1.0;
        int prec;
+       int opt;
+       unsigned crc32=0;
+
+       printf("%s\n", VERSION);
+
+       while((opt=getopt(argc, argv, "vo:"))!=-1) switch(opt)
+       {
+       case 'v':
+               ++verbosity;
+               break;
+       case 'o':
+               out_fn=optarg;
+               break;
+       }
 
-       if(argc>=2)
-               in=fopen(argv[1], "r");
+       if(optind<argc)
+       {
+               in_fn=argv[optind];
+               in=fopen(in_fn, "r");
+       }
        else
        {
                fprintf(stderr, "Benchmark script not specified\n");
-               fprintf(stderr, "Usage: %s <script>\n", argv[0]);
+               fprintf(stderr, "Usage: %s [-v] [-o <outfile>] <script>\n", argv[0]);
                return 1;
        }
 
@@ -99,7 +124,7 @@ int main(int argc, char **argv)
                return 1;
        }
 
-       out=fopen("result.csv", "w");
+       out=fopen(out_fn, "w");
        if(!out)
                fprintf(stderr, "Output file could not be opened, proceeding with stdout only\n");
 
@@ -136,16 +161,20 @@ int main(int argc, char **argv)
        while(1)
        {
                char line[1024];
-               char *delim;
+               char *delim=line;
                int ret;
                
                ret=fscanf(in, " %[^\n]", line);
                if(ret<1)
                        break;
 
+               if(line[0]=='#')
+                       continue;
+
                delim=strchr(line, '=');
                if(delim)
                {
+                       update_crc32(&crc32, line, strlen(line));
                        if(!strncmp(line, "time", delim-line))
                                time=strtoul(delim+1, NULL, 10);
                        else if(!strncmp(line, "batch", delim-line))
@@ -230,6 +259,7 @@ int main(int argc, char **argv)
                        {
                                Result res;
                                unsigned size=0;
+                               update_crc32(&crc32, line, strlen(line));
                                printf("%s:\n", test_name);
                                if(!strcmp(delim+1, "rect") || !strcmp(delim+1, "rectangle"))
                                {
@@ -299,15 +329,33 @@ int main(int argc, char **argv)
        prec=(-log(total)/log(10))+4;
        if(prec<0)
                prec=0;
-       printf("Total score: %.*f\n", prec, total);
+       printf("Total score: %.*f (%s %s-%08x)\n", prec, total, VERSION, in_fn, crc32);
        if(out)
-               fprintf(out, "%.*f\n", prec, total);
+               fprintf(out, "%.*f,%s,%s-%08x\n", prec, total, VERSION, in_fn, crc32);
        fclose(out);
        fclose(in);
 
        return 0;
 }
 
+void update_crc32(unsigned *crc32, const char *data, unsigned size)
+{
+       unsigned i;
+       for(i=0; i<size; ++i)
+       {
+               unsigned j;
+               unsigned char c=data[i];
+               for(j=0; j<8; ++j)
+               {
+                       int high=*crc32>>31;
+                       *crc32=(*crc32<<1)|(c>>7);
+                       if(high)
+                               *crc32^=0x04C11DB7;
+                       c<<=1;
+               }
+       }
+}
+
 unsigned filter_outliers(unsigned *data, unsigned count)
 {
        unsigned i;
@@ -398,7 +446,8 @@ Result measure(void (*func)(), unsigned batch, unsigned time, MeasureType type)
                }
                res.ops_per_sec=count*1000/delta;
                res.error_margin=0;
-               printf("  %d calls in %d ms = %d/sec\n", count, delta, res.ops_per_sec);
+               if(verbosity>=1)
+                       printf("  %d calls in %d ms = %d/sec\n", count, delta, res.ops_per_sec);
                return res;
        }
 }