[compiler-rt] r320726 - [profile] Port the runtime to Solaris

Vedant Kumar via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 14 10:43:14 PST 2017


Author: vedantk
Date: Thu Dec 14 10:43:14 2017
New Revision: 320726

URL: http://llvm.org/viewvc/llvm-project?rev=320726&view=rev
Log:
[profile] Port the runtime to Solaris

This includes a few nice bits of refactoring (e.g splitting out the
exclusive locking code into a common utility).

Patch by Rainer Orth!

Differential Revision: https://reviews.llvm.org/D40944

Modified:
    compiler-rt/trunk/cmake/config-ix.cmake
    compiler-rt/trunk/lib/profile/CMakeLists.txt
    compiler-rt/trunk/lib/profile/GCDAProfiling.c
    compiler-rt/trunk/lib/profile/InstrProfiling.c
    compiler-rt/trunk/lib/profile/InstrProfilingFile.c
    compiler-rt/trunk/lib/profile/InstrProfilingInternal.h
    compiler-rt/trunk/lib/profile/InstrProfilingPlatformLinux.c
    compiler-rt/trunk/lib/profile/InstrProfilingPlatformOther.c
    compiler-rt/trunk/lib/profile/InstrProfilingPort.h
    compiler-rt/trunk/lib/profile/InstrProfilingUtil.c
    compiler-rt/trunk/lib/profile/InstrProfilingUtil.h
    compiler-rt/trunk/lib/profile/InstrProfilingValue.c
    compiler-rt/trunk/lib/profile/InstrProfilingWriter.c
    compiler-rt/trunk/lib/profile/WindowsMMap.c
    compiler-rt/trunk/test/profile/lit.cfg

Modified: compiler-rt/trunk/cmake/config-ix.cmake
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/cmake/config-ix.cmake?rev=320726&r1=320725&r2=320726&view=diff
==============================================================================
--- compiler-rt/trunk/cmake/config-ix.cmake (original)
+++ compiler-rt/trunk/cmake/config-ix.cmake Thu Dec 14 10:43:14 2017
@@ -542,7 +542,7 @@ else()
 endif()
 
 if (PROFILE_SUPPORTED_ARCH AND NOT LLVM_USE_SANITIZER AND
-    OS_NAME MATCHES "Darwin|Linux|FreeBSD|Windows|Android")
+    OS_NAME MATCHES "Darwin|Linux|FreeBSD|Windows|Android|SunOS")
   set(COMPILER_RT_HAS_PROFILE TRUE)
 else()
   set(COMPILER_RT_HAS_PROFILE FALSE)

Modified: compiler-rt/trunk/lib/profile/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/CMakeLists.txt?rev=320726&r1=320725&r2=320726&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/CMakeLists.txt (original)
+++ compiler-rt/trunk/lib/profile/CMakeLists.txt Thu Dec 14 10:43:14 2017
@@ -38,6 +38,14 @@ int main() {
 
 " COMPILER_RT_TARGET_HAS_FCNTL_LCK)
 
+CHECK_CXX_SOURCE_COMPILES("
+#include <sys/utsname.h>
+int main() {
+ return 0;
+}
+
+" COMPILER_RT_TARGET_HAS_UNAME)
+
 add_compiler_rt_component(profile)
 
 set(PROFILE_SOURCES
@@ -78,6 +86,12 @@ if(COMPILER_RT_TARGET_HAS_FCNTL_LCK)
      -DCOMPILER_RT_HAS_FCNTL_LCK=1)
 endif()
 
+if(COMPILER_RT_TARGET_HAS_UNAME)
+ set(EXTRA_FLAGS
+     ${EXTRA_FLAGS}
+     -DCOMPILER_RT_HAS_UNAME=1)
+endif()
+
 # This appears to be a C-only warning banning the use of locals in aggregate
 # initializers. All other compilers accept this, though.
 # nonstandard extension used : 'identifier' : cannot be initialized using address of automatic variable

Modified: compiler-rt/trunk/lib/profile/GCDAProfiling.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/GCDAProfiling.c?rev=320726&r1=320725&r2=320726&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/GCDAProfiling.c (original)
+++ compiler-rt/trunk/lib/profile/GCDAProfiling.c Thu Dec 14 10:43:14 2017
@@ -20,9 +20,6 @@
 |*
 \*===----------------------------------------------------------------------===*/
 
-#include "InstrProfilingPort.h"
-#include "InstrProfilingUtil.h"
-
 #include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
@@ -34,12 +31,6 @@
 #else
 #include <sys/mman.h>
 #include <sys/file.h>
-#ifndef MAP_FILE
-#define MAP_FILE 0
-#endif
-#ifndef O_BINARY
-#define O_BINARY 0
-#endif
 #endif
 
 #if defined(__FreeBSD__) && defined(__i386__)
@@ -65,6 +56,9 @@ typedef unsigned int uint32_t;
 typedef unsigned long long uint64_t;
 #endif
 
+#include "InstrProfiling.h"
+#include "InstrProfilingUtil.h"
+
 /* #define DEBUG_GCDAPROFILING */
 
 /*
@@ -266,7 +260,7 @@ void llvm_gcda_start_file(const char *or
    * 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);
+  lprofLockFd(fd);
   output_file = fdopen(fd, mode);
 
   /* Initialize the write buffer. */
@@ -465,7 +459,7 @@ void llvm_gcda_end_file() {
       unmap_file();
     }
 
-    flock(fd, LOCK_UN);
+    lprofUnlockFd(fd);
     fclose(output_file);
     output_file = NULL;
     write_buffer = NULL;

Modified: compiler-rt/trunk/lib/profile/InstrProfiling.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfiling.c?rev=320726&r1=320725&r2=320726&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfiling.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfiling.c Thu Dec 14 10:43:14 2017
@@ -7,12 +7,14 @@
 |*
 \*===----------------------------------------------------------------------===*/
 
-#include "InstrProfiling.h"
-#include "InstrProfilingInternal.h"
 #include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+
+#include "InstrProfiling.h"
+#include "InstrProfilingInternal.h"
+
 #define INSTR_PROF_VALUE_PROF_DATA
 #include "InstrProfData.inc"
 

Modified: compiler-rt/trunk/lib/profile/InstrProfilingFile.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingFile.c?rev=320726&r1=320725&r2=320726&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingFile.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingFile.c Thu Dec 14 10:43:14 2017
@@ -7,9 +7,6 @@
 |*
 \*===----------------------------------------------------------------------===*/
 
-#include "InstrProfiling.h"
-#include "InstrProfilingInternal.h"
-#include "InstrProfilingUtil.h"
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -31,6 +28,10 @@
 #endif
 #endif
 
+#include "InstrProfiling.h"
+#include "InstrProfilingInternal.h"
+#include "InstrProfilingUtil.h"
+
 /* From where is profile name specified.
  * The order the enumerators define their
  * precedence. Re-order them may lead to
@@ -85,7 +86,6 @@ typedef struct lprofFilename {
 COMPILER_RT_WEAK lprofFilename lprofCurFilename = {0, 0, 0, {0}, {0},
                                                    0, 0, 0, PNS_unknown};
 
-int getpid(void);
 static int getCurFilenameLength();
 static const char *getCurFilename(char *FilenameBuf);
 static unsigned doMerging() { return lprofCurFilename.MergePoolSize; }
@@ -325,7 +325,7 @@ static int parseFilenamePattern(const ch
     if (FilenamePat[I] == '%') {
       if (FilenamePat[++I] == 'p') {
         if (!NumPids++) {
-          if (snprintf(PidChars, MAX_PID_SIZE, "%d", getpid()) <= 0) {
+          if (snprintf(PidChars, MAX_PID_SIZE, "%ld", (long)getpid()) <= 0) {
             PROF_WARN("Unable to get pid for filename pattern %s. Using the "
                       "default name.",
                       FilenamePat);

Modified: compiler-rt/trunk/lib/profile/InstrProfilingInternal.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingInternal.h?rev=320726&r1=320725&r2=320726&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingInternal.h (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingInternal.h Thu Dec 14 10:43:14 2017
@@ -10,8 +10,9 @@
 #ifndef PROFILE_INSTRPROFILING_INTERNALH_
 #define PROFILE_INSTRPROFILING_INTERNALH_
 
+#include <stddef.h>
+
 #include "InstrProfiling.h"
-#include "stddef.h"
 
 /*!
  * \brief Write instrumentation data to the given buffer, given explicit

Modified: compiler-rt/trunk/lib/profile/InstrProfilingPlatformLinux.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingPlatformLinux.c?rev=320726&r1=320725&r2=320726&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingPlatformLinux.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingPlatformLinux.c Thu Dec 14 10:43:14 2017
@@ -7,11 +7,13 @@
 |*
 \*===----------------------------------------------------------------------===*/
 
-#include "InstrProfiling.h"
+#if defined(__linux__) || defined(__FreeBSD__) || \
+    (defined(__sun__) && defined(__svr4__))
 
-#if defined(__linux__) || defined(__FreeBSD__)
 #include <stdlib.h>
 
+#include "InstrProfiling.h"
+
 #define PROF_DATA_START INSTR_PROF_SECT_START(INSTR_PROF_DATA_SECT_NAME)
 #define PROF_DATA_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_DATA_SECT_NAME)
 #define PROF_NAME_START INSTR_PROF_SECT_START(INSTR_PROF_NAME_SECT_NAME)

Modified: compiler-rt/trunk/lib/profile/InstrProfilingPlatformOther.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingPlatformOther.c?rev=320726&r1=320725&r2=320726&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingPlatformOther.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingPlatformOther.c Thu Dec 14 10:43:14 2017
@@ -7,12 +7,13 @@
 |*
 \*===----------------------------------------------------------------------===*/
 
-#include "InstrProfiling.h"
-
-#if !defined(__APPLE__) && !defined(__linux__) && !defined(__FreeBSD__)
+#if !defined(__APPLE__) && !defined(__linux__) && !defined(__FreeBSD__) && \
+    !(defined(__sun__) && defined(__svr4__))
 
 #include <stdlib.h>
 
+#include "InstrProfiling.h"
+
 static const __llvm_profile_data *DataFirst = NULL;
 static const __llvm_profile_data *DataLast = NULL;
 static const char *NamesFirst = NULL;

Modified: compiler-rt/trunk/lib/profile/InstrProfilingPort.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingPort.h?rev=320726&r1=320725&r2=320726&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingPort.h (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingPort.h Thu Dec 14 10:43:14 2017
@@ -7,6 +7,9 @@
 |*
 \*===----------------------------------------------------------------------===*/
 
+/* This header must be included after all others so it can provide fallback
+   definitions for stuff missing in system headers. */
+
 #ifndef PROFILE_INSTRPROFILING_PORT_H_
 #define PROFILE_INSTRPROFILING_PORT_H_
 
@@ -44,9 +47,6 @@
 #define COMPILER_RT_GETHOSTNAME(Name, Len) ((void)(Name), (void)(Len), (-1))
 #else
 #define COMPILER_RT_GETHOSTNAME(Name, Len) lprofGetHostName(Name, Len)
-#ifndef _MSC_VER
-#define COMPILER_RT_HAS_UNAME 1
-#endif
 #endif
 
 #if COMPILER_RT_HAS_ATOMICS == 1
@@ -107,6 +107,14 @@
 #define PROF_NOTE(Format, ...)                                                 \
   fprintf(stderr, "LLVM Profile Note: " Format, __VA_ARGS__);
 
+#ifndef MAP_FILE
+#define MAP_FILE 0
+#endif
+
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
 #if defined(__FreeBSD__)
 
 #include <inttypes.h>

Modified: compiler-rt/trunk/lib/profile/InstrProfilingUtil.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingUtil.c?rev=320726&r1=320725&r2=320726&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingUtil.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingUtil.c Thu Dec 14 10:43:14 2017
@@ -7,9 +7,6 @@
 |*
 \*===----------------------------------------------------------------------===*/
 
-#include "InstrProfilingUtil.h"
-#include "InstrProfiling.h"
-
 #ifdef _WIN32
 #include <direct.h>
 #include <io.h>
@@ -34,6 +31,9 @@
 #include <sys/prctl.h>
 #endif
 
+#include "InstrProfiling.h"
+#include "InstrProfilingUtil.h"
+
 COMPILER_RT_VISIBILITY
 void __llvm_profile_recursive_mkdir(char *path) {
   int i;
@@ -86,16 +86,16 @@ COMPILER_RT_VISIBILITY int lprofGetHostN
 #elif defined(COMPILER_RT_HAS_UNAME)
 COMPILER_RT_VISIBILITY int lprofGetHostName(char *Name, int Len) {
   struct utsname N;
-  int R;
-  if (!(R = uname(&N)))
+  int R = uname(&N);
+  if (R >= 0) {
     strncpy(Name, N.nodename, Len);
+    return 0;
+  }
   return R;
 }
 #endif
 
-COMPILER_RT_VISIBILITY FILE *lprofOpenFileEx(const char *ProfileName) {
-  FILE *f;
-  int fd;
+COMPILER_RT_VISIBILITY int lprofLockFd(int fd) {
 #ifdef COMPILER_RT_HAS_FCNTL_LCK
   struct flock s_flock;
 
@@ -103,21 +103,59 @@ COMPILER_RT_VISIBILITY FILE *lprofOpenFi
   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)
-    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.");
+        return -1;
       }
       break;
     }
   }
+  return 0;
+#else
+  flock(fd, LOCK_EX);
+  return 0;
+#endif
+}
+
+COMPILER_RT_VISIBILITY int lprofUnlockFd(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_UNLCK;
+
+  while (fcntl(fd, F_SETLKW, &s_flock) == -1) {
+    if (errno != EINTR) {
+      if (errno == ENOLCK) {
+        return -1;
+      }
+      break;
+    }
+  }
+  return 0;
+#else
+  flock(fd, LOCK_UN);
+  return 0;
+#endif
+}
+
+COMPILER_RT_VISIBILITY FILE *lprofOpenFileEx(const char *ProfileName) {
+  FILE *f;
+  int fd;
+#ifdef COMPILER_RT_HAS_FCNTL_LCK
+  fd = open(ProfileName, O_RDWR | O_CREAT, 0666);
+  if (fd < 0)
+    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");
 #elif defined(_WIN32)

Modified: compiler-rt/trunk/lib/profile/InstrProfilingUtil.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingUtil.h?rev=320726&r1=320725&r2=320726&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingUtil.h (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingUtil.h Thu Dec 14 10:43:14 2017
@@ -16,6 +16,9 @@
 /*! \brief Create a directory tree. */
 void __llvm_profile_recursive_mkdir(char *Pathname);
 
+int lprofLockFd(int fd);
+int lprofUnlockFd(int fd);
+
 /*! Open file \c Filename for read+write with write
  * lock for exclusive access. The caller will block
  * if the lock is already held by another process. */

Modified: compiler-rt/trunk/lib/profile/InstrProfilingValue.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingValue.c?rev=320726&r1=320725&r2=320726&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingValue.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingValue.c Thu Dec 14 10:43:14 2017
@@ -7,13 +7,15 @@
 |*
 \*===----------------------------------------------------------------------===*/
 
-#include "InstrProfiling.h"
-#include "InstrProfilingInternal.h"
-#include "InstrProfilingUtil.h" /* For PS4 getenv shim. */
 #include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+
+#include "InstrProfiling.h"
+#include "InstrProfilingInternal.h"
+#include "InstrProfilingUtil.h"
+
 #define INSTR_PROF_VALUE_PROF_DATA
 #define INSTR_PROF_COMMON_API_IMPL
 #include "InstrProfData.inc"

Modified: compiler-rt/trunk/lib/profile/InstrProfilingWriter.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingWriter.c?rev=320726&r1=320725&r2=320726&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingWriter.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingWriter.c Thu Dec 14 10:43:14 2017
@@ -7,14 +7,15 @@
 |*
 \*===----------------------------------------------------------------------===*/
 
-#include "InstrProfiling.h"
-#include "InstrProfilingInternal.h"
 #ifdef _MSC_VER
 /* For _alloca */
 #include <malloc.h>
 #endif
 #include <string.h>
 
+#include "InstrProfiling.h"
+#include "InstrProfilingInternal.h"
+
 #define INSTR_PROF_VALUE_PROF_DATA
 #include "InstrProfData.inc"
 

Modified: compiler-rt/trunk/lib/profile/WindowsMMap.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/WindowsMMap.c?rev=320726&r1=320725&r2=320726&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/WindowsMMap.c (original)
+++ compiler-rt/trunk/lib/profile/WindowsMMap.c Thu Dec 14 10:43:14 2017
@@ -18,11 +18,12 @@
 #if defined(_WIN32)
 
 #include "WindowsMMap.h"
-#include "InstrProfiling.h"
 
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 
+#include "InstrProfiling.h"
+
 #ifdef __USE_FILE_OFFSET64
 # define DWORD_HI(x) (x >> 32)
 # define DWORD_LO(x) ((x) & 0xffffffff)

Modified: compiler-rt/trunk/test/profile/lit.cfg
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/profile/lit.cfg?rev=320726&r1=320725&r2=320726&view=diff
==============================================================================
--- compiler-rt/trunk/test/profile/lit.cfg (original)
+++ compiler-rt/trunk/test/profile/lit.cfg Thu Dec 14 10:43:14 2017
@@ -67,7 +67,7 @@ config.substitutions.append( ("%clangxx_
 
 config.substitutions.append( ("%clang_lto_profgen=", build_invocation(clang_cflags, True) + " -fprofile-instr-generate=") )
 
-if config.host_os not in ['Darwin', 'FreeBSD', 'Linux']:
+if config.host_os not in ['Darwin', 'FreeBSD', 'Linux', 'SunOS']:
   config.unsupported = True
 
 if config.target_arch in ['armv7l']:




More information about the llvm-commits mailing list