Introduce llvm::sys::Process::getProcessId() and adopt it

Differential Revision: https://reviews.llvm.org/D78022
This commit is contained in:
Sergej Jaskiewicz 2020-04-13 14:26:35 +03:00
parent 65a2de7e6c
commit 5cef31074f
7 changed files with 35 additions and 25 deletions

View File

@ -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

View File

@ -34,9 +34,8 @@
#include <mutex>
#include <sys/mman.h> // mmap()
#include <sys/types.h> // getpid()
#include <time.h> // clock_gettime(), time(), localtime_r() */
#include <unistd.h> // for getpid(), read(), close()
#include <unistd.h> // 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";

View File

@ -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 <unistd.h>
#elif defined(_WIN32)
#include <windows.h>
#endif
using namespace llvm;
static sys::SmartMutex<true> 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();

View File

@ -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 <cerrno>
@ -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()) {

View File

@ -66,6 +66,12 @@ static std::pair<std::chrono::microseconds, std::chrono::microseconds> 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<unsigned> Process::getPageSize() {

View File

@ -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.

View File

@ -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();