blob: 2ca448620ee5be376faba1c62e9c84c76ece0e87 [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
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000029#if defined(macintosh) || (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
Jack Jansen963659a2001-10-23 22:26:16 +000050#ifdef macintosh
51#define PATH_MAX 254
52#endif
53
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000054#if defined(PYOS_OS2) && defined(PYCC_GCC)
55#define PATH_MAX 260
56#endif
57
Tim Peters1566a172001-10-12 22:08:39 +000058#ifndef PATH_MAX
59# ifdef MAX_PATH
60# define PATH_MAX MAX_PATH
61# else
62# error "Need a defn. for PATH_MAX in _hotshot.c"
63# endif
64#endif
65
Fred Drake8c081a12001-10-12 20:57:55 +000066typedef struct {
67 PyObject_HEAD
68 PyObject *filemap;
69 PyObject *logfilename;
70 int index;
71 unsigned char buffer[BUFFERSIZE];
72 FILE *logfp;
73 int lineevents;
74 int linetimings;
Fred Drake30d1c752001-10-15 22:11:02 +000075 int frametimings;
Fred Drake8c081a12001-10-12 20:57:55 +000076 /* size_t filled; */
77 int active;
78 int next_fileno;
Fred Drake8c081a12001-10-12 20:57:55 +000079 hs_time prev_timeofday;
80} ProfilerObject;
81
82typedef struct {
83 PyObject_HEAD
Fred Drake4c2e1af2001-10-29 20:45:57 +000084 PyObject *info;
Fred Drake8c081a12001-10-12 20:57:55 +000085 FILE *logfp;
Fred Drake8c081a12001-10-12 20:57:55 +000086 int linetimings;
Fred Drake30d1c752001-10-15 22:11:02 +000087 int frametimings;
Fred Drake8c081a12001-10-12 20:57:55 +000088} LogReaderObject;
89
90static PyObject * ProfilerError = NULL;
91
92
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000093#ifndef MS_WINDOWS
Fred Drake8c081a12001-10-12 20:57:55 +000094#ifdef GETTIMEOFDAY_NO_TZ
95#define GETTIMEOFDAY(ptv) gettimeofday((ptv))
96#else
97#define GETTIMEOFDAY(ptv) gettimeofday((ptv), (struct timezone *)NULL)
98#endif
99#endif
100
101
102/* The log reader... */
103
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000104PyDoc_STRVAR(logreader_close__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +0000105"close()\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000106"Close the log file, preventing additional records from being read.");
Fred Drake8c081a12001-10-12 20:57:55 +0000107
108static PyObject *
109logreader_close(LogReaderObject *self, PyObject *args)
110{
Fred Drake666bf522002-07-18 19:11:44 +0000111 if (self->logfp != NULL) {
112 fclose(self->logfp);
113 self->logfp = NULL;
Fred Drake8c081a12001-10-12 20:57:55 +0000114 }
Fred Drake666bf522002-07-18 19:11:44 +0000115 Py_INCREF(Py_None);
116
117 return Py_None;
118}
119
120PyDoc_STRVAR(logreader_fileno__doc__,
121"fileno() -> file descriptor\n"
122"Returns the file descriptor for the log file, if open.\n"
123"Raises ValueError if the log file is closed.");
124
125static PyObject *
126logreader_fileno(LogReaderObject *self)
127{
128 if (self->logfp == NULL) {
129 PyErr_SetString(PyExc_ValueError,
130 "logreader's file object already closed");
131 return NULL;
132 }
133 return PyInt_FromLong(fileno(self->logfp));
Fred Drake8c081a12001-10-12 20:57:55 +0000134}
135
Fred Drake8c081a12001-10-12 20:57:55 +0000136static PyObject *
137logreader_tp_iter(LogReaderObject *self)
138{
139 Py_INCREF(self);
140 return (PyObject *) self;
141}
Fred Drake8c081a12001-10-12 20:57:55 +0000142
143
144/* Log File Format
145 * ---------------
146 *
147 * The log file consists of a sequence of variable-length records.
148 * Each record is identified with a record type identifier in two
149 * bits of the first byte. The two bits are the "least significant"
150 * bits of the byte.
151 *
152 * Low bits: Opcode: Meaning:
153 * 0x00 ENTER enter a frame
154 * 0x01 EXIT exit a frame
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000155 * 0x02 LINENO execution moved onto a different line
Fred Drake8c081a12001-10-12 20:57:55 +0000156 * 0x03 OTHER more bits are needed to deecode
157 *
158 * If the type is OTHER, the record is not packed so tightly, and the
159 * remaining bits are used to disambiguate the record type. These
160 * records are not used as frequently so compaction is not an issue.
161 * Each of the first three record types has a highly tailored
162 * structure that allows it to be packed tightly.
163 *
164 * The OTHER records have the following identifiers:
165 *
166 * First byte: Opcode: Meaning:
167 * 0x13 ADD_INFO define a key/value pair
168 * 0x23 DEFINE_FILE define an int->filename mapping
169 * 0x33 LINE_TIMES indicates if LINENO events have tdeltas
Fred Drake30d1c752001-10-15 22:11:02 +0000170 * 0x43 DEFINE_FUNC define a (fileno,lineno)->funcname mapping
171 * 0x53 FRAME_TIMES indicates if ENTER/EXIT events have tdeltas
Fred Drake8c081a12001-10-12 20:57:55 +0000172 *
173 * Packed Integers
174 *
175 * "Packed integers" are non-negative integer values encoded as a
176 * sequence of bytes. Each byte is encoded such that the most
177 * significant bit is set if the next byte is also part of the
178 * integer. Each byte provides bits to the least-significant end of
179 * the result; the accumulated value must be shifted up to place the
180 * new bits into the result.
181 *
182 * "Modified packed integers" are packed integers where only a portion
183 * of the first byte is used. In the rest of the specification, these
184 * are referred to as "MPI(n,name)", where "n" is the number of bits
185 * discarded from the least-signicant positions of the byte, and
186 * "name" is a name being given to those "discarded" bits, since they
187 * are a field themselves.
188 *
189 * ENTER records:
190 *
191 * MPI(2,type) fileno -- type is 0x00
Fred Drake8c081a12001-10-12 20:57:55 +0000192 * PI lineno
Fred Drake30d1c752001-10-15 22:11:02 +0000193 * PI tdelta -- iff frame times are enabled
Fred Drake8c081a12001-10-12 20:57:55 +0000194 *
195 * EXIT records
196 *
Fred Drake30d1c752001-10-15 22:11:02 +0000197 * MPI(2,type) tdelta -- type is 0x01; tdelta will be 0
198 * if frame times are disabled
Fred Drake8c081a12001-10-12 20:57:55 +0000199 *
200 * LINENO records
201 *
202 * MPI(2,type) lineno -- type is 0x02
203 * PI tdelta -- iff LINENO includes it
204 *
205 * ADD_INFO records
206 *
Fred Drake30d1c752001-10-15 22:11:02 +0000207 * BYTE type -- always 0x13
Fred Drake8c081a12001-10-12 20:57:55 +0000208 * PI len1 -- length of first string
209 * BYTE string1[len1] -- len1 bytes of string data
210 * PI len2 -- length of second string
211 * BYTE string2[len2] -- len2 bytes of string data
212 *
213 * DEFINE_FILE records
214 *
Fred Drake30d1c752001-10-15 22:11:02 +0000215 * BYTE type -- always 0x23
Fred Drake8c081a12001-10-12 20:57:55 +0000216 * PI fileno
217 * PI len -- length of filename
218 * BYTE filename[len] -- len bytes of string data
219 *
Fred Drake30d1c752001-10-15 22:11:02 +0000220 * DEFINE_FUNC records
221 *
222 * BYTE type -- always 0x43
223 * PI fileno
224 * PI lineno
225 * PI len -- length of funcname
226 * BYTE funcname[len] -- len bytes of string data
227 *
Fred Drake8c081a12001-10-12 20:57:55 +0000228 * LINE_TIMES records
Fred Drake30d1c752001-10-15 22:11:02 +0000229 *
230 * This record can be used only before the start of ENTER/EXIT/LINENO
231 * records. If have_tdelta is true, LINENO records will include the
232 * tdelta field, otherwise it will be omitted. If this record is not
233 * given, LINENO records will not contain the tdelta field.
234 *
235 * BYTE type -- always 0x33
Fred Drake8c081a12001-10-12 20:57:55 +0000236 * BYTE have_tdelta -- 0 if LINENO does *not* have
237 * timing information
Fred Drake30d1c752001-10-15 22:11:02 +0000238 * FRAME_TIMES records
239 *
240 * This record can be used only before the start of ENTER/EXIT/LINENO
241 * records. If have_tdelta is true, ENTER and EXIT records will
242 * include the tdelta field, otherwise it will be omitted. If this
243 * record is not given, ENTER and EXIT records will contain the tdelta
244 * field.
245 *
246 * BYTE type -- always 0x53
247 * BYTE have_tdelta -- 0 if ENTER/EXIT do *not* have
248 * timing information
Fred Drake8c081a12001-10-12 20:57:55 +0000249 */
250
251#define WHAT_ENTER 0x00
252#define WHAT_EXIT 0x01
253#define WHAT_LINENO 0x02
254#define WHAT_OTHER 0x03 /* only used in decoding */
255#define WHAT_ADD_INFO 0x13
256#define WHAT_DEFINE_FILE 0x23
257#define WHAT_LINE_TIMES 0x33
Fred Drake30d1c752001-10-15 22:11:02 +0000258#define WHAT_DEFINE_FUNC 0x43
259#define WHAT_FRAME_TIMES 0x53
Fred Drake8c081a12001-10-12 20:57:55 +0000260
261#define ERR_NONE 0
262#define ERR_EOF -1
263#define ERR_EXCEPTION -2
Fred Drake4c2e1af2001-10-29 20:45:57 +0000264#define ERR_BAD_RECTYPE -3
Fred Drake8c081a12001-10-12 20:57:55 +0000265
266#define PISIZE (sizeof(int) + 1)
267#define MPISIZE (PISIZE + 1)
268
269/* Maximum size of "normal" events -- nothing that contains string data */
270#define MAXEVENTSIZE (MPISIZE + PISIZE*2)
271
272
273/* Unpack a packed integer; if "discard" is non-zero, unpack a modified
274 * packed integer with "discard" discarded bits.
275 */
276static int
277unpack_packed_int(LogReaderObject *self, int *pvalue, int discard)
278{
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000279 int c;
Fred Drake8c081a12001-10-12 20:57:55 +0000280 int accum = 0;
281 int bits = 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000282 int cont;
283
284 do {
Fred Drake8c081a12001-10-12 20:57:55 +0000285 /* read byte */
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000286 if ((c = fgetc(self->logfp)) == EOF)
287 return ERR_EOF;
288 accum |= ((c & 0x7F) >> discard) << bits;
Fred Drake8c081a12001-10-12 20:57:55 +0000289 bits += (7 - discard);
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000290 cont = c & 0x80;
Fred Drake8c081a12001-10-12 20:57:55 +0000291 discard = 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000292 } while (cont);
293
Fred Drake8c081a12001-10-12 20:57:55 +0000294 *pvalue = accum;
295
296 return 0;
297}
298
299/* Unpack a string, which is encoded as a packed integer giving the
300 * length of the string, followed by the string data.
301 */
302static int
303unpack_string(LogReaderObject *self, PyObject **pvalue)
304{
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000305 int i;
Fred Drake8c081a12001-10-12 20:57:55 +0000306 int len;
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000307 int err;
Guido van Rossum0b624f62002-07-20 00:38:01 +0000308 int ch;
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000309 char *buf;
310
311 if ((err = unpack_packed_int(self, &len, 0)))
312 return err;
Fred Drake8c081a12001-10-12 20:57:55 +0000313
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000314 buf = malloc(len);
315 for (i=0; i < len; i++) {
Guido van Rossum0b624f62002-07-20 00:38:01 +0000316 ch = fgetc(self->logfp);
317 buf[i] = ch;
318 if (ch == EOF) {
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000319 free(buf);
320 return ERR_EOF;
Fred Drake8c081a12001-10-12 20:57:55 +0000321 }
322 }
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000323 *pvalue = PyString_FromStringAndSize(buf, len);
324 free(buf);
325 if (*pvalue == NULL) {
326 return ERR_EXCEPTION;
327 }
328 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000329}
330
331
Fred Drake4c2e1af2001-10-29 20:45:57 +0000332static int
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000333unpack_add_info(LogReaderObject *self)
Fred Drake4c2e1af2001-10-29 20:45:57 +0000334{
335 PyObject *key;
336 PyObject *value = NULL;
337 int err;
338
Fred Drake4c2e1af2001-10-29 20:45:57 +0000339 err = unpack_string(self, &key);
340 if (!err) {
341 err = unpack_string(self, &value);
342 if (err)
343 Py_DECREF(key);
344 else {
345 PyObject *list = PyDict_GetItem(self->info, key);
346 if (list == NULL) {
347 list = PyList_New(0);
348 if (list == NULL) {
349 err = ERR_EXCEPTION;
350 goto finally;
351 }
352 if (PyDict_SetItem(self->info, key, list)) {
353 err = ERR_EXCEPTION;
354 goto finally;
355 }
356 }
357 if (PyList_Append(list, value))
358 err = ERR_EXCEPTION;
359 }
360 }
361 finally:
362 Py_XDECREF(key);
363 Py_XDECREF(value);
364 return err;
365}
366
367
368static void
Fred Drake666bf522002-07-18 19:11:44 +0000369eof_error(LogReaderObject *self)
Fred Drake4c2e1af2001-10-29 20:45:57 +0000370{
Fred Drake666bf522002-07-18 19:11:44 +0000371 fclose(self->logfp);
372 self->logfp = NULL;
Fred Drake4c2e1af2001-10-29 20:45:57 +0000373 PyErr_SetString(PyExc_EOFError,
374 "end of file with incomplete profile record");
375}
376
Fred Drake8c081a12001-10-12 20:57:55 +0000377static PyObject *
378logreader_tp_iternext(LogReaderObject *self)
379{
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000380 int c;
381 int what;
Fred Drake8c081a12001-10-12 20:57:55 +0000382 int err = ERR_NONE;
383 int lineno = -1;
384 int fileno = -1;
385 int tdelta = -1;
386 PyObject *s1 = NULL, *s2 = NULL;
387 PyObject *result = NULL;
388#if 0
389 unsigned char b0, b1;
390#endif
391
392 if (self->logfp == NULL) {
393 PyErr_SetString(ProfilerError,
394 "cannot iterate over closed LogReader object");
395 return NULL;
396 }
Fred Drake4c2e1af2001-10-29 20:45:57 +0000397
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000398restart:
399 /* decode the record type */
Fred Drake666bf522002-07-18 19:11:44 +0000400 if ((c = fgetc(self->logfp)) == EOF) {
401 fclose(self->logfp);
402 self->logfp = NULL;
Fred Drake8c081a12001-10-12 20:57:55 +0000403 return NULL;
Fred Drake666bf522002-07-18 19:11:44 +0000404 }
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000405 what = c & WHAT_OTHER;
406 if (what == WHAT_OTHER)
407 what = c; /* need all the bits for type */
408 else
409 ungetc(c, self->logfp); /* type byte includes packed int */
Fred Drake8c081a12001-10-12 20:57:55 +0000410
Fred Drake8c081a12001-10-12 20:57:55 +0000411 switch (what) {
412 case WHAT_ENTER:
413 err = unpack_packed_int(self, &fileno, 2);
414 if (!err) {
Fred Drake30d1c752001-10-15 22:11:02 +0000415 err = unpack_packed_int(self, &lineno, 0);
416 if (self->frametimings && !err)
417 err = unpack_packed_int(self, &tdelta, 0);
Fred Drake8c081a12001-10-12 20:57:55 +0000418 }
419 break;
420 case WHAT_EXIT:
421 err = unpack_packed_int(self, &tdelta, 2);
422 break;
423 case WHAT_LINENO:
424 err = unpack_packed_int(self, &lineno, 2);
425 if (self->linetimings && !err)
426 err = unpack_packed_int(self, &tdelta, 0);
427 break;
428 case WHAT_ADD_INFO:
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000429 err = unpack_add_info(self);
Fred Drake8c081a12001-10-12 20:57:55 +0000430 break;
431 case WHAT_DEFINE_FILE:
432 err = unpack_packed_int(self, &fileno, 0);
433 if (!err) {
434 err = unpack_string(self, &s1);
435 if (!err) {
436 Py_INCREF(Py_None);
437 s2 = Py_None;
438 }
439 }
440 break;
Fred Drake30d1c752001-10-15 22:11:02 +0000441 case WHAT_DEFINE_FUNC:
442 err = unpack_packed_int(self, &fileno, 0);
443 if (!err) {
444 err = unpack_packed_int(self, &lineno, 0);
445 if (!err)
446 err = unpack_string(self, &s1);
447 }
448 break;
Fred Drake8c081a12001-10-12 20:57:55 +0000449 case WHAT_LINE_TIMES:
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000450 if ((c = fgetc(self->logfp)) == EOF)
Fred Drake8c081a12001-10-12 20:57:55 +0000451 err = ERR_EOF;
452 else {
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000453 self->linetimings = c ? 1 : 0;
454 goto restart;
455 }
Fred Drake4c2e1af2001-10-29 20:45:57 +0000456 break;
Fred Drake30d1c752001-10-15 22:11:02 +0000457 case WHAT_FRAME_TIMES:
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000458 if ((c = fgetc(self->logfp)) == EOF)
Fred Drake30d1c752001-10-15 22:11:02 +0000459 err = ERR_EOF;
460 else {
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000461 self->frametimings = c ? 1 : 0;
462 goto restart;
463 }
Fred Drake4c2e1af2001-10-29 20:45:57 +0000464 break;
Fred Drake8c081a12001-10-12 20:57:55 +0000465 default:
Fred Drake4c2e1af2001-10-29 20:45:57 +0000466 err = ERR_BAD_RECTYPE;
Fred Drake8c081a12001-10-12 20:57:55 +0000467 }
Fred Drake4c2e1af2001-10-29 20:45:57 +0000468 if (err == ERR_BAD_RECTYPE) {
469 PyErr_SetString(PyExc_ValueError,
470 "unknown record type in log file");
471 }
472 else if (err == ERR_EOF) {
Fred Drake666bf522002-07-18 19:11:44 +0000473 eof_error(self);
Fred Drake8c081a12001-10-12 20:57:55 +0000474 }
475 else if (!err) {
476 result = PyTuple_New(4);
477 PyTuple_SET_ITEM(result, 0, PyInt_FromLong(what));
478 PyTuple_SET_ITEM(result, 2, PyInt_FromLong(fileno));
Fred Drake30d1c752001-10-15 22:11:02 +0000479 if (s1 == NULL)
Fred Drake8c081a12001-10-12 20:57:55 +0000480 PyTuple_SET_ITEM(result, 1, PyInt_FromLong(tdelta));
Fred Drake30d1c752001-10-15 22:11:02 +0000481 else
Fred Drake8c081a12001-10-12 20:57:55 +0000482 PyTuple_SET_ITEM(result, 1, s1);
Fred Drake30d1c752001-10-15 22:11:02 +0000483 if (s2 == NULL)
484 PyTuple_SET_ITEM(result, 3, PyInt_FromLong(lineno));
485 else
Fred Drake8c081a12001-10-12 20:57:55 +0000486 PyTuple_SET_ITEM(result, 3, s2);
Fred Drake8c081a12001-10-12 20:57:55 +0000487 }
488 /* The only other case is err == ERR_EXCEPTION, in which case the
489 * exception is already set.
490 */
491#if 0
492 b0 = self->buffer[self->index];
493 b1 = self->buffer[self->index + 1];
494 if (b0 & 1) {
495 /* This is a line-number event. */
496 what = PyTrace_LINE;
497 lineno = ((b0 & ~1) << 7) + b1;
498 self->index += 2;
499 }
500 else {
501 what = (b0 & 0x0E) >> 1;
502 tdelta = ((b0 & 0xF0) << 4) + b1;
503 if (what == PyTrace_CALL) {
504 /* we know there's a 2-byte file ID & 2-byte line number */
505 fileno = ((self->buffer[self->index + 2] << 8)
506 + self->buffer[self->index + 3]);
507 lineno = ((self->buffer[self->index + 4] << 8)
508 + self->buffer[self->index + 5]);
509 self->index += 6;
510 }
511 else
512 self->index += 2;
513 }
514#endif
515 return result;
516}
517
518static void
519logreader_dealloc(LogReaderObject *self)
520{
521 if (self->logfp != NULL) {
522 fclose(self->logfp);
523 self->logfp = NULL;
524 }
525 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,
801 PyString_AS_STRING(fcode->co_name)) < 0)
802 return -1;
Fred Drake30d1c752001-10-15 22:11:02 +0000803 if (PyDict_SetItem(dict, obj, fcode->co_name))
804 return -1;
805 }
Fred Drake8c081a12001-10-12 20:57:55 +0000806 }
807 return fileno;
808}
809
810static inline int
811get_tdelta(ProfilerObject *self)
812{
813 int tdelta;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000814#ifdef MS_WINDOWS
Fred Drake8c081a12001-10-12 20:57:55 +0000815 hs_time tv;
Tim Peters7d99ff22001-10-13 07:37:52 +0000816 hs_time diff;
Fred Drake8c081a12001-10-12 20:57:55 +0000817
Tim Peters7d99ff22001-10-13 07:37:52 +0000818 GETTIMEOFDAY(&tv);
819 diff = tv - self->prev_timeofday;
820 tdelta = (int)diff;
Fred Drake8c081a12001-10-12 20:57:55 +0000821#else
822 struct timeval tv;
823
824 GETTIMEOFDAY(&tv);
825
826 if (tv.tv_sec == self->prev_timeofday.tv_sec)
827 tdelta = tv.tv_usec - self->prev_timeofday.tv_usec;
828 else
829 tdelta = ((tv.tv_sec - self->prev_timeofday.tv_sec) * 1000000
830 + tv.tv_usec);
831#endif
832 self->prev_timeofday = tv;
833 return tdelta;
834}
835
836
837/* The workhorse: the profiler callback function. */
838
839static int
840profiler_callback(ProfilerObject *self, PyFrameObject *frame, int what,
841 PyObject *arg)
842{
Fred Drake30d1c752001-10-15 22:11:02 +0000843 int tdelta = -1;
Fred Drake8c081a12001-10-12 20:57:55 +0000844 int fileno;
845
Fred Drake30d1c752001-10-15 22:11:02 +0000846 if (self->frametimings)
847 tdelta = get_tdelta(self);
Fred Drake8c081a12001-10-12 20:57:55 +0000848 switch (what) {
849 case PyTrace_CALL:
850 fileno = get_fileno(self, frame->f_code);
851 if (fileno < 0)
852 return -1;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000853 if (pack_enter(self, fileno, tdelta,
854 frame->f_code->co_firstlineno) < 0)
855 return -1;
Fred Drake8c081a12001-10-12 20:57:55 +0000856 break;
857 case PyTrace_RETURN:
Fred Drake62c1e3c2001-12-04 21:40:53 +0000858 if (pack_exit(self, tdelta) < 0)
859 return -1;
Fred Drake8c081a12001-10-12 20:57:55 +0000860 break;
861 default:
862 /* should never get here */
863 break;
864 }
865 return 0;
866}
867
868
869/* Alternate callback when we want PyTrace_LINE events */
870
871static int
872tracer_callback(ProfilerObject *self, PyFrameObject *frame, int what,
873 PyObject *arg)
874{
875 int fileno;
876
Fred Drake8c081a12001-10-12 20:57:55 +0000877 switch (what) {
878 case PyTrace_CALL:
879 fileno = get_fileno(self, frame->f_code);
880 if (fileno < 0)
881 return -1;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000882 return pack_enter(self, fileno,
883 self->frametimings ? get_tdelta(self) : -1,
884 frame->f_code->co_firstlineno);
885
Fred Drake8c081a12001-10-12 20:57:55 +0000886 case PyTrace_RETURN:
Fred Drake62c1e3c2001-12-04 21:40:53 +0000887 return pack_exit(self, get_tdelta(self));
888
Tim Peters1566a172001-10-12 22:08:39 +0000889 case PyTrace_LINE:
Fred Drake8c081a12001-10-12 20:57:55 +0000890 if (self->linetimings)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000891 return pack_lineno_tdelta(self, PyCode_Addr2Line(frame->f_code,
892 frame->f_lasti),
893 get_tdelta(self));
Fred Drake8c081a12001-10-12 20:57:55 +0000894 else
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000895 return pack_lineno(self, PyCode_Addr2Line(frame->f_code,
896 frame->f_lasti));
Fred Drake62c1e3c2001-12-04 21:40:53 +0000897
Fred Drake8c081a12001-10-12 20:57:55 +0000898 default:
899 /* ignore PyTrace_EXCEPTION */
900 break;
901 }
902 return 0;
903}
904
905
906/* A couple of useful helper functions. */
907
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000908#ifdef MS_WINDOWS
Tim Petersfeab23f2001-10-13 00:11:10 +0000909static LARGE_INTEGER frequency = {0, 0};
Fred Drake8c081a12001-10-12 20:57:55 +0000910#endif
911
912static unsigned long timeofday_diff = 0;
913static unsigned long rusage_diff = 0;
914
915static void
916calibrate(void)
917{
918 hs_time tv1, tv2;
919
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000920#ifdef MS_WINDOWS
Tim Peters7d99ff22001-10-13 07:37:52 +0000921 hs_time diff;
Fred Drake8c081a12001-10-12 20:57:55 +0000922 QueryPerformanceFrequency(&frequency);
923#endif
924
925 GETTIMEOFDAY(&tv1);
926 while (1) {
927 GETTIMEOFDAY(&tv2);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000928#ifdef MS_WINDOWS
Tim Peters7d99ff22001-10-13 07:37:52 +0000929 diff = tv2 - tv1;
930 if (diff != 0) {
931 timeofday_diff = (unsigned long)diff;
Fred Drake8c081a12001-10-12 20:57:55 +0000932 break;
933 }
934#else
935 if (tv1.tv_sec != tv2.tv_sec || tv1.tv_usec != tv2.tv_usec) {
936 if (tv1.tv_sec == tv2.tv_sec)
937 timeofday_diff = tv2.tv_usec - tv1.tv_usec;
938 else
939 timeofday_diff = (1000000 - tv1.tv_usec) + tv2.tv_usec;
940 break;
941 }
942#endif
943 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000944#if defined(MS_WINDOWS) || defined(macintosh) || defined(PYOS_OS2)
Fred Drake8c081a12001-10-12 20:57:55 +0000945 rusage_diff = -1;
946#else
947 {
948 struct rusage ru1, ru2;
949
950 getrusage(RUSAGE_SELF, &ru1);
951 while (1) {
952 getrusage(RUSAGE_SELF, &ru2);
953 if (ru1.ru_utime.tv_sec != ru2.ru_utime.tv_sec) {
954 rusage_diff = ((1000000 - ru1.ru_utime.tv_usec)
955 + ru2.ru_utime.tv_usec);
956 break;
957 }
958 else if (ru1.ru_utime.tv_usec != ru2.ru_utime.tv_usec) {
959 rusage_diff = ru2.ru_utime.tv_usec - ru1.ru_utime.tv_usec;
960 break;
961 }
962 else if (ru1.ru_stime.tv_sec != ru2.ru_stime.tv_sec) {
963 rusage_diff = ((1000000 - ru1.ru_stime.tv_usec)
964 + ru2.ru_stime.tv_usec);
965 break;
966 }
967 else if (ru1.ru_stime.tv_usec != ru2.ru_stime.tv_usec) {
968 rusage_diff = ru2.ru_stime.tv_usec - ru1.ru_stime.tv_usec;
969 break;
970 }
971 }
972 }
973#endif
974}
975
976static void
977do_start(ProfilerObject *self)
978{
979 self->active = 1;
980 GETTIMEOFDAY(&self->prev_timeofday);
981 if (self->lineevents)
982 PyEval_SetTrace((Py_tracefunc) tracer_callback, (PyObject *)self);
983 else
984 PyEval_SetProfile((Py_tracefunc) profiler_callback, (PyObject *)self);
985}
986
987static void
988do_stop(ProfilerObject *self)
989{
990 if (self->active) {
991 self->active = 0;
992 if (self->lineevents)
993 PyEval_SetTrace(NULL, NULL);
994 else
995 PyEval_SetProfile(NULL, NULL);
996 }
997 if (self->index > 0) {
998 /* Best effort to dump out any remaining data. */
999 flush_data(self);
1000 }
1001}
1002
1003static int
1004is_available(ProfilerObject *self)
1005{
1006 if (self->active) {
1007 PyErr_SetString(ProfilerError, "profiler already active");
1008 return 0;
1009 }
1010 if (self->logfp == NULL) {
1011 PyErr_SetString(ProfilerError, "profiler already closed");
1012 return 0;
1013 }
1014 return 1;
1015}
1016
1017
1018/* Profiler object interface methods. */
1019
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001020PyDoc_STRVAR(addinfo__doc__,
Fred Drake4c2e1af2001-10-29 20:45:57 +00001021"addinfo(key, value)\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001022"Insert an ADD_INFO record into the log.");
Fred Drake4c2e1af2001-10-29 20:45:57 +00001023
1024static PyObject *
1025profiler_addinfo(ProfilerObject *self, PyObject *args)
1026{
1027 PyObject *result = NULL;
1028 char *key, *value;
1029
1030 if (PyArg_ParseTuple(args, "ss:addinfo", &key, &value)) {
1031 if (self->logfp == NULL)
1032 PyErr_SetString(ProfilerError, "profiler already closed");
1033 else {
Fred Drake62c1e3c2001-12-04 21:40:53 +00001034 if (pack_add_info(self, key, value) == 0) {
1035 result = Py_None;
1036 Py_INCREF(result);
1037 }
Fred Drake4c2e1af2001-10-29 20:45:57 +00001038 }
1039 }
1040 return result;
1041}
1042
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001043PyDoc_STRVAR(close__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +00001044"close()\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001045"Shut down this profiler and close the log files, even if its active.");
Fred Drake8c081a12001-10-12 20:57:55 +00001046
1047static PyObject *
Fred Drake666bf522002-07-18 19:11:44 +00001048profiler_close(ProfilerObject *self)
Fred Drake8c081a12001-10-12 20:57:55 +00001049{
Fred Drake666bf522002-07-18 19:11:44 +00001050 do_stop(self);
1051 if (self->logfp != NULL) {
1052 fclose(self->logfp);
1053 self->logfp = NULL;
Fred Drake8c081a12001-10-12 20:57:55 +00001054 }
Fred Drake666bf522002-07-18 19:11:44 +00001055 Py_INCREF(Py_None);
1056 return Py_None;
1057}
1058
1059#define fileno__doc__ logreader_fileno__doc__
1060
1061static PyObject *
1062profiler_fileno(ProfilerObject *self)
1063{
1064 if (self->logfp == NULL) {
1065 PyErr_SetString(PyExc_ValueError,
1066 "profiler's file object already closed");
1067 return NULL;
1068 }
1069 return PyInt_FromLong(fileno(self->logfp));
Fred Drake8c081a12001-10-12 20:57:55 +00001070}
1071
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001072PyDoc_STRVAR(runcall__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +00001073"runcall(callable[, args[, kw]]) -> callable()\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001074"Profile a specific function call, returning the result of that call.");
Fred Drake8c081a12001-10-12 20:57:55 +00001075
1076static PyObject *
1077profiler_runcall(ProfilerObject *self, PyObject *args)
1078{
1079 PyObject *result = NULL;
1080 PyObject *callargs = NULL;
1081 PyObject *callkw = NULL;
1082 PyObject *callable;
1083
1084 if (PyArg_ParseTuple(args, "O|OO:runcall",
1085 &callable, &callargs, &callkw)) {
1086 if (is_available(self)) {
1087 do_start(self);
1088 result = PyEval_CallObjectWithKeywords(callable, callargs, callkw);
1089 do_stop(self);
1090 }
1091 }
1092 return result;
1093}
1094
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001095PyDoc_STRVAR(runcode__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +00001096"runcode(code, globals[, locals])\n"
1097"Execute a code object while collecting profile data. If locals is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001098"omitted, globals is used for the locals as well.");
Fred Drake8c081a12001-10-12 20:57:55 +00001099
1100static PyObject *
1101profiler_runcode(ProfilerObject *self, PyObject *args)
1102{
1103 PyObject *result = NULL;
1104 PyCodeObject *code;
1105 PyObject *globals;
1106 PyObject *locals = NULL;
1107
1108 if (PyArg_ParseTuple(args, "O!O!|O:runcode",
1109 &PyCode_Type, &code,
1110 &PyDict_Type, &globals,
1111 &locals)) {
1112 if (is_available(self)) {
1113 if (locals == NULL || locals == Py_None)
1114 locals = globals;
1115 else if (!PyDict_Check(locals)) {
1116 PyErr_SetString(PyExc_TypeError,
1117 "locals must be a dictionary or None");
1118 return NULL;
1119 }
1120 do_start(self);
1121 result = PyEval_EvalCode(code, globals, locals);
1122 do_stop(self);
1123#if 0
1124 if (!PyErr_Occurred()) {
1125 result = Py_None;
1126 Py_INCREF(result);
1127 }
1128#endif
1129 }
1130 }
1131 return result;
1132}
1133
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001134PyDoc_STRVAR(start__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +00001135"start()\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001136"Install this profiler for the current thread.");
Fred Drake8c081a12001-10-12 20:57:55 +00001137
1138static PyObject *
1139profiler_start(ProfilerObject *self, PyObject *args)
1140{
1141 PyObject *result = NULL;
1142
Fred Drake666bf522002-07-18 19:11:44 +00001143 if (is_available(self)) {
1144 do_start(self);
1145 result = Py_None;
1146 Py_INCREF(result);
Fred Drake8c081a12001-10-12 20:57:55 +00001147 }
1148 return result;
1149}
1150
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001151PyDoc_STRVAR(stop__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +00001152"stop()\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001153"Remove this profiler from the current thread.");
Fred Drake8c081a12001-10-12 20:57:55 +00001154
1155static PyObject *
1156profiler_stop(ProfilerObject *self, PyObject *args)
1157{
1158 PyObject *result = NULL;
1159
Fred Drake666bf522002-07-18 19:11:44 +00001160 if (!self->active)
1161 PyErr_SetString(ProfilerError, "profiler not active");
1162 else {
1163 do_stop(self);
1164 result = Py_None;
1165 Py_INCREF(result);
Fred Drake8c081a12001-10-12 20:57:55 +00001166 }
1167 return result;
1168}
1169
1170
1171/* Python API support. */
1172
1173static void
1174profiler_dealloc(ProfilerObject *self)
1175{
1176 do_stop(self);
1177 if (self->logfp != NULL)
1178 fclose(self->logfp);
1179 Py_XDECREF(self->filemap);
1180 Py_XDECREF(self->logfilename);
1181 PyObject_Del((PyObject *)self);
1182}
1183
Fred Drake8c081a12001-10-12 20:57:55 +00001184static PyMethodDef profiler_methods[] = {
Fred Drake4c2e1af2001-10-29 20:45:57 +00001185 {"addinfo", (PyCFunction)profiler_addinfo, METH_VARARGS, addinfo__doc__},
Fred Drake666bf522002-07-18 19:11:44 +00001186 {"close", (PyCFunction)profiler_close, METH_NOARGS, close__doc__},
1187 {"fileno", (PyCFunction)profiler_fileno, METH_NOARGS, fileno__doc__},
Fred Drake8c081a12001-10-12 20:57:55 +00001188 {"runcall", (PyCFunction)profiler_runcall, METH_VARARGS, runcall__doc__},
1189 {"runcode", (PyCFunction)profiler_runcode, METH_VARARGS, runcode__doc__},
Fred Drake666bf522002-07-18 19:11:44 +00001190 {"start", (PyCFunction)profiler_start, METH_NOARGS, start__doc__},
1191 {"stop", (PyCFunction)profiler_stop, METH_NOARGS, stop__doc__},
Fred Drake8c081a12001-10-12 20:57:55 +00001192 {NULL, NULL}
1193};
1194
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001195static PyMemberDef profiler_members[] = {
Fred Drake30d1c752001-10-15 22:11:02 +00001196 {"frametimings", T_LONG, offsetof(ProfilerObject, linetimings), READONLY},
1197 {"lineevents", T_LONG, offsetof(ProfilerObject, lineevents), READONLY},
1198 {"linetimings", T_LONG, offsetof(ProfilerObject, linetimings), READONLY},
Fred Drake8c081a12001-10-12 20:57:55 +00001199 {NULL}
1200};
1201
1202static PyObject *
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001203profiler_get_closed(ProfilerObject *self, void *closure)
Fred Drake8c081a12001-10-12 20:57:55 +00001204{
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001205 PyObject *result = (self->logfp == NULL) ? Py_True : Py_False;
1206 Py_INCREF(result);
Fred Drake8c081a12001-10-12 20:57:55 +00001207 return result;
1208}
1209
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001210static PyGetSetDef profiler_getsets[] = {
Fred Draked1eb8b62002-07-17 18:54:20 +00001211 {"closed", (getter)profiler_get_closed, NULL,
Fred Drake5c3ed3d2002-07-17 19:38:05 +00001212 PyDoc_STR("True if the profiler's output file has already been closed.")},
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001213 {NULL}
1214};
1215
Fred Drake8c081a12001-10-12 20:57:55 +00001216
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001217PyDoc_STRVAR(profiler_object__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +00001218"High-performance profiler object.\n"
1219"\n"
1220"Methods:\n"
1221"\n"
Fred Drake30d1c752001-10-15 22:11:02 +00001222"close(): Stop the profiler and close the log files.\n"
Fred Drake666bf522002-07-18 19:11:44 +00001223"fileno(): Returns the file descriptor of the log file.\n"
Fred Drake30d1c752001-10-15 22:11:02 +00001224"runcall(): Run a single function call with profiling enabled.\n"
1225"runcode(): Execute a code object with profiling enabled.\n"
1226"start(): Install the profiler and return.\n"
1227"stop(): Remove the profiler.\n"
Tim Petersfeab23f2001-10-13 00:11:10 +00001228"\n"
1229"Attributes (read-only):\n"
1230"\n"
Fred Drake30d1c752001-10-15 22:11:02 +00001231"closed: True if the profiler has already been closed.\n"
1232"frametimings: True if ENTER/EXIT events collect timing information.\n"
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001233"lineevents: True if line events are reported to the profiler.\n"
1234"linetimings: True if line events collect timing information.");
Fred Drake8c081a12001-10-12 20:57:55 +00001235
1236static PyTypeObject ProfilerType = {
1237 PyObject_HEAD_INIT(NULL)
1238 0, /* ob_size */
Fred Drake4c2e1af2001-10-29 20:45:57 +00001239 "_hotshot.ProfilerType", /* tp_name */
Fred Drake8c081a12001-10-12 20:57:55 +00001240 (int) sizeof(ProfilerObject), /* tp_basicsize */
1241 0, /* tp_itemsize */
1242 (destructor)profiler_dealloc, /* tp_dealloc */
1243 0, /* tp_print */
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001244 0, /* tp_getattr */
Fred Drake8c081a12001-10-12 20:57:55 +00001245 0, /* tp_setattr */
1246 0, /* tp_compare */
1247 0, /* tp_repr */
1248 0, /* tp_as_number */
1249 0, /* tp_as_sequence */
1250 0, /* tp_as_mapping */
1251 0, /* tp_hash */
1252 0, /* tp_call */
1253 0, /* tp_str */
Jason Tishler4df78cd2002-08-08 19:46:15 +00001254 0, /* tp_getattro */
Fred Drake8c081a12001-10-12 20:57:55 +00001255 0, /* tp_setattro */
1256 0, /* tp_as_buffer */
Fred Drake4c2e1af2001-10-29 20:45:57 +00001257 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake8c081a12001-10-12 20:57:55 +00001258 profiler_object__doc__, /* tp_doc */
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001259 0, /* tp_traverse */
1260 0, /* tp_clear */
1261 0, /* tp_richcompare */
1262 0, /* tp_weaklistoffset */
1263 0, /* tp_iter */
1264 0, /* tp_iternext */
1265 profiler_methods, /* tp_methods */
1266 profiler_members, /* tp_members */
1267 profiler_getsets, /* tp_getset */
1268 0, /* tp_base */
1269 0, /* tp_dict */
1270 0, /* tp_descr_get */
1271 0, /* tp_descr_set */
Fred Drake8c081a12001-10-12 20:57:55 +00001272};
1273
1274
1275static PyMethodDef logreader_methods[] = {
Fred Drake666bf522002-07-18 19:11:44 +00001276 {"close", (PyCFunction)logreader_close, METH_NOARGS,
Fred Drake8c081a12001-10-12 20:57:55 +00001277 logreader_close__doc__},
Fred Drake666bf522002-07-18 19:11:44 +00001278 {"fileno", (PyCFunction)logreader_fileno, METH_NOARGS,
1279 logreader_fileno__doc__},
Fred Drake8c081a12001-10-12 20:57:55 +00001280 {NULL, NULL}
1281};
1282
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001283static PyMemberDef logreader_members[] = {
Fred Drake5c3ed3d2002-07-17 19:38:05 +00001284 {"info", T_OBJECT, offsetof(LogReaderObject, info), RO,
1285 PyDoc_STR("Dictionary mapping informational keys to lists of values.")},
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001286 {NULL}
1287};
Fred Drake8c081a12001-10-12 20:57:55 +00001288
1289
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001290PyDoc_STRVAR(logreader__doc__,
1291"logreader(filename) --> log-iterator\n\
1292Create a log-reader for the timing information file.");
Fred Drake8c081a12001-10-12 20:57:55 +00001293
1294static PySequenceMethods logreader_as_sequence = {
1295 0, /* sq_length */
1296 0, /* sq_concat */
1297 0, /* sq_repeat */
1298 (intargfunc)logreader_sq_item, /* sq_item */
1299 0, /* sq_slice */
1300 0, /* sq_ass_item */
1301 0, /* sq_ass_slice */
1302 0, /* sq_contains */
1303 0, /* sq_inplace_concat */
1304 0, /* sq_inplace_repeat */
1305};
1306
Fred Drake666bf522002-07-18 19:11:44 +00001307static PyObject *
1308logreader_get_closed(LogReaderObject *self, void *closure)
1309{
1310 PyObject *result = (self->logfp == NULL) ? Py_True : Py_False;
1311 Py_INCREF(result);
1312 return result;
1313}
1314
1315static PyGetSetDef logreader_getsets[] = {
1316 {"closed", (getter)logreader_get_closed, NULL,
1317 PyDoc_STR("True if the logreader's input file has already been closed.")},
1318 {NULL}
1319};
1320
Fred Drake8c081a12001-10-12 20:57:55 +00001321static PyTypeObject LogReaderType = {
1322 PyObject_HEAD_INIT(NULL)
1323 0, /* ob_size */
Fred Drake4c2e1af2001-10-29 20:45:57 +00001324 "_hotshot.LogReaderType", /* tp_name */
Fred Drake8c081a12001-10-12 20:57:55 +00001325 (int) sizeof(LogReaderObject), /* tp_basicsize */
1326 0, /* tp_itemsize */
1327 (destructor)logreader_dealloc, /* tp_dealloc */
1328 0, /* tp_print */
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001329 0, /* tp_getattr */
Fred Drake8c081a12001-10-12 20:57:55 +00001330 0, /* tp_setattr */
1331 0, /* tp_compare */
1332 0, /* tp_repr */
1333 0, /* tp_as_number */
1334 &logreader_as_sequence, /* tp_as_sequence */
1335 0, /* tp_as_mapping */
1336 0, /* tp_hash */
1337 0, /* tp_call */
1338 0, /* tp_str */
Jason Tishler4df78cd2002-08-08 19:46:15 +00001339 0, /* tp_getattro */
Fred Drake8c081a12001-10-12 20:57:55 +00001340 0, /* tp_setattro */
1341 0, /* tp_as_buffer */
Fred Drake4c2e1af2001-10-29 20:45:57 +00001342 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake8c081a12001-10-12 20:57:55 +00001343 logreader__doc__, /* tp_doc */
Fred Drake8c081a12001-10-12 20:57:55 +00001344 0, /* tp_traverse */
1345 0, /* tp_clear */
1346 0, /* tp_richcompare */
1347 0, /* tp_weaklistoffset */
1348 (getiterfunc)logreader_tp_iter, /* tp_iter */
1349 (iternextfunc)logreader_tp_iternext,/* tp_iternext */
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001350 logreader_methods, /* tp_methods */
1351 logreader_members, /* tp_members */
Fred Drake666bf522002-07-18 19:11:44 +00001352 logreader_getsets, /* tp_getset */
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001353 0, /* tp_base */
1354 0, /* tp_dict */
1355 0, /* tp_descr_get */
1356 0, /* tp_descr_set */
Fred Drake8c081a12001-10-12 20:57:55 +00001357};
1358
1359static PyObject *
1360hotshot_logreader(PyObject *unused, PyObject *args)
1361{
1362 LogReaderObject *self = NULL;
1363 char *filename;
Neil Schemenauer8b6b4912002-05-29 18:19:14 +00001364 int c;
1365 int err = 0;
Fred Drake8c081a12001-10-12 20:57:55 +00001366
1367 if (PyArg_ParseTuple(args, "s:logreader", &filename)) {
1368 self = PyObject_New(LogReaderObject, &LogReaderType);
1369 if (self != NULL) {
Fred Drake30d1c752001-10-15 22:11:02 +00001370 self->frametimings = 1;
Fred Drake8c081a12001-10-12 20:57:55 +00001371 self->linetimings = 0;
Fred Drake4c2e1af2001-10-29 20:45:57 +00001372 self->info = NULL;
Fred Drake8c081a12001-10-12 20:57:55 +00001373 self->logfp = fopen(filename, "rb");
1374 if (self->logfp == NULL) {
1375 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
1376 Py_DECREF(self);
1377 self = NULL;
Fred Drake4c2e1af2001-10-29 20:45:57 +00001378 goto finally;
1379 }
1380 self->info = PyDict_New();
1381 if (self->info == NULL) {
1382 Py_DECREF(self);
1383 goto finally;
1384 }
Neil Schemenauer8b6b4912002-05-29 18:19:14 +00001385 /* read initial info */
1386 for (;;) {
1387 if ((c = fgetc(self->logfp)) == EOF) {
Fred Drake666bf522002-07-18 19:11:44 +00001388 eof_error(self);
Neil Schemenauer8b6b4912002-05-29 18:19:14 +00001389 break;
1390 }
1391 if (c != WHAT_ADD_INFO) {
1392 ungetc(c, self->logfp);
1393 break;
1394 }
1395 err = unpack_add_info(self);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001396 if (err) {
1397 if (err == ERR_EOF)
Fred Drake666bf522002-07-18 19:11:44 +00001398 eof_error(self);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001399 else
1400 PyErr_SetString(PyExc_RuntimeError,
1401 "unexpected error");
1402 break;
1403 }
Fred Drake8c081a12001-10-12 20:57:55 +00001404 }
1405 }
1406 }
Fred Drake4c2e1af2001-10-29 20:45:57 +00001407 finally:
Fred Drake8c081a12001-10-12 20:57:55 +00001408 return (PyObject *) self;
1409}
1410
1411
1412/* Return a Python string that represents the version number without the
1413 * extra cruft added by revision control, even if the right options were
1414 * given to the "cvs export" command to make it not include the extra
1415 * cruft.
1416 */
1417static char *
1418get_version_string(void)
1419{
1420 static char *rcsid = "$Revision$";
1421 char *rev = rcsid;
1422 char *buffer;
1423 int i = 0;
1424
Neal Norwitz3afb2d22002-03-20 21:32:07 +00001425 while (*rev && !isdigit((int)*rev))
Fred Drake8c081a12001-10-12 20:57:55 +00001426 ++rev;
1427 while (rev[i] != ' ' && rev[i] != '\0')
1428 ++i;
1429 buffer = malloc(i + 1);
1430 if (buffer != NULL) {
1431 memmove(buffer, rev, i);
1432 buffer[i] = '\0';
1433 }
1434 return buffer;
1435}
1436
1437/* Write out a RFC 822-style header with various useful bits of
1438 * information to make the output easier to manage.
1439 */
1440static int
1441write_header(ProfilerObject *self)
1442{
1443 char *buffer;
1444 char cwdbuffer[PATH_MAX];
1445 PyObject *temp;
1446 int i, len;
1447
1448 buffer = get_version_string();
1449 if (buffer == NULL) {
1450 PyErr_NoMemory();
1451 return -1;
1452 }
Fred Drake4c2e1af2001-10-29 20:45:57 +00001453 pack_add_info(self, "hotshot-version", buffer);
1454 pack_add_info(self, "requested-frame-timings",
1455 (self->frametimings ? "yes" : "no"));
1456 pack_add_info(self, "requested-line-events",
Fred Drake8c081a12001-10-12 20:57:55 +00001457 (self->lineevents ? "yes" : "no"));
Fred Drake4c2e1af2001-10-29 20:45:57 +00001458 pack_add_info(self, "requested-line-timings",
1459 (self->linetimings ? "yes" : "no"));
1460 pack_add_info(self, "platform", Py_GetPlatform());
1461 pack_add_info(self, "executable", Py_GetProgramFullPath());
Fred Drakef12a68c2001-11-09 15:59:36 +00001462 free(buffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001463 buffer = (char *) Py_GetVersion();
1464 if (buffer == NULL)
1465 PyErr_Clear();
1466 else
Fred Drake4c2e1af2001-10-29 20:45:57 +00001467 pack_add_info(self, "executable-version", buffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001468
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001469#ifdef MS_WINDOWS
Tim Peters885d4572001-11-28 20:27:42 +00001470 PyOS_snprintf(cwdbuffer, sizeof(cwdbuffer), "%I64d", frequency.QuadPart);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001471 pack_add_info(self, "reported-performance-frequency", cwdbuffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001472#else
Tim Peters885d4572001-11-28 20:27:42 +00001473 PyOS_snprintf(cwdbuffer, sizeof(cwdbuffer), "%lu", rusage_diff);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001474 pack_add_info(self, "observed-interval-getrusage", cwdbuffer);
Tim Peters885d4572001-11-28 20:27:42 +00001475 PyOS_snprintf(cwdbuffer, sizeof(cwdbuffer), "%lu", timeofday_diff);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001476 pack_add_info(self, "observed-interval-gettimeofday", cwdbuffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001477#endif
Fred Drake8c081a12001-10-12 20:57:55 +00001478
Fred Drake4c2e1af2001-10-29 20:45:57 +00001479 pack_add_info(self, "current-directory",
Fred Drake8c081a12001-10-12 20:57:55 +00001480 getcwd(cwdbuffer, sizeof cwdbuffer));
1481
1482 temp = PySys_GetObject("path");
1483 len = PyList_GET_SIZE(temp);
1484 for (i = 0; i < len; ++i) {
1485 PyObject *item = PyList_GET_ITEM(temp, i);
1486 buffer = PyString_AsString(item);
Fred Draked1eb8b62002-07-17 18:54:20 +00001487 if (buffer == NULL) {
1488 pack_add_info(self, "sys-path-entry", "<non-string-path-entry>");
1489 PyErr_Clear();
1490 }
1491 else {
1492 pack_add_info(self, "sys-path-entry", buffer);
1493 }
Fred Drake8c081a12001-10-12 20:57:55 +00001494 }
Fred Drake4c2e1af2001-10-29 20:45:57 +00001495 pack_frame_times(self);
1496 pack_line_times(self);
1497
Fred Drake8c081a12001-10-12 20:57:55 +00001498 return 0;
1499}
1500
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001501PyDoc_STRVAR(profiler__doc__,
1502"profiler(logfilename[, lineevents[, linetimes]]) -> profiler\n\
1503Create a new profiler object.");
Fred Drake8c081a12001-10-12 20:57:55 +00001504
1505static PyObject *
1506hotshot_profiler(PyObject *unused, PyObject *args)
1507{
1508 char *logfilename;
1509 ProfilerObject *self = NULL;
1510 int lineevents = 0;
1511 int linetimings = 1;
1512
1513 if (PyArg_ParseTuple(args, "s|ii:profiler", &logfilename,
1514 &lineevents, &linetimings)) {
1515 self = PyObject_New(ProfilerObject, &ProfilerType);
1516 if (self == NULL)
1517 return NULL;
Fred Drake30d1c752001-10-15 22:11:02 +00001518 self->frametimings = 1;
Fred Drake8c081a12001-10-12 20:57:55 +00001519 self->lineevents = lineevents ? 1 : 0;
1520 self->linetimings = (lineevents && linetimings) ? 1 : 0;
1521 self->index = 0;
1522 self->active = 0;
1523 self->next_fileno = 0;
Tim Peters1566a172001-10-12 22:08:39 +00001524 self->logfp = NULL;
Fred Drake8c081a12001-10-12 20:57:55 +00001525 self->logfilename = PyTuple_GET_ITEM(args, 0);
1526 Py_INCREF(self->logfilename);
1527 self->filemap = PyDict_New();
1528 if (self->filemap == NULL) {
1529 Py_DECREF(self);
1530 return NULL;
1531 }
1532 self->logfp = fopen(logfilename, "wb");
1533 if (self->logfp == NULL) {
1534 Py_DECREF(self);
1535 PyErr_SetFromErrnoWithFilename(PyExc_IOError, logfilename);
1536 return NULL;
1537 }
1538 if (timeofday_diff == 0) {
1539 /* Run this several times since sometimes the first
1540 * doesn't give the lowest values, and we're really trying
1541 * to determine the lowest.
1542 */
1543 calibrate();
1544 calibrate();
1545 calibrate();
1546 }
1547 if (write_header(self))
1548 /* some error occurred, exception has been set */
1549 self = NULL;
1550 }
1551 return (PyObject *) self;
1552}
1553
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001554PyDoc_STRVAR(coverage__doc__,
1555"coverage(logfilename) -> profiler\n\
Fred Drake30d1c752001-10-15 22:11:02 +00001556Returns a profiler that doesn't collect any timing information, which is\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001557useful in building a coverage analysis tool.");
Fred Drake30d1c752001-10-15 22:11:02 +00001558
1559static PyObject *
1560hotshot_coverage(PyObject *unused, PyObject *args)
1561{
1562 char *logfilename;
1563 PyObject *result = NULL;
1564
1565 if (PyArg_ParseTuple(args, "s:coverage", &logfilename)) {
1566 result = hotshot_profiler(unused, args);
1567 if (result != NULL) {
1568 ProfilerObject *self = (ProfilerObject *) result;
1569 self->frametimings = 0;
1570 self->linetimings = 0;
1571 self->lineevents = 1;
1572 }
1573 }
1574 return result;
1575}
1576
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001577PyDoc_VAR(resolution__doc__) =
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001578#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001579PyDoc_STR(
Tim Petersfeab23f2001-10-13 00:11:10 +00001580"resolution() -> (performance-counter-ticks, update-frequency)\n"
1581"Return the resolution of the timer provided by the QueryPerformanceCounter()\n"
1582"function. The first value is the smallest observed change, and the second\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001583"is the result of QueryPerformanceFrequency()."
1584)
Fred Drake8c081a12001-10-12 20:57:55 +00001585#else
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001586PyDoc_STR(
Tim Petersfeab23f2001-10-13 00:11:10 +00001587"resolution() -> (gettimeofday-usecs, getrusage-usecs)\n"
1588"Return the resolution of the timers provided by the gettimeofday() and\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001589"getrusage() system calls, or -1 if the call is not supported."
1590)
Fred Drake8c081a12001-10-12 20:57:55 +00001591#endif
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001592;
Fred Drake8c081a12001-10-12 20:57:55 +00001593
1594static PyObject *
1595hotshot_resolution(PyObject *unused, PyObject *args)
1596{
1597 PyObject *result = NULL;
1598
1599 if (PyArg_ParseTuple(args, ":resolution")) {
1600 if (timeofday_diff == 0) {
1601 calibrate();
1602 calibrate();
1603 calibrate();
1604 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001605#ifdef MS_WINDOWS
Fred Drake8c081a12001-10-12 20:57:55 +00001606 result = Py_BuildValue("ii", timeofday_diff, frequency.LowPart);
1607#else
1608 result = Py_BuildValue("ii", timeofday_diff, rusage_diff);
1609#endif
1610 }
1611 return result;
1612}
1613
1614
1615static PyMethodDef functions[] = {
Fred Drake30d1c752001-10-15 22:11:02 +00001616 {"coverage", hotshot_coverage, METH_VARARGS, coverage__doc__},
Fred Drake8c081a12001-10-12 20:57:55 +00001617 {"profiler", hotshot_profiler, METH_VARARGS, profiler__doc__},
1618 {"logreader", hotshot_logreader, METH_VARARGS, logreader__doc__},
1619 {"resolution", hotshot_resolution, METH_VARARGS, resolution__doc__},
1620 {NULL, NULL}
1621};
1622
1623
1624void
1625init_hotshot(void)
1626{
1627 PyObject *module;
1628
1629 LogReaderType.ob_type = &PyType_Type;
Jason Tishler4df78cd2002-08-08 19:46:15 +00001630 LogReaderType.tp_getattro = PyObject_GenericGetAttr;
Fred Drake8c081a12001-10-12 20:57:55 +00001631 ProfilerType.ob_type = &PyType_Type;
Jason Tishler4df78cd2002-08-08 19:46:15 +00001632 ProfilerType.tp_getattro = PyObject_GenericGetAttr;
Fred Drake8c081a12001-10-12 20:57:55 +00001633 module = Py_InitModule("_hotshot", functions);
1634 if (module != NULL) {
1635 char *s = get_version_string();
1636
1637 PyModule_AddStringConstant(module, "__version__", s);
1638 free(s);
1639 Py_INCREF(&LogReaderType);
1640 PyModule_AddObject(module, "LogReaderType",
1641 (PyObject *)&LogReaderType);
1642 Py_INCREF(&ProfilerType);
1643 PyModule_AddObject(module, "ProfilerType",
1644 (PyObject *)&ProfilerType);
1645
1646 if (ProfilerError == NULL)
1647 ProfilerError = PyErr_NewException("hotshot.ProfilerError",
1648 NULL, NULL);
1649 if (ProfilerError != NULL) {
1650 Py_INCREF(ProfilerError);
1651 PyModule_AddObject(module, "ProfilerError", ProfilerError);
1652 }
1653 PyModule_AddIntConstant(module, "WHAT_ENTER", WHAT_ENTER);
1654 PyModule_AddIntConstant(module, "WHAT_EXIT", WHAT_EXIT);
1655 PyModule_AddIntConstant(module, "WHAT_LINENO", WHAT_LINENO);
1656 PyModule_AddIntConstant(module, "WHAT_OTHER", WHAT_OTHER);
1657 PyModule_AddIntConstant(module, "WHAT_ADD_INFO", WHAT_ADD_INFO);
1658 PyModule_AddIntConstant(module, "WHAT_DEFINE_FILE", WHAT_DEFINE_FILE);
Fred Drake30d1c752001-10-15 22:11:02 +00001659 PyModule_AddIntConstant(module, "WHAT_DEFINE_FUNC", WHAT_DEFINE_FUNC);
Fred Drake8c081a12001-10-12 20:57:55 +00001660 PyModule_AddIntConstant(module, "WHAT_LINE_TIMES", WHAT_LINE_TIMES);
1661 }
1662}