ZJIT 모듈

ZJIT 모듈

ZJIT는 CRuby의 JIT(Just-In-Time) 컴파일러인 ZJIT를 들여다볼 수 있게 해주는 모듈이에요. 모듈 안의 모든 것은 구현에 매우 밀접하게 의존적이라, 표준 라이브러리에 비하면 API가 덜 안정적일 수 있어요.

출처: Ruby 4.0 API — ZJIT

본문

ZJIT가 CRuby가 빌드된 특정 플랫폼을 지원하지 않으면 이 모듈이 존재하지 않을 수도 있어요. 따라서 이 모듈을 쓰는 코드는 플랫폼에 따라 동작 여부가 달라질 수 있음을 염두에 둬야 해요.

ZJIT는 실제로는 RubyVM::ZJIT로 접근할 수 있어요. 주로 JIT가 켜져 있는지, 통계는 어떻게 되는지, 어떤 곳에서 JIT를 빠져나왔는지(side exit) 등을 확인하는 용도로 써요.

Public Class Methods

dump_exit_locations(filename)

종료 위치(exit locations)를 Marshal로 지정한 파일명에 덤프해요.

사용 방법은 이렇습니다. 먼저 스크립트 안에서 호출하세요.

RubyVM::ZJIT.dump_exit_locations("my_file.dump")

그런 다음 파일을 다음 옵션과 함께 실행해요.

ruby --zjit --zjit-stats --zjit-trace-exits test.rb

코드가 실행을 마치면 Stackprof로 덤프 파일을 읽어요. 옵션에 대한 자세한 내용은 Stackprof 문서를 참고하세요. --zjit-trace-exits가 켜져 있지 않으면 ArgumentError가 발생해요.

enable()

ZJIT 컴파일을 활성화해요. 이미 활성화되어 있으면 false를 돌려줘요. 다른 JIT(예: YJIT)가 이미 켜져 있으면 "Only one JIT can be enabled at the same time." 경고를 내고 false를 돌려줘요.

enabled?()

ZJIT가 활성화되어 있는지 확인해요.

def enabled?
  Primitive.cexpr! 'RBOOL(rb_zjit_enabled_p)'
end

exit_locations()

--zjit-trace-exits가 켜져 있으면, Primitive.rb_zjit_get_exit_locations가 반환한 해시들을 Stackprof이 읽을 수 있는 형식으로 파싱해요. 이를 통해 ZJIT에서 특정 명령어로 인해 빠져나간(side exit) 정확한 위치를 찾을 수 있어요.

이 메서드는 --zjit-trace-exits가 꺼져 있으면 nil을 돌려줘요. Stackprof 문법에 맞춰 프레임 해시를 만들고, 샘플 수를 누적해 반환해요.

reset_stats!()

--zjit-stats를 위해 수집한 통계를 버려요.

stats(target_key = nil)

ZJIT 통계를 Hash로 돌려줘요.

stats_enabled?()

--zjit-stats를 사용 중인지 확인해요.

stats_string()

ZJIT 통계의 요약을 String으로 돌려줘요. stats가 없으면 nil을 돌려줘요.

출력에는 다음과 같은 내용들이 포함돼요.

  • not inlined C methods — 인라인되지 않은 C 메서드 수
  • calls to C functions from JIT code — JIT 코드에서 C 함수를 호출한 횟수
  • not optimized method types for send / send_without_block — 최적화되지 않은 메서드 타입
  • instructions with uncategorized fallback reason — 분류되지 않은 폴백 사유가 있는 명령어
  • send fallback reasons, setivar/getivar/definedivar fallback reasons — 폴백 사유
  • invokeblock handler
  • popular complex argument-parameter features not optimized — 최적화되지 않은 복잡한 인자 전달 기능
  • compile error reasons, unhandled YARV insns/HIR insns, side exit reasons

마지막에는 send_count, compiled_iseq_count, compile_time_ns, guard_type_count, code_region_bytes, side_exit_count, ratio_in_zjit 같은 핵심 통계들이 나와요.

trace_exit_locations_enabled?()

--zjit-trace-exits를 사용 중인지 확인해요.