Use unique tmp file to allow parallel printf tests (#215)

Parallel printf tests fail because they use the same file /tmp/tmpfile
to capture OpenCL kernel outputs. This patch solves this problem by
using a unique file for each process.
This commit is contained in:
Wenju He
2019-04-26 01:10:46 +08:00
committed by Kévin Petit
parent 355b2d2460
commit 2eea74bc95

View File

@@ -28,6 +28,7 @@
#define streamDup2(fd1,fd2) dup2(fd1,fd2) #define streamDup2(fd1,fd2) dup2(fd1,fd2)
#endif #endif
#include <limits.h> #include <limits.h>
#include <time.h>
#include "test_printf.h" #include "test_printf.h"
#if defined(_WIN32) #if defined(_WIN32)
@@ -55,10 +56,10 @@ static void printUsage( void );
//Stream helper functions //Stream helper functions
//Associate stdout stream with the file(/tmp/tmpfile):i.e redirect stdout stream to the specific files (/tmp/tmpfile) //Associate stdout stream with the file(gFileName):i.e redirect stdout stream to the specific files (gFileName)
static int acquireOutputStream(); static int acquireOutputStream();
//Close the file(/tmp/tmpfile) associated with the stdout stream and disassociates it. //Close the file(gFileName) associated with the stdout stream and disassociates it.
static void releaseOutputStream(int fd); static void releaseOutputStream(int fd);
//Get analysis buffer to verify the correctess of printed data //Get analysis buffer to verify the correctess of printed data
@@ -106,21 +107,42 @@ static cl_context gContext;
static cl_command_queue gQueue; static cl_command_queue gQueue;
static int gFd; static int gFd;
static char gFileName[256];
//----------------------------------------- //-----------------------------------------
// Static helper functions definition // Static helper functions definition
//----------------------------------------- //-----------------------------------------
//-----------------------------------------
// getTempFileName
//-----------------------------------------
static int getTempFileName()
{
// Create a unique temporary file to allow parallel executed tests.
#if (defined(__linux__) || defined(__APPLE__)) && (!defined( __ANDROID__ ))
sprintf(gFileName, "/tmp/tmpfile.XXXXXX");
int fd = mkstemp(gFileName);
if (fd == -1)
return -1;
close(fd);
#elif defined(_WIN32)
UINT ret = GetTempFileName(".", "tmp", 0, gFileName);
if (ret == 0)
return -1;
#else
MTdata d = init_genrand((cl_uint)time(NULL));
sprintf(gFileName, "tmpfile.%u", genrand_int32(d));
#endif
return 0;
}
//----------------------------------------- //-----------------------------------------
// acquireOutputStream // acquireOutputStream
//----------------------------------------- //-----------------------------------------
static int acquireOutputStream() static int acquireOutputStream()
{ {
int fd = streamDup(fileno(stdout)); int fd = streamDup(fileno(stdout));
#if (defined(__linux__) || defined(__APPLE__)) && (!defined( __ANDROID__ )) freopen(gFileName,"w",stdout);
freopen("/tmp/tmpfile","w",stdout);
#else
freopen("tmpfile","w",stdout);
#endif
return fd; return fd;
} }
@@ -142,11 +164,7 @@ static void getAnalysisBuffer(char* analysisBuffer)
FILE *fp; FILE *fp;
memset(analysisBuffer,0,ANALYSIS_BUFFER_SIZE); memset(analysisBuffer,0,ANALYSIS_BUFFER_SIZE);
#if (defined(__linux__) || defined(__APPLE__)) && (!defined( __ANDROID__ )) fp = fopen(gFileName,"r");
fp = fopen("/tmp/tmpfile","r");
#else
fp = fopen("tmpfile","r");
#endif
if(NULL == fp) if(NULL == fp)
log_error("Failed to open analysis buffer ('%s')\n", strerror(errno)); log_error("Failed to open analysis buffer ('%s')\n", strerror(errno));
else else
@@ -978,6 +996,12 @@ int main(int argc, const char* argv[])
} }
} }
if (getTempFileName() == -1)
{
log_error("getTempFileName failed\n");
return -1;
}
int err = runTestHarnessWithCheck( argCount, argList, test_num, test_list, false, true, 0, InitCL ); int err = runTestHarnessWithCheck( argCount, argList, test_num, test_list, false, true, 0, InitCL );
if(gQueue) if(gQueue)
@@ -995,6 +1019,7 @@ int main(int argc, const char* argv[])
free(argList); free(argList);
remove(gFileName);
return err; return err;
} }