This commit was manufactured by cvs2svn to create tag 'r221'.
[python/dscho.git] / Modules / _hotshot.c
blobf88629dc4f32c73155ea76e7b7383be0bd50f3d3
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"
12 * Which timer to use should be made more configurable, but that should not
13 * be difficult. This will do for now.
15 #ifdef MS_WIN32
16 #include <windows.h>
17 #include <largeint.h>
18 #include <direct.h> /* for getcwd() */
19 typedef __int64 hs_time;
20 #define GETTIMEOFDAY(P_HS_TIME) \
21 { LARGE_INTEGER _temp; \
22 QueryPerformanceCounter(&_temp); \
23 *(P_HS_TIME) = _temp.QuadPart; }
26 #else
27 #ifndef HAVE_GETTIMEOFDAY
28 #error "This module requires gettimeofday() on non-Windows platforms!"
29 #endif
30 #ifdef macintosh
31 #include <sys/time.h>
32 #else
33 #include <sys/resource.h>
34 #include <sys/times.h>
35 #endif
36 typedef struct timeval hs_time;
37 #endif
39 #if !defined(__cplusplus) && !defined(inline)
40 #ifdef __GNUC__
41 #define inline __inline
42 #endif
43 #endif
45 #ifndef inline
46 #define inline
47 #endif
49 #define BUFFERSIZE 10240
51 #ifdef macintosh
52 #define PATH_MAX 254
53 #endif
55 #ifndef PATH_MAX
56 # ifdef MAX_PATH
57 # define PATH_MAX MAX_PATH
58 # else
59 # error "Need a defn. for PATH_MAX in _hotshot.c"
60 # endif
61 #endif
63 typedef struct {
64 PyObject_HEAD
65 PyObject *filemap;
66 PyObject *logfilename;
67 int index;
68 unsigned char buffer[BUFFERSIZE];
69 FILE *logfp;
70 int lineevents;
71 int linetimings;
72 int frametimings;
73 /* size_t filled; */
74 int active;
75 int next_fileno;
76 hs_time prev_timeofday;
77 } ProfilerObject;
79 typedef struct {
80 PyObject_HEAD
81 PyObject *info;
82 FILE *logfp;
83 int filled;
84 int index;
85 int linetimings;
86 int frametimings;
87 unsigned char buffer[BUFFERSIZE];
88 } LogReaderObject;
90 static PyObject * ProfilerError = NULL;
93 #ifndef MS_WIN32
94 #ifdef GETTIMEOFDAY_NO_TZ
95 #define GETTIMEOFDAY(ptv) gettimeofday((ptv))
96 #else
97 #define GETTIMEOFDAY(ptv) gettimeofday((ptv), (struct timezone *)NULL)
98 #endif
99 #endif
102 /* The log reader... */
104 static char logreader_close__doc__[] =
105 "close()\n"
106 "Close the log file, preventing additional records from being read.";
108 static PyObject *
109 logreader_close(LogReaderObject *self, PyObject *args)
111 PyObject *result = NULL;
112 if (PyArg_ParseTuple(args, ":close")) {
113 if (self->logfp != NULL) {
114 fclose(self->logfp);
115 self->logfp = NULL;
117 result = Py_None;
118 Py_INCREF(result);
120 return result;
123 #if Py_TPFLAGS_HAVE_ITER
124 /* This is only used if the interpreter has iterator support; the
125 * iternext handler is also used as a helper for other functions, so
126 * does not need to be included in this conditional section.
128 static PyObject *
129 logreader_tp_iter(LogReaderObject *self)
131 Py_INCREF(self);
132 return (PyObject *) self;
134 #endif
137 /* Log File Format
138 * ---------------
140 * The log file consists of a sequence of variable-length records.
141 * Each record is identified with a record type identifier in two
142 * bits of the first byte. The two bits are the "least significant"
143 * bits of the byte.
145 * Low bits: Opcode: Meaning:
146 * 0x00 ENTER enter a frame
147 * 0x01 EXIT exit a frame
148 * 0x02 LINENO SET_LINENO instruction was executed
149 * 0x03 OTHER more bits are needed to deecode
151 * If the type is OTHER, the record is not packed so tightly, and the
152 * remaining bits are used to disambiguate the record type. These
153 * records are not used as frequently so compaction is not an issue.
154 * Each of the first three record types has a highly tailored
155 * structure that allows it to be packed tightly.
157 * The OTHER records have the following identifiers:
159 * First byte: Opcode: Meaning:
160 * 0x13 ADD_INFO define a key/value pair
161 * 0x23 DEFINE_FILE define an int->filename mapping
162 * 0x33 LINE_TIMES indicates if LINENO events have tdeltas
163 * 0x43 DEFINE_FUNC define a (fileno,lineno)->funcname mapping
164 * 0x53 FRAME_TIMES indicates if ENTER/EXIT events have tdeltas
166 * Packed Integers
168 * "Packed integers" are non-negative integer values encoded as a
169 * sequence of bytes. Each byte is encoded such that the most
170 * significant bit is set if the next byte is also part of the
171 * integer. Each byte provides bits to the least-significant end of
172 * the result; the accumulated value must be shifted up to place the
173 * new bits into the result.
175 * "Modified packed integers" are packed integers where only a portion
176 * of the first byte is used. In the rest of the specification, these
177 * are referred to as "MPI(n,name)", where "n" is the number of bits
178 * discarded from the least-signicant positions of the byte, and
179 * "name" is a name being given to those "discarded" bits, since they
180 * are a field themselves.
182 * ENTER records:
184 * MPI(2,type) fileno -- type is 0x00
185 * PI lineno
186 * PI tdelta -- iff frame times are enabled
188 * EXIT records
190 * MPI(2,type) tdelta -- type is 0x01; tdelta will be 0
191 * if frame times are disabled
193 * LINENO records
195 * MPI(2,type) lineno -- type is 0x02
196 * PI tdelta -- iff LINENO includes it
198 * ADD_INFO records
200 * BYTE type -- always 0x13
201 * PI len1 -- length of first string
202 * BYTE string1[len1] -- len1 bytes of string data
203 * PI len2 -- length of second string
204 * BYTE string2[len2] -- len2 bytes of string data
206 * DEFINE_FILE records
208 * BYTE type -- always 0x23
209 * PI fileno
210 * PI len -- length of filename
211 * BYTE filename[len] -- len bytes of string data
213 * DEFINE_FUNC records
215 * BYTE type -- always 0x43
216 * PI fileno
217 * PI lineno
218 * PI len -- length of funcname
219 * BYTE funcname[len] -- len bytes of string data
221 * LINE_TIMES records
223 * This record can be used only before the start of ENTER/EXIT/LINENO
224 * records. If have_tdelta is true, LINENO records will include the
225 * tdelta field, otherwise it will be omitted. If this record is not
226 * given, LINENO records will not contain the tdelta field.
228 * BYTE type -- always 0x33
229 * BYTE have_tdelta -- 0 if LINENO does *not* have
230 * timing information
231 * FRAME_TIMES records
233 * This record can be used only before the start of ENTER/EXIT/LINENO
234 * records. If have_tdelta is true, ENTER and EXIT records will
235 * include the tdelta field, otherwise it will be omitted. If this
236 * record is not given, ENTER and EXIT records will contain the tdelta
237 * field.
239 * BYTE type -- always 0x53
240 * BYTE have_tdelta -- 0 if ENTER/EXIT do *not* have
241 * timing information
244 #define WHAT_ENTER 0x00
245 #define WHAT_EXIT 0x01
246 #define WHAT_LINENO 0x02
247 #define WHAT_OTHER 0x03 /* only used in decoding */
248 #define WHAT_ADD_INFO 0x13
249 #define WHAT_DEFINE_FILE 0x23
250 #define WHAT_LINE_TIMES 0x33
251 #define WHAT_DEFINE_FUNC 0x43
252 #define WHAT_FRAME_TIMES 0x53
254 #define ERR_NONE 0
255 #define ERR_EOF -1
256 #define ERR_EXCEPTION -2
257 #define ERR_BAD_RECTYPE -3
259 #define PISIZE (sizeof(int) + 1)
260 #define MPISIZE (PISIZE + 1)
262 /* Maximum size of "normal" events -- nothing that contains string data */
263 #define MAXEVENTSIZE (MPISIZE + PISIZE*2)
266 /* Unpack a packed integer; if "discard" is non-zero, unpack a modified
267 * packed integer with "discard" discarded bits.
269 static int
270 unpack_packed_int(LogReaderObject *self, int *pvalue, int discard)
272 int accum = 0;
273 int bits = 0;
274 int index = self->index;
275 int cont;
277 do {
278 if (index >= self->filled)
279 return ERR_EOF;
280 /* read byte */
281 accum |= ((self->buffer[index] & 0x7F) >> discard) << bits;
282 bits += (7 - discard);
283 cont = self->buffer[index] & 0x80;
284 /* move to next */
285 discard = 0;
286 index++;
287 } while (cont);
289 /* save state */
290 self->index = index;
291 *pvalue = accum;
293 return 0;
296 /* Unpack a string, which is encoded as a packed integer giving the
297 * length of the string, followed by the string data.
299 static int
300 unpack_string(LogReaderObject *self, PyObject **pvalue)
302 int len;
303 int oldindex = self->index;
304 int err = unpack_packed_int(self, &len, 0);
306 if (!err) {
307 /* need at least len bytes in buffer */
308 if (len > (self->filled - self->index)) {
309 self->index = oldindex;
310 err = ERR_EOF;
312 else {
313 *pvalue = PyString_FromStringAndSize((char *)self->buffer + self->index,
314 len);
315 if (*pvalue == NULL) {
316 self->index = oldindex;
317 err = ERR_EXCEPTION;
319 else
320 self->index += len;
323 return err;
327 static int
328 unpack_add_info(LogReaderObject *self, int skip_opcode)
330 PyObject *key;
331 PyObject *value = NULL;
332 int err;
334 if (skip_opcode) {
335 if (self->buffer[self->index] != WHAT_ADD_INFO)
336 return ERR_BAD_RECTYPE;
337 self->index++;
339 err = unpack_string(self, &key);
340 if (!err) {
341 err = unpack_string(self, &value);
342 if (err)
343 Py_DECREF(key);
344 else {
345 PyObject *list = PyDict_GetItem(self->info, key);
346 if (list == NULL) {
347 list = PyList_New(0);
348 if (list == NULL) {
349 err = ERR_EXCEPTION;
350 goto finally;
352 if (PyDict_SetItem(self->info, key, list)) {
353 err = ERR_EXCEPTION;
354 goto finally;
357 if (PyList_Append(list, value))
358 err = ERR_EXCEPTION;
361 finally:
362 Py_XDECREF(key);
363 Py_XDECREF(value);
364 return err;
368 static void
369 logreader_refill(LogReaderObject *self)
371 int needed;
372 size_t res;
374 if (self->index) {
375 memmove(self->buffer, &self->buffer[self->index],
376 self->filled - self->index);
377 self->filled = self->filled - self->index;
378 self->index = 0;
380 needed = BUFFERSIZE - self->filled;
381 if (needed > 0) {
382 res = fread(&self->buffer[self->filled], 1, needed, self->logfp);
383 self->filled += res;
387 static void
388 eof_error(void)
390 PyErr_SetString(PyExc_EOFError,
391 "end of file with incomplete profile record");
394 static PyObject *
395 logreader_tp_iternext(LogReaderObject *self)
397 int what, oldindex;
398 int err = ERR_NONE;
399 int lineno = -1;
400 int fileno = -1;
401 int tdelta = -1;
402 PyObject *s1 = NULL, *s2 = NULL;
403 PyObject *result = NULL;
404 #if 0
405 unsigned char b0, b1;
406 #endif
408 if (self->logfp == NULL) {
409 PyErr_SetString(ProfilerError,
410 "cannot iterate over closed LogReader object");
411 return NULL;
413 restart:
414 if ((self->filled - self->index) < MAXEVENTSIZE)
415 logreader_refill(self);
417 /* end of input */
418 if (self->filled == 0)
419 return NULL;
421 oldindex = self->index;
423 /* decode the record type */
424 what = self->buffer[self->index] & WHAT_OTHER;
425 if (what == WHAT_OTHER) {
426 what = self->buffer[self->index];
427 self->index++;
429 switch (what) {
430 case WHAT_ENTER:
431 err = unpack_packed_int(self, &fileno, 2);
432 if (!err) {
433 err = unpack_packed_int(self, &lineno, 0);
434 if (self->frametimings && !err)
435 err = unpack_packed_int(self, &tdelta, 0);
437 break;
438 case WHAT_EXIT:
439 err = unpack_packed_int(self, &tdelta, 2);
440 break;
441 case WHAT_LINENO:
442 err = unpack_packed_int(self, &lineno, 2);
443 if (self->linetimings && !err)
444 err = unpack_packed_int(self, &tdelta, 0);
445 break;
446 case WHAT_ADD_INFO:
447 err = unpack_add_info(self, 0);
448 break;
449 case WHAT_DEFINE_FILE:
450 err = unpack_packed_int(self, &fileno, 0);
451 if (!err) {
452 err = unpack_string(self, &s1);
453 if (!err) {
454 Py_INCREF(Py_None);
455 s2 = Py_None;
458 break;
459 case WHAT_DEFINE_FUNC:
460 err = unpack_packed_int(self, &fileno, 0);
461 if (!err) {
462 err = unpack_packed_int(self, &lineno, 0);
463 if (!err)
464 err = unpack_string(self, &s1);
466 break;
467 case WHAT_LINE_TIMES:
468 if (self->index >= self->filled)
469 err = ERR_EOF;
470 else {
471 self->linetimings = self->buffer[self->index] ? 1 : 0;
472 self->index++;
473 goto restart;
475 break;
476 case WHAT_FRAME_TIMES:
477 if (self->index >= self->filled)
478 err = ERR_EOF;
479 else {
480 self->frametimings = self->buffer[self->index] ? 1 : 0;
481 self->index++;
482 goto restart;
484 break;
485 default:
486 err = ERR_BAD_RECTYPE;
488 if (err == ERR_EOF && oldindex != 0) {
489 /* It looks like we ran out of data before we had it all; this
490 * could easily happen with large packed integers or string
491 * data. Try forcing the buffer to be re-filled before failing.
493 err = ERR_NONE;
494 logreader_refill(self);
496 if (err == ERR_BAD_RECTYPE) {
497 PyErr_SetString(PyExc_ValueError,
498 "unknown record type in log file");
500 else if (err == ERR_EOF) {
501 /* Could not avoid end-of-buffer error. */
502 eof_error();
504 else if (!err) {
505 result = PyTuple_New(4);
506 PyTuple_SET_ITEM(result, 0, PyInt_FromLong(what));
507 PyTuple_SET_ITEM(result, 2, PyInt_FromLong(fileno));
508 if (s1 == NULL)
509 PyTuple_SET_ITEM(result, 1, PyInt_FromLong(tdelta));
510 else
511 PyTuple_SET_ITEM(result, 1, s1);
512 if (s2 == NULL)
513 PyTuple_SET_ITEM(result, 3, PyInt_FromLong(lineno));
514 else
515 PyTuple_SET_ITEM(result, 3, s2);
517 /* The only other case is err == ERR_EXCEPTION, in which case the
518 * exception is already set.
520 #if 0
521 b0 = self->buffer[self->index];
522 b1 = self->buffer[self->index + 1];
523 if (b0 & 1) {
524 /* This is a line-number event. */
525 what = PyTrace_LINE;
526 lineno = ((b0 & ~1) << 7) + b1;
527 self->index += 2;
529 else {
530 what = (b0 & 0x0E) >> 1;
531 tdelta = ((b0 & 0xF0) << 4) + b1;
532 if (what == PyTrace_CALL) {
533 /* we know there's a 2-byte file ID & 2-byte line number */
534 fileno = ((self->buffer[self->index + 2] << 8)
535 + self->buffer[self->index + 3]);
536 lineno = ((self->buffer[self->index + 4] << 8)
537 + self->buffer[self->index + 5]);
538 self->index += 6;
540 else
541 self->index += 2;
543 #endif
544 return result;
547 static void
548 logreader_dealloc(LogReaderObject *self)
550 if (self->logfp != NULL) {
551 fclose(self->logfp);
552 self->logfp = NULL;
554 PyObject_Del(self);
557 static PyObject *
558 logreader_sq_item(LogReaderObject *self, int index)
560 PyObject *result = logreader_tp_iternext(self);
561 if (result == NULL && !PyErr_Occurred()) {
562 PyErr_SetString(PyExc_IndexError, "no more events in log");
563 return NULL;
565 return result;
568 static char next__doc__[] =
569 "next() -> event-info\n"
570 "Return the next event record from the log file.";
572 static PyObject *
573 logreader_next(LogReaderObject *self, PyObject *args)
575 PyObject *result = NULL;
577 if (PyArg_ParseTuple(args, ":next")) {
578 result = logreader_tp_iternext(self);
579 /* XXX return None if there's nothing left */
580 /* tp_iternext does the right thing, though */
581 if (result == NULL && !PyErr_Occurred()) {
582 result = Py_None;
583 Py_INCREF(result);
586 return result;
589 static void
590 do_stop(ProfilerObject *self);
592 static int
593 flush_data(ProfilerObject *self)
595 /* Need to dump data to the log file... */
596 size_t written = fwrite(self->buffer, 1, self->index, self->logfp);
597 if (written == (size_t)self->index)
598 self->index = 0;
599 else {
600 memmove(self->buffer, &self->buffer[written],
601 self->index - written);
602 self->index -= written;
603 if (written == 0) {
604 char *s = PyString_AsString(self->logfilename);
605 PyErr_SetFromErrnoWithFilename(PyExc_IOError, s);
606 do_stop(self);
607 return -1;
610 if (written > 0) {
611 if (fflush(self->logfp)) {
612 char *s = PyString_AsString(self->logfilename);
613 PyErr_SetFromErrnoWithFilename(PyExc_IOError, s);
614 do_stop(self);
615 return -1;
618 return 0;
621 static inline int
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);
634 return 0;
637 /* Encode a modified packed integer, with a subfield of modsize bits
638 * containing the value "subfield". The value of subfield is not
639 * checked to ensure it actually fits in modsize bits.
641 static inline int
642 pack_modified_packed_int(ProfilerObject *self, int value,
643 int modsize, int subfield)
645 const int maxvalues[] = {-1, 1, 3, 7, 15, 31, 63, 127};
647 int bits = 7 - modsize;
648 int partial = value & maxvalues[bits];
649 unsigned char b = subfield | (partial << modsize);
651 if (partial != value) {
652 b |= 0x80;
653 self->buffer[self->index] = b;
654 self->index++;
655 return pack_packed_int(self, value >> bits);
657 self->buffer[self->index] = b;
658 self->index++;
659 return 0;
662 static int
663 pack_string(ProfilerObject *self, const char *s, int len)
665 if (len + PISIZE + self->index >= BUFFERSIZE) {
666 if (flush_data(self) < 0)
667 return -1;
669 if (pack_packed_int(self, len) < 0)
670 return -1;
671 memcpy(self->buffer + self->index, s, len);
672 self->index += len;
673 return 0;
676 static int
677 pack_add_info(ProfilerObject *self, const char *s1, const char *s2)
679 int len1 = strlen(s1);
680 int len2 = strlen(s2);
682 if (len1 + len2 + PISIZE*2 + 1 + self->index >= BUFFERSIZE) {
683 if (flush_data(self) < 0)
684 return -1;
686 self->buffer[self->index] = WHAT_ADD_INFO;
687 self->index++;
688 if (pack_string(self, s1, len1) < 0)
689 return -1;
690 return pack_string(self, s2, len2);
693 static int
694 pack_define_file(ProfilerObject *self, int fileno, const char *filename)
696 int len = strlen(filename);
698 if (len + PISIZE*2 + 1 + self->index >= BUFFERSIZE) {
699 if (flush_data(self) < 0)
700 return -1;
702 self->buffer[self->index] = WHAT_DEFINE_FILE;
703 self->index++;
704 if (pack_packed_int(self, fileno) < 0)
705 return -1;
706 return pack_string(self, filename, len);
709 static int
710 pack_define_func(ProfilerObject *self, int fileno, int lineno,
711 const char *funcname)
713 int len = strlen(funcname);
715 if (len + PISIZE*3 + 1 + self->index >= BUFFERSIZE) {
716 if (flush_data(self) < 0)
717 return -1;
719 self->buffer[self->index] = WHAT_DEFINE_FUNC;
720 self->index++;
721 if (pack_packed_int(self, fileno) < 0)
722 return -1;
723 if (pack_packed_int(self, lineno) < 0)
724 return -1;
725 return pack_string(self, funcname, len);
728 static int
729 pack_line_times(ProfilerObject *self)
731 if (2 + self->index >= BUFFERSIZE) {
732 if (flush_data(self) < 0)
733 return -1;
735 self->buffer[self->index] = WHAT_LINE_TIMES;
736 self->buffer[self->index + 1] = self->linetimings ? 1 : 0;
737 self->index += 2;
738 return 0;
741 static int
742 pack_frame_times(ProfilerObject *self)
744 if (2 + self->index >= BUFFERSIZE) {
745 if (flush_data(self) < 0)
746 return -1;
748 self->buffer[self->index] = WHAT_FRAME_TIMES;
749 self->buffer[self->index + 1] = self->frametimings ? 1 : 0;
750 self->index += 2;
751 return 0;
754 static inline int
755 pack_enter(ProfilerObject *self, int fileno, int tdelta, int lineno)
757 if (MPISIZE + PISIZE*2 + self->index >= BUFFERSIZE) {
758 if (flush_data(self) < 0)
759 return -1;
761 pack_modified_packed_int(self, fileno, 2, WHAT_ENTER);
762 pack_packed_int(self, lineno);
763 if (self->frametimings)
764 return pack_packed_int(self, tdelta);
765 else
766 return 0;
769 static inline int
770 pack_exit(ProfilerObject *self, int tdelta)
772 if (MPISIZE + self->index >= BUFFERSIZE) {
773 if (flush_data(self) < 0)
774 return -1;
776 if (self->frametimings)
777 return pack_modified_packed_int(self, tdelta, 2, WHAT_EXIT);
778 self->buffer[self->index] = WHAT_EXIT;
779 self->index++;
780 return 0;
783 static inline int
784 pack_lineno(ProfilerObject *self, int lineno)
786 if (MPISIZE + self->index >= BUFFERSIZE) {
787 if (flush_data(self) < 0)
788 return -1;
790 return pack_modified_packed_int(self, lineno, 2, WHAT_LINENO);
793 static inline int
794 pack_lineno_tdelta(ProfilerObject *self, int lineno, int tdelta)
796 if (MPISIZE + PISIZE + self->index >= BUFFERSIZE) {
797 if (flush_data(self) < 0)
798 return 0;
800 if (pack_modified_packed_int(self, lineno, 2, WHAT_LINENO) < 0)
801 return -1;
802 return pack_packed_int(self, tdelta);
805 static inline int
806 get_fileno(ProfilerObject *self, PyCodeObject *fcode)
808 /* This is only used for ENTER events. */
810 PyObject *obj;
811 PyObject *dict;
812 int fileno;
814 obj = PyDict_GetItem(self->filemap, fcode->co_filename);
815 if (obj == NULL) {
816 /* first sighting of this file */
817 dict = PyDict_New();
818 if (dict == NULL) {
819 return -1;
821 fileno = self->next_fileno;
822 obj = Py_BuildValue("iN", fileno, dict);
823 if (obj == NULL) {
824 return -1;
826 if (PyDict_SetItem(self->filemap, fcode->co_filename, obj)) {
827 Py_DECREF(obj);
828 return -1;
830 self->next_fileno++;
831 Py_DECREF(obj);
832 if (pack_define_file(self, fileno,
833 PyString_AS_STRING(fcode->co_filename)) < 0)
834 return -1;
836 else {
837 /* already know this ID */
838 fileno = PyInt_AS_LONG(PyTuple_GET_ITEM(obj, 0));
839 dict = PyTuple_GET_ITEM(obj, 1);
841 /* make sure we save a function name for this (fileno, lineno) */
842 obj = PyInt_FromLong(fcode->co_firstlineno);
843 if (obj == NULL) {
844 /* We just won't have it saved; too bad. */
845 PyErr_Clear();
847 else {
848 PyObject *name = PyDict_GetItem(dict, obj);
849 if (name == NULL) {
850 if (pack_define_func(self, fileno, fcode->co_firstlineno,
851 PyString_AS_STRING(fcode->co_name)) < 0)
852 return -1;
853 if (PyDict_SetItem(dict, obj, fcode->co_name))
854 return -1;
857 return fileno;
860 static inline int
861 get_tdelta(ProfilerObject *self)
863 int tdelta;
864 #ifdef MS_WIN32
865 hs_time tv;
866 hs_time diff;
868 GETTIMEOFDAY(&tv);
869 diff = tv - self->prev_timeofday;
870 tdelta = (int)diff;
871 #else
872 struct timeval tv;
874 GETTIMEOFDAY(&tv);
876 if (tv.tv_sec == self->prev_timeofday.tv_sec)
877 tdelta = tv.tv_usec - self->prev_timeofday.tv_usec;
878 else
879 tdelta = ((tv.tv_sec - self->prev_timeofday.tv_sec) * 1000000
880 + tv.tv_usec);
881 #endif
882 self->prev_timeofday = tv;
883 return tdelta;
887 /* The workhorse: the profiler callback function. */
889 static int
890 profiler_callback(ProfilerObject *self, PyFrameObject *frame, int what,
891 PyObject *arg)
893 int tdelta = -1;
894 int fileno;
896 if (self->frametimings)
897 tdelta = get_tdelta(self);
898 switch (what) {
899 case PyTrace_CALL:
900 fileno = get_fileno(self, frame->f_code);
901 if (fileno < 0)
902 return -1;
903 if (pack_enter(self, fileno, tdelta,
904 frame->f_code->co_firstlineno) < 0)
905 return -1;
906 break;
907 case PyTrace_RETURN:
908 if (pack_exit(self, tdelta) < 0)
909 return -1;
910 break;
911 default:
912 /* should never get here */
913 break;
915 return 0;
919 /* Alternate callback when we want PyTrace_LINE events */
921 static int
922 tracer_callback(ProfilerObject *self, PyFrameObject *frame, int what,
923 PyObject *arg)
925 int fileno;
927 switch (what) {
928 case PyTrace_CALL:
929 fileno = get_fileno(self, frame->f_code);
930 if (fileno < 0)
931 return -1;
932 return pack_enter(self, fileno,
933 self->frametimings ? get_tdelta(self) : -1,
934 frame->f_code->co_firstlineno);
936 case PyTrace_RETURN:
937 return pack_exit(self, get_tdelta(self));
939 case PyTrace_LINE:
940 if (self->linetimings)
941 return pack_lineno_tdelta(self, frame->f_lineno, get_tdelta(self));
942 else
943 return pack_lineno(self, frame->f_lineno);
945 default:
946 /* ignore PyTrace_EXCEPTION */
947 break;
949 return 0;
953 /* A couple of useful helper functions. */
955 #ifdef MS_WIN32
956 static LARGE_INTEGER frequency = {0, 0};
957 #endif
959 static unsigned long timeofday_diff = 0;
960 static unsigned long rusage_diff = 0;
962 static void
963 calibrate(void)
965 hs_time tv1, tv2;
967 #ifdef MS_WIN32
968 hs_time diff;
969 QueryPerformanceFrequency(&frequency);
970 #endif
972 GETTIMEOFDAY(&tv1);
973 while (1) {
974 GETTIMEOFDAY(&tv2);
975 #ifdef MS_WIN32
976 diff = tv2 - tv1;
977 if (diff != 0) {
978 timeofday_diff = (unsigned long)diff;
979 break;
981 #else
982 if (tv1.tv_sec != tv2.tv_sec || tv1.tv_usec != tv2.tv_usec) {
983 if (tv1.tv_sec == tv2.tv_sec)
984 timeofday_diff = tv2.tv_usec - tv1.tv_usec;
985 else
986 timeofday_diff = (1000000 - tv1.tv_usec) + tv2.tv_usec;
987 break;
989 #endif
991 #if defined(MS_WIN32) || defined(macintosh)
992 rusage_diff = -1;
993 #else
995 struct rusage ru1, ru2;
997 getrusage(RUSAGE_SELF, &ru1);
998 while (1) {
999 getrusage(RUSAGE_SELF, &ru2);
1000 if (ru1.ru_utime.tv_sec != ru2.ru_utime.tv_sec) {
1001 rusage_diff = ((1000000 - ru1.ru_utime.tv_usec)
1002 + ru2.ru_utime.tv_usec);
1003 break;
1005 else if (ru1.ru_utime.tv_usec != ru2.ru_utime.tv_usec) {
1006 rusage_diff = ru2.ru_utime.tv_usec - ru1.ru_utime.tv_usec;
1007 break;
1009 else if (ru1.ru_stime.tv_sec != ru2.ru_stime.tv_sec) {
1010 rusage_diff = ((1000000 - ru1.ru_stime.tv_usec)
1011 + ru2.ru_stime.tv_usec);
1012 break;
1014 else if (ru1.ru_stime.tv_usec != ru2.ru_stime.tv_usec) {
1015 rusage_diff = ru2.ru_stime.tv_usec - ru1.ru_stime.tv_usec;
1016 break;
1020 #endif
1023 static void
1024 do_start(ProfilerObject *self)
1026 self->active = 1;
1027 GETTIMEOFDAY(&self->prev_timeofday);
1028 if (self->lineevents)
1029 PyEval_SetTrace((Py_tracefunc) tracer_callback, (PyObject *)self);
1030 else
1031 PyEval_SetProfile((Py_tracefunc) profiler_callback, (PyObject *)self);
1034 static void
1035 do_stop(ProfilerObject *self)
1037 if (self->active) {
1038 self->active = 0;
1039 if (self->lineevents)
1040 PyEval_SetTrace(NULL, NULL);
1041 else
1042 PyEval_SetProfile(NULL, NULL);
1044 if (self->index > 0) {
1045 /* Best effort to dump out any remaining data. */
1046 flush_data(self);
1050 static int
1051 is_available(ProfilerObject *self)
1053 if (self->active) {
1054 PyErr_SetString(ProfilerError, "profiler already active");
1055 return 0;
1057 if (self->logfp == NULL) {
1058 PyErr_SetString(ProfilerError, "profiler already closed");
1059 return 0;
1061 return 1;
1065 /* Profiler object interface methods. */
1067 static char addinfo__doc__[] =
1068 "addinfo(key, value)\n"
1069 "Insert an ADD_INFO record into the log.";
1071 static PyObject *
1072 profiler_addinfo(ProfilerObject *self, PyObject *args)
1074 PyObject *result = NULL;
1075 char *key, *value;
1077 if (PyArg_ParseTuple(args, "ss:addinfo", &key, &value)) {
1078 if (self->logfp == NULL)
1079 PyErr_SetString(ProfilerError, "profiler already closed");
1080 else {
1081 if (pack_add_info(self, key, value) == 0) {
1082 result = Py_None;
1083 Py_INCREF(result);
1087 return result;
1090 static char close__doc__[] =
1091 "close()\n"
1092 "Shut down this profiler and close the log files, even if its active.";
1094 static PyObject *
1095 profiler_close(ProfilerObject *self, PyObject *args)
1097 PyObject *result = NULL;
1099 if (PyArg_ParseTuple(args, ":close")) {
1100 do_stop(self);
1101 if (self->logfp != NULL) {
1102 fclose(self->logfp);
1103 self->logfp = NULL;
1105 Py_INCREF(Py_None);
1106 result = Py_None;
1108 return result;
1111 static char runcall__doc__[] =
1112 "runcall(callable[, args[, kw]]) -> callable()\n"
1113 "Profile a specific function call, returning the result of that call.";
1115 static PyObject *
1116 profiler_runcall(ProfilerObject *self, PyObject *args)
1118 PyObject *result = NULL;
1119 PyObject *callargs = NULL;
1120 PyObject *callkw = NULL;
1121 PyObject *callable;
1123 if (PyArg_ParseTuple(args, "O|OO:runcall",
1124 &callable, &callargs, &callkw)) {
1125 if (is_available(self)) {
1126 do_start(self);
1127 result = PyEval_CallObjectWithKeywords(callable, callargs, callkw);
1128 do_stop(self);
1131 return result;
1134 static char runcode__doc__[] =
1135 "runcode(code, globals[, locals])\n"
1136 "Execute a code object while collecting profile data. If locals is\n"
1137 "omitted, globals is used for the locals as well.";
1139 static PyObject *
1140 profiler_runcode(ProfilerObject *self, PyObject *args)
1142 PyObject *result = NULL;
1143 PyCodeObject *code;
1144 PyObject *globals;
1145 PyObject *locals = NULL;
1147 if (PyArg_ParseTuple(args, "O!O!|O:runcode",
1148 &PyCode_Type, &code,
1149 &PyDict_Type, &globals,
1150 &locals)) {
1151 if (is_available(self)) {
1152 if (locals == NULL || locals == Py_None)
1153 locals = globals;
1154 else if (!PyDict_Check(locals)) {
1155 PyErr_SetString(PyExc_TypeError,
1156 "locals must be a dictionary or None");
1157 return NULL;
1159 do_start(self);
1160 result = PyEval_EvalCode(code, globals, locals);
1161 do_stop(self);
1162 #if 0
1163 if (!PyErr_Occurred()) {
1164 result = Py_None;
1165 Py_INCREF(result);
1167 #endif
1170 return result;
1173 static char start__doc__[] =
1174 "start()\n"
1175 "Install this profiler for the current thread.";
1177 static PyObject *
1178 profiler_start(ProfilerObject *self, PyObject *args)
1180 PyObject *result = NULL;
1182 if (PyArg_ParseTuple(args, ":start")) {
1183 if (is_available(self)) {
1184 do_start(self);
1185 result = Py_None;
1186 Py_INCREF(result);
1189 return result;
1192 static char stop__doc__[] =
1193 "stop()\n"
1194 "Remove this profiler from the current thread.";
1196 static PyObject *
1197 profiler_stop(ProfilerObject *self, PyObject *args)
1199 PyObject *result = NULL;
1201 if (PyArg_ParseTuple(args, ":stop")) {
1202 if (!self->active)
1203 PyErr_SetString(ProfilerError, "profiler not active");
1204 else {
1205 do_stop(self);
1206 result = Py_None;
1207 Py_INCREF(result);
1210 return result;
1214 /* Python API support. */
1216 static void
1217 profiler_dealloc(ProfilerObject *self)
1219 do_stop(self);
1220 if (self->logfp != NULL)
1221 fclose(self->logfp);
1222 Py_XDECREF(self->filemap);
1223 Py_XDECREF(self->logfilename);
1224 PyObject_Del((PyObject *)self);
1227 /* Always use METH_VARARGS even though some of these could be METH_NOARGS;
1228 * this allows us to maintain compatibility with Python versions < 2.2
1229 * more easily, requiring only the changes to the dispatcher to be made.
1231 static PyMethodDef profiler_methods[] = {
1232 {"addinfo", (PyCFunction)profiler_addinfo, METH_VARARGS, addinfo__doc__},
1233 {"close", (PyCFunction)profiler_close, METH_VARARGS, close__doc__},
1234 {"runcall", (PyCFunction)profiler_runcall, METH_VARARGS, runcall__doc__},
1235 {"runcode", (PyCFunction)profiler_runcode, METH_VARARGS, runcode__doc__},
1236 {"start", (PyCFunction)profiler_start, METH_VARARGS, start__doc__},
1237 {"stop", (PyCFunction)profiler_stop, METH_VARARGS, stop__doc__},
1238 {NULL, NULL}
1241 /* Use a table even though there's only one "simple" member; this allows
1242 * __members__ and therefore dir() to work.
1244 static struct memberlist profiler_members[] = {
1245 {"closed", T_INT, -1, READONLY},
1246 {"frametimings", T_LONG, offsetof(ProfilerObject, linetimings), READONLY},
1247 {"lineevents", T_LONG, offsetof(ProfilerObject, lineevents), READONLY},
1248 {"linetimings", T_LONG, offsetof(ProfilerObject, linetimings), READONLY},
1249 {NULL}
1252 static PyObject *
1253 profiler_getattr(ProfilerObject *self, char *name)
1255 PyObject *result;
1256 if (strcmp(name, "closed") == 0) {
1257 result = (self->logfp == NULL) ? Py_True : Py_False;
1258 Py_INCREF(result);
1260 else {
1261 result = PyMember_Get((char *)self, profiler_members, name);
1262 if (result == NULL) {
1263 PyErr_Clear();
1264 result = Py_FindMethod(profiler_methods, (PyObject *)self, name);
1267 return result;
1271 static char profiler_object__doc__[] =
1272 "High-performance profiler object.\n"
1273 "\n"
1274 "Methods:\n"
1275 "\n"
1276 "close(): Stop the profiler and close the log files.\n"
1277 "runcall(): Run a single function call with profiling enabled.\n"
1278 "runcode(): Execute a code object with profiling enabled.\n"
1279 "start(): Install the profiler and return.\n"
1280 "stop(): Remove the profiler.\n"
1281 "\n"
1282 "Attributes (read-only):\n"
1283 "\n"
1284 "closed: True if the profiler has already been closed.\n"
1285 "frametimings: True if ENTER/EXIT events collect timing information.\n"
1286 "lineevents: True if SET_LINENO events are reported to the profiler.\n"
1287 "linetimings: True if SET_LINENO events collect timing information.";
1289 static PyTypeObject ProfilerType = {
1290 PyObject_HEAD_INIT(NULL)
1291 0, /* ob_size */
1292 "_hotshot.ProfilerType", /* tp_name */
1293 (int) sizeof(ProfilerObject), /* tp_basicsize */
1294 0, /* tp_itemsize */
1295 (destructor)profiler_dealloc, /* tp_dealloc */
1296 0, /* tp_print */
1297 (getattrfunc)profiler_getattr, /* tp_getattr */
1298 0, /* tp_setattr */
1299 0, /* tp_compare */
1300 0, /* tp_repr */
1301 0, /* tp_as_number */
1302 0, /* tp_as_sequence */
1303 0, /* tp_as_mapping */
1304 0, /* tp_hash */
1305 0, /* tp_call */
1306 0, /* tp_str */
1307 0, /* tp_getattro */
1308 0, /* tp_setattro */
1309 0, /* tp_as_buffer */
1310 Py_TPFLAGS_DEFAULT, /* tp_flags */
1311 profiler_object__doc__, /* tp_doc */
1315 static PyMethodDef logreader_methods[] = {
1316 {"close", (PyCFunction)logreader_close, METH_VARARGS,
1317 logreader_close__doc__},
1318 {"next", (PyCFunction)logreader_next, METH_VARARGS,
1319 next__doc__},
1320 {NULL, NULL}
1323 static PyObject *
1324 logreader_getattr(LogReaderObject *self, char *name)
1326 if (strcmp(name, "info") == 0) {
1327 Py_INCREF(self->info);
1328 return self->info;
1330 return Py_FindMethod(logreader_methods, (PyObject *)self, name);
1334 static char logreader__doc__[] = "\
1335 logreader(filename) --> log-iterator\n\
1336 Create a log-reader for the timing information file.";
1338 static PySequenceMethods logreader_as_sequence = {
1339 0, /* sq_length */
1340 0, /* sq_concat */
1341 0, /* sq_repeat */
1342 (intargfunc)logreader_sq_item, /* sq_item */
1343 0, /* sq_slice */
1344 0, /* sq_ass_item */
1345 0, /* sq_ass_slice */
1346 0, /* sq_contains */
1347 0, /* sq_inplace_concat */
1348 0, /* sq_inplace_repeat */
1351 static PyTypeObject LogReaderType = {
1352 PyObject_HEAD_INIT(NULL)
1353 0, /* ob_size */
1354 "_hotshot.LogReaderType", /* tp_name */
1355 (int) sizeof(LogReaderObject), /* tp_basicsize */
1356 0, /* tp_itemsize */
1357 (destructor)logreader_dealloc, /* tp_dealloc */
1358 0, /* tp_print */
1359 (getattrfunc)logreader_getattr, /* tp_getattr */
1360 0, /* tp_setattr */
1361 0, /* tp_compare */
1362 0, /* tp_repr */
1363 0, /* tp_as_number */
1364 &logreader_as_sequence, /* tp_as_sequence */
1365 0, /* tp_as_mapping */
1366 0, /* tp_hash */
1367 0, /* tp_call */
1368 0, /* tp_str */
1369 0, /* tp_getattro */
1370 0, /* tp_setattro */
1371 0, /* tp_as_buffer */
1372 Py_TPFLAGS_DEFAULT, /* tp_flags */
1373 logreader__doc__, /* tp_doc */
1374 #if Py_TPFLAGS_HAVE_ITER
1375 0, /* tp_traverse */
1376 0, /* tp_clear */
1377 0, /* tp_richcompare */
1378 0, /* tp_weaklistoffset */
1379 (getiterfunc)logreader_tp_iter, /* tp_iter */
1380 (iternextfunc)logreader_tp_iternext,/* tp_iternext */
1381 #endif
1384 static PyObject *
1385 hotshot_logreader(PyObject *unused, PyObject *args)
1387 LogReaderObject *self = NULL;
1388 char *filename;
1390 if (PyArg_ParseTuple(args, "s:logreader", &filename)) {
1391 self = PyObject_New(LogReaderObject, &LogReaderType);
1392 if (self != NULL) {
1393 self->filled = 0;
1394 self->index = 0;
1395 self->frametimings = 1;
1396 self->linetimings = 0;
1397 self->info = NULL;
1398 self->logfp = fopen(filename, "rb");
1399 if (self->logfp == NULL) {
1400 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
1401 Py_DECREF(self);
1402 self = NULL;
1403 goto finally;
1405 self->info = PyDict_New();
1406 if (self->info == NULL) {
1407 Py_DECREF(self);
1408 goto finally;
1410 /* Aggressively attempt to load all preliminary ADD_INFO
1411 * records from the log so the info records are available
1412 * from a fresh logreader object.
1414 logreader_refill(self);
1415 while (self->filled > self->index
1416 && self->buffer[self->index] == WHAT_ADD_INFO) {
1417 int err = unpack_add_info(self, 1);
1418 if (err) {
1419 if (err == ERR_EOF)
1420 eof_error();
1421 else
1422 PyErr_SetString(PyExc_RuntimeError,
1423 "unexpected error");
1424 break;
1426 /* Refill agressively so we can avoid EOF during
1427 * initialization unless there's a real EOF condition
1428 * (the tp_iternext handler loops attempts to refill
1429 * and try again).
1431 logreader_refill(self);
1435 finally:
1436 return (PyObject *) self;
1440 /* Return a Python string that represents the version number without the
1441 * extra cruft added by revision control, even if the right options were
1442 * given to the "cvs export" command to make it not include the extra
1443 * cruft.
1445 static char *
1446 get_version_string(void)
1448 static char *rcsid = "$Revision$";
1449 char *rev = rcsid;
1450 char *buffer;
1451 int i = 0;
1453 while (*rev && !isdigit(*rev))
1454 ++rev;
1455 while (rev[i] != ' ' && rev[i] != '\0')
1456 ++i;
1457 buffer = malloc(i + 1);
1458 if (buffer != NULL) {
1459 memmove(buffer, rev, i);
1460 buffer[i] = '\0';
1462 return buffer;
1465 /* Write out a RFC 822-style header with various useful bits of
1466 * information to make the output easier to manage.
1468 static int
1469 write_header(ProfilerObject *self)
1471 char *buffer;
1472 char cwdbuffer[PATH_MAX];
1473 PyObject *temp;
1474 int i, len;
1476 buffer = get_version_string();
1477 if (buffer == NULL) {
1478 PyErr_NoMemory();
1479 return -1;
1481 pack_add_info(self, "hotshot-version", buffer);
1482 pack_add_info(self, "requested-frame-timings",
1483 (self->frametimings ? "yes" : "no"));
1484 pack_add_info(self, "requested-line-events",
1485 (self->lineevents ? "yes" : "no"));
1486 pack_add_info(self, "requested-line-timings",
1487 (self->linetimings ? "yes" : "no"));
1488 pack_add_info(self, "platform", Py_GetPlatform());
1489 pack_add_info(self, "executable", Py_GetProgramFullPath());
1490 free(buffer);
1491 buffer = (char *) Py_GetVersion();
1492 if (buffer == NULL)
1493 PyErr_Clear();
1494 else
1495 pack_add_info(self, "executable-version", buffer);
1497 #ifdef MS_WIN32
1498 PyOS_snprintf(cwdbuffer, sizeof(cwdbuffer), "%I64d", frequency.QuadPart);
1499 pack_add_info(self, "reported-performance-frequency", cwdbuffer);
1500 #else
1501 PyOS_snprintf(cwdbuffer, sizeof(cwdbuffer), "%lu", rusage_diff);
1502 pack_add_info(self, "observed-interval-getrusage", cwdbuffer);
1503 PyOS_snprintf(cwdbuffer, sizeof(cwdbuffer), "%lu", timeofday_diff);
1504 pack_add_info(self, "observed-interval-gettimeofday", cwdbuffer);
1505 #endif
1507 pack_add_info(self, "current-directory",
1508 getcwd(cwdbuffer, sizeof cwdbuffer));
1510 temp = PySys_GetObject("path");
1511 len = PyList_GET_SIZE(temp);
1512 for (i = 0; i < len; ++i) {
1513 PyObject *item = PyList_GET_ITEM(temp, i);
1514 buffer = PyString_AsString(item);
1515 if (buffer == NULL)
1516 return -1;
1517 pack_add_info(self, "sys-path-entry", buffer);
1519 pack_frame_times(self);
1520 pack_line_times(self);
1522 return 0;
1525 static char profiler__doc__[] = "\
1526 profiler(logfilename[, lineevents[, linetimes]]) -> profiler\n\
1527 Create a new profiler object.";
1529 static PyObject *
1530 hotshot_profiler(PyObject *unused, PyObject *args)
1532 char *logfilename;
1533 ProfilerObject *self = NULL;
1534 int lineevents = 0;
1535 int linetimings = 1;
1537 if (PyArg_ParseTuple(args, "s|ii:profiler", &logfilename,
1538 &lineevents, &linetimings)) {
1539 self = PyObject_New(ProfilerObject, &ProfilerType);
1540 if (self == NULL)
1541 return NULL;
1542 self->frametimings = 1;
1543 self->lineevents = lineevents ? 1 : 0;
1544 self->linetimings = (lineevents && linetimings) ? 1 : 0;
1545 self->index = 0;
1546 self->active = 0;
1547 self->next_fileno = 0;
1548 self->logfp = NULL;
1549 self->logfilename = PyTuple_GET_ITEM(args, 0);
1550 Py_INCREF(self->logfilename);
1551 self->filemap = PyDict_New();
1552 if (self->filemap == NULL) {
1553 Py_DECREF(self);
1554 return NULL;
1556 self->logfp = fopen(logfilename, "wb");
1557 if (self->logfp == NULL) {
1558 Py_DECREF(self);
1559 PyErr_SetFromErrnoWithFilename(PyExc_IOError, logfilename);
1560 return NULL;
1562 if (timeofday_diff == 0) {
1563 /* Run this several times since sometimes the first
1564 * doesn't give the lowest values, and we're really trying
1565 * to determine the lowest.
1567 calibrate();
1568 calibrate();
1569 calibrate();
1571 if (write_header(self))
1572 /* some error occurred, exception has been set */
1573 self = NULL;
1575 return (PyObject *) self;
1578 static char coverage__doc__[] = "\
1579 coverage(logfilename) -> profiler\n\
1580 Returns a profiler that doesn't collect any timing information, which is\n\
1581 useful in building a coverage analysis tool.";
1583 static PyObject *
1584 hotshot_coverage(PyObject *unused, PyObject *args)
1586 char *logfilename;
1587 PyObject *result = NULL;
1589 if (PyArg_ParseTuple(args, "s:coverage", &logfilename)) {
1590 result = hotshot_profiler(unused, args);
1591 if (result != NULL) {
1592 ProfilerObject *self = (ProfilerObject *) result;
1593 self->frametimings = 0;
1594 self->linetimings = 0;
1595 self->lineevents = 1;
1598 return result;
1601 static char resolution__doc__[] =
1602 #ifdef MS_WIN32
1603 "resolution() -> (performance-counter-ticks, update-frequency)\n"
1604 "Return the resolution of the timer provided by the QueryPerformanceCounter()\n"
1605 "function. The first value is the smallest observed change, and the second\n"
1606 "is the result of QueryPerformanceFrequency().";
1607 #else
1608 "resolution() -> (gettimeofday-usecs, getrusage-usecs)\n"
1609 "Return the resolution of the timers provided by the gettimeofday() and\n"
1610 "getrusage() system calls, or -1 if the call is not supported.";
1611 #endif
1613 static PyObject *
1614 hotshot_resolution(PyObject *unused, PyObject *args)
1616 PyObject *result = NULL;
1618 if (PyArg_ParseTuple(args, ":resolution")) {
1619 if (timeofday_diff == 0) {
1620 calibrate();
1621 calibrate();
1622 calibrate();
1624 #ifdef MS_WIN32
1625 result = Py_BuildValue("ii", timeofday_diff, frequency.LowPart);
1626 #else
1627 result = Py_BuildValue("ii", timeofday_diff, rusage_diff);
1628 #endif
1630 return result;
1634 static PyMethodDef functions[] = {
1635 {"coverage", hotshot_coverage, METH_VARARGS, coverage__doc__},
1636 {"profiler", hotshot_profiler, METH_VARARGS, profiler__doc__},
1637 {"logreader", hotshot_logreader, METH_VARARGS, logreader__doc__},
1638 {"resolution", hotshot_resolution, METH_VARARGS, resolution__doc__},
1639 {NULL, NULL}
1643 void
1644 init_hotshot(void)
1646 PyObject *module;
1648 LogReaderType.ob_type = &PyType_Type;
1649 ProfilerType.ob_type = &PyType_Type;
1650 module = Py_InitModule("_hotshot", functions);
1651 if (module != NULL) {
1652 char *s = get_version_string();
1654 PyModule_AddStringConstant(module, "__version__", s);
1655 free(s);
1656 Py_INCREF(&LogReaderType);
1657 PyModule_AddObject(module, "LogReaderType",
1658 (PyObject *)&LogReaderType);
1659 Py_INCREF(&ProfilerType);
1660 PyModule_AddObject(module, "ProfilerType",
1661 (PyObject *)&ProfilerType);
1663 if (ProfilerError == NULL)
1664 ProfilerError = PyErr_NewException("hotshot.ProfilerError",
1665 NULL, NULL);
1666 if (ProfilerError != NULL) {
1667 Py_INCREF(ProfilerError);
1668 PyModule_AddObject(module, "ProfilerError", ProfilerError);
1670 PyModule_AddIntConstant(module, "WHAT_ENTER", WHAT_ENTER);
1671 PyModule_AddIntConstant(module, "WHAT_EXIT", WHAT_EXIT);
1672 PyModule_AddIntConstant(module, "WHAT_LINENO", WHAT_LINENO);
1673 PyModule_AddIntConstant(module, "WHAT_OTHER", WHAT_OTHER);
1674 PyModule_AddIntConstant(module, "WHAT_ADD_INFO", WHAT_ADD_INFO);
1675 PyModule_AddIntConstant(module, "WHAT_DEFINE_FILE", WHAT_DEFINE_FILE);
1676 PyModule_AddIntConstant(module, "WHAT_DEFINE_FUNC", WHAT_DEFINE_FUNC);
1677 PyModule_AddIntConstant(module, "WHAT_LINE_TIMES", WHAT_LINE_TIMES);