Fix profiler module.
authorRuben Ayrapetyan <r.ayrapetyan@samsung.com>
Fri, 15 Sep 2017 18:13:56 +0000 (21:13 +0300)
committerRuben Ayrapetyan <r.ayrapetyan@samsung.com>
Fri, 15 Sep 2017 18:11:13 +0000 (21:11 +0300)
 - improve performance;
 - fix maximum name length.

profiler/profiler/src/profiler.cpp
profiler/profiler/src/stackentry.h
src/track/trace.h
src/track/tracetree.h

index a20c087..497e303 100644 (file)
@@ -78,15 +78,25 @@ ULONG STDMETHODCALLTYPE Profiler::Release(void) {
 }
 
 static IUnknown *g_pICorProfilerInfoUnknown;
-static constexpr size_t MAX_NAME_LENGTH = 256;
 static WCHAR *wszModuleNames = nullptr;
 
 extern __thread StackEntry* g_shadowStack;
+__thread StackEntry* g_freeStackEntryListItems = nullptr;
+
+StackEntry::StackEntry(unsigned int funcId,
+                       char* className,
+                       char* methodName,
+                       StackEntry *next)
+  : m_funcId(funcId), m_next(next)
+{
+    strncpy(m_className, className, sizeof (m_className));
+    strncpy(m_methodName, methodName, sizeof (m_methodName));
+}
 
 static HRESULT GetMethodNameFromTokenAndMetaData (mdToken dwToken, IMetaDataImport * pIMetaDataImport,
                                                LPWSTR wszClass, LPWSTR wszMethod)
 {
-  wchar_t _wszMethod[512];
+  wchar_t _wszMethod[MAX_NAME_LENGTH + 1];
   DWORD cchMethod = sizeof (_wszMethod)/sizeof (_wszMethod[0]);
   mdTypeDef mdClass;
   COR_SIGNATURE const * method_signature;
@@ -104,8 +114,8 @@ static HRESULT GetMethodNameFromTokenAndMetaData (mdToken dwToken, IMetaDataImpo
 
   StringCchCopyW (wszMethod, cchMethod, _wszMethod);
 
-  wchar_t wszTypeDef[512];
-  DWORD cchTypeDef = sizeof(wszTypeDef)/sizeof(wszTypeDef[0]);
+  wchar_t wszTypeDef[MAX_NAME_LENGTH + 1];
+  DWORD cchTypeDef = sizeof(wszTypeDef) / sizeof(wszTypeDef[0]);
 
   if (mdClass == 0x02000000)
       mdClass = 0x02000001;
@@ -141,7 +151,7 @@ static HRESULT GetMethodNameFromFunctionId (ICorProfilerInfo *info, FunctionID f
 void encodeWChar(WCHAR *orig, char *encoded) {
   int i = 0;
   while (orig[i] != 0) {
-    if (orig[i] > 128) {
+    if (orig[i] >= 128) {
       encoded[i] = '?';
     } else {
       encoded[i] = (char)orig[i];
@@ -160,28 +170,34 @@ void OnFunctionEnter(FunctionIDOrClientID functionID,
     assert(false && "Failed to retreive ICorProfilerInfo3");
   }
 
-  WCHAR *szClassName = new WCHAR[MAX_NAME_LENGTH];
-  WCHAR *szMethodName = new WCHAR[MAX_NAME_LENGTH];
-  hr = GetMethodNameFromFunctionId(info, functionID.functionID, szClassName,
-                             szMethodName);
+  WCHAR szClassName[MAX_NAME_LENGTH + 1];
+  WCHAR szMethodName[MAX_NAME_LENGTH + 1];
+
+  hr = GetMethodNameFromFunctionId(info, functionID.functionID, szClassName, szMethodName);
 
 
   if (hr != S_OK) {
-    delete[] szMethodName;
-    delete[] szClassName;
     return;
   }
 
-  char *className = new char[MAX_NAME_LENGTH];
-  char *methodName = new char[MAX_NAME_LENGTH];
+  char className[MAX_NAME_LENGTH + 1];
+  char methodName[MAX_NAME_LENGTH + 1];
 
   encodeWChar(szClassName, className);
   encodeWChar(szMethodName, methodName);
 
-  delete[] szClassName;
-  delete[] szMethodName;
+  StackEntry *se;
+
+  if (g_freeStackEntryListItems != nullptr) {
+    se = g_freeStackEntryListItems;
+
+    g_freeStackEntryListItems = g_freeStackEntryListItems->m_next;
+
+    new (se) StackEntry(functionID.functionID, className, methodName, g_shadowStack);
+  } else {
+    se = new StackEntry(functionID.functionID, className, methodName, g_shadowStack);
+  }
 
-  StackEntry *se = new StackEntry(functionID.functionID, className, methodName, g_shadowStack);
   g_shadowStack = se;
 }
 
@@ -189,8 +205,11 @@ void OnFunctionLeave(FunctionIDOrClientID functionID,
                      COR_PRF_ELT_INFO eltInfo) {
   if (g_shadowStack != nullptr) {
     StackEntry *top = g_shadowStack;
-    g_shadowStack = g_shadowStack->next;
-    delete top;
+
+    g_shadowStack = g_shadowStack->m_next;
+
+    top->m_next = g_freeStackEntryListItems;
+    g_freeStackEntryListItems = top;
   }
 }
 
index f26d6c9..5680283 100644 (file)
@@ -1,27 +1,17 @@
 #ifndef STACKENTRY_H
 #define STACKENTRY_H
 
+static constexpr size_t MAX_NAME_LENGTH = 512;
+
 class StackEntry {
 
 public:
+  StackEntry(unsigned int funcId, char* className, char* methodName, StackEntry *next);
 
-  StackEntry(unsigned int funcId, char* className, char* methodName, StackEntry *next)
-  : funcId(funcId),
-    className(className),
-    methodName(methodName),
-    next(next)
-  {
-  }
-
-  ~StackEntry() {
-    delete[] className;
-    delete[] methodName;
-  }
-
-  unsigned int funcId;
-  char* className;
-  char* methodName;
-  StackEntry * next;
+  unsigned int m_funcId;
+  char m_className[MAX_NAME_LENGTH + 1];
+  char m_methodName[MAX_NAME_LENGTH + 1];
+  StackEntry *m_next;
 };
 
 #endif // STACKENTRY_H
index b38609c..4540c4a 100644 (file)
@@ -116,14 +116,14 @@ private:
             ++i;
             --max_size;
             while (stackIter != nullptr && max_size > 0) {
-                ip_t key = reinterpret_cast<ip_t>(stackIter->funcId);
-                m_managed_names[i] = stackIter->className;
+                ip_t key = reinterpret_cast<ip_t>(stackIter->m_funcId);
+                m_managed_names[i] = stackIter->m_className;
                 m_managed_names[i].append("::");
-                m_managed_names[i].append(stackIter->methodName);
+                m_managed_names[i].append(stackIter->m_methodName);
                 --max_size;
                 m_data[i] = key;
                 ++i;
-                stackIter = stackIter->next;
+                stackIter = stackIter->m_next;
             }
         }
         return i - start_index;
index 05fd5b4..947373b 100644 (file)
@@ -24,6 +24,8 @@
  * @brief Efficiently combine and store the data of multiple Traces.
  */
 
+#include <string.h>
+
 #include <algorithm>
 #include <vector>
 #include <unordered_set>