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

Commits:

3 changed files:

Changes:

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

  • testsuite/tests/hpc/all.T
    ... ... @@ -7,6 +7,16 @@ test('T10138', [extra_files(['.keepme.hpc.T10138/']),
    7 7
     
    
    8 8
     test('T11798', normal, makefile_test, [])
    
    9 9
     
    
    10
    +def tixDirTwice(cmd):
    
    11
    +    return (
    
    12
    +        'HPCTIXDIR=tix-files ' + cmd + ' && '
    
    13
    +        'HPCTIXDIR=tix-files ' + cmd + ' && '
    
    14
    +        'set -- tix-files/*.tix && '
    
    15
    +        'test "$#" -eq 4'
    
    16
    +    )
    
    17
    +
    
    18
    +test('tixDir', [cmd_wrapper(tixDirTwice)], compile_and_run, ['-fhpc'])
    
    19
    +
    
    10 20
     # Run tests below only for the hpc way.
    
    11 21
     #
    
    12 22
     # Do not explicitly specify '-fhpc' in extra_hc_opts, unless also setting
    

  • testsuite/tests/hpc/tixDir.hs
    1
    +import System.Environment (getProgName)
    
    2
    +import System.Process (getCurrentPid)
    
    3
    +
    
    4
    +main :: IO ()
    
    5
    +main = do
    
    6
    +  prog <- getProgName
    
    7
    +  pid <- getCurrentPid
    
    8
    +  -- create conflicts with the .tix file that'd normally get created to check
    
    9
    +  -- that filename generation picks something new and unique instead
    
    10
    +  writeFile ("tix-files/" ++ prog ++ "-" ++ show pid ++ "-0.tix") "foo"