📄 _hotshot.c
字号:
/* Portions Copyright (c) 2005 Nokia Corporation */
/*
* This is the High Performance Python Profiler portion of HotShot.
*/
#include "Python.h"
#include "compile.h"
#include "eval.h"
#include "frameobject.h"
#include "structmember.h"
/*
* Which timer to use should be made more configurable, but that should not
* be difficult. This will do for now.
*/
#ifdef MS_WIN32
#include <windows.h>
#include <largeint.h>
#include <direct.h> /* for getcwd() */
typedef __int64 hs_time;
#define GETTIMEOFDAY(P_HS_TIME) \
{ LARGE_INTEGER _temp; \
QueryPerformanceCounter(&_temp); \
*(P_HS_TIME) = _temp.QuadPart; }
#else
#ifndef HAVE_GETTIMEOFDAY
#error "This module requires gettimeofday() on non-Windows platforms!"
#endif
#ifdef macintosh
#include <sys/time.h>
#else
#include <sys/resource.h>
#include <sys/times.h>
#endif
typedef struct timeval hs_time;
#endif
#if !defined(__cplusplus) && !defined(inline)
#ifdef __GNUC__
#define inline __inline
#endif
#endif
#ifndef inline
#define inline
#endif
#define BUFFERSIZE 10240
#ifdef macintosh
#define PATH_MAX 254
#endif
#ifndef PATH_MAX
# ifdef MAX_PATH
# define PATH_MAX MAX_PATH
# elif defined (_POSIX_PATH_MAX)
# define PATH_MAX _POSIX_PATH_MAX
# else
# error "Need a defn. for PATH_MAX in _hotshot.c"
# endif
#endif
typedef struct {
PyObject_HEAD
PyObject *filemap;
PyObject *logfilename;
int index;
unsigned char buffer[BUFFERSIZE];
FILE *logfp;
int lineevents;
int linetimings;
int frametimings;
/* size_t filled; */
int active;
int next_fileno;
hs_time prev_timeofday;
} ProfilerObject;
typedef struct {
PyObject_HEAD
PyObject *info;
FILE *logfp;
int linetimings;
int frametimings;
} LogReaderObject;
static PyObject * ProfilerError = NULL;
#ifndef MS_WIN32
#ifdef GETTIMEOFDAY_NO_TZ
#define GETTIMEOFDAY(ptv) gettimeofday((ptv))
#else
#define GETTIMEOFDAY(ptv) gettimeofday((ptv), (struct timezone *)NULL)
#endif
#endif
/* The log reader... */
static char logreader_close__doc__[] =
"close()\n"
"Close the log file, preventing additional records from being read.";
static PyObject *
logreader_close(LogReaderObject *self, PyObject *args)
{
PyObject *result = NULL;
if (PyArg_ParseTuple(args, ":close")) {
if (self->logfp != NULL) {
fclose(self->logfp);
self->logfp = NULL;
}
result = Py_None;
Py_INCREF(result);
}
return result;
}
#if Py_TPFLAGS_HAVE_ITER
/* This is only used if the interpreter has iterator support; the
* iternext handler is also used as a helper for other functions, so
* does not need to be included in this conditional section.
*/
static PyObject *
logreader_tp_iter(LogReaderObject *self)
{
Py_INCREF(self);
return (PyObject *) self;
}
#endif
/* Log File Format
* ---------------
*
* The log file consists of a sequence of variable-length records.
* Each record is identified with a record type identifier in two
* bits of the first byte. The two bits are the "least significant"
* bits of the byte.
*
* Low bits: Opcode: Meaning:
* 0x00 ENTER enter a frame
* 0x01 EXIT exit a frame
* 0x02 LINENO SET_LINENO instruction was executed
* 0x03 OTHER more bits are needed to deecode
*
* If the type is OTHER, the record is not packed so tightly, and the
* remaining bits are used to disambiguate the record type. These
* records are not used as frequently so compaction is not an issue.
* Each of the first three record types has a highly tailored
* structure that allows it to be packed tightly.
*
* The OTHER records have the following identifiers:
*
* First byte: Opcode: Meaning:
* 0x13 ADD_INFO define a key/value pair
* 0x23 DEFINE_FILE define an int->filename mapping
* 0x33 LINE_TIMES indicates if LINENO events have tdeltas
* 0x43 DEFINE_FUNC define a (fileno,lineno)->funcname mapping
* 0x53 FRAME_TIMES indicates if ENTER/EXIT events have tdeltas
*
* Packed Integers
*
* "Packed integers" are non-negative integer values encoded as a
* sequence of bytes. Each byte is encoded such that the most
* significant bit is set if the next byte is also part of the
* integer. Each byte provides bits to the least-significant end of
* the result; the accumulated value must be shifted up to place the
* new bits into the result.
*
* "Modified packed integers" are packed integers where only a portion
* of the first byte is used. In the rest of the specification, these
* are referred to as "MPI(n,name)", where "n" is the number of bits
* discarded from the least-signicant positions of the byte, and
* "name" is a name being given to those "discarded" bits, since they
* are a field themselves.
*
* ENTER records:
*
* MPI(2,type) fileno -- type is 0x00
* PI lineno
* PI tdelta -- iff frame times are enabled
*
* EXIT records
*
* MPI(2,type) tdelta -- type is 0x01; tdelta will be 0
* if frame times are disabled
*
* LINENO records
*
* MPI(2,type) lineno -- type is 0x02
* PI tdelta -- iff LINENO includes it
*
* ADD_INFO records
*
* BYTE type -- always 0x13
* PI len1 -- length of first string
* BYTE string1[len1] -- len1 bytes of string data
* PI len2 -- length of second string
* BYTE string2[len2] -- len2 bytes of string data
*
* DEFINE_FILE records
*
* BYTE type -- always 0x23
* PI fileno
* PI len -- length of filename
* BYTE filename[len] -- len bytes of string data
*
* DEFINE_FUNC records
*
* BYTE type -- always 0x43
* PI fileno
* PI lineno
* PI len -- length of funcname
* BYTE funcname[len] -- len bytes of string data
*
* LINE_TIMES records
*
* This record can be used only before the start of ENTER/EXIT/LINENO
* records. If have_tdelta is true, LINENO records will include the
* tdelta field, otherwise it will be omitted. If this record is not
* given, LINENO records will not contain the tdelta field.
*
* BYTE type -- always 0x33
* BYTE have_tdelta -- 0 if LINENO does *not* have
* timing information
* FRAME_TIMES records
*
* This record can be used only before the start of ENTER/EXIT/LINENO
* records. If have_tdelta is true, ENTER and EXIT records will
* include the tdelta field, otherwise it will be omitted. If this
* record is not given, ENTER and EXIT records will contain the tdelta
* field.
*
* BYTE type -- always 0x53
* BYTE have_tdelta -- 0 if ENTER/EXIT do *not* have
* timing information
*/
#define WHAT_ENTER 0x00
#define WHAT_EXIT 0x01
#define WHAT_LINENO 0x02
#define WHAT_OTHER 0x03 /* only used in decoding */
#define WHAT_ADD_INFO 0x13
#define WHAT_DEFINE_FILE 0x23
#define WHAT_LINE_TIMES 0x33
#define WHAT_DEFINE_FUNC 0x43
#define WHAT_FRAME_TIMES 0x53
#define ERR_NONE 0
#define ERR_EOF -1
#define ERR_EXCEPTION -2
#define ERR_BAD_RECTYPE -3
#define PISIZE (sizeof(int) + 1)
#define MPISIZE (PISIZE + 1)
/* Maximum size of "normal" events -- nothing that contains string data */
#define MAXEVENTSIZE (MPISIZE + PISIZE*2)
/* Unpack a packed integer; if "discard" is non-zero, unpack a modified
* packed integer with "discard" discarded bits.
*/
static int
unpack_packed_int(LogReaderObject *self, int *pvalue, int discard)
{
int c;
int accum = 0;
int bits = 0;
int cont;
do {
/* read byte */
if ((c = fgetc(self->logfp)) == EOF)
return ERR_EOF;
accum |= ((c & 0x7F) >> discard) << bits;
bits += (7 - discard);
cont = c & 0x80;
discard = 0;
} while (cont);
*pvalue = accum;
return 0;
}
/* Unpack a string, which is encoded as a packed integer giving the
* length of the string, followed by the string data.
*/
static int
unpack_string(LogReaderObject *self, PyObject **pvalue)
{
int i;
int len;
int err;
char *buf;
if ((err = unpack_packed_int(self, &len, 0)))
return err;
buf = malloc(len);
for (i=0; i < len; i++) {
if ((buf[i] = fgetc(self->logfp)) == EOF) {
free(buf);
return ERR_EOF;
}
}
*pvalue = PyString_FromStringAndSize(buf, len);
free(buf);
if (*pvalue == NULL) {
return ERR_EXCEPTION;
}
return 0;
}
static int
unpack_add_info(LogReaderObject *self)
{
PyObject *key;
PyObject *value = NULL;
int err;
err = unpack_string(self, &key);
if (!err) {
err = unpack_string(self, &value);
if (err)
Py_DECREF(key);
else {
PyObject *list = PyDict_GetItem(self->info, key);
if (list == NULL) {
list = PyList_New(0);
if (list == NULL) {
err = ERR_EXCEPTION;
goto finally;
}
if (PyDict_SetItem(self->info, key, list)) {
err = ERR_EXCEPTION;
goto finally;
}
}
if (PyList_Append(list, value))
err = ERR_EXCEPTION;
}
}
finally:
Py_XDECREF(key);
Py_XDECREF(value);
return err;
}
static void
eof_error(void)
{
PyErr_SetString(PyExc_EOFError,
"end of file with incomplete profile record");
}
static PyObject *
logreader_tp_iternext(LogReaderObject *self)
{
int c;
int what;
int err = ERR_NONE;
int lineno = -1;
int fileno = -1;
int tdelta = -1;
PyObject *s1 = NULL, *s2 = NULL;
PyObject *result = NULL;
#if 0
unsigned char b0, b1;
#endif
if (self->logfp == NULL) {
PyErr_SetString(ProfilerError,
"cannot iterate over closed LogReader object");
return NULL;
}
restart:
/* decode the record type */
if ((c = fgetc(self->logfp)) == EOF)
return NULL;
what = c & WHAT_OTHER;
if (what == WHAT_OTHER)
what = c; /* need all the bits for type */
else
ungetc(c, self->logfp); /* type byte includes packed int */
switch (what) {
case WHAT_ENTER:
err = unpack_packed_int(self, &fileno, 2);
if (!err) {
err = unpack_packed_int(self, &lineno, 0);
if (self->frametimings && !err)
err = unpack_packed_int(self, &tdelta, 0);
}
break;
case WHAT_EXIT:
err = unpack_packed_int(self, &tdelta, 2);
break;
case WHAT_LINENO:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -