Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add report_invalidations to tabulate invalidations summary #303

Merged
merged 5 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
MethodAnalysis = "85b6ec6f-f7df-4429-9514-a64bcd9ee824"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["ColorTypes", "Documenter", "FixedPointNumbers", "MethodAnalysis", "Pkg", "Random", "Test"]
test = ["ColorTypes", "PrettyTables", "Documenter", "FixedPointNumbers", "MethodAnalysis", "Pkg", "Random", "Test"]
4 changes: 4 additions & 0 deletions src/SnoopCompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ function __init__()
if isdefined(SnoopCompile, :runtime_inferencetime)
@require PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee" include("visualizations.jl")
end
if isdefined(SnoopCompileCore, Symbol("@snoopr"))
@require PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d" include("report_invalidations.jl")
end
return nothing
end

end # module
41 changes: 41 additions & 0 deletions src/invalidations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,47 @@ else
new_backedge_table() = Dict{Tuple{Int32,UInt64},Tuple{Any,Vector{Any}}}()
end

"""
report_invalidations(
io::IO = stdout;
invalidations,
n_rows::Int = 10,
process_filename::Function = x -> x,
)

Print a tabular summary of invalidations given:

- `invalidations` the output of [`SnoopCompileCore.@snoopr`](@ref)

and (optionally)

- `io::IO` IO stream. Defaults to `stdout`
- `n_rows::Int` the number of rows to be displayed in the
truncated table. A value of 0 indicates no truncation.
A positive value will truncate the table to the specified
number of rows.
- `process_filename(::String)::String` a function to post-process
each filename, where invalidations are found

# Example usage

```julia
charleskawczynski marked this conversation as resolved.
Show resolved Hide resolved
import SnoopCompileCore
invalidations = SnoopCompileCore.@snoopr begin

# load packages & define any additional methods

end;

using SnoopCompile
using PrettyTables # to load report_invalidations
report_invalidations(;invalidations)
```

Using `report_invalidations` requires that you first load the `PrettyTables.jl` package.
"""
function report_invalidations end

"""
trees = invalidation_trees(list)

Expand Down
62 changes: 62 additions & 0 deletions src/report_invalidations.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# This file is loaded conditionally via @require if PrettyTables is loaded

import .PrettyTables

export report_invalidations

function report_invalidations(io::IO = stdout;
invalidations,
n_rows::Int = 10,
process_filename::Function = x -> x,
)
@assert n_rows β‰₯ 0
trees = reverse(invalidation_trees(invalidations))
n_total_invalidations = length(uinvalidated(invalidations))
# TODO: Merge `@info` statement with one below
invs_per_method = map(trees) do methinvs
countchildren(methinvs)
end
n_invs_total = length(invs_per_method)
if n_invs_total == 0
@info "Zero invalidations! πŸŽ‰"
return nothing
end
nr = n_rows == 0 ? n_invs_total : n_rows
truncated_invs = nr < n_invs_total
sum_invs = sum(invs_per_method)
invs_per_method = invs_per_method[1:min(nr, n_invs_total)]
trees = trees[1:min(nr, n_invs_total)]
trunc_msg = truncated_invs ? " (showing $nr functions) " : ""
@info "$n_total_invalidations methods invalidated for $n_invs_total functions$trunc_msg"
n_invalidations_percent = map(invs_per_method) do inv
inv_perc = inv / sum_invs
Int(round(inv_perc*100, digits = 0))
end
meth_name = map(trees) do inv
"$(inv.method.name)"
end
fileinfo = map(trees) do inv
"$(process_filename(string(inv.method.file))):$(inv.method.line)"
end
header = (
["<file name>:<line number>", "Function Name", "Invalidations", "Invalidations %"],
["", "", "", "(xα΅’/βˆ‘x)"],
)
table_data = hcat(
fileinfo,
meth_name,
invs_per_method,
n_invalidations_percent,
)

PrettyTables.pretty_table(
io,
table_data;
header,
formatters = PrettyTables.ft_printf("%s", 2:2),
header_crayon = PrettyTables.crayon"yellow bold",
subheader_crayon = PrettyTables.crayon"green bold",
crop = :none,
alignment = [:l, :c, :c, :c],
)
end
9 changes: 9 additions & 0 deletions test/snoopr.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SnoopCompile, InteractiveUtils, MethodAnalysis, Pkg, Test
import PrettyTables # so that the report_invalidations.jl file is loaded

const qualify_mi = Base.VERSION >= v"1.7.0-DEV.5" # julia PR #38608

Expand Down Expand Up @@ -187,6 +188,14 @@ end
Base.@irrational twoΟ€ 6.2831853071795864769 2*big(Ο€)
end
end
# Tabulate invalidations:
io = IOBuffer()
SnoopCompile.report_invalidations(io;
invalidations = invs,
)
str = String(take!(io))
@test occursin("Invalidations %", str)

charleskawczynski marked this conversation as resolved.
Show resolved Hide resolved
trees = invalidation_trees(invs)
@test length(trees) == 3
io = IOBuffer()
Expand Down