blob: 0683be6c32dfceee826ab2aa31166992f68438b1 [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"
6#include "compile.h"
7#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)) {
350 err = ERR_EXCEPTION;
351 goto finally;
352 }
353 }
354 if (PyList_Append(list, value))
355 err = ERR_EXCEPTION;
356 }
357 }
358 finally:
359 Py_XDECREF(key);
360 Py_XDECREF(value);
361 return err;
362}
363
364
365static void
Fred Drake666bf522002-07-18 19:11:44 +0000366eof_error(LogReaderObject *self)
Fred Drake4c2e1af2001-10-29 20:45:57 +0000367{
Fred Drake666bf522002-07-18 19:11:44 +0000368 fclose(self->logfp);
369 self->logfp = NULL;
Fred Drake4c2e1af2001-10-29 20:45:57 +0000370 PyErr_SetString(PyExc_EOFError,
371 "end of file with incomplete profile record");
372}
373
Fred Drake8c081a12001-10-12 20:57:55 +0000374static PyObject *
375logreader_tp_iternext(LogReaderObject *self)
376{
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000377 int c;
378 int what;
Fred Drake8c081a12001-10-12 20:57:55 +0000379 int err = ERR_NONE;
380 int lineno = -1;
381 int fileno = -1;
382 int tdelta = -1;
383 PyObject *s1 = NULL, *s2 = NULL;
384 PyObject *result = NULL;
385#if 0
386 unsigned char b0, b1;
387#endif
388
389 if (self->logfp == NULL) {
390 PyErr_SetString(ProfilerError,
391 "cannot iterate over closed LogReader object");
392 return NULL;
393 }
Fred Drake4c2e1af2001-10-29 20:45:57 +0000394
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000395restart:
396 /* decode the record type */
Fred Drake666bf522002-07-18 19:11:44 +0000397 if ((c = fgetc(self->logfp)) == EOF) {
398 fclose(self->logfp);
399 self->logfp = NULL;
Fred Drake8c081a12001-10-12 20:57:55 +0000400 return NULL;
Fred Drake666bf522002-07-18 19:11:44 +0000401 }
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000402 what = c & WHAT_OTHER;
403 if (what == WHAT_OTHER)
404 what = c; /* need all the bits for type */
405 else
406 ungetc(c, self->logfp); /* type byte includes packed int */
Fred Drake8c081a12001-10-12 20:57:55 +0000407
Fred Drake8c081a12001-10-12 20:57:55 +0000408 switch (what) {
409 case WHAT_ENTER:
410 err = unpack_packed_int(self, &fileno, 2);
411 if (!err) {
Fred Drake30d1c752001-10-15 22:11:02 +0000412 err = unpack_packed_int(self, &lineno, 0);
413 if (self->frametimings && !err)
414 err = unpack_packed_int(self, &tdelta, 0);
Fred Drake8c081a12001-10-12 20:57:55 +0000415 }
416 break;
417 case WHAT_EXIT:
418 err = unpack_packed_int(self, &tdelta, 2);
419 break;
420 case WHAT_LINENO:
421 err = unpack_packed_int(self, &lineno, 2);
422 if (self->linetimings && !err)
423 err = unpack_packed_int(self, &tdelta, 0);
424 break;
425 case WHAT_ADD_INFO:
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000426 err = unpack_add_info(self);
Fred Drake8c081a12001-10-12 20:57:55 +0000427 break;
428 case WHAT_DEFINE_FILE:
429 err = unpack_packed_int(self, &fileno, 0);
430 if (!err) {
431 err = unpack_string(self, &s1);
432 if (!err) {
433 Py_INCREF(Py_None);
434 s2 = Py_None;
435 }
436 }
437 break;
Fred Drake30d1c752001-10-15 22:11:02 +0000438 case WHAT_DEFINE_FUNC:
439 err = unpack_packed_int(self, &fileno, 0);
440 if (!err) {
441 err = unpack_packed_int(self, &lineno, 0);
442 if (!err)
443 err = unpack_string(self, &s1);
444 }
445 break;
Fred Drake8c081a12001-10-12 20:57:55 +0000446 case WHAT_LINE_TIMES:
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000447 if ((c = fgetc(self->logfp)) == EOF)
Fred Drake8c081a12001-10-12 20:57:55 +0000448 err = ERR_EOF;
449 else {
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000450 self->linetimings = c ? 1 : 0;
451 goto restart;
452 }
Fred Drake4c2e1af2001-10-29 20:45:57 +0000453 break;
Fred Drake30d1c752001-10-15 22:11:02 +0000454 case WHAT_FRAME_TIMES:
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000455 if ((c = fgetc(self->logfp)) == EOF)
Fred Drake30d1c752001-10-15 22:11:02 +0000456 err = ERR_EOF;
457 else {
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000458 self->frametimings = c ? 1 : 0;
459 goto restart;
460 }
Fred Drake4c2e1af2001-10-29 20:45:57 +0000461 break;
Fred Drake8c081a12001-10-12 20:57:55 +0000462 default:
Fred Drake4c2e1af2001-10-29 20:45:57 +0000463 err = ERR_BAD_RECTYPE;
Fred Drake8c081a12001-10-12 20:57:55 +0000464 }
Fred Drake4c2e1af2001-10-29 20:45:57 +0000465 if (err == ERR_BAD_RECTYPE) {
466 PyErr_SetString(PyExc_ValueError,
467 "unknown record type in log file");
468 }
469 else if (err == ERR_EOF) {
Fred Drake666bf522002-07-18 19:11:44 +0000470 eof_error(self);
Fred Drake8c081a12001-10-12 20:57:55 +0000471 }
472 else if (!err) {
473 result = PyTuple_New(4);
474 PyTuple_SET_ITEM(result, 0, PyInt_FromLong(what));
475 PyTuple_SET_ITEM(result, 2, PyInt_FromLong(fileno));
Fred Drake30d1c752001-10-15 22:11:02 +0000476 if (s1 == NULL)
Fred Drake8c081a12001-10-12 20:57:55 +0000477 PyTuple_SET_ITEM(result, 1, PyInt_FromLong(tdelta));
Fred Drake30d1c752001-10-15 22:11:02 +0000478 else
Fred Drake8c081a12001-10-12 20:57:55 +0000479 PyTuple_SET_ITEM(result, 1, s1);
Fred Drake30d1c752001-10-15 22:11:02 +0000480 if (s2 == NULL)
481 PyTuple_SET_ITEM(result, 3, PyInt_FromLong(lineno));
482 else
Fred Drake8c081a12001-10-12 20:57:55 +0000483 PyTuple_SET_ITEM(result, 3, s2);
Fred Drake8c081a12001-10-12 20:57:55 +0000484 }
485 /* The only other case is err == ERR_EXCEPTION, in which case the
486 * exception is already set.
487 */
488#if 0
489 b0 = self->buffer[self->index];
490 b1 = self->buffer[self->index + 1];
491 if (b0 & 1) {
492 /* This is a line-number event. */
493 what = PyTrace_LINE;
494 lineno = ((b0 & ~1) << 7) + b1;
495 self->index += 2;
496 }
497 else {
498 what = (b0 & 0x0E) >> 1;
499 tdelta = ((b0 & 0xF0) << 4) + b1;
500 if (what == PyTrace_CALL) {
501 /* we know there's a 2-byte file ID & 2-byte line number */
502 fileno = ((self->buffer[self->index + 2] << 8)
503 + self->buffer[self->index + 3]);
504 lineno = ((self->buffer[self->index + 4] << 8)
505 + self->buffer[self->index + 5]);
506 self->index += 6;
507 }
508 else
509 self->index += 2;
510 }
511#endif
512 return result;
513}
514
515static void
516logreader_dealloc(LogReaderObject *self)
517{
518 if (self->logfp != NULL) {
519 fclose(self->logfp);
520 self->logfp = NULL;
521 }
522 PyObject_Del(self);
523}
524
525static PyObject *
526logreader_sq_item(LogReaderObject *self, int index)
527{
528 PyObject *result = logreader_tp_iternext(self);
529 if (result == NULL && !PyErr_Occurred()) {
530 PyErr_SetString(PyExc_IndexError, "no more events in log");
531 return NULL;
532 }
533 return result;
534}
535
Fred Drake62c1e3c2001-12-04 21:40:53 +0000536static void
537do_stop(ProfilerObject *self);
Fred Drake8c081a12001-10-12 20:57:55 +0000538
539static int
540flush_data(ProfilerObject *self)
541{
542 /* Need to dump data to the log file... */
543 size_t written = fwrite(self->buffer, 1, self->index, self->logfp);
Tim Peters1566a172001-10-12 22:08:39 +0000544 if (written == (size_t)self->index)
Fred Drake8c081a12001-10-12 20:57:55 +0000545 self->index = 0;
546 else {
547 memmove(self->buffer, &self->buffer[written],
548 self->index - written);
549 self->index -= written;
550 if (written == 0) {
551 char *s = PyString_AsString(self->logfilename);
552 PyErr_SetFromErrnoWithFilename(PyExc_IOError, s);
Fred Drake62c1e3c2001-12-04 21:40:53 +0000553 do_stop(self);
Fred Drake8c081a12001-10-12 20:57:55 +0000554 return -1;
555 }
556 }
557 if (written > 0) {
558 if (fflush(self->logfp)) {
559 char *s = PyString_AsString(self->logfilename);
560 PyErr_SetFromErrnoWithFilename(PyExc_IOError, s);
Fred Drake62c1e3c2001-12-04 21:40:53 +0000561 do_stop(self);
Fred Drake8c081a12001-10-12 20:57:55 +0000562 return -1;
563 }
564 }
565 return 0;
566}
567
Fred Drake62c1e3c2001-12-04 21:40:53 +0000568static inline int
Fred Drake8c081a12001-10-12 20:57:55 +0000569pack_packed_int(ProfilerObject *self, int value)
570{
571 unsigned char partial;
572
573 do {
574 partial = value & 0x7F;
575 value >>= 7;
576 if (value)
577 partial |= 0x80;
578 self->buffer[self->index] = partial;
579 self->index++;
580 } while (value);
Fred Drake62c1e3c2001-12-04 21:40:53 +0000581 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000582}
583
584/* Encode a modified packed integer, with a subfield of modsize bits
585 * containing the value "subfield". The value of subfield is not
586 * checked to ensure it actually fits in modsize bits.
587 */
Fred Drake62c1e3c2001-12-04 21:40:53 +0000588static inline int
Fred Drake8c081a12001-10-12 20:57:55 +0000589pack_modified_packed_int(ProfilerObject *self, int value,
590 int modsize, int subfield)
591{
592 const int maxvalues[] = {-1, 1, 3, 7, 15, 31, 63, 127};
593
594 int bits = 7 - modsize;
595 int partial = value & maxvalues[bits];
596 unsigned char b = subfield | (partial << modsize);
597
598 if (partial != value) {
599 b |= 0x80;
600 self->buffer[self->index] = b;
601 self->index++;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000602 return pack_packed_int(self, value >> bits);
Fred Drake8c081a12001-10-12 20:57:55 +0000603 }
Fred Drake62c1e3c2001-12-04 21:40:53 +0000604 self->buffer[self->index] = b;
605 self->index++;
606 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000607}
608
Fred Drake62c1e3c2001-12-04 21:40:53 +0000609static int
Fred Drake30d1c752001-10-15 22:11:02 +0000610pack_string(ProfilerObject *self, const char *s, int len)
Fred Drake8c081a12001-10-12 20:57:55 +0000611{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000612 if (len + PISIZE + self->index >= BUFFERSIZE) {
613 if (flush_data(self) < 0)
614 return -1;
615 }
616 if (pack_packed_int(self, len) < 0)
617 return -1;
Fred Drake8c081a12001-10-12 20:57:55 +0000618 memcpy(self->buffer + self->index, s, len);
619 self->index += len;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000620 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000621}
622
Fred Drake62c1e3c2001-12-04 21:40:53 +0000623static int
Fred Drake8c081a12001-10-12 20:57:55 +0000624pack_add_info(ProfilerObject *self, const char *s1, const char *s2)
625{
626 int len1 = strlen(s1);
627 int len2 = strlen(s2);
628
Fred Drake62c1e3c2001-12-04 21:40:53 +0000629 if (len1 + len2 + PISIZE*2 + 1 + self->index >= BUFFERSIZE) {
630 if (flush_data(self) < 0)
631 return -1;
632 }
Fred Drake8c081a12001-10-12 20:57:55 +0000633 self->buffer[self->index] = WHAT_ADD_INFO;
634 self->index++;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000635 if (pack_string(self, s1, len1) < 0)
636 return -1;
637 return pack_string(self, s2, len2);
Fred Drake8c081a12001-10-12 20:57:55 +0000638}
639
Fred Drake62c1e3c2001-12-04 21:40:53 +0000640static int
Fred Drake8c081a12001-10-12 20:57:55 +0000641pack_define_file(ProfilerObject *self, int fileno, const char *filename)
642{
643 int len = strlen(filename);
644
Fred Drake62c1e3c2001-12-04 21:40:53 +0000645 if (len + PISIZE*2 + 1 + self->index >= BUFFERSIZE) {
646 if (flush_data(self) < 0)
647 return -1;
648 }
Fred Drake8c081a12001-10-12 20:57:55 +0000649 self->buffer[self->index] = WHAT_DEFINE_FILE;
650 self->index++;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000651 if (pack_packed_int(self, fileno) < 0)
652 return -1;
653 return pack_string(self, filename, len);
Fred Drake30d1c752001-10-15 22:11:02 +0000654}
655
Fred Drake62c1e3c2001-12-04 21:40:53 +0000656static int
Fred Drake30d1c752001-10-15 22:11:02 +0000657pack_define_func(ProfilerObject *self, int fileno, int lineno,
658 const char *funcname)
659{
660 int len = strlen(funcname);
661
Fred Drake62c1e3c2001-12-04 21:40:53 +0000662 if (len + PISIZE*3 + 1 + self->index >= BUFFERSIZE) {
663 if (flush_data(self) < 0)
664 return -1;
665 }
Fred Drake30d1c752001-10-15 22:11:02 +0000666 self->buffer[self->index] = WHAT_DEFINE_FUNC;
667 self->index++;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000668 if (pack_packed_int(self, fileno) < 0)
669 return -1;
670 if (pack_packed_int(self, lineno) < 0)
671 return -1;
672 return pack_string(self, funcname, len);
Fred Drake8c081a12001-10-12 20:57:55 +0000673}
674
Fred Drake62c1e3c2001-12-04 21:40:53 +0000675static int
Fred Drake8c081a12001-10-12 20:57:55 +0000676pack_line_times(ProfilerObject *self)
677{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000678 if (2 + self->index >= BUFFERSIZE) {
679 if (flush_data(self) < 0)
680 return -1;
681 }
Fred Drake8c081a12001-10-12 20:57:55 +0000682 self->buffer[self->index] = WHAT_LINE_TIMES;
683 self->buffer[self->index + 1] = self->linetimings ? 1 : 0;
684 self->index += 2;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000685 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000686}
687
Fred Drake62c1e3c2001-12-04 21:40:53 +0000688static int
Fred Drake30d1c752001-10-15 22:11:02 +0000689pack_frame_times(ProfilerObject *self)
690{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000691 if (2 + self->index >= BUFFERSIZE) {
692 if (flush_data(self) < 0)
693 return -1;
694 }
Fred Drake30d1c752001-10-15 22:11:02 +0000695 self->buffer[self->index] = WHAT_FRAME_TIMES;
696 self->buffer[self->index + 1] = self->frametimings ? 1 : 0;
697 self->index += 2;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000698 return 0;
Fred Drake30d1c752001-10-15 22:11:02 +0000699}
700
Fred Drake62c1e3c2001-12-04 21:40:53 +0000701static inline int
Fred Drake8c081a12001-10-12 20:57:55 +0000702pack_enter(ProfilerObject *self, int fileno, int tdelta, int lineno)
703{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000704 if (MPISIZE + PISIZE*2 + self->index >= BUFFERSIZE) {
705 if (flush_data(self) < 0)
706 return -1;
707 }
Fred Drake8c081a12001-10-12 20:57:55 +0000708 pack_modified_packed_int(self, fileno, 2, WHAT_ENTER);
Fred Drake8c081a12001-10-12 20:57:55 +0000709 pack_packed_int(self, lineno);
Fred Drake30d1c752001-10-15 22:11:02 +0000710 if (self->frametimings)
Fred Drake62c1e3c2001-12-04 21:40:53 +0000711 return pack_packed_int(self, tdelta);
712 else
713 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000714}
715
Fred Drake62c1e3c2001-12-04 21:40:53 +0000716static inline int
Fred Drake8c081a12001-10-12 20:57:55 +0000717pack_exit(ProfilerObject *self, int tdelta)
718{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000719 if (MPISIZE + self->index >= BUFFERSIZE) {
720 if (flush_data(self) < 0)
721 return -1;
Fred Drake30d1c752001-10-15 22:11:02 +0000722 }
Fred Drake62c1e3c2001-12-04 21:40:53 +0000723 if (self->frametimings)
724 return pack_modified_packed_int(self, tdelta, 2, WHAT_EXIT);
725 self->buffer[self->index] = WHAT_EXIT;
726 self->index++;
727 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000728}
729
Fred Drake62c1e3c2001-12-04 21:40:53 +0000730static inline int
Fred Drake8c081a12001-10-12 20:57:55 +0000731pack_lineno(ProfilerObject *self, int lineno)
732{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000733 if (MPISIZE + self->index >= BUFFERSIZE) {
734 if (flush_data(self) < 0)
735 return -1;
736 }
737 return pack_modified_packed_int(self, lineno, 2, WHAT_LINENO);
Fred Drake8c081a12001-10-12 20:57:55 +0000738}
739
Fred Drake62c1e3c2001-12-04 21:40:53 +0000740static inline int
Fred Drake8c081a12001-10-12 20:57:55 +0000741pack_lineno_tdelta(ProfilerObject *self, int lineno, int tdelta)
742{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000743 if (MPISIZE + PISIZE + self->index >= BUFFERSIZE) {
744 if (flush_data(self) < 0)
745 return 0;
746 }
747 if (pack_modified_packed_int(self, lineno, 2, WHAT_LINENO) < 0)
748 return -1;
749 return pack_packed_int(self, tdelta);
Fred Drake8c081a12001-10-12 20:57:55 +0000750}
751
752static inline int
753get_fileno(ProfilerObject *self, PyCodeObject *fcode)
754{
Fred Drake30d1c752001-10-15 22:11:02 +0000755 /* This is only used for ENTER events. */
756
757 PyObject *obj;
758 PyObject *dict;
Fred Drake8c081a12001-10-12 20:57:55 +0000759 int fileno;
760
Fred Drake30d1c752001-10-15 22:11:02 +0000761 obj = PyDict_GetItem(self->filemap, fcode->co_filename);
762 if (obj == NULL) {
Fred Drake8c081a12001-10-12 20:57:55 +0000763 /* first sighting of this file */
Fred Drake30d1c752001-10-15 22:11:02 +0000764 dict = PyDict_New();
765 if (dict == NULL) {
Fred Drake8c081a12001-10-12 20:57:55 +0000766 return -1;
767 }
Fred Drake30d1c752001-10-15 22:11:02 +0000768 fileno = self->next_fileno;
769 obj = Py_BuildValue("iN", fileno, dict);
770 if (obj == NULL) {
771 return -1;
772 }
773 if (PyDict_SetItem(self->filemap, fcode->co_filename, obj)) {
774 Py_DECREF(obj);
Fred Drake8c081a12001-10-12 20:57:55 +0000775 return -1;
776 }
777 self->next_fileno++;
Fred Drake30d1c752001-10-15 22:11:02 +0000778 Py_DECREF(obj);
Fred Drake62c1e3c2001-12-04 21:40:53 +0000779 if (pack_define_file(self, fileno,
780 PyString_AS_STRING(fcode->co_filename)) < 0)
781 return -1;
Fred Drake8c081a12001-10-12 20:57:55 +0000782 }
783 else {
784 /* already know this ID */
Fred Drake30d1c752001-10-15 22:11:02 +0000785 fileno = PyInt_AS_LONG(PyTuple_GET_ITEM(obj, 0));
786 dict = PyTuple_GET_ITEM(obj, 1);
787 }
788 /* make sure we save a function name for this (fileno, lineno) */
789 obj = PyInt_FromLong(fcode->co_firstlineno);
790 if (obj == NULL) {
791 /* We just won't have it saved; too bad. */
792 PyErr_Clear();
793 }
794 else {
795 PyObject *name = PyDict_GetItem(dict, obj);
796 if (name == NULL) {
Fred Drake62c1e3c2001-12-04 21:40:53 +0000797 if (pack_define_func(self, fileno, fcode->co_firstlineno,
798 PyString_AS_STRING(fcode->co_name)) < 0)
799 return -1;
Fred Drake30d1c752001-10-15 22:11:02 +0000800 if (PyDict_SetItem(dict, obj, fcode->co_name))
801 return -1;
802 }
Fred Drake8c081a12001-10-12 20:57:55 +0000803 }
804 return fileno;
805}
806
807static inline int
808get_tdelta(ProfilerObject *self)
809{
810 int tdelta;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000811#ifdef MS_WINDOWS
Fred Drake8c081a12001-10-12 20:57:55 +0000812 hs_time tv;
Tim Peters7d99ff22001-10-13 07:37:52 +0000813 hs_time diff;
Fred Drake8c081a12001-10-12 20:57:55 +0000814
Tim Peters7d99ff22001-10-13 07:37:52 +0000815 GETTIMEOFDAY(&tv);
816 diff = tv - self->prev_timeofday;
817 tdelta = (int)diff;
Fred Drake8c081a12001-10-12 20:57:55 +0000818#else
819 struct timeval tv;
820
821 GETTIMEOFDAY(&tv);
822
Neal Norwitz7a135162004-06-13 20:45:11 +0000823 tdelta = tv.tv_usec - self->prev_timeofday.tv_usec;
824 if (tv.tv_sec != self->prev_timeofday.tv_sec)
825 tdelta += (tv.tv_sec - self->prev_timeofday.tv_sec) * 1000000;
Fred Drake8c081a12001-10-12 20:57:55 +0000826#endif
Neal Norwitz7a135162004-06-13 20:45:11 +0000827 /* time can go backwards on some multiprocessor systems or by NTP */
828 if (tdelta < 0)
829 return 0;
830
Fred Drake8c081a12001-10-12 20:57:55 +0000831 self->prev_timeofday = tv;
832 return tdelta;
833}
834
835
836/* The workhorse: the profiler callback function. */
837
838static int
839profiler_callback(ProfilerObject *self, PyFrameObject *frame, int what,
840 PyObject *arg)
841{
Fred Drake30d1c752001-10-15 22:11:02 +0000842 int tdelta = -1;
Fred Drake8c081a12001-10-12 20:57:55 +0000843 int fileno;
844
Fred Drake30d1c752001-10-15 22:11:02 +0000845 if (self->frametimings)
846 tdelta = get_tdelta(self);
Fred Drake8c081a12001-10-12 20:57:55 +0000847 switch (what) {
848 case PyTrace_CALL:
849 fileno = get_fileno(self, frame->f_code);
850 if (fileno < 0)
851 return -1;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000852 if (pack_enter(self, fileno, tdelta,
853 frame->f_code->co_firstlineno) < 0)
854 return -1;
Fred Drake8c081a12001-10-12 20:57:55 +0000855 break;
856 case PyTrace_RETURN:
Fred Drake62c1e3c2001-12-04 21:40:53 +0000857 if (pack_exit(self, tdelta) < 0)
858 return -1;
Fred Drake8c081a12001-10-12 20:57:55 +0000859 break;
860 default:
861 /* should never get here */
862 break;
863 }
864 return 0;
865}
866
867
868/* Alternate callback when we want PyTrace_LINE events */
869
870static int
871tracer_callback(ProfilerObject *self, PyFrameObject *frame, int what,
872 PyObject *arg)
873{
874 int fileno;
875
Fred Drake8c081a12001-10-12 20:57:55 +0000876 switch (what) {
877 case PyTrace_CALL:
878 fileno = get_fileno(self, frame->f_code);
879 if (fileno < 0)
880 return -1;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000881 return pack_enter(self, fileno,
882 self->frametimings ? get_tdelta(self) : -1,
883 frame->f_code->co_firstlineno);
884
Fred Drake8c081a12001-10-12 20:57:55 +0000885 case PyTrace_RETURN:
Fred Drake62c1e3c2001-12-04 21:40:53 +0000886 return pack_exit(self, get_tdelta(self));
887
Tim Peters1566a172001-10-12 22:08:39 +0000888 case PyTrace_LINE:
Fred Drake8c081a12001-10-12 20:57:55 +0000889 if (self->linetimings)
Michael W. Hudson806d1c82002-09-11 17:09:45 +0000890 return pack_lineno_tdelta(self, frame->f_lineno,
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000891 get_tdelta(self));
Fred Drake8c081a12001-10-12 20:57:55 +0000892 else
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000893 return pack_lineno(self, frame->f_lineno);
Fred Drake62c1e3c2001-12-04 21:40:53 +0000894
Fred Drake8c081a12001-10-12 20:57:55 +0000895 default:
896 /* ignore PyTrace_EXCEPTION */
897 break;
898 }
899 return 0;
900}
901
902
903/* A couple of useful helper functions. */
904
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000905#ifdef MS_WINDOWS
Tim Petersfeab23f2001-10-13 00:11:10 +0000906static LARGE_INTEGER frequency = {0, 0};
Fred Drake8c081a12001-10-12 20:57:55 +0000907#endif
908
909static unsigned long timeofday_diff = 0;
910static unsigned long rusage_diff = 0;
911
912static void
913calibrate(void)
914{
915 hs_time tv1, tv2;
916
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000917#ifdef MS_WINDOWS
Tim Peters7d99ff22001-10-13 07:37:52 +0000918 hs_time diff;
Fred Drake8c081a12001-10-12 20:57:55 +0000919 QueryPerformanceFrequency(&frequency);
920#endif
921
922 GETTIMEOFDAY(&tv1);
923 while (1) {
924 GETTIMEOFDAY(&tv2);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000925#ifdef MS_WINDOWS
Tim Peters7d99ff22001-10-13 07:37:52 +0000926 diff = tv2 - tv1;
927 if (diff != 0) {
928 timeofday_diff = (unsigned long)diff;
Fred Drake8c081a12001-10-12 20:57:55 +0000929 break;
930 }
931#else
932 if (tv1.tv_sec != tv2.tv_sec || tv1.tv_usec != tv2.tv_usec) {
933 if (tv1.tv_sec == tv2.tv_sec)
934 timeofday_diff = tv2.tv_usec - tv1.tv_usec;
935 else
936 timeofday_diff = (1000000 - tv1.tv_usec) + tv2.tv_usec;
937 break;
938 }
939#endif
940 }
Jack Janseneddc1442003-11-20 01:44:59 +0000941#if defined(MS_WINDOWS) || defined(PYOS_OS2) || \
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000942 defined(__VMS)
Fred Drake8c081a12001-10-12 20:57:55 +0000943 rusage_diff = -1;
944#else
945 {
946 struct rusage ru1, ru2;
947
948 getrusage(RUSAGE_SELF, &ru1);
949 while (1) {
950 getrusage(RUSAGE_SELF, &ru2);
951 if (ru1.ru_utime.tv_sec != ru2.ru_utime.tv_sec) {
952 rusage_diff = ((1000000 - ru1.ru_utime.tv_usec)
953 + ru2.ru_utime.tv_usec);
954 break;
955 }
956 else if (ru1.ru_utime.tv_usec != ru2.ru_utime.tv_usec) {
957 rusage_diff = ru2.ru_utime.tv_usec - ru1.ru_utime.tv_usec;
958 break;
959 }
960 else if (ru1.ru_stime.tv_sec != ru2.ru_stime.tv_sec) {
961 rusage_diff = ((1000000 - ru1.ru_stime.tv_usec)
962 + ru2.ru_stime.tv_usec);
963 break;
964 }
965 else if (ru1.ru_stime.tv_usec != ru2.ru_stime.tv_usec) {
966 rusage_diff = ru2.ru_stime.tv_usec - ru1.ru_stime.tv_usec;
967 break;
968 }
969 }
970 }
971#endif
972}
973
974static void
975do_start(ProfilerObject *self)
976{
977 self->active = 1;
978 GETTIMEOFDAY(&self->prev_timeofday);
979 if (self->lineevents)
980 PyEval_SetTrace((Py_tracefunc) tracer_callback, (PyObject *)self);
981 else
982 PyEval_SetProfile((Py_tracefunc) profiler_callback, (PyObject *)self);
983}
984
985static void
986do_stop(ProfilerObject *self)
987{
988 if (self->active) {
989 self->active = 0;
990 if (self->lineevents)
991 PyEval_SetTrace(NULL, NULL);
992 else
993 PyEval_SetProfile(NULL, NULL);
994 }
995 if (self->index > 0) {
996 /* Best effort to dump out any remaining data. */
997 flush_data(self);
998 }
999}
1000
1001static int
1002is_available(ProfilerObject *self)
1003{
1004 if (self->active) {
1005 PyErr_SetString(ProfilerError, "profiler already active");
1006 return 0;
1007 }
1008 if (self->logfp == NULL) {
1009 PyErr_SetString(ProfilerError, "profiler already closed");
1010 return 0;
1011 }
1012 return 1;
1013}
1014
1015
1016/* Profiler object interface methods. */
1017
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001018PyDoc_STRVAR(addinfo__doc__,
Fred Drake4c2e1af2001-10-29 20:45:57 +00001019"addinfo(key, value)\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001020"Insert an ADD_INFO record into the log.");
Fred Drake4c2e1af2001-10-29 20:45:57 +00001021
1022static PyObject *
1023profiler_addinfo(ProfilerObject *self, PyObject *args)
1024{
1025 PyObject *result = NULL;
1026 char *key, *value;
1027
1028 if (PyArg_ParseTuple(args, "ss:addinfo", &key, &value)) {
1029 if (self->logfp == NULL)
1030 PyErr_SetString(ProfilerError, "profiler already closed");
1031 else {
Fred Drake62c1e3c2001-12-04 21:40:53 +00001032 if (pack_add_info(self, key, value) == 0) {
1033 result = Py_None;
1034 Py_INCREF(result);
1035 }
Fred Drake4c2e1af2001-10-29 20:45:57 +00001036 }
1037 }
1038 return result;
1039}
1040
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001041PyDoc_STRVAR(close__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +00001042"close()\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001043"Shut down this profiler and close the log files, even if its active.");
Fred Drake8c081a12001-10-12 20:57:55 +00001044
1045static PyObject *
Fred Drake666bf522002-07-18 19:11:44 +00001046profiler_close(ProfilerObject *self)
Fred Drake8c081a12001-10-12 20:57:55 +00001047{
Fred Drake666bf522002-07-18 19:11:44 +00001048 do_stop(self);
1049 if (self->logfp != NULL) {
1050 fclose(self->logfp);
1051 self->logfp = NULL;
Fred Drake8c081a12001-10-12 20:57:55 +00001052 }
Fred Drake666bf522002-07-18 19:11:44 +00001053 Py_INCREF(Py_None);
1054 return Py_None;
1055}
1056
1057#define fileno__doc__ logreader_fileno__doc__
1058
1059static PyObject *
1060profiler_fileno(ProfilerObject *self)
1061{
1062 if (self->logfp == NULL) {
1063 PyErr_SetString(PyExc_ValueError,
1064 "profiler's file object already closed");
1065 return NULL;
1066 }
1067 return PyInt_FromLong(fileno(self->logfp));
Fred Drake8c081a12001-10-12 20:57:55 +00001068}
1069
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001070PyDoc_STRVAR(runcall__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +00001071"runcall(callable[, args[, kw]]) -> callable()\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001072"Profile a specific function call, returning the result of that call.");
Fred Drake8c081a12001-10-12 20:57:55 +00001073
1074static PyObject *
1075profiler_runcall(ProfilerObject *self, PyObject *args)
1076{
1077 PyObject *result = NULL;
1078 PyObject *callargs = NULL;
1079 PyObject *callkw = NULL;
1080 PyObject *callable;
1081
1082 if (PyArg_ParseTuple(args, "O|OO:runcall",
1083 &callable, &callargs, &callkw)) {
1084 if (is_available(self)) {
1085 do_start(self);
1086 result = PyEval_CallObjectWithKeywords(callable, callargs, callkw);
1087 do_stop(self);
1088 }
1089 }
1090 return result;
1091}
1092
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001093PyDoc_STRVAR(runcode__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +00001094"runcode(code, globals[, locals])\n"
1095"Execute a code object while collecting profile data. If locals is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001096"omitted, globals is used for the locals as well.");
Fred Drake8c081a12001-10-12 20:57:55 +00001097
1098static PyObject *
1099profiler_runcode(ProfilerObject *self, PyObject *args)
1100{
1101 PyObject *result = NULL;
1102 PyCodeObject *code;
1103 PyObject *globals;
1104 PyObject *locals = NULL;
1105
1106 if (PyArg_ParseTuple(args, "O!O!|O:runcode",
1107 &PyCode_Type, &code,
1108 &PyDict_Type, &globals,
1109 &locals)) {
1110 if (is_available(self)) {
1111 if (locals == NULL || locals == Py_None)
1112 locals = globals;
1113 else if (!PyDict_Check(locals)) {
1114 PyErr_SetString(PyExc_TypeError,
1115 "locals must be a dictionary or None");
1116 return NULL;
1117 }
1118 do_start(self);
1119 result = PyEval_EvalCode(code, globals, locals);
1120 do_stop(self);
1121#if 0
1122 if (!PyErr_Occurred()) {
1123 result = Py_None;
1124 Py_INCREF(result);
1125 }
1126#endif
1127 }
1128 }
1129 return result;
1130}
1131
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001132PyDoc_STRVAR(start__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +00001133"start()\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001134"Install this profiler for the current thread.");
Fred Drake8c081a12001-10-12 20:57:55 +00001135
1136static PyObject *
1137profiler_start(ProfilerObject *self, PyObject *args)
1138{
1139 PyObject *result = NULL;
1140
Fred Drake666bf522002-07-18 19:11:44 +00001141 if (is_available(self)) {
1142 do_start(self);
1143 result = Py_None;
1144 Py_INCREF(result);
Fred Drake8c081a12001-10-12 20:57:55 +00001145 }
1146 return result;
1147}
1148
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001149PyDoc_STRVAR(stop__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +00001150"stop()\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001151"Remove this profiler from the current thread.");
Fred Drake8c081a12001-10-12 20:57:55 +00001152
1153static PyObject *
1154profiler_stop(ProfilerObject *self, PyObject *args)
1155{
1156 PyObject *result = NULL;
1157
Fred Drake666bf522002-07-18 19:11:44 +00001158 if (!self->active)
1159 PyErr_SetString(ProfilerError, "profiler not active");
1160 else {
1161 do_stop(self);
1162 result = Py_None;
1163 Py_INCREF(result);
Fred Drake8c081a12001-10-12 20:57:55 +00001164 }
1165 return result;
1166}
1167
1168
1169/* Python API support. */
1170
1171static void
1172profiler_dealloc(ProfilerObject *self)
1173{
1174 do_stop(self);
1175 if (self->logfp != NULL)
1176 fclose(self->logfp);
1177 Py_XDECREF(self->filemap);
1178 Py_XDECREF(self->logfilename);
1179 PyObject_Del((PyObject *)self);
1180}
1181
Fred Drake8c081a12001-10-12 20:57:55 +00001182static PyMethodDef profiler_methods[] = {
Fred Drake4c2e1af2001-10-29 20:45:57 +00001183 {"addinfo", (PyCFunction)profiler_addinfo, METH_VARARGS, addinfo__doc__},
Fred Drake666bf522002-07-18 19:11:44 +00001184 {"close", (PyCFunction)profiler_close, METH_NOARGS, close__doc__},
1185 {"fileno", (PyCFunction)profiler_fileno, METH_NOARGS, fileno__doc__},
Fred Drake8c081a12001-10-12 20:57:55 +00001186 {"runcall", (PyCFunction)profiler_runcall, METH_VARARGS, runcall__doc__},
1187 {"runcode", (PyCFunction)profiler_runcode, METH_VARARGS, runcode__doc__},
Fred Drake666bf522002-07-18 19:11:44 +00001188 {"start", (PyCFunction)profiler_start, METH_NOARGS, start__doc__},
1189 {"stop", (PyCFunction)profiler_stop, METH_NOARGS, stop__doc__},
Fred Drake8c081a12001-10-12 20:57:55 +00001190 {NULL, NULL}
1191};
1192
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001193static PyMemberDef profiler_members[] = {
Fred Drake30d1c752001-10-15 22:11:02 +00001194 {"frametimings", T_LONG, offsetof(ProfilerObject, linetimings), READONLY},
1195 {"lineevents", T_LONG, offsetof(ProfilerObject, lineevents), READONLY},
1196 {"linetimings", T_LONG, offsetof(ProfilerObject, linetimings), READONLY},
Fred Drake8c081a12001-10-12 20:57:55 +00001197 {NULL}
1198};
1199
1200static PyObject *
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001201profiler_get_closed(ProfilerObject *self, void *closure)
Fred Drake8c081a12001-10-12 20:57:55 +00001202{
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001203 PyObject *result = (self->logfp == NULL) ? Py_True : Py_False;
1204 Py_INCREF(result);
Fred Drake8c081a12001-10-12 20:57:55 +00001205 return result;
1206}
1207
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001208static PyGetSetDef profiler_getsets[] = {
Fred Draked1eb8b62002-07-17 18:54:20 +00001209 {"closed", (getter)profiler_get_closed, NULL,
Fred Drake5c3ed3d2002-07-17 19:38:05 +00001210 PyDoc_STR("True if the profiler's output file has already been closed.")},
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001211 {NULL}
1212};
1213
Fred Drake8c081a12001-10-12 20:57:55 +00001214
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001215PyDoc_STRVAR(profiler_object__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +00001216"High-performance profiler object.\n"
1217"\n"
1218"Methods:\n"
1219"\n"
Fred Drake30d1c752001-10-15 22:11:02 +00001220"close(): Stop the profiler and close the log files.\n"
Fred Drake666bf522002-07-18 19:11:44 +00001221"fileno(): Returns the file descriptor of the log file.\n"
Fred Drake30d1c752001-10-15 22:11:02 +00001222"runcall(): Run a single function call with profiling enabled.\n"
1223"runcode(): Execute a code object with profiling enabled.\n"
1224"start(): Install the profiler and return.\n"
1225"stop(): Remove the profiler.\n"
Tim Petersfeab23f2001-10-13 00:11:10 +00001226"\n"
1227"Attributes (read-only):\n"
1228"\n"
Fred Drake30d1c752001-10-15 22:11:02 +00001229"closed: True if the profiler has already been closed.\n"
1230"frametimings: True if ENTER/EXIT events collect timing information.\n"
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001231"lineevents: True if line events are reported to the profiler.\n"
1232"linetimings: True if line events collect timing information.");
Fred Drake8c081a12001-10-12 20:57:55 +00001233
1234static PyTypeObject ProfilerType = {
1235 PyObject_HEAD_INIT(NULL)
1236 0, /* ob_size */
Fred Drake4c2e1af2001-10-29 20:45:57 +00001237 "_hotshot.ProfilerType", /* tp_name */
Fred Drake8c081a12001-10-12 20:57:55 +00001238 (int) sizeof(ProfilerObject), /* tp_basicsize */
1239 0, /* tp_itemsize */
1240 (destructor)profiler_dealloc, /* tp_dealloc */
1241 0, /* tp_print */
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001242 0, /* tp_getattr */
Fred Drake8c081a12001-10-12 20:57:55 +00001243 0, /* tp_setattr */
1244 0, /* tp_compare */
1245 0, /* tp_repr */
1246 0, /* tp_as_number */
1247 0, /* tp_as_sequence */
1248 0, /* tp_as_mapping */
1249 0, /* tp_hash */
1250 0, /* tp_call */
1251 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00001252 PyObject_GenericGetAttr, /* tp_getattro */
Fred Drake8c081a12001-10-12 20:57:55 +00001253 0, /* tp_setattro */
1254 0, /* tp_as_buffer */
Fred Drake4c2e1af2001-10-29 20:45:57 +00001255 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake8c081a12001-10-12 20:57:55 +00001256 profiler_object__doc__, /* tp_doc */
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001257 0, /* tp_traverse */
1258 0, /* tp_clear */
1259 0, /* tp_richcompare */
1260 0, /* tp_weaklistoffset */
1261 0, /* tp_iter */
1262 0, /* tp_iternext */
1263 profiler_methods, /* tp_methods */
1264 profiler_members, /* tp_members */
1265 profiler_getsets, /* tp_getset */
1266 0, /* tp_base */
1267 0, /* tp_dict */
1268 0, /* tp_descr_get */
1269 0, /* tp_descr_set */
Fred Drake8c081a12001-10-12 20:57:55 +00001270};
1271
1272
1273static PyMethodDef logreader_methods[] = {
Fred Drake666bf522002-07-18 19:11:44 +00001274 {"close", (PyCFunction)logreader_close, METH_NOARGS,
Fred Drake8c081a12001-10-12 20:57:55 +00001275 logreader_close__doc__},
Fred Drake666bf522002-07-18 19:11:44 +00001276 {"fileno", (PyCFunction)logreader_fileno, METH_NOARGS,
1277 logreader_fileno__doc__},
Fred Drake8c081a12001-10-12 20:57:55 +00001278 {NULL, NULL}
1279};
1280
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001281static PyMemberDef logreader_members[] = {
Fred Drake5c3ed3d2002-07-17 19:38:05 +00001282 {"info", T_OBJECT, offsetof(LogReaderObject, info), RO,
1283 PyDoc_STR("Dictionary mapping informational keys to lists of values.")},
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001284 {NULL}
1285};
Fred Drake8c081a12001-10-12 20:57:55 +00001286
1287
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001288PyDoc_STRVAR(logreader__doc__,
1289"logreader(filename) --> log-iterator\n\
1290Create a log-reader for the timing information file.");
Fred Drake8c081a12001-10-12 20:57:55 +00001291
1292static PySequenceMethods logreader_as_sequence = {
1293 0, /* sq_length */
1294 0, /* sq_concat */
1295 0, /* sq_repeat */
1296 (intargfunc)logreader_sq_item, /* sq_item */
1297 0, /* sq_slice */
1298 0, /* sq_ass_item */
1299 0, /* sq_ass_slice */
1300 0, /* sq_contains */
1301 0, /* sq_inplace_concat */
1302 0, /* sq_inplace_repeat */
1303};
1304
Fred Drake666bf522002-07-18 19:11:44 +00001305static PyObject *
1306logreader_get_closed(LogReaderObject *self, void *closure)
1307{
1308 PyObject *result = (self->logfp == NULL) ? Py_True : Py_False;
1309 Py_INCREF(result);
1310 return result;
1311}
1312
1313static PyGetSetDef logreader_getsets[] = {
1314 {"closed", (getter)logreader_get_closed, NULL,
1315 PyDoc_STR("True if the logreader's input file has already been closed.")},
1316 {NULL}
1317};
1318
Fred Drake8c081a12001-10-12 20:57:55 +00001319static PyTypeObject LogReaderType = {
1320 PyObject_HEAD_INIT(NULL)
1321 0, /* ob_size */
Fred Drake4c2e1af2001-10-29 20:45:57 +00001322 "_hotshot.LogReaderType", /* tp_name */
Fred Drake8c081a12001-10-12 20:57:55 +00001323 (int) sizeof(LogReaderObject), /* tp_basicsize */
1324 0, /* tp_itemsize */
1325 (destructor)logreader_dealloc, /* tp_dealloc */
1326 0, /* tp_print */
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001327 0, /* tp_getattr */
Fred Drake8c081a12001-10-12 20:57:55 +00001328 0, /* tp_setattr */
1329 0, /* tp_compare */
1330 0, /* tp_repr */
1331 0, /* tp_as_number */
1332 &logreader_as_sequence, /* tp_as_sequence */
1333 0, /* tp_as_mapping */
1334 0, /* tp_hash */
1335 0, /* tp_call */
1336 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00001337 PyObject_GenericGetAttr, /* tp_getattro */
Fred Drake8c081a12001-10-12 20:57:55 +00001338 0, /* tp_setattro */
1339 0, /* tp_as_buffer */
Fred Drake4c2e1af2001-10-29 20:45:57 +00001340 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake8c081a12001-10-12 20:57:55 +00001341 logreader__doc__, /* tp_doc */
Fred Drake8c081a12001-10-12 20:57:55 +00001342 0, /* tp_traverse */
1343 0, /* tp_clear */
1344 0, /* tp_richcompare */
1345 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00001346 PyObject_SelfIter, /* tp_iter */
Fred Drake8c081a12001-10-12 20:57:55 +00001347 (iternextfunc)logreader_tp_iternext,/* tp_iternext */
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001348 logreader_methods, /* tp_methods */
1349 logreader_members, /* tp_members */
Fred Drake666bf522002-07-18 19:11:44 +00001350 logreader_getsets, /* tp_getset */
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001351 0, /* tp_base */
1352 0, /* tp_dict */
1353 0, /* tp_descr_get */
1354 0, /* tp_descr_set */
Fred Drake8c081a12001-10-12 20:57:55 +00001355};
1356
1357static PyObject *
1358hotshot_logreader(PyObject *unused, PyObject *args)
1359{
1360 LogReaderObject *self = NULL;
1361 char *filename;
Neil Schemenauer8b6b4912002-05-29 18:19:14 +00001362 int c;
1363 int err = 0;
Fred Drake8c081a12001-10-12 20:57:55 +00001364
1365 if (PyArg_ParseTuple(args, "s:logreader", &filename)) {
1366 self = PyObject_New(LogReaderObject, &LogReaderType);
1367 if (self != NULL) {
Fred Drake30d1c752001-10-15 22:11:02 +00001368 self->frametimings = 1;
Fred Drake8c081a12001-10-12 20:57:55 +00001369 self->linetimings = 0;
Fred Drake4c2e1af2001-10-29 20:45:57 +00001370 self->info = NULL;
Fred Drake8c081a12001-10-12 20:57:55 +00001371 self->logfp = fopen(filename, "rb");
1372 if (self->logfp == NULL) {
1373 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
1374 Py_DECREF(self);
1375 self = NULL;
Fred Drake4c2e1af2001-10-29 20:45:57 +00001376 goto finally;
1377 }
1378 self->info = PyDict_New();
1379 if (self->info == NULL) {
1380 Py_DECREF(self);
1381 goto finally;
1382 }
Neil Schemenauer8b6b4912002-05-29 18:19:14 +00001383 /* read initial info */
1384 for (;;) {
1385 if ((c = fgetc(self->logfp)) == EOF) {
Fred Drake666bf522002-07-18 19:11:44 +00001386 eof_error(self);
Neil Schemenauer8b6b4912002-05-29 18:19:14 +00001387 break;
1388 }
1389 if (c != WHAT_ADD_INFO) {
1390 ungetc(c, self->logfp);
1391 break;
1392 }
1393 err = unpack_add_info(self);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001394 if (err) {
1395 if (err == ERR_EOF)
Fred Drake666bf522002-07-18 19:11:44 +00001396 eof_error(self);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001397 else
1398 PyErr_SetString(PyExc_RuntimeError,
1399 "unexpected error");
1400 break;
1401 }
Fred Drake8c081a12001-10-12 20:57:55 +00001402 }
1403 }
1404 }
Fred Drake4c2e1af2001-10-29 20:45:57 +00001405 finally:
Fred Drake8c081a12001-10-12 20:57:55 +00001406 return (PyObject *) self;
1407}
1408
1409
1410/* Return a Python string that represents the version number without the
1411 * extra cruft added by revision control, even if the right options were
1412 * given to the "cvs export" command to make it not include the extra
1413 * cruft.
1414 */
1415static char *
1416get_version_string(void)
1417{
1418 static char *rcsid = "$Revision$";
1419 char *rev = rcsid;
1420 char *buffer;
1421 int i = 0;
1422
Neal Norwitz3afb2d22002-03-20 21:32:07 +00001423 while (*rev && !isdigit((int)*rev))
Fred Drake8c081a12001-10-12 20:57:55 +00001424 ++rev;
1425 while (rev[i] != ' ' && rev[i] != '\0')
1426 ++i;
1427 buffer = malloc(i + 1);
1428 if (buffer != NULL) {
1429 memmove(buffer, rev, i);
1430 buffer[i] = '\0';
1431 }
1432 return buffer;
1433}
1434
1435/* Write out a RFC 822-style header with various useful bits of
1436 * information to make the output easier to manage.
1437 */
1438static int
1439write_header(ProfilerObject *self)
1440{
1441 char *buffer;
1442 char cwdbuffer[PATH_MAX];
1443 PyObject *temp;
1444 int i, len;
1445
1446 buffer = get_version_string();
1447 if (buffer == NULL) {
1448 PyErr_NoMemory();
1449 return -1;
1450 }
Fred Drake4c2e1af2001-10-29 20:45:57 +00001451 pack_add_info(self, "hotshot-version", buffer);
1452 pack_add_info(self, "requested-frame-timings",
1453 (self->frametimings ? "yes" : "no"));
1454 pack_add_info(self, "requested-line-events",
Fred Drake8c081a12001-10-12 20:57:55 +00001455 (self->lineevents ? "yes" : "no"));
Fred Drake4c2e1af2001-10-29 20:45:57 +00001456 pack_add_info(self, "requested-line-timings",
1457 (self->linetimings ? "yes" : "no"));
1458 pack_add_info(self, "platform", Py_GetPlatform());
1459 pack_add_info(self, "executable", Py_GetProgramFullPath());
Fred Drakef12a68c2001-11-09 15:59:36 +00001460 free(buffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001461 buffer = (char *) Py_GetVersion();
1462 if (buffer == NULL)
1463 PyErr_Clear();
1464 else
Fred Drake4c2e1af2001-10-29 20:45:57 +00001465 pack_add_info(self, "executable-version", buffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001466
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001467#ifdef MS_WINDOWS
Tim Peters885d4572001-11-28 20:27:42 +00001468 PyOS_snprintf(cwdbuffer, sizeof(cwdbuffer), "%I64d", frequency.QuadPart);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001469 pack_add_info(self, "reported-performance-frequency", cwdbuffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001470#else
Tim Peters885d4572001-11-28 20:27:42 +00001471 PyOS_snprintf(cwdbuffer, sizeof(cwdbuffer), "%lu", rusage_diff);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001472 pack_add_info(self, "observed-interval-getrusage", cwdbuffer);
Tim Peters885d4572001-11-28 20:27:42 +00001473 PyOS_snprintf(cwdbuffer, sizeof(cwdbuffer), "%lu", timeofday_diff);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001474 pack_add_info(self, "observed-interval-gettimeofday", cwdbuffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001475#endif
Fred Drake8c081a12001-10-12 20:57:55 +00001476
Fred Drake4c2e1af2001-10-29 20:45:57 +00001477 pack_add_info(self, "current-directory",
Fred Drake8c081a12001-10-12 20:57:55 +00001478 getcwd(cwdbuffer, sizeof cwdbuffer));
1479
1480 temp = PySys_GetObject("path");
1481 len = PyList_GET_SIZE(temp);
1482 for (i = 0; i < len; ++i) {
1483 PyObject *item = PyList_GET_ITEM(temp, i);
1484 buffer = PyString_AsString(item);
Fred Draked1eb8b62002-07-17 18:54:20 +00001485 if (buffer == NULL) {
1486 pack_add_info(self, "sys-path-entry", "<non-string-path-entry>");
1487 PyErr_Clear();
1488 }
1489 else {
1490 pack_add_info(self, "sys-path-entry", buffer);
1491 }
Fred Drake8c081a12001-10-12 20:57:55 +00001492 }
Fred Drake4c2e1af2001-10-29 20:45:57 +00001493 pack_frame_times(self);
1494 pack_line_times(self);
1495
Fred Drake8c081a12001-10-12 20:57:55 +00001496 return 0;
1497}
1498
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001499PyDoc_STRVAR(profiler__doc__,
1500"profiler(logfilename[, lineevents[, linetimes]]) -> profiler\n\
1501Create a new profiler object.");
Fred Drake8c081a12001-10-12 20:57:55 +00001502
1503static PyObject *
1504hotshot_profiler(PyObject *unused, PyObject *args)
1505{
1506 char *logfilename;
1507 ProfilerObject *self = NULL;
1508 int lineevents = 0;
1509 int linetimings = 1;
1510
1511 if (PyArg_ParseTuple(args, "s|ii:profiler", &logfilename,
1512 &lineevents, &linetimings)) {
1513 self = PyObject_New(ProfilerObject, &ProfilerType);
1514 if (self == NULL)
1515 return NULL;
Fred Drake30d1c752001-10-15 22:11:02 +00001516 self->frametimings = 1;
Fred Drake8c081a12001-10-12 20:57:55 +00001517 self->lineevents = lineevents ? 1 : 0;
1518 self->linetimings = (lineevents && linetimings) ? 1 : 0;
1519 self->index = 0;
1520 self->active = 0;
1521 self->next_fileno = 0;
Tim Peters1566a172001-10-12 22:08:39 +00001522 self->logfp = NULL;
Fred Drake8c081a12001-10-12 20:57:55 +00001523 self->logfilename = PyTuple_GET_ITEM(args, 0);
1524 Py_INCREF(self->logfilename);
1525 self->filemap = PyDict_New();
1526 if (self->filemap == NULL) {
1527 Py_DECREF(self);
1528 return NULL;
1529 }
1530 self->logfp = fopen(logfilename, "wb");
1531 if (self->logfp == NULL) {
1532 Py_DECREF(self);
1533 PyErr_SetFromErrnoWithFilename(PyExc_IOError, logfilename);
1534 return NULL;
1535 }
1536 if (timeofday_diff == 0) {
1537 /* Run this several times since sometimes the first
1538 * doesn't give the lowest values, and we're really trying
1539 * to determine the lowest.
1540 */
1541 calibrate();
1542 calibrate();
1543 calibrate();
1544 }
1545 if (write_header(self))
1546 /* some error occurred, exception has been set */
1547 self = NULL;
1548 }
1549 return (PyObject *) self;
1550}
1551
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001552PyDoc_STRVAR(coverage__doc__,
1553"coverage(logfilename) -> profiler\n\
Fred Drake30d1c752001-10-15 22:11:02 +00001554Returns a profiler that doesn't collect any timing information, which is\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001555useful in building a coverage analysis tool.");
Fred Drake30d1c752001-10-15 22:11:02 +00001556
1557static PyObject *
1558hotshot_coverage(PyObject *unused, PyObject *args)
1559{
1560 char *logfilename;
1561 PyObject *result = NULL;
1562
1563 if (PyArg_ParseTuple(args, "s:coverage", &logfilename)) {
1564 result = hotshot_profiler(unused, args);
1565 if (result != NULL) {
1566 ProfilerObject *self = (ProfilerObject *) result;
1567 self->frametimings = 0;
1568 self->linetimings = 0;
1569 self->lineevents = 1;
1570 }
1571 }
1572 return result;
1573}
1574
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001575PyDoc_VAR(resolution__doc__) =
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001576#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001577PyDoc_STR(
Tim Petersfeab23f2001-10-13 00:11:10 +00001578"resolution() -> (performance-counter-ticks, update-frequency)\n"
1579"Return the resolution of the timer provided by the QueryPerformanceCounter()\n"
1580"function. The first value is the smallest observed change, and the second\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001581"is the result of QueryPerformanceFrequency()."
1582)
Fred Drake8c081a12001-10-12 20:57:55 +00001583#else
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001584PyDoc_STR(
Tim Petersfeab23f2001-10-13 00:11:10 +00001585"resolution() -> (gettimeofday-usecs, getrusage-usecs)\n"
1586"Return the resolution of the timers provided by the gettimeofday() and\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001587"getrusage() system calls, or -1 if the call is not supported."
1588)
Fred Drake8c081a12001-10-12 20:57:55 +00001589#endif
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001590;
Fred Drake8c081a12001-10-12 20:57:55 +00001591
1592static PyObject *
1593hotshot_resolution(PyObject *unused, PyObject *args)
1594{
1595 PyObject *result = NULL;
1596
1597 if (PyArg_ParseTuple(args, ":resolution")) {
1598 if (timeofday_diff == 0) {
1599 calibrate();
1600 calibrate();
1601 calibrate();
1602 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001603#ifdef MS_WINDOWS
Fred Drake8c081a12001-10-12 20:57:55 +00001604 result = Py_BuildValue("ii", timeofday_diff, frequency.LowPart);
1605#else
1606 result = Py_BuildValue("ii", timeofday_diff, rusage_diff);
1607#endif
1608 }
1609 return result;
1610}
1611
1612
1613static PyMethodDef functions[] = {
Fred Drake30d1c752001-10-15 22:11:02 +00001614 {"coverage", hotshot_coverage, METH_VARARGS, coverage__doc__},
Fred Drake8c081a12001-10-12 20:57:55 +00001615 {"profiler", hotshot_profiler, METH_VARARGS, profiler__doc__},
1616 {"logreader", hotshot_logreader, METH_VARARGS, logreader__doc__},
1617 {"resolution", hotshot_resolution, METH_VARARGS, resolution__doc__},
1618 {NULL, NULL}
1619};
1620
1621
1622void
1623init_hotshot(void)
1624{
1625 PyObject *module;
1626
1627 LogReaderType.ob_type = &PyType_Type;
1628 ProfilerType.ob_type = &PyType_Type;
1629 module = Py_InitModule("_hotshot", functions);
1630 if (module != NULL) {
1631 char *s = get_version_string();
1632
1633 PyModule_AddStringConstant(module, "__version__", s);
1634 free(s);
1635 Py_INCREF(&LogReaderType);
1636 PyModule_AddObject(module, "LogReaderType",
1637 (PyObject *)&LogReaderType);
1638 Py_INCREF(&ProfilerType);
1639 PyModule_AddObject(module, "ProfilerType",
1640 (PyObject *)&ProfilerType);
1641
1642 if (ProfilerError == NULL)
1643 ProfilerError = PyErr_NewException("hotshot.ProfilerError",
1644 NULL, NULL);
1645 if (ProfilerError != NULL) {
1646 Py_INCREF(ProfilerError);
1647 PyModule_AddObject(module, "ProfilerError", ProfilerError);
1648 }
1649 PyModule_AddIntConstant(module, "WHAT_ENTER", WHAT_ENTER);
1650 PyModule_AddIntConstant(module, "WHAT_EXIT", WHAT_EXIT);
1651 PyModule_AddIntConstant(module, "WHAT_LINENO", WHAT_LINENO);
1652 PyModule_AddIntConstant(module, "WHAT_OTHER", WHAT_OTHER);
1653 PyModule_AddIntConstant(module, "WHAT_ADD_INFO", WHAT_ADD_INFO);
1654 PyModule_AddIntConstant(module, "WHAT_DEFINE_FILE", WHAT_DEFINE_FILE);
Fred Drake30d1c752001-10-15 22:11:02 +00001655 PyModule_AddIntConstant(module, "WHAT_DEFINE_FUNC", WHAT_DEFINE_FUNC);
Fred Drake8c081a12001-10-12 20:57:55 +00001656 PyModule_AddIntConstant(module, "WHAT_LINE_TIMES", WHAT_LINE_TIMES);
1657 }
1658}