aboutsummaryrefslogtreecommitdiff
path: root/lib/profile/GCDAProfiling.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/profile/GCDAProfiling.c')
-rw-r--r--lib/profile/GCDAProfiling.c35
1 files changed, 10 insertions, 25 deletions
diff --git a/lib/profile/GCDAProfiling.c b/lib/profile/GCDAProfiling.c
index 45fbd07e544b..aec232856e74 100644
--- a/lib/profile/GCDAProfiling.c
+++ b/lib/profile/GCDAProfiling.c
@@ -20,23 +20,18 @@
|*
\*===----------------------------------------------------------------------===*/
+#include "InstrProfilingUtil.h"
+
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
-#ifdef _WIN32
-#include <direct.h>
-#endif
+#include <sys/file.h>
#define I386_FREEBSD (defined(__FreeBSD__) && defined(__i386__))
-#if !I386_FREEBSD
-#include <sys/stat.h>
-#include <sys/types.h>
-#endif
-
#if !defined(_MSC_VER) && !I386_FREEBSD
#include <stdint.h>
#endif
@@ -51,7 +46,6 @@ typedef unsigned long long uint64_t;
typedef unsigned char uint8_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
-int mkdir(const char*, unsigned short);
#endif
/* #define DEBUG_GCDAPROFILING */
@@ -208,21 +202,6 @@ static char *mangle_filename(const char *orig_filename) {
return new_filename;
}
-static void recursive_mkdir(char *path) {
- int i;
-
- for (i = 1; path[i] != '\0'; ++i) {
- if (path[i] != '/') continue;
- path[i] = '\0';
-#ifdef _WIN32
- _mkdir(path);
-#else
- mkdir(path, 0755); /* Some of these will fail, ignore it. */
-#endif
- path[i] = '/';
- }
-}
-
static int map_file() {
fseek(output_file, 0L, SEEK_END);
file_size = ftell(output_file);
@@ -282,7 +261,7 @@ void llvm_gcda_start_file(const char *orig_filename, const char version[4],
fd = open(filename, O_RDWR | O_CREAT, 0644);
if (fd == -1) {
/* Try creating the directories first then opening the file. */
- recursive_mkdir(filename);
+ __llvm_profile_recursive_mkdir(filename);
fd = open(filename, O_RDWR | O_CREAT, 0644);
if (fd == -1) {
/* Bah! It's hopeless. */
@@ -294,6 +273,11 @@ void llvm_gcda_start_file(const char *orig_filename, const char version[4],
}
}
+ /* Try to flock the file to serialize concurrent processes writing out to the
+ * same GCDA. This can fail if the filesystem doesn't support it, but in that
+ * case we'll just carry on with the old racy behaviour and hope for the best.
+ */
+ flock(fd, LOCK_EX);
output_file = fdopen(fd, mode);
/* Initialize the write buffer. */
@@ -493,6 +477,7 @@ void llvm_gcda_end_file() {
}
fclose(output_file);
+ flock(fd, LOCK_UN);
output_file = NULL;
write_buffer = NULL;
}