blob: 5e4a9f774792ed0c14e83ad0f52c182d5a5e15ec [file] [log] [blame]
Fred Drake8c081a12001-10-12 20:57:55 +00001/*
2 * This is the High Performance Python Profiler portion of HotShot.
3 */
4
Tim Peters885d4572001-11-28 20:27:42 +00005#include "Python.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "code.h"
Tim Peters885d4572001-11-28 20:27:42 +00007#include "eval.h"
8#include "frameobject.h"
9#include "structmember.h"
Fred Drake8c081a12001-10-12 20:57:55 +000010
Fred Drake8c081a12001-10-12 20:57:55 +000011/*
12 * Which timer to use should be made more configurable, but that should not
Tim Petersfeab23f2001-10-13 00:11:10 +000013 * be difficult. This will do for now.
Fred Drake8c081a12001-10-12 20:57:55 +000014 */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000015#ifdef MS_WINDOWS
Fred Drake8c081a12001-10-12 20:57:55 +000016#include <windows.h>
Tim Peters1566a172001-10-12 22:08:39 +000017#include <direct.h> /* for getcwd() */
Tim Peters7d99ff22001-10-13 07:37:52 +000018typedef __int64 hs_time;
19#define GETTIMEOFDAY(P_HS_TIME) \
20 { LARGE_INTEGER _temp; \
21 QueryPerformanceCounter(&_temp); \
22 *(P_HS_TIME) = _temp.QuadPart; }
23
Tim Petersfeab23f2001-10-13 00:11:10 +000024
Fred Drake8c081a12001-10-12 20:57:55 +000025#else
26#ifndef HAVE_GETTIMEOFDAY
27#error "This module requires gettimeofday() on non-Windows platforms!"
28#endif
Jack Janseneddc1442003-11-20 01:44:59 +000029#if (defined(PYOS_OS2) && defined(PYCC_GCC))
Jack Jansen963659a2001-10-23 22:26:16 +000030#include <sys/time.h>
31#else
Fred Drake8c081a12001-10-12 20:57:55 +000032#include <sys/resource.h>
33#include <sys/times.h>
Jack Jansen963659a2001-10-23 22:26:16 +000034#endif
Fred Drake8c081a12001-10-12 20:57:55 +000035typedef struct timeval hs_time;
36#endif
37
38#if !defined(__cplusplus) && !defined(inline)
39#ifdef __GNUC__
40#define inline __inline
41#endif
42#endif
43
44#ifndef inline
45#define inline
46#endif
47
48#define BUFFERSIZE 10240
49
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000050#if defined(PYOS_OS2) && defined(PYCC_GCC)
51#define PATH_MAX 260
52#endif
53
Martin v. Löwis8eb92a02002-09-19 08:03:21 +000054#if defined(__sgi) && _COMPILER_VERSION>700 && !defined(PATH_MAX)
55/* fix PATH_MAX not being defined with MIPSPro 7.x
56 if mode is ANSI C (default) */
57#define PATH_MAX 1024
58#endif
59
Tim Peters1566a172001-10-12 22:08:39 +000060#ifndef PATH_MAX
61# ifdef MAX_PATH
62# define PATH_MAX MAX_PATH
Martin v. Löwis21ee4092002-09-30 16:19:48 +000063# elif defined (_POSIX_PATH_MAX)
64# define PATH_MAX _POSIX_PATH_MAX
Tim Peters1566a172001-10-12 22:08:39 +000065# else
66# error "Need a defn. for PATH_MAX in _hotshot.c"
67# endif
68#endif
69
Fred Drake8c081a12001-10-12 20:57:55 +000070typedef struct {
71 PyObject_HEAD
72 PyObject *filemap;
73 PyObject *logfilename;
74 int index;
75 unsigned char buffer[BUFFERSIZE];
76 FILE *logfp;
77 int lineevents;
78 int linetimings;
Fred Drake30d1c752001-10-15 22:11:02 +000079 int frametimings;
Fred Drake8c081a12001-10-12 20:57:55 +000080 /* size_t filled; */
81 int active;
82 int next_fileno;
Fred Drake8c081a12001-10-12 20:57:55 +000083 hs_time prev_timeofday;
84} ProfilerObject;
85
86typedef struct {
87 PyObject_HEAD
Fred Drake4c2e1af2001-10-29 20:45:57 +000088 PyObject *info;
Fred Drake8c081a12001-10-12 20:57:55 +000089 FILE *logfp;
Fred Drake8c081a12001-10-12 20:57:55 +000090 int linetimings;
Fred Drake30d1c752001-10-15 22:11:02 +000091 int frametimings;
Fred Drake8c081a12001-10-12 20:57:55 +000092} LogReaderObject;
93
94static PyObject * ProfilerError = NULL;
95
96
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000097#ifndef MS_WINDOWS
Fred Drake8c081a12001-10-12 20:57:55 +000098#ifdef GETTIMEOFDAY_NO_TZ
99#define GETTIMEOFDAY(ptv) gettimeofday((ptv))
100#else
101#define GETTIMEOFDAY(ptv) gettimeofday((ptv), (struct timezone *)NULL)
102#endif
103#endif
104
105
106/* The log reader... */
107
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000108PyDoc_STRVAR(logreader_close__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +0000109"close()\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000110"Close the log file, preventing additional records from being read.");
Fred Drake8c081a12001-10-12 20:57:55 +0000111
112static PyObject *
113logreader_close(LogReaderObject *self, PyObject *args)
114{
Fred Drake666bf522002-07-18 19:11:44 +0000115 if (self->logfp != NULL) {
116 fclose(self->logfp);
117 self->logfp = NULL;
Fred Drake8c081a12001-10-12 20:57:55 +0000118 }
Fred Drake666bf522002-07-18 19:11:44 +0000119 Py_INCREF(Py_None);
120
121 return Py_None;
122}
123
124PyDoc_STRVAR(logreader_fileno__doc__,
125"fileno() -> file descriptor\n"
126"Returns the file descriptor for the log file, if open.\n"
127"Raises ValueError if the log file is closed.");
128
129static PyObject *
130logreader_fileno(LogReaderObject *self)
131{
132 if (self->logfp == NULL) {
133 PyErr_SetString(PyExc_ValueError,
134 "logreader's file object already closed");
135 return NULL;
136 }
137 return PyInt_FromLong(fileno(self->logfp));
Fred Drake8c081a12001-10-12 20:57:55 +0000138}
139
Fred Drake8c081a12001-10-12 20:57:55 +0000140
141/* Log File Format
142 * ---------------
143 *
144 * The log file consists of a sequence of variable-length records.
145 * Each record is identified with a record type identifier in two
146 * bits of the first byte. The two bits are the "least significant"
147 * bits of the byte.
148 *
149 * Low bits: Opcode: Meaning:
150 * 0x00 ENTER enter a frame
151 * 0x01 EXIT exit a frame
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000152 * 0x02 LINENO execution moved onto a different line
Fred Drake8c081a12001-10-12 20:57:55 +0000153 * 0x03 OTHER more bits are needed to deecode
154 *
155 * If the type is OTHER, the record is not packed so tightly, and the
156 * remaining bits are used to disambiguate the record type. These
157 * records are not used as frequently so compaction is not an issue.
158 * Each of the first three record types has a highly tailored
159 * structure that allows it to be packed tightly.
160 *
161 * The OTHER records have the following identifiers:
162 *
163 * First byte: Opcode: Meaning:
164 * 0x13 ADD_INFO define a key/value pair
165 * 0x23 DEFINE_FILE define an int->filename mapping
166 * 0x33 LINE_TIMES indicates if LINENO events have tdeltas
Fred Drake30d1c752001-10-15 22:11:02 +0000167 * 0x43 DEFINE_FUNC define a (fileno,lineno)->funcname mapping
168 * 0x53 FRAME_TIMES indicates if ENTER/EXIT events have tdeltas
Fred Drake8c081a12001-10-12 20:57:55 +0000169 *
170 * Packed Integers
171 *
172 * "Packed integers" are non-negative integer values encoded as a
173 * sequence of bytes. Each byte is encoded such that the most
174 * significant bit is set if the next byte is also part of the
175 * integer. Each byte provides bits to the least-significant end of
176 * the result; the accumulated value must be shifted up to place the
177 * new bits into the result.
178 *
179 * "Modified packed integers" are packed integers where only a portion
180 * of the first byte is used. In the rest of the specification, these
181 * are referred to as "MPI(n,name)", where "n" is the number of bits
182 * discarded from the least-signicant positions of the byte, and
183 * "name" is a name being given to those "discarded" bits, since they
184 * are a field themselves.
185 *
186 * ENTER records:
187 *
188 * MPI(2,type) fileno -- type is 0x00
Fred Drake8c081a12001-10-12 20:57:55 +0000189 * PI lineno
Fred Drake30d1c752001-10-15 22:11:02 +0000190 * PI tdelta -- iff frame times are enabled
Fred Drake8c081a12001-10-12 20:57:55 +0000191 *
192 * EXIT records
193 *
Fred Drake30d1c752001-10-15 22:11:02 +0000194 * MPI(2,type) tdelta -- type is 0x01; tdelta will be 0
195 * if frame times are disabled
Fred Drake8c081a12001-10-12 20:57:55 +0000196 *
197 * LINENO records
198 *
199 * MPI(2,type) lineno -- type is 0x02
200 * PI tdelta -- iff LINENO includes it
201 *
202 * ADD_INFO records
203 *
Fred Drake30d1c752001-10-15 22:11:02 +0000204 * BYTE type -- always 0x13
Fred Drake8c081a12001-10-12 20:57:55 +0000205 * PI len1 -- length of first string
206 * BYTE string1[len1] -- len1 bytes of string data
207 * PI len2 -- length of second string
208 * BYTE string2[len2] -- len2 bytes of string data
209 *
210 * DEFINE_FILE records
211 *
Fred Drake30d1c752001-10-15 22:11:02 +0000212 * BYTE type -- always 0x23
Fred Drake8c081a12001-10-12 20:57:55 +0000213 * PI fileno
214 * PI len -- length of filename
215 * BYTE filename[len] -- len bytes of string data
216 *
Fred Drake30d1c752001-10-15 22:11:02 +0000217 * DEFINE_FUNC records
218 *
219 * BYTE type -- always 0x43
220 * PI fileno
221 * PI lineno
222 * PI len -- length of funcname
223 * BYTE funcname[len] -- len bytes of string data
224 *
Fred Drake8c081a12001-10-12 20:57:55 +0000225 * LINE_TIMES records
Fred Drake30d1c752001-10-15 22:11:02 +0000226 *
227 * This record can be used only before the start of ENTER/EXIT/LINENO
228 * records. If have_tdelta is true, LINENO records will include the
229 * tdelta field, otherwise it will be omitted. If this record is not
230 * given, LINENO records will not contain the tdelta field.
231 *
232 * BYTE type -- always 0x33
Fred Drake8c081a12001-10-12 20:57:55 +0000233 * BYTE have_tdelta -- 0 if LINENO does *not* have
234 * timing information
Fred Drake30d1c752001-10-15 22:11:02 +0000235 * FRAME_TIMES records
236 *
237 * This record can be used only before the start of ENTER/EXIT/LINENO
238 * records. If have_tdelta is true, ENTER and EXIT records will
239 * include the tdelta field, otherwise it will be omitted. If this
240 * record is not given, ENTER and EXIT records will contain the tdelta
241 * field.
242 *
243 * BYTE type -- always 0x53
244 * BYTE have_tdelta -- 0 if ENTER/EXIT do *not* have
245 * timing information
Fred Drake8c081a12001-10-12 20:57:55 +0000246 */
247
248#define WHAT_ENTER 0x00
249#define WHAT_EXIT 0x01
250#define WHAT_LINENO 0x02
251#define WHAT_OTHER 0x03 /* only used in decoding */
252#define WHAT_ADD_INFO 0x13
253#define WHAT_DEFINE_FILE 0x23
254#define WHAT_LINE_TIMES 0x33
Fred Drake30d1c752001-10-15 22:11:02 +0000255#define WHAT_DEFINE_FUNC 0x43
256#define WHAT_FRAME_TIMES 0x53
Fred Drake8c081a12001-10-12 20:57:55 +0000257
258#define ERR_NONE 0
259#define ERR_EOF -1
260#define ERR_EXCEPTION -2
Fred Drake4c2e1af2001-10-29 20:45:57 +0000261#define ERR_BAD_RECTYPE -3
Fred Drake8c081a12001-10-12 20:57:55 +0000262
263#define PISIZE (sizeof(int) + 1)
264#define MPISIZE (PISIZE + 1)
265
266/* Maximum size of "normal" events -- nothing that contains string data */
267#define MAXEVENTSIZE (MPISIZE + PISIZE*2)
268
269
270/* Unpack a packed integer; if "discard" is non-zero, unpack a modified
271 * packed integer with "discard" discarded bits.
272 */
273static int
274unpack_packed_int(LogReaderObject *self, int *pvalue, int discard)
275{
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000276 int c;
Fred Drake8c081a12001-10-12 20:57:55 +0000277 int accum = 0;
278 int bits = 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000279 int cont;
280
281 do {
Fred Drake8c081a12001-10-12 20:57:55 +0000282 /* read byte */
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000283 if ((c = fgetc(self->logfp)) == EOF)
284 return ERR_EOF;
285 accum |= ((c & 0x7F) >> discard) << bits;
Fred Drake8c081a12001-10-12 20:57:55 +0000286 bits += (7 - discard);
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000287 cont = c & 0x80;
Fred Drake8c081a12001-10-12 20:57:55 +0000288 discard = 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000289 } while (cont);
290
Fred Drake8c081a12001-10-12 20:57:55 +0000291 *pvalue = accum;
292
293 return 0;
294}
295
296/* Unpack a string, which is encoded as a packed integer giving the
297 * length of the string, followed by the string data.
298 */
299static int
300unpack_string(LogReaderObject *self, PyObject **pvalue)
301{
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000302 int i;
Fred Drake8c081a12001-10-12 20:57:55 +0000303 int len;
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000304 int err;
Guido van Rossum0b624f62002-07-20 00:38:01 +0000305 int ch;
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000306 char *buf;
307
308 if ((err = unpack_packed_int(self, &len, 0)))
309 return err;
Fred Drake8c081a12001-10-12 20:57:55 +0000310
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000311 buf = malloc(len);
312 for (i=0; i < len; i++) {
Guido van Rossum0b624f62002-07-20 00:38:01 +0000313 ch = fgetc(self->logfp);
314 buf[i] = ch;
315 if (ch == EOF) {
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000316 free(buf);
317 return ERR_EOF;
Fred Drake8c081a12001-10-12 20:57:55 +0000318 }
319 }
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000320 *pvalue = PyString_FromStringAndSize(buf, len);
321 free(buf);
322 if (*pvalue == NULL) {
323 return ERR_EXCEPTION;
324 }
325 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000326}
327
328
Fred Drake4c2e1af2001-10-29 20:45:57 +0000329static int
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000330unpack_add_info(LogReaderObject *self)
Fred Drake4c2e1af2001-10-29 20:45:57 +0000331{
332 PyObject *key;
333 PyObject *value = NULL;
334 int err;
335
Fred Drake4c2e1af2001-10-29 20:45:57 +0000336 err = unpack_string(self, &key);
337 if (!err) {
338 err = unpack_string(self, &value);
339 if (err)
340 Py_DECREF(key);
341 else {
342 PyObject *list = PyDict_GetItem(self->info, key);
343 if (list == NULL) {
344 list = PyList_New(0);
345 if (list == NULL) {
346 err = ERR_EXCEPTION;
347 goto finally;
348 }
349 if (PyDict_SetItem(self->info, key, list)) {
Armin Rigoa4127692004-08-03 08:33:55 +0000350 Py_DECREF(list);
Fred Drake4c2e1af2001-10-29 20:45:57 +0000351 err = ERR_EXCEPTION;
352 goto finally;
353 }
Armin Rigoa4127692004-08-03 08:33:55 +0000354 Py_DECREF(list);
Fred Drake4c2e1af2001-10-29 20:45:57 +0000355 }
356 if (PyList_Append(list, value))
357 err = ERR_EXCEPTION;
358 }
359 }
360 finally:
361 Py_XDECREF(key);
362 Py_XDECREF(value);
363 return err;
364}
365
366
367static void
Fred Drake666bf522002-07-18 19:11:44 +0000368eof_error(LogReaderObject *self)
Fred Drake4c2e1af2001-10-29 20:45:57 +0000369{
Fred Drake666bf522002-07-18 19:11:44 +0000370 fclose(self->logfp);
371 self->logfp = NULL;
Fred Drake4c2e1af2001-10-29 20:45:57 +0000372 PyErr_SetString(PyExc_EOFError,
373 "end of file with incomplete profile record");
374}
375
Fred Drake8c081a12001-10-12 20:57:55 +0000376static PyObject *
377logreader_tp_iternext(LogReaderObject *self)
378{
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000379 int c;
380 int what;
Fred Drake8c081a12001-10-12 20:57:55 +0000381 int err = ERR_NONE;
382 int lineno = -1;
383 int fileno = -1;
384 int tdelta = -1;
385 PyObject *s1 = NULL, *s2 = NULL;
386 PyObject *result = NULL;
387#if 0
388 unsigned char b0, b1;
389#endif
390
391 if (self->logfp == NULL) {
392 PyErr_SetString(ProfilerError,
393 "cannot iterate over closed LogReader object");
394 return NULL;
395 }
Fred Drake4c2e1af2001-10-29 20:45:57 +0000396
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000397restart:
398 /* decode the record type */
Fred Drake666bf522002-07-18 19:11:44 +0000399 if ((c = fgetc(self->logfp)) == EOF) {
400 fclose(self->logfp);
401 self->logfp = NULL;
Fred Drake8c081a12001-10-12 20:57:55 +0000402 return NULL;
Fred Drake666bf522002-07-18 19:11:44 +0000403 }
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000404 what = c & WHAT_OTHER;
405 if (what == WHAT_OTHER)
406 what = c; /* need all the bits for type */
407 else
408 ungetc(c, self->logfp); /* type byte includes packed int */
Fred Drake8c081a12001-10-12 20:57:55 +0000409
Fred Drake8c081a12001-10-12 20:57:55 +0000410 switch (what) {
411 case WHAT_ENTER:
412 err = unpack_packed_int(self, &fileno, 2);
413 if (!err) {
Fred Drake30d1c752001-10-15 22:11:02 +0000414 err = unpack_packed_int(self, &lineno, 0);
415 if (self->frametimings && !err)
416 err = unpack_packed_int(self, &tdelta, 0);
Fred Drake8c081a12001-10-12 20:57:55 +0000417 }
418 break;
419 case WHAT_EXIT:
420 err = unpack_packed_int(self, &tdelta, 2);
421 break;
422 case WHAT_LINENO:
423 err = unpack_packed_int(self, &lineno, 2);
424 if (self->linetimings && !err)
425 err = unpack_packed_int(self, &tdelta, 0);
426 break;
427 case WHAT_ADD_INFO:
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000428 err = unpack_add_info(self);
Fred Drake8c081a12001-10-12 20:57:55 +0000429 break;
430 case WHAT_DEFINE_FILE:
431 err = unpack_packed_int(self, &fileno, 0);
432 if (!err) {
433 err = unpack_string(self, &s1);
434 if (!err) {
435 Py_INCREF(Py_None);
436 s2 = Py_None;
437 }
438 }
439 break;
Fred Drake30d1c752001-10-15 22:11:02 +0000440 case WHAT_DEFINE_FUNC:
441 err = unpack_packed_int(self, &fileno, 0);
442 if (!err) {
443 err = unpack_packed_int(self, &lineno, 0);
444 if (!err)
445 err = unpack_string(self, &s1);
446 }
447 break;
Fred Drake8c081a12001-10-12 20:57:55 +0000448 case WHAT_LINE_TIMES:
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000449 if ((c = fgetc(self->logfp)) == EOF)
Fred Drake8c081a12001-10-12 20:57:55 +0000450 err = ERR_EOF;
451 else {
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000452 self->linetimings = c ? 1 : 0;
453 goto restart;
454 }
Fred Drake4c2e1af2001-10-29 20:45:57 +0000455 break;
Fred Drake30d1c752001-10-15 22:11:02 +0000456 case WHAT_FRAME_TIMES:
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000457 if ((c = fgetc(self->logfp)) == EOF)
Fred Drake30d1c752001-10-15 22:11:02 +0000458 err = ERR_EOF;
459 else {
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000460 self->frametimings = c ? 1 : 0;
461 goto restart;
462 }
Fred Drake4c2e1af2001-10-29 20:45:57 +0000463 break;
Fred Drake8c081a12001-10-12 20:57:55 +0000464 default:
Fred Drake4c2e1af2001-10-29 20:45:57 +0000465 err = ERR_BAD_RECTYPE;
Fred Drake8c081a12001-10-12 20:57:55 +0000466 }
Fred Drake4c2e1af2001-10-29 20:45:57 +0000467 if (err == ERR_BAD_RECTYPE) {
468 PyErr_SetString(PyExc_ValueError,
469 "unknown record type in log file");
470 }
471 else if (err == ERR_EOF) {
Fred Drake666bf522002-07-18 19:11:44 +0000472 eof_error(self);
Fred Drake8c081a12001-10-12 20:57:55 +0000473 }
474 else if (!err) {
475 result = PyTuple_New(4);
476 PyTuple_SET_ITEM(result, 0, PyInt_FromLong(what));
477 PyTuple_SET_ITEM(result, 2, PyInt_FromLong(fileno));
Fred Drake30d1c752001-10-15 22:11:02 +0000478 if (s1 == NULL)
Fred Drake8c081a12001-10-12 20:57:55 +0000479 PyTuple_SET_ITEM(result, 1, PyInt_FromLong(tdelta));
Fred Drake30d1c752001-10-15 22:11:02 +0000480 else
Fred Drake8c081a12001-10-12 20:57:55 +0000481 PyTuple_SET_ITEM(result, 1, s1);
Fred Drake30d1c752001-10-15 22:11:02 +0000482 if (s2 == NULL)
483 PyTuple_SET_ITEM(result, 3, PyInt_FromLong(lineno));
484 else
Fred Drake8c081a12001-10-12 20:57:55 +0000485 PyTuple_SET_ITEM(result, 3, s2);
Fred Drake8c081a12001-10-12 20:57:55 +0000486 }
487 /* The only other case is err == ERR_EXCEPTION, in which case the
488 * exception is already set.
489 */
490#if 0
491 b0 = self->buffer[self->index];
492 b1 = self->buffer[self->index + 1];
493 if (b0 & 1) {
494 /* This is a line-number event. */
495 what = PyTrace_LINE;
496 lineno = ((b0 & ~1) << 7) + b1;
497 self->index += 2;
498 }
499 else {
500 what = (b0 & 0x0E) >> 1;
501 tdelta = ((b0 & 0xF0) << 4) + b1;
502 if (what == PyTrace_CALL) {
503 /* we know there's a 2-byte file ID & 2-byte line number */
504 fileno = ((self->buffer[self->index + 2] << 8)
505 + self->buffer[self->index + 3]);
506 lineno = ((self->buffer[self->index + 4] << 8)
507 + self->buffer[self->index + 5]);
508 self->index += 6;
509 }
510 else
511 self->index += 2;
512 }
513#endif
514 return result;
515}
516
517static void
518logreader_dealloc(LogReaderObject *self)
519{
520 if (self->logfp != NULL) {
521 fclose(self->logfp);
522 self->logfp = NULL;
523 }
Armin Rigoa4127692004-08-03 08:33:55 +0000524 Py_XDECREF(self->info);
Fred Drake8c081a12001-10-12 20:57:55 +0000525 PyObject_Del(self);
526}
527
528static PyObject *
529logreader_sq_item(LogReaderObject *self, int index)
530{
531 PyObject *result = logreader_tp_iternext(self);
532 if (result == NULL && !PyErr_Occurred()) {
533 PyErr_SetString(PyExc_IndexError, "no more events in log");
534 return NULL;
535 }
536 return result;
537}
538
Fred Drake62c1e3c2001-12-04 21:40:53 +0000539static void
540do_stop(ProfilerObject *self);
Fred Drake8c081a12001-10-12 20:57:55 +0000541
542static int
543flush_data(ProfilerObject *self)
544{
545 /* Need to dump data to the log file... */
546 size_t written = fwrite(self->buffer, 1, self->index, self->logfp);
Tim Peters1566a172001-10-12 22:08:39 +0000547 if (written == (size_t)self->index)
Fred Drake8c081a12001-10-12 20:57:55 +0000548 self->index = 0;
549 else {
550 memmove(self->buffer, &self->buffer[written],
551 self->index - written);
552 self->index -= written;
553 if (written == 0) {
554 char *s = PyString_AsString(self->logfilename);
555 PyErr_SetFromErrnoWithFilename(PyExc_IOError, s);
Fred Drake62c1e3c2001-12-04 21:40:53 +0000556 do_stop(self);
Fred Drake8c081a12001-10-12 20:57:55 +0000557 return -1;
558 }
559 }
560 if (written > 0) {
561 if (fflush(self->logfp)) {
562 char *s = PyString_AsString(self->logfilename);
563 PyErr_SetFromErrnoWithFilename(PyExc_IOError, s);
Fred Drake62c1e3c2001-12-04 21:40:53 +0000564 do_stop(self);
Fred Drake8c081a12001-10-12 20:57:55 +0000565 return -1;
566 }
567 }
568 return 0;
569}
570
Fred Drake62c1e3c2001-12-04 21:40:53 +0000571static inline int
Fred Drake8c081a12001-10-12 20:57:55 +0000572pack_packed_int(ProfilerObject *self, int value)
573{
574 unsigned char partial;
575
576 do {
577 partial = value & 0x7F;
578 value >>= 7;
579 if (value)
580 partial |= 0x80;
581 self->buffer[self->index] = partial;
582 self->index++;
583 } while (value);
Fred Drake62c1e3c2001-12-04 21:40:53 +0000584 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000585}
586
587/* Encode a modified packed integer, with a subfield of modsize bits
588 * containing the value "subfield". The value of subfield is not
589 * checked to ensure it actually fits in modsize bits.
590 */
Fred Drake62c1e3c2001-12-04 21:40:53 +0000591static inline int
Fred Drake8c081a12001-10-12 20:57:55 +0000592pack_modified_packed_int(ProfilerObject *self, int value,
593 int modsize, int subfield)
594{
595 const int maxvalues[] = {-1, 1, 3, 7, 15, 31, 63, 127};
596
597 int bits = 7 - modsize;
598 int partial = value & maxvalues[bits];
599 unsigned char b = subfield | (partial << modsize);
600
601 if (partial != value) {
602 b |= 0x80;
603 self->buffer[self->index] = b;
604 self->index++;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000605 return pack_packed_int(self, value >> bits);
Fred Drake8c081a12001-10-12 20:57:55 +0000606 }
Fred Drake62c1e3c2001-12-04 21:40:53 +0000607 self->buffer[self->index] = b;
608 self->index++;
609 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000610}
611
Fred Drake62c1e3c2001-12-04 21:40:53 +0000612static int
Fred Drake30d1c752001-10-15 22:11:02 +0000613pack_string(ProfilerObject *self, const char *s, int len)
Fred Drake8c081a12001-10-12 20:57:55 +0000614{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000615 if (len + PISIZE + self->index >= BUFFERSIZE) {
616 if (flush_data(self) < 0)
617 return -1;
618 }
619 if (pack_packed_int(self, len) < 0)
620 return -1;
Fred Drake8c081a12001-10-12 20:57:55 +0000621 memcpy(self->buffer + self->index, s, len);
622 self->index += len;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000623 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000624}
625
Fred Drake62c1e3c2001-12-04 21:40:53 +0000626static int
Fred Drake8c081a12001-10-12 20:57:55 +0000627pack_add_info(ProfilerObject *self, const char *s1, const char *s2)
628{
629 int len1 = strlen(s1);
630 int len2 = strlen(s2);
631
Fred Drake62c1e3c2001-12-04 21:40:53 +0000632 if (len1 + len2 + PISIZE*2 + 1 + self->index >= BUFFERSIZE) {
633 if (flush_data(self) < 0)
634 return -1;
635 }
Fred Drake8c081a12001-10-12 20:57:55 +0000636 self->buffer[self->index] = WHAT_ADD_INFO;
637 self->index++;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000638 if (pack_string(self, s1, len1) < 0)
639 return -1;
640 return pack_string(self, s2, len2);
Fred Drake8c081a12001-10-12 20:57:55 +0000641}
642
Fred Drake62c1e3c2001-12-04 21:40:53 +0000643static int
Fred Drake8c081a12001-10-12 20:57:55 +0000644pack_define_file(ProfilerObject *self, int fileno, const char *filename)
645{
646 int len = strlen(filename);
647
Fred Drake62c1e3c2001-12-04 21:40:53 +0000648 if (len + PISIZE*2 + 1 + self->index >= BUFFERSIZE) {
649 if (flush_data(self) < 0)
650 return -1;
651 }
Fred Drake8c081a12001-10-12 20:57:55 +0000652 self->buffer[self->index] = WHAT_DEFINE_FILE;
653 self->index++;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000654 if (pack_packed_int(self, fileno) < 0)
655 return -1;
656 return pack_string(self, filename, len);
Fred Drake30d1c752001-10-15 22:11:02 +0000657}
658
Fred Drake62c1e3c2001-12-04 21:40:53 +0000659static int
Fred Drake30d1c752001-10-15 22:11:02 +0000660pack_define_func(ProfilerObject *self, int fileno, int lineno,
661 const char *funcname)
662{
663 int len = strlen(funcname);
664
Fred Drake62c1e3c2001-12-04 21:40:53 +0000665 if (len + PISIZE*3 + 1 + self->index >= BUFFERSIZE) {
666 if (flush_data(self) < 0)
667 return -1;
668 }
Fred Drake30d1c752001-10-15 22:11:02 +0000669 self->buffer[self->index] = WHAT_DEFINE_FUNC;
670 self->index++;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000671 if (pack_packed_int(self, fileno) < 0)
672 return -1;
673 if (pack_packed_int(self, lineno) < 0)
674 return -1;
675 return pack_string(self, funcname, len);
Fred Drake8c081a12001-10-12 20:57:55 +0000676}
677
Fred Drake62c1e3c2001-12-04 21:40:53 +0000678static int
Fred Drake8c081a12001-10-12 20:57:55 +0000679pack_line_times(ProfilerObject *self)
680{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000681 if (2 + self->index >= BUFFERSIZE) {
682 if (flush_data(self) < 0)
683 return -1;
684 }
Fred Drake8c081a12001-10-12 20:57:55 +0000685 self->buffer[self->index] = WHAT_LINE_TIMES;
686 self->buffer[self->index + 1] = self->linetimings ? 1 : 0;
687 self->index += 2;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000688 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000689}
690
Fred Drake62c1e3c2001-12-04 21:40:53 +0000691static int
Fred Drake30d1c752001-10-15 22:11:02 +0000692pack_frame_times(ProfilerObject *self)
693{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000694 if (2 + self->index >= BUFFERSIZE) {
695 if (flush_data(self) < 0)
696 return -1;
697 }
Fred Drake30d1c752001-10-15 22:11:02 +0000698 self->buffer[self->index] = WHAT_FRAME_TIMES;
699 self->buffer[self->index + 1] = self->frametimings ? 1 : 0;
700 self->index += 2;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000701 return 0;
Fred Drake30d1c752001-10-15 22:11:02 +0000702}
703
Fred Drake62c1e3c2001-12-04 21:40:53 +0000704static inline int
Fred Drake8c081a12001-10-12 20:57:55 +0000705pack_enter(ProfilerObject *self, int fileno, int tdelta, int lineno)
706{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000707 if (MPISIZE + PISIZE*2 + self->index >= BUFFERSIZE) {
708 if (flush_data(self) < 0)
709 return -1;
710 }
Fred Drake8c081a12001-10-12 20:57:55 +0000711 pack_modified_packed_int(self, fileno, 2, WHAT_ENTER);
Fred Drake8c081a12001-10-12 20:57:55 +0000712 pack_packed_int(self, lineno);
Fred Drake30d1c752001-10-15 22:11:02 +0000713 if (self->frametimings)
Fred Drake62c1e3c2001-12-04 21:40:53 +0000714 return pack_packed_int(self, tdelta);
715 else
716 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000717}
718
Fred Drake62c1e3c2001-12-04 21:40:53 +0000719static inline int
Fred Drake8c081a12001-10-12 20:57:55 +0000720pack_exit(ProfilerObject *self, int tdelta)
721{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000722 if (MPISIZE + self->index >= BUFFERSIZE) {
723 if (flush_data(self) < 0)
724 return -1;
Fred Drake30d1c752001-10-15 22:11:02 +0000725 }
Fred Drake62c1e3c2001-12-04 21:40:53 +0000726 if (self->frametimings)
727 return pack_modified_packed_int(self, tdelta, 2, WHAT_EXIT);
728 self->buffer[self->index] = WHAT_EXIT;
729 self->index++;
730 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000731}
732
Fred Drake62c1e3c2001-12-04 21:40:53 +0000733static inline int
Fred Drake8c081a12001-10-12 20:57:55 +0000734pack_lineno(ProfilerObject *self, int lineno)
735{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000736 if (MPISIZE + self->index >= BUFFERSIZE) {
737 if (flush_data(self) < 0)
738 return -1;
739 }
740 return pack_modified_packed_int(self, lineno, 2, WHAT_LINENO);
Fred Drake8c081a12001-10-12 20:57:55 +0000741}
742
Fred Drake62c1e3c2001-12-04 21:40:53 +0000743static inline int
Fred Drake8c081a12001-10-12 20:57:55 +0000744pack_lineno_tdelta(ProfilerObject *self, int lineno, int tdelta)
745{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000746 if (MPISIZE + PISIZE + self->index >= BUFFERSIZE) {
747 if (flush_data(self) < 0)
748 return 0;
749 }
750 if (pack_modified_packed_int(self, lineno, 2, WHAT_LINENO) < 0)
751 return -1;
752 return pack_packed_int(self, tdelta);
Fred Drake8c081a12001-10-12 20:57:55 +0000753}
754
755static inline int
756get_fileno(ProfilerObject *self, PyCodeObject *fcode)
757{
Fred Drake30d1c752001-10-15 22:11:02 +0000758 /* This is only used for ENTER events. */
759
760 PyObject *obj;
761 PyObject *dict;
Fred Drake8c081a12001-10-12 20:57:55 +0000762 int fileno;
763
Fred Drake30d1c752001-10-15 22:11:02 +0000764 obj = PyDict_GetItem(self->filemap, fcode->co_filename);
765 if (obj == NULL) {
Fred Drake8c081a12001-10-12 20:57:55 +0000766 /* first sighting of this file */
Fred Drake30d1c752001-10-15 22:11:02 +0000767 dict = PyDict_New();
768 if (dict == NULL) {
Fred Drake8c081a12001-10-12 20:57:55 +0000769 return -1;
770 }
Fred Drake30d1c752001-10-15 22:11:02 +0000771 fileno = self->next_fileno;
772 obj = Py_BuildValue("iN", fileno, dict);
773 if (obj == NULL) {
774 return -1;
775 }
776 if (PyDict_SetItem(self->filemap, fcode->co_filename, obj)) {
777 Py_DECREF(obj);
Fred Drake8c081a12001-10-12 20:57:55 +0000778 return -1;
779 }
780 self->next_fileno++;
Fred Drake30d1c752001-10-15 22:11:02 +0000781 Py_DECREF(obj);
Fred Drake62c1e3c2001-12-04 21:40:53 +0000782 if (pack_define_file(self, fileno,
783 PyString_AS_STRING(fcode->co_filename)) < 0)
784 return -1;
Fred Drake8c081a12001-10-12 20:57:55 +0000785 }
786 else {
787 /* already know this ID */
Fred Drake30d1c752001-10-15 22:11:02 +0000788 fileno = PyInt_AS_LONG(PyTuple_GET_ITEM(obj, 0));
789 dict = PyTuple_GET_ITEM(obj, 1);
790 }
791 /* make sure we save a function name for this (fileno, lineno) */
792 obj = PyInt_FromLong(fcode->co_firstlineno);
793 if (obj == NULL) {
794 /* We just won't have it saved; too bad. */
795 PyErr_Clear();
796 }
797 else {
798 PyObject *name = PyDict_GetItem(dict, obj);
799 if (name == NULL) {
Fred Drake62c1e3c2001-12-04 21:40:53 +0000800 if (pack_define_func(self, fileno, fcode->co_firstlineno,
Armin Rigoa4127692004-08-03 08:33:55 +0000801 PyString_AS_STRING(fcode->co_name)) < 0) {
802 Py_DECREF(obj);
Fred Drake62c1e3c2001-12-04 21:40:53 +0000803 return -1;
Armin Rigoa4127692004-08-03 08:33:55 +0000804 }
805 if (PyDict_SetItem(dict, obj, fcode->co_name)) {
806 Py_DECREF(obj);
Fred Drake30d1c752001-10-15 22:11:02 +0000807 return -1;
Armin Rigoa4127692004-08-03 08:33:55 +0000808 }
Fred Drake30d1c752001-10-15 22:11:02 +0000809 }
Armin Rigoa4127692004-08-03 08:33:55 +0000810 Py_DECREF(obj);
Fred Drake8c081a12001-10-12 20:57:55 +0000811 }
812 return fileno;
813}
814
815static inline int
816get_tdelta(ProfilerObject *self)
817{
818 int tdelta;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000819#ifdef MS_WINDOWS
Fred Drake8c081a12001-10-12 20:57:55 +0000820 hs_time tv;
Tim Peters7d99ff22001-10-13 07:37:52 +0000821 hs_time diff;
Fred Drake8c081a12001-10-12 20:57:55 +0000822
Tim Peters7d99ff22001-10-13 07:37:52 +0000823 GETTIMEOFDAY(&tv);
824 diff = tv - self->prev_timeofday;
825 tdelta = (int)diff;
Fred Drake8c081a12001-10-12 20:57:55 +0000826#else
827 struct timeval tv;
828
829 GETTIMEOFDAY(&tv);
830
Neal Norwitz7a135162004-06-13 20:45:11 +0000831 tdelta = tv.tv_usec - self->prev_timeofday.tv_usec;
832 if (tv.tv_sec != self->prev_timeofday.tv_sec)
833 tdelta += (tv.tv_sec - self->prev_timeofday.tv_sec) * 1000000;
Fred Drake8c081a12001-10-12 20:57:55 +0000834#endif
Neal Norwitz7a135162004-06-13 20:45:11 +0000835 /* time can go backwards on some multiprocessor systems or by NTP */
836 if (tdelta < 0)
837 return 0;
838
Fred Drake8c081a12001-10-12 20:57:55 +0000839 self->prev_timeofday = tv;
840 return tdelta;
841}
842
843
844/* The workhorse: the profiler callback function. */
845
846static int
Fred Drake8c081a12001-10-12 20:57:55 +0000847tracer_callback(ProfilerObject *self, PyFrameObject *frame, int what,
848 PyObject *arg)
849{
850 int fileno;
851
Fred Drake8c081a12001-10-12 20:57:55 +0000852 switch (what) {
853 case PyTrace_CALL:
854 fileno = get_fileno(self, frame->f_code);
855 if (fileno < 0)
856 return -1;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000857 return pack_enter(self, fileno,
858 self->frametimings ? get_tdelta(self) : -1,
859 frame->f_code->co_firstlineno);
860
Fred Drake8c081a12001-10-12 20:57:55 +0000861 case PyTrace_RETURN:
Fred Drake62c1e3c2001-12-04 21:40:53 +0000862 return pack_exit(self, get_tdelta(self));
863
Armin Rigode5f05f2005-12-06 14:07:39 +0000864 case PyTrace_LINE: /* we only get these events if we asked for them */
Fred Drake8c081a12001-10-12 20:57:55 +0000865 if (self->linetimings)
Michael W. Hudson806d1c82002-09-11 17:09:45 +0000866 return pack_lineno_tdelta(self, frame->f_lineno,
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000867 get_tdelta(self));
Fred Drake8c081a12001-10-12 20:57:55 +0000868 else
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000869 return pack_lineno(self, frame->f_lineno);
Fred Drake62c1e3c2001-12-04 21:40:53 +0000870
Fred Drake8c081a12001-10-12 20:57:55 +0000871 default:
872 /* ignore PyTrace_EXCEPTION */
873 break;
874 }
875 return 0;
876}
877
878
879/* A couple of useful helper functions. */
880
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000881#ifdef MS_WINDOWS
Tim Petersfeab23f2001-10-13 00:11:10 +0000882static LARGE_INTEGER frequency = {0, 0};
Fred Drake8c081a12001-10-12 20:57:55 +0000883#endif
884
885static unsigned long timeofday_diff = 0;
886static unsigned long rusage_diff = 0;
887
888static void
889calibrate(void)
890{
891 hs_time tv1, tv2;
892
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000893#ifdef MS_WINDOWS
Tim Peters7d99ff22001-10-13 07:37:52 +0000894 hs_time diff;
Fred Drake8c081a12001-10-12 20:57:55 +0000895 QueryPerformanceFrequency(&frequency);
896#endif
897
898 GETTIMEOFDAY(&tv1);
899 while (1) {
900 GETTIMEOFDAY(&tv2);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000901#ifdef MS_WINDOWS
Tim Peters7d99ff22001-10-13 07:37:52 +0000902 diff = tv2 - tv1;
903 if (diff != 0) {
904 timeofday_diff = (unsigned long)diff;
Fred Drake8c081a12001-10-12 20:57:55 +0000905 break;
906 }
907#else
908 if (tv1.tv_sec != tv2.tv_sec || tv1.tv_usec != tv2.tv_usec) {
909 if (tv1.tv_sec == tv2.tv_sec)
910 timeofday_diff = tv2.tv_usec - tv1.tv_usec;
911 else
912 timeofday_diff = (1000000 - tv1.tv_usec) + tv2.tv_usec;
913 break;
914 }
915#endif
916 }
Jack Janseneddc1442003-11-20 01:44:59 +0000917#if defined(MS_WINDOWS) || defined(PYOS_OS2) || \
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000918 defined(__VMS)
Fred Drake8c081a12001-10-12 20:57:55 +0000919 rusage_diff = -1;
920#else
921 {
922 struct rusage ru1, ru2;
923
924 getrusage(RUSAGE_SELF, &ru1);
925 while (1) {
926 getrusage(RUSAGE_SELF, &ru2);
927 if (ru1.ru_utime.tv_sec != ru2.ru_utime.tv_sec) {
928 rusage_diff = ((1000000 - ru1.ru_utime.tv_usec)
929 + ru2.ru_utime.tv_usec);
930 break;
931 }
932 else if (ru1.ru_utime.tv_usec != ru2.ru_utime.tv_usec) {
933 rusage_diff = ru2.ru_utime.tv_usec - ru1.ru_utime.tv_usec;
934 break;
935 }
936 else if (ru1.ru_stime.tv_sec != ru2.ru_stime.tv_sec) {
937 rusage_diff = ((1000000 - ru1.ru_stime.tv_usec)
938 + ru2.ru_stime.tv_usec);
939 break;
940 }
941 else if (ru1.ru_stime.tv_usec != ru2.ru_stime.tv_usec) {
942 rusage_diff = ru2.ru_stime.tv_usec - ru1.ru_stime.tv_usec;
943 break;
944 }
945 }
946 }
947#endif
948}
949
950static void
951do_start(ProfilerObject *self)
952{
953 self->active = 1;
954 GETTIMEOFDAY(&self->prev_timeofday);
955 if (self->lineevents)
956 PyEval_SetTrace((Py_tracefunc) tracer_callback, (PyObject *)self);
957 else
Armin Rigode5f05f2005-12-06 14:07:39 +0000958 PyEval_SetProfile((Py_tracefunc) tracer_callback, (PyObject *)self);
Fred Drake8c081a12001-10-12 20:57:55 +0000959}
960
961static void
962do_stop(ProfilerObject *self)
963{
964 if (self->active) {
965 self->active = 0;
966 if (self->lineevents)
967 PyEval_SetTrace(NULL, NULL);
968 else
969 PyEval_SetProfile(NULL, NULL);
970 }
971 if (self->index > 0) {
972 /* Best effort to dump out any remaining data. */
973 flush_data(self);
974 }
975}
976
977static int
978is_available(ProfilerObject *self)
979{
980 if (self->active) {
981 PyErr_SetString(ProfilerError, "profiler already active");
982 return 0;
983 }
984 if (self->logfp == NULL) {
985 PyErr_SetString(ProfilerError, "profiler already closed");
986 return 0;
987 }
988 return 1;
989}
990
991
992/* Profiler object interface methods. */
993
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000994PyDoc_STRVAR(addinfo__doc__,
Fred Drake4c2e1af2001-10-29 20:45:57 +0000995"addinfo(key, value)\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000996"Insert an ADD_INFO record into the log.");
Fred Drake4c2e1af2001-10-29 20:45:57 +0000997
998static PyObject *
999profiler_addinfo(ProfilerObject *self, PyObject *args)
1000{
1001 PyObject *result = NULL;
1002 char *key, *value;
1003
1004 if (PyArg_ParseTuple(args, "ss:addinfo", &key, &value)) {
1005 if (self->logfp == NULL)
1006 PyErr_SetString(ProfilerError, "profiler already closed");
1007 else {
Fred Drake62c1e3c2001-12-04 21:40:53 +00001008 if (pack_add_info(self, key, value) == 0) {
1009 result = Py_None;
1010 Py_INCREF(result);
1011 }
Fred Drake4c2e1af2001-10-29 20:45:57 +00001012 }
1013 }
1014 return result;
1015}
1016
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001017PyDoc_STRVAR(close__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +00001018"close()\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001019"Shut down this profiler and close the log files, even if its active.");
Fred Drake8c081a12001-10-12 20:57:55 +00001020
1021static PyObject *
Fred Drake666bf522002-07-18 19:11:44 +00001022profiler_close(ProfilerObject *self)
Fred Drake8c081a12001-10-12 20:57:55 +00001023{
Fred Drake666bf522002-07-18 19:11:44 +00001024 do_stop(self);
1025 if (self->logfp != NULL) {
1026 fclose(self->logfp);
1027 self->logfp = NULL;
Fred Drake8c081a12001-10-12 20:57:55 +00001028 }
Fred Drake666bf522002-07-18 19:11:44 +00001029 Py_INCREF(Py_None);
1030 return Py_None;
1031}
1032
1033#define fileno__doc__ logreader_fileno__doc__
1034
1035static PyObject *
1036profiler_fileno(ProfilerObject *self)
1037{
1038 if (self->logfp == NULL) {
1039 PyErr_SetString(PyExc_ValueError,
1040 "profiler's file object already closed");
1041 return NULL;
1042 }
1043 return PyInt_FromLong(fileno(self->logfp));
Fred Drake8c081a12001-10-12 20:57:55 +00001044}
1045
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001046PyDoc_STRVAR(runcall__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +00001047"runcall(callable[, args[, kw]]) -> callable()\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001048"Profile a specific function call, returning the result of that call.");
Fred Drake8c081a12001-10-12 20:57:55 +00001049
1050static PyObject *
1051profiler_runcall(ProfilerObject *self, PyObject *args)
1052{
1053 PyObject *result = NULL;
1054 PyObject *callargs = NULL;
1055 PyObject *callkw = NULL;
1056 PyObject *callable;
1057
1058 if (PyArg_ParseTuple(args, "O|OO:runcall",
1059 &callable, &callargs, &callkw)) {
1060 if (is_available(self)) {
1061 do_start(self);
1062 result = PyEval_CallObjectWithKeywords(callable, callargs, callkw);
1063 do_stop(self);
1064 }
1065 }
1066 return result;
1067}
1068
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001069PyDoc_STRVAR(runcode__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +00001070"runcode(code, globals[, locals])\n"
1071"Execute a code object while collecting profile data. If locals is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001072"omitted, globals is used for the locals as well.");
Fred Drake8c081a12001-10-12 20:57:55 +00001073
1074static PyObject *
1075profiler_runcode(ProfilerObject *self, PyObject *args)
1076{
1077 PyObject *result = NULL;
1078 PyCodeObject *code;
1079 PyObject *globals;
1080 PyObject *locals = NULL;
1081
1082 if (PyArg_ParseTuple(args, "O!O!|O:runcode",
1083 &PyCode_Type, &code,
1084 &PyDict_Type, &globals,
1085 &locals)) {
1086 if (is_available(self)) {
1087 if (locals == NULL || locals == Py_None)
1088 locals = globals;
1089 else if (!PyDict_Check(locals)) {
1090 PyErr_SetString(PyExc_TypeError,
1091 "locals must be a dictionary or None");
1092 return NULL;
1093 }
1094 do_start(self);
1095 result = PyEval_EvalCode(code, globals, locals);
1096 do_stop(self);
1097#if 0
1098 if (!PyErr_Occurred()) {
1099 result = Py_None;
1100 Py_INCREF(result);
1101 }
1102#endif
1103 }
1104 }
1105 return result;
1106}
1107
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001108PyDoc_STRVAR(start__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +00001109"start()\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001110"Install this profiler for the current thread.");
Fred Drake8c081a12001-10-12 20:57:55 +00001111
1112static PyObject *
1113profiler_start(ProfilerObject *self, PyObject *args)
1114{
1115 PyObject *result = NULL;
1116
Fred Drake666bf522002-07-18 19:11:44 +00001117 if (is_available(self)) {
1118 do_start(self);
1119 result = Py_None;
1120 Py_INCREF(result);
Fred Drake8c081a12001-10-12 20:57:55 +00001121 }
1122 return result;
1123}
1124
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001125PyDoc_STRVAR(stop__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +00001126"stop()\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001127"Remove this profiler from the current thread.");
Fred Drake8c081a12001-10-12 20:57:55 +00001128
1129static PyObject *
1130profiler_stop(ProfilerObject *self, PyObject *args)
1131{
1132 PyObject *result = NULL;
1133
Fred Drake666bf522002-07-18 19:11:44 +00001134 if (!self->active)
1135 PyErr_SetString(ProfilerError, "profiler not active");
1136 else {
1137 do_stop(self);
1138 result = Py_None;
1139 Py_INCREF(result);
Fred Drake8c081a12001-10-12 20:57:55 +00001140 }
1141 return result;
1142}
1143
1144
1145/* Python API support. */
1146
1147static void
1148profiler_dealloc(ProfilerObject *self)
1149{
1150 do_stop(self);
1151 if (self->logfp != NULL)
1152 fclose(self->logfp);
1153 Py_XDECREF(self->filemap);
1154 Py_XDECREF(self->logfilename);
1155 PyObject_Del((PyObject *)self);
1156}
1157
Fred Drake8c081a12001-10-12 20:57:55 +00001158static PyMethodDef profiler_methods[] = {
Fred Drake4c2e1af2001-10-29 20:45:57 +00001159 {"addinfo", (PyCFunction)profiler_addinfo, METH_VARARGS, addinfo__doc__},
Fred Drake666bf522002-07-18 19:11:44 +00001160 {"close", (PyCFunction)profiler_close, METH_NOARGS, close__doc__},
1161 {"fileno", (PyCFunction)profiler_fileno, METH_NOARGS, fileno__doc__},
Fred Drake8c081a12001-10-12 20:57:55 +00001162 {"runcall", (PyCFunction)profiler_runcall, METH_VARARGS, runcall__doc__},
1163 {"runcode", (PyCFunction)profiler_runcode, METH_VARARGS, runcode__doc__},
Fred Drake666bf522002-07-18 19:11:44 +00001164 {"start", (PyCFunction)profiler_start, METH_NOARGS, start__doc__},
1165 {"stop", (PyCFunction)profiler_stop, METH_NOARGS, stop__doc__},
Fred Drake8c081a12001-10-12 20:57:55 +00001166 {NULL, NULL}
1167};
1168
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001169static PyMemberDef profiler_members[] = {
Fred Drake30d1c752001-10-15 22:11:02 +00001170 {"frametimings", T_LONG, offsetof(ProfilerObject, linetimings), READONLY},
1171 {"lineevents", T_LONG, offsetof(ProfilerObject, lineevents), READONLY},
1172 {"linetimings", T_LONG, offsetof(ProfilerObject, linetimings), READONLY},
Fred Drake8c081a12001-10-12 20:57:55 +00001173 {NULL}
1174};
1175
1176static PyObject *
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001177profiler_get_closed(ProfilerObject *self, void *closure)
Fred Drake8c081a12001-10-12 20:57:55 +00001178{
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001179 PyObject *result = (self->logfp == NULL) ? Py_True : Py_False;
1180 Py_INCREF(result);
Fred Drake8c081a12001-10-12 20:57:55 +00001181 return result;
1182}
1183
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001184static PyGetSetDef profiler_getsets[] = {
Fred Draked1eb8b62002-07-17 18:54:20 +00001185 {"closed", (getter)profiler_get_closed, NULL,
Fred Drake5c3ed3d2002-07-17 19:38:05 +00001186 PyDoc_STR("True if the profiler's output file has already been closed.")},
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001187 {NULL}
1188};
1189
Fred Drake8c081a12001-10-12 20:57:55 +00001190
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001191PyDoc_STRVAR(profiler_object__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +00001192"High-performance profiler object.\n"
1193"\n"
1194"Methods:\n"
1195"\n"
Fred Drake30d1c752001-10-15 22:11:02 +00001196"close(): Stop the profiler and close the log files.\n"
Fred Drake666bf522002-07-18 19:11:44 +00001197"fileno(): Returns the file descriptor of the log file.\n"
Fred Drake30d1c752001-10-15 22:11:02 +00001198"runcall(): Run a single function call with profiling enabled.\n"
1199"runcode(): Execute a code object with profiling enabled.\n"
1200"start(): Install the profiler and return.\n"
1201"stop(): Remove the profiler.\n"
Tim Petersfeab23f2001-10-13 00:11:10 +00001202"\n"
1203"Attributes (read-only):\n"
1204"\n"
Fred Drake30d1c752001-10-15 22:11:02 +00001205"closed: True if the profiler has already been closed.\n"
1206"frametimings: True if ENTER/EXIT events collect timing information.\n"
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001207"lineevents: True if line events are reported to the profiler.\n"
1208"linetimings: True if line events collect timing information.");
Fred Drake8c081a12001-10-12 20:57:55 +00001209
1210static PyTypeObject ProfilerType = {
1211 PyObject_HEAD_INIT(NULL)
1212 0, /* ob_size */
Fred Drake4c2e1af2001-10-29 20:45:57 +00001213 "_hotshot.ProfilerType", /* tp_name */
Fred Drake8c081a12001-10-12 20:57:55 +00001214 (int) sizeof(ProfilerObject), /* tp_basicsize */
1215 0, /* tp_itemsize */
1216 (destructor)profiler_dealloc, /* tp_dealloc */
1217 0, /* tp_print */
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001218 0, /* tp_getattr */
Fred Drake8c081a12001-10-12 20:57:55 +00001219 0, /* tp_setattr */
1220 0, /* tp_compare */
1221 0, /* tp_repr */
1222 0, /* tp_as_number */
1223 0, /* tp_as_sequence */
1224 0, /* tp_as_mapping */
1225 0, /* tp_hash */
1226 0, /* tp_call */
1227 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00001228 PyObject_GenericGetAttr, /* tp_getattro */
Fred Drake8c081a12001-10-12 20:57:55 +00001229 0, /* tp_setattro */
1230 0, /* tp_as_buffer */
Fred Drake4c2e1af2001-10-29 20:45:57 +00001231 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake8c081a12001-10-12 20:57:55 +00001232 profiler_object__doc__, /* tp_doc */
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001233 0, /* tp_traverse */
1234 0, /* tp_clear */
1235 0, /* tp_richcompare */
1236 0, /* tp_weaklistoffset */
1237 0, /* tp_iter */
1238 0, /* tp_iternext */
1239 profiler_methods, /* tp_methods */
1240 profiler_members, /* tp_members */
1241 profiler_getsets, /* tp_getset */
1242 0, /* tp_base */
1243 0, /* tp_dict */
1244 0, /* tp_descr_get */
1245 0, /* tp_descr_set */
Fred Drake8c081a12001-10-12 20:57:55 +00001246};
1247
1248
1249static PyMethodDef logreader_methods[] = {
Fred Drake666bf522002-07-18 19:11:44 +00001250 {"close", (PyCFunction)logreader_close, METH_NOARGS,
Fred Drake8c081a12001-10-12 20:57:55 +00001251 logreader_close__doc__},
Fred Drake666bf522002-07-18 19:11:44 +00001252 {"fileno", (PyCFunction)logreader_fileno, METH_NOARGS,
1253 logreader_fileno__doc__},
Fred Drake8c081a12001-10-12 20:57:55 +00001254 {NULL, NULL}
1255};
1256
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001257static PyMemberDef logreader_members[] = {
Fred Drake5c3ed3d2002-07-17 19:38:05 +00001258 {"info", T_OBJECT, offsetof(LogReaderObject, info), RO,
1259 PyDoc_STR("Dictionary mapping informational keys to lists of values.")},
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001260 {NULL}
1261};
Fred Drake8c081a12001-10-12 20:57:55 +00001262
1263
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001264PyDoc_STRVAR(logreader__doc__,
1265"logreader(filename) --> log-iterator\n\
1266Create a log-reader for the timing information file.");
Fred Drake8c081a12001-10-12 20:57:55 +00001267
1268static PySequenceMethods logreader_as_sequence = {
1269 0, /* sq_length */
1270 0, /* sq_concat */
1271 0, /* sq_repeat */
1272 (intargfunc)logreader_sq_item, /* sq_item */
1273 0, /* sq_slice */
1274 0, /* sq_ass_item */
1275 0, /* sq_ass_slice */
1276 0, /* sq_contains */
1277 0, /* sq_inplace_concat */
1278 0, /* sq_inplace_repeat */
1279};
1280
Fred Drake666bf522002-07-18 19:11:44 +00001281static PyObject *
1282logreader_get_closed(LogReaderObject *self, void *closure)
1283{
1284 PyObject *result = (self->logfp == NULL) ? Py_True : Py_False;
1285 Py_INCREF(result);
1286 return result;
1287}
1288
1289static PyGetSetDef logreader_getsets[] = {
1290 {"closed", (getter)logreader_get_closed, NULL,
1291 PyDoc_STR("True if the logreader's input file has already been closed.")},
1292 {NULL}
1293};
1294
Fred Drake8c081a12001-10-12 20:57:55 +00001295static PyTypeObject LogReaderType = {
1296 PyObject_HEAD_INIT(NULL)
1297 0, /* ob_size */
Fred Drake4c2e1af2001-10-29 20:45:57 +00001298 "_hotshot.LogReaderType", /* tp_name */
Fred Drake8c081a12001-10-12 20:57:55 +00001299 (int) sizeof(LogReaderObject), /* tp_basicsize */
1300 0, /* tp_itemsize */
1301 (destructor)logreader_dealloc, /* tp_dealloc */
1302 0, /* tp_print */
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001303 0, /* tp_getattr */
Fred Drake8c081a12001-10-12 20:57:55 +00001304 0, /* tp_setattr */
1305 0, /* tp_compare */
1306 0, /* tp_repr */
1307 0, /* tp_as_number */
1308 &logreader_as_sequence, /* tp_as_sequence */
1309 0, /* tp_as_mapping */
1310 0, /* tp_hash */
1311 0, /* tp_call */
1312 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00001313 PyObject_GenericGetAttr, /* tp_getattro */
Fred Drake8c081a12001-10-12 20:57:55 +00001314 0, /* tp_setattro */
1315 0, /* tp_as_buffer */
Fred Drake4c2e1af2001-10-29 20:45:57 +00001316 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake8c081a12001-10-12 20:57:55 +00001317 logreader__doc__, /* tp_doc */
Fred Drake8c081a12001-10-12 20:57:55 +00001318 0, /* tp_traverse */
1319 0, /* tp_clear */
1320 0, /* tp_richcompare */
1321 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00001322 PyObject_SelfIter, /* tp_iter */
Fred Drake8c081a12001-10-12 20:57:55 +00001323 (iternextfunc)logreader_tp_iternext,/* tp_iternext */
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001324 logreader_methods, /* tp_methods */
1325 logreader_members, /* tp_members */
Fred Drake666bf522002-07-18 19:11:44 +00001326 logreader_getsets, /* tp_getset */
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001327 0, /* tp_base */
1328 0, /* tp_dict */
1329 0, /* tp_descr_get */
1330 0, /* tp_descr_set */
Fred Drake8c081a12001-10-12 20:57:55 +00001331};
1332
1333static PyObject *
1334hotshot_logreader(PyObject *unused, PyObject *args)
1335{
1336 LogReaderObject *self = NULL;
1337 char *filename;
Neil Schemenauer8b6b4912002-05-29 18:19:14 +00001338 int c;
1339 int err = 0;
Fred Drake8c081a12001-10-12 20:57:55 +00001340
1341 if (PyArg_ParseTuple(args, "s:logreader", &filename)) {
1342 self = PyObject_New(LogReaderObject, &LogReaderType);
1343 if (self != NULL) {
Fred Drake30d1c752001-10-15 22:11:02 +00001344 self->frametimings = 1;
Fred Drake8c081a12001-10-12 20:57:55 +00001345 self->linetimings = 0;
Fred Drake4c2e1af2001-10-29 20:45:57 +00001346 self->info = NULL;
Fred Drake8c081a12001-10-12 20:57:55 +00001347 self->logfp = fopen(filename, "rb");
1348 if (self->logfp == NULL) {
1349 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
1350 Py_DECREF(self);
1351 self = NULL;
Fred Drake4c2e1af2001-10-29 20:45:57 +00001352 goto finally;
1353 }
1354 self->info = PyDict_New();
1355 if (self->info == NULL) {
1356 Py_DECREF(self);
1357 goto finally;
1358 }
Neil Schemenauer8b6b4912002-05-29 18:19:14 +00001359 /* read initial info */
1360 for (;;) {
1361 if ((c = fgetc(self->logfp)) == EOF) {
Fred Drake666bf522002-07-18 19:11:44 +00001362 eof_error(self);
Neil Schemenauer8b6b4912002-05-29 18:19:14 +00001363 break;
1364 }
1365 if (c != WHAT_ADD_INFO) {
1366 ungetc(c, self->logfp);
1367 break;
1368 }
1369 err = unpack_add_info(self);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001370 if (err) {
1371 if (err == ERR_EOF)
Fred Drake666bf522002-07-18 19:11:44 +00001372 eof_error(self);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001373 else
1374 PyErr_SetString(PyExc_RuntimeError,
1375 "unexpected error");
1376 break;
1377 }
Fred Drake8c081a12001-10-12 20:57:55 +00001378 }
1379 }
1380 }
Fred Drake4c2e1af2001-10-29 20:45:57 +00001381 finally:
Fred Drake8c081a12001-10-12 20:57:55 +00001382 return (PyObject *) self;
1383}
1384
1385
1386/* Return a Python string that represents the version number without the
1387 * extra cruft added by revision control, even if the right options were
1388 * given to the "cvs export" command to make it not include the extra
1389 * cruft.
1390 */
1391static char *
1392get_version_string(void)
1393{
1394 static char *rcsid = "$Revision$";
1395 char *rev = rcsid;
1396 char *buffer;
1397 int i = 0;
1398
Neal Norwitz3afb2d22002-03-20 21:32:07 +00001399 while (*rev && !isdigit((int)*rev))
Fred Drake8c081a12001-10-12 20:57:55 +00001400 ++rev;
1401 while (rev[i] != ' ' && rev[i] != '\0')
1402 ++i;
1403 buffer = malloc(i + 1);
1404 if (buffer != NULL) {
1405 memmove(buffer, rev, i);
1406 buffer[i] = '\0';
1407 }
1408 return buffer;
1409}
1410
1411/* Write out a RFC 822-style header with various useful bits of
1412 * information to make the output easier to manage.
1413 */
1414static int
1415write_header(ProfilerObject *self)
1416{
1417 char *buffer;
1418 char cwdbuffer[PATH_MAX];
1419 PyObject *temp;
1420 int i, len;
1421
1422 buffer = get_version_string();
1423 if (buffer == NULL) {
1424 PyErr_NoMemory();
1425 return -1;
1426 }
Fred Drake4c2e1af2001-10-29 20:45:57 +00001427 pack_add_info(self, "hotshot-version", buffer);
1428 pack_add_info(self, "requested-frame-timings",
1429 (self->frametimings ? "yes" : "no"));
1430 pack_add_info(self, "requested-line-events",
Fred Drake8c081a12001-10-12 20:57:55 +00001431 (self->lineevents ? "yes" : "no"));
Fred Drake4c2e1af2001-10-29 20:45:57 +00001432 pack_add_info(self, "requested-line-timings",
1433 (self->linetimings ? "yes" : "no"));
1434 pack_add_info(self, "platform", Py_GetPlatform());
1435 pack_add_info(self, "executable", Py_GetProgramFullPath());
Fred Drakef12a68c2001-11-09 15:59:36 +00001436 free(buffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001437 buffer = (char *) Py_GetVersion();
1438 if (buffer == NULL)
1439 PyErr_Clear();
1440 else
Fred Drake4c2e1af2001-10-29 20:45:57 +00001441 pack_add_info(self, "executable-version", buffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001442
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001443#ifdef MS_WINDOWS
Tim Peters885d4572001-11-28 20:27:42 +00001444 PyOS_snprintf(cwdbuffer, sizeof(cwdbuffer), "%I64d", frequency.QuadPart);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001445 pack_add_info(self, "reported-performance-frequency", cwdbuffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001446#else
Tim Peters885d4572001-11-28 20:27:42 +00001447 PyOS_snprintf(cwdbuffer, sizeof(cwdbuffer), "%lu", rusage_diff);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001448 pack_add_info(self, "observed-interval-getrusage", cwdbuffer);
Tim Peters885d4572001-11-28 20:27:42 +00001449 PyOS_snprintf(cwdbuffer, sizeof(cwdbuffer), "%lu", timeofday_diff);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001450 pack_add_info(self, "observed-interval-gettimeofday", cwdbuffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001451#endif
Fred Drake8c081a12001-10-12 20:57:55 +00001452
Fred Drake4c2e1af2001-10-29 20:45:57 +00001453 pack_add_info(self, "current-directory",
Fred Drake8c081a12001-10-12 20:57:55 +00001454 getcwd(cwdbuffer, sizeof cwdbuffer));
1455
1456 temp = PySys_GetObject("path");
1457 len = PyList_GET_SIZE(temp);
1458 for (i = 0; i < len; ++i) {
1459 PyObject *item = PyList_GET_ITEM(temp, i);
1460 buffer = PyString_AsString(item);
Fred Draked1eb8b62002-07-17 18:54:20 +00001461 if (buffer == NULL) {
1462 pack_add_info(self, "sys-path-entry", "<non-string-path-entry>");
1463 PyErr_Clear();
1464 }
1465 else {
1466 pack_add_info(self, "sys-path-entry", buffer);
1467 }
Fred Drake8c081a12001-10-12 20:57:55 +00001468 }
Fred Drake4c2e1af2001-10-29 20:45:57 +00001469 pack_frame_times(self);
1470 pack_line_times(self);
1471
Fred Drake8c081a12001-10-12 20:57:55 +00001472 return 0;
1473}
1474
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001475PyDoc_STRVAR(profiler__doc__,
1476"profiler(logfilename[, lineevents[, linetimes]]) -> profiler\n\
1477Create a new profiler object.");
Fred Drake8c081a12001-10-12 20:57:55 +00001478
1479static PyObject *
1480hotshot_profiler(PyObject *unused, PyObject *args)
1481{
1482 char *logfilename;
1483 ProfilerObject *self = NULL;
1484 int lineevents = 0;
1485 int linetimings = 1;
1486
1487 if (PyArg_ParseTuple(args, "s|ii:profiler", &logfilename,
1488 &lineevents, &linetimings)) {
1489 self = PyObject_New(ProfilerObject, &ProfilerType);
1490 if (self == NULL)
1491 return NULL;
Fred Drake30d1c752001-10-15 22:11:02 +00001492 self->frametimings = 1;
Fred Drake8c081a12001-10-12 20:57:55 +00001493 self->lineevents = lineevents ? 1 : 0;
1494 self->linetimings = (lineevents && linetimings) ? 1 : 0;
1495 self->index = 0;
1496 self->active = 0;
1497 self->next_fileno = 0;
Tim Peters1566a172001-10-12 22:08:39 +00001498 self->logfp = NULL;
Fred Drake8c081a12001-10-12 20:57:55 +00001499 self->logfilename = PyTuple_GET_ITEM(args, 0);
1500 Py_INCREF(self->logfilename);
1501 self->filemap = PyDict_New();
1502 if (self->filemap == NULL) {
1503 Py_DECREF(self);
1504 return NULL;
1505 }
1506 self->logfp = fopen(logfilename, "wb");
1507 if (self->logfp == NULL) {
1508 Py_DECREF(self);
1509 PyErr_SetFromErrnoWithFilename(PyExc_IOError, logfilename);
1510 return NULL;
1511 }
1512 if (timeofday_diff == 0) {
1513 /* Run this several times since sometimes the first
1514 * doesn't give the lowest values, and we're really trying
1515 * to determine the lowest.
1516 */
1517 calibrate();
1518 calibrate();
1519 calibrate();
1520 }
1521 if (write_header(self))
1522 /* some error occurred, exception has been set */
1523 self = NULL;
1524 }
1525 return (PyObject *) self;
1526}
1527
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001528PyDoc_STRVAR(coverage__doc__,
1529"coverage(logfilename) -> profiler\n\
Fred Drake30d1c752001-10-15 22:11:02 +00001530Returns a profiler that doesn't collect any timing information, which is\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001531useful in building a coverage analysis tool.");
Fred Drake30d1c752001-10-15 22:11:02 +00001532
1533static PyObject *
1534hotshot_coverage(PyObject *unused, PyObject *args)
1535{
1536 char *logfilename;
1537 PyObject *result = NULL;
1538
1539 if (PyArg_ParseTuple(args, "s:coverage", &logfilename)) {
1540 result = hotshot_profiler(unused, args);
1541 if (result != NULL) {
1542 ProfilerObject *self = (ProfilerObject *) result;
1543 self->frametimings = 0;
1544 self->linetimings = 0;
1545 self->lineevents = 1;
1546 }
1547 }
1548 return result;
1549}
1550
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001551PyDoc_VAR(resolution__doc__) =
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001552#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001553PyDoc_STR(
Tim Petersfeab23f2001-10-13 00:11:10 +00001554"resolution() -> (performance-counter-ticks, update-frequency)\n"
1555"Return the resolution of the timer provided by the QueryPerformanceCounter()\n"
1556"function. The first value is the smallest observed change, and the second\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001557"is the result of QueryPerformanceFrequency()."
1558)
Fred Drake8c081a12001-10-12 20:57:55 +00001559#else
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001560PyDoc_STR(
Tim Petersfeab23f2001-10-13 00:11:10 +00001561"resolution() -> (gettimeofday-usecs, getrusage-usecs)\n"
1562"Return the resolution of the timers provided by the gettimeofday() and\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001563"getrusage() system calls, or -1 if the call is not supported."
1564)
Fred Drake8c081a12001-10-12 20:57:55 +00001565#endif
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001566;
Fred Drake8c081a12001-10-12 20:57:55 +00001567
1568static PyObject *
1569hotshot_resolution(PyObject *unused, PyObject *args)
1570{
1571 PyObject *result = NULL;
1572
1573 if (PyArg_ParseTuple(args, ":resolution")) {
1574 if (timeofday_diff == 0) {
1575 calibrate();
1576 calibrate();
1577 calibrate();
1578 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001579#ifdef MS_WINDOWS
Fred Drake8c081a12001-10-12 20:57:55 +00001580 result = Py_BuildValue("ii", timeofday_diff, frequency.LowPart);
1581#else
1582 result = Py_BuildValue("ii", timeofday_diff, rusage_diff);
1583#endif
1584 }
1585 return result;
1586}
1587
1588
1589static PyMethodDef functions[] = {
Fred Drake30d1c752001-10-15 22:11:02 +00001590 {"coverage", hotshot_coverage, METH_VARARGS, coverage__doc__},
Fred Drake8c081a12001-10-12 20:57:55 +00001591 {"profiler", hotshot_profiler, METH_VARARGS, profiler__doc__},
1592 {"logreader", hotshot_logreader, METH_VARARGS, logreader__doc__},
1593 {"resolution", hotshot_resolution, METH_VARARGS, resolution__doc__},
1594 {NULL, NULL}
1595};
1596
1597
1598void
1599init_hotshot(void)
1600{
1601 PyObject *module;
1602
1603 LogReaderType.ob_type = &PyType_Type;
1604 ProfilerType.ob_type = &PyType_Type;
1605 module = Py_InitModule("_hotshot", functions);
1606 if (module != NULL) {
1607 char *s = get_version_string();
1608
1609 PyModule_AddStringConstant(module, "__version__", s);
1610 free(s);
1611 Py_INCREF(&LogReaderType);
1612 PyModule_AddObject(module, "LogReaderType",
1613 (PyObject *)&LogReaderType);
1614 Py_INCREF(&ProfilerType);
1615 PyModule_AddObject(module, "ProfilerType",
1616 (PyObject *)&ProfilerType);
1617
1618 if (ProfilerError == NULL)
1619 ProfilerError = PyErr_NewException("hotshot.ProfilerError",
1620 NULL, NULL);
1621 if (ProfilerError != NULL) {
1622 Py_INCREF(ProfilerError);
1623 PyModule_AddObject(module, "ProfilerError", ProfilerError);
1624 }
1625 PyModule_AddIntConstant(module, "WHAT_ENTER", WHAT_ENTER);
1626 PyModule_AddIntConstant(module, "WHAT_EXIT", WHAT_EXIT);
1627 PyModule_AddIntConstant(module, "WHAT_LINENO", WHAT_LINENO);
1628 PyModule_AddIntConstant(module, "WHAT_OTHER", WHAT_OTHER);
1629 PyModule_AddIntConstant(module, "WHAT_ADD_INFO", WHAT_ADD_INFO);
1630 PyModule_AddIntConstant(module, "WHAT_DEFINE_FILE", WHAT_DEFINE_FILE);
Fred Drake30d1c752001-10-15 22:11:02 +00001631 PyModule_AddIntConstant(module, "WHAT_DEFINE_FUNC", WHAT_DEFINE_FUNC);
Fred Drake8c081a12001-10-12 20:57:55 +00001632 PyModule_AddIntConstant(module, "WHAT_LINE_TIMES", WHAT_LINE_TIMES);
1633 }
1634}