Clean up some code in Program.

NFC here, this just raises some platform specific ifdef hackery
out of a class and creates proper platform-independent typedefs
for the relevant things.  This allows these typedefs to be
reused in other places without having to reinvent this preprocessor
logic.

llvm-svn: 334294
This commit is contained in:
Zachary Turner 2018-06-08 15:16:25 +00:00
parent 58a53155d0
commit 5f66a3a103
5 changed files with 30 additions and 33 deletions

View File

@ -32,29 +32,26 @@ namespace sys {
const char EnvPathSeparator = ';';
#endif
/// This struct encapsulates information about a process.
struct ProcessInfo {
#if defined(LLVM_ON_UNIX)
typedef pid_t ProcessId;
#elif defined(_WIN32)
typedef unsigned long ProcessId; // Must match the type of DWORD on Windows.
typedef void * HANDLE; // Must match the type of HANDLE on Windows.
/// The handle to the process (available on Windows only).
HANDLE ProcessHandle;
#if defined(_WIN32)
typedef unsigned long procid_t; // Must match the type of DWORD on Windows.
typedef void *process_t; // Must match the type of HANDLE on Windows.
#else
#error "ProcessInfo is not defined for this platform!"
typedef pid_t procid_t;
typedef procid_t process_t;
#endif
enum : ProcessId { InvalidPid = 0 };
/// This struct encapsulates information about a process.
struct ProcessInfo {
enum : procid_t { InvalidPid = 0 };
/// The process identifier.
ProcessId Pid;
procid_t Pid; /// The process identifier.
process_t Process; /// Platform-dependent process object.
/// The return code, set after execution.
int ReturnCode;
/// The return code, set after execution.
int ReturnCode;
ProcessInfo();
};
ProcessInfo();
};
/// Find the first executable file \p Name in \p Paths.
///

View File

@ -237,6 +237,7 @@ static bool Execute(ProcessInfo &PI, StringRef Program, const char **Args,
return !MakeErrMsg(ErrMsg, "posix_spawn failed", Err);
PI.Pid = PID;
PI.Process = PID;
return true;
}
@ -300,6 +301,7 @@ static bool Execute(ProcessInfo &PI, StringRef Program, const char **Args,
}
PI.Pid = child;
PI.Process = child;
return true;
}

View File

@ -31,7 +31,7 @@
namespace llvm {
ProcessInfo::ProcessInfo() : ProcessHandle(0), Pid(0), ReturnCode(0) {}
ProcessInfo::ProcessInfo() : Process(0), Pid(0), ReturnCode(0) {}
ErrorOr<std::string> sys::findProgramByName(StringRef Name,
ArrayRef<StringRef> Paths) {
@ -381,7 +381,7 @@ static bool Execute(ProcessInfo &PI, StringRef Program, const char **Args,
}
PI.Pid = pi.dwProcessId;
PI.ProcessHandle = pi.hProcess;
PI.Process = pi.hProcess;
// Make sure these get closed no matter what.
ScopedCommonHandle hThread(pi.hThread);
@ -418,7 +418,7 @@ namespace llvm {
ProcessInfo sys::Wait(const ProcessInfo &PI, unsigned SecondsToWait,
bool WaitUntilChildTerminates, std::string *ErrMsg) {
assert(PI.Pid && "invalid pid to wait on, process not started?");
assert(PI.ProcessHandle &&
assert((PI.Process && PI.Process != INVALID_HANDLE_VALUE) &&
"invalid process handle to wait on, process not started?");
DWORD milliSecondsToWait = 0;
if (WaitUntilChildTerminates)
@ -427,20 +427,20 @@ ProcessInfo sys::Wait(const ProcessInfo &PI, unsigned SecondsToWait,
milliSecondsToWait = SecondsToWait * 1000;
ProcessInfo WaitResult = PI;
DWORD WaitStatus = WaitForSingleObject(PI.ProcessHandle, milliSecondsToWait);
DWORD WaitStatus = WaitForSingleObject(PI.Process, milliSecondsToWait);
if (WaitStatus == WAIT_TIMEOUT) {
if (SecondsToWait) {
if (!TerminateProcess(PI.ProcessHandle, 1)) {
if (!TerminateProcess(PI.Process, 1)) {
if (ErrMsg)
MakeErrMsg(ErrMsg, "Failed to terminate timed-out program");
// -2 indicates a crash or timeout as opposed to failure to execute.
WaitResult.ReturnCode = -2;
CloseHandle(PI.ProcessHandle);
CloseHandle(PI.Process);
return WaitResult;
}
WaitForSingleObject(PI.ProcessHandle, INFINITE);
CloseHandle(PI.ProcessHandle);
WaitForSingleObject(PI.Process, INFINITE);
CloseHandle(PI.Process);
} else {
// Non-blocking wait.
return ProcessInfo();
@ -449,10 +449,10 @@ ProcessInfo sys::Wait(const ProcessInfo &PI, unsigned SecondsToWait,
// Get its exit status.
DWORD status;
BOOL rc = GetExitCodeProcess(PI.ProcessHandle, &status);
BOOL rc = GetExitCodeProcess(PI.Process, &status);
DWORD err = GetLastError();
if (err != ERROR_INVALID_HANDLE)
CloseHandle(PI.ProcessHandle);
CloseHandle(PI.Process);
if (!rc) {
SetLastError(err);

View File

@ -29,12 +29,11 @@ namespace xray {
class LatencyAccountant {
public:
typedef std::map<int32_t, std::vector<uint64_t>> FunctionLatencyMap;
typedef std::map<llvm::sys::ProcessInfo::ProcessId,
std::pair<uint64_t, uint64_t>>
typedef std::map<llvm::sys::procid_t, std::pair<uint64_t, uint64_t>>
PerThreadMinMaxTSCMap;
typedef std::map<uint8_t, std::pair<uint64_t, uint64_t>> PerCPUMinMaxTSCMap;
typedef std::vector<std::pair<int32_t, uint64_t>> FunctionStack;
typedef std::map<llvm::sys::ProcessInfo::ProcessId, FunctionStack>
typedef std::map<llvm::sys::procid_t, FunctionStack>
PerThreadFunctionStackMap;
private:
@ -79,8 +78,7 @@ public:
///
bool accountRecord(const XRayRecord &Record);
const FunctionStack *
getThreadFunctionStack(llvm::sys::ProcessInfo::ProcessId TId) const {
const FunctionStack *getThreadFunctionStack(llvm::sys::procid_t TId) const {
auto I = PerThreadFunctionStack.find(TId);
if (I == PerThreadFunctionStack.end())
return nullptr;

View File

@ -80,7 +80,7 @@ public:
using FunctionStack = SmallVector<FunctionAttr, 4>;
using PerThreadFunctionStackMap =
DenseMap<llvm::sys::ProcessInfo::ProcessId, FunctionStack>;
DenseMap<llvm::sys::procid_t, FunctionStack>;
class GraphT : public Graph<FunctionStats, CallStats, int32_t> {
public: