|
| 1 | +#include "Python.h" |
| 2 | +#include "pycore_ceval.h" |
| 3 | +#include "pycore_frame.h" |
| 4 | +#include "pycore_interp.h" |
| 5 | + |
| 6 | +#ifdef HAVE_PERF_TRAMPOLINE |
| 7 | + |
| 8 | +#include <stdio.h> |
| 9 | +#include <stdlib.h> |
| 10 | +#include <sys/mman.h> |
| 11 | +#include <sys/types.h> |
| 12 | +#include <unistd.h> |
| 13 | + |
| 14 | +typedef PyObject *(*py_evaluator)(PyThreadState *, _PyInterpreterFrame *, |
| 15 | + int throwflag); |
| 16 | +typedef PyObject *(*py_trampoline)(py_evaluator, PyThreadState *, |
| 17 | + _PyInterpreterFrame *, int throwflag); |
| 18 | +extern void *_Py_trampoline_func_start; |
| 19 | +extern void *_Py_trampoline_func_end; |
| 20 | + |
| 21 | +typedef struct { |
| 22 | + char *start_addr; |
| 23 | + char *current_addr; |
| 24 | + size_t size; |
| 25 | + size_t size_left; |
| 26 | + size_t code_size; |
| 27 | +} code_arena_t; |
| 28 | + |
| 29 | +static Py_ssize_t extra_code_index = -1; |
| 30 | +static code_arena_t code_arena; |
| 31 | + |
| 32 | +static int |
| 33 | +new_code_arena() |
| 34 | +{ |
| 35 | + size_t page_size = sysconf(_SC_PAGESIZE); |
| 36 | + char *memory = mmap(NULL, // address |
| 37 | + page_size, PROT_READ | PROT_WRITE | PROT_EXEC, |
| 38 | + MAP_PRIVATE | MAP_ANONYMOUS, |
| 39 | + -1, // fd (not used here) |
| 40 | + 0); // offset (not used here) |
| 41 | + if (!memory) { |
| 42 | + Py_FatalError("Failed to allocate new code arena"); |
| 43 | + return -1; |
| 44 | + } |
| 45 | + void *start = &_Py_trampoline_func_start; |
| 46 | + void *end = &_Py_trampoline_func_end; |
| 47 | + size_t code_size = end - start; |
| 48 | + |
| 49 | + long n_copies = page_size / code_size; |
| 50 | + for (int i = 0; i < n_copies; i++) { |
| 51 | + memcpy(memory + i * code_size, start, code_size * sizeof(char)); |
| 52 | + } |
| 53 | + |
| 54 | + mprotect(memory, page_size, PROT_READ | PROT_EXEC); |
| 55 | + |
| 56 | + code_arena.start_addr = memory; |
| 57 | + code_arena.current_addr = memory; |
| 58 | + code_arena.size = page_size; |
| 59 | + code_arena.size_left = page_size; |
| 60 | + code_arena.code_size = code_size; |
| 61 | + return 0; |
| 62 | +} |
| 63 | + |
| 64 | +static inline py_trampoline |
| 65 | +code_arena_new_code(code_arena_t *code_arena) |
| 66 | +{ |
| 67 | + py_trampoline trampoline = (py_trampoline)code_arena->current_addr; |
| 68 | + code_arena->size_left -= code_arena->code_size; |
| 69 | + code_arena->current_addr += code_arena->code_size; |
| 70 | + return trampoline; |
| 71 | +} |
| 72 | + |
| 73 | +static inline py_trampoline |
| 74 | +compile_trampoline(void) |
| 75 | +{ |
| 76 | + if (code_arena.size_left <= code_arena.code_size) { |
| 77 | + if (new_code_arena() < 0) { |
| 78 | + return NULL; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + assert(code_arena.size_left <= code_arena.size); |
| 83 | + return code_arena_new_code(&code_arena); |
| 84 | +} |
| 85 | + |
| 86 | +static inline FILE * |
| 87 | +perf_map_open(pid_t pid) |
| 88 | +{ |
| 89 | + char filename[100]; |
| 90 | + snprintf(filename, sizeof(filename), "/tmp/perf-%d.map", pid); |
| 91 | + FILE *res = fopen(filename, "a"); |
| 92 | + if (!res) { |
| 93 | + _Py_FatalErrorFormat(__func__, "Couldn't open %s: errno(%d)", filename, errno); |
| 94 | + return NULL; |
| 95 | + } |
| 96 | + return res; |
| 97 | +} |
| 98 | + |
| 99 | +static inline int |
| 100 | +perf_map_close(FILE *fp) |
| 101 | +{ |
| 102 | + if (fp) { |
| 103 | + return fclose(fp); |
| 104 | + } |
| 105 | + return 0; |
| 106 | +} |
| 107 | + |
| 108 | +static void |
| 109 | +perf_map_write_entry(FILE *method_file, const void *code_addr, |
| 110 | + unsigned int code_size, const char *entry, |
| 111 | + const char *file) |
| 112 | +{ |
| 113 | + fprintf(method_file, "%lx %x py::%s:%s\n", (unsigned long)code_addr, |
| 114 | + code_size, entry, file); |
| 115 | +} |
| 116 | + |
| 117 | +static PyObject * |
| 118 | +py_trampoline_evaluator(PyThreadState *ts, _PyInterpreterFrame *frame, |
| 119 | + int throw) |
| 120 | +{ |
| 121 | + PyCodeObject *co = frame->f_code; |
| 122 | + py_trampoline f = NULL; |
| 123 | + _PyCode_GetExtra((PyObject *)co, extra_code_index, (void **)&f); |
| 124 | + if (f == NULL) { |
| 125 | + if (extra_code_index == -1) { |
| 126 | + extra_code_index = _PyEval_RequestCodeExtraIndex(NULL); |
| 127 | + } |
| 128 | + py_trampoline new_trampoline = compile_trampoline(); |
| 129 | + if (new_trampoline == NULL) { |
| 130 | + return NULL; |
| 131 | + } |
| 132 | + FILE *pfile = perf_map_open(getpid()); |
| 133 | + if (pfile == NULL) { |
| 134 | + return NULL; |
| 135 | + } |
| 136 | + perf_map_write_entry(pfile, new_trampoline, code_arena.code_size, |
| 137 | + PyUnicode_AsUTF8(co->co_qualname), |
| 138 | + PyUnicode_AsUTF8(co->co_filename)); |
| 139 | + perf_map_close(pfile); |
| 140 | + _PyCode_SetExtra((PyObject *)co, extra_code_index, |
| 141 | + (void *)new_trampoline); |
| 142 | + f = new_trampoline; |
| 143 | + } |
| 144 | + assert(f != NULL); |
| 145 | + return f(_PyEval_EvalFrameDefault, ts, frame, throw); |
| 146 | +} |
| 147 | +#endif |
| 148 | + |
| 149 | +int |
| 150 | +_PyPerfTrampoline_Init(int activate) |
| 151 | +{ |
| 152 | + PyThreadState *tstate = _PyThreadState_GET(); |
| 153 | + if (!activate) { |
| 154 | + tstate->interp->eval_frame = NULL; |
| 155 | + } |
| 156 | + else { |
| 157 | +#ifdef HAVE_PERF_TRAMPOLINE |
| 158 | + tstate->interp->eval_frame = py_trampoline_evaluator; |
| 159 | + if (new_code_arena() < 0) { |
| 160 | + return -1; |
| 161 | + } |
| 162 | +#endif |
| 163 | + } |
| 164 | + return 0; |
| 165 | +} |
0 commit comments