Skip to content

Commit

Permalink
Various fixes for generation aware analysis (#70764)
Browse files Browse the repository at this point in the history
  • Loading branch information
cshung authored Aug 7, 2022
1 parent ccc2250 commit e43e315
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/coreclr/inc/stresslog.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@
#define STRESS_LOG_GC_STACK
#endif //_DEBUG

void AppendPid(LPCWSTR logFilename, LPWSTR fileName, size_t fileNameLength);

class ThreadStressLog;

struct StressLogMsg;
Expand Down
32 changes: 18 additions & 14 deletions src/coreclr/utilcode/stresslog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,38 +143,42 @@ void StressLog::Leave(CRITSEC_COOKIE) {
DecCantAllocCount();
}

#ifdef MEMORY_MAPPED_STRESSLOG
static LPVOID CreateMemoryMappedFile(LPWSTR logFilename, size_t maxBytesTotal)
void AppendPid(LPCWSTR logFilename, LPWSTR fileName, size_t fileNameLength)
{
if (maxBytesTotal < sizeof(StressLog::StressLogHeader))
{
return nullptr;
}
WCHAR fileName[MAX_PATH];

// if the string "{pid}" occurs in the logFilename,
// replace it by the PID of our process
// only the first occurrence will be replaced
const WCHAR* pidLit = W("{pid}");
WCHAR* pidPtr = wcsstr(logFilename, pidLit);
const WCHAR* pidPtr = wcsstr(logFilename, pidLit);
if (pidPtr != nullptr)
{
// copy the file name up to the "{pid}" occurrence
ptrdiff_t pidInx = pidPtr - logFilename;
wcsncpy_s(fileName, MAX_PATH, logFilename, pidInx);
wcsncpy_s(fileName, fileNameLength, logFilename, pidInx);

// append the string representation of the PID
DWORD pid = GetCurrentProcessId();
WCHAR pidStr[20];
_itow_s(pid, pidStr, ARRAY_SIZE(pidStr), 10);
wcscat_s(fileName, MAX_PATH, pidStr);
wcscat_s(fileName, fileNameLength, pidStr);

// append the rest of the filename
wcscat_s(fileName, MAX_PATH, logFilename + pidInx + wcslen(pidLit));
wcscat_s(fileName, fileNameLength, logFilename + pidInx + wcslen(pidLit));
}
}

logFilename = fileName;
#ifdef MEMORY_MAPPED_STRESSLOG
static LPVOID CreateMemoryMappedFile(LPWSTR logFilename, size_t maxBytesTotal)
{
if (maxBytesTotal < sizeof(StressLog::StressLogHeader))
{
return nullptr;
}
HandleHolder hFile = WszCreateFile(logFilename,

WCHAR fileName[MAX_PATH];
AppendPid(logFilename, fileName, MAX_PATH);

HandleHolder hFile = WszCreateFile(fileName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ,
NULL, // default security descriptor
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/ceemain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,6 @@ void EEStartupHelper()
// Initialize the event pipe.
EventPipeAdapter::Initialize();
#endif // FEATURE_PERFTRACING
GenAnalysis::Initialize();

#ifdef TARGET_UNIX
PAL_SetShutdownCallback(EESocketCleanupHelper);
Expand Down Expand Up @@ -899,6 +898,7 @@ void EEStartupHelper()
// EventPipe initialization, so this is done after the GC has been fully initialized.
EventPipeAdapter::FinishInitialize();
#endif // FEATURE_PERFTRACING
GenAnalysis::Initialize();

// This isn't done as part of InitializeGarbageCollector() above because thread
// creation requires AppDomains to have been set up.
Expand Down
5 changes: 4 additions & 1 deletion src/coreclr/vm/finalizerthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,11 @@ VOID FinalizerThread::FinalizerThreadWorker(void *args)
GenAnalysis::EnableGenerationalAwareSession();
#endif
}

// Writing an empty file to indicate completion
fclose(fopen(GENAWARE_COMPLETION_FILE_NAME,"w+"));
WCHAR outputPath[MAX_PATH];
AppendPid(GENAWARE_COMPLETION_FILE_NAME, outputPath, MAX_PATH);
fclose(_wfopen(outputPath, W("w+")));
}

if (!bPriorityBoosted)
Expand Down
4 changes: 3 additions & 1 deletion src/coreclr/vm/gcenv.ee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1695,7 +1695,9 @@ void GCToEEInterface::AnalyzeSurvivorsFinished(size_t gcIndex, int condemnedGene
{
EX_TRY
{
GenerateDump (GENAWARE_DUMP_FILE_NAME, 2, GenerateDumpFlagsNone, nullptr, 0);
WCHAR outputPath[MAX_PATH];
AppendPid(GENAWARE_DUMP_FILE_NAME, outputPath, MAX_PATH);
GenerateDump (outputPath, 2, GenerateDumpFlagsNone, nullptr, 0);
}
EX_CATCH {}
EX_END_CATCH(SwallowAllExceptions);
Expand Down
9 changes: 7 additions & 2 deletions src/coreclr/vm/genanalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ bool gcGenAnalysisDump = false;

/* static */ void GenAnalysis::EnableGenerationalAwareSession()
{
LPCWSTR outputPath = nullptr;
outputPath = GENAWARE_TRACE_FILE_NAME;
WCHAR outputPath[MAX_PATH];
AppendPid(GENAWARE_TRACE_FILE_NAME, outputPath, MAX_PATH);

NewArrayHolder<COR_PRF_EVENTPIPE_PROVIDER_CONFIG> pProviders;
int providerCnt = 1;
pProviders = new COR_PRF_EVENTPIPE_PROVIDER_CONFIG[providerCnt];
Expand Down Expand Up @@ -110,4 +111,8 @@ bool gcGenAnalysisDump = false;
EventPipeAdapter::StartStreaming(gcGenAnalysisEventPipeSessionId);
gcGenAnalysisState = GcGenAnalysisState::Enabled;
}
else
{
gcGenAnalysisTrace = false;
}
}
6 changes: 3 additions & 3 deletions src/coreclr/vm/genanalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ enum GcGenAnalysisState
Done = 3,
};

#define GENAWARE_TRACE_FILE_NAME W("gcgenaware.nettrace")
#define GENAWARE_DUMP_FILE_NAME W("gcgenaware.dmp")
#define GENAWARE_COMPLETION_FILE_NAME "gcgenaware.nettrace.completed"
#define GENAWARE_TRACE_FILE_NAME W("gcgenaware.{pid}.nettrace")
#define GENAWARE_DUMP_FILE_NAME W("gcgenaware.{pid}.dmp")
#define GENAWARE_COMPLETION_FILE_NAME W("gcgenaware.{pid}.nettrace.completed")

extern bool s_forcedGCInProgress;
extern GcGenAnalysisState gcGenAnalysisState;
Expand Down
1 change: 1 addition & 0 deletions src/native/eventpipe/ep-session.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ ep_session_alloc (
case EP_SESSION_TYPE_FILESTREAM :
if (output_path) {
file_stream_writer = ep_file_stream_writer_alloc (output_path);
ep_raise_error_if_nok (file_stream_writer != NULL);
instance->file = ep_file_alloc (ep_file_stream_writer_get_stream_writer_ref (file_stream_writer), format);
ep_raise_error_if_nok (instance->file != NULL);
file_stream_writer = NULL;
Expand Down

0 comments on commit e43e315

Please sign in to comment.