forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_memory_leak.rb
175 lines (137 loc) · 4 KB
/
test_memory_leak.rb
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
# frozen_string_literal: true
# simple test to check for memory leaks
#
# this performs a trivial operation walking all multisites and grabbing first topic / localizing
# the expectation is that RSS will remain static no matter how many iterations run
if ENV['RAILS_ENV'] != "production"
exec "RAILS_ENV=production ruby #{__FILE__}"
end
if !ENV['LD_PRELOAD']
exec "LD_PRELOAD=/usr/lib/libjemalloc.so.1 ruby #{__FILE__}"
end
if ENV['LD_PRELOAD'].include?("jemalloc")
# for 3.6.0 we need a patch jemal 1.1.0 gem (1.1.1 does not support 3.6.0)
# however ffi is a problem so we need to patch the gem
require 'jemal'
$jemalloc = true
end
if ENV['LD_PRELOAD'].include?("mwrap")
$mwrap = true
require 'mwrap'
end
def bin_diff(current)
$baseline[:arenas].each_with_index do |arena, i|
next if !arena || !arena[:bins]
arena[:bins].each do |size, stat|
allocated = (current.dig(:arenas, i, :bins, size, :allocated) || 0)
diff = allocated - stat[:allocated]
puts "bin #{size} delta #{diff}"
end
end
end
require File.expand_path("../../config/environment", __FILE__)
Rails.application.routes.recognize_path('abc') rescue nil
I18n.t(:posts)
def rss
`ps -o rss -p #{$$}`.chomp.split("\n").last.to_i
end
def loop_sites
RailsMultisite::ConnectionManagement.each_connection do
yield
end
end
def biggest_klass(klass)
ObjectSpace.each_object(klass).max { |a, b| a.length <=> b.length }
end
def iter(warmup: false)
loop_sites { Topic.first; I18n.t('too_late_to_edit') }
if !warmup
GC.start(full_mark: true, immediate_sweep: true)
if $jemalloc
jemal_stats = Jemal.stats
jedelta = "(jdelta #{jemal_stats[:active] - $baseline_jemalloc_active})"
end
if $mwrap
mwrap_delta = (Mwrap.total_bytes_allocated - Mwrap.total_bytes_freed) - $mwrap_baseline
mwrap_delta = "(mwrap delta #{mwrap_delta})"
end
rss_delta = rss - $baseline_rss
array_delta = biggest_klass(Array).length - $biggest_array_length
puts "rss: #{rss} (#{rss_delta}) #{mwrap_delta}#{jedelta} heap_delta: #{GC.stat[:heap_live_slots] - $baseline_slots} array_delta: #{array_delta}"
if $jemalloc
bin_diff(jemal_stats)
end
end
end
iter(warmup: true)
4.times do
GC.start(full_mark: true, immediate_sweep: true)
end
if $jemalloc
$baseline = Jemal.stats
$baseline_jemalloc_active = $baseline[:active]
4.times do
GC.start(full_mark: true, immediate_sweep: true)
end
end
def render_table(array)
buffer = +""
width = array[0].map { |k| k.to_s.length }
cols = array[0].length
array.each do |row|
row.each_with_index do |val, i|
width[i] = [width[i].to_i, val.to_s.length].max
end
end
array[0].each_with_index do |col, i|
buffer << col.to_s.ljust(width[i], ' ')
if i == cols - 1
buffer << "\n"
else
buffer << ' | '
end
end
buffer << ("-" * (width.sum + width.length))
buffer << "\n"
array.drop(1).each do |row|
row.each_with_index do |val, i|
buffer << val.to_s.ljust(width[i], ' ')
if i == cols - 1
buffer << "\n"
else
buffer << ' | '
end
end
end
buffer
end
def mwrap_log
report = +""
Mwrap.quiet do
report << "Allocated bytes: #{Mwrap.total_bytes_allocated} Freed bytes: #{Mwrap.total_bytes_freed}\n"
report << "\n"
table = []
Mwrap.each(200000) do |loc, total, allocations, frees, age_sum, max_life|
table << [total, allocations - frees, frees == 0 ? -1 : (age_sum / frees.to_f).round(2), max_life, loc]
end
table.sort! { |a, b| b[1] <=> a[1] }
table = table[0..50]
table.prepend(["total", "delta", "mean_life", "max_life", "location"])
report << render_table(table)
end
report
end
Mwrap.clear
if $mwrap
$mwrap_baseline = Mwrap.total_bytes_allocated - Mwrap.total_bytes_freed
end
$baseline_slots = GC.stat[:heap_live_slots]
$baseline_rss = rss
$biggest_array_length = biggest_klass(Array).length
100000.times do
iter
if $mwrap
puts mwrap_log
GC.start(full_mark: true, immediate_sweep: true)
end
end