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
# Not ISO specified
assert() do
assert_false GC.disable
assert_true GC.enable
assert_false GC.enable
end
assert() do
begin
assert_false GC.disable
assert_true GC.disable
ensure
GC.enable
end
end
# mrb_gc_add_region() must refuse a buffer too small to hold a page once its
# base is aligned, rather than wrapping the usable size and carving a page out
# past the buffer's end. Driven from C (mrbgems/mruby-test/gc.c) since the
# function has no Ruby-visible face.
assert() do
assert_equal 0, __gc_add_region_undersized
end
assert() do
origin = GC.interval_ratio
begin
assert_equal 150, (GC.interval_ratio = 150)
ensure
GC.interval_ratio = origin
end
end
assert() do
origin = GC.step_ratio
begin
assert_equal 150, (GC.step_ratio = 150)
assert_raise(ArgumentError) { GC.step_ratio = 0 }
assert_raise(ArgumentError) { GC.step_ratio = -1 }
ensure
GC.step_ratio = origin
end
end
assert() do
# The ratios are stored in a C int. A value past its range must be rejected,
# not truncated: truncating 2**32 to 0 leaves the incremental collector with
# a zero step budget and it stops making progress.
origin_s = GC.step_ratio
origin_i = GC.interval_ratio
begin
huge = 1 << 40 # fits a 64-bit mrb_int, overflows a 32-bit C int
rescue RangeError
skip
end
begin
# On a wide mrb_int the setter's own bounds check rejects it (ArgumentError);
# on a narrower one the argument conversion rejects it first (RangeError).
assert_raise(ArgumentError, RangeError) { GC.step_ratio = huge }
assert_raise(ArgumentError, RangeError) { GC.interval_ratio = huge }
ensure
GC.step_ratio = origin_s
GC.interval_ratio = origin_i
end
end
assert() do
origin = GC.step_limit
begin
assert_equal 0, origin # default: unlimited
assert_equal 512, (GC.step_limit = 512)
assert_equal 512, GC.step_limit
assert_equal 0, (GC.step_limit = 0) # back to unlimited
ensure
GC.step_limit = origin
end
end
assert() do
origin = GC.step_limit
begin
GC.step_limit = 64
# GC should still complete even with a small step limit
GC.start
assert_true GC.stat[:live] > 0
ensure
GC.step_limit = origin
end
end
assert() do
origin = GC.malloc_threshold
begin
# No assertion on `origin`: the initial value is MRB_GC_MALLOC_THRESHOLD,
# which a build may set to anything including 0.
assert_equal 65536, (GC.malloc_threshold = 65536)
assert_equal 65536, GC.malloc_threshold
assert_equal 0, (GC.malloc_threshold = 0) # back to disabled
ensure
GC.malloc_threshold = origin
end
end
assert() do
origin = GC.malloc_threshold
begin
GC.malloc_threshold = 4096
GC.start # force a full GC cycle
# allocate large strings to exceed threshold
100.times { * 1024 }
stat = GC.stat
assert_true stat[:malloc_increase] >= 0
ensure
GC.malloc_threshold = origin
end
end
# A cycle that runs incrementally needs one call per step, and outside this
# path every one of those calls comes from an object allocation. A workload
# that allocates bytes without allocating objects therefore has to advance the
# cycle on byte pressure alone; if it cannot, the first threshold crossing
# starts a cycle that parks mid-mark and no later crossing is ever honoured.
# `malloc_increase` is cleared on every crossing, so its value at the end
# counts the bytes allocated since the last one: it stays near the threshold
# while the axis keeps working and grows to the whole loop once it stops.
#
# The 40 buffers below are Strings, and an object allocation does step the
# collector, so the loop has to stay far enough under that axis for the
# reading to mean what it says. It does, by construction: `GC.start` ends
# with `gc_debt` at minus a credit that is floored at `GC_STEP_SIZE`, so the
# object axis takes at least 1024 allocations to step even once. 40 is
# twenty-five times under that floor, and the floor is the worst case, since
# a build with a larger live set gets a proportionally larger credit.
#
# That floor is the one thing MRB_GC_STRESS takes away: it collects fully at
# every object allocation, and with MRB_DEBUG at every heap allocation too, so
# the assertion passes there whether or not byte pressure works. It is not
# weaker than it looks, it is measuring a build that cannot hold the state it
# is about: a cycle can only park where something allocates without collecting,
# which is exactly what those builds rule out.
assert() do
origin = GC.malloc_threshold
origin_mode = GC.generational_mode
begin
GC.generational_mode = false # every cycle is incremental in this mode
GC.start # start from MRB_GC_STATE_ROOT
GC.malloc_threshold = 1024
40.times { * 65536 } # 2.6MB of buffers, 40 objects
assert_true GC.stat[:malloc_increase] < 262144
ensure
GC.malloc_threshold = origin
GC.generational_mode = origin_mode
end
end
assert() do
origin = GC.malloc_threshold
begin
GC.malloc_threshold = 1
GC.start # reset malloc_increase
h = {}
300.times { h[i] = i }
assert_equal 300, h.size
300.times { assert_equal i, h[i] }
a = []
1000.times { a << i }
assert_equal 1000, a.size
1000.times { assert_equal i, a[i] }
ensure
GC.malloc_threshold = origin
end
end
assert() do
origin = GC.generational_mode
begin
assert_false (GC.generational_mode = false)
assert_true (GC.generational_mode = true)
assert_true (GC.generational_mode = true)
ensure
GC.generational_mode = origin
end
end
# The inline `[]`, `[]=` and arithmetic opcodes answer from C without a method
# call, so the arena restore that every cfunc return performs never runs for
# them. What they allocate then stays arena-protected for the rest of the
# enclosing method. The loop bodies below are built only from opcodes that do
# not restore, since a single send in the body would empty the arena and hide
# the retention.
#
# Each assertion runs a full GC while the arena still holds what the loop left
# there. The arena is a GC root, so exactly the pinned objects survive that
# collection and the rise in `GC.stat[:live]` counts them and nothing else.
# The `GC.start` has to be the first send after the loop: any cfunc return
# drains the arena, so reading `GC.stat` first would discard the very thing
# being measured.
#
# A retaining branch pins one object per iteration and reports the full 20000.
# The margin below covers the few objects `GC.stat` allocates for its own
# result; it is not slack for a partial leak, and a branch that retains on even
# a small fraction of the iterations is over it.
#
# An index opcode answers from C only while the operator it reimplements is
# still the builtin, so a loop body stops being send-free the moment something
# redefines that operator on the core class. Nothing in this tree redefines
# `Hash#[]` or `Hash#[]=`, so the Hash loops below need no help; mruby-regexp
# does redefine `String#[]`, which is what `with_builtin_string_aref` puts back.
# Runs the block with the C implementation of `String#[]` in place, so that
# `s[0]` and `s[1]` inside it reach `OP_GETIDX0` and `OP_GETIDX` rather than
# compiling down to a send, whose cfunc return would drain the arena and pass
# the assertion for the wrong reason. mruby-regexp keeps the C implementation
# under `__aref` when it replaces `String#[]`; where no gem replaced it there
# is nothing to swap and the block runs as it stands.
swapped = String.method_defined?(:__aref)
if swapped
String.class_eval do
alias_method :__aref_gc_test_saved, :[]
alias_method :[], :__aref
end
end
begin
yield
ensure
if swapped
String.class_eval do
alias_method :[], :__aref_gc_test_saved
# `remove_method` comes from mruby-metaprog, which the core test build
# does not have; the saved alias is harmless where it is missing.
remove_method :__aref_gc_test_saved if respond_to?(:remove_method, true)
end
end
end
end
assert() do
stress
with_builtin_string_aref do
s =
GC.start
base = GC.stat[:live]
i = 0
while i < 20000
s[1]
i += 1
end
GC.start
assert_operator GC.stat[:live] - base, :<, 100
end
end
assert() do
stress
h = Hash.new { Object.new }
GC.start
base = GC.stat[:live]
i = 0
while i < 20000
h[1]
i += 1
end
GC.start
assert_operator GC.stat[:live] - base, :<, 100
end
assert() do
stress
with_builtin_string_aref do
s =
GC.start
base = GC.stat[:live]
i = 0
while i < 20000
s[0]
i += 1
end
GC.start
assert_operator GC.stat[:live] - base, :<, 100
end
end
assert() do
stress
h = Hash.new { Object.new }
GC.start
base = GC.stat[:live]
i = 0
while i < 20000
h[0]
i += 1
end
GC.start
assert_operator GC.stat[:live] - base, :<, 100
end
assert() do
stress
h = {}
k =
GC.start
base = GC.stat[:live]
i = 0
while i < 20000
h[k] = 1
i += 1
end
GC.start
assert_operator GC.stat[:live] - base, :<, 100
end
assert() do
stress
# A call that passes fewer positional arguments than the `*rest` and post
# parameters span has its post arguments moved to the end of that span,
# and when nothing but the required arguments came, or they came packed in
# one array with a keyword hash after it, that is the register the block
# arrived in. The empty `rest` is allocated right after, with the block
# held in a C local and nowhere the marker looks, so a collection landing
# on that allocation freed the `Proc`, and what the method then read as its
# block was whatever the cell was reused for. Enough calls for the
# collector to land there.
; blk; end
; blk; end
args = [1, 2]
kw = {}
bad = 0
i = 0
while i < 5000
b = gc_test_short_args(1, 2) { :c }
bad += 1 unless b.call == :c
b = gc_test_short_args_kw(*args, **kw) { :c }
bad += 1 unless b.call == :c
i += 1
end
assert_equal 0, bad
end
assert() do
stress
# The overflow branch promotes to a big integer, so it only exists with
# mruby-bigint. The shift count is a variable because a constant shift is
# folded at compile time, and a folded result out of mrb_int range makes the
# build fail rather than raise.
begin
k = 62
x = 1 << k
x + x
rescue RangeError
skip
end
# 1 << 30 overflows mrb_int on MRB_INT32 and 1 << 62 on MRB_INT64, so
# whichever width this build has, one of the two takes the overflow branch.
[30, 62].each do
x = 1 << shift
GC.start
base = GC.stat[:live]
i = 0
while i < 20000
x + x
i += 1
end
GC.start
assert_operator GC.stat[:live] - base, :<, 100
end
end
# The assertions below cover the boxing sites inside the interpreter loop
# rather than the calls out of it. `SET_INT_VALUE()` heap-allocates an
# RInteger for a value outside the fixnum range, and the inline opcodes that
# box one have no cfunc epilogue behind them either, so what they allocate
# stays in the arena the same way. Where the fixnum range ends depends on the
# boxing mode, so the arithmetic loops are run at more than one width; on a
# build whose boxing macro cannot allocate at all they retain nothing and the
# assertions hold trivially. `1 << shift` is written with a variable shift
# because a constant shift is folded at compile time, and a folded result out
# of mrb_int range makes the build fail rather than raise.
assert() do
stress
[30, 31, 62].each do
begin
x = 1 << shift
rescue RangeError
next # mrb_int is narrower than this and mruby-bigint is absent
end
one = 1
GC.start
base = GC.stat[:live]
i = 0
while i < 20000
x + one # OP_ADD
x + 1 # OP_ADDI
i += 1
end
GC.start
assert_operator GC.stat[:live] - base, :<, 100
end
end
assert() do
stress
[30, 31, 62].each do
begin
x = 1 << shift
rescue RangeError
next
end
one = 1
GC.start
base = GC.stat[:live]
i = 0
while i < 20000
x / one
i += 1
end
GC.start
assert_operator GC.stat[:live] - base, :<, 100
end
end
assert() do
stress
# `1073741824` is `2**30`, which fits in the operand of `OP_LOADI32` rather
# than going to the pool, and is the first value outside the fixnum range of
# a 32-bit host under word boxing. That is the only configuration where this
# opcode can allocate: everywhere else the value is a fixnum, the loop retains
# nothing and the assertion holds trivially.
GC.start
base = GC.stat[:live]
i = 0
while i < 20000
z = 1073741824
i += 1
end
GC.start
assert_operator GC.stat[:live] - base, :<, 100
assert_equal 1073741824, z
end
# The Float branches of the same opcodes box through `SET_FLOAT_VALUE()`, which
# under word boxing heap-allocates an RFloat whenever the mrb_value word cannot
# hold the value inline. With `MRB_WORDBOX_NO_INLINE_FLOAT` that is every
# Float; without it, on a 64-bit host, it is a subnormal, an exponent outside
# the inlinable range, or a rotation that would collide with a sentinel.
# `5.0e-324` and `1.0e100` are on the wrong side of both bounds, so the loops
# allocate under either setting. The immediate-operand opcodes are run only on
# `1.0e100`, since a subnormal plus a non-zero integer is an ordinary Float that
# does inline. On a boxing mode that keeps the Float in the word the loops
# retain nothing and the assertions hold trivially.
assert() do
stress
skip unless Object.const_defined?(:Float)
[1.0e100, 5.0e-324].each do
zero = 0.0
one = 1.0
y = nil
GC.start
base = GC.stat[:live]
i = 0
while i < 20000
y = x + zero # OP_ADD
y = x - zero # OP_SUB
y = x * one # OP_MUL
i += 1
end
GC.start
assert_operator GC.stat[:live] - base, :<, 100
assert_equal x, y
end
end
assert() do
stress
skip unless Object.const_defined?(:Float)
# OP_DIV boxes from a helper outside the interpreter loop, so it restores to
# its own saved arena index rather than to the frame's.
[1.0e100, 5.0e-324].each do
one = 1.0
y = nil
GC.start
base = GC.stat[:live]
i = 0
while i < 20000
y = x / one
i += 1
end
GC.start
assert_operator GC.stat[:live] - base, :<, 100
assert_equal x, y
end
end
assert() do
stress
skip unless Object.const_defined?(:Float)
x = 1.0e100
y = nil
GC.start
base = GC.stat[:live]
i = 0
while i < 20000
y = x + 1 # OP_ADDI
y = x - 1 # OP_SUBI
x += 1 # OP_ADDILV, the fused local form
x -= 1 # OP_SUBILV
i += 1
end
GC.start
assert_operator GC.stat[:live] - base, :<, 100
assert_equal 1.0e100, x
assert_equal 1.0e100, y
end
assert() do
stress
skip unless Object.const_defined?(:Float)
y = nil
GC.start
base = GC.stat[:live]
i = 0
while i < 20000
y = 1.0e100
y = 5.0e-324
i += 1
end
GC.start
assert_operator GC.stat[:live] - base, :<, 100
assert_equal 5.0e-324, y
end