-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Profile.jl
1159 lines (1068 loc) · 45.4 KB
/
Profile.jl
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is a part of Julia. License is MIT: https://julialang.org/license
"""
Profiling support, main entry point is the [`@profile`](@ref) macro.
"""
module Profile
import Base.StackTraces: lookup, UNKNOWN, show_spec_linfo, StackFrame
const nmeta = 4 # number of metadata fields per block (threadid, taskid, cpu_cycle_clock, thread_sleeping)
# deprecated functions: use `getdict` instead
lookup(ip::UInt) = lookup(convert(Ptr{Cvoid}, ip))
export @profile
"""
@profile
`@profile <expression>` runs your expression while taking periodic backtraces. These are
appended to an internal buffer of backtraces.
"""
macro profile(ex)
return quote
try
status = start_timer()
if status < 0
error(error_codes[status])
end
$(esc(ex))
finally
stop_timer()
end
end
end
####
#### User-level functions
####
"""
init(; n::Integer, delay::Real)
Configure the `delay` between backtraces (measured in seconds), and the number `n` of instruction pointers that may be
stored per thread. Each instruction pointer corresponds to a single line of code; backtraces generally consist of a long
list of instruction pointers. Note that 6 spaces for instruction pointers per backtrace are used to store metadata and two
NULL end markers. Current settings can be obtained by calling this function with no arguments, and each can be set independently
using keywords or in the order `(n, delay)`.
!!! compat "Julia 1.8"
As of Julia 1.8, this function allocates space for `n` instruction pointers per thread being profiled.
Previously this was `n` total.
"""
function init(; n::Union{Nothing,Integer} = nothing, delay::Union{Nothing,Real} = nothing, limitwarn::Bool = true)
n_cur = ccall(:jl_profile_maxlen_data, Csize_t, ())
delay_cur = ccall(:jl_profile_delay_nsec, UInt64, ())/10^9
if n === nothing && delay === nothing
nthreads = Sys.iswindows() ? 1 : Threads.nthreads() # windows only profiles the main thread
return round(Int, n_cur / nthreads), delay_cur
end
nnew = (n === nothing) ? n_cur : n
delaynew = (delay === nothing) ? delay_cur : delay
init(nnew, delaynew; limitwarn)
end
function init(n::Integer, delay::Real; limitwarn::Bool = true)
nthreads = Sys.iswindows() ? 1 : Threads.nthreads() # windows only profiles the main thread
sample_size_bytes = sizeof(Ptr) # == Sys.WORD_SIZE / 8
buffer_samples = n * nthreads
buffer_size_bytes = buffer_samples * sample_size_bytes
if buffer_size_bytes > 2^29 && Sys.WORD_SIZE == 32
buffer_size_bytes_per_thread = floor(Int, 2^29 / nthreads)
buffer_samples_per_thread = floor(Int, buffer_size_bytes_per_thread / sample_size_bytes)
buffer_samples = buffer_samples_per_thread * nthreads
buffer_size_bytes = buffer_samples * sample_size_bytes
limitwarn && @warn "Requested profile buffer limited to 512MB (n = $buffer_samples_per_thread per thread) given that this system is 32-bit"
end
status = ccall(:jl_profile_init, Cint, (Csize_t, UInt64), buffer_samples, round(UInt64,10^9*delay))
if status == -1
error("could not allocate space for ", n, " instruction pointers per thread being profiled ($nthreads threads, $(Base.format_bytes(buffer_size_bytes)) total)")
end
end
# init with default values
# Use a max size of 10M profile samples, and fire timer every 1ms
# (that should typically give around 100 seconds of record)
if Sys.iswindows() && Sys.WORD_SIZE == 32
# The Win32 unwinder is 1000x slower than elsewhere (around 1ms/frame),
# so we don't want to slow the program down by quite that much
__init__() = init(1_000_000, 0.01, limitwarn = false)
else
__init__() = init(10_000_000, 0.001, limitwarn = false)
end
"""
clear()
Clear any existing backtraces from the internal buffer.
"""
clear() = ccall(:jl_profile_clear_data, Cvoid, ())
const LineInfoDict = Dict{UInt64, Vector{StackFrame}}
const LineInfoFlatDict = Dict{UInt64, StackFrame}
struct ProfileFormat
maxdepth::Int
mincount::Int
noisefloor::Float64
sortedby::Symbol
combine::Bool
C::Bool
recur::Symbol
function ProfileFormat(;
C = false,
combine = true,
maxdepth::Int = typemax(Int),
mincount::Int = 0,
noisefloor = 0,
sortedby::Symbol = :filefuncline,
recur::Symbol = :off)
return new(maxdepth, mincount, noisefloor, sortedby, combine, C, recur)
end
end
# offsets of the metadata in the data stream
const META_OFFSET_SLEEPSTATE = 2
const META_OFFSET_CPUCYCLECLOCK = 3
const META_OFFSET_TASKID = 4
const META_OFFSET_THREADID = 5
"""
print([io::IO = stdout,] [data::Vector]; kwargs...)
Prints profiling results to `io` (by default, `stdout`). If you do not
supply a `data` vector, the internal buffer of accumulated backtraces
will be used.
The keyword arguments can be any combination of:
- `format` -- Determines whether backtraces are printed with (default, `:tree`) or without (`:flat`)
indentation indicating tree structure.
- `C` -- If `true`, backtraces from C and Fortran code are shown (normally they are excluded).
- `combine` -- If `true` (default), instruction pointers are merged that correspond to the same line of code.
- `maxdepth` -- Limits the depth higher than `maxdepth` in the `:tree` format.
- `sortedby` -- Controls the order in `:flat` format. `:filefuncline` (default) sorts by the source
line, `:count` sorts in order of number of collected samples, and `:overhead` sorts by the number of samples
incurred by each function by itself.
- `groupby` -- Controls grouping over tasks and threads, or no grouping. Options are `:none` (default), `:thread`, `:task`,
`[:thread, :task]`, or `[:task, :thread]` where the last two provide nested grouping.
- `noisefloor` -- Limits frames that exceed the heuristic noise floor of the sample (only applies to format `:tree`).
A suggested value to try for this is 2.0 (the default is 0). This parameter hides samples for which `n <= noisefloor * √N`,
where `n` is the number of samples on this line, and `N` is the number of samples for the callee.
- `mincount` -- Limits the printout to only those lines with at least `mincount` occurrences.
- `recur` -- Controls the recursion handling in `:tree` format. `:off` (default) prints the tree as normal. `:flat` instead
compresses any recursion (by ip), showing the approximate effect of converting any self-recursion into an iterator.
`:flatc` does the same but also includes collapsing of C frames (may do odd things around `jl_apply`).
- `threads::Union{Int,AbstractVector{Int}}` -- Specify which threads to include snapshots from in the report. Note that
this does not control which threads samples are collected on.
- `tasks::Union{Int,AbstractVector{Int}}` -- Specify which tasks to include snapshots from in the report. Note that this
does not control which tasks samples are collected within.
"""
function print(io::IO,
data::Vector{<:Unsigned} = fetch(include_meta = true),
lidict::Union{LineInfoDict, LineInfoFlatDict} = getdict(data)
;
format = :tree,
C = false,
combine = true,
maxdepth::Int = typemax(Int),
mincount::Int = 0,
noisefloor = 0,
sortedby::Symbol = :filefuncline,
groupby::Union{Symbol,AbstractVector{Symbol}} = :none,
recur::Symbol = :off,
threads::Union{Int,AbstractVector{Int}} = 1:Threads.nthreads(),
tasks::Union{UInt,AbstractVector{UInt}} = typemin(UInt):typemax(UInt))
pf = ProfileFormat(;C, combine, maxdepth, mincount, noisefloor, sortedby, recur)
if groupby == :none
print(io, data, lidict, pf, format, threads, tasks, false)
else
if !in(groupby, [:thread, :task, [:task, :thread], [:thread, :task]])
error(ArgumentError("Unrecognized groupby option: $groupby. Options are :none (default), :task, :thread, [:task, :thread], or [:thread, :task]"))
elseif Sys.iswindows() && in(groupby, [:thread, [:task, :thread], [:thread, :task]])
@warn "Profiling on windows is limited to the main thread. Other threads have not been sampled and will not show in the report"
end
any_nosamples = false
println(io, "Overhead ╎ [+additional indent] Count File:Line; Function")
println(io, "=========================================================")
if groupby == [:task, :thread]
for taskid in intersect(get_task_ids(data), tasks)
threadids = intersect(get_thread_ids(data, taskid), threads)
if length(threadids) == 0
any_nosamples = true
else
nl = length(threadids) > 1 ? "\n" : ""
printstyled(io, "Task $(Base.repr(taskid))$nl"; bold=true, color=Base.debug_color())
for threadid in threadids
printstyled(io, " Thread $threadid\n"; bold=true, color=Base.info_color())
nosamples = print(io, data, lidict, pf, format, threadid, taskid, true)
nosamples && (any_nosamples = true)
println(io)
end
end
end
elseif groupby == [:thread, :task]
for threadid in intersect(get_thread_ids(data), threads)
taskids = intersect(get_task_ids(data, threadid), tasks)
if length(taskids) == 0
any_nosamples = true
else
nl = length(taskids) > 1 ? "\n" : ""
printstyled(io, "Thread $threadid$nl"; bold=true, color=Base.info_color())
for taskid in taskids
printstyled(io, " Task $(Base.repr(taskid))\n"; bold=true, color=Base.debug_color())
nosamples = print(io, data, lidict, pf, format, threadid, taskid, true)
nosamples && (any_nosamples = true)
println(io)
end
end
end
elseif groupby == :task
threads = 1:typemax(Int)
for taskid in intersect(get_task_ids(data), tasks)
printstyled(io, "Task $(Base.repr(taskid))\n"; bold=true, color=Base.debug_color())
nosamples = print(io, data, lidict, pf, format, threads, taskid, true)
nosamples && (any_nosamples = true)
println(io)
end
elseif groupby == :thread
tasks = 1:typemax(UInt)
for threadid in intersect(get_thread_ids(data), threads)
printstyled(io, "Thread $threadid\n"; bold=true, color=Base.info_color())
nosamples = print(io, data, lidict, pf, format, threadid, tasks, true)
nosamples && (any_nosamples = true)
println(io)
end
end
any_nosamples && warning_empty(summary = true)
end
return
end
function print(io::IO, data::Vector{<:Unsigned}, lidict::Union{LineInfoDict, LineInfoFlatDict}, fmt::ProfileFormat,
format::Symbol, threads::Union{Int,AbstractVector{Int}}, tasks::Union{UInt,AbstractVector{UInt}},
is_subsection::Bool = false)
cols::Int = Base.displaysize(io)[2]
data = convert(Vector{UInt64}, data)
fmt.recur ∈ (:off, :flat, :flatc) || throw(ArgumentError("recur value not recognized"))
if format === :tree
nosamples = tree(io, data, lidict, cols, fmt, threads, tasks, is_subsection)
return nosamples
elseif format === :flat
fmt.recur === :off || throw(ArgumentError("format flat only implements recur=:off"))
nosamples = flat(io, data, lidict, cols, fmt, threads, tasks, is_subsection)
return nosamples
else
throw(ArgumentError("output format $(repr(format)) not recognized"))
end
end
function get_task_ids(data::Vector{<:Unsigned}, threadid = nothing)
taskids = UInt[]
for i in length(data):-1:1
if is_block_end(data, i)
if isnothing(threadid) || data[i - META_OFFSET_THREADID] == threadid
taskid = data[i - META_OFFSET_TASKID]
!in(taskid, taskids) && push!(taskids, taskid)
end
end
end
return taskids
end
function get_thread_ids(data::Vector{<:Unsigned}, taskid = nothing)
threadids = Int[]
for i in length(data):-1:1
if is_block_end(data, i)
if isnothing(taskid) || data[i - META_OFFSET_TASKID] == taskid
threadid = data[i - META_OFFSET_THREADID]
!in(threadid, threadids) && push!(threadids, threadid)
end
end
end
return sort(threadids)
end
function is_block_end(data, i)
i < nmeta + 1 && return false
# 32-bit linux has been seen to have rogue NULL ips, so we use two to
# indicate block end, where the 2nd is the actual end index.
# and we could have (though very unlikely):
# 1:<stack><metadata><null><null><NULL><metadata><null><null>:end
# and we want to ignore the triple NULL (which is an ip).
return data[i] == 0 && data[i - 1] == 0 && data[i - META_OFFSET_SLEEPSTATE] != 0
end
function has_meta(data)
for i in 6:length(data)
data[i] == 0 || continue # first block end null
data[i - 1] == 0 || continue # second block end null
data[i - META_OFFSET_SLEEPSTATE] in 1:2 || continue
data[i - META_OFFSET_CPUCYCLECLOCK] != 0 || continue
data[i - META_OFFSET_TASKID] != 0 || continue
data[i - META_OFFSET_THREADID] != 0 || continue
return true
end
return false
end
"""
print([io::IO = stdout,] data::Vector, lidict::LineInfoDict; kwargs...)
Prints profiling results to `io`. This variant is used to examine results exported by a
previous call to [`retrieve`](@ref). Supply the vector `data` of backtraces and
a dictionary `lidict` of line information.
See `Profile.print([io], data)` for an explanation of the valid keyword arguments.
"""
print(data::Vector{<:Unsigned} = fetch(include_meta = true), lidict::Union{LineInfoDict, LineInfoFlatDict} = getdict(data); kwargs...) =
print(stdout, data, lidict; kwargs...)
"""
retrieve(; kwargs...) -> data, lidict
"Exports" profiling results in a portable format, returning the set of all backtraces
(`data`) and a dictionary that maps the (session-specific) instruction pointers in `data` to
`LineInfo` values that store the file name, function name, and line number. This function
allows you to save profiling results for future analysis.
"""
function retrieve(; kwargs...)
data = fetch(; kwargs...)
return (data, getdict(data))
end
function getdict(data::Vector{UInt})
dict = LineInfoDict()
return getdict!(dict, data)
end
function getdict!(dict::LineInfoDict, data::Vector{UInt})
# we don't want metadata here as we're just looking up ips
unique_ips = unique(has_meta(data) ? strip_meta(data) : data)
n_unique_ips = length(unique_ips)
n_unique_ips == 0 && return dict
iplookups = similar(unique_ips, Vector{StackFrame})
@sync for indexes_part in Iterators.partition(eachindex(unique_ips), div(n_unique_ips, Threads.nthreads(), RoundUp))
Threads.@spawn begin
for i in indexes_part
iplookups[i] = _lookup_corrected(unique_ips[i])
end
end
end
for i in eachindex(unique_ips)
dict[unique_ips[i]] = iplookups[i]
end
return dict
end
function _lookup_corrected(ip::UInt)
st = lookup(convert(Ptr{Cvoid}, ip))
# To correct line numbers for moving code, put it in the form expected by
# Base.update_stackframes_callback[]
stn = map(x->(x, 1), st)
# Note: Base.update_stackframes_callback[] should be data-race free
try Base.invokelatest(Base.update_stackframes_callback[], stn) catch end
return map(first, stn)
end
"""
flatten(btdata::Vector, lidict::LineInfoDict) -> (newdata::Vector{UInt64}, newdict::LineInfoFlatDict)
Produces "flattened" backtrace data. Individual instruction pointers
sometimes correspond to a multi-frame backtrace due to inlining; in
such cases, this function inserts fake instruction pointers for the
inlined calls, and returns a dictionary that is a 1-to-1 mapping
between instruction pointers and a single StackFrame.
"""
function flatten(data::Vector, lidict::LineInfoDict)
# Makes fake instruction pointers, counting down from typemax(UInt)
newip = typemax(UInt64) - 1
taken = Set(keys(lidict)) # make sure we don't pick one that's already used
newdict = Dict{UInt64,StackFrame}()
newmap = Dict{UInt64,Vector{UInt64}}()
for (ip, trace) in lidict
if length(trace) == 1
newdict[ip] = trace[1]
else
newm = UInt64[]
for sf in trace
while newip ∈ taken && newip > 0
newip -= 1
end
newip == 0 && error("all possible instruction pointers used")
push!(newm, newip)
newdict[newip] = sf
newip -= 1
end
newmap[ip] = newm
end
end
newdata = UInt64[]
for ip::UInt64 in data
if haskey(newmap, ip)
append!(newdata, newmap[ip])
else
push!(newdata, ip)
end
end
return (newdata, newdict)
end
# Take a file-system path and try to form a concise representation of it
# based on the package ecosystem
function short_path(spath::Symbol, filenamecache::Dict{Symbol, String})
return get!(filenamecache, spath) do
path = string(spath)
if isabspath(path)
if ispath(path)
# try to replace the file-system prefix with a short "@Module" one,
# assuming that profile came from the current machine
# (or at least has the same file-system layout)
root = path
while !isempty(root)
root, base = splitdir(root)
isempty(base) && break
@assert startswith(path, root)
for proj in Base.project_names
project_file = joinpath(root, proj)
if Base.isfile_casesensitive(project_file)
pkgid = Base.project_file_name_uuid(project_file, "")
isempty(pkgid.name) && return path # bad Project file
# return the joined the module name prefix and path suffix
path = path[nextind(path, sizeof(root)):end]
return string("@", pkgid.name, path)
end
end
end
end
return path
elseif isfile(joinpath(Sys.BINDIR::String, Base.DATAROOTDIR, "julia", "base", path))
# do the same mechanic for Base (or Core/Compiler) files as above,
# but they start from a relative path
return joinpath("@Base", normpath(path))
else
# for non-existent relative paths (such as "REPL[1]"), just consider simplifying them
return normpath(path) # drop leading "./"
end
end
end
"""
callers(funcname, [data, lidict], [filename=<filename>], [linerange=<start:stop>]) -> Vector{Tuple{count, lineinfo}}
Given a previous profiling run, determine who called a particular function. Supplying the
filename (and optionally, range of line numbers over which the function is defined) allows
you to disambiguate an overloaded method. The returned value is a vector containing a count
of the number of calls and line information about the caller. One can optionally supply
backtrace `data` obtained from [`retrieve`](@ref); otherwise, the current internal
profile buffer is used.
"""
function callers end
function callers(funcname::String, bt::Vector, lidict::LineInfoFlatDict; filename = nothing, linerange = nothing)
if filename === nothing && linerange === nothing
return callersf(li -> String(li.func) == funcname,
bt, lidict)
end
filename === nothing && throw(ArgumentError("if supplying linerange, you must also supply the filename"))
filename = String(filename)
if linerange === nothing
return callersf(li -> String(li.func) == funcname && String(li.file) == filename,
bt, lidict)
else
return callersf(li -> String(li.func) == funcname && String(li.file) == filename && in(li.line, linerange),
bt, lidict)
end
end
callers(funcname::String, bt::Vector, lidict::LineInfoDict; kwargs...) =
callers(funcname, flatten(bt, lidict)...; kwargs...)
callers(funcname::String; kwargs...) = callers(funcname, retrieve()...; kwargs...)
callers(func::Function, bt::Vector, lidict::LineInfoFlatDict; kwargs...) =
callers(string(func), bt, lidict; kwargs...)
callers(func::Function; kwargs...) = callers(string(func), retrieve()...; kwargs...)
##
## For --track-allocation
##
# Reset the malloc log. Used to avoid counting memory allocated during
# compilation.
"""
clear_malloc_data()
Clears any stored memory allocation data when running julia with `--track-allocation`.
Execute the command(s) you want to test (to force JIT-compilation), then call
[`clear_malloc_data`](@ref). Then execute your command(s) again, quit
Julia, and examine the resulting `*.mem` files.
"""
clear_malloc_data() = ccall(:jl_clear_malloc_data, Cvoid, ())
# C wrappers
start_timer() = ccall(:jl_profile_start_timer, Cint, ())
stop_timer() = ccall(:jl_profile_stop_timer, Cvoid, ())
is_running() = ccall(:jl_profile_is_running, Cint, ())!=0
is_buffer_full() = ccall(:jl_profile_is_buffer_full, Cint, ())!=0
get_data_pointer() = convert(Ptr{UInt}, ccall(:jl_profile_get_data, Ptr{UInt8}, ()))
len_data() = convert(Int, ccall(:jl_profile_len_data, Csize_t, ()))
maxlen_data() = convert(Int, ccall(:jl_profile_maxlen_data, Csize_t, ()))
error_codes = Dict(
-1=>"cannot specify signal action for profiling",
-2=>"cannot create the timer for profiling",
-3=>"cannot start the timer for profiling",
-4=>"cannot unblock SIGUSR1")
"""
fetch(;include_meta = true) -> data
Returns a copy of the buffer of profile backtraces. Note that the
values in `data` have meaning only on this machine in the current session, because it
depends on the exact memory addresses used in JIT-compiling. This function is primarily for
internal use; [`retrieve`](@ref) may be a better choice for most users.
By default metadata such as threadid and taskid is included. Set `include_meta` to `false` to strip metadata.
"""
function fetch(;include_meta = true)
maxlen = maxlen_data()
len = len_data()
if is_buffer_full()
@warn """The profile data buffer is full; profiling probably terminated
before your program finished. To profile for longer runs, call
`Profile.init()` with a larger buffer and/or larger delay."""
end
data = Vector{UInt}(undef, len)
GC.@preserve data unsafe_copyto!(pointer(data), get_data_pointer(), len)
if include_meta || isempty(data)
return data
end
return strip_meta(data)
end
function strip_meta(data)
nblocks = count(Base.Fix1(is_block_end, data), eachindex(data))
data_stripped = Vector{UInt}(undef, length(data) - (nblocks * (nmeta + 1)))
j = length(data_stripped)
i = length(data)
while i > 0 && j > 0
data_stripped[j] = data[i]
if is_block_end(data, i)
i -= (nmeta + 1) # metadata fields and the extra NULL IP
end
i -= 1
j -= 1
end
@assert i == j == 0 "metadata stripping failed"
return data_stripped
end
"""
Profile.add_fake_meta(data; threadid = 1, taskid = 0xf0f0f0f0) -> data_with_meta
The converse of `Profile.fetch(;include_meta = false)`; this will add fake metadata, and can be used
for compatibility and by packages (e.g., FlameGraphs.jl) that would rather not depend on the internal
details of the metadata format.
"""
function add_fake_meta(data; threadid = 1, taskid = 0xf0f0f0f0)
threadid == 0 && error("Fake threadid cannot be 0")
taskid == 0 && error("Fake taskid cannot be 0")
!isempty(data) && has_meta(data) && error("input already has metadata")
cpu_clock_cycle = UInt64(99)
data_with_meta = similar(data, 0)
for i = 1:length(data)
val = data[i]
if iszero(val)
# (threadid, taskid, cpu_cycle_clock, thread_sleeping)
push!(data_with_meta, threadid, taskid, cpu_clock_cycle+=1, false+1, 0, 0)
else
push!(data_with_meta, val)
end
end
return data_with_meta
end
## Print as a flat list
# Counts the number of times each line appears, at any nesting level and at the topmost level
# Merging multiple equivalent entries and recursive calls
function parse_flat(::Type{T}, data::Vector{UInt64}, lidict::Union{LineInfoDict, LineInfoFlatDict}, C::Bool,
threads::Union{Int,AbstractVector{Int}}, tasks::Union{UInt,AbstractVector{UInt}}) where {T}
!isempty(data) && !has_meta(data) && error("Profile data is missing required metadata")
lilist = StackFrame[]
n = Int[]
m = Int[]
lilist_idx = Dict{T, Int}()
recursive = Set{T}()
first = true
totalshots = 0
startframe = length(data)
skip = false
nsleeping = 0
for i in startframe:-1:1
(startframe - 1) >= i >= (startframe - (nmeta + 1)) && continue # skip metadata (its read ahead below) and extra block end NULL IP
ip = data[i]
if is_block_end(data, i)
# read metadata
thread_sleeping = data[i - META_OFFSET_SLEEPSTATE] - 1 # subtract 1 as state is incremented to avoid being equal to 0
# cpu_cycle_clock = data[i - META_OFFSET_CPUCYCLECLOCK]
taskid = data[i - META_OFFSET_TASKID]
threadid = data[i - META_OFFSET_THREADID]
if !in(threadid, threads) || !in(taskid, tasks)
skip = true
continue
end
if thread_sleeping == 1
nsleeping += 1
end
skip = false
totalshots += 1
empty!(recursive)
first = true
startframe = i
elseif !skip
frames = lidict[ip]
nframes = (frames isa Vector ? length(frames) : 1)
for j = 1:nframes
frame = (frames isa Vector ? frames[j] : frames)
!C && frame.from_c && continue
key = (T === UInt64 ? ip : frame)
idx = get!(lilist_idx, key, length(lilist) + 1)
if idx > length(lilist)
push!(recursive, key)
push!(lilist, frame)
push!(n, 1)
push!(m, 0)
elseif !(key in recursive)
push!(recursive, key)
n[idx] += 1
end
if first
m[idx] += 1
first = false
end
end
end
end
@assert length(lilist) == length(n) == length(m) == length(lilist_idx)
return (lilist, n, m, totalshots, nsleeping)
end
function flat(io::IO, data::Vector{UInt64}, lidict::Union{LineInfoDict, LineInfoFlatDict}, cols::Int, fmt::ProfileFormat,
threads::Union{Int,AbstractVector{Int}}, tasks::Union{UInt,AbstractVector{UInt}}, is_subsection::Bool)
lilist, n, m, totalshots, nsleeping = parse_flat(fmt.combine ? StackFrame : UInt64, data, lidict, fmt.C, threads, tasks)
util_perc = (1 - (nsleeping / totalshots)) * 100
if isempty(lilist)
if is_subsection
Base.print(io, "Total snapshots: ")
printstyled(io, "$(totalshots)", color=Base.warn_color())
Base.println(io, " (", round(Int, util_perc), "% utilization)")
else
warning_empty()
end
return true
end
if false # optional: drop the "non-interpretable" ones
keep = map(frame -> frame != UNKNOWN && frame.line != 0, lilist)
lilist = lilist[keep]
n = n[keep]
m = m[keep]
end
filenamemap = Dict{Symbol,String}()
print_flat(io, lilist, n, m, cols, filenamemap, fmt)
Base.print(io, "Total snapshots: ", totalshots, " (", round(Int, util_perc), "% utilization")
if is_subsection
println(io, ")")
else
println(io, " across all threads and tasks. Use the `groupby` kwarg to break down by thread and/or task)")
end
return false
end
function print_flat(io::IO, lilist::Vector{StackFrame},
n::Vector{Int}, m::Vector{Int},
cols::Int, filenamemap::Dict{Symbol,String},
fmt::ProfileFormat)
if fmt.sortedby === :count
p = sortperm(n)
elseif fmt.sortedby === :overhead
p = sortperm(m)
else
p = liperm(lilist)
end
lilist = lilist[p]
n = n[p]
m = m[p]
filenames = String[short_path(li.file, filenamemap) for li in lilist]
funcnames = String[string(li.func) for li in lilist]
wcounts = max(6, ndigits(maximum(n)))
wself = max(9, ndigits(maximum(m)))
maxline = 1
maxfile = 6
maxfunc = 10
for i in 1:length(lilist)
li = lilist[i]
maxline = max(maxline, li.line)
maxfunc = max(maxfunc, length(funcnames[i]))
maxfile = max(maxfile, length(filenames[i]))
end
wline = max(5, ndigits(maxline))
ntext = max(20, cols - wcounts - wself - wline - 3)
maxfunc += 25 # for type signatures
if maxfile + maxfunc <= ntext
wfile = maxfile
wfunc = ntext - maxfunc # take the full width (for type sig)
else
wfile = 2*ntext÷5
wfunc = 3*ntext÷5
end
println(io, lpad("Count", wcounts, " "), " ", lpad("Overhead", wself, " "), " ",
rpad("File", wfile, " "), " ", lpad("Line", wline, " "), " Function")
println(io, lpad("=====", wcounts, " "), " ", lpad("========", wself, " "), " ",
rpad("====", wfile, " "), " ", lpad("====", wline, " "), " ========")
for i = 1:length(n)
n[i] < fmt.mincount && continue
li = lilist[i]
Base.print(io, lpad(string(n[i]), wcounts, " "), " ")
Base.print(io, lpad(string(m[i]), wself, " "), " ")
if li == UNKNOWN
if !fmt.combine && li.pointer != 0
Base.print(io, "@0x", string(li.pointer, base=16))
else
Base.print(io, "[any unknown stackframes]")
end
else
file = filenames[i]
isempty(file) && (file = "[unknown file]")
Base.print(io, rpad(rtruncto(file, wfile), wfile, " "), " ")
Base.print(io, lpad(li.line > 0 ? string(li.line) : "?", wline, " "), " ")
fname = funcnames[i]
if !li.from_c && li.linfo !== nothing
fname = sprint(show_spec_linfo, li)
end
isempty(fname) && (fname = "[unknown function]")
Base.print(io, ltruncto(fname, wfunc))
end
println(io)
end
nothing
end
## A tree representation
# Representation of a prefix trie of backtrace counts
mutable struct StackFrameTree{T} # where T <: Union{UInt64, StackFrame}
# content fields:
frame::StackFrame
count::Int # number of frames this appeared in
overhead::Int # number frames where this was the code being executed
flat_count::Int # number of times this frame was in the flattened representation (unlike count, this'll sum to 100% of parent)
max_recur::Int # maximum number of times this frame was the *top* of the recursion in the stack
count_recur::Int # sum of the number of times this frame was the *top* of the recursion in a stack (divide by count to get an average)
down::Dict{T, StackFrameTree{T}}
# construction workers:
recur::Int
builder_key::Vector{UInt64}
builder_value::Vector{StackFrameTree{T}}
up::StackFrameTree{T}
StackFrameTree{T}() where {T} = new(UNKNOWN, 0, 0, 0, 0, 0, Dict{T, StackFrameTree{T}}(), 0, UInt64[], StackFrameTree{T}[])
end
const indent_s = " ╎"^10
const indent_z = collect(eachindex(indent_s))
function indent(depth::Int)
depth < 1 && return ""
depth <= length(indent_z) && return indent_s[1:indent_z[depth]]
div, rem = divrem(depth, length(indent_z))
indent = indent_s^div
rem != 0 && (indent *= SubString(indent_s, 1, indent_z[rem]))
return indent
end
function tree_format(frames::Vector{<:StackFrameTree}, level::Int, cols::Int, maxes, filenamemap::Dict{Symbol,String}, showpointer::Bool)
nindent = min(cols>>1, level)
ndigoverhead = ndigits(maxes.overhead)
ndigcounts = ndigits(maxes.count)
ndigline = ndigits(maximum(frame.frame.line for frame in frames)) + 6
ntext = max(30, cols - ndigoverhead - nindent - ndigcounts - ndigline - 6)
widthfile = 2*ntext÷5 # min 12
widthfunc = 3*ntext÷5 # min 18
strs = Vector{String}(undef, length(frames))
showextra = false
if level > nindent
nextra = level - nindent
nindent -= ndigits(nextra) + 2
showextra = true
end
for i = 1:length(frames)
frame = frames[i]
li = frame.frame
stroverhead = lpad(frame.overhead > 0 ? string(frame.overhead) : "", ndigoverhead, " ")
base = nindent == 0 ? "" : indent(nindent - 1) * " "
if showextra
base = string(base, "+", nextra, " ")
end
strcount = rpad(string(frame.count), ndigcounts, " ")
if li != UNKNOWN
if li.line == li.pointer
strs[i] = string(stroverhead, "╎", base, strcount, " ",
"[unknown function] (pointer: 0x",
string(li.pointer, base = 16, pad = 2*sizeof(Ptr{Cvoid})),
")")
else
if !li.from_c && li.linfo !== nothing
fname = sprint(show_spec_linfo, li)
else
fname = string(li.func)
end
filename = short_path(li.file, filenamemap)
if showpointer
fname = string(
"0x",
string(li.pointer, base = 16, pad = 2*sizeof(Ptr{Cvoid})),
" ",
fname)
end
strs[i] = string(stroverhead, "╎", base, strcount, " ",
rtruncto(filename, widthfile),
":",
li.line == -1 ? "?" : string(li.line),
"; ",
ltruncto(fname, widthfunc))
end
else
strs[i] = string(stroverhead, "╎", base, strcount, " [unknown stackframe]")
end
end
return strs
end
# turn a list of backtraces into a tree (implicitly separated by NULL markers)
function tree!(root::StackFrameTree{T}, all::Vector{UInt64}, lidict::Union{LineInfoFlatDict, LineInfoDict}, C::Bool, recur::Symbol,
threads::Union{Int,AbstractVector{Int},Nothing}=nothing, tasks::Union{UInt,AbstractVector{UInt},Nothing}=nothing) where {T}
!isempty(all) && !has_meta(all) && error("Profile data is missing required metadata")
parent = root
tops = Vector{StackFrameTree{T}}()
build = Vector{StackFrameTree{T}}()
startframe = length(all)
skip = false
nsleeping = 0
for i in startframe:-1:1
(startframe - 1) >= i >= (startframe - (nmeta + 1)) && continue # skip metadata (it's read ahead below) and extra block end NULL IP
ip = all[i]
if is_block_end(all, i)
# read metadata
thread_sleeping = all[i - META_OFFSET_SLEEPSTATE] - 1 # subtract 1 as state is incremented to avoid being equal to 0
# cpu_cycle_clock = all[i - META_OFFSET_CPUCYCLECLOCK]
taskid = all[i - META_OFFSET_TASKID]
threadid = all[i - META_OFFSET_THREADID]
if (threads !== nothing && !in(threadid, threads)) ||
(tasks !== nothing && !in(taskid, tasks))
skip = true
continue
end
if thread_sleeping == 1
nsleeping += 1
end
skip = false
# sentinel value indicates the start of a new backtrace
empty!(build)
root.recur = 0
if recur !== :off
# We mark all visited nodes to so we'll only count those branches
# once for each backtrace. Reset that now for the next backtrace.
push!(tops, parent)
for top in tops
while top.recur != 0
top.max_recur < top.recur && (top.max_recur = top.recur)
top.recur = 0
top = top.up
end
end
empty!(tops)
end
let this = parent
while this !== root
this.flat_count += 1
this = this.up
end
end
parent.overhead += 1
parent = root
root.count += 1
startframe = i
elseif !skip
pushfirst!(build, parent)
if recur === :flat || recur === :flatc
# Rewind the `parent` tree back, if this exact ip was already present *higher* in the current tree
found = false
for j in 1:(startframe - i)
if ip == all[i + j]
if recur === :flat # if not flattening C frames, check that now
frames = lidict[ip]
frame = (frames isa Vector ? frames[1] : frames)
frame.from_c && break # not flattening this frame
end
push!(tops, parent)
parent = build[j]
parent.recur += 1
parent.count_recur += 1
found = true
break
end
end
found && continue
end
builder_key = parent.builder_key
builder_value = parent.builder_value
fastkey = searchsortedfirst(builder_key, ip)
if fastkey < length(builder_key) && builder_key[fastkey] === ip
# jump forward to the end of the inlining chain
# avoiding an extra (slow) lookup of `ip` in `lidict`
# and an extra chain of them in `down`
# note that we may even have this === parent (if we're ignoring this frame ip)
this = builder_value[fastkey]
let this = this
while this !== parent && (recur === :off || this.recur == 0)
this.count += 1
this.recur = 1
this = this.up
end
end
parent = this
continue
end
frames = lidict[ip]
nframes = (frames isa Vector ? length(frames) : 1)
this = parent
# add all the inlining frames
for i = nframes:-1:1
frame = (frames isa Vector ? frames[i] : frames)
!C && frame.from_c && continue
key = (T === UInt64 ? ip : frame)
this = get!(StackFrameTree{T}, parent.down, key)
if recur === :off || this.recur == 0
this.frame = frame
this.up = parent
this.count += 1
this.recur = 1
end
parent = this
end
# record where the end of this chain is for this ip
insert!(builder_key, fastkey, ip)
insert!(builder_value, fastkey, this)
end
end
function cleanup!(node::StackFrameTree)
stack = [node]
while !isempty(stack)
node = pop!(stack)
node.recur = 0
empty!(node.builder_key)
empty!(node.builder_value)
append!(stack, values(node.down))
end
nothing
end
cleanup!(root)
return root, nsleeping
end
function maxstats(root::StackFrameTree)
maxcount = Ref(0)
maxflatcount = Ref(0)
maxoverhead = Ref(0)
maxmaxrecur = Ref(0)
stack = [root]
while !isempty(stack)
node = pop!(stack)
maxcount[] = max(maxcount[], node.count)
maxoverhead[] = max(maxoverhead[], node.overhead)
maxflatcount[] = max(maxflatcount[], node.flat_count)