ProfileString.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "RuntimeLibraryPch.h"
  6. #ifdef PROFILE_STRINGS
  7. namespace Js
  8. {
  9. // The VS2013 linker treats this as a redefinition of an already
  10. // defined constant and complains. So skip the declaration if we're compiling
  11. // with VS2013 or below.
  12. #if !defined(_MSC_VER) || _MSC_VER >= 1900
  13. const uint StringProfiler::k_MaxConcatLength;
  14. #endif
  15. StringProfiler::StringProfiler(PageAllocator * pageAllocator)
  16. : allocator(_u("StringProfiler"), pageAllocator, Throw::OutOfMemory ),
  17. mainThreadId(GetCurrentThreadContextId() ),
  18. discardedWrongThread(0),
  19. stringLengthMetrics(&allocator),
  20. embeddedNULChars(0),
  21. embeddedNULStrings(0),
  22. emptyStrings(0),
  23. singleCharStrings(0),
  24. stringConcatMetrics(&allocator, 43)
  25. {
  26. }
  27. bool StringProfiler::IsOnWrongThread() const
  28. {
  29. return GetCurrentThreadContextId() != this->mainThreadId;
  30. }
  31. void StringProfiler::RecordNewString( const char16* sz, uint length )
  32. {
  33. if( IsOnWrongThread() )
  34. {
  35. ::InterlockedIncrement(&discardedWrongThread);
  36. return;
  37. }
  38. RequiredEncoding encoding = ASCII7bit;
  39. if(sz)
  40. {
  41. encoding = GetRequiredEncoding(sz, length);
  42. }
  43. StringMetrics metrics = {};
  44. if( stringLengthMetrics.TryGetValue(length, &metrics) )
  45. {
  46. metrics.Accumulate(encoding);
  47. stringLengthMetrics.Item(length,metrics);
  48. }
  49. else
  50. {
  51. metrics.Accumulate(encoding);
  52. stringLengthMetrics.Add(length,metrics);
  53. }
  54. if(sz)
  55. {
  56. uint embeddedNULs = CountEmbeddedNULs(sz, length);
  57. if( embeddedNULs != 0 )
  58. {
  59. this->embeddedNULChars += embeddedNULs;
  60. this->embeddedNULStrings++;
  61. }
  62. }
  63. }
  64. /*static*/ StringProfiler::RequiredEncoding StringProfiler::GetRequiredEncoding( const char16* sz, uint length )
  65. {
  66. RequiredEncoding encoding = ASCII7bit;
  67. for( uint i = 0; i != length; ++i )
  68. {
  69. unsigned short ch = static_cast< unsigned short >(sz[i]);
  70. if( ch >= 0x100 )
  71. {
  72. encoding = Unicode16bit;
  73. break; // no need to look further
  74. }
  75. else if( ch >= 0x80 )
  76. {
  77. encoding = ASCII8bit;
  78. }
  79. }
  80. return encoding;
  81. }
  82. /*static*/ uint StringProfiler::CountEmbeddedNULs( const char16* sz, uint length )
  83. {
  84. uint result = 0;
  85. for( uint i = 0; i != length; ++i )
  86. {
  87. if( sz[i] == _u('\0') ) ++result;
  88. }
  89. return result;
  90. }
  91. StringProfiler::HistogramIndex::HistogramIndex( ArenaAllocator* allocator, uint size )
  92. {
  93. this->index = AnewArray(allocator, UintUintPair, size);
  94. this->count = 0;
  95. }
  96. void StringProfiler::HistogramIndex::Add( uint len, uint freq )
  97. {
  98. index[count].first = len;
  99. index[count].second = freq;
  100. count++;
  101. }
  102. uint StringProfiler::HistogramIndex::Get( uint i ) const
  103. {
  104. Assert( i < count );
  105. return index[i].first;
  106. }
  107. uint StringProfiler::HistogramIndex::Count() const
  108. {
  109. return count;
  110. }
  111. /*static*/ int StringProfiler::HistogramIndex::CompareDescending( const void* lhs, const void* rhs )
  112. {
  113. // Compare on frequency (second)
  114. const UintUintPair* lhsPair = static_cast< const UintUintPair* >(lhs);
  115. const UintUintPair* rhsPair = static_cast< const UintUintPair* >(rhs);
  116. if( lhsPair->second < rhsPair->second ) return 1;
  117. if( lhsPair->second == rhsPair->second ) return 0;
  118. return -1;
  119. }
  120. void StringProfiler::HistogramIndex::SortDescending()
  121. {
  122. qsort(this->index, this->count, sizeof(UintUintPair), CompareDescending);
  123. }
  124. /*static*/ void StringProfiler::PrintOne(
  125. unsigned int len,
  126. StringMetrics metrics,
  127. uint totalCount
  128. )
  129. {
  130. Output::Print(_u("%10u %10u %10u %10u %10u (%.1f%%)\n"),
  131. len,
  132. metrics.count7BitASCII,
  133. metrics.count8BitASCII,
  134. metrics.countUnicode,
  135. metrics.Total(),
  136. 100.0*(double)metrics.Total()/(double)totalCount
  137. );
  138. }
  139. /*static*/ void StringProfiler::PrintUintOrLarge( uint val )
  140. {
  141. if( val >= k_MaxConcatLength )
  142. {
  143. Output::Print(_u(" Large"), k_MaxConcatLength);
  144. }
  145. else
  146. {
  147. Output::Print(_u("%6u"), val);
  148. }
  149. }
  150. /*static*/ void StringProfiler::PrintOneConcat( UintUintPair const& key, const ConcatMetrics& metrics)
  151. {
  152. PrintUintOrLarge(key.first);
  153. Output::Print(_u(" "));
  154. PrintUintOrLarge(key.second);
  155. Output::Print(_u(" %6u"), metrics.compoundStringCount);
  156. Output::Print(_u(" %6u"), metrics.concatTreeCount);
  157. Output::Print(_u(" %6u"), metrics.bufferStringBuilderCount);
  158. Output::Print(_u(" %6u"), metrics.unknownCount);
  159. Output::Print(_u(" %6u\n"), metrics.Total());
  160. }
  161. void StringProfiler::PrintAll()
  162. {
  163. Output::Print(_u("=============================================================\n"));
  164. Output::Print(_u("String Statistics\n"));
  165. Output::Print(_u("-------------------------------------------------------------\n"));
  166. Output::Print(_u(" Length 7bit ASCII 8bit ASCII Unicode Total %%Total\n"));
  167. Output::Print(_u(" --------- ---------- ---------- ---------- ---------- ------\n"));
  168. // Build an index for printing the histogram in descending order
  169. HistogramIndex index(&allocator, stringLengthMetrics.Count());
  170. uint totalStringCount = 0;
  171. stringLengthMetrics.Map([this, &index, &totalStringCount](unsigned int len, StringMetrics metrics)
  172. {
  173. uint lengthTotal = metrics.Total();
  174. index.Add(len, lengthTotal);
  175. totalStringCount += lengthTotal;
  176. });
  177. index.SortDescending();
  178. StringMetrics cumulative = {};
  179. uint maxLength = 0;
  180. for(uint i = 0; i != index.Count(); ++i )
  181. {
  182. uint length = index.Get(i);
  183. // UintHashMap::Lookup doesn't work with value-types (it returns NULL
  184. // on error), so use TryGetValue instead.
  185. StringMetrics metrics;
  186. if( stringLengthMetrics.TryGetValue(length, &metrics) )
  187. {
  188. PrintOne( length, metrics, totalStringCount );
  189. cumulative.Accumulate(metrics);
  190. maxLength = max( maxLength, length );
  191. }
  192. }
  193. Output::Print(_u("-------------------------------------------------------------\n"));
  194. Output::Print(_u(" Totals %10u %10u %10u %10u (100%%)\n"),
  195. cumulative.count7BitASCII,
  196. cumulative.count8BitASCII,
  197. cumulative.countUnicode,
  198. cumulative.Total() );
  199. if(discardedWrongThread>0)
  200. {
  201. Output::Print(_u("WARNING: %u strings were not counted because they were allocated on a background thread\n"),discardedWrongThread);
  202. }
  203. Output::Print(_u("\n"));
  204. Output::Print(_u("Max string length is %u chars\n"), maxLength);
  205. Output::Print(_u("%u empty strings (Literals or BufferString) were requested\n"), emptyStrings);
  206. Output::Print(_u("%u single char strings (Literals or BufferString) were requested\n"), singleCharStrings);
  207. if( this->embeddedNULStrings == 0 )
  208. {
  209. Output::Print(_u("No embedded NULs were detected\n"));
  210. }
  211. else
  212. {
  213. Output::Print(_u("Embedded NULs: %u NULs in %u strings\n"), this->embeddedNULChars, this->embeddedNULStrings);
  214. }
  215. Output::Print(_u("\n"));
  216. if(stringConcatMetrics.Count() == 0)
  217. {
  218. Output::Print(_u("No string concatenations were performed\n"));
  219. }
  220. else
  221. {
  222. Output::Print(_u("String concatenations (Strings %u chars or longer are treated as \"Large\")\n"), k_MaxConcatLength);
  223. Output::Print(_u(" LHS + RHS SB Concat Buf Other Total\n"));
  224. Output::Print(_u("------ ------ ------ ------ ------ ------ ------\n"));
  225. uint totalConcatenations = 0;
  226. uint totalConcatTree = 0;
  227. uint totalBufString = 0;
  228. uint totalCompoundString = 0;
  229. uint totalOther = 0;
  230. stringConcatMetrics.Map([&](UintUintPair const& key, const ConcatMetrics& metrics)
  231. {
  232. PrintOneConcat(key, metrics);
  233. totalConcatenations += metrics.Total();
  234. totalConcatTree += metrics.concatTreeCount;
  235. totalBufString += metrics.bufferStringBuilderCount;
  236. totalCompoundString += metrics.compoundStringCount;
  237. totalOther += metrics.unknownCount;
  238. }
  239. );
  240. Output::Print(_u("-------------------------------------------------------\n"));
  241. Output::Print(_u("Total %6u %6u %6u %6u %6u\n"), totalConcatenations, totalCompoundString, totalConcatTree, totalBufString, totalOther);
  242. }
  243. Output::Flush();
  244. }
  245. void StringProfiler::RecordConcatenation( uint lenLeft, uint lenRight, ConcatType type )
  246. {
  247. if( IsOnWrongThread() )
  248. {
  249. return;
  250. }
  251. lenLeft = min( lenLeft, k_MaxConcatLength );
  252. lenRight = min( lenRight, k_MaxConcatLength );
  253. UintUintPair key = { lenLeft, lenRight };
  254. ConcatMetrics* metrics;
  255. if(!stringConcatMetrics.TryGetReference(key, &metrics))
  256. {
  257. stringConcatMetrics.Add(key, ConcatMetrics(type));
  258. }
  259. else
  260. {
  261. metrics->Accumulate(type);
  262. }
  263. }
  264. /*static*/ void StringProfiler::RecordNewString( ScriptContext* scriptContext, const char16* sz, uint length )
  265. {
  266. StringProfiler* stringProfiler = scriptContext->GetStringProfiler();
  267. if( stringProfiler )
  268. {
  269. stringProfiler->RecordNewString(sz, length);
  270. }
  271. }
  272. /*static*/ void StringProfiler::RecordConcatenation( ScriptContext* scriptContext, uint lenLeft, uint lenRight, ConcatType type)
  273. {
  274. StringProfiler* stringProfiler = scriptContext->GetStringProfiler();
  275. if( stringProfiler )
  276. {
  277. stringProfiler->RecordConcatenation(lenLeft, lenRight, type);
  278. }
  279. }
  280. /*static*/ void StringProfiler::RecordEmptyStringRequest( ScriptContext* scriptContext )
  281. {
  282. StringProfiler* stringProfiler = scriptContext->GetStringProfiler();
  283. if( stringProfiler )
  284. {
  285. ::InterlockedIncrement( &stringProfiler->emptyStrings );
  286. }
  287. }
  288. /*static*/ void StringProfiler::RecordSingleCharStringRequest( ScriptContext* scriptContext )
  289. {
  290. StringProfiler* stringProfiler = scriptContext->GetStringProfiler();
  291. if( stringProfiler )
  292. {
  293. ::InterlockedIncrement( &stringProfiler->singleCharStrings );
  294. }
  295. }
  296. } // namespace Js
  297. #endif