FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
DirectXShaderCompiler/lib/Support/TimeProfiler.cpp at main · microsoft/DirectXShaderCompiler · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
microsoft
/
DirectXShaderCompiler
Public
Notifications
You must be signed in to change notification settings
Fork
897
Star
3.6k
Code
Issues
654
Pull requests
141
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
DirectXShaderCompiler
/
lib
/
Support
/
TimeProfiler.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
188 lines (161 loc) · 5.73 KB
Breadcrumbs
DirectXShaderCompiler
/
lib
/
Support
/
TimeProfiler.cpp
Copy path
File metadata and controls
188 lines (161 loc) · 5.73 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
===-- TimeProfiler.cpp - Hierarchical Time Profiler ---------------------===//
//
//
The LLVM Compiler Infrastructure
//
//
This file is distributed under the University of Illinois Open Source
//
License. See LICENSE.TXT for details.
//
//
===----------------------------------------------------------------------===//
//
//
/ \file Hierarchical time profiler implementation.
//
//
===----------------------------------------------------------------------===//
#
include
"
llvm/Support/TimeProfiler.h
"
#
include
"
llvm/ADT/StringExtras.h
"
#
include
"
llvm/Support/FileSystem.h
"
#
include
<
cassert
>
#
include
<
chrono
>
#
include
<
string
>
#
include
<
unordered_map
>
#
include
<
vector
>
using
namespace
std
::chrono
;
namespace
llvm
{
TimeTraceProfiler *TimeTraceProfilerInstance =
nullptr
;
static
std::string
escapeString
(StringRef Src) {
std::string
OS
;
for
(
const
char
&C : Src) {
switch
(C) {
case
'
"
'
:
case
'
/
'
:
case
'
\\
'
:
case
'
\b
'
:
case
'
\f
'
:
case
'
\n
'
:
case
'
\r
'
:
case
'
\t
'
:
OS
+=
'
\\
'
;
OS
+= C;
break
;
default
:
if
(
std::isprint
(C) !=
0
) {
OS
+= C;
}
}
}
return
OS
;
}
typedef
duration<steady_clock::rep, steady_clock::period> DurationType;
typedef
std::pair<std::string, DurationType> NameAndDuration;
struct
Entry
{
time_point<steady_clock> Start;
DurationType Duration;
std::string Name;
std::string Detail;
};
struct
TimeTraceProfiler
{
TimeTraceProfiler
() {
Stack.
reserve
(
8
);
Entries.
reserve
(
128
);
StartTime =
steady_clock::now
();
}
void
begin
(std::string Name, llvm::function_ref<std::string()> Detail) {
Entry E = {
steady_clock::now
(), {}, Name,
Detail
()};
Stack.
push_back
(
std::move
(E));
}
void
end
() {
assert
(!Stack.
empty
() &&
"
Must call begin() first
"
);
auto
&E = Stack.
back
();
E.
Duration
=
steady_clock::now
() - E.
Start
;
//
Only include sections longer than TimeTraceGranularity msec.
if
(duration_cast<microseconds>(E.
Duration
).
count
() > TimeTraceGranularity)
Entries.
emplace_back
(E);
//
Track total time taken by each "name", but only the topmost levels of
//
them; e.g. if there's a template instantiation that instantiates other
//
templates from within, we only want to add the topmost one. "topmost"
//
happens to be the ones that don't have any currently open entries above
//
itself.
if
(
std::find_if
(++Stack.
rbegin
(), Stack.
rend
(), [&](
const
Entry &Val) {
return
Val.
Name
== E.
Name
;
}) == Stack.
rend
()) {
TotalPerName[E.
Name
] += E.
Duration
;
CountPerName[E.
Name
]++;
}
Stack.
pop_back
();
}
void
Write
(raw_ostream &
OS
) {
assert
(Stack.
empty
() &&
"
All profiler sections should be ended when calling Write
"
);
OS
<<
"
{
\"
traceEvents
\"
: [
\n
"
;
//
Emit all events for the main flame graph.
for
(
const
auto
&E : Entries) {
auto
StartUs = duration_cast<microseconds>(E.
Start
- StartTime).
count
();
auto
DurUs = duration_cast<microseconds>(E.
Duration
).
count
();
OS
<<
"
{
\"
pid
\"
:1,
\"
tid
\"
:0,
\"
ph
\"
:
\"
X
\"
,
\"
ts
\"
:
"
<< StartUs
<<
"
,
\"
dur
\"
:
"
<< DurUs <<
"
,
\"
name
\"
:
\"
"
<<
escapeString
(E.
Name
)
<<
"
\"
,
\"
args
\"
:{
\"
detail
\"
:
\"
"
<<
escapeString
(E.
Detail
)
<<
"
\"
} },
\n
"
;
}
//
Emit totals by section name as additional "thread" events, sorted from
//
longest one.
int
Tid =
1
;
std::vector<NameAndDuration> SortedTotals;
SortedTotals.
reserve
(TotalPerName.
size
());
for
(
const
auto
&E : TotalPerName) {
SortedTotals.
push_back
(E);
}
std::sort
(SortedTotals.
begin
(), SortedTotals.
end
(),
[](
const
NameAndDuration &A,
const
NameAndDuration &B) {
return
A.
second
> B.
second
;
});
for
(
const
auto
&E : SortedTotals) {
auto
DurUs = duration_cast<microseconds>(E.
second
).
count
();
OS
<<
"
{
\"
pid
\"
:1,
\"
tid
\"
:
"
<< Tid <<
"
,
\"
ph
\"
:
\"
X
\"
,
\"
ts
\"
:
"
<<
0
<<
"
,
\"
dur
\"
:
"
<< DurUs <<
"
,
\"
name
\"
:
\"
Total
"
<<
escapeString
(E.
first
)
<<
"
\"
,
\"
args
\"
:{
\"
count
\"
:
"
<< CountPerName[E.
first
]
<<
"
,
\"
avg ms
\"
:
"
<< (DurUs / CountPerName[E.
first
] /
1000
)
<<
"
} },
\n
"
;
++Tid;
}
//
Emit metadata event with process name.
OS
<<
"
{
\"
cat
\"
:
\"\"
,
\"
pid
\"
:1,
\"
tid
\"
:0,
\"
ts
\"
:0,
\"
ph
\"
:
\"
M
\"
,
"
"
\"
name
\"
:
\"
process_name
\"
,
\"
args
\"
:{
\"
name
\"
:
\"
clang
\"
} }
\n
"
;
OS
<<
"
] }
\n
"
;
}
std::vector<Entry> Stack;
std::vector<Entry> Entries;
std::unordered_map<std::string, DurationType> TotalPerName;
std::unordered_map<std::string,
size_t
> CountPerName;
time_point<steady_clock> StartTime;
//
Minimum time granularity (in microseconds)
unsigned
TimeTraceGranularity;
};
void
timeTraceProfilerInitialize
(
unsigned
TimeTraceGranularity) {
assert
(TimeTraceProfilerInstance ==
nullptr
&&
"
Profiler should not be initialized
"
);
TimeTraceProfilerInstance =
new
TimeTraceProfiler
();
TimeTraceProfilerInstance->
TimeTraceGranularity
= TimeTraceGranularity;
}
void
timeTraceProfilerCleanup
() {
delete
TimeTraceProfilerInstance;
TimeTraceProfilerInstance =
nullptr
;
}
void
timeTraceProfilerWrite
(raw_ostream &
OS
) {
assert
(TimeTraceProfilerInstance !=
nullptr
&&
"
Profiler object can't be null
"
);
TimeTraceProfilerInstance->
Write
(
OS
);
}
void
timeTraceProfilerBegin
(StringRef Name, StringRef Detail) {
if
(TimeTraceProfilerInstance !=
nullptr
)
TimeTraceProfilerInstance->
begin
(Name, [&]() {
return
Detail; });
}
void
timeTraceProfilerBegin
(StringRef Name,
llvm::function_ref<std::string()> Detail) {
if
(TimeTraceProfilerInstance !=
nullptr
)
TimeTraceProfilerInstance->
begin
(Name, Detail);
}
void
timeTraceProfilerEnd
() {
if
(TimeTraceProfilerInstance !=
nullptr
)
TimeTraceProfilerInstance->
end
();
}
}
//
namespace llvm
Back
|
FazBrowse Home
|
New Git URL