/// 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
#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;
void NotifyDebug(uint64_t CodeAddr, DILineInfoTable Lines);
// cache lookups
- pid_t Pid;
+ sys::Process::Pid Pid;
// base directory for output data
std::string JitPath;
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";
#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;
// 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();
#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>
}
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()) {
#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() {
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.
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();