This commit was manufactured by cvs2svn to create tag 'r22b2-mac'.
[python/dscho.git] / Modules / _hotshot.c
blob4db23f87f1733186afc89355c843dda2f0355419
1 /*
2 * This is the High Performance Python Profiler portion of HotShot.
3 */
5 #include "Python.h"
6 #include "compile.h"
7 #include "eval.h"
8 #include "frameobject.h"
9 #include "structmember.h"
11 #ifdef HAVE_UNISTD_H
12 #include <unistd.h>
13 #endif
16 * Which timer to use should be made more configurable, but that should not
17 * be difficult. This will do for now.
19 #ifdef MS_WIN32
20 #include <windows.h>
21 #include <largeint.h>
22 #include <direct.h> /* for getcwd() */
23 typedef __int64 hs_time;
24 #define GETTIMEOFDAY(P_HS_TIME) \
25 { LARGE_INTEGER _temp; \
26 QueryPerformanceCounter(&_temp); \
27 *(P_HS_TIME) = _temp.QuadPart; }
30 #else
31 #ifndef HAVE_GETTIMEOFDAY
32 #error "This module requires gettimeofday() on non-Windows platforms!"
33 #endif
34 #ifdef macintosh
35 #include <sys/time.h>
36 #else
37 #include <sys/resource.h>
38 #include <sys/times.h>
39 #endif
40 typedef struct timeval hs_time;
41 #endif
43 #if !defined(__cplusplus) && !defined(inline)
44 #ifdef __GNUC__
45 #define inline __inline
46 #endif
47 #endif
49 #ifndef inline
50 #define inline
51 #endif
53 #define BUFFERSIZE 10240
55 #ifdef macintosh
56 #define PATH_MAX 254
57 #endif
59 #ifndef PATH_MAX
60 # ifdef MAX_PATH
61 # define PATH_MAX MAX_PATH
62 # else
63 # error "Need a defn. for PATH_MAX in _hotshot.c"
64 # endif
65 #endif
67 typedef struct {
68 PyObject_HEAD
69 PyObject *filemap;
70 PyObject *logfilename;
71 int index;
72 unsigned char buffer[BUFFERSIZE];
73 FILE *logfp;
74 int lineevents;
75 int linetimings;
76 int frametimings;
77 /* size_t filled; */
78 int active;
79 int next_fileno;
80 hs_time prev_timeofday;
81 } ProfilerObject;
83 typedef struct {
84 PyObject_HEAD
85 PyObject *info;
86 FILE *logfp;
87 int filled;
88 int index;
89 int linetimings;
90 int frametimings;
91 unsigned char buffer[BUFFERSIZE];
92 } LogReaderObject;
94 static PyObject * ProfilerError = NULL;
97 #ifndef MS_WIN32
98 #ifdef GETTIMEOFDAY_NO_TZ
99 #define GETTIMEOFDAY(ptv) gettimeofday((ptv))
100 #else
101 #define GETTIMEOFDAY(ptv) gettimeofday((ptv), (struct timezone *)NULL)
102 #endif
103 #endif
106 /* The log reader... */
108 static char logreader_close__doc__[] =
109 "close()\n"
110 "Close the log file, preventing additional records from being read.";
112 static PyObject *
113 logreader_close(LogReaderObject *self, PyObject *args)
115 PyObject *result = NULL;
116 if (PyArg_ParseTuple(args, ":close")) {
117 if (self->logfp != NULL) {
118 fclose(self->logfp);
119 self->logfp = NULL;
121 result = Py_None;
122 Py_INCREF(result);
124 return result;
127 #if Py_TPFLAGS_HAVE_ITER
128 /* This is only used if the interpreter has iterator support; the
129 * iternext handler is also used as a helper for other functions, so
130 * does not need to be included in this conditional section.
132 static PyObject *
133 logreader_tp_iter(LogReaderObject *self)
135 Py_INCREF(self);
136 return (PyObject *) self;
138 #endif
141 /* Log File Format
142 * ---------------
144 * The log file consists of a sequence of variable-length records.
145 * Each record is identified with a record type identifier in two
146 * bits of the first byte. The two bits are the "least significant"
147 * bits of the byte.
149 * Low bits: Opcode: Meaning:
150 * 0x00 ENTER enter a frame
151 * 0x01 EXIT exit a frame
152 * 0x02 LINENO SET_LINENO instruction was executed
153 * 0x03 OTHER more bits are needed to deecode
155 * If the type is OTHER, the record is not packed so tightly, and the
156 * remaining bits are used to disambiguate the record type. These
157 * records are not used as frequently so compaction is not an issue.
158 * Each of the first three record types has a highly tailored
159 * structure that allows it to be packed tightly.
161 * The OTHER records have the following identifiers:
163 * First byte: Opcode: Meaning:
164 * 0x13 ADD_INFO define a key/value pair
165 * 0x23 DEFINE_FILE define an int->filename mapping
166 * 0x33 LINE_TIMES indicates if LINENO events have tdeltas
167 * 0x43 DEFINE_FUNC define a (fileno,lineno)->funcname mapping
168 * 0x53 FRAME_TIMES indicates if ENTER/EXIT events have tdeltas
170 * Packed Integers
172 * "Packed integers" are non-negative integer values encoded as a
173 * sequence of bytes. Each byte is encoded such that the most
174 * significant bit is set if the next byte is also part of the
175 * integer. Each byte provides bits to the least-significant end of
176 * the result; the accumulated value must be shifted up to place the
177 * new bits into the result.
179 * "Modified packed integers" are packed integers where only a portion
180 * of the first byte is used. In the rest of the specification, these
181 * are referred to as "MPI(n,name)", where "n" is the number of bits
182 * discarded from the least-signicant positions of the byte, and
183 * "name" is a name being given to those "discarded" bits, since they
184 * are a field themselves.
186 * ENTER records:
188 * MPI(2,type) fileno -- type is 0x00
189 * PI lineno
190 * PI tdelta -- iff frame times are enabled
192 * EXIT records
194 * MPI(2,type) tdelta -- type is 0x01; tdelta will be 0
195 * if frame times are disabled
197 * LINENO records
199 * MPI(2,type) lineno -- type is 0x02
200 * PI tdelta -- iff LINENO includes it
202 * ADD_INFO records
204 * BYTE type -- always 0x13
205 * PI len1 -- length of first string
206 * BYTE string1[len1] -- len1 bytes of string data
207 * PI len2 -- length of second string
208 * BYTE string2[len2] -- len2 bytes of string data
210 * DEFINE_FILE records
212 * BYTE type -- always 0x23
213 * PI fileno
214 * PI len -- length of filename
215 * BYTE filename[len] -- len bytes of string data
217 * DEFINE_FUNC records
219 * BYTE type -- always 0x43
220 * PI fileno
221 * PI lineno
222 * PI len -- length of funcname
223 * BYTE funcname[len] -- len bytes of string data
225 * LINE_TIMES records
227 * This record can be used only before the start of ENTER/EXIT/LINENO
228 * records. If have_tdelta is true, LINENO records will include the
229 * tdelta field, otherwise it will be omitted. If this record is not
230 * given, LINENO records will not contain the tdelta field.
232 * BYTE type -- always 0x33
233 * BYTE have_tdelta -- 0 if LINENO does *not* have
234 * timing information
235 * FRAME_TIMES records
237 * This record can be used only before the start of ENTER/EXIT/LINENO
238 * records. If have_tdelta is true, ENTER and EXIT records will
239 * include the tdelta field, otherwise it will be omitted. If this
240 * record is not given, ENTER and EXIT records will contain the tdelta
241 * field.
243 * BYTE type -- always 0x53
244 * BYTE have_tdelta -- 0 if ENTER/EXIT do *not* have
245 * timing information
248 #define WHAT_ENTER 0x00
249 #define WHAT_EXIT 0x01
250 #define WHAT_LINENO 0x02
251 #define WHAT_OTHER 0x03 /* only used in decoding */
252 #define WHAT_ADD_INFO 0x13
253 #define WHAT_DEFINE_FILE 0x23
254 #define WHAT_LINE_TIMES 0x33
255 #define WHAT_DEFINE_FUNC 0x43
256 #define WHAT_FRAME_TIMES 0x53
258 #define ERR_NONE 0
259 #define ERR_EOF -1
260 #define ERR_EXCEPTION -2
261 #define ERR_BAD_RECTYPE -3
263 #define PISIZE (sizeof(int) + 1)
264 #define MPISIZE (PISIZE + 1)
266 /* Maximum size of "normal" events -- nothing that contains string data */
267 #define MAXEVENTSIZE (MPISIZE + PISIZE*2)
270 /* Unpack a packed integer; if "discard" is non-zero, unpack a modified
271 * packed integer with "discard" discarded bits.
273 static int
274 unpack_packed_int(LogReaderObject *self, int *pvalue, int discard)
276 int accum = 0;
277 int bits = 0;
278 int index = self->index;
279 int cont;
281 do {
282 if (index >= self->filled)
283 return ERR_EOF;
284 /* read byte */
285 accum |= ((self->buffer[index] & 0x7F) >> discard) << bits;
286 bits += (7 - discard);
287 cont = self->buffer[index] & 0x80;
288 /* move to next */
289 discard = 0;
290 index++;
291 } while (cont);
293 /* save state */
294 self->index = index;
295 *pvalue = accum;
297 return 0;
300 /* Unpack a string, which is encoded as a packed integer giving the
301 * length of the string, followed by the string data.
303 static int
304 unpack_string(LogReaderObject *self, PyObject **pvalue)
306 int len;
307 int oldindex = self->index;
308 int err = unpack_packed_int(self, &len, 0);
310 if (!err) {
311 /* need at least len bytes in buffer */
312 if (len > (self->filled - self->index)) {
313 self->index = oldindex;
314 err = ERR_EOF;
316 else {
317 *pvalue = PyString_FromStringAndSize((char *)self->buffer + self->index,
318 len);
319 if (*pvalue == NULL) {
320 self->index = oldindex;
321 err = ERR_EXCEPTION;
323 else
324 self->index += len;
327 return err;
331 static int
332 unpack_add_info(LogReaderObject *self, int skip_opcode)
334 PyObject *key;
335 PyObject *value = NULL;
336 int err;
338 if (skip_opcode) {
339 if (self->buffer[self->index] != WHAT_ADD_INFO)
340 return ERR_BAD_RECTYPE;
341 self->index++;
343 err = unpack_string(self, &key);
344 if (!err) {
345 err = unpack_string(self, &value);
346 if (err)
347 Py_DECREF(key);
348 else {
349 PyObject *list = PyDict_GetItem(self->info, key);
350 if (list == NULL) {
351 list = PyList_New(0);
352 if (list == NULL) {
353 err = ERR_EXCEPTION;
354 goto finally;
356 if (PyDict_SetItem(self->info, key, list)) {
357 err = ERR_EXCEPTION;
358 goto finally;
361 if (PyList_Append(list, value))
362 err = ERR_EXCEPTION;
365 finally:
366 Py_XDECREF(key);
367 Py_XDECREF(value);
368 return err;
372 static void
373 logreader_refill(LogReaderObject *self)
375 int needed;
376 size_t res;
378 if (self->index) {
379 memmove(self->buffer, &self->buffer[self->index],
380 self->filled - self->index);
381 self->filled = self->filled - self->index;
382 self->index = 0;
384 needed = BUFFERSIZE - self->filled;
385 if (needed > 0) {
386 res = fread(&self->buffer[self->filled], 1, needed, self->logfp);
387 self->filled += res;
391 static void
392 eof_error(void)
394 PyErr_SetString(PyExc_EOFError,
395 "end of file with incomplete profile record");
398 static PyObject *
399 logreader_tp_iternext(LogReaderObject *self)
401 int what, oldindex;
402 int err = ERR_NONE;
403 int lineno = -1;
404 int fileno = -1;
405 int tdelta = -1;
406 PyObject *s1 = NULL, *s2 = NULL;
407 PyObject *result = NULL;
408 #if 0
409 unsigned char b0, b1;
410 #endif
412 if (self->logfp == NULL) {
413 PyErr_SetString(ProfilerError,
414 "cannot iterate over closed LogReader object");
415 return NULL;
417 restart:
418 if ((self->filled - self->index) < MAXEVENTSIZE)
419 logreader_refill(self);
421 /* end of input */
422 if (self->filled == 0)
423 return NULL;
425 oldindex = self->index;
427 /* decode the record type */
428 what = self->buffer[self->index] & WHAT_OTHER;
429 if (what == WHAT_OTHER) {
430 what = self->buffer[self->index];
431 self->index++;
433 switch (what) {
434 case WHAT_ENTER:
435 err = unpack_packed_int(self, &fileno, 2);
436 if (!err) {
437 err = unpack_packed_int(self, &lineno, 0);
438 if (self->frametimings && !err)
439 err = unpack_packed_int(self, &tdelta, 0);
441 break;
442 case WHAT_EXIT:
443 err = unpack_packed_int(self, &tdelta, 2);
444 break;
445 case WHAT_LINENO:
446 err = unpack_packed_int(self, &lineno, 2);
447 if (self->linetimings && !err)
448 err = unpack_packed_int(self, &tdelta, 0);
449 break;
450 case WHAT_ADD_INFO:
451 err = unpack_add_info(self, 0);
452 break;
453 case WHAT_DEFINE_FILE:
454 err = unpack_packed_int(self, &fileno, 0);
455 if (!err) {
456 err = unpack_string(self, &s1);
457 if (!err) {
458 Py_INCREF(Py_None);
459 s2 = Py_None;
462 break;
463 case WHAT_DEFINE_FUNC:
464 err = unpack_packed_int(self, &fileno, 0);
465 if (!err) {
466 err = unpack_packed_int(self, &lineno, 0);
467 if (!err)
468 err = unpack_string(self, &s1);
470 break;
471 case WHAT_LINE_TIMES:
472 if (self->index >= self->filled)
473 err = ERR_EOF;
474 else {
475 self->linetimings = self->buffer[self->index] ? 1 : 0;
476 self->index++;
477 goto restart;
479 break;
480 case WHAT_FRAME_TIMES:
481 if (self->index >= self->filled)
482 err = ERR_EOF;
483 else {
484 self->frametimings = self->buffer[self->index] ? 1 : 0;
485 self->index++;
486 goto restart;
488 break;
489 default:
490 err = ERR_BAD_RECTYPE;
492 if (err == ERR_EOF && oldindex != 0) {
493 /* It looks like we ran out of data before we had it all; this
494 * could easily happen with large packed integers or string
495 * data. Try forcing the buffer to be re-filled before failing.
497 err = ERR_NONE;
498 logreader_refill(self);
500 if (err == ERR_BAD_RECTYPE) {
501 PyErr_SetString(PyExc_ValueError,
502 "unknown record type in log file");
504 else if (err == ERR_EOF) {
505 /* Could not avoid end-of-buffer error. */
506 eof_error();
508 else if (!err) {
509 result = PyTuple_New(4);
510 PyTuple_SET_ITEM(result, 0, PyInt_FromLong(what));
511 PyTuple_SET_ITEM(result, 2, PyInt_FromLong(fileno));
512 if (s1 == NULL)
513 PyTuple_SET_ITEM(result, 1, PyInt_FromLong(tdelta));
514 else
515 PyTuple_SET_ITEM(result, 1, s1);
516 if (s2 == NULL)
517 PyTuple_SET_ITEM(result, 3, PyInt_FromLong(lineno));
518 else
519 PyTuple_SET_ITEM(result, 3, s2);
521 /* The only other case is err == ERR_EXCEPTION, in which case the
522 * exception is already set.
524 #if 0
525 b0 = self->buffer[self->index];
526 b1 = self->buffer[self->index + 1];
527 if (b0 & 1) {
528 /* This is a line-number event. */
529 what = PyTrace_LINE;
530 lineno = ((b0 & ~1) << 7) + b1;
531 self->index += 2;
533 else {
534 what = (b0 & 0x0E) >> 1;
535 tdelta = ((b0 & 0xF0) << 4) + b1;
536 if (what == PyTrace_CALL) {
537 /* we know there's a 2-byte file ID & 2-byte line number */
538 fileno = ((self->buffer[self->index + 2] << 8)
539 + self->buffer[self->index + 3]);
540 lineno = ((self->buffer[self->index + 4] << 8)
541 + self->buffer[self->index + 5]);
542 self->index += 6;
544 else
545 self->index += 2;
547 #endif
548 return result;
551 static void
552 logreader_dealloc(LogReaderObject *self)
554 if (self->logfp != NULL) {
555 fclose(self->logfp);
556 self->logfp = NULL;
558 PyObject_Del(self);
561 static PyObject *
562 logreader_sq_item(LogReaderObject *self, int index)
564 PyObject *result = logreader_tp_iternext(self);
565 if (result == NULL && !PyErr_Occurred()) {
566 PyErr_SetString(PyExc_IndexError, "no more events in log");
567 return NULL;
569 return result;
572 static char next__doc__[] =
573 "next() -> event-info\n"
574 "Return the next event record from the log file.";
576 static PyObject *
577 logreader_next(LogReaderObject *self, PyObject *args)
579 PyObject *result = NULL;
581 if (PyArg_ParseTuple(args, ":next")) {
582 result = logreader_tp_iternext(self);
583 /* XXX return None if there's nothing left */
584 /* tp_iternext does the right thing, though */
585 if (result == NULL && !PyErr_Occurred()) {
586 result = Py_None;
587 Py_INCREF(result);
590 return result;
594 static int
595 flush_data(ProfilerObject *self)
597 /* Need to dump data to the log file... */
598 size_t written = fwrite(self->buffer, 1, self->index, self->logfp);
599 if (written == (size_t)self->index)
600 self->index = 0;
601 else {
602 memmove(self->buffer, &self->buffer[written],
603 self->index - written);
604 self->index -= written;
605 if (written == 0) {
606 char *s = PyString_AsString(self->logfilename);
607 PyErr_SetFromErrnoWithFilename(PyExc_IOError, s);
608 return -1;
611 if (written > 0) {
612 if (fflush(self->logfp)) {
613 char *s = PyString_AsString(self->logfilename);
614 PyErr_SetFromErrnoWithFilename(PyExc_IOError, s);
615 return -1;
618 return 0;
621 static inline void
622 pack_packed_int(ProfilerObject *self, int value)
624 unsigned char partial;
626 do {
627 partial = value & 0x7F;
628 value >>= 7;
629 if (value)
630 partial |= 0x80;
631 self->buffer[self->index] = partial;
632 self->index++;
633 } while (value);
636 /* Encode a modified packed integer, with a subfield of modsize bits
637 * containing the value "subfield". The value of subfield is not
638 * checked to ensure it actually fits in modsize bits.
640 static inline void
641 pack_modified_packed_int(ProfilerObject *self, int value,
642 int modsize, int subfield)
644 const int maxvalues[] = {-1, 1, 3, 7, 15, 31, 63, 127};
646 int bits = 7 - modsize;
647 int partial = value & maxvalues[bits];
648 unsigned char b = subfield | (partial << modsize);
650 if (partial != value) {
651 b |= 0x80;
652 self->buffer[self->index] = b;
653 self->index++;
654 pack_packed_int(self, value >> bits);
656 else {
657 self->buffer[self->index] = b;
658 self->index++;
662 static void
663 pack_string(ProfilerObject *self, const char *s, int len)
665 if (len + PISIZE + self->index >= BUFFERSIZE)
666 (void) flush_data(self);
667 pack_packed_int(self, len);
668 memcpy(self->buffer + self->index, s, len);
669 self->index += len;
672 static void
673 pack_add_info(ProfilerObject *self, const char *s1, const char *s2)
675 int len1 = strlen(s1);
676 int len2 = strlen(s2);
678 if (len1 + len2 + PISIZE*2 + 1 + self->index >= BUFFERSIZE)
679 (void) flush_data(self);
680 self->buffer[self->index] = WHAT_ADD_INFO;
681 self->index++;
682 pack_string(self, s1, len1);
683 pack_string(self, s2, len2);
686 static void
687 pack_define_file(ProfilerObject *self, int fileno, const char *filename)
689 int len = strlen(filename);
691 if (len + PISIZE*2 + 1 + self->index >= BUFFERSIZE)
692 (void) flush_data(self);
693 self->buffer[self->index] = WHAT_DEFINE_FILE;
694 self->index++;
695 pack_packed_int(self, fileno);
696 pack_string(self, filename, len);
699 static void
700 pack_define_func(ProfilerObject *self, int fileno, int lineno,
701 const char *funcname)
703 int len = strlen(funcname);
705 if (len + PISIZE*3 + 1 + self->index >= BUFFERSIZE)
706 (void) flush_data(self);
707 self->buffer[self->index] = WHAT_DEFINE_FUNC;
708 self->index++;
709 pack_packed_int(self, fileno);
710 pack_packed_int(self, lineno);
711 pack_string(self, funcname, len);
714 static void
715 pack_line_times(ProfilerObject *self)
717 if (2 + self->index >= BUFFERSIZE)
718 (void) flush_data(self);
719 self->buffer[self->index] = WHAT_LINE_TIMES;
720 self->buffer[self->index + 1] = self->linetimings ? 1 : 0;
721 self->index += 2;
724 static void
725 pack_frame_times(ProfilerObject *self)
727 if (2 + self->index >= BUFFERSIZE)
728 (void) flush_data(self);
729 self->buffer[self->index] = WHAT_FRAME_TIMES;
730 self->buffer[self->index + 1] = self->frametimings ? 1 : 0;
731 self->index += 2;
734 static inline void
735 pack_enter(ProfilerObject *self, int fileno, int tdelta, int lineno)
737 if (MPISIZE + PISIZE*2 + self->index >= BUFFERSIZE)
738 (void) flush_data(self);
739 pack_modified_packed_int(self, fileno, 2, WHAT_ENTER);
740 pack_packed_int(self, lineno);
741 if (self->frametimings)
742 pack_packed_int(self, tdelta);
745 static inline void
746 pack_exit(ProfilerObject *self, int tdelta)
748 if (MPISIZE + self->index >= BUFFERSIZE)
749 (void) flush_data(self);
750 if (self->frametimings)
751 pack_modified_packed_int(self, tdelta, 2, WHAT_EXIT);
752 else {
753 self->buffer[self->index] = WHAT_EXIT;
754 self->index++;
758 static inline void
759 pack_lineno(ProfilerObject *self, int lineno)
761 if (MPISIZE + self->index >= BUFFERSIZE)
762 (void) flush_data(self);
763 pack_modified_packed_int(self, lineno, 2, WHAT_LINENO);
766 static inline void
767 pack_lineno_tdelta(ProfilerObject *self, int lineno, int tdelta)
769 if (MPISIZE + PISIZE + self->index >= BUFFERSIZE)
770 (void) flush_data(self);
771 pack_modified_packed_int(self, lineno, 2, WHAT_LINENO);
772 pack_packed_int(self, tdelta);
775 static inline int
776 get_fileno(ProfilerObject *self, PyCodeObject *fcode)
778 /* This is only used for ENTER events. */
780 PyObject *obj;
781 PyObject *dict;
782 int fileno;
784 obj = PyDict_GetItem(self->filemap, fcode->co_filename);
785 if (obj == NULL) {
786 /* first sighting of this file */
787 dict = PyDict_New();
788 if (dict == NULL) {
789 return -1;
791 fileno = self->next_fileno;
792 obj = Py_BuildValue("iN", fileno, dict);
793 if (obj == NULL) {
794 return -1;
796 if (PyDict_SetItem(self->filemap, fcode->co_filename, obj)) {
797 Py_DECREF(obj);
798 return -1;
800 self->next_fileno++;
801 Py_DECREF(obj);
802 pack_define_file(self, fileno, PyString_AS_STRING(fcode->co_filename));
804 else {
805 /* already know this ID */
806 fileno = PyInt_AS_LONG(PyTuple_GET_ITEM(obj, 0));
807 dict = PyTuple_GET_ITEM(obj, 1);
809 /* make sure we save a function name for this (fileno, lineno) */
810 obj = PyInt_FromLong(fcode->co_firstlineno);
811 if (obj == NULL) {
812 /* We just won't have it saved; too bad. */
813 PyErr_Clear();
815 else {
816 PyObject *name = PyDict_GetItem(dict, obj);
817 if (name == NULL) {
818 pack_define_func(self, fileno, fcode->co_firstlineno,
819 PyString_AS_STRING(fcode->co_name));
820 if (PyDict_SetItem(dict, obj, fcode->co_name))
821 return -1;
824 return fileno;
827 static inline int
828 get_tdelta(ProfilerObject *self)
830 int tdelta;
831 #ifdef MS_WIN32
832 hs_time tv;
833 hs_time diff;
835 GETTIMEOFDAY(&tv);
836 diff = tv - self->prev_timeofday;
837 tdelta = (int)diff;
838 #else
839 struct timeval tv;
841 GETTIMEOFDAY(&tv);
843 if (tv.tv_sec == self->prev_timeofday.tv_sec)
844 tdelta = tv.tv_usec - self->prev_timeofday.tv_usec;
845 else
846 tdelta = ((tv.tv_sec - self->prev_timeofday.tv_sec) * 1000000
847 + tv.tv_usec);
848 #endif
849 self->prev_timeofday = tv;
850 return tdelta;
854 /* The workhorse: the profiler callback function. */
856 static int
857 profiler_callback(ProfilerObject *self, PyFrameObject *frame, int what,
858 PyObject *arg)
860 int tdelta = -1;
861 int fileno;
863 if (self->frametimings)
864 tdelta = get_tdelta(self);
865 switch (what) {
866 case PyTrace_CALL:
867 fileno = get_fileno(self, frame->f_code);
868 if (fileno < 0)
869 return -1;
870 pack_enter(self, fileno, tdelta,
871 frame->f_code->co_firstlineno);
872 break;
873 case PyTrace_RETURN:
874 pack_exit(self, tdelta);
875 break;
876 default:
877 /* should never get here */
878 break;
880 return 0;
884 /* Alternate callback when we want PyTrace_LINE events */
886 static int
887 tracer_callback(ProfilerObject *self, PyFrameObject *frame, int what,
888 PyObject *arg)
890 int fileno;
892 switch (what) {
893 case PyTrace_CALL:
894 fileno = get_fileno(self, frame->f_code);
895 if (fileno < 0)
896 return -1;
897 pack_enter(self, fileno, self->frametimings ? get_tdelta(self) : -1,
898 frame->f_code->co_firstlineno);
899 break;
900 case PyTrace_RETURN:
901 pack_exit(self, get_tdelta(self));
902 break;
903 case PyTrace_LINE:
904 if (self->linetimings)
905 pack_lineno_tdelta(self, frame->f_lineno, get_tdelta(self));
906 else
907 pack_lineno(self, frame->f_lineno);
908 break;
909 default:
910 /* ignore PyTrace_EXCEPTION */
911 break;
913 return 0;
917 /* A couple of useful helper functions. */
919 #ifdef MS_WIN32
920 static LARGE_INTEGER frequency = {0, 0};
921 #endif
923 static unsigned long timeofday_diff = 0;
924 static unsigned long rusage_diff = 0;
926 static void
927 calibrate(void)
929 hs_time tv1, tv2;
931 #ifdef MS_WIN32
932 hs_time diff;
933 QueryPerformanceFrequency(&frequency);
934 #endif
936 GETTIMEOFDAY(&tv1);
937 while (1) {
938 GETTIMEOFDAY(&tv2);
939 #ifdef MS_WIN32
940 diff = tv2 - tv1;
941 if (diff != 0) {
942 timeofday_diff = (unsigned long)diff;
943 break;
945 #else
946 if (tv1.tv_sec != tv2.tv_sec || tv1.tv_usec != tv2.tv_usec) {
947 if (tv1.tv_sec == tv2.tv_sec)
948 timeofday_diff = tv2.tv_usec - tv1.tv_usec;
949 else
950 timeofday_diff = (1000000 - tv1.tv_usec) + tv2.tv_usec;
951 break;
953 #endif
955 #if defined(MS_WIN32) || defined(macintosh)
956 rusage_diff = -1;
957 #else
959 struct rusage ru1, ru2;
961 getrusage(RUSAGE_SELF, &ru1);
962 while (1) {
963 getrusage(RUSAGE_SELF, &ru2);
964 if (ru1.ru_utime.tv_sec != ru2.ru_utime.tv_sec) {
965 rusage_diff = ((1000000 - ru1.ru_utime.tv_usec)
966 + ru2.ru_utime.tv_usec);
967 break;
969 else if (ru1.ru_utime.tv_usec != ru2.ru_utime.tv_usec) {
970 rusage_diff = ru2.ru_utime.tv_usec - ru1.ru_utime.tv_usec;
971 break;
973 else if (ru1.ru_stime.tv_sec != ru2.ru_stime.tv_sec) {
974 rusage_diff = ((1000000 - ru1.ru_stime.tv_usec)
975 + ru2.ru_stime.tv_usec);
976 break;
978 else if (ru1.ru_stime.tv_usec != ru2.ru_stime.tv_usec) {
979 rusage_diff = ru2.ru_stime.tv_usec - ru1.ru_stime.tv_usec;
980 break;
984 #endif
987 static void
988 do_start(ProfilerObject *self)
990 self->active = 1;
991 GETTIMEOFDAY(&self->prev_timeofday);
992 if (self->lineevents)
993 PyEval_SetTrace((Py_tracefunc) tracer_callback, (PyObject *)self);
994 else
995 PyEval_SetProfile((Py_tracefunc) profiler_callback, (PyObject *)self);
998 static void
999 do_stop(ProfilerObject *self)
1001 if (self->active) {
1002 self->active = 0;
1003 if (self->lineevents)
1004 PyEval_SetTrace(NULL, NULL);
1005 else
1006 PyEval_SetProfile(NULL, NULL);
1008 if (self->index > 0) {
1009 /* Best effort to dump out any remaining data. */
1010 flush_data(self);
1014 static int
1015 is_available(ProfilerObject *self)
1017 if (self->active) {
1018 PyErr_SetString(ProfilerError, "profiler already active");
1019 return 0;
1021 if (self->logfp == NULL) {
1022 PyErr_SetString(ProfilerError, "profiler already closed");
1023 return 0;
1025 return 1;
1029 /* Profiler object interface methods. */
1031 static char addinfo__doc__[] =
1032 "addinfo(key, value)\n"
1033 "Insert an ADD_INFO record into the log.";
1035 static PyObject *
1036 profiler_addinfo(ProfilerObject *self, PyObject *args)
1038 PyObject *result = NULL;
1039 char *key, *value;
1041 if (PyArg_ParseTuple(args, "ss:addinfo", &key, &value)) {
1042 if (self->logfp == NULL)
1043 PyErr_SetString(ProfilerError, "profiler already closed");
1044 else {
1045 pack_add_info(self, key, value);
1046 result = Py_None;
1047 Py_INCREF(result);
1050 return result;
1053 static char close__doc__[] =
1054 "close()\n"
1055 "Shut down this profiler and close the log files, even if its active.";
1057 static PyObject *
1058 profiler_close(ProfilerObject *self, PyObject *args)
1060 PyObject *result = NULL;
1062 if (PyArg_ParseTuple(args, ":close")) {
1063 do_stop(self);
1064 if (self->logfp != NULL) {
1065 fclose(self->logfp);
1066 self->logfp = NULL;
1068 Py_INCREF(Py_None);
1069 result = Py_None;
1071 return result;
1074 static char runcall__doc__[] =
1075 "runcall(callable[, args[, kw]]) -> callable()\n"
1076 "Profile a specific function call, returning the result of that call.";
1078 static PyObject *
1079 profiler_runcall(ProfilerObject *self, PyObject *args)
1081 PyObject *result = NULL;
1082 PyObject *callargs = NULL;
1083 PyObject *callkw = NULL;
1084 PyObject *callable;
1086 if (PyArg_ParseTuple(args, "O|OO:runcall",
1087 &callable, &callargs, &callkw)) {
1088 if (is_available(self)) {
1089 do_start(self);
1090 result = PyEval_CallObjectWithKeywords(callable, callargs, callkw);
1091 do_stop(self);
1094 return result;
1097 static char runcode__doc__[] =
1098 "runcode(code, globals[, locals])\n"
1099 "Execute a code object while collecting profile data. If locals is\n"
1100 "omitted, globals is used for the locals as well.";
1102 static PyObject *
1103 profiler_runcode(ProfilerObject *self, PyObject *args)
1105 PyObject *result = NULL;
1106 PyCodeObject *code;
1107 PyObject *globals;
1108 PyObject *locals = NULL;
1110 if (PyArg_ParseTuple(args, "O!O!|O:runcode",
1111 &PyCode_Type, &code,
1112 &PyDict_Type, &globals,
1113 &locals)) {
1114 if (is_available(self)) {
1115 if (locals == NULL || locals == Py_None)
1116 locals = globals;
1117 else if (!PyDict_Check(locals)) {
1118 PyErr_SetString(PyExc_TypeError,
1119 "locals must be a dictionary or None");
1120 return NULL;
1122 do_start(self);
1123 result = PyEval_EvalCode(code, globals, locals);
1124 do_stop(self);
1125 #if 0
1126 if (!PyErr_Occurred()) {
1127 result = Py_None;
1128 Py_INCREF(result);
1130 #endif
1133 return result;
1136 static char start__doc__[] =
1137 "start()\n"
1138 "Install this profiler for the current thread.";
1140 static PyObject *
1141 profiler_start(ProfilerObject *self, PyObject *args)
1143 PyObject *result = NULL;
1145 if (PyArg_ParseTuple(args, ":start")) {
1146 if (is_available(self))
1147 do_start(self);
1149 return result;
1152 static char stop__doc__[] =
1153 "stop()\n"
1154 "Remove this profiler from the current thread.";
1156 static PyObject *
1157 profiler_stop(ProfilerObject *self, PyObject *args)
1159 PyObject *result = NULL;
1161 if (PyArg_ParseTuple(args, ":stop")) {
1162 if (!self->active)
1163 PyErr_SetString(ProfilerError, "profiler not active");
1164 else
1165 do_stop(self);
1167 return result;
1171 /* Python API support. */
1173 static void
1174 profiler_dealloc(ProfilerObject *self)
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);
1184 /* Always use METH_VARARGS even though some of these could be METH_NOARGS;
1185 * this allows us to maintain compatibility with Python versions < 2.2
1186 * more easily, requiring only the changes to the dispatcher to be made.
1188 static PyMethodDef profiler_methods[] = {
1189 {"addinfo", (PyCFunction)profiler_addinfo, METH_VARARGS, addinfo__doc__},
1190 {"close", (PyCFunction)profiler_close, METH_VARARGS, close__doc__},
1191 {"runcall", (PyCFunction)profiler_runcall, METH_VARARGS, runcall__doc__},
1192 {"runcode", (PyCFunction)profiler_runcode, METH_VARARGS, runcode__doc__},
1193 {"start", (PyCFunction)profiler_start, METH_VARARGS, start__doc__},
1194 {"stop", (PyCFunction)profiler_stop, METH_VARARGS, stop__doc__},
1195 {NULL, NULL}
1198 /* Use a table even though there's only one "simple" member; this allows
1199 * __members__ and therefore dir() to work.
1201 static struct memberlist profiler_members[] = {
1202 {"closed", T_INT, -1, READONLY},
1203 {"frametimings", T_LONG, offsetof(ProfilerObject, linetimings), READONLY},
1204 {"lineevents", T_LONG, offsetof(ProfilerObject, lineevents), READONLY},
1205 {"linetimings", T_LONG, offsetof(ProfilerObject, linetimings), READONLY},
1206 {NULL}
1209 static PyObject *
1210 profiler_getattr(ProfilerObject *self, char *name)
1212 PyObject *result;
1213 if (strcmp(name, "closed") == 0) {
1214 result = (self->logfp == NULL) ? Py_True : Py_False;
1215 Py_INCREF(result);
1217 else {
1218 result = PyMember_Get((char *)self, profiler_members, name);
1219 if (result == NULL) {
1220 PyErr_Clear();
1221 result = Py_FindMethod(profiler_methods, (PyObject *)self, name);
1224 return result;
1228 static char profiler_object__doc__[] =
1229 "High-performance profiler object.\n"
1230 "\n"
1231 "Methods:\n"
1232 "\n"
1233 "close(): Stop the profiler and close the log files.\n"
1234 "runcall(): Run a single function call with profiling enabled.\n"
1235 "runcode(): Execute a code object with profiling enabled.\n"
1236 "start(): Install the profiler and return.\n"
1237 "stop(): Remove the profiler.\n"
1238 "\n"
1239 "Attributes (read-only):\n"
1240 "\n"
1241 "closed: True if the profiler has already been closed.\n"
1242 "frametimings: True if ENTER/EXIT events collect timing information.\n"
1243 "lineevents: True if SET_LINENO events are reported to the profiler.\n"
1244 "linetimings: True if SET_LINENO events collect timing information.";
1246 static PyTypeObject ProfilerType = {
1247 PyObject_HEAD_INIT(NULL)
1248 0, /* ob_size */
1249 "_hotshot.ProfilerType", /* tp_name */
1250 (int) sizeof(ProfilerObject), /* tp_basicsize */
1251 0, /* tp_itemsize */
1252 (destructor)profiler_dealloc, /* tp_dealloc */
1253 0, /* tp_print */
1254 (getattrfunc)profiler_getattr, /* tp_getattr */
1255 0, /* tp_setattr */
1256 0, /* tp_compare */
1257 0, /* tp_repr */
1258 0, /* tp_as_number */
1259 0, /* tp_as_sequence */
1260 0, /* tp_as_mapping */
1261 0, /* tp_hash */
1262 0, /* tp_call */
1263 0, /* tp_str */
1264 0, /* tp_getattro */
1265 0, /* tp_setattro */
1266 0, /* tp_as_buffer */
1267 Py_TPFLAGS_DEFAULT, /* tp_flags */
1268 profiler_object__doc__, /* tp_doc */
1272 static PyMethodDef logreader_methods[] = {
1273 {"close", (PyCFunction)logreader_close, METH_VARARGS,
1274 logreader_close__doc__},
1275 {"next", (PyCFunction)logreader_next, METH_VARARGS,
1276 next__doc__},
1277 {NULL, NULL}
1280 static PyObject *
1281 logreader_getattr(LogReaderObject *self, char *name)
1283 if (strcmp(name, "info") == 0) {
1284 Py_INCREF(self->info);
1285 return self->info;
1287 return Py_FindMethod(logreader_methods, (PyObject *)self, name);
1291 static char logreader__doc__[] = "\
1292 logreader(filename) --> log-iterator\n\
1293 Create a log-reader for the timing information file.";
1295 static PySequenceMethods logreader_as_sequence = {
1296 0, /* sq_length */
1297 0, /* sq_concat */
1298 0, /* sq_repeat */
1299 (intargfunc)logreader_sq_item, /* sq_item */
1300 0, /* sq_slice */
1301 0, /* sq_ass_item */
1302 0, /* sq_ass_slice */
1303 0, /* sq_contains */
1304 0, /* sq_inplace_concat */
1305 0, /* sq_inplace_repeat */
1308 static PyTypeObject LogReaderType = {
1309 PyObject_HEAD_INIT(NULL)
1310 0, /* ob_size */
1311 "_hotshot.LogReaderType", /* tp_name */
1312 (int) sizeof(LogReaderObject), /* tp_basicsize */
1313 0, /* tp_itemsize */
1314 (destructor)logreader_dealloc, /* tp_dealloc */
1315 0, /* tp_print */
1316 (getattrfunc)logreader_getattr, /* tp_getattr */
1317 0, /* tp_setattr */
1318 0, /* tp_compare */
1319 0, /* tp_repr */
1320 0, /* tp_as_number */
1321 &logreader_as_sequence, /* tp_as_sequence */
1322 0, /* tp_as_mapping */
1323 0, /* tp_hash */
1324 0, /* tp_call */
1325 0, /* tp_str */
1326 0, /* tp_getattro */
1327 0, /* tp_setattro */
1328 0, /* tp_as_buffer */
1329 Py_TPFLAGS_DEFAULT, /* tp_flags */
1330 logreader__doc__, /* tp_doc */
1331 #if Py_TPFLAGS_HAVE_ITER
1332 0, /* tp_traverse */
1333 0, /* tp_clear */
1334 0, /* tp_richcompare */
1335 0, /* tp_weaklistoffset */
1336 (getiterfunc)logreader_tp_iter, /* tp_iter */
1337 (iternextfunc)logreader_tp_iternext,/* tp_iternext */
1338 #endif
1341 static PyObject *
1342 hotshot_logreader(PyObject *unused, PyObject *args)
1344 LogReaderObject *self = NULL;
1345 char *filename;
1347 if (PyArg_ParseTuple(args, "s:logreader", &filename)) {
1348 self = PyObject_New(LogReaderObject, &LogReaderType);
1349 if (self != NULL) {
1350 self->filled = 0;
1351 self->index = 0;
1352 self->frametimings = 1;
1353 self->linetimings = 0;
1354 self->info = NULL;
1355 self->logfp = fopen(filename, "rb");
1356 if (self->logfp == NULL) {
1357 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
1358 Py_DECREF(self);
1359 self = NULL;
1360 goto finally;
1362 self->info = PyDict_New();
1363 if (self->info == NULL) {
1364 Py_DECREF(self);
1365 goto finally;
1367 /* Aggressively attempt to load all preliminary ADD_INFO
1368 * records from the log so the info records are available
1369 * from a fresh logreader object.
1371 logreader_refill(self);
1372 while (self->filled > self->index
1373 && self->buffer[self->index] == WHAT_ADD_INFO) {
1374 int err = unpack_add_info(self, 1);
1375 if (err) {
1376 if (err == ERR_EOF)
1377 eof_error();
1378 else
1379 PyErr_SetString(PyExc_RuntimeError,
1380 "unexpected error");
1381 break;
1383 /* Refill agressively so we can avoid EOF during
1384 * initialization unless there's a real EOF condition
1385 * (the tp_iternext handler loops attempts to refill
1386 * and try again).
1388 logreader_refill(self);
1392 finally:
1393 return (PyObject *) self;
1397 /* Return a Python string that represents the version number without the
1398 * extra cruft added by revision control, even if the right options were
1399 * given to the "cvs export" command to make it not include the extra
1400 * cruft.
1402 static char *
1403 get_version_string(void)
1405 static char *rcsid = "$Revision$";
1406 char *rev = rcsid;
1407 char *buffer;
1408 int i = 0;
1410 while (*rev && !isdigit(*rev))
1411 ++rev;
1412 while (rev[i] != ' ' && rev[i] != '\0')
1413 ++i;
1414 buffer = malloc(i + 1);
1415 if (buffer != NULL) {
1416 memmove(buffer, rev, i);
1417 buffer[i] = '\0';
1419 return buffer;
1422 /* Write out a RFC 822-style header with various useful bits of
1423 * information to make the output easier to manage.
1425 static int
1426 write_header(ProfilerObject *self)
1428 char *buffer;
1429 char cwdbuffer[PATH_MAX];
1430 PyObject *temp;
1431 int i, len;
1433 buffer = get_version_string();
1434 if (buffer == NULL) {
1435 PyErr_NoMemory();
1436 return -1;
1438 pack_add_info(self, "hotshot-version", buffer);
1439 pack_add_info(self, "requested-frame-timings",
1440 (self->frametimings ? "yes" : "no"));
1441 pack_add_info(self, "requested-line-events",
1442 (self->lineevents ? "yes" : "no"));
1443 pack_add_info(self, "requested-line-timings",
1444 (self->linetimings ? "yes" : "no"));
1445 pack_add_info(self, "platform", Py_GetPlatform());
1446 pack_add_info(self, "executable", Py_GetProgramFullPath());
1447 free(buffer);
1448 buffer = (char *) Py_GetVersion();
1449 if (buffer == NULL)
1450 PyErr_Clear();
1451 else
1452 pack_add_info(self, "executable-version", buffer);
1454 #ifdef MS_WIN32
1455 sprintf(cwdbuffer, "%I64d", frequency.QuadPart);
1456 pack_add_info(self, "reported-performance-frequency", cwdbuffer);
1457 #else
1458 sprintf(cwdbuffer, "%lu", rusage_diff);
1459 pack_add_info(self, "observed-interval-getrusage", cwdbuffer);
1460 sprintf(cwdbuffer, "%lu", timeofday_diff);
1461 pack_add_info(self, "observed-interval-gettimeofday", cwdbuffer);
1462 #endif
1464 pack_add_info(self, "current-directory",
1465 getcwd(cwdbuffer, sizeof cwdbuffer));
1467 temp = PySys_GetObject("path");
1468 len = PyList_GET_SIZE(temp);
1469 for (i = 0; i < len; ++i) {
1470 PyObject *item = PyList_GET_ITEM(temp, i);
1471 buffer = PyString_AsString(item);
1472 if (buffer == NULL)
1473 return -1;
1474 pack_add_info(self, "sys-path-entry", buffer);
1476 pack_frame_times(self);
1477 pack_line_times(self);
1479 return 0;
1482 static char profiler__doc__[] = "\
1483 profiler(logfilename[, lineevents[, linetimes]]) -> profiler\n\
1484 Create a new profiler object.";
1486 static PyObject *
1487 hotshot_profiler(PyObject *unused, PyObject *args)
1489 char *logfilename;
1490 ProfilerObject *self = NULL;
1491 int lineevents = 0;
1492 int linetimings = 1;
1494 if (PyArg_ParseTuple(args, "s|ii:profiler", &logfilename,
1495 &lineevents, &linetimings)) {
1496 self = PyObject_New(ProfilerObject, &ProfilerType);
1497 if (self == NULL)
1498 return NULL;
1499 self->frametimings = 1;
1500 self->lineevents = lineevents ? 1 : 0;
1501 self->linetimings = (lineevents && linetimings) ? 1 : 0;
1502 self->index = 0;
1503 self->active = 0;
1504 self->next_fileno = 0;
1505 self->logfp = NULL;
1506 self->logfilename = PyTuple_GET_ITEM(args, 0);
1507 Py_INCREF(self->logfilename);
1508 self->filemap = PyDict_New();
1509 if (self->filemap == NULL) {
1510 Py_DECREF(self);
1511 return NULL;
1513 self->logfp = fopen(logfilename, "wb");
1514 if (self->logfp == NULL) {
1515 Py_DECREF(self);
1516 PyErr_SetFromErrnoWithFilename(PyExc_IOError, logfilename);
1517 return NULL;
1519 if (timeofday_diff == 0) {
1520 /* Run this several times since sometimes the first
1521 * doesn't give the lowest values, and we're really trying
1522 * to determine the lowest.
1524 calibrate();
1525 calibrate();
1526 calibrate();
1528 if (write_header(self))
1529 /* some error occurred, exception has been set */
1530 self = NULL;
1532 return (PyObject *) self;
1535 static char coverage__doc__[] = "\
1536 coverage(logfilename) -> profiler\n\
1537 Returns a profiler that doesn't collect any timing information, which is\n\
1538 useful in building a coverage analysis tool.";
1540 static PyObject *
1541 hotshot_coverage(PyObject *unused, PyObject *args)
1543 char *logfilename;
1544 PyObject *result = NULL;
1546 if (PyArg_ParseTuple(args, "s:coverage", &logfilename)) {
1547 result = hotshot_profiler(unused, args);
1548 if (result != NULL) {
1549 ProfilerObject *self = (ProfilerObject *) result;
1550 self->frametimings = 0;
1551 self->linetimings = 0;
1552 self->lineevents = 1;
1555 return result;
1558 static char resolution__doc__[] =
1559 #ifdef MS_WIN32
1560 "resolution() -> (performance-counter-ticks, update-frequency)\n"
1561 "Return the resolution of the timer provided by the QueryPerformanceCounter()\n"
1562 "function. The first value is the smallest observed change, and the second\n"
1563 "is the result of QueryPerformanceFrequency().";
1564 #else
1565 "resolution() -> (gettimeofday-usecs, getrusage-usecs)\n"
1566 "Return the resolution of the timers provided by the gettimeofday() and\n"
1567 "getrusage() system calls, or -1 if the call is not supported.";
1568 #endif
1570 static PyObject *
1571 hotshot_resolution(PyObject *unused, PyObject *args)
1573 PyObject *result = NULL;
1575 if (PyArg_ParseTuple(args, ":resolution")) {
1576 if (timeofday_diff == 0) {
1577 calibrate();
1578 calibrate();
1579 calibrate();
1581 #ifdef MS_WIN32
1582 result = Py_BuildValue("ii", timeofday_diff, frequency.LowPart);
1583 #else
1584 result = Py_BuildValue("ii", timeofday_diff, rusage_diff);
1585 #endif
1587 return result;
1591 static PyMethodDef functions[] = {
1592 {"coverage", hotshot_coverage, METH_VARARGS, coverage__doc__},
1593 {"profiler", hotshot_profiler, METH_VARARGS, profiler__doc__},
1594 {"logreader", hotshot_logreader, METH_VARARGS, logreader__doc__},
1595 {"resolution", hotshot_resolution, METH_VARARGS, resolution__doc__},
1596 {NULL, NULL}
1600 void
1601 init_hotshot(void)
1603 PyObject *module;
1605 LogReaderType.ob_type = &PyType_Type;
1606 ProfilerType.ob_type = &PyType_Type;
1607 module = Py_InitModule("_hotshot", functions);
1608 if (module != NULL) {
1609 char *s = get_version_string();
1611 PyModule_AddStringConstant(module, "__version__", s);
1612 free(s);
1613 Py_INCREF(&LogReaderType);
1614 PyModule_AddObject(module, "LogReaderType",
1615 (PyObject *)&LogReaderType);
1616 Py_INCREF(&ProfilerType);
1617 PyModule_AddObject(module, "ProfilerType",
1618 (PyObject *)&ProfilerType);
1620 if (ProfilerError == NULL)
1621 ProfilerError = PyErr_NewException("hotshot.ProfilerError",
1622 NULL, NULL);
1623 if (ProfilerError != NULL) {
1624 Py_INCREF(ProfilerError);
1625 PyModule_AddObject(module, "ProfilerError", ProfilerError);
1627 PyModule_AddIntConstant(module, "WHAT_ENTER", WHAT_ENTER);
1628 PyModule_AddIntConstant(module, "WHAT_EXIT", WHAT_EXIT);
1629 PyModule_AddIntConstant(module, "WHAT_LINENO", WHAT_LINENO);
1630 PyModule_AddIntConstant(module, "WHAT_OTHER", WHAT_OTHER);
1631 PyModule_AddIntConstant(module, "WHAT_ADD_INFO", WHAT_ADD_INFO);
1632 PyModule_AddIntConstant(module, "WHAT_DEFINE_FILE", WHAT_DEFINE_FILE);
1633 PyModule_AddIntConstant(module, "WHAT_DEFINE_FUNC", WHAT_DEFINE_FUNC);
1634 PyModule_AddIntConstant(module, "WHAT_LINE_TIMES", WHAT_LINE_TIMES);