Skip to content

Commit 7e454aa

Browse files
committed
Make file paths more testable by making them relative to a given path
1 parent 7f647db commit 7e454aa

15 files changed

Lines changed: 276 additions & 33 deletions

haml/filetable.hamljl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
%tr
1010
%td= $report.traces_by_file[file].exclusive |> fmtcount($report.tracecount)
1111
%td
12-
%a(href=outputfilename(file))= file
12+
%a(href=outputfilename($report, file))= relpath($report, file)
1313
-# vim: set filetype=haml:

haml/flamegraphitem.hamljl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
= pt.containing_function.name
1414
- @surround("(", ")") do
1515
= cnt |> fmtcount($report.tracecount)
16-
%a(target="_top", var"xlink:href"=href(pt.containing_function))
16+
%a(target="_top", var"xlink:href"=href($report, pt.containing_function))
1717
%rect(x=$x[],
1818
y=$y,
1919
width=width,

haml/methodtable.hamljl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
%td= $report.traces_by_function[fn].exclusive |> fmtcount($report.tracecount)
1212
%td= $report.traces_by_function[fn].inclusive |> fmtcount($report.tracecount)
1313
%td
14-
%a(href=href(fn))= fn.name
14+
%a(href=href($report, fn))= fn.name
1515
-# vim: set filetype=haml:

haml/sourcefile.hamljl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@
3131
= cnt.exclusive |> fmtcount(total, " (ex.),")
3232
= cnt.inclusive |> fmtcount(total, " (incl.)")
3333
when called from $(callsite.containing_function.name)
34-
%a(href=href(callsite)) line $(callsite.point.line)
34+
%a(href=href($report, callsite)) line $(callsite.point.line)
3535
%br/
3636
- if (callees = get($report.callees, point, nothing)) |> !isnothing
3737
.callees
3838
- total = sum(values(callees))
3939
- for (callee, cnt) in pairs(callees)
4040
= cnt |> fmtcount(total)
4141
samples spent calling
42-
%a(href=href(callee))= callee.name
42+
%a(href=href($report, callee))= callee.name
4343
%br/
4444
%span.code= code
4545

haml/statprofiler.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,4 @@ td {
7676
.code {
7777
font-family: monospace;
7878
white-space: pre;
79-
}
79+
}

src/HTML.jl

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module HTML
22

3+
34
import SHA: sha1
45
import Dates: format, RFC1123Format
56
import StableRNGs: StableRNG
@@ -24,22 +25,24 @@ includehaml(@__MODULE__,
2425
:render_flamegraphitem => templatefile("flamegraphitem.hamljl"),
2526
)
2627

27-
outputfilename(::Nothing) = ""
28-
outputfilename(sourcefile::Symbol) = begin
29-
b = basename(string(sourcefile))
30-
x = bytes2hex(sha1(String(sourcefile)))
28+
outputfilename(::Report, ::Nothing) = ""
29+
outputfilename(r::Report, sourcefile::Symbol) = begin
30+
s = string(sourcefile)
31+
b = basename(s)
32+
r = relpath(s, r.startpath)
33+
x = bytes2hex(sha1(r))
3134
b = first(b, MAX_OUTPUT_FILE_NAME_LENGTH - length(x) - length("-.html"))
3235
return "$b-$x.html"
3336
end
3437

35-
href(pt::FunctionPoint) = begin
36-
fn = outputfilename(pt.point.file)
38+
href(r::Report, pt::FunctionPoint) = begin
39+
fn = outputfilename(r, pt.point.file)
3740
anchor = "L$(pt.point.line)"
3841
return "$fn#$anchor"
3942
end
4043

41-
href(pt::TracePoint) = begin
42-
fn = outputfilename(pt.point.file)
44+
href(r::Report, pt::TracePoint) = begin
45+
fn = outputfilename(r, pt.point.file)
4346
anchor = "L$(pt.point.line)"
4447
return "$fn#$anchor"
4548
end
@@ -76,8 +79,8 @@ output(r::Report, path) = begin
7679
(LineNumberNode(i, file), code)
7780
for (i, code) in enumerate(eachline(string(file)))
7881
]
79-
lockfreeopenwrite(joinpath(path, outputfilename(file))) do io
80-
render_sourcefile(io; filename=file, lines=lines, report=r)
82+
lockfreeopenwrite(joinpath(path, outputfilename(r, file))) do io
83+
render_sourcefile(io; filename=relpath(r, file), lines=lines, report=r)
8184
end
8285
end
8386
end

src/Reports.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function find_source_file(file)
2525
!isnothing(res) && isfile(res) && return res
2626
# try to translate build bot directory to local source
2727
res = replace(file, r".*?[\\/]usr[\\/]share[\\/]julia[\\/]stdlib" => joinpath(Sys.BINDIR, Base.DATAROOTDIR, "julia", "stdlib"))
28-
isfile(res) ? normpath(res) : nothing
28+
isfile(res) ? res : nothing
2929
end
3030

