StatsCapture.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <native/utilities/StatsCapture.h>
  9. #include <native/assetprocessor.h>
  10. #include <native/AssetDatabase/AssetDatabase.h>
  11. #include <AzCore/Memory/SystemAllocator.h>
  12. #include <AzCore/Settings/SettingsRegistry.h>
  13. #include <AzCore/std/chrono/chrono.h>
  14. #include <AzCore/std/chrono/chrono.h>
  15. #include <AzCore/std/string/string.h>
  16. #include <AzCore/std/sort.h>
  17. #include <AzCore/std/containers/unordered_map.h>
  18. #include <AzCore/std/containers/unordered_set.h>
  19. #include <AzCore/std/containers/vector.h>
  20. #include <AzCore/StringFunc/StringFunc.h>
  21. #include <inttypes.h>
  22. namespace AssetProcessor
  23. {
  24. namespace StatsCapture
  25. {
  26. // This class captures stats by storing them in a map of type
  27. // [name of stat] -> Stat struct
  28. // It can then analyze these stats and produce more stats from the original
  29. // Captures, before dumping.
  30. class StatsCaptureImpl final
  31. {
  32. public:
  33. AZ_CLASS_ALLOCATOR(StatsCaptureImpl, AZ::SystemAllocator);
  34. StatsCaptureImpl();
  35. void BeginCaptureStat(AZStd::string_view statName);
  36. AZStd::optional<AZStd::sys_time_t> EndCaptureStat(AZStd::string_view statName, bool persistToDb);
  37. void Dump();
  38. private:
  39. using timepoint = AZStd::chrono::steady_clock::time_point;
  40. using duration = AZStd::chrono::milliseconds;
  41. struct StatsEntry
  42. {
  43. duration m_cumulativeTime = {}; // The total amount of time spent on this.
  44. timepoint m_operationStartTime = {}; // Async tracking - the last time stamp an operation started.
  45. int64_t m_operationCount = 0; // In case there's more than one sample. Used to calc average.
  46. };
  47. AssetDatabaseConnection m_dbConnection;
  48. AZStd::unordered_map<AZStd::string, StatsEntry> m_stats;
  49. bool m_dumpMachineReadableStats = false;
  50. bool m_dumpHumanReadableStats = true;
  51. bool m_dbConnectionIsOpen = false;
  52. // Make a friendly time string of the format nnHnnMhhS.xxxms
  53. AZStd::string FormatDuration(const duration& duration)
  54. {
  55. int64_t milliseconds = duration.count();
  56. constexpr int64_t millisecondsInASecond = 1000;
  57. constexpr int64_t millisecondsInAMinute = millisecondsInASecond * 60;
  58. constexpr int64_t millisecondsInAnHour = millisecondsInAMinute * 60;
  59. int64_t hours = milliseconds / millisecondsInAnHour;
  60. milliseconds -= hours * millisecondsInAnHour;
  61. int64_t minutes = milliseconds / millisecondsInAMinute;
  62. milliseconds -= minutes * millisecondsInAMinute;
  63. int64_t seconds = milliseconds / millisecondsInASecond;
  64. milliseconds -= seconds * millisecondsInASecond;
  65. // omit the sections which dont make sense for readability
  66. if (hours)
  67. {
  68. return AZStd::string::format("%02" PRId64 "h%02" PRId64 "m%02" PRId64 "s%03" PRId64 "ms" , hours, minutes, seconds, milliseconds);
  69. }
  70. else if (minutes)
  71. {
  72. return AZStd::string::format(" %02" PRId64 "m%02" PRId64 "s%03" PRId64 "ms", minutes, seconds, milliseconds);
  73. }
  74. else if (seconds)
  75. {
  76. return AZStd::string::format(" %02" PRId64 "s%03" PRId64 "ms", seconds, milliseconds);
  77. }
  78. return AZStd::string::format(" %03" PRId64 "ms", milliseconds);
  79. }
  80. // Prints out a single stat.
  81. void PrintStat([[maybe_unused]] const char* name, duration milliseconds, int64_t count)
  82. {
  83. // note that name may be unused as it only appears in Trace macros, which are
  84. // stripped out in release builds.
  85. if (count <= 1)
  86. {
  87. count = 1;
  88. }
  89. duration average(static_cast<int64_t>(static_cast<double>(milliseconds.count()) / static_cast<double>(count)));
  90. if (m_dumpHumanReadableStats)
  91. {
  92. if (count > 1)
  93. {
  94. AZ_TracePrintf(AssetProcessor::ConsoleChannel, " Time: %s, Count: %4" PRId64 ", Average: %s, EventName: %s\n",
  95. FormatDuration(milliseconds).c_str(),
  96. count,
  97. FormatDuration(average).c_str(),
  98. name);
  99. }
  100. else
  101. {
  102. AZ_TracePrintf(AssetProcessor::ConsoleChannel, " Time: %s, EventName: %s\n",
  103. FormatDuration(milliseconds).c_str(),
  104. name);
  105. }
  106. }
  107. if (m_dumpMachineReadableStats)
  108. {
  109. // machine Readable mode prints raw milliseconds and uses a CSV-like format
  110. // note that the stat itself may contain commas, so we dont acutally separate with comma
  111. // instead we separate with :
  112. // and each "interesting line" is 'MachineReadableStat:milliseconds:count:average:name'
  113. AZ_TracePrintf(AssetProcessor::ConsoleChannel, "MachineReadableStat:%" PRId64 ":%" PRId64 ":%" PRId64 ":%s\n",
  114. milliseconds.count(),
  115. count,
  116. count > 1 ? average.count() : milliseconds.count(),
  117. name);
  118. }
  119. }
  120. // calls PrintStat on each element in the vector.
  121. void PrintStatsArray(AZStd::vector<AZStd::string>& keys, int maxToPrint, const char* header)
  122. {
  123. // don't print anything out at all, not even a header, if the keys are empty.
  124. if (keys.empty())
  125. {
  126. return;
  127. }
  128. if ((m_dumpHumanReadableStats)&&(header))
  129. {
  130. AZ_TracePrintf(AssetProcessor::ConsoleChannel,"Top %i %s\n", maxToPrint, header);
  131. }
  132. auto sortByTimeDescending = [&](const AZStd::string& s1, const AZStd::string& s2)
  133. {
  134. return this->m_stats[s1].m_cumulativeTime > this->m_stats[s2].m_cumulativeTime;
  135. };
  136. AZStd::sort(keys.begin(), keys.end(), sortByTimeDescending);
  137. for (int idx = 0; idx < maxToPrint; ++idx)
  138. {
  139. if (idx < keys.size())
  140. {
  141. PrintStat(keys[idx].c_str(), m_stats[keys[idx]].m_cumulativeTime, m_stats[keys[idx]].m_operationCount);
  142. }
  143. }
  144. }
  145. };
  146. StatsCaptureImpl::StatsCaptureImpl()
  147. {
  148. m_dbConnectionIsOpen = m_dbConnection.OpenDatabase();
  149. AZ_Error(
  150. AssetProcessor::ConsoleChannel,
  151. m_dbConnectionIsOpen,
  152. "Cannot open the asset database for capturing asset processing stats.\n");
  153. }
  154. void StatsCaptureImpl::BeginCaptureStat(AZStd::string_view statName)
  155. {
  156. if (!m_dbConnectionIsOpen)
  157. {
  158. return;
  159. }
  160. StatsEntry& existingStat = m_stats[statName];
  161. if (existingStat.m_operationStartTime != timepoint())
  162. {
  163. // prevent double 'Begins'
  164. return;
  165. }
  166. existingStat.m_operationStartTime = AZStd::chrono::steady_clock::now();
  167. }
  168. AZStd::optional<AZStd::sys_time_t> StatsCaptureImpl::EndCaptureStat(AZStd::string_view statName, bool persistToDb)
  169. {
  170. if (!m_dbConnectionIsOpen)
  171. {
  172. return AZStd::optional<AZStd::sys_time_t>();
  173. }
  174. StatsEntry& existingStat = m_stats[statName];
  175. AZStd::optional<AZStd::sys_time_t> operationDurationInMillisecond;
  176. if (existingStat.m_operationStartTime != timepoint())
  177. {
  178. duration operationDuration = AZStd::chrono::duration_cast<duration>(AZStd::chrono::steady_clock::now() - existingStat.m_operationStartTime);
  179. operationDurationInMillisecond = operationDuration.count();
  180. existingStat.m_cumulativeTime = existingStat.m_cumulativeTime + operationDuration;
  181. existingStat.m_operationCount = existingStat.m_operationCount + 1;
  182. existingStat.m_operationStartTime = timepoint(); // reset the start time so that double 'Ends' are ignored.
  183. if (persistToDb)
  184. {
  185. AzToolsFramework::AssetDatabase::StatDatabaseEntry entry;
  186. entry.m_statName = statName;
  187. entry.m_statValue = aznumeric_cast<AZ::s64>(operationDuration.count());
  188. entry.m_lastLogTime = aznumeric_cast<AZ::s64>(QDateTime::currentMSecsSinceEpoch());
  189. m_dbConnection.ReplaceStat(entry);
  190. }
  191. }
  192. return operationDurationInMillisecond;
  193. }
  194. void StatsCaptureImpl::Dump()
  195. {
  196. if (!m_dbConnectionIsOpen)
  197. {
  198. return;
  199. }
  200. timepoint startTimeStamp = AZStd::chrono::steady_clock::now();
  201. auto settingsRegistry = AZ::SettingsRegistry::Get();
  202. int maxCumulativeStats = 5; // default max cumulative stats to show
  203. int maxIndividualStats = 5; // default max individual files to show
  204. if (settingsRegistry)
  205. {
  206. AZ::u64 cumulativeStats = static_cast<AZ::u64>(maxCumulativeStats);
  207. AZ::u64 individualStats = static_cast<AZ::u64>(maxIndividualStats);
  208. settingsRegistry->Get(m_dumpHumanReadableStats, "/Amazon/AssetProcessor/Settings/Stats/HumanReadable");
  209. settingsRegistry->Get(m_dumpMachineReadableStats, "/Amazon/AssetProcessor/Settings/Stats/MachineReadable");
  210. settingsRegistry->Get(cumulativeStats, "/Amazon/AssetProcessor/Settings/Stats/MaxCumulativeStats");
  211. settingsRegistry->Get(individualStats, "/Amazon/AssetProcessor/Settings/Stats/MaxIndividualStats");
  212. maxCumulativeStats = static_cast<int>(cumulativeStats);
  213. maxIndividualStats = static_cast<int>(individualStats);
  214. }
  215. if ((!m_dumpHumanReadableStats)&&(!m_dumpMachineReadableStats))
  216. {
  217. return;
  218. }
  219. AZStd::vector<AZStd::string> allCreateJobs; // individual
  220. AZStd::vector<AZStd::string> allCreateJobsByBuilder; // bucketed by builder
  221. AZStd::vector<AZStd::string> allProcessJobs; // individual
  222. AZStd::vector<AZStd::string> allProcessJobsByPlatform; // bucketed by platform
  223. AZStd::vector<AZStd::string> allProcessJobsByJobKey; // bucketed by type of job (job key)
  224. AZStd::vector<AZStd::string> allHashFiles;
  225. // capture only existing keys as we will be expanding the stats
  226. // this approach avoids mutating an iterator.
  227. AZStd::vector<AZStd::string> statKeys;
  228. for (const auto& element : m_stats)
  229. {
  230. statKeys.push_back(element.first);
  231. }
  232. for (const AZStd::string& statKey : statKeys)
  233. {
  234. const StatsEntry& statistic = m_stats[statKey];
  235. // Createjobs stats encode like (CreateJobs,sourcefilepath,builderid)
  236. if (AZ::StringFunc::StartsWith(statKey, "CreateJobs,", true))
  237. {
  238. allCreateJobs.push_back(statKey);
  239. AZStd::vector<AZStd::string> tokens;
  240. AZ::StringFunc::Tokenize(statKey, tokens, ",", false, false);
  241. // look up the builder so you can get its name:
  242. AZStd::string_view builderName = tokens[2];
  243. // synthesize a stat to track per-builder createjobs times:
  244. {
  245. AZStd::string newStatKey = AZStd::string::format("CreateJobsByBuilder,%.*s", AZ_STRING_ARG(builderName));
  246. auto insertion = m_stats.insert(newStatKey);
  247. StatsEntry& statToSynth = insertion.first->second;
  248. statToSynth.m_cumulativeTime += statistic.m_cumulativeTime;
  249. statToSynth.m_operationCount += statistic.m_operationCount;
  250. if (insertion.second)
  251. {
  252. allCreateJobsByBuilder.push_back(newStatKey);
  253. }
  254. }
  255. // synthesize a stat to track total createjobs times:
  256. {
  257. StatsEntry& statToSynth = m_stats["CreateJobsTotal"];
  258. statToSynth.m_cumulativeTime += statistic.m_cumulativeTime;
  259. statToSynth.m_operationCount += statistic.m_operationCount;
  260. }
  261. }
  262. else if (AZ::StringFunc::StartsWith(statKey, "ProcessJob,", true))
  263. {
  264. allProcessJobs.push_back(statKey);
  265. // processjob has the format ProcessJob,sourcename,jobkey,platformname
  266. AZStd::vector<AZStd::string> tokens;
  267. AZ::StringFunc::Tokenize(statKey, tokens, ",", false, false);
  268. AZStd::string_view jobKey = tokens[2];
  269. AZStd::string_view platformName = tokens[3];
  270. // synthesize a stat to record process time accumulated by job key platform
  271. {
  272. AZStd::string newStatKey = AZStd::string::format("ProcessJobsByPlatform,%.*s", AZ_STRING_ARG(platformName));
  273. auto insertion = m_stats.insert(newStatKey);
  274. StatsEntry& statToSynth = insertion.first->second;
  275. statToSynth.m_cumulativeTime += statistic.m_cumulativeTime;
  276. statToSynth.m_operationCount += statistic.m_operationCount;
  277. if (insertion.second)
  278. {
  279. allProcessJobsByPlatform.push_back(newStatKey);
  280. }
  281. }
  282. // synthesize a stat to record process time accumulated job key total across all platforms
  283. {
  284. AZStd::string newStatKey = AZStd::string::format("ProcessJobsByJobKey,%.*s", AZ_STRING_ARG(jobKey));
  285. auto insertion = m_stats.insert(newStatKey);
  286. StatsEntry& statToSynth = insertion.first->second;
  287. statToSynth.m_cumulativeTime += statistic.m_cumulativeTime;
  288. statToSynth.m_operationCount += statistic.m_operationCount;
  289. if (insertion.second)
  290. {
  291. allProcessJobsByJobKey.push_back(newStatKey);
  292. }
  293. }
  294. // synthesize a stat to track total processjob times:
  295. {
  296. StatsEntry& statToSynth = m_stats["ProcessJobsTotal"];
  297. statToSynth.m_cumulativeTime += statistic.m_cumulativeTime;
  298. statToSynth.m_operationCount += statistic.m_operationCount;
  299. }
  300. }
  301. else if (AZ::StringFunc::StartsWith(statKey, "HashFile,", true))
  302. {
  303. allHashFiles.push_back(statKey);
  304. // processjob has the format ProcessJob,sourcename,jobkey,platformname
  305. // synthesize a stat to track total hash times:
  306. StatsEntry& statToSynth = m_stats["HashFileTotal"];
  307. statToSynth.m_cumulativeTime += statistic.m_cumulativeTime;
  308. statToSynth.m_operationCount += statistic.m_operationCount;
  309. }
  310. }
  311. StatsEntry& gemLoadStat = m_stats["LoadingModules"];
  312. PrintStat("LoadingGems", gemLoadStat.m_cumulativeTime, 1);
  313. // analysis-related stats
  314. StatsEntry& totalScanTime = m_stats["AssetScanning"];
  315. PrintStat("AssetScanning", totalScanTime.m_cumulativeTime, totalScanTime.m_operationCount);
  316. StatsEntry& cacheWarmTime = m_stats["WarmingFileCache"];
  317. PrintStat("WarmingFileCache", cacheWarmTime.m_cumulativeTime, cacheWarmTime.m_operationCount);
  318. StatsEntry& assessTime = m_stats["InitialFileAssessment"];
  319. PrintStat("InitialFileAssessment", assessTime.m_cumulativeTime, assessTime.m_operationCount);
  320. StatsEntry& totalHashTime = m_stats["HashFileTotal"];
  321. PrintStat("HashFileTotal", totalHashTime.m_cumulativeTime, totalHashTime.m_operationCount);
  322. PrintStatsArray(allHashFiles, maxIndividualStats, "longest individual file hashes:");
  323. // CreateJobs stats
  324. StatsEntry& totalCreateJobs = m_stats["CreateJobsTotal"];
  325. if (totalCreateJobs.m_operationCount)
  326. {
  327. PrintStat("CreateJobsTotal", totalCreateJobs.m_cumulativeTime, totalCreateJobs.m_operationCount);
  328. PrintStatsArray(allCreateJobs, maxIndividualStats, "longest individual CreateJobs");
  329. PrintStatsArray(allCreateJobsByBuilder, maxCumulativeStats, "longest CreateJobs By builder");
  330. }
  331. // ProcessJobs stats
  332. StatsEntry& totalProcessJobs = m_stats["ProcessJobsTotal"];
  333. if (totalProcessJobs.m_operationCount)
  334. {
  335. PrintStat("ProcessJobsTotal", totalProcessJobs.m_cumulativeTime, totalProcessJobs.m_operationCount);
  336. PrintStatsArray(allProcessJobs, maxIndividualStats, "longest individual ProcessJob");
  337. PrintStatsArray(allProcessJobsByJobKey, maxCumulativeStats, "cumulative time spent in ProcessJob by JobKey");
  338. PrintStatsArray(allProcessJobsByPlatform, maxCumulativeStats, "cumulative time spent in ProcessJob by Platform");
  339. }
  340. duration costToGenerateStats = AZStd::chrono::duration_cast<duration>(AZStd::chrono::steady_clock::now() - startTimeStamp);
  341. PrintStat("ComputeStatsTime", costToGenerateStats, 1);
  342. }
  343. // Public interface:
  344. static StatsCaptureImpl* g_instance = nullptr;
  345. //! call this one time before capturing stats.
  346. void Initialize()
  347. {
  348. if (g_instance)
  349. {
  350. AZ_Assert(false, "An instance of StatsCaptureImpl already exists.");
  351. return;
  352. }
  353. g_instance = aznew StatsCaptureImpl();
  354. }
  355. //! Call this one time as part of shutting down.
  356. //! note that while it is an error to double-initialize, it is intentionally
  357. //! not an error to call any other function when uninitialized, allowing this system
  358. //! to essentially be "turned off" just by not initializing it in the first place.
  359. void Shutdown()
  360. {
  361. if (g_instance)
  362. {
  363. delete g_instance;
  364. g_instance = nullptr;
  365. }
  366. }
  367. //! Start the clock running for a particular stat name.
  368. void BeginCaptureStat(AZStd::string_view statName)
  369. {
  370. if (g_instance)
  371. {
  372. g_instance->BeginCaptureStat(statName);
  373. }
  374. }
  375. //! Stop the clock running for a particular stat name. If persistToDb is true and the returned duration is valid, write the duration
  376. //! to asset database Stats table.
  377. AZStd::optional<AZStd::sys_time_t> EndCaptureStat(AZStd::string_view statName, bool persistToDb)
  378. {
  379. if (g_instance)
  380. {
  381. return g_instance->EndCaptureStat(statName, persistToDb);
  382. }
  383. return AZStd::optional<AZStd::sys_time_t>();
  384. }
  385. //! Do additional processing and then write the cumulative stats to log.
  386. //! Note that since this is an AP-specific system, the analysis done in the dump function
  387. //! is going to make a lot of assumptions about the way the data is encoded.
  388. void Dump()
  389. {
  390. if (g_instance)
  391. {
  392. g_instance->Dump();
  393. }
  394. }
  395. }
  396. }