blob: cca538c3d2596eadc8e87dfb8a84865a712a363f [file] [log] [blame]
Chris Lattner6dad11f2002-10-01 19:36:54 +00001//===-- Timer.cpp - Interval Timing Support -------------------------------===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman10468d82005-04-21 22:55:34 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner6dad11f2002-10-01 19:36:54 +00009//
Matthias Braunac287032016-10-14 00:17:19 +000010/// \file Interval Timing implementation.
Chris Lattner6dad11f2002-10-01 19:36:54 +000011//
12//===----------------------------------------------------------------------===//
13
Reid Spencer7c16caa2004-09-01 22:55:40 +000014#include "llvm/Support/Timer.h"
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000015#include "llvm/ADT/Statistic.h"
Chris Lattner707431c2010-03-30 04:03:22 +000016#include "llvm/ADT/StringMap.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/Support/CommandLine.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000018#include "llvm/Support/FileSystem.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Support/Format.h"
20#include "llvm/Support/ManagedStatic.h"
Zachary Turnerccbf3d02014-06-16 22:49:41 +000021#include "llvm/Support/Mutex.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/Support/Process.h"
23#include "llvm/Support/raw_ostream.h"
Chris Lattnerdd978ce2003-12-14 21:27:33 +000024using namespace llvm;
Chris Lattnerb0e59582003-05-09 20:05:44 +000025
Matthias Braunac287032016-10-14 00:17:19 +000026// This ugly hack is brought to you courtesy of constructor/destructor ordering
27// being unspecified by C++. Basically the problem is that a Statistic object
28// gets destroyed, which ends up calling 'GetLibSupportInfoOutputFile()'
29// (below), which calls this function. LibSupportInfoOutputFilename used to be
30// a global variable, but sometimes it would get destroyed before the Statistic,
31// causing havoc to ensue. We "fix" this by creating the string the first time
32// it is needed and never destroying it.
Chris Lattner8111c592006-10-04 21:52:35 +000033static ManagedStatic<std::string> LibSupportInfoOutputFilename;
Chris Lattnerc4bbc712003-07-31 19:38:34 +000034static std::string &getLibSupportInfoOutputFilename() {
Reid Spencer87ad6662004-12-14 03:55:21 +000035 return *LibSupportInfoOutputFilename;
Chris Lattnerc4bbc712003-07-31 19:38:34 +000036}
Chris Lattner6dad11f2002-10-01 19:36:54 +000037
Owen Andersone9b1beb2009-06-23 20:52:29 +000038static ManagedStatic<sys::SmartMutex<true> > TimerLock;
39
Chris Lattner2f752042003-01-30 23:08:50 +000040namespace {
Dan Gohmanc107d002008-04-23 23:15:23 +000041 static cl::opt<bool>
Chris Lattner2f752042003-01-30 23:08:50 +000042 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
43 "tracking (this may be slow)"),
44 cl::Hidden);
Chris Lattnerb0e59582003-05-09 20:05:44 +000045
Dan Gohmanc107d002008-04-23 23:15:23 +000046 static cl::opt<std::string, true>
Chris Lattnerf1afe32352003-08-01 22:15:15 +000047 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
Chris Lattnerb0e59582003-05-09 20:05:44 +000048 cl::desc("File to append -stats and -timer output to"),
Chris Lattnerc4bbc712003-07-31 19:38:34 +000049 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
Alexander Kornienkof00654e2015-06-23 09:49:53 +000050}
Chris Lattner2f752042003-01-30 23:08:50 +000051
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000052std::unique_ptr<raw_fd_ostream> llvm::CreateInfoOutputFile() {
Chris Lattner39e56a02010-03-30 05:34:02 +000053 const std::string &OutputFilename = getLibSupportInfoOutputFilename();
54 if (OutputFilename.empty())
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000055 return llvm::make_unique<raw_fd_ostream>(2, false); // stderr.
Chris Lattner39e56a02010-03-30 05:34:02 +000056 if (OutputFilename == "-")
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000057 return llvm::make_unique<raw_fd_ostream>(1, false); // stdout.
58
Dan Gohman744c96d2010-05-19 01:21:34 +000059 // Append mode is used because the info output file is opened and closed
60 // each time -stats or -time-passes wants to print output to it. To
61 // compensate for this, the test-suite Makefiles have code to delete the
62 // info output file before running commands which write to it.
Rafael Espindola3fd1e992014-08-25 18:16:47 +000063 std::error_code EC;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000064 auto Result = llvm::make_unique<raw_fd_ostream>(
65 OutputFilename, EC, sys::fs::F_Append | sys::fs::F_Text);
Rafael Espindola3fd1e992014-08-25 18:16:47 +000066 if (!EC)
Chris Lattnerdcd7f922010-03-29 21:34:06 +000067 return Result;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000068
Chris Lattnerdcd7f922010-03-29 21:34:06 +000069 errs() << "Error opening info-output-file '"
Chris Lattner39e56a02010-03-30 05:34:02 +000070 << OutputFilename << " for appending!\n";
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000071 return llvm::make_unique<raw_fd_ostream>(2, false); // stderr.
Chris Lattnerdcd7f922010-03-29 21:34:06 +000072}
73
74
Craig Topperc10719f2014-04-07 04:17:22 +000075static TimerGroup *DefaultTimerGroup = nullptr;
Chris Lattner6dad11f2002-10-01 19:36:54 +000076static TimerGroup *getDefaultTimerGroup() {
Chris Lattnerfafa57a2010-03-29 20:35:01 +000077 TimerGroup *tmp = DefaultTimerGroup;
Benjamin Kramer17388a62014-03-03 18:02:34 +000078 sys::MemoryFence();
Chris Lattnerfafa57a2010-03-29 20:35:01 +000079 if (tmp) return tmp;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +000080
Zachary Turner6ad24442014-06-19 16:17:42 +000081 sys::SmartScopedLock<true> Lock(*TimerLock);
Chris Lattnerfafa57a2010-03-29 20:35:01 +000082 tmp = DefaultTimerGroup;
Owen Anderson4ed41c82009-06-23 17:33:37 +000083 if (!tmp) {
Matthias Braun9f15a792016-11-18 19:43:18 +000084 tmp = new TimerGroup("misc", "Miscellaneous Ungrouped Timers");
Benjamin Kramer17388a62014-03-03 18:02:34 +000085 sys::MemoryFence();
Chris Lattnerfafa57a2010-03-29 20:35:01 +000086 DefaultTimerGroup = tmp;
Owen Anderson4ed41c82009-06-23 17:33:37 +000087 }
Mikhail Glushenkov358607d2009-11-07 06:33:12 +000088
Owen Anderson4ed41c82009-06-23 17:33:37 +000089 return tmp;
Chris Lattner6dad11f2002-10-01 19:36:54 +000090}
91
Chris Lattnerfafa57a2010-03-29 20:35:01 +000092//===----------------------------------------------------------------------===//
93// Timer Implementation
94//===----------------------------------------------------------------------===//
95
Matthias Braun9f15a792016-11-18 19:43:18 +000096void Timer::init(StringRef Name, StringRef Description) {
97 init(Name, Description, *getDefaultTimerGroup());
Chris Lattner6dad11f2002-10-01 19:36:54 +000098}
99
Matthias Braun9f15a792016-11-18 19:43:18 +0000100void Timer::init(StringRef Name, StringRef Description, TimerGroup &tg) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000101 assert(!TG && "Timer already initialized");
Matthias Braun9f15a792016-11-18 19:43:18 +0000102 this->Name.assign(Name.begin(), Name.end());
103 this->Description.assign(Description.begin(), Description.end());
Vedant Kumard1675862015-12-22 17:36:17 +0000104 Running = Triggered = false;
Chris Lattner707431c2010-03-30 04:03:22 +0000105 TG = &tg;
Chris Lattner9a608d22010-03-30 04:40:01 +0000106 TG->addTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000107}
108
Chris Lattner6dad11f2002-10-01 19:36:54 +0000109Timer::~Timer() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000110 if (!TG) return; // Never initialized, or already cleared.
Chris Lattner9a608d22010-03-30 04:40:01 +0000111 TG->removeTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000112}
113
Jeff Cohen1a26d152005-01-08 20:15:57 +0000114static inline size_t getMemUsage() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000115 if (!TrackSpace) return 0;
116 return sys::Process::GetMallocUsage();
Reid Spencerad7bdf72004-12-27 08:03:04 +0000117}
118
Chris Lattner707431c2010-03-30 04:03:22 +0000119TimeRecord TimeRecord::getCurrentTime(bool Start) {
Pavel Labath757ca882016-10-24 10:59:17 +0000120 using Seconds = std::chrono::duration<double, std::ratio<1>>;
Chris Lattner60683452004-06-07 19:34:51 +0000121 TimeRecord Result;
Pavel Labath757ca882016-10-24 10:59:17 +0000122 sys::TimePoint<> now;
123 std::chrono::nanoseconds user, sys;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000124
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000125 if (Start) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000126 Result.MemUsed = getMemUsage();
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000127 sys::Process::GetTimeUsage(now, user, sys);
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000128 } else {
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000129 sys::Process::GetTimeUsage(now, user, sys);
Chris Lattner4cd65712010-03-30 05:20:02 +0000130 Result.MemUsed = getMemUsage();
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000131 }
Reid Spencer27088812004-12-20 00:59:04 +0000132
Pavel Labath757ca882016-10-24 10:59:17 +0000133 Result.WallTime = Seconds(now.time_since_epoch()).count();
134 Result.UserTime = Seconds(user).count();
135 Result.SystemTime = Seconds(sys).count();
Chris Lattner6dad11f2002-10-01 19:36:54 +0000136 return Result;
137}
138
Chris Lattner6dad11f2002-10-01 19:36:54 +0000139void Timer::startTimer() {
Vedant Kumard1675862015-12-22 17:36:17 +0000140 assert(!Running && "Cannot start a running timer");
141 Running = Triggered = true;
142 StartTime = TimeRecord::getCurrentTime(true);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000143}
144
145void Timer::stopTimer() {
Vedant Kumard1675862015-12-22 17:36:17 +0000146 assert(Running && "Cannot stop a paused timer");
147 Running = false;
Chris Lattner707431c2010-03-30 04:03:22 +0000148 Time += TimeRecord::getCurrentTime(false);
Vedant Kumard1675862015-12-22 17:36:17 +0000149 Time -= StartTime;
150}
Chris Lattnerf96a2182002-11-18 21:47:09 +0000151
Vedant Kumard1675862015-12-22 17:36:17 +0000152void Timer::clear() {
153 Running = Triggered = false;
154 Time = StartTime = TimeRecord();
Chris Lattner6dad11f2002-10-01 19:36:54 +0000155}
156
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000157static void printVal(double Val, double Total, raw_ostream &OS) {
Chris Lattner5092a6d2010-03-29 20:40:19 +0000158 if (Total < 1e-7) // Avoid dividing by zero.
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000159 OS << " ----- ";
Benjamin Kramercc863b22011-10-16 16:30:34 +0000160 else
161 OS << format(" %7.4f (%5.1f%%)", Val, Val*100/Total);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000162}
163
Chris Lattner707431c2010-03-30 04:03:22 +0000164void TimeRecord::print(const TimeRecord &Total, raw_ostream &OS) const {
165 if (Total.getUserTime())
166 printVal(getUserTime(), Total.getUserTime(), OS);
167 if (Total.getSystemTime())
168 printVal(getSystemTime(), Total.getSystemTime(), OS);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000169 if (Total.getProcessTime())
170 printVal(getProcessTime(), Total.getProcessTime(), OS);
Chris Lattner707431c2010-03-30 04:03:22 +0000171 printVal(getWallTime(), Total.getWallTime(), OS);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000172
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000173 OS << " ";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000174
Chris Lattner707431c2010-03-30 04:03:22 +0000175 if (Total.getMemUsed())
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000176 OS << format("%9" PRId64 " ", (int64_t)getMemUsed());
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000177}
178
179
Chris Lattner8bfda652003-10-06 15:02:31 +0000180//===----------------------------------------------------------------------===//
181// NamedRegionTimer Implementation
182//===----------------------------------------------------------------------===//
183
Dan Gohmanb29cda92010-04-15 17:08:50 +0000184namespace {
185
Chris Lattner707431c2010-03-30 04:03:22 +0000186typedef StringMap<Timer> Name2TimerMap;
Chris Lattner4cd65712010-03-30 05:20:02 +0000187
188class Name2PairMap {
189 StringMap<std::pair<TimerGroup*, Name2TimerMap> > Map;
190public:
191 ~Name2PairMap() {
192 for (StringMap<std::pair<TimerGroup*, Name2TimerMap> >::iterator
193 I = Map.begin(), E = Map.end(); I != E; ++I)
194 delete I->second.first;
195 }
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000196
Matthias Braun9f15a792016-11-18 19:43:18 +0000197 Timer &get(StringRef Name, StringRef Description, StringRef GroupName,
198 StringRef GroupDescription) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000199 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000200
Chris Lattner4cd65712010-03-30 05:20:02 +0000201 std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName];
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000202
Chris Lattner4cd65712010-03-30 05:20:02 +0000203 if (!GroupEntry.first)
Matthias Braun9f15a792016-11-18 19:43:18 +0000204 GroupEntry.first = new TimerGroup(GroupName, GroupDescription);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000205
Chris Lattner4cd65712010-03-30 05:20:02 +0000206 Timer &T = GroupEntry.second[Name];
207 if (!T.isInitialized())
Matthias Braun9f15a792016-11-18 19:43:18 +0000208 T.init(Name, Description, *GroupEntry.first);
Chris Lattner4cd65712010-03-30 05:20:02 +0000209 return T;
210 }
211};
Dan Gohmanadec96f2008-07-14 18:19:29 +0000212
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000213}
Dan Gohmanb29cda92010-04-15 17:08:50 +0000214
Chris Lattner707431c2010-03-30 04:03:22 +0000215static ManagedStatic<Name2PairMap> NamedGroupedTimers;
Chris Lattner8bfda652003-10-06 15:02:31 +0000216
Matthias Braun9f15a792016-11-18 19:43:18 +0000217NamedRegionTimer::NamedRegionTimer(StringRef Name, StringRef Description,
218 StringRef GroupName,
219 StringRef GroupDescription, bool Enabled)
220 : TimeRegion(!Enabled ? nullptr
221 : &NamedGroupedTimers->get(Name, Description, GroupName,
222 GroupDescription)) {}
Chris Lattnerf96a2182002-11-18 21:47:09 +0000223
Chris Lattner6dad11f2002-10-01 19:36:54 +0000224//===----------------------------------------------------------------------===//
225// TimerGroup Implementation
226//===----------------------------------------------------------------------===//
227
Matthias Braunac287032016-10-14 00:17:19 +0000228/// This is the global list of TimerGroups, maintained by the TimerGroup
229/// ctor/dtor and is protected by the TimerLock lock.
Craig Topperc10719f2014-04-07 04:17:22 +0000230static TimerGroup *TimerGroupList = nullptr;
Chris Lattner90fe73d2010-03-30 05:27:58 +0000231
Matthias Braun9f15a792016-11-18 19:43:18 +0000232TimerGroup::TimerGroup(StringRef Name, StringRef Description)
233 : Name(Name.begin(), Name.end()),
234 Description(Description.begin(), Description.end()) {
Chris Lattner90fe73d2010-03-30 05:27:58 +0000235 // Add the group to TimerGroupList.
236 sys::SmartScopedLock<true> L(*TimerLock);
237 if (TimerGroupList)
238 TimerGroupList->Prev = &Next;
239 Next = TimerGroupList;
240 Prev = &TimerGroupList;
241 TimerGroupList = this;
242}
243
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000244TimerGroup::~TimerGroup() {
245 // If the timer group is destroyed before the timers it owns, accumulate and
246 // print the timing data.
Craig Topper8d399f82014-04-09 04:20:00 +0000247 while (FirstTimer)
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000248 removeTimer(*FirstTimer);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000249
Chris Lattner90fe73d2010-03-30 05:27:58 +0000250 // Remove the group from the TimerGroupList.
251 sys::SmartScopedLock<true> L(*TimerLock);
252 *Prev = Next;
253 if (Next)
254 Next->Prev = Prev;
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000255}
256
257
Chris Lattner9a608d22010-03-30 04:40:01 +0000258void TimerGroup::removeTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000259 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000260
Chris Lattner9a608d22010-03-30 04:40:01 +0000261 // If the timer was started, move its data to TimersToPrint.
Vedant Kumard1675862015-12-22 17:36:17 +0000262 if (T.hasTriggered())
Matthias Braun9f15a792016-11-18 19:43:18 +0000263 TimersToPrint.emplace_back(T.Time, T.Description);
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000264
Craig Topperc10719f2014-04-07 04:17:22 +0000265 T.TG = nullptr;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000266
Chris Lattner9a608d22010-03-30 04:40:01 +0000267 // Unlink the timer from our list.
268 *T.Prev = T.Next;
269 if (T.Next)
270 T.Next->Prev = T.Prev;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000271
Chris Lattner9a608d22010-03-30 04:40:01 +0000272 // Print the report when all timers in this group are destroyed if some of
273 // them were started.
Craig Topper8d399f82014-04-09 04:20:00 +0000274 if (FirstTimer || TimersToPrint.empty())
Chris Lattner9a608d22010-03-30 04:40:01 +0000275 return;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +0000276
277 std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
Chris Lattner9a608d22010-03-30 04:40:01 +0000278 PrintQueuedTimers(*OutStream);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000279}
Brian Gaeke960707c2003-11-11 22:41:34 +0000280
Chris Lattner9a608d22010-03-30 04:40:01 +0000281void TimerGroup::addTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000282 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000283
Chris Lattner9a608d22010-03-30 04:40:01 +0000284 // Add the timer to our list.
285 if (FirstTimer)
286 FirstTimer->Prev = &T.Next;
287 T.Next = FirstTimer;
288 T.Prev = &FirstTimer;
289 FirstTimer = &T;
Owen Andersone9b1beb2009-06-23 20:52:29 +0000290}
291
Chris Lattner9a608d22010-03-30 04:40:01 +0000292void TimerGroup::PrintQueuedTimers(raw_ostream &OS) {
293 // Sort the timers in descending order by amount of time taken.
Benjamin Kramera7d0ccf2010-08-07 13:27:41 +0000294 std::sort(TimersToPrint.begin(), TimersToPrint.end());
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000295
Chris Lattner9a608d22010-03-30 04:40:01 +0000296 TimeRecord Total;
Vedant Kumar11dc6dc2015-12-21 23:41:38 +0000297 for (auto &RecordNamePair : TimersToPrint)
298 Total += RecordNamePair.first;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000299
Chris Lattner9a608d22010-03-30 04:40:01 +0000300 // Print out timing header.
301 OS << "===" << std::string(73, '-') << "===\n";
302 // Figure out how many spaces to indent TimerGroup name.
Matthias Braun9f15a792016-11-18 19:43:18 +0000303 unsigned Padding = (80-Description.length())/2;
Chris Lattner9a608d22010-03-30 04:40:01 +0000304 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
Matthias Braun9f15a792016-11-18 19:43:18 +0000305 OS.indent(Padding) << Description << '\n';
Chris Lattner9a608d22010-03-30 04:40:01 +0000306 OS << "===" << std::string(73, '-') << "===\n";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000307
Chris Lattner9a608d22010-03-30 04:40:01 +0000308 // If this is not an collection of ungrouped times, print the total time.
309 // Ungrouped timers don't really make sense to add up. We still print the
310 // TOTAL line to make the percentages make sense.
Benjamin Kramercc863b22011-10-16 16:30:34 +0000311 if (this != DefaultTimerGroup)
312 OS << format(" Total Execution Time: %5.4f seconds (%5.4f wall clock)\n",
313 Total.getProcessTime(), Total.getWallTime());
Chris Lattner9a608d22010-03-30 04:40:01 +0000314 OS << '\n';
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000315
Chris Lattner9a608d22010-03-30 04:40:01 +0000316 if (Total.getUserTime())
317 OS << " ---User Time---";
318 if (Total.getSystemTime())
319 OS << " --System Time--";
320 if (Total.getProcessTime())
321 OS << " --User+System--";
322 OS << " ---Wall Time---";
323 if (Total.getMemUsed())
324 OS << " ---Mem---";
325 OS << " --- Name ---\n";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000326
Chris Lattner9a608d22010-03-30 04:40:01 +0000327 // Loop through all of the timing data, printing it out.
328 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i) {
329 const std::pair<TimeRecord, std::string> &Entry = TimersToPrint[e-i-1];
330 Entry.first.print(Total, OS);
331 OS << Entry.second << '\n';
332 }
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000333
Chris Lattner9a608d22010-03-30 04:40:01 +0000334 Total.print(Total, OS);
335 OS << "Total\n\n";
336 OS.flush();
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000337
Chris Lattner9a608d22010-03-30 04:40:01 +0000338 TimersToPrint.clear();
Owen Andersone9b1beb2009-06-23 20:52:29 +0000339}
340
Chris Lattner4cd65712010-03-30 05:20:02 +0000341void TimerGroup::print(raw_ostream &OS) {
342 sys::SmartScopedLock<true> L(*TimerLock);
343
344 // See if any of our timers were started, if so add them to TimersToPrint and
345 // reset them.
346 for (Timer *T = FirstTimer; T; T = T->Next) {
Vedant Kumard1675862015-12-22 17:36:17 +0000347 if (!T->hasTriggered()) continue;
Matthias Braun9f15a792016-11-18 19:43:18 +0000348 TimersToPrint.emplace_back(T->Time, T->Description);
Matthias Braunac287032016-10-14 00:17:19 +0000349
Chris Lattner4cd65712010-03-30 05:20:02 +0000350 // Clear out the time.
Vedant Kumard1675862015-12-22 17:36:17 +0000351 T->clear();
Chris Lattner4cd65712010-03-30 05:20:02 +0000352 }
353
354 // If any timers were started, print the group.
355 if (!TimersToPrint.empty())
356 PrintQueuedTimers(OS);
357}
Chris Lattner90fe73d2010-03-30 05:27:58 +0000358
Chris Lattner90fe73d2010-03-30 05:27:58 +0000359void TimerGroup::printAll(raw_ostream &OS) {
360 sys::SmartScopedLock<true> L(*TimerLock);
361
362 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
363 TG->print(OS);
364}