get new block fix
[lttv.git] / genevent / genevent.c
index 5930489f755bc1f7f9ce7140f31313509a337c85..67ecbd402d78a817a1ff3b0eac0c2aceae205a2f 100644 (file)
@@ -45,10 +45,14 @@ This program is distributed in the hope that it will be useful,
 #include "parser.h"
 #include "genevent.h"
 
+#define max(a,b) ((a)<(b))?(b):(a)
+
 /* Named types may be referenced from anywhere */
 
 facility * fac;
 
+unsigned alignment = 0;
+
 int main(int argc, char** argv)
 {
   char *token;
@@ -58,6 +62,10 @@ int main(int argc, char** argv)
 
   if(argc < 2){
     printf("At least one event definition file is needed\n");
+    printf("You may specify the default alignment for a facility with\n");
+    printf("  -a x , where x is the desired alignment in bytes.\n");
+    printf("The alignment value will affect all the following xml files.\n");
+    printf("i.e. genevent -a 8 core.xml -a 4 kernel.xml is valid.\n");
     exit(1);
   }
 
@@ -65,52 +73,61 @@ int main(int argc, char** argv)
   in.error = error_callback;
 
   for(i = 1 ; i < argc ; i++) {
-    in.lineno = 0;
-    in.name = allocAndCopy(argv[i]);
 
-    in.fp = fopen(in.name, "r");
-    if(!in.fp ){
-      in.error(&in,"cannot open facility input file");
-    }
-
-    while(1){
-      token = getToken(&in);
-      if(in.type == ENDFILE) break;
+               if(strcmp("-a", argv[i])==0) {
+                       if(i >= argc-1) {
+                               printf("Error : missing argument to -a\n");
+                               exit(1);
+                       } else i++;
+                       alignment = atoi(argv[i]);
+               } else {
+               
+                       in.lineno = 0;
+                       in.name = allocAndCopy(argv[i]);
 
-      if(strcmp(token, "<")) in.error(&in,"not a facility file");
-      token = getName(&in);
-    
-      if(strcmp("facility",token) == 0) {
-        fac = memAlloc(sizeof(facility));
-        fac->name = NULL;
-        fac->description = NULL;
-        sequence_init(&(fac->events));
-        table_init(&(fac->named_types));
-        sequence_init(&(fac->unnamed_types));
-        
-        parseFacility(&in, fac);
-        
-        //check if any namedType is not defined
-        checkNamedTypesImplemented(&fac->named_types);
-      }
-      else in.error(&in,"facility token was expected");
-
-      generateFile(argv[i]);
-
-      free(fac->name);
-      free(fac->description);
-      freeEvents(&fac->events);
-      sequence_dispose(&fac->events);
-      freeNamedType(&fac->named_types);
-      table_dispose(&fac->named_types);
-      freeTypes(&fac->unnamed_types);
-      sequence_dispose(&fac->unnamed_types);      
-      free(fac);      
-    }
+                       in.fp = fopen(in.name, "r");
+                       if(!in.fp ){
+                               in.error(&in,"cannot open facility input file");
+                       }
 
-    free(in.name);
-    fclose(in.fp);
+                       while(1){
+                               token = getToken(&in);
+                               if(in.type == ENDFILE) break;
+
+                               if(strcmp(token, "<")) in.error(&in,"not a facility file");
+                               token = getName(&in);
+                       
+                               if(strcmp("facility",token) == 0) {
+                                       fac = memAlloc(sizeof(facility));
+                                       fac->name = NULL;
+                                       fac->description = NULL;
+                                       sequence_init(&(fac->events));
+                                       table_init(&(fac->named_types));
+                                       sequence_init(&(fac->unnamed_types));
+                                       
+                                       parseFacility(&in, fac);
+                                       
+                                       //check if any namedType is not defined
+                                       checkNamedTypesImplemented(&fac->named_types);
+                               }
+                               else in.error(&in,"facility token was expected");
+
+                               generateFile(argv[i]);
+
+                               free(fac->name);
+                               free(fac->description);
+                               freeEvents(&fac->events);
+                               sequence_dispose(&fac->events);
+                               freeNamedType(&fac->named_types);
+                               table_dispose(&fac->named_types);
+                               freeTypes(&fac->unnamed_types);
+                               sequence_dispose(&fac->unnamed_types);      
+                               free(fac);      
+                       }
 
+                       free(in.name);
+                       fclose(in.fp);
+               }
   }
   return 0;
 }
