Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 950361c

Browse files
committed
Patches for (two forms of) optional dynamic execution profiling --
i.e., counting opcode frequencies, or (with DXPAIRS defined) opcode pair frequencies. Define DYNAMIC_EXECUTION_PROFILE on the command line (for this file and for sysmodule.c) to enable.
1 parent 8c5df06 commit 950361c

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

Python/ceval.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ static int exec_statement PROTO((object *, object *, object *));
103103
static object *find_from_args PROTO((frameobject *, int));
104104

105105

106+
/* Dynamic execution profile */
107+
#ifdef DYNAMIC_EXECUTION_PROFILE
108+
#ifdef DXPAIRS
109+
static long dxpairs[257][256];
110+
#define dxp dxpairs[256]
111+
#else
112+
static long dxp[256];
113+
#endif
114+
#endif
115+
116+
106117
/* Pointer to current frame, used to link new frames to */
107118

108119
static frameobject *current_frame;
@@ -315,6 +326,9 @@ eval_code2(co, globals, locals,
315326
int defcount;
316327
object *owner;
317328
{
329+
#ifdef DXPAIRS
330+
int lastopcode = 0;
331+
#endif
318332
register unsigned char *next_instr;
319333
register int opcode = 0; /* Current opcode */
320334
register int oparg = 0; /* Current opcode argument, if any */
@@ -592,6 +606,13 @@ eval_code2(co, globals, locals,
592606
opcode = NEXTOP();
593607
if (HAS_ARG(opcode))
594608
oparg = NEXTARG();
609+
#ifdef DYNAMIC_EXECUTION_PROFILE
610+
#ifdef DXPAIRS
611+
dxpairs[lastopcode][opcode]++;
612+
lastopcode = opcode;
613+
#endif
614+
dxp[opcode]++;
615+
#endif
595616

596617
#ifdef LLTRACE
597618
/* Instruction tracing */
@@ -2961,3 +2982,50 @@ find_from_args(f, nexti)
29612982

29622983
return list;
29632984
}
2985+
2986+
2987+
#ifdef DYNAMIC_EXECUTION_PROFILE
2988+
2989+
PyObject *
2990+
getarray(a)
2991+
long a[256];
2992+
{
2993+
int i;
2994+
PyObject *l = PyList_New(256);
2995+
if (l == NULL) return NULL;
2996+
for (i = 0; i < 256; i++) {
2997+
PyObject *x = PyInt_FromLong(a[i]);
2998+
if (x == NULL) {
2999+
Py_DECREF(l);
3000+
return NULL;
3001+
}
3002+
PyList_SetItem(l, i, x);
3003+
}
3004+
for (i = 0; i < 256; i++)
3005+
a[i] = 0;
3006+
return l;
3007+
}
3008+
3009+
PyObject *
3010+
_Py_GetDXProfile(self, args)
3011+
PyObject *self, *args;
3012+
{
3013+
#ifndef DXPAIRS
3014+
return getarray(dxp);
3015+
#else
3016+
int i;
3017+
PyObject *l = PyList_New(257);
3018+
if (l == NULL) return NULL;
3019+
for (i = 0; i < 257; i++) {
3020+
PyObject *x = getarray(dxpairs[i]);
3021+
if (x == NULL) {
3022+
Py_DECREF(l);
3023+
return NULL;
3024+
}
3025+
PyList_SetItem(l, i, x);
3026+
}
3027+
return l;
3028+
#endif
3029+
}
3030+
3031+
#endif

0 commit comments

Comments
 (0)