Timer.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. //===-- Timer.cpp - Interval Timing Support -------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. /// \file Interval Timing implementation.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Support/Timer.h"
  13. #include "DebugOptions.h"
  14. #include "llvm/ADT/Statistic.h"
  15. #include "llvm/ADT/StringMap.h"
  16. #include "llvm/Config/config.h"
  17. #include "llvm/Support/CommandLine.h"
  18. #include "llvm/Support/FileSystem.h"
  19. #include "llvm/Support/Format.h"
  20. #include "llvm/Support/ManagedStatic.h"
  21. #include "llvm/Support/Mutex.h"
  22. #include "llvm/Support/Process.h"
  23. #include "llvm/Support/Signposts.h"
  24. #include "llvm/Support/YAMLTraits.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. #include <limits>
  27. #if HAVE_UNISTD_H
  28. #include <unistd.h>
  29. #endif
  30. #ifdef HAVE_PROC_PID_RUSAGE
  31. #include <libproc.h>
  32. #endif
  33. using namespace llvm;
  34. // This ugly hack is brought to you courtesy of constructor/destructor ordering
  35. // being unspecified by C++. Basically the problem is that a Statistic object
  36. // gets destroyed, which ends up calling 'GetLibSupportInfoOutputFile()'
  37. // (below), which calls this function. LibSupportInfoOutputFilename used to be
  38. // a global variable, but sometimes it would get destroyed before the Statistic,
  39. // causing havoc to ensue. We "fix" this by creating the string the first time
  40. // it is needed and never destroying it.
  41. static ManagedStatic<std::string> LibSupportInfoOutputFilename;
  42. static std::string &getLibSupportInfoOutputFilename() {
  43. return *LibSupportInfoOutputFilename;
  44. }
  45. static ManagedStatic<sys::SmartMutex<true> > TimerLock;
  46. /// Allows llvm::Timer to emit signposts when supported.
  47. static ManagedStatic<SignpostEmitter> Signposts;
  48. namespace {
  49. struct CreateTrackSpace {
  50. static void *call() {
  51. return new cl::opt<bool>("track-memory",
  52. cl::desc("Enable -time-passes memory "
  53. "tracking (this may be slow)"),
  54. cl::Hidden);
  55. }
  56. };
  57. static ManagedStatic<cl::opt<bool>, CreateTrackSpace> TrackSpace;
  58. struct CreateInfoOutputFilename {
  59. static void *call() {
  60. return new cl::opt<std::string, true>(
  61. "info-output-file", cl::value_desc("filename"),
  62. cl::desc("File to append -stats and -timer output to"), cl::Hidden,
  63. cl::location(getLibSupportInfoOutputFilename()));
  64. }
  65. };
  66. static ManagedStatic<cl::opt<std::string, true>, CreateInfoOutputFilename>
  67. InfoOutputFilename;
  68. struct CreateSortTimers {
  69. static void *call() {
  70. return new cl::opt<bool>(
  71. "sort-timers",
  72. cl::desc("In the report, sort the timers in each group "
  73. "in wall clock time order"),
  74. cl::init(true), cl::Hidden);
  75. }
  76. };
  77. ManagedStatic<cl::opt<bool>, CreateSortTimers> SortTimers;
  78. } // namespace
  79. void llvm::initTimerOptions() {
  80. *TrackSpace;
  81. *InfoOutputFilename;
  82. *SortTimers;
  83. }
  84. std::unique_ptr<raw_fd_ostream> llvm::CreateInfoOutputFile() {
  85. const std::string &OutputFilename = getLibSupportInfoOutputFilename();
  86. if (OutputFilename.empty())
  87. return std::make_unique<raw_fd_ostream>(2, false); // stderr.
  88. if (OutputFilename == "-")
  89. return std::make_unique<raw_fd_ostream>(1, false); // stdout.
  90. // Append mode is used because the info output file is opened and closed
  91. // each time -stats or -time-passes wants to print output to it. To
  92. // compensate for this, the test-suite Makefiles have code to delete the
  93. // info output file before running commands which write to it.
  94. std::error_code EC;
  95. auto Result = std::make_unique<raw_fd_ostream>(
  96. OutputFilename, EC, sys::fs::OF_Append | sys::fs::OF_TextWithCRLF);
  97. if (!EC)
  98. return Result;
  99. errs() << "Error opening info-output-file '"
  100. << OutputFilename << " for appending!\n";
  101. return std::make_unique<raw_fd_ostream>(2, false); // stderr.
  102. }
  103. namespace {
  104. struct CreateDefaultTimerGroup {
  105. static void *call() {
  106. return new TimerGroup("misc", "Miscellaneous Ungrouped Timers");
  107. }
  108. };
  109. } // namespace
  110. static ManagedStatic<TimerGroup, CreateDefaultTimerGroup> DefaultTimerGroup;
  111. static TimerGroup *getDefaultTimerGroup() { return &*DefaultTimerGroup; }
  112. //===----------------------------------------------------------------------===//
  113. // Timer Implementation
  114. //===----------------------------------------------------------------------===//
  115. void Timer::init(StringRef TimerName, StringRef TimerDescription) {
  116. init(TimerName, TimerDescription, *getDefaultTimerGroup());
  117. }
  118. void Timer::init(StringRef TimerName, StringRef TimerDescription,
  119. TimerGroup &tg) {
  120. assert(!TG && "Timer already initialized");
  121. Name.assign(TimerName.begin(), TimerName.end());
  122. Description.assign(TimerDescription.begin(), TimerDescription.end());
  123. Running = Triggered = false;
  124. TG = &tg;
  125. TG->addTimer(*this);
  126. }
  127. Timer::~Timer() {
  128. if (!TG) return; // Never initialized, or already cleared.
  129. TG->removeTimer(*this);
  130. }
  131. static inline size_t getMemUsage() {
  132. if (!*TrackSpace)
  133. return 0;
  134. return sys::Process::GetMallocUsage();
  135. }
  136. static uint64_t getCurInstructionsExecuted() {
  137. #if defined(HAVE_UNISTD_H) && defined(HAVE_PROC_PID_RUSAGE) && \
  138. defined(RUSAGE_INFO_V4)
  139. struct rusage_info_v4 ru;
  140. if (proc_pid_rusage(getpid(), RUSAGE_INFO_V4, (rusage_info_t *)&ru) == 0) {
  141. return ru.ri_instructions;
  142. }
  143. #endif
  144. return 0;
  145. }
  146. TimeRecord TimeRecord::getCurrentTime(bool Start) {
  147. using Seconds = std::chrono::duration<double, std::ratio<1>>;
  148. TimeRecord Result;
  149. sys::TimePoint<> now;
  150. std::chrono::nanoseconds user, sys;
  151. if (Start) {
  152. Result.MemUsed = getMemUsage();
  153. Result.InstructionsExecuted = getCurInstructionsExecuted();
  154. sys::Process::GetTimeUsage(now, user, sys);
  155. } else {
  156. sys::Process::GetTimeUsage(now, user, sys);
  157. Result.InstructionsExecuted = getCurInstructionsExecuted();
  158. Result.MemUsed = getMemUsage();
  159. }
  160. Result.WallTime = Seconds(now.time_since_epoch()).count();
  161. Result.UserTime = Seconds(user).count();
  162. Result.SystemTime = Seconds(sys).count();
  163. return Result;
  164. }
  165. void Timer::startTimer() {
  166. assert(!Running && "Cannot start a running timer");
  167. Running = Triggered = true;
  168. Signposts->startInterval(this, getName());
  169. StartTime = TimeRecord::getCurrentTime(true);
  170. }
  171. void Timer::stopTimer() {
  172. assert(Running && "Cannot stop a paused timer");
  173. Running = false;
  174. Time += TimeRecord::getCurrentTime(false);
  175. Time -= StartTime;
  176. Signposts->endInterval(this, getName());
  177. }
  178. void Timer::clear() {
  179. Running = Triggered = false;
  180. Time = StartTime = TimeRecord();
  181. }
  182. static void printVal(double Val, double Total, raw_ostream &OS) {
  183. if (Total < 1e-7) // Avoid dividing by zero.
  184. OS << " ----- ";
  185. else
  186. OS << format(" %7.4f (%5.1f%%)", Val, Val*100/Total);
  187. }
  188. void TimeRecord::print(const TimeRecord &Total, raw_ostream &OS) const {
  189. if (Total.getUserTime())
  190. printVal(getUserTime(), Total.getUserTime(), OS);
  191. if (Total.getSystemTime())
  192. printVal(getSystemTime(), Total.getSystemTime(), OS);
  193. if (Total.getProcessTime())
  194. printVal(getProcessTime(), Total.getProcessTime(), OS);
  195. printVal(getWallTime(), Total.getWallTime(), OS);
  196. OS << " ";
  197. if (Total.getMemUsed())
  198. OS << format("%9" PRId64 " ", (int64_t)getMemUsed());
  199. if (Total.getInstructionsExecuted())
  200. OS << format("%9" PRId64 " ", (int64_t)getInstructionsExecuted());
  201. }
  202. //===----------------------------------------------------------------------===//
  203. // NamedRegionTimer Implementation
  204. //===----------------------------------------------------------------------===//
  205. namespace {
  206. typedef StringMap<Timer> Name2TimerMap;
  207. class Name2PairMap {
  208. StringMap<std::pair<TimerGroup*, Name2TimerMap> > Map;
  209. public:
  210. ~Name2PairMap() {
  211. for (StringMap<std::pair<TimerGroup*, Name2TimerMap> >::iterator
  212. I = Map.begin(), E = Map.end(); I != E; ++I)
  213. delete I->second.first;
  214. }
  215. Timer &get(StringRef Name, StringRef Description, StringRef GroupName,
  216. StringRef GroupDescription) {
  217. sys::SmartScopedLock<true> L(*TimerLock);
  218. std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName];
  219. if (!GroupEntry.first)
  220. GroupEntry.first = new TimerGroup(GroupName, GroupDescription);
  221. Timer &T = GroupEntry.second[Name];
  222. if (!T.isInitialized())
  223. T.init(Name, Description, *GroupEntry.first);
  224. return T;
  225. }
  226. };
  227. }
  228. static ManagedStatic<Name2PairMap> NamedGroupedTimers;
  229. NamedRegionTimer::NamedRegionTimer(StringRef Name, StringRef Description,
  230. StringRef GroupName,
  231. StringRef GroupDescription, bool Enabled)
  232. : TimeRegion(!Enabled ? nullptr
  233. : &NamedGroupedTimers->get(Name, Description, GroupName,
  234. GroupDescription)) {}
  235. //===----------------------------------------------------------------------===//
  236. // TimerGroup Implementation
  237. //===----------------------------------------------------------------------===//
  238. /// This is the global list of TimerGroups, maintained by the TimerGroup
  239. /// ctor/dtor and is protected by the TimerLock lock.
  240. static TimerGroup *TimerGroupList = nullptr;
  241. TimerGroup::TimerGroup(StringRef Name, StringRef Description)
  242. : Name(Name.begin(), Name.end()),
  243. Description(Description.begin(), Description.end()) {
  244. // Add the group to TimerGroupList.
  245. sys::SmartScopedLock<true> L(*TimerLock);
  246. if (TimerGroupList)
  247. TimerGroupList->Prev = &Next;
  248. Next = TimerGroupList;
  249. Prev = &TimerGroupList;
  250. TimerGroupList = this;
  251. }
  252. TimerGroup::TimerGroup(StringRef Name, StringRef Description,
  253. const StringMap<TimeRecord> &Records)
  254. : TimerGroup(Name, Description) {
  255. TimersToPrint.reserve(Records.size());
  256. for (const auto &P : Records)
  257. TimersToPrint.emplace_back(P.getValue(), std::string(P.getKey()),
  258. std::string(P.getKey()));
  259. assert(TimersToPrint.size() == Records.size() && "Size mismatch");
  260. }
  261. TimerGroup::~TimerGroup() {
  262. // If the timer group is destroyed before the timers it owns, accumulate and
  263. // print the timing data.
  264. while (FirstTimer)
  265. removeTimer(*FirstTimer);
  266. // Remove the group from the TimerGroupList.
  267. sys::SmartScopedLock<true> L(*TimerLock);
  268. *Prev = Next;
  269. if (Next)
  270. Next->Prev = Prev;
  271. }
  272. void TimerGroup::removeTimer(Timer &T) {
  273. sys::SmartScopedLock<true> L(*TimerLock);
  274. // If the timer was started, move its data to TimersToPrint.
  275. if (T.hasTriggered())
  276. TimersToPrint.emplace_back(T.Time, T.Name, T.Description);
  277. T.TG = nullptr;
  278. // Unlink the timer from our list.
  279. *T.Prev = T.Next;
  280. if (T.Next)
  281. T.Next->Prev = T.Prev;
  282. // Print the report when all timers in this group are destroyed if some of
  283. // them were started.
  284. if (FirstTimer || TimersToPrint.empty())
  285. return;
  286. std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
  287. PrintQueuedTimers(*OutStream);
  288. }
  289. void TimerGroup::addTimer(Timer &T) {
  290. sys::SmartScopedLock<true> L(*TimerLock);
  291. // Add the timer to our list.
  292. if (FirstTimer)
  293. FirstTimer->Prev = &T.Next;
  294. T.Next = FirstTimer;
  295. T.Prev = &FirstTimer;
  296. FirstTimer = &T;
  297. }
  298. void TimerGroup::PrintQueuedTimers(raw_ostream &OS) {
  299. // Perhaps sort the timers in descending order by amount of time taken.
  300. if (*SortTimers)
  301. llvm::sort(TimersToPrint);
  302. TimeRecord Total;
  303. for (const PrintRecord &Record : TimersToPrint)
  304. Total += Record.Time;
  305. // Print out timing header.
  306. OS << "===" << std::string(73, '-') << "===\n";
  307. // Figure out how many spaces to indent TimerGroup name.
  308. unsigned Padding = (80-Description.length())/2;
  309. if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
  310. OS.indent(Padding) << Description << '\n';
  311. OS << "===" << std::string(73, '-') << "===\n";
  312. // If this is not an collection of ungrouped times, print the total time.
  313. // Ungrouped timers don't really make sense to add up. We still print the
  314. // TOTAL line to make the percentages make sense.
  315. if (this != getDefaultTimerGroup())
  316. OS << format(" Total Execution Time: %5.4f seconds (%5.4f wall clock)\n",
  317. Total.getProcessTime(), Total.getWallTime());
  318. OS << '\n';
  319. if (Total.getUserTime())
  320. OS << " ---User Time---";
  321. if (Total.getSystemTime())
  322. OS << " --System Time--";
  323. if (Total.getProcessTime())
  324. OS << " --User+System--";
  325. OS << " ---Wall Time---";
  326. if (Total.getMemUsed())
  327. OS << " ---Mem---";
  328. if (Total.getInstructionsExecuted())
  329. OS << " ---Instr---";
  330. OS << " --- Name ---\n";
  331. // Loop through all of the timing data, printing it out.
  332. for (const PrintRecord &Record : llvm::reverse(TimersToPrint)) {
  333. Record.Time.print(Total, OS);
  334. OS << Record.Description << '\n';
  335. }
  336. Total.print(Total, OS);
  337. OS << "Total\n\n";
  338. OS.flush();
  339. TimersToPrint.clear();
  340. }
  341. void TimerGroup::prepareToPrintList(bool ResetTime) {
  342. // See if any of our timers were started, if so add them to TimersToPrint.
  343. for (Timer *T = FirstTimer; T; T = T->Next) {
  344. if (!T->hasTriggered()) continue;
  345. bool WasRunning = T->isRunning();
  346. if (WasRunning)
  347. T->stopTimer();
  348. TimersToPrint.emplace_back(T->Time, T->Name, T->Description);
  349. if (ResetTime)
  350. T->clear();
  351. if (WasRunning)
  352. T->startTimer();
  353. }
  354. }
  355. void TimerGroup::print(raw_ostream &OS, bool ResetAfterPrint) {
  356. {
  357. // After preparing the timers we can free the lock
  358. sys::SmartScopedLock<true> L(*TimerLock);
  359. prepareToPrintList(ResetAfterPrint);
  360. }
  361. // If any timers were started, print the group.
  362. if (!TimersToPrint.empty())
  363. PrintQueuedTimers(OS);
  364. }
  365. void TimerGroup::clear() {
  366. sys::SmartScopedLock<true> L(*TimerLock);
  367. for (Timer *T = FirstTimer; T; T = T->Next)
  368. T->clear();
  369. }
  370. void TimerGroup::printAll(raw_ostream &OS) {
  371. sys::SmartScopedLock<true> L(*TimerLock);
  372. for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
  373. TG->print(OS);
  374. }
  375. void TimerGroup::clearAll() {
  376. sys::SmartScopedLock<true> L(*TimerLock);
  377. for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
  378. TG->clear();
  379. }
  380. void TimerGroup::printJSONValue(raw_ostream &OS, const PrintRecord &R,
  381. const char *suffix, double Value) {
  382. assert(yaml::needsQuotes(Name) == yaml::QuotingType::None &&
  383. "TimerGroup name should not need quotes");
  384. assert(yaml::needsQuotes(R.Name) == yaml::QuotingType::None &&
  385. "Timer name should not need quotes");
  386. constexpr auto max_digits10 = std::numeric_limits<double>::max_digits10;
  387. OS << "\t\"time." << Name << '.' << R.Name << suffix
  388. << "\": " << format("%.*e", max_digits10 - 1, Value);
  389. }
  390. const char *TimerGroup::printJSONValues(raw_ostream &OS, const char *delim) {
  391. sys::SmartScopedLock<true> L(*TimerLock);
  392. prepareToPrintList(false);
  393. for (const PrintRecord &R : TimersToPrint) {
  394. OS << delim;
  395. delim = ",\n";
  396. const TimeRecord &T = R.Time;
  397. printJSONValue(OS, R, ".wall", T.getWallTime());
  398. OS << delim;
  399. printJSONValue(OS, R, ".user", T.getUserTime());
  400. OS << delim;
  401. printJSONValue(OS, R, ".sys", T.getSystemTime());
  402. if (T.getMemUsed()) {
  403. OS << delim;
  404. printJSONValue(OS, R, ".mem", T.getMemUsed());
  405. }
  406. if (T.getInstructionsExecuted()) {
  407. OS << delim;
  408. printJSONValue(OS, R, ".instr", T.getInstructionsExecuted());
  409. }
  410. }
  411. TimersToPrint.clear();
  412. return delim;
  413. }
  414. const char *TimerGroup::printAllJSONValues(raw_ostream &OS, const char *delim) {
  415. sys::SmartScopedLock<true> L(*TimerLock);
  416. for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
  417. delim = TG->printJSONValues(OS, delim);
  418. return delim;
  419. }
  420. void TimerGroup::ConstructTimerLists() {
  421. (void)*NamedGroupedTimers;
  422. }
  423. std::unique_ptr<TimerGroup> TimerGroup::aquireDefaultGroup() {
  424. return std::unique_ptr<TimerGroup>(DefaultTimerGroup.claim());
  425. }