static PyObject* do_fullcompile(PyFrameObject* frame, PyObject* arg) { PyCodeStats* cs; cs = PyCodeStats_Get(frame->f_code); if (cs->st_codebuf == NULL) { /* not already compiled, compile it now */ PyObject* g = frame->f_globals; int rec, module; stats_printf(("stats: full compile code: %s\n", PyCodeObject_NAME(frame->f_code))); if (cs->st_globals && PyInt_Check(cs->st_globals)) rec = PyInt_AS_LONG(cs->st_globals); else rec = DEFAULT_RECURSION; module = frame->f_globals == frame->f_locals; cs->st_codebuf = PsycoCode_CompileCode(frame->f_code, g, rec, module); if (cs->st_codebuf == Py_None) g = NULL; /* failed */ else { Py_INCREF(g); extra_assert(CodeBuffer_Check(cs->st_codebuf)); } Py_XDECREF(cs->st_globals); cs->st_globals = g; } /* already compiled a Psyco version, run it if the globals match */ extra_assert(frame->f_globals != NULL); if (cs->st_globals == frame->f_globals) { Py_INCREF(cs->st_codebuf); return cs->st_codebuf; } return NULL; }
static void stats_dump(void) { float top[STATLINES]; char* top_names[STATLINES]; int i, j, k=0; PyObject *key, *value; for (i=0; i<STATLINES; i++) top[i] = -1.0f; while (PyDict_Next(codestats_dict, &k, &key, &value)) { PyCodeStats* cs = (PyCodeStats*) key; PyCodeObject* co; extra_assert(PyCStruct_Check(key)); extra_assert(PyCode_Check(cs->cs_key)); co = (PyCodeObject*) cs->cs_key; for (i=0; i<STATLINES; i++) { if (cs->st_charge > top[i]) { for (j=STATLINES-1; j>i; j--) { top [j] = top [j-1]; top_names[j] = top_names[j-1]; } top [i] = cs->st_charge; top_names[i] = PyCodeObject_NAME(co); break; } } } for (i=0; i<STATLINES; i++) { if (top[i] < 0.0f) break; stats_printf(("stats: #%d %18g %s\n", i, top[i], top_names[i])); } }
static PyObject* profile_call(PyFrameObject* frame, PyObject* arg) { PyCodeStats* cs; psyco_stats_append(frame->f_tstate, frame->f_back); cs = PyCodeStats_Get(frame->f_code); if (cs->st_globals != NULL) { /* we want to accelerate this code object */ if (cs->st_codebuf == NULL) { /* not already compiled, compile it now */ PyObject* codebuf; PyObject* g = frame->f_globals; int rec, module; stats_printf(("stats: compile code: %s\n", PyCodeObject_NAME(frame->f_code))); if (PyInt_Check(cs->st_globals)) rec = PyInt_AS_LONG(cs->st_globals); else rec = DEFAULT_RECURSION; module = frame->f_globals == frame->f_locals; codebuf = PsycoCode_CompileCode(frame->f_code, g, rec, module); /* rare race condition: 'cs' might have been mutated during the call to PsycoCode_CompileCode(), so cs->st_codebuf might no longer be NULL, or cs->st_globals might be NULL again */ Py_XDECREF(cs->st_codebuf); cs->st_codebuf = codebuf; if (cs->st_codebuf == Py_None) g = NULL; /* failed */ else { Py_INCREF(g); extra_assert (CodeBuffer_Check(cs->st_codebuf)); } Py_XDECREF(cs->st_globals); cs->st_globals = g; } /* already compiled a Psyco version, run it if the globals match */ extra_assert(frame->f_globals != NULL); if (cs->st_globals == frame->f_globals) { Py_INCREF(cs->st_codebuf); return cs->st_codebuf; } } return NULL; }
PSY_INLINE bool call_ceval_hooks(ceval_events_t* cev, int what, PyFrameObject* f) { bool r = true; int n; struct cevents_s* events; PyObject* codebuf; PyObject* obj; extra_assert(what >= 0); if (what >= PyTrace_TOTAL) return true; /* Python >= 2.4 defines PyTrace_C_xxx */ #if VERBOSE_LEVEL >= 3 stats_printf(("hook: %d %s\n", what, PyCodeObject_NAME(f->f_code))); #endif events = cev->events + what; n = events->count; do { if (n == 0) return true; /* done */ n--; extra_assert(n < events->count); codebuf = events->items[n].fn(f, events->items[n].arg); if (events->items[n].fn == &deleted_ceval_hook) { events->items[n] = events->items[--events->count]; } } while (codebuf == NULL); /* call the other hooks, if any */ while (n != 0) { n--; extra_assert(n < events->count); obj = events->items[n].fn(f, events->items[n].arg); Py_XDECREF(obj); if (events->items[n].fn == &deleted_ceval_hook) { events->items[n] = events->items[--events->count]; } } /* enable recursive calls to call_ceval_hooks() */ f->f_tstate->use_tracing = 1; f->f_tstate->tracing--; /* run the compiled code */ r = PsycoCode_Run(codebuf, f, what == PyTrace_CALL); f->f_tstate->tracing++; Py_DECREF(codebuf); #if (PY_VERSION_HEX >= 0x02030000) && (PY_VERSION_HEX < 0x020300f0) if (!r) f->f_stacktop = NULL; /* work around a bug in Python 2.3b1 */ #endif return r; }
static PyObject* turbo_go(PyFrameObject* frame, PyObject* target_frame) { PyObject* result; ceval_events_t* cev = get_cevents(frame->f_tstate); /* single-shooting callback */ unset_ceval_hook(cev, PyTrace_LINE, &turbo_go, target_frame); if ((PyObject*) frame == target_frame) { /* the target is the current frame, compile it now */ stats_printf(("stats: compile frame: %s\n", PyCodeObject_NAME(frame->f_code))); result = PsycoCode_CompileFrame(frame, DEFAULT_RECURSION); if (result == Py_None) { Py_DECREF(result); result = NULL; } } else { /* hey, where is my frame? */ PyFrameObject* f = frame->f_back; stats_printf(("stats: where is my frame?\n")); for (; f; f = f->f_back) { if ((PyObject*) f == target_frame) { /* it is lower in the stack, wait until we return to it */ stats_printf(("stats: lower in the stack.\n")); set_ceval_hook(cev, PyTrace_RETURN, &turbo_wait, target_frame); break; } } /* if nowhere to be seen, forget it */ result = NULL; } if (!update_ceval_hooks(cev)) unset_ceval_hook(cev, PyTrace_RETURN, &turbo_wait, target_frame); return result; }
DEFINEFN bool psyco_turbo_frame(PyFrameObject* frame) { if (frame->f_lasti >= 0) { /* turbo-run the frame at the next possible occasion unless the frame is actually emulated from a Psyco frame */ ceval_events_t* cev = get_cevents(frame->f_tstate); stats_printf(("stats: turbo frame: %s\n", PyCodeObject_NAME(frame->f_code))); /* if (frame->f_tstate != PyThreadState_GET()) { */ /* stats_printf(("stats: TSTATE = %p, F_TSTATE=%p\n", */ /* PyThreadState_GET(), */ /* frame->f_tstate)); */ /* } */ set_ceval_hook(cev, PyTrace_LINE, &turbo_go, (PyObject*) frame); if (!update_ceval_hooks(cev)) { unset_ceval_hook(cev, PyTrace_LINE, &turbo_go, (PyObject*) frame); return false; } } return true; }
DEFINEFN void psyco_dump_code_buffers(void) { static int is_dumping = 0; FILE* f; #if CODE_DUMP >= 3 static int alt = 1; char filename[200]; sprintf(filename, "%s-%d", CODE_DUMP_FILE, alt); alt = 3-alt; #else char* filename = CODE_DUMP_FILE; #endif #if defined(PSYCO_TRACE) trace_flush(); #endif if (is_dumping) return; is_dumping = 1; f = fopen(filename, "wb"); if (f != NULL) { CodeBufferObject* obj; PyObject *exc, *val, *tb; long buftablepos; void** chain; int bufcount = 0; long* buftable; PyErr_Fetch(&exc, &val, &tb); debug_printf(1, ("writing %s\n", filename)); for (obj=psyco_codebuf_chained_list; obj != NULL; obj=obj->chained_list) bufcount++; buftable = PyMem_NEW(long, bufcount); fprintf(f, "Psyco dump [%s]\n", MACHINE_CODE_FORMAT); fwrite(&bufcount, sizeof(bufcount), 1, f); buftablepos = ftell(f); fwrite(buftable, sizeof(long), bufcount, f); /* give the address of an arbitrary symbol from the Python interpreter and from the Psyco module */ fprintf(f, "PyInt_FromLong: 0x%lx\n", (long) &PyInt_FromLong); fprintf(f, "psyco_dump_code_buffers: 0x%lx\n", (long) &psyco_dump_code_buffers); for (obj=psyco_codebuf_chained_list; obj != NULL; obj=obj->chained_list) { PyCodeObject* co = obj->snapshot.fz_pyc_data ? obj->snapshot.fz_pyc_data->co : NULL; fprintf(f, "CodeBufferObject 0x%lx %d '%s' '%s' %d '%s'\n", (long) obj->codestart, get_stack_depth(&obj->snapshot), co?PyString_AsString(co->co_filename):"", co?PyCodeObject_NAME(co):"", co?obj->snapshot.fz_pyc_data->next_instr:-1, obj->codemode); } psyco_dump_bigbuffers(f); for (chain = psyco_codebuf_spec_dict_list; chain; chain=(void**)*chain) { PyObject* spec_dict = (PyObject*)(chain[-1]); int i = 0; PyObject *key, *value; fprintf(f, "spec_dict 0x%lx\n", (long) chain); while (PyDict_Next(spec_dict, &i, &key, &value)) { PyObject* repr; if (PyInt_Check(key)) { repr = (key->ob_type->tp_as_number->nb_hex)(key); } else { repr = PyObject_Repr(key); } psyco_assert(!PyErr_Occurred()); psyco_assert(PyString_Check(repr)); psyco_assert(CodeBuffer_Check(value)); fprintf(f, "0x%lx %s\n", (long)((CodeBufferObject*)value)->codestart, PyString_AsString(repr)); Py_DECREF(repr); } fprintf(f, "\n"); } { int i = 0; fprintf(f, "vinfo_array\n"); for (obj=psyco_codebuf_chained_list; obj != NULL; obj=obj->chained_list) { PsycoObject* live_po; PyObject* d; if (psyco_top_array_count(&obj->snapshot) > 0) live_po = fpo_unfreeze(&obj->snapshot); else live_po = NULL; d = PyDict_New(); psyco_assert(d); buftable[i++] = ftell(f); vinfo_array_dump(live_po ? &live_po->vlocals : NullArray, f, d); Py_DECREF(d); if (live_po) PsycoObject_Delete(live_po); } psyco_assert(i==bufcount); fseek(f, buftablepos, 0); fwrite(buftable, sizeof(long), bufcount, f); } PyMem_FREE(buftable); psyco_assert(!PyErr_Occurred()); fclose(f); PyErr_Restore(exc, val, tb); }