Skip to content

Commit 5cef310

Browse files
committed
Introduce llvm::sys::Process::getProcessId() and adopt it
Differential Revision: https://reviews.llvm.org/D78022
1 parent 65a2de7 commit 5cef310

File tree

7 files changed

+35
-25
lines changed

7 files changed

+35
-25
lines changed

llvm/include/llvm/Support/Process.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ namespace sys {
4242
/// current executing process.
4343
class Process {
4444
public:
45+
using Pid = int32_t;
46+
47+
/// Get the process's identifier.
48+
static Pid getProcessId();
49+
4550
/// Get the process's page size.
4651
/// This may fail if the underlying syscall returns an error. In most cases,
4752
/// page size information is used for optimization, and this error can be

llvm/lib/ExecutionEngine/PerfJITEvents/PerfJITEventListener.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@
3434
#include <mutex>
3535

3636
#include <sys/mman.h> // mmap()
37-
#include <sys/types.h> // getpid()
3837
#include <time.h> // clock_gettime(), time(), localtime_r() */
39-
#include <unistd.h> // for getpid(), read(), close()
38+
#include <unistd.h> // for read(), close()
4039

4140
using namespace llvm;
4241
using namespace llvm::object;
@@ -81,7 +80,7 @@ class PerfJITEventListener : public JITEventListener {
8180
void NotifyDebug(uint64_t CodeAddr, DILineInfoTable Lines);
8281

8382
// cache lookups
84-
pid_t Pid;
83+
sys::Process::Pid Pid;
8584

8685
// base directory for output data
8786
std::string JitPath;
@@ -177,7 +176,8 @@ static inline uint64_t perf_get_timestamp(void) {
177176
return timespec_to_ns(&ts);
178177
}
179178

180-
PerfJITEventListener::PerfJITEventListener() : Pid(::getpid()) {
179+
PerfJITEventListener::PerfJITEventListener()
180+
: Pid(sys::Process::getProcessId()) {
181181
// check if clock-source is supported
182182
if (!perf_get_timestamp()) {
183183
errs() << "kernel does not support CLOCK_MONOTONIC\n";

llvm/lib/Support/CodeGenCoverage.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,14 @@
1111

1212
#include "llvm/Support/CodeGenCoverage.h"
1313

14-
#include "llvm/Config/llvm-config.h"
1514
#include "llvm/Support/Endian.h"
1615
#include "llvm/Support/FileSystem.h"
1716
#include "llvm/Support/MemoryBuffer.h"
1817
#include "llvm/Support/Mutex.h"
18+
#include "llvm/Support/Process.h"
1919
#include "llvm/Support/ScopedPrinter.h"
2020
#include "llvm/Support/ToolOutputFile.h"
2121

22-
#if LLVM_ON_UNIX
23-
#include <unistd.h>
24-
#elif defined(_WIN32)
25-
#include <windows.h>
26-
#endif
27-
2822
using namespace llvm;
2923

3024
static sys::SmartMutex<true> OutputMutex;
@@ -89,14 +83,7 @@ bool CodeGenCoverage::emit(StringRef CoveragePrefix,
8983
// We can handle locking within a process easily enough but we don't want to
9084
// manage it between multiple processes. Use the process ID to ensure no
9185
// more than one process is ever writing to the same file at the same time.
92-
std::string Pid =
93-
#if LLVM_ON_UNIX
94-
llvm::to_string(::getpid());
95-
#elif defined(_WIN32)
96-
llvm::to_string(::GetCurrentProcessId());
97-
#else
98-
"";
99-
#endif
86+
std::string Pid = llvm::to_string(sys::Process::getProcessId());
10087

10188
std::string CoverageFilename = (CoveragePrefix + Pid).str();
10289

llvm/lib/Support/LockFileManager.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "llvm/Support/ErrorOr.h"
1515
#include "llvm/Support/FileSystem.h"
1616
#include "llvm/Support/MemoryBuffer.h"
17+
#include "llvm/Support/Process.h"
1718
#include "llvm/Support/Signals.h"
1819
#include "llvm/Support/raw_ostream.h"
1920
#include <cerrno>
@@ -195,12 +196,7 @@ LockFileManager::LockFileManager(StringRef FileName)
195196
}
196197

197198
raw_fd_ostream Out(UniqueLockFileID, /*shouldClose=*/true);
198-
Out << HostID << ' ';
199-
#if LLVM_ON_UNIX
200-
Out << getpid();
201-
#else
202-
Out << "1";
203-
#endif
199+
Out << HostID << ' ' << sys::Process::getProcessId();
204200
Out.close();
205201

206202
if (Out.has_error()) {

llvm/lib/Support/Unix/Process.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ static std::pair<std::chrono::microseconds, std::chrono::microseconds> getRUsage
6666
#endif
6767
}
6868

69+
Process::Pid Process::getProcessId() {
70+
static_assert(sizeof(Pid) >= sizeof(pid_t),
71+
"Process::Pid should be big enough to store pid_t");
72+
return Pid(::getpid());
73+
}
74+
6975
// On Cygwin, getpagesize() returns 64k(AllocationGranularity) and
7076
// offset in mmap(3) should be aligned to the AllocationGranularity.
7177
Expected<unsigned> Process::getPageSize() {

llvm/lib/Support/Windows/Process.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343

4444
using namespace llvm;
4545

46+
Process::Pid Process::getProcessId() {
47+
static_assert(sizeof(Pid) >= sizeof(DWORD),
48+
"Process::Pid should be big enough to store DWORD");
49+
return Pid(::GetCurrentProcessId());
50+
}
51+
4652
// This function retrieves the page size using GetNativeSystemInfo() and is
4753
// present solely so it can be called once to initialize the self_process member
4854
// below.

llvm/unittests/Support/ProcessTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ namespace {
2121
using namespace llvm;
2222
using namespace sys;
2323

24+
TEST(ProcessTest, GetProcessIdTest) {
25+
const Process::Pid pid = Process::getProcessId();
26+
27+
#ifdef _WIN32
28+
EXPECT_EQ(pid, ::GetCurrentProcessId());
29+
#else
30+
EXPECT_EQ(pid, ::getpid());
31+
#endif
32+
}
33+
2434
TEST(ProcessTest, GetRandomNumberTest) {
2535
const unsigned r1 = Process::GetRandomNumber();
2636
const unsigned r2 = Process::GetRandomNumber();

0 commit comments

Comments
 (0)