3131
TracePoint(frame::StackFrame) = begin
@@ -70,6 +70,7 @@ mutable struct Report{Dict1, Dict2, Dict3, Dict4, Dict5, Dict6, FlameGraph}
7070
flamegraph :: FlameGraph
7171
maxdepth :: Int
7272
generated_on :: DateTime
73+
startpath :: String
7374
end
7475

7576
default4() = TraceCounts()
@@ -78,7 +79,7 @@ default1() = DefaultDict{TracePoint, TraceCounts}(default4)
7879
default2() = DefaultDict{FunctionPoint, Int}(default0)
7980
default3() = Symbol("#error: no name#")
8081

81-
Report(flamegraph, generated_on) = Report(
82+
Report(flamegraph, generated_on, startpath) = Report(
8283
DefaultDict{LineNumberNode, TraceCounts}(TraceCounts),
8384
DefaultDict{FunctionPoint, TraceCounts}(TraceCounts),
8485
DefaultDict{Union{Nothing, Symbol}, TraceCounts}(TraceCounts),
@@ -91,6 +92,7 @@ Report(flamegraph, generated_on) = Report(
9192
flamegraph,
9293
0,
9394
generated_on,
95+
startpath,
9496
)
9597

9698
# TODO: Handle and use metadata (threadid, taskid etc.) rather than always remove it
@@ -102,7 +104,7 @@ else
102104
const DUMMY_SEPARATOR = UInt64[0, 0]
103105
end
104106

105-
Report(data::Vector{<:Unsigned}, litrace::Dict{<:Unsigned, Vector{StackFrame}}, from_c, generated_on) = begin
107+
Report(data::Vector{<:Unsigned}, litrace::Dict{<:Unsigned, Vector{StackFrame}}, from_c, generated_on, startpath=".") = begin
106108
# point different lines of the same function to the same stack frame --
107109
# we show line-by-line info in the source files, not in the flame graph.
108110
seenfunctions = Dict{FunctionPoint, StackFrame}()
@@ -112,7 +114,7 @@ Report(data::Vector{<:Unsigned}, litrace::Dict{<:Unsigned, Vector{StackFrame}},
112114
# 32-bit support: it seems Profile is a bit undecided about whether `data`
113115
# is a Vector{UInt} or a Vector{UInt64}. flamegraph calls methods where
114116
# it _has_ to be UInt64 even on 32 bits platforms
115-
report = Report(flamegraph(UInt64.(data), lidict=merged_litrace, C=from_c), generated_on)
117+
report = Report(flamegraph(UInt64.(data), lidict=merged_litrace, C=from_c), generated_on, startpath)
116118

117119
data = _strip_data(data)
118120
data, litrace = Profile.flatten(data, litrace)
@@ -184,5 +186,6 @@ Base.sort!(r::Report) = begin
184186
return r
185187
end
186188

189+
Base.relpath(r::Report, file) = relpath(string(file), r.startpath)
187190

188191
end # module

test/fake-source.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#=
2+
A comment filling up
3+
a few lines
4+
until we reach
5+
line number
6+
8
7+
=#
8+
function helper_function()
9+
return true
10+
end
11+
12+
#=
13+
Another comment
14+
=#
15+
function main_function()
16+
return helper_function()
17+
end
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'></meta>
5+
<link rel='stylesheet' type='text/css' href='statprofiler.css'></link>
6+
<title>StatProfilerHTML - fake-source.jl</title>
7+
</head>
8+
<body>
9+
<div class='report-header'>
10+
<div class='backlink'>
11+
<a href='index.html'>Report index</a>
12+
</div>
13+
<div class='report-title'>
14+
StatProfilerHTML.jl report
15+
</div>
16+
<div class='generated-on'>
17+
Generated on Sat, 31 Dec 2022 00:00:00
18+
</div>
19+
</div>
20+
<table>
21+
<caption>File source code</caption>
22+
<thead>
23+
<tr>
24+
<th>Line</th>
25+
<th>Exclusive</th>
26+
<th>Inclusive</th>
27+
<th>Code</th>
28+
</tr>
29+
</thead>
30+
<tbody>
31+
<tr id='L1'>
32+
<td class='lineno'>1</td>
33+
<td class='tracecount excl'></td>
34+
<td class='tracecount incl'></td>
35+
<td>
36+
37+
38+
<span class='code'>#&#61;</span>
39+
</td>
40+
</tr>
41+
<tr id='L2'>
42+
<td class='lineno'>2</td>
43+
<td class='tracecount excl'></td>
44+
<td class='tracecount incl'></td>
45+
<td>
46+
47+
48+
<span class='code'>A comment filling up</span>
49+
</td>
50+
</tr>
51+
<tr id='L3'>
52+
<td class='lineno'>3</td>
53+
<td class='tracecount excl'></td>
54+
<td class='tracecount incl'></td>
55+
<td>
56+
57+
58+
<span class='code'>a few lines</span>
59+
</td>
60+
</tr>
61+
<tr id='L4'>
62+
<td class='lineno'>4</td>
63+
<td class='tracecount excl'></td>
64+
<td class='tracecount incl'></td>
65+
<td>
66+
67+
68+
<span class='code'>until we reach</span>
69+
</td>
70+
</tr>
71+
<tr id='L5'>
72+
<td class='lineno'>5</td>
73+
<td class='tracecount excl'></td>
74+
<td class='tracecount incl'></td>
75+
<td>
76+
77+
78+
<span class='code'>line number</span>
79+
</td>
80+
</tr>
81+
<tr id='L6'>
82+
<td class='lineno'>6</td>
83+
<td class='tracecount excl'></td>
84+
<td class='tracecount incl'></td>
85+
<td>
86+
87+
88+
<span class='code'>8</span>
89+
</td>
90+
</tr>
91+
<tr id='L7'>
92+
<td class='lineno'>7</td>
93+
<td class='tracecount excl'></td>
94+
<td class='tracecount incl'></td>
95+
<td>
96+
97+
98+
<span class='code'>&#61;#</span>
99+
</td>
100+
</tr>
101+
<tr id='L8'>
102+
<td class='lineno'>8</td>
103+
<td class='tracecount excl'>1 &#40;50 &#37;&#41;</td>
104+
<td class='tracecount incl'>1 &#40;50 &#37;&#41;</td>
105+
<td>
106+
<div class='call-sites'>
107+
1 &#40;50 &#37;&#41;
108+
samples spent in helper_function
109+
<br />
110+
1 &#40;100 &#37;&#41; &#40;ex.&#41;,
111+
1 &#40;100 &#37;&#41; &#40;incl.&#41;
112+
when called from main_function
113+
<a href='fake-source.jl-4c8b0079d11b2925585882f699dd2fdccff100a6.html#L15'>line 15</a>
114+
<br />
115+
</div>
116+
117+
<span class='code'>function helper_function&#40;&#41;</span>
118+
</td>
119+
</tr>
120+
<tr id='L9'>
121+
<td class='lineno'>9</td>
122+
<td class='tracecount excl'></td>
123+
<td class='tracecount incl'></td>
124+
<td>
125+
126+
127+
<span class='code'> return true</span>
128+
</td>
129+
</tr>
130+
<tr id='L10'>
131+
<td class='lineno'>10</td>
132+
<td class='tracecount excl'></td>
133+
<td class='tracecount incl'></td>
134+
<td>
135+
136+
137+
<span class='code'>end</span>
138+
</td>
139+
</tr>
140+
<tr id='L11'>
141+
<td class='lineno'>11</td>
142+
<td class='tracecount excl'></td>
143+
<td class='tracecount incl'></td>
144+
<td>
145+
146+
147+
<span class='code'></span>
148+
</td>
149+
</tr>
150+
<tr id='L12'>
151+
<td class='lineno'>12</td>
152+
<td class='tracecount excl'></td>
153+
<td class='tracecount incl'></td>
154+
<td>
155+
156+
157+
<span class='code'>#&#61;</span>
158+
</td>
159+
</tr>
160+
<tr id='L13'>
161+
<td class='lineno'>13</td>
162+
<td class='tracecount excl'></td>
163+
<td class='tracecount incl'></td>
164+
<td>
165+
166+
167+
<span class='code'>Another comment</span>
168+
</td>
169+
</tr>
170+
<tr id='L14'>
171+
<td class='lineno'>14</td>
172+
<td class='tracecount excl'></td>
173+
<td class='tracecount incl'></td>
174+
<td>
175+
176+
177+
<span class='code'>&#61;#</span>
178+
</td>
179+
</tr>
180+
<tr id='L15'>
181+
<td class='lineno'>15</td>
182+
<td class='tracecount excl'>1 &#40;50 &#37;&#41;</td>
183+
<td class='tracecount incl'>2 &#40;100 &#37;&#41;</td>
184+
<td>
185+
186+
<div class='callees'>
187+
1 &#40;100 &#37;&#41;
188+
samples spent calling
189+
<a href='fake-source.jl-4c8b0079d11b2925585882f699dd2fdccff100a6.html#L8'>helper_function</a>
190+
<br />
191+
</div>
192+
<span class='code'>function main_function&#40;&#41;</span>
193+
</td>
194+
</tr>
195+
<tr id='L16'>
196+
<td class='lineno'>16</td>
197+
<td class='tracecount excl'></td>
198+
<td class='tracecount incl'></td>
199+
<td>
200+
201+
202+
<span class='code'> return helper_function&#40;&#41;</span>
203+
</td>
204+
</tr>
205+
<tr id='L17'>
206+
<td class='lineno'>17</td>
207+
<td class='tracecount excl'></td>
208+
<td class='tracecount incl'></td>
209+
<td>
210+
211+
212+
<span class='code'>end</span>
213+
</td>
214+
</tr>
215+
</tbody>
216+
</table>
217+
</body>
218+
</html>
219+

0 commit comments

Comments
 (0)