Skip to content

Commit 29239b0

Browse files
authored
Enable Ruby JIT by default (#5464)
**Which issue(s) this PR fixes**: Fixes #5284 **What this PR does / why we need it**: Enable Ruby JIT by default. **Docs Changes**: **Release Note**: * Enable Ruby JIT by default * The system configuration `enable_jit` now defaults to `true`. * JIT increases memory usage of each worker process. Set `enable_jit false` in the `<system>` section to restore the previous behavior. * This has no effect on Windows because YJIT is not supported there. Fluentd just logs `Ruby JIT is not available on this Ruby` at the info level. --------- Signed-off-by: Shizuo Fujita <fujita@clear-code.com>
1 parent ff8682d commit 29239b0

4 files changed

Lines changed: 63 additions & 8 deletions

File tree

lib/fluent/supervisor.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,7 @@ def run_worker
780780
MessagePackFactory.init(enable_time_support: @system_config.enable_msgpack_time_support)
781781
Fluent::Engine.init(@system_config, start_in_parallel: ENV.key?("FLUENT_RUNNING_IN_PARALLEL_WITH_OLD"))
782782
Fluent::Engine.run_configure(@conf)
783+
enable_ruby_jit if @system_config.enable_jit
783784
Fluent::Engine.run
784785
self.class.cleanup_socketmanager_path if @standalone_worker
785786
exit 0
@@ -1214,6 +1215,19 @@ def main_process(&block)
12141215
exit!(unrecoverable_error ? 2 : 1)
12151216
end
12161217

1218+
def enable_ruby_jit
1219+
unless defined?(RubyVM::YJIT) && RubyVM::YJIT.respond_to?(:enable)
1220+
$log.info "Ruby JIT is not available on this Ruby"
1221+
return
1222+
end
1223+
1224+
if RubyVM::YJIT.enable
1225+
$log.info "enabled Ruby JIT"
1226+
else
1227+
$log.warn "failed to enable Ruby JIT"
1228+
end
1229+
end
1230+
12171231
def build_system_config(conf)
12181232
system_config = SystemConfig.create(conf, @cl_opt[:strict_config_value])
12191233
# Prefer the options explicitly specified in the command line
@@ -1281,11 +1295,6 @@ def build_spawn_command
12811295
fluentd_spawn_cmd << '-Eascii-8bit:ascii-8bit'
12821296
end
12831297

1284-
if @system_config.enable_jit
1285-
$log.info "enable Ruby JIT for workers (--jit)"
1286-
fluentd_spawn_cmd << '--jit'
1287-
end
1288-
12891298
# Adding `-h` so that it can avoid ruby's command blocking
12901299
# e.g. `ruby -Eascii-8bit:ascii-8bit` will block. but `ruby -Eascii-8bit:ascii-8bit -h` won't.
12911300
_, e, s = Open3.capture3(*fluentd_spawn_cmd, "-h")

lib/fluent/system_config.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class SystemConfig
5353
config_param :disable_shared_socket, :bool, default: nil
5454
config_param :enable_input_metrics, :bool, default: true
5555
config_param :enable_size_metrics, :bool, default: nil
56-
config_param :enable_jit, :bool, default: false
56+
config_param :enable_jit, :bool, default: true
5757
config_param :file_permission, default: nil do |v|
5858
v.to_i(8)
5959
end

test/config/test_system_config.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def parse_text(text)
7878
assert_true(sc.enable_input_metrics)
7979
assert_nil(sc.enable_size_metrics)
8080
assert_nil(sc.enable_msgpack_time_support)
81-
assert(!sc.enable_jit)
81+
assert(sc.enable_jit)
8282
assert_nil(sc.log.path)
8383
assert_equal(:text, sc.log.format)
8484
assert_equal('%Y-%m-%d %H:%M:%S %z', sc.log.time_format)
@@ -100,7 +100,7 @@ def parse_text(text)
100100
'enable_msgpack_time_support' => ['enable_msgpack_time_support', true],
101101
'enable_input_metrics' => ['enable_input_metrics', false],
102102
'enable_size_metrics' => ['enable_size_metrics', true],
103-
'enable_jit' => ['enable_jit', true],
103+
'enable_jit' => ['enable_jit', false],
104104
'umask' => ['umask', '0022'],
105105
)
106106
test "accepts parameters" do |(k, v)|

test/test_supervisor.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,52 @@ def server.config
957957
end
958958
end
959959

960+
sub_test_case "enable_jit" do
961+
setup do
962+
omit "YJIT is not supported on Windows, and RubyVM::YJIT is not defined either" if Fluent.windows?
963+
end
964+
965+
def create_worker(enable_jit)
966+
sv = Fluent::Supervisor.new({})
967+
conf = Fluent::Config::Element.new(
968+
'ROOT', '', {}, [Fluent::Config::Element.new('system', '', { 'enable_jit' => enable_jit.to_s }, [])]
969+
)
970+
sv.instance_variable_set(:@system_config, sv.__send__(:build_system_config, conf))
971+
sv.instance_variable_set(:@conf, conf)
972+
sv
973+
end
974+
975+
data("enabled" => [true, 1],
976+
"disabled" => [false, 0])
977+
def test_run_worker((enable_jit, expected_count))
978+
sv = create_worker(enable_jit)
979+
980+
stub(sv).install_main_process_signal_handlers
981+
stub(Fluent::MessagePackFactory).init
982+
stub(Fluent::Engine).init
983+
stub(Fluent::Engine).run_configure
984+
stub(Fluent::Engine).run
985+
986+
enable_count = 0
987+
stub(RubyVM::YJIT).enable { enable_count += 1; true }
988+
989+
assert_raise(SystemExit) { sv.run_worker }
990+
assert_equal(expected_count, enable_count)
991+
end
992+
993+
data("succeeded" => [true, "enabled Ruby JIT"],
994+
"failed" => [false, "failed to enable Ruby JIT"])
995+
def test_log_result((result, expected_message))
996+
sv = create_worker(true)
997+
create_info_dummy_logger
998+
999+
mock(RubyVM::YJIT).enable { result }
1000+
sv.__send__(:enable_ruby_jit)
1001+
1002+
assert { $log.out.logs.any? { |log| log.include?(expected_message) } }
1003+
end
1004+
end
1005+
9601006
sub_test_case "zero_downtime_restart" do
9611007
setup do
9621008
omit "Not supported on Windows" if Fluent.windows?

0 commit comments

Comments
 (0)