blob: 2eb7bdb25371241524a884a2896f15f83cde8612 [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 */
15#ifdef MS_WIN32
16#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;
86 int filled;
87 int index;
88 int linetimings;
Fred Drake30d1c752001-10-15 22:11:02 +000089 int frametimings;
Fred Drake8c081a12001-10-12 20:57:55 +000090 unsigned char buffer[BUFFERSIZE];
91} LogReaderObject;
92
93static PyObject * ProfilerError = NULL;
94
95
96#ifndef MS_WIN32
97#ifdef GETTIMEOFDAY_NO_TZ
98#define GETTIMEOFDAY(ptv) gettimeofday((ptv))
99#else
100#define GETTIMEOFDAY(ptv) gettimeofday((ptv), (struct timezone *)NULL)
101#endif
102#endif
103
104
105/* The log reader... */
106
Tim Petersfeab23f2001-10-13 00:11:10 +0000107static char logreader_close__doc__[] =
108"close()\n"
109"Close the log file, preventing additional records from being read.";
Fred Drake8c081a12001-10-12 20:57:55 +0000110
111static PyObject *
112logreader_close(LogReaderObject *self, PyObject *args)
113{
114 PyObject *result = NULL;
115 if (PyArg_ParseTuple(args, ":close")) {
116 if (self->logfp != NULL) {
117 fclose(self->logfp);
118 self->logfp = NULL;
119 }
120 result = Py_None;
121 Py_INCREF(result);
122 }
123 return result;
124}
125
126#if Py_TPFLAGS_HAVE_ITER
127/* This is only used if the interpreter has iterator support; the
128 * iternext handler is also used as a helper for other functions, so
129 * does not need to be included in this conditional section.
130 */
131static PyObject *
132logreader_tp_iter(LogReaderObject *self)
133{
134 Py_INCREF(self);
135 return (PyObject *) self;
136}
137#endif
138
139
140/* Log File Format
141 * ---------------
142 *
143 * The log file consists of a sequence of variable-length records.
144 * Each record is identified with a record type identifier in two
145 * bits of the first byte. The two bits are the "least significant"
146 * bits of the byte.
147 *
148 * Low bits: Opcode: Meaning:
149 * 0x00 ENTER enter a frame
150 * 0x01 EXIT exit a frame
151 * 0x02 LINENO SET_LINENO instruction was executed
152 * 0x03 OTHER more bits are needed to deecode
153 *
154 * If the type is OTHER, the record is not packed so tightly, and the
155 * remaining bits are used to disambiguate the record type. These
156 * records are not used as frequently so compaction is not an issue.
157 * Each of the first three record types has a highly tailored
158 * structure that allows it to be packed tightly.
159 *
160 * The OTHER records have the following identifiers:
161 *
162 * First byte: Opcode: Meaning:
163 * 0x13 ADD_INFO define a key/value pair
164 * 0x23 DEFINE_FILE define an int->filename mapping
165 * 0x33 LINE_TIMES indicates if LINENO events have tdeltas
Fred Drake30d1c752001-10-15 22:11:02 +0000166 * 0x43 DEFINE_FUNC define a (fileno,lineno)->funcname mapping
167 * 0x53 FRAME_TIMES indicates if ENTER/EXIT events have tdeltas
Fred Drake8c081a12001-10-12 20:57:55 +0000168 *
169 * Packed Integers
170 *
171 * "Packed integers" are non-negative integer values encoded as a
172 * sequence of bytes. Each byte is encoded such that the most
173 * significant bit is set if the next byte is also part of the
174 * integer. Each byte provides bits to the least-significant end of
175 * the result; the accumulated value must be shifted up to place the
176 * new bits into the result.
177 *
178 * "Modified packed integers" are packed integers where only a portion
179 * of the first byte is used. In the rest of the specification, these
180 * are referred to as "MPI(n,name)", where "n" is the number of bits
181 * discarded from the least-signicant positions of the byte, and
182 * "name" is a name being given to those "discarded" bits, since they
183 * are a field themselves.
184 *
185 * ENTER records:
186 *
187 * MPI(2,type) fileno -- type is 0x00
Fred Drake8c081a12001-10-12 20:57:55 +0000188 * PI lineno
Fred Drake30d1c752001-10-15 22:11:02 +0000189 * PI tdelta -- iff frame times are enabled
Fred Drake8c081a12001-10-12 20:57:55 +0000190 *
191 * EXIT records
192 *
Fred Drake30d1c752001-10-15 22:11:02 +0000193 * MPI(2,type) tdelta -- type is 0x01; tdelta will be 0
194 * if frame times are disabled
Fred Drake8c081a12001-10-12 20:57:55 +0000195 *
196 * LINENO records
197 *
198 * MPI(2,type) lineno -- type is 0x02
199 * PI tdelta -- iff LINENO includes it
200 *
201 * ADD_INFO records
202 *
Fred Drake30d1c752001-10-15 22:11:02 +0000203 * BYTE type -- always 0x13
Fred Drake8c081a12001-10-12 20:57:55 +0000204 * PI len1 -- length of first string
205 * BYTE string1[len1] -- len1 bytes of string data
206 * PI len2 -- length of second string
207 * BYTE string2[len2] -- len2 bytes of string data
208 *
209 * DEFINE_FILE records
210 *
Fred Drake30d1c752001-10-15 22:11:02 +0000211 * BYTE type -- always 0x23
Fred Drake8c081a12001-10-12 20:57:55 +0000212 * PI fileno
213 * PI len -- length of filename
214 * BYTE filename[len] -- len bytes of string data
215 *
Fred Drake30d1c752001-10-15 22:11:02 +0000216 * DEFINE_FUNC records
217 *
218 * BYTE type -- always 0x43
219 * PI fileno
220 * PI lineno
221 * PI len -- length of funcname
222 * BYTE funcname[len] -- len bytes of string data
223 *
Fred Drake8c081a12001-10-12 20:57:55 +0000224 * LINE_TIMES records
Fred Drake30d1c752001-10-15 22:11:02 +0000225 *
226 * This record can be used only before the start of ENTER/EXIT/LINENO
227 * records. If have_tdelta is true, LINENO records will include the
228 * tdelta field, otherwise it will be omitted. If this record is not
229 * given, LINENO records will not contain the tdelta field.
230 *
231 * BYTE type -- always 0x33
Fred Drake8c081a12001-10-12 20:57:55 +0000232 * BYTE have_tdelta -- 0 if LINENO does *not* have
233 * timing information
Fred Drake30d1c752001-10-15 22:11:02 +0000234 * FRAME_TIMES records
235 *
236 * This record can be used only before the start of ENTER/EXIT/LINENO
237 * records. If have_tdelta is true, ENTER and EXIT records will
238 * include the tdelta field, otherwise it will be omitted. If this
239 * record is not given, ENTER and EXIT records will contain the tdelta
240 * field.
241 *
242 * BYTE type -- always 0x53
243 * BYTE have_tdelta -- 0 if ENTER/EXIT do *not* have
244 * timing information
Fred Drake8c081a12001-10-12 20:57:55 +0000245 */
246
247#define WHAT_ENTER 0x00
248#define WHAT_EXIT 0x01
249#define WHAT_LINENO 0x02
250#define WHAT_OTHER 0x03 /* only used in decoding */
251#define WHAT_ADD_INFO 0x13
252#define WHAT_DEFINE_FILE 0x23
253#define WHAT_LINE_TIMES 0x33
Fred Drake30d1c752001-10-15 22:11:02 +0000254#define WHAT_DEFINE_FUNC 0x43
255#define WHAT_FRAME_TIMES 0x53
Fred Drake8c081a12001-10-12 20:57:55 +0000256
257#define ERR_NONE 0
258#define ERR_EOF -1
259#define ERR_EXCEPTION -2
Fred Drake4c2e1af2001-10-29 20:45:57 +0000260#define ERR_BAD_RECTYPE -3
Fred Drake8c081a12001-10-12 20:57:55 +0000261
262#define PISIZE (sizeof(int) + 1)
263#define MPISIZE (PISIZE + 1)
264
265/* Maximum size of "normal" events -- nothing that contains string data */
266#define MAXEVENTSIZE (MPISIZE + PISIZE*2)
267
268
269/* Unpack a packed integer; if "discard" is non-zero, unpack a modified
270 * packed integer with "discard" discarded bits.
271 */
272static int
273unpack_packed_int(LogReaderObject *self, int *pvalue, int discard)
274{
275 int accum = 0;
276 int bits = 0;
277 int index = self->index;
278 int cont;
279
280 do {
281 if (index >= self->filled)
282 return ERR_EOF;
283 /* read byte */
284 accum |= ((self->buffer[index] & 0x7F) >> discard) << bits;
285 bits += (7 - discard);
286 cont = self->buffer[index] & 0x80;
287 /* move to next */
288 discard = 0;
289 index++;
290 } while (cont);
291
292 /* save state */
293 self->index = index;
294 *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{
305 int len;
306 int oldindex = self->index;
307 int err = unpack_packed_int(self, &len, 0);
308
309 if (!err) {
310 /* need at least len bytes in buffer */
311 if (len > (self->filled - self->index)) {
312 self->index = oldindex;
313 err = ERR_EOF;
314 }
315 else {
Jack Jansen963659a2001-10-23 22:26:16 +0000316 *pvalue = PyString_FromStringAndSize((char *)self->buffer + self->index,
Fred Drake8c081a12001-10-12 20:57:55 +0000317 len);
318 if (*pvalue == NULL) {
319 self->index = oldindex;
320 err = ERR_EXCEPTION;
321 }
322 else
323 self->index += len;
324 }
325 }
326 return err;
327}
328
329
Fred Drake4c2e1af2001-10-29 20:45:57 +0000330static int
331unpack_add_info(LogReaderObject *self, int skip_opcode)
332{
333 PyObject *key;
334 PyObject *value = NULL;
335 int err;
336
337 if (skip_opcode) {
338 if (self->buffer[self->index] != WHAT_ADD_INFO)
339 return ERR_BAD_RECTYPE;
340 self->index++;
341 }
342 err = unpack_string(self, &key);
343 if (!err) {
344 err = unpack_string(self, &value);
345 if (err)
346 Py_DECREF(key);
347 else {
348 PyObject *list = PyDict_GetItem(self->info, key);
349 if (list == NULL) {
350 list = PyList_New(0);
351 if (list == NULL) {
352 err = ERR_EXCEPTION;
353 goto finally;
354 }
355 if (PyDict_SetItem(self->info, key, list)) {
356 err = ERR_EXCEPTION;
357 goto finally;
358 }
359 }
360 if (PyList_Append(list, value))
361 err = ERR_EXCEPTION;
362 }
363 }
364 finally:
365 Py_XDECREF(key);
366 Py_XDECREF(value);
367 return err;
368}
369
370
371static void
372logreader_refill(LogReaderObject *self)
373{
374 int needed;
375 size_t res;
376
377 if (self->index) {
378 memmove(self->buffer, &self->buffer[self->index],
379 self->filled - self->index);
380 self->filled = self->filled - self->index;
381 self->index = 0;
382 }
383 needed = BUFFERSIZE - self->filled;
384 if (needed > 0) {
385 res = fread(&self->buffer[self->filled], 1, needed, self->logfp);
386 self->filled += res;
387 }
388}
389
390static void
391eof_error(void)
392{
393 PyErr_SetString(PyExc_EOFError,
394 "end of file with incomplete profile record");
395}
396
Fred Drake8c081a12001-10-12 20:57:55 +0000397static PyObject *
398logreader_tp_iternext(LogReaderObject *self)
399{
400 int what, oldindex;
401 int err = ERR_NONE;
402 int lineno = -1;
403 int fileno = -1;
404 int tdelta = -1;
405 PyObject *s1 = NULL, *s2 = NULL;
406 PyObject *result = NULL;
407#if 0
408 unsigned char b0, b1;
409#endif
410
411 if (self->logfp == NULL) {
412 PyErr_SetString(ProfilerError,
413 "cannot iterate over closed LogReader object");
414 return NULL;
415 }
416 restart:
Fred Drake4c2e1af2001-10-29 20:45:57 +0000417 if ((self->filled - self->index) < MAXEVENTSIZE)
418 logreader_refill(self);
419
Fred Drake8c081a12001-10-12 20:57:55 +0000420 /* end of input */
421 if (self->filled == 0)
422 return NULL;
423
424 oldindex = self->index;
425
426 /* decode the record type */
427 what = self->buffer[self->index] & WHAT_OTHER;
428 if (what == WHAT_OTHER) {
429 what = self->buffer[self->index];
430 self->index++;
431 }
432 switch (what) {
433 case WHAT_ENTER:
434 err = unpack_packed_int(self, &fileno, 2);
435 if (!err) {
Fred Drake30d1c752001-10-15 22:11:02 +0000436 err = unpack_packed_int(self, &lineno, 0);
437 if (self->frametimings && !err)
438 err = unpack_packed_int(self, &tdelta, 0);
Fred Drake8c081a12001-10-12 20:57:55 +0000439 }
440 break;
441 case WHAT_EXIT:
442 err = unpack_packed_int(self, &tdelta, 2);
443 break;
444 case WHAT_LINENO:
445 err = unpack_packed_int(self, &lineno, 2);
446 if (self->linetimings && !err)
447 err = unpack_packed_int(self, &tdelta, 0);
448 break;
449 case WHAT_ADD_INFO:
Fred Drake4c2e1af2001-10-29 20:45:57 +0000450 err = unpack_add_info(self, 0);
Fred Drake8c081a12001-10-12 20:57:55 +0000451 break;
452 case WHAT_DEFINE_FILE:
453 err = unpack_packed_int(self, &fileno, 0);
454 if (!err) {
455 err = unpack_string(self, &s1);
456 if (!err) {
457 Py_INCREF(Py_None);
458 s2 = Py_None;
459 }
460 }
461 break;
Fred Drake30d1c752001-10-15 22:11:02 +0000462 case WHAT_DEFINE_FUNC:
463 err = unpack_packed_int(self, &fileno, 0);
464 if (!err) {
465 err = unpack_packed_int(self, &lineno, 0);
466 if (!err)
467 err = unpack_string(self, &s1);
468 }
469 break;
Fred Drake8c081a12001-10-12 20:57:55 +0000470 case WHAT_LINE_TIMES:
471 if (self->index >= self->filled)
472 err = ERR_EOF;
473 else {
474 self->linetimings = self->buffer[self->index] ? 1 : 0;
475 self->index++;
476 goto restart;
477 }
Fred Drake4c2e1af2001-10-29 20:45:57 +0000478 break;
Fred Drake30d1c752001-10-15 22:11:02 +0000479 case WHAT_FRAME_TIMES:
480 if (self->index >= self->filled)
481 err = ERR_EOF;
482 else {
483 self->frametimings = self->buffer[self->index] ? 1 : 0;
484 self->index++;
485 goto restart;
486 }
Fred Drake4c2e1af2001-10-29 20:45:57 +0000487 break;
Fred Drake8c081a12001-10-12 20:57:55 +0000488 default:
Fred Drake4c2e1af2001-10-29 20:45:57 +0000489 err = ERR_BAD_RECTYPE;
Fred Drake8c081a12001-10-12 20:57:55 +0000490 }
491 if (err == ERR_EOF && oldindex != 0) {
492 /* It looks like we ran out of data before we had it all; this
493 * could easily happen with large packed integers or string
494 * data. Try forcing the buffer to be re-filled before failing.
495 */
496 err = ERR_NONE;
Fred Drake4c2e1af2001-10-29 20:45:57 +0000497 logreader_refill(self);
Fred Drake8c081a12001-10-12 20:57:55 +0000498 }
Fred Drake4c2e1af2001-10-29 20:45:57 +0000499 if (err == ERR_BAD_RECTYPE) {
500 PyErr_SetString(PyExc_ValueError,
501 "unknown record type in log file");
502 }
503 else if (err == ERR_EOF) {
Fred Drake8c081a12001-10-12 20:57:55 +0000504 /* Could not avoid end-of-buffer error. */
Fred Drake4c2e1af2001-10-29 20:45:57 +0000505 eof_error();
Fred Drake8c081a12001-10-12 20:57:55 +0000506 }
507 else if (!err) {
508 result = PyTuple_New(4);
509 PyTuple_SET_ITEM(result, 0, PyInt_FromLong(what));
510 PyTuple_SET_ITEM(result, 2, PyInt_FromLong(fileno));
Fred Drake30d1c752001-10-15 22:11:02 +0000511 if (s1 == NULL)
Fred Drake8c081a12001-10-12 20:57:55 +0000512 PyTuple_SET_ITEM(result, 1, PyInt_FromLong(tdelta));
Fred Drake30d1c752001-10-15 22:11:02 +0000513 else
Fred Drake8c081a12001-10-12 20:57:55 +0000514 PyTuple_SET_ITEM(result, 1, s1);
Fred Drake30d1c752001-10-15 22:11:02 +0000515 if (s2 == NULL)
516 PyTuple_SET_ITEM(result, 3, PyInt_FromLong(lineno));
517 else
Fred Drake8c081a12001-10-12 20:57:55 +0000518 PyTuple_SET_ITEM(result, 3, s2);
Fred Drake8c081a12001-10-12 20:57:55 +0000519 }
520 /* The only other case is err == ERR_EXCEPTION, in which case the
521 * exception is already set.
522 */
523#if 0
524 b0 = self->buffer[self->index];
525 b1 = self->buffer[self->index + 1];
526 if (b0 & 1) {
527 /* This is a line-number event. */
528 what = PyTrace_LINE;
529 lineno = ((b0 & ~1) << 7) + b1;
530 self->index += 2;
531 }
532 else {
533 what = (b0 & 0x0E) >> 1;
534 tdelta = ((b0 & 0xF0) << 4) + b1;
535 if (what == PyTrace_CALL) {
536 /* we know there's a 2-byte file ID & 2-byte line number */
537 fileno = ((self->buffer[self->index + 2] << 8)
538 + self->buffer[self->index + 3]);
539 lineno = ((self->buffer[self->index + 4] << 8)
540 + self->buffer[self->index + 5]);
541 self->index += 6;
542 }
543 else
544 self->index += 2;
545 }
546#endif
547 return result;
548}
549
550static void
551logreader_dealloc(LogReaderObject *self)
552{
553 if (self->logfp != NULL) {
554 fclose(self->logfp);
555 self->logfp = NULL;
556 }
557 PyObject_Del(self);
558}
559
560static PyObject *
561logreader_sq_item(LogReaderObject *self, int index)
562{
563 PyObject *result = logreader_tp_iternext(self);
564 if (result == NULL && !PyErr_Occurred()) {
565 PyErr_SetString(PyExc_IndexError, "no more events in log");
566 return NULL;
567 }
568 return result;
569}
570
Tim Petersfeab23f2001-10-13 00:11:10 +0000571static char next__doc__[] =
572"next() -> event-info\n"
573"Return the next event record from the log file.";
Fred Drake8c081a12001-10-12 20:57:55 +0000574
575static PyObject *
576logreader_next(LogReaderObject *self, PyObject *args)
577{
578 PyObject *result = NULL;
579
580 if (PyArg_ParseTuple(args, ":next")) {
581 result = logreader_tp_iternext(self);
582 /* XXX return None if there's nothing left */
583 /* tp_iternext does the right thing, though */
584 if (result == NULL && !PyErr_Occurred()) {
585 result = Py_None;
586 Py_INCREF(result);
587 }
588 }
589 return result;
590}
591
Fred Drake62c1e3c2001-12-04 21:40:53 +0000592static void
593do_stop(ProfilerObject *self);
Fred Drake8c081a12001-10-12 20:57:55 +0000594
595static int
596flush_data(ProfilerObject *self)
597{
598 /* Need to dump data to the log file... */
599 size_t written = fwrite(self->buffer, 1, self->index, self->logfp);
Tim Peters1566a172001-10-12 22:08:39 +0000600 if (written == (size_t)self->index)
Fred Drake8c081a12001-10-12 20:57:55 +0000601 self->index = 0;
602 else {
603 memmove(self->buffer, &self->buffer[written],
604 self->index - written);
605 self->index -= written;
606 if (written == 0) {
607 char *s = PyString_AsString(self->logfilename);
608 PyErr_SetFromErrnoWithFilename(PyExc_IOError, s);
Fred Drake62c1e3c2001-12-04 21:40:53 +0000609 do_stop(self);
Fred Drake8c081a12001-10-12 20:57:55 +0000610 return -1;
611 }
612 }
613 if (written > 0) {
614 if (fflush(self->logfp)) {
615 char *s = PyString_AsString(self->logfilename);
616 PyErr_SetFromErrnoWithFilename(PyExc_IOError, s);
Fred Drake62c1e3c2001-12-04 21:40:53 +0000617 do_stop(self);
Fred Drake8c081a12001-10-12 20:57:55 +0000618 return -1;
619 }
620 }
621 return 0;
622}
623
Fred Drake62c1e3c2001-12-04 21:40:53 +0000624static inline int
Fred Drake8c081a12001-10-12 20:57:55 +0000625pack_packed_int(ProfilerObject *self, int value)
626{
627 unsigned char partial;
628
629 do {
630 partial = value & 0x7F;
631 value >>= 7;
632 if (value)
633 partial |= 0x80;
634 self->buffer[self->index] = partial;
635 self->index++;
636 } while (value);
Fred Drake62c1e3c2001-12-04 21:40:53 +0000637 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000638}
639
640/* Encode a modified packed integer, with a subfield of modsize bits
641 * containing the value "subfield". The value of subfield is not
642 * checked to ensure it actually fits in modsize bits.
643 */
Fred Drake62c1e3c2001-12-04 21:40:53 +0000644static inline int
Fred Drake8c081a12001-10-12 20:57:55 +0000645pack_modified_packed_int(ProfilerObject *self, int value,
646 int modsize, int subfield)
647{
648 const int maxvalues[] = {-1, 1, 3, 7, 15, 31, 63, 127};
649
650 int bits = 7 - modsize;
651 int partial = value & maxvalues[bits];
652 unsigned char b = subfield | (partial << modsize);
653
654 if (partial != value) {
655 b |= 0x80;
656 self->buffer[self->index] = b;
657 self->index++;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000658 return pack_packed_int(self, value >> bits);
Fred Drake8c081a12001-10-12 20:57:55 +0000659 }
Fred Drake62c1e3c2001-12-04 21:40:53 +0000660 self->buffer[self->index] = b;
661 self->index++;
662 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000663}
664
Fred Drake62c1e3c2001-12-04 21:40:53 +0000665static int
Fred Drake30d1c752001-10-15 22:11:02 +0000666pack_string(ProfilerObject *self, const char *s, int len)
Fred Drake8c081a12001-10-12 20:57:55 +0000667{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000668 if (len + PISIZE + self->index >= BUFFERSIZE) {
669 if (flush_data(self) < 0)
670 return -1;
671 }
672 if (pack_packed_int(self, len) < 0)
673 return -1;
Fred Drake8c081a12001-10-12 20:57:55 +0000674 memcpy(self->buffer + self->index, s, len);
675 self->index += len;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000676 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000677}
678
Fred Drake62c1e3c2001-12-04 21:40:53 +0000679static int
Fred Drake8c081a12001-10-12 20:57:55 +0000680pack_add_info(ProfilerObject *self, const char *s1, const char *s2)
681{
682 int len1 = strlen(s1);
683 int len2 = strlen(s2);
684
Fred Drake62c1e3c2001-12-04 21:40:53 +0000685 if (len1 + len2 + PISIZE*2 + 1 + self->index >= BUFFERSIZE) {
686 if (flush_data(self) < 0)
687 return -1;
688 }
Fred Drake8c081a12001-10-12 20:57:55 +0000689 self->buffer[self->index] = WHAT_ADD_INFO;
690 self->index++;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000691 if (pack_string(self, s1, len1) < 0)
692 return -1;
693 return pack_string(self, s2, len2);
Fred Drake8c081a12001-10-12 20:57:55 +0000694}
695
Fred Drake62c1e3c2001-12-04 21:40:53 +0000696static int
Fred Drake8c081a12001-10-12 20:57:55 +0000697pack_define_file(ProfilerObject *self, int fileno, const char *filename)
698{
699 int len = strlen(filename);
700
Fred Drake62c1e3c2001-12-04 21:40:53 +0000701 if (len + PISIZE*2 + 1 + self->index >= BUFFERSIZE) {
702 if (flush_data(self) < 0)
703 return -1;
704 }
Fred Drake8c081a12001-10-12 20:57:55 +0000705 self->buffer[self->index] = WHAT_DEFINE_FILE;
706 self->index++;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000707 if (pack_packed_int(self, fileno) < 0)
708 return -1;
709 return pack_string(self, filename, len);
Fred Drake30d1c752001-10-15 22:11:02 +0000710}
711
Fred Drake62c1e3c2001-12-04 21:40:53 +0000712static int
Fred Drake30d1c752001-10-15 22:11:02 +0000713pack_define_func(ProfilerObject *self, int fileno, int lineno,
714 const char *funcname)
715{
716 int len = strlen(funcname);
717
Fred Drake62c1e3c2001-12-04 21:40:53 +0000718 if (len + PISIZE*3 + 1 + self->index >= BUFFERSIZE) {
719 if (flush_data(self) < 0)
720 return -1;
721 }
Fred Drake30d1c752001-10-15 22:11:02 +0000722 self->buffer[self->index] = WHAT_DEFINE_FUNC;
723 self->index++;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000724 if (pack_packed_int(self, fileno) < 0)
725 return -1;
726 if (pack_packed_int(self, lineno) < 0)
727 return -1;
728 return pack_string(self, funcname, len);
Fred Drake8c081a12001-10-12 20:57:55 +0000729}
730
Fred Drake62c1e3c2001-12-04 21:40:53 +0000731static int
Fred Drake8c081a12001-10-12 20:57:55 +0000732pack_line_times(ProfilerObject *self)
733{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000734 if (2 + self->index >= BUFFERSIZE) {
735 if (flush_data(self) < 0)
736 return -1;
737 }
Fred Drake8c081a12001-10-12 20:57:55 +0000738 self->buffer[self->index] = WHAT_LINE_TIMES;
739 self->buffer[self->index + 1] = self->linetimings ? 1 : 0;
740 self->index += 2;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000741 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000742}
743
Fred Drake62c1e3c2001-12-04 21:40:53 +0000744static int
Fred Drake30d1c752001-10-15 22:11:02 +0000745pack_frame_times(ProfilerObject *self)
746{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000747 if (2 + self->index >= BUFFERSIZE) {
748 if (flush_data(self) < 0)
749 return -1;
750 }
Fred Drake30d1c752001-10-15 22:11:02 +0000751 self->buffer[self->index] = WHAT_FRAME_TIMES;
752 self->buffer[self->index + 1] = self->frametimings ? 1 : 0;
753 self->index += 2;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000754 return 0;
Fred Drake30d1c752001-10-15 22:11:02 +0000755}
756
Fred Drake62c1e3c2001-12-04 21:40:53 +0000757static inline int
Fred Drake8c081a12001-10-12 20:57:55 +0000758pack_enter(ProfilerObject *self, int fileno, int tdelta, int lineno)
759{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000760 if (MPISIZE + PISIZE*2 + self->index >= BUFFERSIZE) {
761 if (flush_data(self) < 0)
762 return -1;
763 }
Fred Drake8c081a12001-10-12 20:57:55 +0000764 pack_modified_packed_int(self, fileno, 2, WHAT_ENTER);
Fred Drake8c081a12001-10-12 20:57:55 +0000765 pack_packed_int(self, lineno);
Fred Drake30d1c752001-10-15 22:11:02 +0000766 if (self->frametimings)
Fred Drake62c1e3c2001-12-04 21:40:53 +0000767 return pack_packed_int(self, tdelta);
768 else
769 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000770}
771
Fred Drake62c1e3c2001-12-04 21:40:53 +0000772static inline int
Fred Drake8c081a12001-10-12 20:57:55 +0000773pack_exit(ProfilerObject *self, int tdelta)
774{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000775 if (MPISIZE + self->index >= BUFFERSIZE) {
776 if (flush_data(self) < 0)
777 return -1;
Fred Drake30d1c752001-10-15 22:11:02 +0000778 }
Fred Drake62c1e3c2001-12-04 21:40:53 +0000779 if (self->frametimings)
780 return pack_modified_packed_int(self, tdelta, 2, WHAT_EXIT);
781 self->buffer[self->index] = WHAT_EXIT;
782 self->index++;
783 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000784}
785
Fred Drake62c1e3c2001-12-04 21:40:53 +0000786static inline int
Fred Drake8c081a12001-10-12 20:57:55 +0000787pack_lineno(ProfilerObject *self, int lineno)
788{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000789 if (MPISIZE + self->index >= BUFFERSIZE) {
790 if (flush_data(self) < 0)
791 return -1;
792 }
793 return pack_modified_packed_int(self, lineno, 2, WHAT_LINENO);
Fred Drake8c081a12001-10-12 20:57:55 +0000794}
795
Fred Drake62c1e3c2001-12-04 21:40:53 +0000796static inline int
Fred Drake8c081a12001-10-12 20:57:55 +0000797pack_lineno_tdelta(ProfilerObject *self, int lineno, int tdelta)
798{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000799 if (MPISIZE + PISIZE + self->index >= BUFFERSIZE) {
800 if (flush_data(self) < 0)
801 return 0;
802 }
803 if (pack_modified_packed_int(self, lineno, 2, WHAT_LINENO) < 0)
804 return -1;
805 return pack_packed_int(self, tdelta);
Fred Drake8c081a12001-10-12 20:57:55 +0000806}
807
808static inline int
809get_fileno(ProfilerObject *self, PyCodeObject *fcode)
810{
Fred Drake30d1c752001-10-15 22:11:02 +0000811 /* This is only used for ENTER events. */
812
813 PyObject *obj;
814 PyObject *dict;
Fred Drake8c081a12001-10-12 20:57:55 +0000815 int fileno;
816
Fred Drake30d1c752001-10-15 22:11:02 +0000817 obj = PyDict_GetItem(self->filemap, fcode->co_filename);
818 if (obj == NULL) {
Fred Drake8c081a12001-10-12 20:57:55 +0000819 /* first sighting of this file */
Fred Drake30d1c752001-10-15 22:11:02 +0000820 dict = PyDict_New();
821 if (dict == NULL) {
Fred Drake8c081a12001-10-12 20:57:55 +0000822 return -1;
823 }
Fred Drake30d1c752001-10-15 22:11:02 +0000824 fileno = self->next_fileno;
825 obj = Py_BuildValue("iN", fileno, dict);
826 if (obj == NULL) {
827 return -1;
828 }
829 if (PyDict_SetItem(self->filemap, fcode->co_filename, obj)) {
830 Py_DECREF(obj);
Fred Drake8c081a12001-10-12 20:57:55 +0000831 return -1;
832 }
833 self->next_fileno++;
Fred Drake30d1c752001-10-15 22:11:02 +0000834 Py_DECREF(obj);
Fred Drake62c1e3c2001-12-04 21:40:53 +0000835 if (pack_define_file(self, fileno,
836 PyString_AS_STRING(fcode->co_filename)) < 0)
837 return -1;
Fred Drake8c081a12001-10-12 20:57:55 +0000838 }
839 else {
840 /* already know this ID */
Fred Drake30d1c752001-10-15 22:11:02 +0000841 fileno = PyInt_AS_LONG(PyTuple_GET_ITEM(obj, 0));
842 dict = PyTuple_GET_ITEM(obj, 1);
843 }
844 /* make sure we save a function name for this (fileno, lineno) */
845 obj = PyInt_FromLong(fcode->co_firstlineno);
846 if (obj == NULL) {
847 /* We just won't have it saved; too bad. */
848 PyErr_Clear();
849 }
850 else {
851 PyObject *name = PyDict_GetItem(dict, obj);
852 if (name == NULL) {
Fred Drake62c1e3c2001-12-04 21:40:53 +0000853 if (pack_define_func(self, fileno, fcode->co_firstlineno,
854 PyString_AS_STRING(fcode->co_name)) < 0)
855 return -1;
Fred Drake30d1c752001-10-15 22:11:02 +0000856 if (PyDict_SetItem(dict, obj, fcode->co_name))
857 return -1;
858 }
Fred Drake8c081a12001-10-12 20:57:55 +0000859 }
860 return fileno;
861}
862
863static inline int
864get_tdelta(ProfilerObject *self)
865{
866 int tdelta;
867#ifdef MS_WIN32
868 hs_time tv;
Tim Peters7d99ff22001-10-13 07:37:52 +0000869 hs_time diff;
Fred Drake8c081a12001-10-12 20:57:55 +0000870
Tim Peters7d99ff22001-10-13 07:37:52 +0000871 GETTIMEOFDAY(&tv);
872 diff = tv - self->prev_timeofday;
873 tdelta = (int)diff;
Fred Drake8c081a12001-10-12 20:57:55 +0000874#else
875 struct timeval tv;
876
877 GETTIMEOFDAY(&tv);
878
879 if (tv.tv_sec == self->prev_timeofday.tv_sec)
880 tdelta = tv.tv_usec - self->prev_timeofday.tv_usec;
881 else
882 tdelta = ((tv.tv_sec - self->prev_timeofday.tv_sec) * 1000000
883 + tv.tv_usec);
884#endif
885 self->prev_timeofday = tv;
886 return tdelta;
887}
888
889
890/* The workhorse: the profiler callback function. */
891
892static int
893profiler_callback(ProfilerObject *self, PyFrameObject *frame, int what,
894 PyObject *arg)
895{
Fred Drake30d1c752001-10-15 22:11:02 +0000896 int tdelta = -1;
Fred Drake8c081a12001-10-12 20:57:55 +0000897 int fileno;
898
Fred Drake30d1c752001-10-15 22:11:02 +0000899 if (self->frametimings)
900 tdelta = get_tdelta(self);
Fred Drake8c081a12001-10-12 20:57:55 +0000901 switch (what) {
902 case PyTrace_CALL:
903 fileno = get_fileno(self, frame->f_code);
904 if (fileno < 0)
905 return -1;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000906 if (pack_enter(self, fileno, tdelta,
907 frame->f_code->co_firstlineno) < 0)
908 return -1;
Fred Drake8c081a12001-10-12 20:57:55 +0000909 break;
910 case PyTrace_RETURN:
Fred Drake62c1e3c2001-12-04 21:40:53 +0000911 if (pack_exit(self, tdelta) < 0)
912 return -1;
Fred Drake8c081a12001-10-12 20:57:55 +0000913 break;
914 default:
915 /* should never get here */
916 break;
917 }
918 return 0;
919}
920
921
922/* Alternate callback when we want PyTrace_LINE events */
923
924static int
925tracer_callback(ProfilerObject *self, PyFrameObject *frame, int what,
926 PyObject *arg)
927{
928 int fileno;
929
Fred Drake8c081a12001-10-12 20:57:55 +0000930 switch (what) {
931 case PyTrace_CALL:
932 fileno = get_fileno(self, frame->f_code);
933 if (fileno < 0)
934 return -1;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000935 return pack_enter(self, fileno,
936 self->frametimings ? get_tdelta(self) : -1,
937 frame->f_code->co_firstlineno);
938
Fred Drake8c081a12001-10-12 20:57:55 +0000939 case PyTrace_RETURN:
Fred Drake62c1e3c2001-12-04 21:40:53 +0000940 return pack_exit(self, get_tdelta(self));
941
Tim Peters1566a172001-10-12 22:08:39 +0000942 case PyTrace_LINE:
Fred Drake8c081a12001-10-12 20:57:55 +0000943 if (self->linetimings)
Fred Drake62c1e3c2001-12-04 21:40:53 +0000944 return pack_lineno_tdelta(self, frame->f_lineno, get_tdelta(self));
Fred Drake8c081a12001-10-12 20:57:55 +0000945 else
Fred Drake62c1e3c2001-12-04 21:40:53 +0000946 return pack_lineno(self, frame->f_lineno);
947
Fred Drake8c081a12001-10-12 20:57:55 +0000948 default:
949 /* ignore PyTrace_EXCEPTION */
950 break;
951 }
952 return 0;
953}
954
955
956/* A couple of useful helper functions. */
957
958#ifdef MS_WIN32
Tim Petersfeab23f2001-10-13 00:11:10 +0000959static LARGE_INTEGER frequency = {0, 0};
Fred Drake8c081a12001-10-12 20:57:55 +0000960#endif
961
962static unsigned long timeofday_diff = 0;
963static unsigned long rusage_diff = 0;
964
965static void
966calibrate(void)
967{
968 hs_time tv1, tv2;
969
970#ifdef MS_WIN32
Tim Peters7d99ff22001-10-13 07:37:52 +0000971 hs_time diff;
Fred Drake8c081a12001-10-12 20:57:55 +0000972 QueryPerformanceFrequency(&frequency);
973#endif
974
975 GETTIMEOFDAY(&tv1);
976 while (1) {
977 GETTIMEOFDAY(&tv2);
978#ifdef MS_WIN32
Tim Peters7d99ff22001-10-13 07:37:52 +0000979 diff = tv2 - tv1;
980 if (diff != 0) {
981 timeofday_diff = (unsigned long)diff;
Fred Drake8c081a12001-10-12 20:57:55 +0000982 break;
983 }
984#else
985 if (tv1.tv_sec != tv2.tv_sec || tv1.tv_usec != tv2.tv_usec) {
986 if (tv1.tv_sec == tv2.tv_sec)
987 timeofday_diff = tv2.tv_usec - tv1.tv_usec;
988 else
989 timeofday_diff = (1000000 - tv1.tv_usec) + tv2.tv_usec;
990 break;
991 }
992#endif
993 }
Andrew MacIntyre7bf68332002-03-03 02:59:16 +0000994#if defined(MS_WIN32) || defined(macintosh) || defined(PYOS_OS2)
Fred Drake8c081a12001-10-12 20:57:55 +0000995 rusage_diff = -1;
996#else
997 {
998 struct rusage ru1, ru2;
999
1000 getrusage(RUSAGE_SELF, &ru1);
1001 while (1) {
1002 getrusage(RUSAGE_SELF, &ru2);
1003 if (ru1.ru_utime.tv_sec != ru2.ru_utime.tv_sec) {
1004 rusage_diff = ((1000000 - ru1.ru_utime.tv_usec)
1005 + ru2.ru_utime.tv_usec);
1006 break;
1007 }
1008 else if (ru1.ru_utime.tv_usec != ru2.ru_utime.tv_usec) {
1009 rusage_diff = ru2.ru_utime.tv_usec - ru1.ru_utime.tv_usec;
1010 break;
1011 }
1012 else if (ru1.ru_stime.tv_sec != ru2.ru_stime.tv_sec) {
1013 rusage_diff = ((1000000 - ru1.ru_stime.tv_usec)
1014 + ru2.ru_stime.tv_usec);
1015 break;
1016 }
1017 else if (ru1.ru_stime.tv_usec != ru2.ru_stime.tv_usec) {
1018 rusage_diff = ru2.ru_stime.tv_usec - ru1.ru_stime.tv_usec;
1019 break;
1020 }
1021 }
1022 }
1023#endif
1024}
1025
1026static void
1027do_start(ProfilerObject *self)
1028{
1029 self->active = 1;
1030 GETTIMEOFDAY(&self->prev_timeofday);
1031 if (self->lineevents)
1032 PyEval_SetTrace((Py_tracefunc) tracer_callback, (PyObject *)self);
1033 else
1034 PyEval_SetProfile((Py_tracefunc) profiler_callback, (PyObject *)self);
1035}
1036
1037static void
1038do_stop(ProfilerObject *self)
1039{
1040 if (self->active) {
1041 self->active = 0;
1042 if (self->lineevents)
1043 PyEval_SetTrace(NULL, NULL);
1044 else
1045 PyEval_SetProfile(NULL, NULL);
1046 }
1047 if (self->index > 0) {
1048 /* Best effort to dump out any remaining data. */
1049 flush_data(self);
1050 }
1051}
1052
1053static int
1054is_available(ProfilerObject *self)
1055{
1056 if (self->active) {
1057 PyErr_SetString(ProfilerError, "profiler already active");
1058 return 0;
1059 }
1060 if (self->logfp == NULL) {
1061 PyErr_SetString(ProfilerError, "profiler already closed");
1062 return 0;
1063 }
1064 return 1;
1065}
1066
1067
1068/* Profiler object interface methods. */
1069
Fred Drake4c2e1af2001-10-29 20:45:57 +00001070static char addinfo__doc__[] =
1071"addinfo(key, value)\n"
1072"Insert an ADD_INFO record into the log.";
1073
1074static PyObject *
1075profiler_addinfo(ProfilerObject *self, PyObject *args)
1076{
1077 PyObject *result = NULL;
1078 char *key, *value;
1079
1080 if (PyArg_ParseTuple(args, "ss:addinfo", &key, &value)) {
1081 if (self->logfp == NULL)
1082 PyErr_SetString(ProfilerError, "profiler already closed");
1083 else {
Fred Drake62c1e3c2001-12-04 21:40:53 +00001084 if (pack_add_info(self, key, value) == 0) {
1085 result = Py_None;
1086 Py_INCREF(result);
1087 }
Fred Drake4c2e1af2001-10-29 20:45:57 +00001088 }
1089 }
1090 return result;
1091}
1092
Tim Petersfeab23f2001-10-13 00:11:10 +00001093static char close__doc__[] =
1094"close()\n"
1095"Shut down this profiler and close the log files, even if its active.";
Fred Drake8c081a12001-10-12 20:57:55 +00001096
1097static PyObject *
1098profiler_close(ProfilerObject *self, PyObject *args)
1099{
1100 PyObject *result = NULL;
1101
1102 if (PyArg_ParseTuple(args, ":close")) {
1103 do_stop(self);
1104 if (self->logfp != NULL) {
1105 fclose(self->logfp);
1106 self->logfp = NULL;
1107 }
1108 Py_INCREF(Py_None);
1109 result = Py_None;
1110 }
1111 return result;
1112}
1113
Tim Petersfeab23f2001-10-13 00:11:10 +00001114static char runcall__doc__[] =
1115"runcall(callable[, args[, kw]]) -> callable()\n"
1116"Profile a specific function call, returning the result of that call.";
Fred Drake8c081a12001-10-12 20:57:55 +00001117
1118static PyObject *
1119profiler_runcall(ProfilerObject *self, PyObject *args)
1120{
1121 PyObject *result = NULL;
1122 PyObject *callargs = NULL;
1123 PyObject *callkw = NULL;
1124 PyObject *callable;
1125
1126 if (PyArg_ParseTuple(args, "O|OO:runcall",
1127 &callable, &callargs, &callkw)) {
1128 if (is_available(self)) {
1129 do_start(self);
1130 result = PyEval_CallObjectWithKeywords(callable, callargs, callkw);
1131 do_stop(self);
1132 }
1133 }
1134 return result;
1135}
1136
Tim Petersfeab23f2001-10-13 00:11:10 +00001137static char runcode__doc__[] =
1138"runcode(code, globals[, locals])\n"
1139"Execute a code object while collecting profile data. If locals is\n"
1140"omitted, globals is used for the locals as well.";
Fred Drake8c081a12001-10-12 20:57:55 +00001141
1142static PyObject *
1143profiler_runcode(ProfilerObject *self, PyObject *args)
1144{
1145 PyObject *result = NULL;
1146 PyCodeObject *code;
1147 PyObject *globals;
1148 PyObject *locals = NULL;
1149
1150 if (PyArg_ParseTuple(args, "O!O!|O:runcode",
1151 &PyCode_Type, &code,
1152 &PyDict_Type, &globals,
1153 &locals)) {
1154 if (is_available(self)) {
1155 if (locals == NULL || locals == Py_None)
1156 locals = globals;
1157 else if (!PyDict_Check(locals)) {
1158 PyErr_SetString(PyExc_TypeError,
1159 "locals must be a dictionary or None");
1160 return NULL;
1161 }
1162 do_start(self);
1163 result = PyEval_EvalCode(code, globals, locals);
1164 do_stop(self);
1165#if 0
1166 if (!PyErr_Occurred()) {
1167 result = Py_None;
1168 Py_INCREF(result);
1169 }
1170#endif
1171 }
1172 }
1173 return result;
1174}
1175
Tim Petersfeab23f2001-10-13 00:11:10 +00001176static char start__doc__[] =
1177"start()\n"
1178"Install this profiler for the current thread.";
Fred Drake8c081a12001-10-12 20:57:55 +00001179
1180static PyObject *
1181profiler_start(ProfilerObject *self, PyObject *args)
1182{
1183 PyObject *result = NULL;
1184
1185 if (PyArg_ParseTuple(args, ":start")) {
Fred Drake2c146bf2002-02-08 21:27:50 +00001186 if (is_available(self)) {
Fred Drake8c081a12001-10-12 20:57:55 +00001187 do_start(self);
Fred Drake2c146bf2002-02-08 21:27:50 +00001188 result = Py_None;
1189 Py_INCREF(result);
1190 }
Fred Drake8c081a12001-10-12 20:57:55 +00001191 }
1192 return result;
1193}
1194
Tim Petersfeab23f2001-10-13 00:11:10 +00001195static char stop__doc__[] =
1196"stop()\n"
1197"Remove this profiler from the current thread.";
Fred Drake8c081a12001-10-12 20:57:55 +00001198
1199static PyObject *
1200profiler_stop(ProfilerObject *self, PyObject *args)
1201{
1202 PyObject *result = NULL;
1203
1204 if (PyArg_ParseTuple(args, ":stop")) {
1205 if (!self->active)
1206 PyErr_SetString(ProfilerError, "profiler not active");
Fred Drake2c146bf2002-02-08 21:27:50 +00001207 else {
Fred Drake8c081a12001-10-12 20:57:55 +00001208 do_stop(self);
Fred Drake2c146bf2002-02-08 21:27:50 +00001209 result = Py_None;
1210 Py_INCREF(result);
1211 }
Fred Drake8c081a12001-10-12 20:57:55 +00001212 }
1213 return result;
1214}
1215
1216
1217/* Python API support. */
1218
1219static void
1220profiler_dealloc(ProfilerObject *self)
1221{
1222 do_stop(self);
1223 if (self->logfp != NULL)
1224 fclose(self->logfp);
1225 Py_XDECREF(self->filemap);
1226 Py_XDECREF(self->logfilename);
1227 PyObject_Del((PyObject *)self);
1228}
1229
1230/* Always use METH_VARARGS even though some of these could be METH_NOARGS;
1231 * this allows us to maintain compatibility with Python versions < 2.2
1232 * more easily, requiring only the changes to the dispatcher to be made.
1233 */
1234static PyMethodDef profiler_methods[] = {
Fred Drake4c2e1af2001-10-29 20:45:57 +00001235 {"addinfo", (PyCFunction)profiler_addinfo, METH_VARARGS, addinfo__doc__},
Fred Drake8c081a12001-10-12 20:57:55 +00001236 {"close", (PyCFunction)profiler_close, METH_VARARGS, close__doc__},
1237 {"runcall", (PyCFunction)profiler_runcall, METH_VARARGS, runcall__doc__},
1238 {"runcode", (PyCFunction)profiler_runcode, METH_VARARGS, runcode__doc__},
1239 {"start", (PyCFunction)profiler_start, METH_VARARGS, start__doc__},
1240 {"stop", (PyCFunction)profiler_stop, METH_VARARGS, stop__doc__},
1241 {NULL, NULL}
1242};
1243
1244/* Use a table even though there's only one "simple" member; this allows
1245 * __members__ and therefore dir() to work.
1246 */
1247static struct memberlist profiler_members[] = {
Fred Drake30d1c752001-10-15 22:11:02 +00001248 {"closed", T_INT, -1, READONLY},
1249 {"frametimings", T_LONG, offsetof(ProfilerObject, linetimings), READONLY},
1250 {"lineevents", T_LONG, offsetof(ProfilerObject, lineevents), READONLY},
1251 {"linetimings", T_LONG, offsetof(ProfilerObject, linetimings), READONLY},
Fred Drake8c081a12001-10-12 20:57:55 +00001252 {NULL}
1253};
1254
1255static PyObject *
1256profiler_getattr(ProfilerObject *self, char *name)
1257{
1258 PyObject *result;
1259 if (strcmp(name, "closed") == 0) {
1260 result = (self->logfp == NULL) ? Py_True : Py_False;
1261 Py_INCREF(result);
1262 }
1263 else {
1264 result = PyMember_Get((char *)self, profiler_members, name);
1265 if (result == NULL) {
1266 PyErr_Clear();
1267 result = Py_FindMethod(profiler_methods, (PyObject *)self, name);
1268 }
1269 }
1270 return result;
1271}
1272
1273
Tim Petersfeab23f2001-10-13 00:11:10 +00001274static char profiler_object__doc__[] =
1275"High-performance profiler object.\n"
1276"\n"
1277"Methods:\n"
1278"\n"
Fred Drake30d1c752001-10-15 22:11:02 +00001279"close(): Stop the profiler and close the log files.\n"
1280"runcall(): Run a single function call with profiling enabled.\n"
1281"runcode(): Execute a code object with profiling enabled.\n"
1282"start(): Install the profiler and return.\n"
1283"stop(): Remove the profiler.\n"
Tim Petersfeab23f2001-10-13 00:11:10 +00001284"\n"
1285"Attributes (read-only):\n"
1286"\n"
Fred Drake30d1c752001-10-15 22:11:02 +00001287"closed: True if the profiler has already been closed.\n"
1288"frametimings: True if ENTER/EXIT events collect timing information.\n"
1289"lineevents: True if SET_LINENO events are reported to the profiler.\n"
1290"linetimings: True if SET_LINENO events collect timing information.";
Fred Drake8c081a12001-10-12 20:57:55 +00001291
1292static PyTypeObject ProfilerType = {
1293 PyObject_HEAD_INIT(NULL)
1294 0, /* ob_size */
Fred Drake4c2e1af2001-10-29 20:45:57 +00001295 "_hotshot.ProfilerType", /* tp_name */
Fred Drake8c081a12001-10-12 20:57:55 +00001296 (int) sizeof(ProfilerObject), /* tp_basicsize */
1297 0, /* tp_itemsize */
1298 (destructor)profiler_dealloc, /* tp_dealloc */
1299 0, /* tp_print */
1300 (getattrfunc)profiler_getattr, /* tp_getattr */
1301 0, /* tp_setattr */
1302 0, /* tp_compare */
1303 0, /* tp_repr */
1304 0, /* tp_as_number */
1305 0, /* tp_as_sequence */
1306 0, /* tp_as_mapping */
1307 0, /* tp_hash */
1308 0, /* tp_call */
1309 0, /* tp_str */
1310 0, /* tp_getattro */
1311 0, /* tp_setattro */
1312 0, /* tp_as_buffer */
Fred Drake4c2e1af2001-10-29 20:45:57 +00001313 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake8c081a12001-10-12 20:57:55 +00001314 profiler_object__doc__, /* tp_doc */
1315};
1316
1317
1318static PyMethodDef logreader_methods[] = {
1319 {"close", (PyCFunction)logreader_close, METH_VARARGS,
1320 logreader_close__doc__},
1321 {"next", (PyCFunction)logreader_next, METH_VARARGS,
1322 next__doc__},
1323 {NULL, NULL}
1324};
1325
1326static PyObject *
Fred Drake4c2e1af2001-10-29 20:45:57 +00001327logreader_getattr(LogReaderObject *self, char *name)
Fred Drake8c081a12001-10-12 20:57:55 +00001328{
Fred Drake4c2e1af2001-10-29 20:45:57 +00001329 if (strcmp(name, "info") == 0) {
1330 Py_INCREF(self->info);
1331 return self->info;
1332 }
Fred Drake8c081a12001-10-12 20:57:55 +00001333 return Py_FindMethod(logreader_methods, (PyObject *)self, name);
1334}
1335
1336
1337static char logreader__doc__[] = "\
1338logreader(filename) --> log-iterator\n\
1339Create a log-reader for the timing information file.";
1340
1341static PySequenceMethods logreader_as_sequence = {
1342 0, /* sq_length */
1343 0, /* sq_concat */
1344 0, /* sq_repeat */
1345 (intargfunc)logreader_sq_item, /* sq_item */
1346 0, /* sq_slice */
1347 0, /* sq_ass_item */
1348 0, /* sq_ass_slice */
1349 0, /* sq_contains */
1350 0, /* sq_inplace_concat */
1351 0, /* sq_inplace_repeat */
1352};
1353
1354static PyTypeObject LogReaderType = {
1355 PyObject_HEAD_INIT(NULL)
1356 0, /* ob_size */
Fred Drake4c2e1af2001-10-29 20:45:57 +00001357 "_hotshot.LogReaderType", /* tp_name */
Fred Drake8c081a12001-10-12 20:57:55 +00001358 (int) sizeof(LogReaderObject), /* tp_basicsize */
1359 0, /* tp_itemsize */
1360 (destructor)logreader_dealloc, /* tp_dealloc */
1361 0, /* tp_print */
1362 (getattrfunc)logreader_getattr, /* tp_getattr */
1363 0, /* tp_setattr */
1364 0, /* tp_compare */
1365 0, /* tp_repr */
1366 0, /* tp_as_number */
1367 &logreader_as_sequence, /* tp_as_sequence */
1368 0, /* tp_as_mapping */
1369 0, /* tp_hash */
1370 0, /* tp_call */
1371 0, /* tp_str */
1372 0, /* tp_getattro */
1373 0, /* tp_setattro */
1374 0, /* tp_as_buffer */
Fred Drake4c2e1af2001-10-29 20:45:57 +00001375 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake8c081a12001-10-12 20:57:55 +00001376 logreader__doc__, /* tp_doc */
1377#if Py_TPFLAGS_HAVE_ITER
1378 0, /* tp_traverse */
1379 0, /* tp_clear */
1380 0, /* tp_richcompare */
1381 0, /* tp_weaklistoffset */
1382 (getiterfunc)logreader_tp_iter, /* tp_iter */
1383 (iternextfunc)logreader_tp_iternext,/* tp_iternext */
1384#endif
1385};
1386
1387static PyObject *
1388hotshot_logreader(PyObject *unused, PyObject *args)
1389{
1390 LogReaderObject *self = NULL;
1391 char *filename;
1392
1393 if (PyArg_ParseTuple(args, "s:logreader", &filename)) {
1394 self = PyObject_New(LogReaderObject, &LogReaderType);
1395 if (self != NULL) {
1396 self->filled = 0;
1397 self->index = 0;
Fred Drake30d1c752001-10-15 22:11:02 +00001398 self->frametimings = 1;
Fred Drake8c081a12001-10-12 20:57:55 +00001399 self->linetimings = 0;
Fred Drake4c2e1af2001-10-29 20:45:57 +00001400 self->info = NULL;
Fred Drake8c081a12001-10-12 20:57:55 +00001401 self->logfp = fopen(filename, "rb");
1402 if (self->logfp == NULL) {
1403 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
1404 Py_DECREF(self);
1405 self = NULL;
Fred Drake4c2e1af2001-10-29 20:45:57 +00001406 goto finally;
1407 }
1408 self->info = PyDict_New();
1409 if (self->info == NULL) {
1410 Py_DECREF(self);
1411 goto finally;
1412 }
1413 /* Aggressively attempt to load all preliminary ADD_INFO
1414 * records from the log so the info records are available
1415 * from a fresh logreader object.
1416 */
1417 logreader_refill(self);
1418 while (self->filled > self->index
1419 && self->buffer[self->index] == WHAT_ADD_INFO) {
1420 int err = unpack_add_info(self, 1);
1421 if (err) {
1422 if (err == ERR_EOF)
1423 eof_error();
1424 else
1425 PyErr_SetString(PyExc_RuntimeError,
1426 "unexpected error");
1427 break;
1428 }
1429 /* Refill agressively so we can avoid EOF during
1430 * initialization unless there's a real EOF condition
1431 * (the tp_iternext handler loops attempts to refill
1432 * and try again).
1433 */
1434 logreader_refill(self);
Fred Drake8c081a12001-10-12 20:57:55 +00001435 }
1436 }
1437 }
Fred Drake4c2e1af2001-10-29 20:45:57 +00001438 finally:
Fred Drake8c081a12001-10-12 20:57:55 +00001439 return (PyObject *) self;
1440}
1441
1442
1443/* Return a Python string that represents the version number without the
1444 * extra cruft added by revision control, even if the right options were
1445 * given to the "cvs export" command to make it not include the extra
1446 * cruft.
1447 */
1448static char *
1449get_version_string(void)
1450{
1451 static char *rcsid = "$Revision$";
1452 char *rev = rcsid;
1453 char *buffer;
1454 int i = 0;
1455
Neal Norwitz3afb2d22002-03-20 21:32:07 +00001456 while (*rev && !isdigit((int)*rev))
Fred Drake8c081a12001-10-12 20:57:55 +00001457 ++rev;
1458 while (rev[i] != ' ' && rev[i] != '\0')
1459 ++i;
1460 buffer = malloc(i + 1);
1461 if (buffer != NULL) {
1462 memmove(buffer, rev, i);
1463 buffer[i] = '\0';
1464 }
1465 return buffer;
1466}
1467
1468/* Write out a RFC 822-style header with various useful bits of
1469 * information to make the output easier to manage.
1470 */
1471static int
1472write_header(ProfilerObject *self)
1473{
1474 char *buffer;
1475 char cwdbuffer[PATH_MAX];
1476 PyObject *temp;
1477 int i, len;
1478
1479 buffer = get_version_string();
1480 if (buffer == NULL) {
1481 PyErr_NoMemory();
1482 return -1;
1483 }
Fred Drake4c2e1af2001-10-29 20:45:57 +00001484 pack_add_info(self, "hotshot-version", buffer);
1485 pack_add_info(self, "requested-frame-timings",
1486 (self->frametimings ? "yes" : "no"));
1487 pack_add_info(self, "requested-line-events",
Fred Drake8c081a12001-10-12 20:57:55 +00001488 (self->lineevents ? "yes" : "no"));
Fred Drake4c2e1af2001-10-29 20:45:57 +00001489 pack_add_info(self, "requested-line-timings",
1490 (self->linetimings ? "yes" : "no"));
1491 pack_add_info(self, "platform", Py_GetPlatform());
1492 pack_add_info(self, "executable", Py_GetProgramFullPath());
Fred Drakef12a68c2001-11-09 15:59:36 +00001493 free(buffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001494 buffer = (char *) Py_GetVersion();
1495 if (buffer == NULL)
1496 PyErr_Clear();
1497 else
Fred Drake4c2e1af2001-10-29 20:45:57 +00001498 pack_add_info(self, "executable-version", buffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001499
1500#ifdef MS_WIN32
Tim Peters885d4572001-11-28 20:27:42 +00001501 PyOS_snprintf(cwdbuffer, sizeof(cwdbuffer), "%I64d", frequency.QuadPart);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001502 pack_add_info(self, "reported-performance-frequency", cwdbuffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001503#else
Tim Peters885d4572001-11-28 20:27:42 +00001504 PyOS_snprintf(cwdbuffer, sizeof(cwdbuffer), "%lu", rusage_diff);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001505 pack_add_info(self, "observed-interval-getrusage", cwdbuffer);
Tim Peters885d4572001-11-28 20:27:42 +00001506 PyOS_snprintf(cwdbuffer, sizeof(cwdbuffer), "%lu", timeofday_diff);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001507 pack_add_info(self, "observed-interval-gettimeofday", cwdbuffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001508#endif
Fred Drake8c081a12001-10-12 20:57:55 +00001509
Fred Drake4c2e1af2001-10-29 20:45:57 +00001510 pack_add_info(self, "current-directory",
Fred Drake8c081a12001-10-12 20:57:55 +00001511 getcwd(cwdbuffer, sizeof cwdbuffer));
1512
1513 temp = PySys_GetObject("path");
1514 len = PyList_GET_SIZE(temp);
1515 for (i = 0; i < len; ++i) {
1516 PyObject *item = PyList_GET_ITEM(temp, i);
1517 buffer = PyString_AsString(item);
1518 if (buffer == NULL)
1519 return -1;
Fred Drake4c2e1af2001-10-29 20:45:57 +00001520 pack_add_info(self, "sys-path-entry", buffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001521 }
Fred Drake4c2e1af2001-10-29 20:45:57 +00001522 pack_frame_times(self);
1523 pack_line_times(self);
1524
Fred Drake8c081a12001-10-12 20:57:55 +00001525 return 0;
1526}
1527
1528static char profiler__doc__[] = "\
1529profiler(logfilename[, lineevents[, linetimes]]) -> profiler\n\
1530Create a new profiler object.";
1531
1532static PyObject *
1533hotshot_profiler(PyObject *unused, PyObject *args)
1534{
1535 char *logfilename;
1536 ProfilerObject *self = NULL;
1537 int lineevents = 0;
1538 int linetimings = 1;
1539
1540 if (PyArg_ParseTuple(args, "s|ii:profiler", &logfilename,
1541 &lineevents, &linetimings)) {
1542 self = PyObject_New(ProfilerObject, &ProfilerType);
1543 if (self == NULL)
1544 return NULL;
Fred Drake30d1c752001-10-15 22:11:02 +00001545 self->frametimings = 1;
Fred Drake8c081a12001-10-12 20:57:55 +00001546 self->lineevents = lineevents ? 1 : 0;
1547 self->linetimings = (lineevents && linetimings) ? 1 : 0;
1548 self->index = 0;
1549 self->active = 0;
1550 self->next_fileno = 0;
Tim Peters1566a172001-10-12 22:08:39 +00001551 self->logfp = NULL;
Fred Drake8c081a12001-10-12 20:57:55 +00001552 self->logfilename = PyTuple_GET_ITEM(args, 0);
1553 Py_INCREF(self->logfilename);
1554 self->filemap = PyDict_New();
1555 if (self->filemap == NULL) {
1556 Py_DECREF(self);
1557 return NULL;
1558 }
1559 self->logfp = fopen(logfilename, "wb");
1560 if (self->logfp == NULL) {
1561 Py_DECREF(self);
1562 PyErr_SetFromErrnoWithFilename(PyExc_IOError, logfilename);
1563 return NULL;
1564 }
1565 if (timeofday_diff == 0) {
1566 /* Run this several times since sometimes the first
1567 * doesn't give the lowest values, and we're really trying
1568 * to determine the lowest.
1569 */
1570 calibrate();
1571 calibrate();
1572 calibrate();
1573 }
1574 if (write_header(self))
1575 /* some error occurred, exception has been set */
1576 self = NULL;
1577 }
1578 return (PyObject *) self;
1579}
1580
Fred Drake30d1c752001-10-15 22:11:02 +00001581static char coverage__doc__[] = "\
1582coverage(logfilename) -> profiler\n\
1583Returns a profiler that doesn't collect any timing information, which is\n\
1584useful in building a coverage analysis tool.";
1585
1586static PyObject *
1587hotshot_coverage(PyObject *unused, PyObject *args)
1588{
1589 char *logfilename;
1590 PyObject *result = NULL;
1591
1592 if (PyArg_ParseTuple(args, "s:coverage", &logfilename)) {
1593 result = hotshot_profiler(unused, args);
1594 if (result != NULL) {
1595 ProfilerObject *self = (ProfilerObject *) result;
1596 self->frametimings = 0;
1597 self->linetimings = 0;
1598 self->lineevents = 1;
1599 }
1600 }
1601 return result;
1602}
1603
Fred Drake8c081a12001-10-12 20:57:55 +00001604static char resolution__doc__[] =
1605#ifdef MS_WIN32
Tim Petersfeab23f2001-10-13 00:11:10 +00001606"resolution() -> (performance-counter-ticks, update-frequency)\n"
1607"Return the resolution of the timer provided by the QueryPerformanceCounter()\n"
1608"function. The first value is the smallest observed change, and the second\n"
1609"is the result of QueryPerformanceFrequency().";
Fred Drake8c081a12001-10-12 20:57:55 +00001610#else
Tim Petersfeab23f2001-10-13 00:11:10 +00001611"resolution() -> (gettimeofday-usecs, getrusage-usecs)\n"
1612"Return the resolution of the timers provided by the gettimeofday() and\n"
1613"getrusage() system calls, or -1 if the call is not supported.";
Fred Drake8c081a12001-10-12 20:57:55 +00001614#endif
1615
1616static PyObject *
1617hotshot_resolution(PyObject *unused, PyObject *args)
1618{
1619 PyObject *result = NULL;
1620
1621 if (PyArg_ParseTuple(args, ":resolution")) {
1622 if (timeofday_diff == 0) {
1623 calibrate();
1624 calibrate();
1625 calibrate();
1626 }
1627#ifdef MS_WIN32
1628 result = Py_BuildValue("ii", timeofday_diff, frequency.LowPart);
1629#else
1630 result = Py_BuildValue("ii", timeofday_diff, rusage_diff);
1631#endif
1632 }
1633 return result;
1634}
1635
1636
1637static PyMethodDef functions[] = {
Fred Drake30d1c752001-10-15 22:11:02 +00001638 {"coverage", hotshot_coverage, METH_VARARGS, coverage__doc__},
Fred Drake8c081a12001-10-12 20:57:55 +00001639 {"profiler", hotshot_profiler, METH_VARARGS, profiler__doc__},
1640 {"logreader", hotshot_logreader, METH_VARARGS, logreader__doc__},
1641 {"resolution", hotshot_resolution, METH_VARARGS, resolution__doc__},
1642 {NULL, NULL}
1643};
1644
1645
1646void
1647init_hotshot(void)
1648{
1649 PyObject *module;
1650
1651 LogReaderType.ob_type = &PyType_Type;
1652 ProfilerType.ob_type = &PyType_Type;
1653 module = Py_InitModule("_hotshot", functions);
1654 if (module != NULL) {
1655 char *s = get_version_string();
1656
1657 PyModule_AddStringConstant(module, "__version__", s);
1658 free(s);
1659 Py_INCREF(&LogReaderType);
1660 PyModule_AddObject(module, "LogReaderType",
1661 (PyObject *)&LogReaderType);
1662 Py_INCREF(&ProfilerType);
1663 PyModule_AddObject(module, "ProfilerType",
1664 (PyObject *)&ProfilerType);
1665
1666 if (ProfilerError == NULL)
1667 ProfilerError = PyErr_NewException("hotshot.ProfilerError",
1668 NULL, NULL);
1669 if (ProfilerError != NULL) {
1670 Py_INCREF(ProfilerError);
1671 PyModule_AddObject(module, "ProfilerError", ProfilerError);
1672 }
1673 PyModule_AddIntConstant(module, "WHAT_ENTER", WHAT_ENTER);
1674 PyModule_AddIntConstant(module, "WHAT_EXIT", WHAT_EXIT);
1675 PyModule_AddIntConstant(module, "WHAT_LINENO", WHAT_LINENO);
1676 PyModule_AddIntConstant(module, "WHAT_OTHER", WHAT_OTHER);
1677 PyModule_AddIntConstant(module, "WHAT_ADD_INFO", WHAT_ADD_INFO);
1678 PyModule_AddIntConstant(module, "WHAT_DEFINE_FILE", WHAT_DEFINE_FILE);
Fred Drake30d1c752001-10-15 22:11:02 +00001679 PyModule_AddIntConstant(module, "WHAT_DEFINE_FUNC", WHAT_DEFINE_FUNC);
Fred Drake8c081a12001-10-12 20:57:55 +00001680 PyModule_AddIntConstant(module, "WHAT_LINE_TIMES", WHAT_LINE_TIMES);
1681 }
1682}