Class: Autotuner::Heuristic::Malloc

Inherits:
Base
  • Object
show all
Defined in:
lib/autotuner/heuristic/malloc.rb

Constant Summary collapse

MALLOC_GC_RATIO_THRESHOLD =
0.1
MIN_MALLOC_GC =
10
DEFAULT_MALLOC_LIMIT =
16 * 1024 * 1024
DEFAULT_MALLOC_LIMIT_MAX =
32 * 1024 * 1024
LIMIT_ENV =
"RUBY_GC_MALLOC_LIMIT"
LIMIT_MAX_ENV =
"RUBY_GC_MALLOC_LIMIT_MAX"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

disable!, enabled?

Constructor Details

#initialize(_system_context) ⇒ Malloc

Returns a new instance of Malloc.



28
29
30
31
32
33
34
35
# File 'lib/autotuner/heuristic/malloc.rb', line 28

def initialize(_system_context)
  super

  @minor_gc_count = 0
  @malloc_gc_count = 0

  @given_suggestion = false
end

Instance Attribute Details

#malloc_gc_countObject (readonly)

Returns the value of attribute malloc_gc_count.



26
27
28
# File 'lib/autotuner/heuristic/malloc.rb', line 26

def malloc_gc_count
  @malloc_gc_count
end

#minor_gc_countObject (readonly)

Returns the value of attribute minor_gc_count.



25
26
27
# File 'lib/autotuner/heuristic/malloc.rb', line 25

def minor_gc_count
  @minor_gc_count
end

Class Method Details

.supported?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/autotuner/heuristic/malloc.rb', line 7

def supported?
  true
end

Instance Method Details

#call(request_context) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/autotuner/heuristic/malloc.rb', line 41

def call(request_context)
  # gc_by is only useful if we ran at least one minor GC during the request.
  if request_context.after_gc_context.stat[:minor_gc_count] ==
      request_context.before_gc_context.stat[:minor_gc_count]
    return
  end
  # gc_by is only useful if it wasn't a major GC.
  # It is a major GC when where is a major_by reason set.
  return if request_context.after_gc_context.latest_gc_info[:major_by]

  @minor_gc_count += 1
  @malloc_gc_count += 1 if request_context.after_gc_context.latest_gc_info[:gc_by] == :malloc
end

#debug_stateObject



80
81
82
83
84
85
86
# File 'lib/autotuner/heuristic/malloc.rb', line 80

def debug_state
  {
    given_suggestion: @given_suggestion,
    minor_gc_count: minor_gc_count,
    malloc_gc_count: malloc_gc_count,
  }
end

#nameObject



37
38
39
# File 'lib/autotuner/heuristic/malloc.rb', line 37

def name
  "Malloc"
end

#tuning_reportObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/autotuner/heuristic/malloc.rb', line 55

def tuning_report
  # Don't give suggestions twice.
  return if @given_suggestion
  # Don't report if there's very few data points
  return if malloc_gc_count < MIN_MALLOC_GC

  malloc_gc_ratio = malloc_gc_count.to_f / minor_gc_count
  # Don't report if there's very few malloc GC.
  return if malloc_gc_ratio <= MALLOC_GC_RATIO_THRESHOLD

  @given_suggestion = true

  Report::MultipleEnvironmentVariables.new(
    <<~MSG,
      The following suggestions reduce the number of minor garbage collection cycles, specifically a cycle called "malloc". Your app runs malloc cycles in approximately #{format("%.2f", malloc_gc_ratio * 100)}% of all minor garbage collection cycles.

      Reducing minor garbage collection cycles can help reduce response times. The following tuning values aim to reduce malloc garbage collection cycles by setting it to a higher value. This may cause a slight increase in memory usage. You should monitor memory usage carefully to ensure your app is not running out of memory.
    MSG
    [LIMIT_ENV, LIMIT_MAX_ENV],
    # Suggest to double the limit and max
    [suggested_malloc_limit, suggested_malloc_limit_max],
    [configured_malloc_limit, configured_malloc_limit_max],
  )
end