python / python/cpython

Refactor perf trampoline proposal: split the assembly per architecture, auto-generate DWARF unwind data

Đang mở
#149,800 5 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

interpreter-core type-refactor
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Feature or enhancement

Proposal:

Not a user visible feature per se but I still think it warrants discussion.

The problem:

I've been working on fixing up the RISC-V support for perf and also checking out adding s390x and ppc64le but the process is quite tedious, I'd say and the various connections around this infra are fragile, so I've been thinking of how to make the process a little bit easier and more maintainable/readable.

The steps to add perf trampoline support for a new CPU architecture currently:

The DWARF CFI instructions must mirror the assembly. This synchronization needs to be done manually and involves compiling a C equivalent of the trampoline, running readelf, and hand-translating the output into DWRF macros. Getting it wrong can produces silent failures, broken stack unwinding.

With x86-64, aarch64, and RISC-V there (RISC-V broken atm), when adding more things there, the assembly file is becoming bigger and less readable, elf_init_ehframe_perf() is becoming a wall of #ifdef blocks without much shared logic and so on.

Proposal1:

Split assembly files.

Replace the single Python/asm_trampoline.S with per-arch files:

  • Python/asm_trampoline_x86_64.S
  • Python/asm_trampoline_aarch64.S
  • Python/asm_trampoline_riscv64.S
  • (maybe in the future: asm_trampoline_ppc64le.S, asm_trampoline_s390x.S)

configure.ac is modified accordingly and selects which file to compile depending on arch. With each file self-contained, each arch can be reviewed, modified, tested independently. This will also require a few more lines in configure and careful handling of the MacOS case.

Proposal2 (followup):

Auto-generate DWARF data from compiled assembly.

A build-time script extracts the .eh_frame section from the compiled trampoline object and generates a header with the raw DWARF unwind data. At runtime, elf_init_ehframe_perf() copies this data and patches in the actual code address and size, replacing the current architecture-specific DWARF generation code entirely.

The JIT's Tools/jit/build.py already extracts DWARF CFI data from compiled objects to generate jit_unwind_info-.h for the GDB unwind path so something similar could be deployed here.

Another possibility would be a dependency on readelf, but I don't think that this is something desirable.

Pros and cons

Pros:

  • Adding a new architecture requires exactly one new file (the assembly) and a few lines in configure.ac. No DWARF knowledge needed (assuming the DWARF extraction script is robust :) ).
  • Eliminates possible bugs with syncronization of DWARF data (or move them to the extraction script, either way it should be simpler to deal with)
  • jit_unwind.c's elf_init_ehframe_perf shrinks to only the arch-independent code.

Cons

  • PYTHON_FOR_REGEN must be available when the trampoline assembly changes (same constraint as JIT stencil generation). That practically means that perf support will now depend on PYTHON_FOR_REGEN, making it unavailable for "clean" builds.
Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs
  • gh-149894
  • gh-150364
  • gh-150365
  • gh-157246

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách đọc Python/asm_trampoline.S, Python/jit_unwind.c, Python/perf_jit_trampoline.c, configure.ac và Tools/jit/build.py để hiểu assembly dành riêng cho từng kiến trúc hiện tại và việc tạo DWARF. Công việc sẽ được coi là hoàn tất khi có một phương pháp đã được thống nhất và kiểm thử cho các trampoline theo từng kiến trúc cùng việc tạo tự động dữ liệu unwind, nhưng các PRs được liên kết và liệt kê cho thấy đề xuất này đã được thực hiện ở nơi khác.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
c, python
Lĩnh vực
build-system, performance
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.