Skip to content

Enable Child Memory Usage Tracking on Windows #23686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/Basic/Statistic.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class UnifiedStatsReporter {
private:
bool currentProcessExitStatusSet;
int currentProcessExitStatus;
long maxChildRSS = 0;
SmallString<128> StatsFilename;
SmallString<128> TraceFilename;
SmallString<128> ProfileDirname;
Expand Down Expand Up @@ -192,6 +193,8 @@ class UnifiedStatsReporter {
void flushTracesAndProfiles();
void noteCurrentProcessExitStatus(int);
void saveAnyFrontendStatsEvents(FrontendStatsTracer const &T, bool IsEntry);
void recordJobMaxRSS(long rss);
int64_t getChildrenMaxResidentSetSize();
};

// This is a non-nested type just to make it less work to write at call sites.
Expand Down
1 change: 1 addition & 0 deletions include/swift/Basic/TaskQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ struct TaskProcessInformation {
#if defined(HAVE_GETRUSAGE) && !defined(__HAIKU__)
TaskProcessInformation(ProcessId Pid, struct rusage Usage);
#endif // defined(HAVE_GETRUSAGE) && !defined(__HAIKU__)
Optional<ResourceUsage> getResourceUsage() { return ProcessUsage; }
virtual ~TaskProcessInformation() = default;
virtual void provideMapping(json::Output &out);
};
Expand Down
30 changes: 28 additions & 2 deletions lib/Basic/Default/TaskQueue.inc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Signals.h"

#if defined(_WIN32)
#define NOMINMAX
#include <Windows.h>
#include <psapi.h>
#endif

using namespace llvm::sys;

namespace swift {
Expand Down Expand Up @@ -150,15 +156,35 @@ bool TaskQueue::execute(TaskBeganCallback Began, TaskFinishedCallback Finished,
// This isn't a true signal on Windows, but we'll treat it as such so that
// we clean up after it properly
bool crashed = ReturnCode & 0xC0000000;

FILETIME creationTime;
FILETIME exitTime;
FILETIME utimeTicks;
FILETIME stimeTicks;
PROCESS_MEMORY_COUNTERS counters = {};
GetProcessTimes(PI.Process, &creationTime, &exitTime, &stimeTicks,
&utimeTicks);
// Each tick is 100ns
uint64_t utime =
((uint64_t)utimeTicks.dwHighDateTime << 32 | utimeTicks.dwLowDateTime) /
10;
uint64_t stime =
((uint64_t)stimeTicks.dwHighDateTime << 32 | stimeTicks.dwLowDateTime) /
10;
GetProcessMemoryInfo(PI.Process, &counters, sizeof(counters));

TaskProcessInformation tpi(PI.Pid, utime, stime,
counters.PeakWorkingSetSize);
#else
// Wait() returning a return code of -2 indicates the process received
// a signal during execution.
bool crashed = ReturnCode == -2;
TaskProcessInformation tpi(PI.Pid);
#endif
if (crashed) {
if (Signalled) {
TaskFinishedResponse Response =
Signalled(PI.Pid, ErrMsg, stdoutContents, stderrContents, T->Context, ReturnCode, TaskProcessInformation(PI.Pid));
Signalled(PI.Pid, ErrMsg, stdoutContents, stderrContents, T->Context, ReturnCode, tpi);
ContinueExecution = Response != TaskFinishedResponse::StopExecution;
} else {
// If we don't have a Signalled callback, unconditionally stop.
Expand All @@ -169,7 +195,7 @@ bool TaskQueue::execute(TaskBeganCallback Began, TaskFinishedCallback Finished,
// finished.
if (Finished) {
TaskFinishedResponse Response = Finished(PI.Pid, PI.ReturnCode,
stdoutContents, stderrContents, TaskProcessInformation(PI.Pid), T->Context);
stdoutContents, stderrContents, tpi, T->Context);
ContinueExecution = Response != TaskFinishedResponse::StopExecution;
} else if (PI.ReturnCode != 0) {
ContinueExecution = false;
Expand Down
48 changes: 28 additions & 20 deletions lib/Basic/Statistic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
#ifdef HAVE_MALLOC_MALLOC_H
#include <malloc/malloc.h>
#endif
#if defined(_WIN32)
#define NOMINMAX
#include "Windows.h"
#include "psapi.h"
#endif

namespace swift {
using namespace llvm;
Expand All @@ -56,26 +61,6 @@ bool environmentVariableRequestedMaximumDeterminism() {
return false;
}

static int64_t
getChildrenMaxResidentSetSize() {
#if defined(HAVE_GETRUSAGE) && !defined(__HAIKU__)
struct rusage RU;
::getrusage(RUSAGE_CHILDREN, &RU);
int64_t M = static_cast<int64_t>(RU.ru_maxrss);
if (M < 0) {
M = std::numeric_limits<int64_t>::max();
} else {
#ifndef __APPLE__
// Apple systems report bytes; everything else appears to report KB.
M <<= 10;
#endif
}
return M;
#else
return 0;
#endif
}

static std::string
makeFileName(StringRef Prefix,
StringRef ProgramName,
Expand Down Expand Up @@ -380,6 +365,29 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
EntityProfilers = make_unique<StatsProfilers>();
}

void UnifiedStatsReporter::recordJobMaxRSS(long rss) {
maxChildRSS = std::max(maxChildRSS, rss);
}

int64_t UnifiedStatsReporter::getChildrenMaxResidentSetSize() {
#if defined(HAVE_GETRUSAGE) && !defined(__HAIKU__)
struct rusage RU;
::getrusage(RUSAGE_CHILDREN, &RU);
int64_t M = static_cast<int64_t>(RU.ru_maxrss);
if (M < 0) {
M = std::numeric_limits<int64_t>::max();
} else {
#ifndef __APPLE__
// Apple systems report bytes; everything else appears to report KB.
M <<= 10;
#endif
}
return M;
#else
return maxChildRSS;
#endif
}

UnifiedStatsReporter::AlwaysOnDriverCounters &
UnifiedStatsReporter::getDriverCounters()
{
Expand Down
9 changes: 9 additions & 0 deletions lib/Driver/Compilation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,10 @@ namespace driver {
}
}

if (Comp.getStatsReporter() && ProcInfo.getResourceUsage().hasValue())
Comp.getStatsReporter()->recordJobMaxRSS(
ProcInfo.getResourceUsage()->Maxrss);

if (isBatchJob(FinishedCmd)) {
return unpackAndFinishBatch(ReturnCode, Output, Errors,
static_cast<const BatchJob *>(FinishedCmd));
Expand Down Expand Up @@ -684,6 +688,11 @@ namespace driver {
if (TaskQueue::supportsBufferingOutput())
llvm::errs() << Output;
}

if (Comp.getStatsReporter() && ProcInfo.getResourceUsage().hasValue())
Comp.getStatsReporter()->recordJobMaxRSS(
ProcInfo.getResourceUsage()->Maxrss);

if (!ErrorMsg.empty())
Comp.getDiags().diagnose(SourceLoc(),
diag::error_unable_to_execute_command,
Expand Down