From: Sergej Jaskiewicz Date: Mon, 13 Apr 2020 11:26:35 +0000 (+0300) Subject: Introduce llvm::sys::Process::getProcessId() and adopt it X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=5cef31074ff5ff63a38e0142783849987c598ef8;p=platform%2Fupstream%2Fllvm.git Introduce llvm::sys::Process::getProcessId() and adopt it Differential Revision: https://reviews.llvm.org/D78022 --- diff --git a/llvm/include/llvm/Support/Process.h b/llvm/include/llvm/Support/Process.h index bb5c33d..0ba6d58 100644 --- a/llvm/include/llvm/Support/Process.h +++ b/llvm/include/llvm/Support/Process.h @@ -42,6 +42,11 @@ namespace sys { /// current executing process. class Process { public: + using Pid = int32_t; + + /// Get the process's identifier. + static Pid getProcessId(); + /// Get the process's page size. /// This may fail if the underlying syscall returns an error. In most cases, /// page size information is used for optimization, and this error can be diff --git a/llvm/lib/ExecutionEngine/PerfJITEvents/PerfJITEventListener.cpp b/llvm/lib/ExecutionEngine/PerfJITEvents/PerfJITEventListener.cpp index ba9e747..d4c715c 100644 --- a/llvm/lib/ExecutionEngine/PerfJITEvents/PerfJITEventListener.cpp +++ b/llvm/lib/ExecutionEngine/PerfJITEvents/PerfJITEventListener.cpp @@ -34,9 +34,8 @@ #include #include // mmap() -#include // getpid() #include // clock_gettime(), time(), localtime_r() */ -#include // for getpid(), read(), close() +#include // for read(), close() using namespace llvm; using namespace llvm::object; @@ -81,7 +80,7 @@ private: void NotifyDebug(uint64_t CodeAddr, DILineInfoTable Lines); // cache lookups - pid_t Pid; + sys::Process::Pid Pid; // base directory for output data std::string JitPath; @@ -177,7 +176,8 @@ static inline uint64_t perf_get_timestamp(void) { return timespec_to_ns(&ts); } -PerfJITEventListener::PerfJITEventListener() : Pid(::getpid()) { +PerfJITEventListener::PerfJITEventListener() + : Pid(sys::Process::getProcessId()) { // check if clock-source is supported if (!perf_get_timestamp()) { errs() << "kernel does not support CLOCK_MONOTONIC\n"; diff --git a/llvm/lib/Support/CodeGenCoverage.cpp b/llvm/lib/Support/CodeGenCoverage.cpp index 2db4193..93f386b 100644 --- a/llvm/lib/Support/CodeGenCoverage.cpp +++ b/llvm/lib/Support/CodeGenCoverage.cpp @@ -11,20 +11,14 @@ #include "llvm/Support/CodeGenCoverage.h" -#include "llvm/Config/llvm-config.h" #include "llvm/Support/Endian.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Mutex.h" +#include "llvm/Support/Process.h" #include "llvm/Support/ScopedPrinter.h" #include "llvm/Support/ToolOutputFile.h" -#if LLVM_ON_UNIX -#include -#elif defined(_WIN32) -#include -#endif - using namespace llvm; static sys::SmartMutex OutputMutex; @@ -89,14 +83,7 @@ bool CodeGenCoverage::emit(StringRef CoveragePrefix, // We can handle locking within a process easily enough but we don't want to // manage it between multiple processes. Use the process ID to ensure no // more than one process is ever writing to the same file at the same time. - std::string Pid = -#if LLVM_ON_UNIX - llvm::to_string(::getpid()); -#elif defined(_WIN32) - llvm::to_string(::GetCurrentProcessId()); -#else - ""; -#endif + std::string Pid = llvm::to_string(sys::Process::getProcessId()); std::string CoverageFilename = (CoveragePrefix + Pid).str(); diff --git a/llvm/lib/Support/LockFileManager.cpp b/llvm/lib/Support/LockFileManager.cpp index 88489a6..a2b56ab 100644 --- a/llvm/lib/Support/LockFileManager.cpp +++ b/llvm/lib/Support/LockFileManager.cpp @@ -14,6 +14,7 @@ #include "llvm/Support/ErrorOr.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/Process.h" #include "llvm/Support/Signals.h" #include "llvm/Support/raw_ostream.h" #include @@ -195,12 +196,7 @@ LockFileManager::LockFileManager(StringRef FileName) } raw_fd_ostream Out(UniqueLockFileID, /*shouldClose=*/true); - Out << HostID << ' '; -#if LLVM_ON_UNIX - Out << getpid(); -#else - Out << "1"; -#endif + Out << HostID << ' ' << sys::Process::getProcessId(); Out.close(); if (Out.has_error()) { diff --git a/llvm/lib/Support/Unix/Process.inc b/llvm/lib/Support/Unix/Process.inc index a68b30a..24f16b5 100644 --- a/llvm/lib/Support/Unix/Process.inc +++ b/llvm/lib/Support/Unix/Process.inc @@ -66,6 +66,12 @@ static std::pair getRUsage #endif } +Process::Pid Process::getProcessId() { + static_assert(sizeof(Pid) >= sizeof(pid_t), + "Process::Pid should be big enough to store pid_t"); + return Pid(::getpid()); +} + // On Cygwin, getpagesize() returns 64k(AllocationGranularity) and // offset in mmap(3) should be aligned to the AllocationGranularity. Expected Process::getPageSize() { diff --git a/llvm/lib/Support/Windows/Process.inc b/llvm/lib/Support/Windows/Process.inc index 6eb4a5eb..8064d4e 100644 --- a/llvm/lib/Support/Windows/Process.inc +++ b/llvm/lib/Support/Windows/Process.inc @@ -43,6 +43,12 @@ using namespace llvm; +Process::Pid Process::getProcessId() { + static_assert(sizeof(Pid) >= sizeof(DWORD), + "Process::Pid should be big enough to store DWORD"); + return Pid(::GetCurrentProcessId()); +} + // This function retrieves the page size using GetNativeSystemInfo() and is // present solely so it can be called once to initialize the self_process member // below. diff --git a/llvm/unittests/Support/ProcessTest.cpp b/llvm/unittests/Support/ProcessTest.cpp index 83be3a9..86208d4 100644 --- a/llvm/unittests/Support/ProcessTest.cpp +++ b/llvm/unittests/Support/ProcessTest.cpp @@ -21,6 +21,16 @@ namespace { using namespace llvm; using namespace sys; +TEST(ProcessTest, GetProcessIdTest) { + const Process::Pid pid = Process::getProcessId(); + +#ifdef _WIN32 + EXPECT_EQ(pid, ::GetCurrentProcessId()); +#else + EXPECT_EQ(pid, ::getpid()); +#endif +} + TEST(ProcessTest, GetRandomNumberTest) { const unsigned r1 = Process::GetRandomNumber(); const unsigned r2 = Process::GetRandomNumber();