@@ -314,15 +331,17 @@ printStruct(FILE * fp, int len, void ** array, char * name, char * facName,
   }
 
   if(flag) {
-    if(type->alignment == 0)
+               unsigned align = max(alignment, type->alignment);
+               
+    if(align == 0)
       fprintf(fp,"} __attribute__ ((packed));\n\n");
     else {
-      if(type->alignment != 1 && type->alignment != 2
-          && type->alignment != 4 && type->alignment != 8) {
-        printf("Wrong alignment %i, using packed.\n", type->alignment);
+      if(align != 1 && align != 2
+          && align != 4 && align != 8) {
+        printf("Wrong alignment %i, using packed.\n", align);
         fprintf(fp,"} __attribute__ ((packed));\n\n");
       } else
-        fprintf(fp,"} __attribute__ ((aligned(%i)));\n\n", type->alignment);
+        fprintf(fp,"} __attribute__ ((aligned(%i)));\n\n", align);
     }
   }
 }
@@ -540,8 +559,12 @@ void generateStructFunc(FILE * fp, char * facName, unsigned long checksum){
                 * address for alignment */
                /* NOTE : using cmpxchg in reserve with repeat for atomicity */
     // Replaces _offset
+               /* NOTE2 : as the read old address from memory must come before any
+                * protected event, let's add a memory barrier() to make sure the compiler
+                * will not reorder this read. */
                fprintf(fp, "\t\tdo {\n");
                fprintf(fp, "\t\t\told_address = buf->data + buf->offset;\n");
+               fprintf(fp, "\t\t\tbarrier();\n");
     fprintf(fp, "\t\t\tchar *ptr = (char*)old_address;\n");
 
 
@@ -553,9 +576,10 @@ void generateStructFunc(FILE * fp, char * facName, unsigned long checksum){
 
     for(pos1=0;pos1<structCount;pos1++){
 
-      if(ev->type->alignment > 1) {
+                       unsigned align = max(alignment, ev->type->alignment);
+      if(align > 1) {
         fprintf(fp,"\t\t\tptr += (%u - ((unsigned int)ptr&(%u-1)))&(%u-1) ;\n",
-            ev->type->alignment, ev->type->alignment, ev->type->alignment);
+            align, align, align);
       }
       fprintf(fp,"\t\t\tptr += sizeof(struct %s_%s_%d);\n",ev->name,
           facName,pos1+1);
@@ -572,25 +596,34 @@ void generateStructFunc(FILE * fp, char * facName, unsigned long checksum){
                                td = fld->type;
                                if(td->type == SEQUENCE || td->type==STRING || td->type==ARRAY){
                                        if(td->type == SEQUENCE) {
-            if(td->alignment > 1) {
-              fprintf(fp,"\t\t\tptr += (%u - ((unsigned int)ptr&(%u-1)))&(%u-1)) ;\n",
-                      td->alignment, td->alignment, td->alignment);
-            }
-                                               fprintf(fp,
-                "\t\t\tptr += sizeof(%s) + (sizeof(%s) * seqlength_%d);\n",
-                                                               uintOutputTypes[td->size], getTypeStr(td), ++seqCount);
+
+                                               unsigned align = max(alignment, td->alignment);
+                                               if(align > 1) {
+                                                       fprintf(fp,"\t\tptr+=(%u - ((unsigned int)ptr&(%u-1)))&(%u-1);\n",
+                                                                                       align, align, align);
+                                               }
+                                               fprintf(fp,"\t\tptr += sizeof(%s);\n",uintOutputTypes[td->size]);
+                                               if(align > 1) {
+                                                       fprintf(fp,"\t\tptr+=(%u - ((unsigned int)ptr&(%u-1)))&(%u-1);\n",
+                                                                                       align, align, align);
+                                               }
+                                               fprintf(fp,"\t\tptr += sizeof(%s) * seqlength_%d;\n\n",
+                                                       getTypeStr(td), seqCount);
+
           } else if(td->type==STRING) {
-            if(td->alignment > 1) {
+                                               unsigned align = max(alignment, td->alignment);
+            if(align > 1) {
               fprintf(fp,"\t\t\tptr += (%u - ((unsigned int)ptr&(%u-1)))&(%u-1)) ;\n",
-                 td->alignment, td->alignment, td->alignment);
+                 align, align, align);
             }
             fprintf(fp,"ptr += strlength_%d + 1;\n",
                                                 ++strCount);
           }
                                        else if(td->type==ARRAY) {
-            if(td->alignment > 1) {
+                                               unsigned align = max(alignment, td->alignment);
+            if(align > 1) {
               fprintf(fp,"\t\t\tptr += (%u - ((unsigned int)ptr&(%u-1)))&(%u-1);\n",
-                 td->alignment, td->alignment, td->alignment);
+                 align, align, align);
             }
                                                fprintf(fp,"\t\t\tptr += sizeof(%s) * %d;\n",
                 getTypeStr(td),td->size);
@@ -602,7 +635,12 @@ void generateStructFunc(FILE * fp, char * facName, unsigned long checksum){
 
     fprintf(fp, "\t\t\tevent_length = (unsigned long)ptr -"
                                "(unsigned long)old_address;\n");
-
+               
+               /* let's put some protection before the cmpxchg : the space reservation and
+                * the get TSC are not dependant from each other. I don't want the compiler
+                * to reorder those in the wrong order. And relay_reserve is inline, so
+                * _yes_, the compiler could mess it up. */
+               fprintf(fp, "\t\t\tbarrier();\n");
                fprintf(fp, "\t\t\tbuff = relay_reserve(channel->rchan, event_length, "
                                                                                                "old_address);\n");
                fprintf(fp, "\n");
@@ -651,9 +689,10 @@ void generateStructFunc(FILE * fp, char * facName, unsigned long checksum){
     // Declare an alias pointer of the struct type to the beginning
     // of the reserved area, just after the event header.
     if(ev->type != 0) {
-      if(ev->type->alignment > 1) {
+                       unsigned align = max(alignment, td->alignment);
+      if(align > 1) {
         fprintf(fp,"\t\tptr+=(%u - ((unsigned int)ptr&(%u-1)))&(%u-1);\n",
-              ev->type->alignment, ev->type->alignment, ev->type->alignment);
+              align, align, align);
       }
     
       fprintf(fp, "\t\t__1 = (struct %s_%s_1 *)(ptr);\n",
@@ -731,15 +770,16 @@ void generateStructFunc(FILE * fp, char * facName, unsigned long checksum){
                                        flag = 0;
                                        fprintf(fp,"\t\t//copy sequence length and sequence to buffer\n");
 
-          if(td->alignment > 1) {
+                                       unsigned align = max(alignment, td->alignment);
+          if(align > 1) {
             fprintf(fp,"\t\tptr+=(%u - ((unsigned int)ptr&(%u-1)))&(%u-1);\n",
-                    td->alignment, td->alignment, td->alignment);
+                    align, align, align);
           }
                                        fprintf(fp,"\t\t*ptr = seqlength_%d;\n",++seqCount);
                                        fprintf(fp,"\t\tptr += sizeof(%s);\n",uintOutputTypes[td->size]);
-          if(td->alignment > 1) {
+          if(align > 1) {
             fprintf(fp,"\t\tptr+=(%u - ((unsigned int)ptr&(%u-1)))&(%u-1);\n",
-                    td->alignment, td->alignment, td->alignment);
+                    align, align, align);
           }
                                        fprintf(fp,"\t\tmemcpy(ptr, %s, sizeof(%s) * seqlength_%d);\n",
                                                fld->name, getTypeStr(td), seqCount);
@@ -750,9 +790,10 @@ void generateStructFunc(FILE * fp, char * facName, unsigned long checksum){
                                        flag = 0;
                                        fprintf(fp,"\t\t//copy string to buffer\n");
                                        fprintf(fp,"\t\tif(strlength_%d > 0){\n",++strCount);
-          if(td->alignment > 1) {
+                                       unsigned align = max(alignment, td->alignment);
+          if(align > 1) {
             fprintf(fp,"\t\tptr+=(%u - ((unsigned int)ptr&(%u-1)))&(%u-1);\n",
-                    td->alignment, td->alignment, td->alignment);
+                    align, align, align);
           }
                                        fprintf(fp,"\t\t\tmemcpy(ptr, %s, strlength_%d + 1);\n",
                                                        fld->name, strCount);
@@ -764,9 +805,10 @@ void generateStructFunc(FILE * fp, char * facName, unsigned long checksum){
                                }else if(td->type==ARRAY){
                                        flag = 0;
                                        fprintf(fp,"\t//copy array to buffer\n");
-          if(td->alignment > 1) {
+                                       unsigned align = max(alignment, td->alignment);
+          if(align > 1) {
             fprintf(fp,"\t\tptr+=(%u - ((unsigned int)ptr&(%u-1)))&(%u-1);\n",
-                    td->alignment, td->alignment, td->alignment);
+                    align, align, align);
           }
                                        fprintf(fp,"\tmemcpy(ptr, %s, sizeof(%s) * %d);\n",
                                                        fld->name, getTypeStr(td), td->size);
This page took 0.047581 seconds and 4 git commands to generate.