2015-07-10 01:21:52 +08:00
|
|
|
/*===- InstrProfilingUtil.c - Support library for PGO instrumentation -----===*\
|
|
|
|
|*
|
|
|
|
|* The LLVM Compiler Infrastructure
|
|
|
|
|*
|
|
|
|
|* This file is distributed under the University of Illinois Open Source
|
|
|
|
|* License. See LICENSE.TXT for details.
|
|
|
|
|*
|
|
|
|
\*===----------------------------------------------------------------------===*/
|
|
|
|
|
|
|
|
#include "InstrProfilingUtil.h"
|
2015-11-23 12:38:17 +08:00
|
|
|
#include "InstrProfiling.h"
|
2015-07-10 01:21:52 +08:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <direct.h>
|
2016-06-06 11:17:58 +08:00
|
|
|
#include <windows.h>
|
2015-07-10 01:21:52 +08:00
|
|
|
#else
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2016-06-06 11:17:58 +08:00
|
|
|
#if defined(__linux__)
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2015-07-10 01:21:52 +08:00
|
|
|
#endif
|
|
|
|
|
2016-03-06 12:52:45 +08:00
|
|
|
#ifdef COMPILER_RT_HAS_UNAME
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-12-16 11:29:15 +08:00
|
|
|
COMPILER_RT_VISIBILITY
|
2015-07-10 01:21:52 +08:00
|
|
|
void __llvm_profile_recursive_mkdir(char *path) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 1; path[i] != '\0'; ++i) {
|
2016-03-29 05:32:46 +08:00
|
|
|
char save = path[i];
|
|
|
|
if (!(path[i] == '/' || path[i] == '\\'))
|
|
|
|
continue;
|
2015-07-10 01:21:52 +08:00
|
|
|
path[i] = '\0';
|
|
|
|
#ifdef _WIN32
|
|
|
|
_mkdir(path);
|
|
|
|
#else
|
2016-06-06 11:17:58 +08:00
|
|
|
mkdir(path, 0755); /* Some of these will fail, ignore it. */
|
2015-07-10 01:21:52 +08:00
|
|
|
#endif
|
2016-03-29 05:32:46 +08:00
|
|
|
path[i] = save;
|
2015-07-10 01:21:52 +08:00
|
|
|
}
|
|
|
|
}
|
2016-03-06 08:55:20 +08:00
|
|
|
|
|
|
|
#if COMPILER_RT_HAS_ATOMICS != 1
|
|
|
|
COMPILER_RT_VISIBILITY
|
2016-03-06 12:18:13 +08:00
|
|
|
uint32_t lprofBoolCmpXchg(void **Ptr, void *OldV, void *NewV) {
|
2016-03-06 08:55:20 +08:00
|
|
|
void *R = *Ptr;
|
|
|
|
if (R == OldV) {
|
|
|
|
*Ptr = NewV;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2016-05-17 07:01:03 +08:00
|
|
|
COMPILER_RT_VISIBILITY
|
|
|
|
void *lprofPtrFetchAdd(void **Mem, long ByteIncr) {
|
|
|
|
void *Old = *Mem;
|
|
|
|
*((char **)Mem) += ByteIncr;
|
|
|
|
return Old;
|
|
|
|
}
|
|
|
|
|
2016-03-06 08:55:20 +08:00
|
|
|
#endif
|
|
|
|
|
2016-03-06 12:52:45 +08:00
|
|
|
#ifdef COMPILER_RT_HAS_UNAME
|
|
|
|
int lprofGetHostName(char *Name, int Len) {
|
|
|
|
struct utsname N;
|
|
|
|
int R;
|
|
|
|
if (!(R = uname(&N)))
|
|
|
|
strncpy(Name, N.nodename, Len);
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-06-06 11:17:58 +08:00
|
|
|
FILE *lprofOpenFileEx(const char *ProfileName) {
|
|
|
|
FILE *f;
|
|
|
|
int fd;
|
|
|
|
#ifdef COMPILER_RT_HAS_FCNTL_LCK
|
|
|
|
struct flock s_flock;
|
|
|
|
|
|
|
|
s_flock.l_whence = SEEK_SET;
|
|
|
|
s_flock.l_start = 0;
|
|
|
|
s_flock.l_len = 0; /* Until EOF. */
|
|
|
|
s_flock.l_pid = getpid();
|
|
|
|
|
|
|
|
s_flock.l_type = F_WRLCK;
|
|
|
|
fd = open(ProfileName, O_RDWR | O_CREAT, 0666);
|
|
|
|
if (fd < 0)
|
2016-06-07 02:31:29 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while (fcntl(fd, F_SETLKW, &s_flock) == -1) {
|
|
|
|
if (errno != EINTR) {
|
|
|
|
if (errno == ENOLCK) {
|
|
|
|
PROF_WARN("Data may be corrupted during profile merging : %s\n",
|
|
|
|
"Fail to obtain file lock due to system limit.");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-06-06 11:17:58 +08:00
|
|
|
|
|
|
|
f = fdopen(fd, "r+b");
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
HANDLE h = CreateFile(ProfileName, GENERIC_READ | GENERIC_WRITE, 0, 0,
|
|
|
|
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
|
|
|
|
if (h == INVALID_HANDLE_VALUE)
|
2016-06-07 02:31:29 +08:00
|
|
|
return NULL;
|
2016-06-06 11:17:58 +08:00
|
|
|
|
|
|
|
fd = _open_osfhandle((intptr_t)h, 0);
|
|
|
|
if (fd == -1) {
|
|
|
|
CloseHandle(h);
|
2016-06-07 02:31:29 +08:00
|
|
|
return NULL;
|
2016-06-06 11:17:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
f = _fdopen(fd, "r+b");
|
|
|
|
if (f == 0) {
|
|
|
|
CloseHandle(h);
|
2016-06-07 02:31:29 +08:00
|
|
|
return NULL;
|
2016-06-06 11:17:58 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* Worst case no locking applied. */
|
2016-06-07 02:31:29 +08:00
|
|
|
PROF_WARN("Concurrent file access is not supported : %s\n",
|
|
|
|
"lack file locking");
|
2016-06-06 11:17:58 +08:00
|
|
|
fd = open(ProfileName, O_RDWR | O_CREAT, 0666);
|
|
|
|
if (fd < 0)
|
2016-06-07 02:31:29 +08:00
|
|
|
return NULL;
|
2016-06-06 11:17:58 +08:00
|
|
|
f = fdopen(fd, "r+b");
|
|
|
|
#endif
|
2016-03-06 12:52:45 +08:00
|
|
|
|
2016-06-06 11:17:58 +08:00
|
|
|
return f;
|
|
|
|
}
|