Florian Ragwitz pushed to branch wip/rafl/hpc-tixdir at Glasgow Haskell Compiler / GHC

Commits:

1 changed file:

Changes:

  • rts/Hpc.c
    ... ... @@ -13,6 +13,7 @@
    13 13
     #include <ctype.h>
    
    14 14
     #include <string.h>
    
    15 15
     #include <assert.h>
    
    16
    +#include <limits.h>
    
    16 17
     #include <fs_rts.h>
    
    17 18
     
    
    18 19
     #if defined(HAVE_SYS_TYPES_H)
    
    ... ... @@ -36,6 +37,7 @@
    36 37
     static int hpc_inited = 0;              // Have you started this component?
    
    37 38
     static pid_t hpc_pid = 0;               // pid of this process at hpc-boot time.
    
    38 39
                                             // Only this pid will read or write .tix file(s).
    
    40
    +static char *tixDir = NULL;             // Directory to write .tix files to
    
    39 41
     static FILE *tixFile;                   // file being read/written
    
    40 42
     static int tix_ch;                      // current char
    
    41 43
     
    
    ... ... @@ -216,27 +218,20 @@ startupHpc(void)
    216 218
       if (hpc_tixfile != NULL) {
    
    217 219
         tixFilename = strdup(hpc_tixfile);
    
    218 220
       } else if (hpc_tixdir != NULL) {
    
    219
    -    /* Make sure the directory is present;
    
    220
    -     * conditional code for mkdir lifted from lndir.c
    
    221
    -     */
    
    221
    +    tixDir = strdup(hpc_tixdir);
    
    222 222
     #if defined(WIN32)
    
    223
    -    mkdir(hpc_tixdir);
    
    223
    +    mkdir(tixDir);
    
    224 224
     #else
    
    225
    -    mkdir(hpc_tixdir,0777);
    
    225
    +    mkdir(tixDir,0777);
    
    226 226
     #endif
    
    227
    -    /* Then, try open the file
    
    228
    -     */
    
    229
    -    tixFilename = (char *) stgMallocBytes(strlen(hpc_tixdir) +
    
    230
    -                                          strlen(prog_name) + 12,
    
    231
    -                                          "Hpc.startupHpc");
    
    232
    -    sprintf(tixFilename,"%s/%s-%d.tix",hpc_tixdir,prog_name,(int)hpc_pid);
    
    233 227
       } else {
    
    234 228
         tixFilename = (char *) stgMallocBytes(strlen(prog_name) + 6,
    
    235 229
                                               "Hpc.startupHpc");
    
    236 230
         sprintf(tixFilename, "%s.tix", prog_name);
    
    237 231
       }
    
    238 232
     
    
    239
    -  if (RtsFlags.HpcFlags.readTixFile && init_open(__rts_fopen(tixFilename,"r"))) {
    
    233
    +  if (tixFilename != NULL && RtsFlags.HpcFlags.readTixFile
    
    234
    +      && init_open(__rts_fopen(tixFilename,"r"))) {
    
    240 235
         readTix();
    
    241 236
       }
    
    242 237
     }
    
    ... ... @@ -377,6 +372,29 @@ freeHpcModuleInfo (HpcModuleInfo *mod)
    377 372
         stgFree(mod);
    
    378 373
     }
    
    379 374
     
    
    375
    +// Just using mkstemps would be so much nicer, but it's not available on all
    
    376
    +// target platforms. We could use it on platforms where it is, but that would
    
    377
    +// just make it even more complicated. In this particular context, the approach
    
    378
    +// below seems fine.
    
    379
    +static FILE *uniqueFile(const char *dir) {
    
    380
    +  const char fmt[] = "%s/%s-%d-%u.tix";
    
    381
    +  int len = snprintf(NULL, 0, fmt, dir, prog_name, (int)hpc_pid, UINT_MAX);
    
    382
    +  if (len < 0)
    
    383
    +    return NULL;
    
    384
    +
    
    385
    +  char *name = stgMallocBytes((size_t)len + 1, "Hpc.uniqueFile");
    
    386
    +  FILE *f = NULL;
    
    387
    +  for (unsigned int i = 0; i < UINT_MAX; i++) {
    
    388
    +    snprintf(name, (size_t)len + 1, fmt, dir, prog_name, (int)hpc_pid, i);
    
    389
    +    f = __rts_fopen(name, "wx");
    
    390
    +    if (f != NULL || errno != EEXIST)
    
    391
    +      break;
    
    392
    +  }
    
    393
    +
    
    394
    +  stgFree(name);
    
    395
    +  return f;
    
    396
    +}
    
    397
    +
    
    380 398
     /* Called at the end of execution, to write out the Hpc *.tix file
    
    381 399
      * for this execution. Safe to call, even if coverage is not used.
    
    382 400
      */
    
    ... ... @@ -398,7 +416,7 @@ exitHpc(void) {
    398 416
       bool is_subprocess = false;
    
    399 417
     #endif
    
    400 418
       if (!is_subprocess && RtsFlags.HpcFlags.writeTixFile) {
    
    401
    -    FILE *f = __rts_fopen(tixFilename,"w+");
    
    419
    +    FILE *f = tixDir != NULL ? uniqueFile(tixDir) : __rts_fopen(tixFilename, "w+");
    
    402 420
         writeTix(f);
    
    403 421
       }
    
    404 422
     
    
    ... ... @@ -407,6 +425,8 @@ exitHpc(void) {
    407 425
     
    
    408 426
       stgFree(tixFilename);
    
    409 427
       tixFilename = NULL;
    
    428
    +  free(tixDir);
    
    429
    +  tixDir = NULL;
    
    410 430
     }
    
    411 431
     
    
    412 432
     //////////////////////////////////////////////////////////////////////////////