Index: compiler-rt/lib/profile/InstrProfilingUtil.c =================================================================== --- compiler-rt/lib/profile/InstrProfilingUtil.c +++ compiler-rt/lib/profile/InstrProfilingUtil.c @@ -207,8 +207,9 @@ f = fdopen(fd, "r+b"); #elif defined(_WIN32) // FIXME: Use the wide variants to handle Unicode filenames. - HANDLE h = CreateFileA(ProfileName, GENERIC_READ | GENERIC_WRITE, 0, 0, - OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); + HANDLE h = CreateFileA(ProfileName, GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_ALWAYS, + FILE_ATTRIBUTE_NORMAL, 0); if (h == INVALID_HANDLE_VALUE) return NULL; @@ -218,6 +219,10 @@ return NULL; } + if (lprofLockFd(fd) != 0) + PROF_WARN("Data may be corrupted during profile merging : %s\n", + "Fail to obtain file lock due to system limit."); + f = _fdopen(fd, "r+b"); if (f == 0) { CloseHandle(h); Index: compiler-rt/test/profile/Windows/Inputs/instrprof-multiwrite.c =================================================================== --- /dev/null +++ compiler-rt/test/profile/Windows/Inputs/instrprof-multiwrite.c @@ -0,0 +1,83 @@ +/* This is a test case where the parent process forks 10 children + * which contend to merge profile data to the same file. With + * file locking support, the data from each child should not + * be lost. + */ +#include +#include +#include + +void spawn_child(PROCESS_INFORMATION *pi, int child_num) { + wchar_t child_str[10]; + _itow(child_num, child_str, 10); + if (!SetEnvironmentVariableW(L"CHILD_NUM", child_str)) { + printf("SetEnvironmentVariableW failed (0x%8lx).\n", GetLastError()); + fflush(stdout); + exit(1); + } + + STARTUPINFOW si; + memset(&si, 0, sizeof(si)); + si.cb = sizeof(si); + + memset(pi, 0, sizeof(PROCESS_INFORMATION)); + + if (!CreateProcessW(NULL, // No module name (use command line) + GetCommandLineW(), // Command line + NULL, // Process handle not inheritable + NULL, // Thread handle not inheritable + TRUE, // Set handle inheritance to TRUE + 0, // No flags + NULL, // Use parent's environment block + NULL, // Use parent's starting directory + &si, pi)) { + printf("CreateProcess failed (0x%08lx).\n", GetLastError()); + fflush(stdout); + exit(1); + } +} + +int wait_child(PROCESS_INFORMATION *pi) { + WaitForSingleObject(pi->hProcess, INFINITE); + + DWORD exit_code; + if (!GetExitCodeProcess(pi->hProcess, &exit_code)) { + printf("GetExitCodeProcess failed (0x%08lx).\n", GetLastError()); + fflush(stdout); + exit(1); + } + + CloseHandle(pi->hProcess); + CloseHandle(pi->hThread); + + return exit_code; +} + +int foo(int a, int b) { + return a + b; +} + +extern FILE *lprofOpenFileEx(const char *); +int main(int argc, char *argv[]) { + char *child_str = getenv("CHILD_NUM"); + if (!child_str) { + PROCESS_INFORMATION child[10]; + // In parent + for (int i = 0; i < 10; i++) { + spawn_child(&child[i], i); + } + for (int i = 0; i < 10; i++) { + wait_child(&child[i]); + } + return 0; + } else { + // In child + int child_num = atoi(child_str); + int result = foo(child_num, child_num); + if (result > 20) { + fprintf(stderr, "Invalid child count!"); + return 1; + } + return 0; + } +} Index: compiler-rt/test/profile/Windows/Inputs/instrprof-open-file.c =================================================================== --- /dev/null +++ compiler-rt/test/profile/Windows/Inputs/instrprof-open-file.c @@ -0,0 +1,107 @@ +/* This is a test case where the parent process forks 10 + * children which contend to write to the same file. With + * file locking support, the data from each child should not + * be lost. + */ +#include +#include +#include +#include +#include +#include + +void spawn_child(PROCESS_INFORMATION *pi, int child_num) { + wchar_t child_str[10]; + _itow(child_num, child_str, 10); + if (!SetEnvironmentVariableW(L"CHILD_NUM", child_str)) { + printf("SetEnvironmentVariableW failed (0x%8lx).\n", GetLastError()); + fflush(stdout); + exit(1); + } + + STARTUPINFOW si; + memset(&si, 0, sizeof(si)); + si.cb = sizeof(si); + + memset(pi, 0, sizeof(PROCESS_INFORMATION)); + + if (!CreateProcessW(NULL, // No module name (use command line) + GetCommandLineW(), // Command line + NULL, // Process handle not inheritable + NULL, // Thread handle not inheritable + TRUE, // Set handle inheritance to TRUE + 0, // No flags + NULL, // Use parent's environment block + NULL, // Use parent's starting directory + &si, pi)) { + printf("CreateProcess failed (0x%08lx).\n", GetLastError()); + fflush(stdout); + exit(1); + } +} + +int wait_child(PROCESS_INFORMATION *pi) { + WaitForSingleObject(pi->hProcess, INFINITE); + + DWORD exit_code; + if (!GetExitCodeProcess(pi->hProcess, &exit_code)) { + printf("GetExitCodeProcess failed (0x%08lx).\n", GetLastError()); + fflush(stdout); + exit(1); + } + + CloseHandle(pi->hProcess); + CloseHandle(pi->hThread); + + return exit_code; +} + +extern FILE *lprofOpenFileEx(const char *); +int main(int argc, char *argv[]) { + FILE *F; + const char *FN; + + if (argc < 2) { + fprintf(stderr, "Requires one argument \n"); + return 1; + } + FN = argv[1]; + + char *child_num = getenv("CHILD_NUM"); + if (!child_num) { + PROCESS_INFORMATION child[10]; + + // In parent + int fh; + if (_sopen_s(&fh, FN, _O_RDWR | _O_CREAT, _SH_DENYNO, + _S_IREAD | _S_IWRITE) == 0) { + _chsize(fh, 0); + _close(fh); + } + for (int i = 0; i < 10; i++) { + spawn_child(&child[i], i); + } + for (int i = 0; i < 10; i++) { + wait_child(&child[i]); + } + F = lprofOpenFileEx(FN); + if (!F) { + fprintf(stderr, "Can not open file %s from parent\n", FN); + return 1; + } + fseek(F, 0, SEEK_END); + fprintf(F, "Dump from parent %d\n", 10); + return 0; + } else { + // In child + FILE *F = lprofOpenFileEx(FN); + if (!F) { + fprintf(stderr, "Can not open file %s from child\n", FN); + return 1; + } + fseek(F, 0, SEEK_END); + fprintf(F, "Dump from Child %s\n", child_num); + fclose(F); + return 0; + } +} Index: compiler-rt/test/profile/Windows/instrprof-multiwrite.test =================================================================== --- /dev/null +++ compiler-rt/test/profile/Windows/instrprof-multiwrite.test @@ -0,0 +1,17 @@ +RUN: mkdir -p %t.d +RUN: %clang_profgen %S/Inputs/instrprof-multiwrite.c -o %t +RUN: rm -f %t_*.profraw +RUN: env LLVM_PROFILE_FILE=%t_%m.profraw %run %t +RUN: %run %t +RUN: llvm-profdata show -function=foo %t_*.profraw | FileCheck %s + +CHECK: Counters: +CHECK: foo: +CHECK: Hash: 0x0000000000000018 +CHECK: Counters: 1 +CHECK: Function count: 10 +CHECK: Instrumentation level: Front-end +CHECK: Functions shown: 1 +CHECK: Total functions: 14 +CHECK: Maximum function count: 44 +CHECK: Maximum internal block count: 10 Index: compiler-rt/test/profile/Windows/instrprof-open-file.test =================================================================== --- /dev/null +++ compiler-rt/test/profile/Windows/instrprof-open-file.test @@ -0,0 +1,17 @@ +RUN: mkdir -p %t.d +RUN: %clang_profgen %S/Inputs/instrprof-open-file.c -o %t +RUN: rm -f %t.d/run.dump +RUN: %run %t %t.d/run.dump +RUN: sort %t.d/run.dump | FileCheck %s + +CHECK: Dump from Child 0 +CHECK-NEXT: Dump from Child 1 +CHECK-NEXT: Dump from Child 2 +CHECK-NEXT: Dump from Child 3 +CHECK-NEXT: Dump from Child 4 +CHECK-NEXT: Dump from Child 5 +CHECK-NEXT: Dump from Child 6 +CHECK-NEXT: Dump from Child 7 +CHECK-NEXT: Dump from Child 8 +CHECK-NEXT: Dump from Child 9 +CHECK-NEXT: Dump from parent 10 Index: compiler-rt/test/profile/Windows/lit.local.cfg.py =================================================================== --- /dev/null +++ compiler-rt/test/profile/Windows/lit.local.cfg.py @@ -0,0 +1,9 @@ +def getRoot(config): + if not config.parent: + return config + return getRoot(config.parent) + +root = getRoot(config) + +if root.host_os not in ['Windows']: + config.unsupported = True