Fix superpmi-shim-counter (#19639)
authorSergey Andreenko <seandree@microsoft.com>
Fri, 24 Aug 2018 05:54:52 +0000 (22:54 -0700)
committerGitHub <noreply@github.com>
Fri, 24 Aug 2018 05:54:52 +0000 (22:54 -0700)
* fix the issue

* improve counter perfomance

do not rewrite output file after each method

src/ToolBox/superpmi/superpmi-shim-counter/icorjitcompiler.cpp
src/ToolBox/superpmi/superpmi-shim-counter/methodcallsummarizer.cpp
src/ToolBox/superpmi/superpmi-shim-counter/superpmi-shim-counter.cpp

index e0121c3..37a0559 100644 (file)
@@ -27,7 +27,6 @@ CorJitResult __stdcall interceptor_ICJC::compileMethod(ICorJitInfo*
     CorJitResult temp =
         original_ICorJitCompiler->compileMethod(&our_ICorJitInfo, info, flags, nativeEntry, nativeSizeOfCode);
 
-    mcs->SaveTextFile();
     return temp;
 }
 
index eb4a56d..ad12ffc 100644 (file)
@@ -5,6 +5,7 @@
 
 #include "standardpch.h"
 #include "methodcallsummarizer.h"
+#include "logging.h"
 
 MethodCallSummarizer::MethodCallSummarizer(WCHAR* logPath)
 {
@@ -133,6 +134,12 @@ void MethodCallSummarizer::SaveTextFile()
     HANDLE hFile        = CreateFileW(dataFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
                                FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
 
+    if (hFile == INVALID_HANDLE_VALUE)
+    {
+        LogError("Couldn't open file '%ws': error %d", dataFileName, ::GetLastError());
+        return;
+    }
+
     DWORD len = (DWORD)sprintf_s(buff, 512, "FunctionName,Count\n");
     WriteFile(hFile, buff, len, &bytesWritten, NULL);
 
index f838ea6..b234727 100644 (file)
 #include "spmiutil.h"
 #include "jithost.h"
 
-HMODULE g_hRealJit           = 0;       // We leak this currently (could do the proper shutdown in process_detach)
-WCHAR*  g_realJitPath        = nullptr; // We leak this (could do the proper shutdown in process_detach)
-WCHAR*  g_logPath            = nullptr; // Again, we leak this one too...
-char*   g_logFilePath        = nullptr; // We *don't* leak this, hooray!
-WCHAR*  g_HomeDirectory      = nullptr;
-WCHAR*  g_DefaultRealJitPath = nullptr;
+HMODULE               g_hRealJit      = 0; // We leak this currently (could do the proper shutdown in process_detach)
+WCHAR*                g_realJitPath   = nullptr; // We leak this (could do the proper shutdown in process_detach)
+WCHAR*                g_logPath       = nullptr; // Again, we leak this one too...
+char*                 g_logFilePath   = nullptr; // We *don't* leak this, hooray!
+WCHAR*                g_HomeDirectory = nullptr;
+WCHAR*                g_DefaultRealJitPath = nullptr;
+MethodCallSummarizer* g_globalContext      = nullptr;
 
 void SetDefaultPaths()
 {
@@ -95,6 +96,11 @@ extern "C" BOOL
             delete[] g_logFilePath;
             g_logFilePath = nullptr;
 
+            if (g_globalContext != nullptr)
+            {
+                g_globalContext->SaveTextFile();
+            }
+
             break;
 
         case DLL_THREAD_ATTACH:
@@ -131,6 +137,15 @@ extern "C" void __stdcall jitStartup(ICorJitHost* host)
     }
 
     g_ourJitHost = new JitHost(host);
+
+    if (g_globalContext == nullptr)
+    {
+        SetLogPath();
+        g_globalContext = new MethodCallSummarizer(g_logPath);
+    }
+
+    g_ourJitHost->setMethodCallSummarizer(g_globalContext);
+
     pnjitStartup(g_ourJitHost);
 }
 
@@ -144,7 +159,6 @@ extern "C" ICorJitCompiler* __stdcall getJit()
 
     SetDefaultPaths();
     SetLibName();
-    SetLogPath();
 
     // Load Library
     if (g_hRealJit == 0)
@@ -174,11 +188,13 @@ extern "C" ICorJitCompiler* __stdcall getJit()
 
     pJitInstance                           = new interceptor_ICJC();
     pJitInstance->original_ICorJitCompiler = tICJI;
-    pJitInstance->mcs                      = new MethodCallSummarizer(g_logPath);
-    if (g_ourJitHost != nullptr)
+
+    if (g_globalContext == nullptr)
     {
-        g_ourJitHost->setMethodCallSummarizer(pJitInstance->mcs);
+        SetLogPath();
+        g_globalContext = new MethodCallSummarizer(g_logPath);
     }
+    pJitInstance->mcs = g_globalContext;
     return pJitInstance;
 }