2 * This is the High Performance Python Profiler portion of HotShot.
8 #include "frameobject.h"
9 #include "structmember.h"
16 * Which timer to use should be made more configurable, but that should not
17 * be difficult. This will do for now.
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; }
31 #ifndef HAVE_GETTIMEOFDAY
32 #error "This module requires gettimeofday() on non-Windows platforms!"
37 #include <sys/resource.h>
38 #include <sys/times.h>
40 typedef struct timeval hs_time
;
43 #if !defined(__cplusplus) && !defined(inline)
45 #define inline __inline
53 #define BUFFERSIZE 10240
61 # define PATH_MAX MAX_PATH
63 # error "Need a defn. for PATH_MAX in _hotshot.c"
70 PyObject
*logfilename
;
72 unsigned char buffer
[BUFFERSIZE
];
80 hs_time prev_timeofday
;
91 unsigned char buffer
[BUFFERSIZE
];
94 static PyObject
* ProfilerError
= NULL
;
98 #ifdef GETTIMEOFDAY_NO_TZ
99 #define GETTIMEOFDAY(ptv) gettimeofday((ptv))
101 #define GETTIMEOFDAY(ptv) gettimeofday((ptv), (struct timezone *)NULL)
106 /* The log reader... */
108 static char logreader_close__doc__
[] =
110 "Close the log file, preventing additional records from being read.";
113 logreader_close(LogReaderObject
*self
, PyObject
*args
)
115 PyObject
*result
= NULL
;
116 if (PyArg_ParseTuple(args
, ":close")) {
117 if (self
->logfp
!= NULL
) {
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.
133 logreader_tp_iter(LogReaderObject
*self
)
136 return (PyObject
*) self
;
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"
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
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.
188 * MPI(2,type) fileno -- type is 0x00
190 * PI tdelta -- iff frame times are enabled
194 * MPI(2,type) tdelta -- type is 0x01; tdelta will be 0
195 * if frame times are disabled
199 * MPI(2,type) lineno -- type is 0x02
200 * PI tdelta -- iff LINENO includes it
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
214 * PI len -- length of filename
215 * BYTE filename[len] -- len bytes of string data
217 * DEFINE_FUNC records
219 * BYTE type -- always 0x43
222 * PI len -- length of funcname
223 * BYTE funcname[len] -- len bytes of string data
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
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
243 * BYTE type -- always 0x53
244 * BYTE have_tdelta -- 0 if ENTER/EXIT do *not* have
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
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.
274 unpack_packed_int(LogReaderObject
*self
, int *pvalue
, int discard
)
278 int index
= self
->index
;
282 if (index
>= self
->filled
)
285 accum
|= ((self
->buffer
[index
] & 0x7F) >> discard
) << bits
;
286 bits
+= (7 - discard
);
287 cont
= self
->buffer
[index
] & 0x80;
300 /* Unpack a string, which is encoded as a packed integer giving the
301 * length of the string, followed by the string data.
304 unpack_string(LogReaderObject
*self
, PyObject
**pvalue
)
307 int oldindex
= self
->index
;
308 int err
= unpack_packed_int(self
, &len
, 0);
311 /* need at least len bytes in buffer */
312 if (len
> (self
->filled
- self
->index
)) {
313 self
->index
= oldindex
;
317 *pvalue
= PyString_FromStringAndSize((char *)self
->buffer
+ self
->index
,
319 if (*pvalue
== NULL
) {
320 self
->index
= oldindex
;
332 unpack_add_info(LogReaderObject
*self
, int skip_opcode
)
335 PyObject
*value
= NULL
;
339 if (self
->buffer
[self
->index
] != WHAT_ADD_INFO
)
340 return ERR_BAD_RECTYPE
;
343 err
= unpack_string(self
, &key
);
345 err
= unpack_string(self
, &value
);
349 PyObject
*list
= PyDict_GetItem(self
->info
, key
);
351 list
= PyList_New(0);
356 if (PyDict_SetItem(self
->info
, key
, list
)) {
361 if (PyList_Append(list
, value
))
373 logreader_refill(LogReaderObject
*self
)
379 memmove(self
->buffer
, &self
->buffer
[self
->index
],
380 self
->filled
- self
->index
);
381 self
->filled
= self
->filled
- self
->index
;
384 needed
= BUFFERSIZE
- self
->filled
;
386 res
= fread(&self
->buffer
[self
->filled
], 1, needed
, self
->logfp
);
394 PyErr_SetString(PyExc_EOFError
,
395 "end of file with incomplete profile record");
399 logreader_tp_iternext(LogReaderObject
*self
)
406 PyObject
*s1
= NULL
, *s2
= NULL
;
407 PyObject
*result
= NULL
;
409 unsigned char b0
, b1
;
412 if (self
->logfp
== NULL
) {
413 PyErr_SetString(ProfilerError
,
414 "cannot iterate over closed LogReader object");
418 if ((self
->filled
- self
->index
) < MAXEVENTSIZE
)
419 logreader_refill(self
);
422 if (self
->filled
== 0)
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
];
435 err
= unpack_packed_int(self
, &fileno
, 2);
437 err
= unpack_packed_int(self
, &lineno
, 0);
438 if (self
->frametimings
&& !err
)
439 err
= unpack_packed_int(self
, &tdelta
, 0);
443 err
= unpack_packed_int(self
, &tdelta
, 2);
446 err
= unpack_packed_int(self
, &lineno
, 2);
447 if (self
->linetimings
&& !err
)
448 err
= unpack_packed_int(self
, &tdelta
, 0);
451 err
= unpack_add_info(self
, 0);
453 case WHAT_DEFINE_FILE
:
454 err
= unpack_packed_int(self
, &fileno
, 0);
456 err
= unpack_string(self
, &s1
);
463 case WHAT_DEFINE_FUNC
:
464 err
= unpack_packed_int(self
, &fileno
, 0);
466 err
= unpack_packed_int(self
, &lineno
, 0);
468 err
= unpack_string(self
, &s1
);
471 case WHAT_LINE_TIMES
:
472 if (self
->index
>= self
->filled
)
475 self
->linetimings
= self
->buffer
[self
->index
] ? 1 : 0;
480 case WHAT_FRAME_TIMES
:
481 if (self
->index
>= self
->filled
)
484 self
->frametimings
= self
->buffer
[self
->index
] ? 1 : 0;
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.
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. */
509 result
= PyTuple_New(4);
510 PyTuple_SET_ITEM(result
, 0, PyInt_FromLong(what
));
511 PyTuple_SET_ITEM(result
, 2, PyInt_FromLong(fileno
));
513 PyTuple_SET_ITEM(result
, 1, PyInt_FromLong(tdelta
));
515 PyTuple_SET_ITEM(result
, 1, s1
);
517 PyTuple_SET_ITEM(result
, 3, PyInt_FromLong(lineno
));
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.
525 b0
= self
->buffer
[self
->index
];
526 b1
= self
->buffer
[self
->index
+ 1];
528 /* This is a line-number event. */
530 lineno
= ((b0
& ~1) << 7) + b1
;
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]);
552 logreader_dealloc(LogReaderObject
*self
)
554 if (self
->logfp
!= NULL
) {
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");
572 static char next__doc__
[] =
573 "next() -> event-info\n"
574 "Return the next event record from the log file.";
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()) {
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
)
602 memmove(self
->buffer
, &self
->buffer
[written
],
603 self
->index
- written
);
604 self
->index
-= written
;
606 char *s
= PyString_AsString(self
->logfilename
);
607 PyErr_SetFromErrnoWithFilename(PyExc_IOError
, s
);
612 if (fflush(self
->logfp
)) {
613 char *s
= PyString_AsString(self
->logfilename
);
614 PyErr_SetFromErrnoWithFilename(PyExc_IOError
, s
);
622 pack_packed_int(ProfilerObject
*self
, int value
)
624 unsigned char partial
;
627 partial
= value
& 0x7F;
631 self
->buffer
[self
->index
] = partial
;
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.
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
) {
652 self
->buffer
[self
->index
] = b
;
654 pack_packed_int(self
, value
>> bits
);
657 self
->buffer
[self
->index
] = b
;
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
);
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
;
682 pack_string(self
, s1
, len1
);
683 pack_string(self
, s2
, len2
);
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
;
695 pack_packed_int(self
, fileno
);
696 pack_string(self
, filename
, len
);
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
;
709 pack_packed_int(self
, fileno
);
710 pack_packed_int(self
, lineno
);
711 pack_string(self
, funcname
, len
);
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;
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;
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
);
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
);
753 self
->buffer
[self
->index
] = WHAT_EXIT
;
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
);
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
);
776 get_fileno(ProfilerObject
*self
, PyCodeObject
*fcode
)
778 /* This is only used for ENTER events. */
784 obj
= PyDict_GetItem(self
->filemap
, fcode
->co_filename
);
786 /* first sighting of this file */
791 fileno
= self
->next_fileno
;
792 obj
= Py_BuildValue("iN", fileno
, dict
);
796 if (PyDict_SetItem(self
->filemap
, fcode
->co_filename
, obj
)) {
802 pack_define_file(self
, fileno
, PyString_AS_STRING(fcode
->co_filename
));
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
);
812 /* We just won't have it saved; too bad. */
816 PyObject
*name
= PyDict_GetItem(dict
, obj
);
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
))
828 get_tdelta(ProfilerObject
*self
)
836 diff
= tv
- self
->prev_timeofday
;
843 if (tv
.tv_sec
== self
->prev_timeofday
.tv_sec
)
844 tdelta
= tv
.tv_usec
- self
->prev_timeofday
.tv_usec
;
846 tdelta
= ((tv
.tv_sec
- self
->prev_timeofday
.tv_sec
) * 1000000
849 self
->prev_timeofday
= tv
;
854 /* The workhorse: the profiler callback function. */
857 profiler_callback(ProfilerObject
*self
, PyFrameObject
*frame
, int what
,
863 if (self
->frametimings
)
864 tdelta
= get_tdelta(self
);
867 fileno
= get_fileno(self
, frame
->f_code
);
870 pack_enter(self
, fileno
, tdelta
,
871 frame
->f_code
->co_firstlineno
);
874 pack_exit(self
, tdelta
);
877 /* should never get here */
884 /* Alternate callback when we want PyTrace_LINE events */
887 tracer_callback(ProfilerObject
*self
, PyFrameObject
*frame
, int what
,
894 fileno
= get_fileno(self
, frame
->f_code
);
897 pack_enter(self
, fileno
, self
->frametimings
? get_tdelta(self
) : -1,
898 frame
->f_code
->co_firstlineno
);
901 pack_exit(self
, get_tdelta(self
));
904 if (self
->linetimings
)
905 pack_lineno_tdelta(self
, frame
->f_lineno
, get_tdelta(self
));
907 pack_lineno(self
, frame
->f_lineno
);
910 /* ignore PyTrace_EXCEPTION */
917 /* A couple of useful helper functions. */
920 static LARGE_INTEGER frequency
= {0, 0};
923 static unsigned long timeofday_diff
= 0;
924 static unsigned long rusage_diff
= 0;
933 QueryPerformanceFrequency(&frequency
);
942 timeofday_diff
= (unsigned long)diff
;
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
;
950 timeofday_diff
= (1000000 - tv1
.tv_usec
) + tv2
.tv_usec
;
955 #if defined(MS_WIN32) || defined(macintosh)
959 struct rusage ru1
, ru2
;
961 getrusage(RUSAGE_SELF
, &ru1
);
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
);
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
;
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
);
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
;
988 do_start(ProfilerObject
*self
)
991 GETTIMEOFDAY(&self
->prev_timeofday
);
992 if (self
->lineevents
)
993 PyEval_SetTrace((Py_tracefunc
) tracer_callback
, (PyObject
*)self
);
995 PyEval_SetProfile((Py_tracefunc
) profiler_callback
, (PyObject
*)self
);
999 do_stop(ProfilerObject
*self
)
1003 if (self
->lineevents
)
1004 PyEval_SetTrace(NULL
, NULL
);
1006 PyEval_SetProfile(NULL
, NULL
);
1008 if (self
->index
> 0) {
1009 /* Best effort to dump out any remaining data. */
1015 is_available(ProfilerObject
*self
)
1018 PyErr_SetString(ProfilerError
, "profiler already active");
1021 if (self
->logfp
== NULL
) {
1022 PyErr_SetString(ProfilerError
, "profiler already closed");
1029 /* Profiler object interface methods. */
1031 static char addinfo__doc__
[] =
1032 "addinfo(key, value)\n"
1033 "Insert an ADD_INFO record into the log.";
1036 profiler_addinfo(ProfilerObject
*self
, PyObject
*args
)
1038 PyObject
*result
= NULL
;
1041 if (PyArg_ParseTuple(args
, "ss:addinfo", &key
, &value
)) {
1042 if (self
->logfp
== NULL
)
1043 PyErr_SetString(ProfilerError
, "profiler already closed");
1045 pack_add_info(self
, key
, value
);
1053 static char close__doc__
[] =
1055 "Shut down this profiler and close the log files, even if its active.";
1058 profiler_close(ProfilerObject
*self
, PyObject
*args
)
1060 PyObject
*result
= NULL
;
1062 if (PyArg_ParseTuple(args
, ":close")) {
1064 if (self
->logfp
!= NULL
) {
1065 fclose(self
->logfp
);
1074 static char runcall__doc__
[] =
1075 "runcall(callable[, args[, kw]]) -> callable()\n"
1076 "Profile a specific function call, returning the result of that call.";
1079 profiler_runcall(ProfilerObject
*self
, PyObject
*args
)
1081 PyObject
*result
= NULL
;
1082 PyObject
*callargs
= NULL
;
1083 PyObject
*callkw
= NULL
;
1086 if (PyArg_ParseTuple(args
, "O|OO:runcall",
1087 &callable
, &callargs
, &callkw
)) {
1088 if (is_available(self
)) {
1090 result
= PyEval_CallObjectWithKeywords(callable
, callargs
, callkw
);
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.";
1103 profiler_runcode(ProfilerObject
*self
, PyObject
*args
)
1105 PyObject
*result
= NULL
;
1108 PyObject
*locals
= NULL
;
1110 if (PyArg_ParseTuple(args
, "O!O!|O:runcode",
1111 &PyCode_Type
, &code
,
1112 &PyDict_Type
, &globals
,
1114 if (is_available(self
)) {
1115 if (locals
== NULL
|| locals
== Py_None
)
1117 else if (!PyDict_Check(locals
)) {
1118 PyErr_SetString(PyExc_TypeError
,
1119 "locals must be a dictionary or None");
1123 result
= PyEval_EvalCode(code
, globals
, locals
);
1126 if (!PyErr_Occurred()) {
1136 static char start__doc__
[] =
1138 "Install this profiler for the current thread.";
1141 profiler_start(ProfilerObject
*self
, PyObject
*args
)
1143 PyObject
*result
= NULL
;
1145 if (PyArg_ParseTuple(args
, ":start")) {
1146 if (is_available(self
))
1152 static char stop__doc__
[] =
1154 "Remove this profiler from the current thread.";
1157 profiler_stop(ProfilerObject
*self
, PyObject
*args
)
1159 PyObject
*result
= NULL
;
1161 if (PyArg_ParseTuple(args
, ":stop")) {
1163 PyErr_SetString(ProfilerError
, "profiler not active");
1171 /* Python API support. */
1174 profiler_dealloc(ProfilerObject
*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__
},
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
},
1210 profiler_getattr(ProfilerObject
*self
, char *name
)
1213 if (strcmp(name
, "closed") == 0) {
1214 result
= (self
->logfp
== NULL
) ? Py_True
: Py_False
;
1218 result
= PyMember_Get((char *)self
, profiler_members
, name
);
1219 if (result
== NULL
) {
1221 result
= Py_FindMethod(profiler_methods
, (PyObject
*)self
, name
);
1228 static char profiler_object__doc__
[] =
1229 "High-performance profiler object.\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"
1239 "Attributes (read-only):\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
)
1249 "_hotshot.ProfilerType", /* tp_name */
1250 (int) sizeof(ProfilerObject
), /* tp_basicsize */
1251 0, /* tp_itemsize */
1252 (destructor
)profiler_dealloc
, /* tp_dealloc */
1254 (getattrfunc
)profiler_getattr
, /* tp_getattr */
1258 0, /* tp_as_number */
1259 0, /* tp_as_sequence */
1260 0, /* tp_as_mapping */
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
,
1281 logreader_getattr(LogReaderObject
*self
, char *name
)
1283 if (strcmp(name
, "info") == 0) {
1284 Py_INCREF(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
= {
1299 (intargfunc
)logreader_sq_item
, /* sq_item */
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
)
1311 "_hotshot.LogReaderType", /* tp_name */
1312 (int) sizeof(LogReaderObject
), /* tp_basicsize */
1313 0, /* tp_itemsize */
1314 (destructor
)logreader_dealloc
, /* tp_dealloc */
1316 (getattrfunc
)logreader_getattr
, /* tp_getattr */
1320 0, /* tp_as_number */
1321 &logreader_as_sequence
, /* tp_as_sequence */
1322 0, /* tp_as_mapping */
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 */
1334 0, /* tp_richcompare */
1335 0, /* tp_weaklistoffset */
1336 (getiterfunc
)logreader_tp_iter
, /* tp_iter */
1337 (iternextfunc
)logreader_tp_iternext
,/* tp_iternext */
1342 hotshot_logreader(PyObject
*unused
, PyObject
*args
)
1344 LogReaderObject
*self
= NULL
;
1347 if (PyArg_ParseTuple(args
, "s:logreader", &filename
)) {
1348 self
= PyObject_New(LogReaderObject
, &LogReaderType
);
1352 self
->frametimings
= 1;
1353 self
->linetimings
= 0;
1355 self
->logfp
= fopen(filename
, "rb");
1356 if (self
->logfp
== NULL
) {
1357 PyErr_SetFromErrnoWithFilename(PyExc_IOError
, filename
);
1362 self
->info
= PyDict_New();
1363 if (self
->info
== NULL
) {
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);
1379 PyErr_SetString(PyExc_RuntimeError
,
1380 "unexpected error");
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
1388 logreader_refill(self
);
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
1403 get_version_string(void)
1405 static char *rcsid
= "$Revision$";
1410 while (*rev
&& !isdigit(*rev
))
1412 while (rev
[i
] != ' ' && rev
[i
] != '\0')
1414 buffer
= malloc(i
+ 1);
1415 if (buffer
!= NULL
) {
1416 memmove(buffer
, rev
, i
);
1422 /* Write out a RFC 822-style header with various useful bits of
1423 * information to make the output easier to manage.
1426 write_header(ProfilerObject
*self
)
1429 char cwdbuffer
[PATH_MAX
];
1433 buffer
= get_version_string();
1434 if (buffer
== NULL
) {
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());
1448 buffer
= (char *) Py_GetVersion();
1452 pack_add_info(self
, "executable-version", buffer
);
1455 sprintf(cwdbuffer
, "%I64d", frequency
.QuadPart
);
1456 pack_add_info(self
, "reported-performance-frequency", cwdbuffer
);
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
);
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
);
1474 pack_add_info(self
, "sys-path-entry", buffer
);
1476 pack_frame_times(self
);
1477 pack_line_times(self
);
1482 static char profiler__doc__
[] = "\
1483 profiler(logfilename[, lineevents[, linetimes]]) -> profiler\n\
1484 Create a new profiler object.";
1487 hotshot_profiler(PyObject
*unused
, PyObject
*args
)
1490 ProfilerObject
*self
= NULL
;
1492 int linetimings
= 1;
1494 if (PyArg_ParseTuple(args
, "s|ii:profiler", &logfilename
,
1495 &lineevents
, &linetimings
)) {
1496 self
= PyObject_New(ProfilerObject
, &ProfilerType
);
1499 self
->frametimings
= 1;
1500 self
->lineevents
= lineevents
? 1 : 0;
1501 self
->linetimings
= (lineevents
&& linetimings
) ? 1 : 0;
1504 self
->next_fileno
= 0;
1506 self
->logfilename
= PyTuple_GET_ITEM(args
, 0);
1507 Py_INCREF(self
->logfilename
);
1508 self
->filemap
= PyDict_New();
1509 if (self
->filemap
== NULL
) {
1513 self
->logfp
= fopen(logfilename
, "wb");
1514 if (self
->logfp
== NULL
) {
1516 PyErr_SetFromErrnoWithFilename(PyExc_IOError
, logfilename
);
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.
1528 if (write_header(self
))
1529 /* some error occurred, exception has been set */
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.";
1541 hotshot_coverage(PyObject
*unused
, PyObject
*args
)
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;
1558 static char resolution__doc__
[] =
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().";
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.";
1571 hotshot_resolution(PyObject
*unused
, PyObject
*args
)
1573 PyObject
*result
= NULL
;
1575 if (PyArg_ParseTuple(args
, ":resolution")) {
1576 if (timeofday_diff
== 0) {
1582 result
= Py_BuildValue("ii", timeofday_diff
, frequency
.LowPart
);
1584 result
= Py_BuildValue("ii", timeofday_diff
, rusage_diff
);
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__
},
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
);
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",
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
);