From 2a66a8b4e3b8981757be183054b18f2905f65c94 Mon Sep 17 00:00:00 2001 From: Andrew Au Date: Tue, 14 Jun 2022 20:50:29 -0700 Subject: [PATCH 1/5] Initialize Generation Aware Analysis later --- src/coreclr/vm/ceemain.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/vm/ceemain.cpp b/src/coreclr/vm/ceemain.cpp index 36d63aa9e771dc..a45a6996c86bc9 100644 --- a/src/coreclr/vm/ceemain.cpp +++ b/src/coreclr/vm/ceemain.cpp @@ -651,7 +651,6 @@ void EEStartupHelper() // Initialize the event pipe. EventPipeAdapter::Initialize(); #endif // FEATURE_PERFTRACING - GenAnalysis::Initialize(); #ifdef TARGET_UNIX PAL_SetShutdownCallback(EESocketCleanupHelper); @@ -877,6 +876,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. From cd6dc930c5f6c32343ed29b6744f758304d8aa29 Mon Sep 17 00:00:00 2001 From: Andrew Au Date: Thu, 16 Jun 2022 10:44:24 -0700 Subject: [PATCH 2/5] Disable Generation Aware Analysis tracing if we had an error --- src/coreclr/vm/genanalysis.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/coreclr/vm/genanalysis.cpp b/src/coreclr/vm/genanalysis.cpp index aa1248b4e56a39..99d0ac70755cd8 100644 --- a/src/coreclr/vm/genanalysis.cpp +++ b/src/coreclr/vm/genanalysis.cpp @@ -110,4 +110,8 @@ bool gcGenAnalysisDump = false; EventPipeAdapter::StartStreaming(gcGenAnalysisEventPipeSessionId); gcGenAnalysisState = GcGenAnalysisState::Enabled; } + else + { + gcGenAnalysisTrace = false; + } } From e4f7cf35c85db1a7beffd571e288a4a069ca8924 Mon Sep 17 00:00:00 2001 From: Andrew Au Date: Wed, 29 Jun 2022 21:20:57 -0700 Subject: [PATCH 3/5] Check null --- src/native/eventpipe/ep-session.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/native/eventpipe/ep-session.c b/src/native/eventpipe/ep-session.c index 6f16f990d0e71e..2dc9f624b6cc15 100644 --- a/src/native/eventpipe/ep-session.c +++ b/src/native/eventpipe/ep-session.c @@ -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; From 529bfb64bc94d18c73676303ae309eb4b7709ba4 Mon Sep 17 00:00:00 2001 From: Andrew Au Date: Sun, 31 Jul 2022 11:22:24 -0700 Subject: [PATCH 4/5] Trace with pid support --- src/coreclr/vm/genanalysis.cpp | 26 ++++++++++++++++++++++++++ src/coreclr/vm/genanalysis.h | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/coreclr/vm/genanalysis.cpp b/src/coreclr/vm/genanalysis.cpp index 99d0ac70755cd8..1369d526bca6a5 100644 --- a/src/coreclr/vm/genanalysis.cpp +++ b/src/coreclr/vm/genanalysis.cpp @@ -78,6 +78,32 @@ bool gcGenAnalysisDump = false; { LPCWSTR outputPath = nullptr; outputPath = GENAWARE_TRACE_FILE_NAME; + + // if the string "{pid}" occurs in the logFilename, + // replace it by the PID of our process + // only the first occurrence will be replaced + + WCHAR fileName[MAX_PATH]; + const WCHAR* pidLit = W("{pid}"); + const WCHAR* pidPtr = wcsstr(outputPath, pidLit); + if (pidPtr != nullptr) + { + // copy the file name up to the "{pid}" occurrence + ptrdiff_t pidInx = pidPtr - outputPath; + wcsncpy_s(fileName, MAX_PATH, outputPath, 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); + + // append the rest of the filename + wcscat_s(fileName, MAX_PATH, outputPath + pidInx + wcslen(pidLit)); + + outputPath = fileName; + } + NewArrayHolder pProviders; int providerCnt = 1; pProviders = new COR_PRF_EVENTPIPE_PROVIDER_CONFIG[providerCnt]; diff --git a/src/coreclr/vm/genanalysis.h b/src/coreclr/vm/genanalysis.h index aca019ff3837b9..f75016b6601ec6 100644 --- a/src/coreclr/vm/genanalysis.h +++ b/src/coreclr/vm/genanalysis.h @@ -18,7 +18,7 @@ enum GcGenAnalysisState Done = 3, }; -#define GENAWARE_TRACE_FILE_NAME W("gcgenaware.nettrace") +#define GENAWARE_TRACE_FILE_NAME W("gcgenaware.{pid}.nettrace") #define GENAWARE_DUMP_FILE_NAME W("gcgenaware.dmp") #define GENAWARE_COMPLETION_FILE_NAME "gcgenaware.nettrace.completed" From 4b843da090832695bbef1cebd1f35c55fd0bae63 Mon Sep 17 00:00:00 2001 From: Andrew Au Date: Wed, 3 Aug 2022 16:27:25 -0700 Subject: [PATCH 5/5] Add Pid support for all genaware analysis files --- src/coreclr/inc/stresslog.h | 2 ++ src/coreclr/utilcode/stresslog.cpp | 32 +++++++++++++++++------------- src/coreclr/vm/finalizerthread.cpp | 5 ++++- src/coreclr/vm/gcenv.ee.cpp | 4 +++- src/coreclr/vm/genanalysis.cpp | 29 ++------------------------- src/coreclr/vm/genanalysis.h | 4 ++-- 6 files changed, 31 insertions(+), 45 deletions(-) diff --git a/src/coreclr/inc/stresslog.h b/src/coreclr/inc/stresslog.h index ab84311792e4c3..58e1ba9660d3d5 100644 --- a/src/coreclr/inc/stresslog.h +++ b/src/coreclr/inc/stresslog.h @@ -259,6 +259,8 @@ #define STRESS_LOG_GC_STACK #endif //_DEBUG +void AppendPid(LPCWSTR logFilename, LPWSTR fileName, size_t fileNameLength); + class ThreadStressLog; struct StressLogMsg; diff --git a/src/coreclr/utilcode/stresslog.cpp b/src/coreclr/utilcode/stresslog.cpp index 6bb8229d8048d7..861f56da000310 100644 --- a/src/coreclr/utilcode/stresslog.cpp +++ b/src/coreclr/utilcode/stresslog.cpp @@ -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 diff --git a/src/coreclr/vm/finalizerthread.cpp b/src/coreclr/vm/finalizerthread.cpp index e8370315e66651..9fce6cc34941b3 100644 --- a/src/coreclr/vm/finalizerthread.cpp +++ b/src/coreclr/vm/finalizerthread.cpp @@ -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) diff --git a/src/coreclr/vm/gcenv.ee.cpp b/src/coreclr/vm/gcenv.ee.cpp index 02537f0dd7f545..42c85aa788770c 100644 --- a/src/coreclr/vm/gcenv.ee.cpp +++ b/src/coreclr/vm/gcenv.ee.cpp @@ -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); diff --git a/src/coreclr/vm/genanalysis.cpp b/src/coreclr/vm/genanalysis.cpp index 1369d526bca6a5..358d0f0270138c 100644 --- a/src/coreclr/vm/genanalysis.cpp +++ b/src/coreclr/vm/genanalysis.cpp @@ -76,33 +76,8 @@ bool gcGenAnalysisDump = false; /* static */ void GenAnalysis::EnableGenerationalAwareSession() { - LPCWSTR outputPath = nullptr; - outputPath = GENAWARE_TRACE_FILE_NAME; - - // if the string "{pid}" occurs in the logFilename, - // replace it by the PID of our process - // only the first occurrence will be replaced - - WCHAR fileName[MAX_PATH]; - const WCHAR* pidLit = W("{pid}"); - const WCHAR* pidPtr = wcsstr(outputPath, pidLit); - if (pidPtr != nullptr) - { - // copy the file name up to the "{pid}" occurrence - ptrdiff_t pidInx = pidPtr - outputPath; - wcsncpy_s(fileName, MAX_PATH, outputPath, 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); - - // append the rest of the filename - wcscat_s(fileName, MAX_PATH, outputPath + pidInx + wcslen(pidLit)); - - outputPath = fileName; - } + WCHAR outputPath[MAX_PATH]; + AppendPid(GENAWARE_TRACE_FILE_NAME, outputPath, MAX_PATH); NewArrayHolder pProviders; int providerCnt = 1; diff --git a/src/coreclr/vm/genanalysis.h b/src/coreclr/vm/genanalysis.h index f75016b6601ec6..ab24a61c9befb4 100644 --- a/src/coreclr/vm/genanalysis.h +++ b/src/coreclr/vm/genanalysis.h @@ -19,8 +19,8 @@ enum GcGenAnalysisState }; #define GENAWARE_TRACE_FILE_NAME W("gcgenaware.{pid}.nettrace") -#define GENAWARE_DUMP_FILE_NAME W("gcgenaware.dmp") -#define GENAWARE_COMPLETION_FILE_NAME "gcgenaware.nettrace.completed" +#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;