forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsummarysize.jl
196 lines (178 loc) · 6.16 KB
/
summarysize.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
# This file is a part of Julia. License is MIT: https://julialang.org/license
struct SummarySize
seen::IdDict{Any,Any}
frontier_x::Vector{Any}
frontier_i::Vector{Int}
exclude::Any
chargeall::Any
end
"""
Base.summarysize(obj; exclude=Union{...}, chargeall=Union{...}) -> Int
Compute the amount of memory, in bytes, used by all unique objects reachable from the argument.
# Keyword Arguments
- `exclude`: specifies the types of objects to exclude from the traversal.
- `chargeall`: specifies the types of objects to always charge the size of all of their
fields, even if those fields would normally be excluded.
See also [`sizeof`](@ref).
# Examples
```jldoctest
julia> Base.summarysize(1.0)
8
julia> Base.summarysize(Ref(rand(100)))
848
julia> sizeof(Ref(rand(100)))
8
```
"""
function summarysize(obj;
exclude = Union{DataType, Core.TypeName, Core.MethodInstance},
chargeall = Union{Core.TypeMapEntry, Method})
@nospecialize obj exclude chargeall
ss = SummarySize(IdDict(), Any[], Int[], exclude, chargeall)
size::Int = ss(obj)
while !isempty(ss.frontier_x)
# DFS heap traversal of everything without a specialization
# BFS heap traversal of anything with a specialization
x = ss.frontier_x[end]
i = ss.frontier_i[end]
val = nothing
if isa(x, SimpleVector)
nf = length(x)
if isassigned(x, i)
val = x[i]
end
elseif isa(x, Array)
nf = length(x)
if ccall(:jl_array_isassigned, Cint, (Any, UInt), x, i - 1) != 0
val = x[i]
end
else
nf = nfields(x)
ft = typeof(x).types
if !isbitstype(ft[i]) && isdefined(x, i)
val = getfield(x, i)
end
end
if nf > i
ss.frontier_i[end] = i + 1
else
pop!(ss.frontier_x)
pop!(ss.frontier_i)
end
if val !== nothing && !isa(val, Module) && (!isa(val, ss.exclude) || isa(x, ss.chargeall))
size += ss(val)::Int
end
end
return size
end
(ss::SummarySize)(@nospecialize obj) = _summarysize(ss, obj)
# define the general case separately to make sure it is not specialized for every type
@noinline function _summarysize(ss::SummarySize, @nospecialize obj)
issingletontype(typeof(obj)) && return 0
# NOTE: this attempts to discover multiple copies of the same immutable value,
# and so is somewhat approximate.
key = ccall(:jl_value_ptr, Ptr{Cvoid}, (Any,), obj)
haskey(ss.seen, key) ? (return 0) : (ss.seen[key] = true)
if nfields(obj) > 0
push!(ss.frontier_x, obj)
push!(ss.frontier_i, 1)
end
if isa(obj, UnionAll) || isa(obj, Union)
# black-list of items that don't have a Core.sizeof
sz = 2 * sizeof(Int)
else
sz = Core.sizeof(obj)
end
if sz == 0
# 0-field mutable structs are not unique
return gc_alignment(0)
end
return sz
end
(::SummarySize)(obj::Symbol) = 0
(::SummarySize)(obj::SummarySize) = 0
function (ss::SummarySize)(obj::String)
key = ccall(:jl_value_ptr, Ptr{Cvoid}, (Any,), obj)
haskey(ss.seen, key) ? (return 0) : (ss.seen[key] = true)
return Core.sizeof(Int) + Core.sizeof(obj)
end
function (ss::SummarySize)(obj::DataType)
key = pointer_from_objref(obj)
haskey(ss.seen, key) ? (return 0) : (ss.seen[key] = true)
size::Int = 7 * Core.sizeof(Int) + 6 * Core.sizeof(Int32)
size += 4 * nfields(obj) + ifelse(Sys.WORD_SIZE == 64, 4, 0)
size += ss(obj.parameters)::Int
if isdefined(obj, :types)
size += ss(obj.types)::Int
end
return size
end
function (ss::SummarySize)(obj::Core.TypeName)
key = pointer_from_objref(obj)
haskey(ss.seen, key) ? (return 0) : (ss.seen[key] = true)
return Core.sizeof(obj) + (isdefined(obj, :mt) ? ss(obj.mt) : 0)
end
function (ss::SummarySize)(obj::Array)
haskey(ss.seen, obj) ? (return 0) : (ss.seen[obj] = true)
headersize = 4*sizeof(Int) + 8 + max(0, ndims(obj)-2)*sizeof(Int)
size::Int = headersize
datakey = unsafe_convert(Ptr{Cvoid}, obj)
if !haskey(ss.seen, datakey)
ss.seen[datakey] = true
dsize = Core.sizeof(obj)
T = eltype(obj)
if isbitsunion(T)
# add 1 union selector byte for each element
dsize += length(obj)
end
size += dsize
if !isempty(obj) && T !== Symbol && (!Base.allocatedinline(T) || (T isa DataType && !Base.datatype_pointerfree(T)))
push!(ss.frontier_x, obj)
push!(ss.frontier_i, 1)
end
end
return size
end
function (ss::SummarySize)(obj::SimpleVector)
key = pointer_from_objref(obj)
haskey(ss.seen, key) ? (return 0) : (ss.seen[key] = true)
size::Int = Core.sizeof(obj)
if !isempty(obj)
push!(ss.frontier_x, obj)
push!(ss.frontier_i, 1)
end
return size
end
function (ss::SummarySize)(obj::Module)
haskey(ss.seen, obj) ? (return 0) : (ss.seen[obj] = true)
size::Int = Core.sizeof(obj)
for binding in names(obj, all = true)
if isdefined(obj, binding) && !isdeprecated(obj, binding)
value = getfield(obj, binding)
if !isa(value, Module) || parentmodule(value) === obj
size += ss(value)::Int
if isa(value, UnionAll)
value = unwrap_unionall(value)
end
if isa(value, DataType) && parentmodule(value) === obj && nameof(value) === binding
# charge a TypeName to its module (but not to the type)
size += ss(value.name)::Int
end
end
end
end
return size
end
function (ss::SummarySize)(obj::Task)
haskey(ss.seen, obj) ? (return 0) : (ss.seen[obj] = true)
size::Int = Core.sizeof(obj)
if isdefined(obj, :code)
size += ss(obj.code)::Int
end
size += ss(obj.storage)::Int
size += ss(obj.donenotify)::Int
size += ss(obj.result)::Int
# TODO: add stack size, and possibly traverse stack roots
return size
end
(ss::SummarySize)(obj::BigInt) = _summarysize(ss, obj) + obj.alloc*sizeof(Base.GMP.Limb)