blob: 0465d06a58da9c7364c9882b7360d86961ac759a [file] [log] [blame]
Fred Drake8c081a12001-10-12 20:57:55 +00001/*
2 * This is the High Performance Python Profiler portion of HotShot.
3 */
4
5#include <Python.h>
6#include <compile.h>
7#include <eval.h>
8#include <frameobject.h>
9#include <structmember.h>
10
11#ifdef HAVE_UNISTD_H
12#include <unistd.h>
13#endif
14
15/*
16 * Which timer to use should be made more configurable, but that should not
Tim Petersfeab23f2001-10-13 00:11:10 +000017 * be difficult. This will do for now.
Fred Drake8c081a12001-10-12 20:57:55 +000018 */
19#ifdef MS_WIN32
20#include <windows.h>
21#include <largeint.h>
Tim Peters1566a172001-10-12 22:08:39 +000022#include <direct.h> /* for getcwd() */
Fred Drake8c081a12001-10-12 20:57:55 +000023typedef LARGE_INTEGER hs_time;
Tim Petersfeab23f2001-10-13 00:11:10 +000024
Fred Drake8c081a12001-10-12 20:57:55 +000025#else
26#ifndef HAVE_GETTIMEOFDAY
27#error "This module requires gettimeofday() on non-Windows platforms!"
28#endif
29#include <sys/resource.h>
30#include <sys/times.h>
31typedef struct timeval hs_time;
32#endif
33
34#if !defined(__cplusplus) && !defined(inline)
35#ifdef __GNUC__
36#define inline __inline
37#endif
38#endif
39
40#ifndef inline
41#define inline
42#endif
43
44#define BUFFERSIZE 10240
45
Tim Peters1566a172001-10-12 22:08:39 +000046#ifndef PATH_MAX
47# ifdef MAX_PATH
48# define PATH_MAX MAX_PATH
49# else
50# error "Need a defn. for PATH_MAX in _hotshot.c"
51# endif
52#endif
53
Fred Drake8c081a12001-10-12 20:57:55 +000054typedef struct {
55 PyObject_HEAD
56 PyObject *filemap;
57 PyObject *logfilename;
58 int index;
59 unsigned char buffer[BUFFERSIZE];
60 FILE *logfp;
61 int lineevents;
62 int linetimings;
63 /* size_t filled; */
64 int active;
65 int next_fileno;
66 long callcount;
67 hs_time prev_timeofday;
68} ProfilerObject;
69
70typedef struct {
71 PyObject_HEAD
72 FILE *logfp;
73 int filled;
74 int index;
75 int linetimings;
76 unsigned char buffer[BUFFERSIZE];
77} LogReaderObject;
78
79static PyObject * ProfilerError = NULL;
80
81
82#ifndef MS_WIN32
83#ifdef GETTIMEOFDAY_NO_TZ
84#define GETTIMEOFDAY(ptv) gettimeofday((ptv))
85#else
86#define GETTIMEOFDAY(ptv) gettimeofday((ptv), (struct timezone *)NULL)
87#endif
88#endif
89
90
91/* The log reader... */
92
Tim Petersfeab23f2001-10-13 00:11:10 +000093static char logreader_close__doc__[] =
94"close()\n"
95"Close the log file, preventing additional records from being read.";
Fred Drake8c081a12001-10-12 20:57:55 +000096
97static PyObject *
98logreader_close(LogReaderObject *self, PyObject *args)
99{
100 PyObject *result = NULL;
101 if (PyArg_ParseTuple(args, ":close")) {
102 if (self->logfp != NULL) {
103 fclose(self->logfp);
104 self->logfp = NULL;
105 }
106 result = Py_None;
107 Py_INCREF(result);
108 }
109 return result;
110}
111
112#if Py_TPFLAGS_HAVE_ITER
113/* This is only used if the interpreter has iterator support; the
114 * iternext handler is also used as a helper for other functions, so
115 * does not need to be included in this conditional section.
116 */
117static PyObject *
118logreader_tp_iter(LogReaderObject *self)
119{
120 Py_INCREF(self);
121 return (PyObject *) self;
122}
123#endif
124
125
126/* Log File Format
127 * ---------------
128 *
129 * The log file consists of a sequence of variable-length records.
130 * Each record is identified with a record type identifier in two
131 * bits of the first byte. The two bits are the "least significant"
132 * bits of the byte.
133 *
134 * Low bits: Opcode: Meaning:
135 * 0x00 ENTER enter a frame
136 * 0x01 EXIT exit a frame
137 * 0x02 LINENO SET_LINENO instruction was executed
138 * 0x03 OTHER more bits are needed to deecode
139 *
140 * If the type is OTHER, the record is not packed so tightly, and the
141 * remaining bits are used to disambiguate the record type. These
142 * records are not used as frequently so compaction is not an issue.
143 * Each of the first three record types has a highly tailored
144 * structure that allows it to be packed tightly.
145 *
146 * The OTHER records have the following identifiers:
147 *
148 * First byte: Opcode: Meaning:
149 * 0x13 ADD_INFO define a key/value pair
150 * 0x23 DEFINE_FILE define an int->filename mapping
151 * 0x33 LINE_TIMES indicates if LINENO events have tdeltas
152 *
153 * Packed Integers
154 *
155 * "Packed integers" are non-negative integer values encoded as a
156 * sequence of bytes. Each byte is encoded such that the most
157 * significant bit is set if the next byte is also part of the
158 * integer. Each byte provides bits to the least-significant end of
159 * the result; the accumulated value must be shifted up to place the
160 * new bits into the result.
161 *
162 * "Modified packed integers" are packed integers where only a portion
163 * of the first byte is used. In the rest of the specification, these
164 * are referred to as "MPI(n,name)", where "n" is the number of bits
165 * discarded from the least-signicant positions of the byte, and
166 * "name" is a name being given to those "discarded" bits, since they
167 * are a field themselves.
168 *
169 * ENTER records:
170 *
171 * MPI(2,type) fileno -- type is 0x00
172 * PI tdelta
173 * PI lineno
174 *
175 * EXIT records
176 *
177 * MPI(2,type) tdelta -- type is 0x01
178 *
179 * LINENO records
180 *
181 * MPI(2,type) lineno -- type is 0x02
182 * PI tdelta -- iff LINENO includes it
183 *
184 * ADD_INFO records
185 *
186 * BYTE type -- always 0x03
187 * PI len1 -- length of first string
188 * BYTE string1[len1] -- len1 bytes of string data
189 * PI len2 -- length of second string
190 * BYTE string2[len2] -- len2 bytes of string data
191 *
192 * DEFINE_FILE records
193 *
194 * BYTE type -- always 0x13
195 * PI fileno
196 * PI len -- length of filename
197 * BYTE filename[len] -- len bytes of string data
198 *
199 * LINE_TIMES records
200 * BYTE type -- always 0x23
201 * BYTE have_tdelta -- 0 if LINENO does *not* have
202 * timing information
203 */
204
205#define WHAT_ENTER 0x00
206#define WHAT_EXIT 0x01
207#define WHAT_LINENO 0x02
208#define WHAT_OTHER 0x03 /* only used in decoding */
209#define WHAT_ADD_INFO 0x13
210#define WHAT_DEFINE_FILE 0x23
211#define WHAT_LINE_TIMES 0x33
212
213#define ERR_NONE 0
214#define ERR_EOF -1
215#define ERR_EXCEPTION -2
216
217#define PISIZE (sizeof(int) + 1)
218#define MPISIZE (PISIZE + 1)
219
220/* Maximum size of "normal" events -- nothing that contains string data */
221#define MAXEVENTSIZE (MPISIZE + PISIZE*2)
222
223
224/* Unpack a packed integer; if "discard" is non-zero, unpack a modified
225 * packed integer with "discard" discarded bits.
226 */
227static int
228unpack_packed_int(LogReaderObject *self, int *pvalue, int discard)
229{
230 int accum = 0;
231 int bits = 0;
232 int index = self->index;
233 int cont;
234
235 do {
236 if (index >= self->filled)
237 return ERR_EOF;
238 /* read byte */
239 accum |= ((self->buffer[index] & 0x7F) >> discard) << bits;
240 bits += (7 - discard);
241 cont = self->buffer[index] & 0x80;
242 /* move to next */
243 discard = 0;
244 index++;
245 } while (cont);
246
247 /* save state */
248 self->index = index;
249 *pvalue = accum;
250
251 return 0;
252}
253
254/* Unpack a string, which is encoded as a packed integer giving the
255 * length of the string, followed by the string data.
256 */
257static int
258unpack_string(LogReaderObject *self, PyObject **pvalue)
259{
260 int len;
261 int oldindex = self->index;
262 int err = unpack_packed_int(self, &len, 0);
263
264 if (!err) {
265 /* need at least len bytes in buffer */
266 if (len > (self->filled - self->index)) {
267 self->index = oldindex;
268 err = ERR_EOF;
269 }
270 else {
271 *pvalue = PyString_FromStringAndSize(self->buffer + self->index,
272 len);
273 if (*pvalue == NULL) {
274 self->index = oldindex;
275 err = ERR_EXCEPTION;
276 }
277 else
278 self->index += len;
279 }
280 }
281 return err;
282}
283
284
285static PyObject *
286logreader_tp_iternext(LogReaderObject *self)
287{
288 int what, oldindex;
289 int err = ERR_NONE;
290 int lineno = -1;
291 int fileno = -1;
292 int tdelta = -1;
293 PyObject *s1 = NULL, *s2 = NULL;
294 PyObject *result = NULL;
295#if 0
296 unsigned char b0, b1;
297#endif
298
299 if (self->logfp == NULL) {
300 PyErr_SetString(ProfilerError,
301 "cannot iterate over closed LogReader object");
302 return NULL;
303 }
304 restart:
305 if ((self->filled - self->index) < MAXEVENTSIZE) {
306 /* add a little to the buffer */
307 int needed;
308 size_t res;
309 refill:
310 if (self->index) {
311 memmove(self->buffer, &self->buffer[self->index],
312 self->filled - self->index);
313 self->filled = self->filled - self->index;
314 self->index = 0;
315 }
316 needed = BUFFERSIZE - self->filled;
317 res = fread(&self->buffer[self->filled], 1, needed, self->logfp);
318 self->filled += res;
319 }
320 /* end of input */
321 if (self->filled == 0)
322 return NULL;
323
324 oldindex = self->index;
325
326 /* decode the record type */
327 what = self->buffer[self->index] & WHAT_OTHER;
328 if (what == WHAT_OTHER) {
329 what = self->buffer[self->index];
330 self->index++;
331 }
332 switch (what) {
333 case WHAT_ENTER:
334 err = unpack_packed_int(self, &fileno, 2);
335 if (!err) {
336 err = unpack_packed_int(self, &tdelta, 0);
337 if (!err)
338 err = unpack_packed_int(self, &lineno, 0);
339 }
340 break;
341 case WHAT_EXIT:
342 err = unpack_packed_int(self, &tdelta, 2);
343 break;
344 case WHAT_LINENO:
345 err = unpack_packed_int(self, &lineno, 2);
346 if (self->linetimings && !err)
347 err = unpack_packed_int(self, &tdelta, 0);
348 break;
349 case WHAT_ADD_INFO:
350 err = unpack_string(self, &s1);
351 if (!err) {
352 err = unpack_string(self, &s2);
353 if (err) {
354 Py_DECREF(s1);
355 s1 = NULL;
356 }
357 }
358 break;
359 case WHAT_DEFINE_FILE:
360 err = unpack_packed_int(self, &fileno, 0);
361 if (!err) {
362 err = unpack_string(self, &s1);
363 if (!err) {
364 Py_INCREF(Py_None);
365 s2 = Py_None;
366 }
367 }
368 break;
369 case WHAT_LINE_TIMES:
370 if (self->index >= self->filled)
371 err = ERR_EOF;
372 else {
373 self->linetimings = self->buffer[self->index] ? 1 : 0;
374 self->index++;
375 goto restart;
376 }
377 default:
Tim Peters1566a172001-10-12 22:08:39 +0000378 ;
Fred Drake8c081a12001-10-12 20:57:55 +0000379 }
380 if (err == ERR_EOF && oldindex != 0) {
381 /* It looks like we ran out of data before we had it all; this
382 * could easily happen with large packed integers or string
383 * data. Try forcing the buffer to be re-filled before failing.
384 */
385 err = ERR_NONE;
386 goto refill;
387 }
388 if (err == ERR_EOF) {
389 /* Could not avoid end-of-buffer error. */
390 PyErr_SetString(PyExc_EOFError,
391 "end of file with incomplete profile record");
392 }
393 else if (!err) {
394 result = PyTuple_New(4);
395 PyTuple_SET_ITEM(result, 0, PyInt_FromLong(what));
396 PyTuple_SET_ITEM(result, 2, PyInt_FromLong(fileno));
397 if (s1 == NULL) {
398 PyTuple_SET_ITEM(result, 1, PyInt_FromLong(tdelta));
399 PyTuple_SET_ITEM(result, 3, PyInt_FromLong(lineno));
400 }
401 else {
402 PyTuple_SET_ITEM(result, 1, s1);
403 PyTuple_SET_ITEM(result, 3, s2);
404 }
405 }
406 /* The only other case is err == ERR_EXCEPTION, in which case the
407 * exception is already set.
408 */
409#if 0
410 b0 = self->buffer[self->index];
411 b1 = self->buffer[self->index + 1];
412 if (b0 & 1) {
413 /* This is a line-number event. */
414 what = PyTrace_LINE;
415 lineno = ((b0 & ~1) << 7) + b1;
416 self->index += 2;
417 }
418 else {
419 what = (b0 & 0x0E) >> 1;
420 tdelta = ((b0 & 0xF0) << 4) + b1;
421 if (what == PyTrace_CALL) {
422 /* we know there's a 2-byte file ID & 2-byte line number */
423 fileno = ((self->buffer[self->index + 2] << 8)
424 + self->buffer[self->index + 3]);
425 lineno = ((self->buffer[self->index + 4] << 8)
426 + self->buffer[self->index + 5]);
427 self->index += 6;
428 }
429 else
430 self->index += 2;
431 }
432#endif
433 return result;
434}
435
436static void
437logreader_dealloc(LogReaderObject *self)
438{
439 if (self->logfp != NULL) {
440 fclose(self->logfp);
441 self->logfp = NULL;
442 }
443 PyObject_Del(self);
444}
445
446static PyObject *
447logreader_sq_item(LogReaderObject *self, int index)
448{
449 PyObject *result = logreader_tp_iternext(self);
450 if (result == NULL && !PyErr_Occurred()) {
451 PyErr_SetString(PyExc_IndexError, "no more events in log");
452 return NULL;
453 }
454 return result;
455}
456
Tim Petersfeab23f2001-10-13 00:11:10 +0000457static char next__doc__[] =
458"next() -> event-info\n"
459"Return the next event record from the log file.";
Fred Drake8c081a12001-10-12 20:57:55 +0000460
461static PyObject *
462logreader_next(LogReaderObject *self, PyObject *args)
463{
464 PyObject *result = NULL;
465
466 if (PyArg_ParseTuple(args, ":next")) {
467 result = logreader_tp_iternext(self);
468 /* XXX return None if there's nothing left */
469 /* tp_iternext does the right thing, though */
470 if (result == NULL && !PyErr_Occurred()) {
471 result = Py_None;
472 Py_INCREF(result);
473 }
474 }
475 return result;
476}
477
478
479static int
480flush_data(ProfilerObject *self)
481{
482 /* Need to dump data to the log file... */
483 size_t written = fwrite(self->buffer, 1, self->index, self->logfp);
Tim Peters1566a172001-10-12 22:08:39 +0000484 if (written == (size_t)self->index)
Fred Drake8c081a12001-10-12 20:57:55 +0000485 self->index = 0;
486 else {
487 memmove(self->buffer, &self->buffer[written],
488 self->index - written);
489 self->index -= written;
490 if (written == 0) {
491 char *s = PyString_AsString(self->logfilename);
492 PyErr_SetFromErrnoWithFilename(PyExc_IOError, s);
493 return -1;
494 }
495 }
496 if (written > 0) {
497 if (fflush(self->logfp)) {
498 char *s = PyString_AsString(self->logfilename);
499 PyErr_SetFromErrnoWithFilename(PyExc_IOError, s);
500 return -1;
501 }
502 }
503 return 0;
504}
505
506static inline void
507pack_packed_int(ProfilerObject *self, int value)
508{
509 unsigned char partial;
510
511 do {
512 partial = value & 0x7F;
513 value >>= 7;
514 if (value)
515 partial |= 0x80;
516 self->buffer[self->index] = partial;
517 self->index++;
518 } while (value);
519}
520
521/* Encode a modified packed integer, with a subfield of modsize bits
522 * containing the value "subfield". The value of subfield is not
523 * checked to ensure it actually fits in modsize bits.
524 */
525static inline void
526pack_modified_packed_int(ProfilerObject *self, int value,
527 int modsize, int subfield)
528{
529 const int maxvalues[] = {-1, 1, 3, 7, 15, 31, 63, 127};
530
531 int bits = 7 - modsize;
532 int partial = value & maxvalues[bits];
533 unsigned char b = subfield | (partial << modsize);
534
535 if (partial != value) {
536 b |= 0x80;
537 self->buffer[self->index] = b;
538 self->index++;
539 pack_packed_int(self, value >> bits);
540 }
541 else {
542 self->buffer[self->index] = b;
543 self->index++;
544 }
545}
546
547static void
548pack_string(ProfilerObject *self, const char *s)
549{
550 int len = strlen(s);
551
552 pack_packed_int(self, len);
553 if (len + self->index >= BUFFERSIZE)
554 (void) flush_data(self);
555 memcpy(self->buffer + self->index, s, len);
556 self->index += len;
557}
558
559static void
560pack_add_info(ProfilerObject *self, const char *s1, const char *s2)
561{
562 int len1 = strlen(s1);
563 int len2 = strlen(s2);
564
565 if (len1 + len2 + PISIZE*2 + 1 + self->index >= BUFFERSIZE)
566 (void) flush_data(self);
567 self->buffer[self->index] = WHAT_ADD_INFO;
568 self->index++;
569 pack_string(self, s1);
570 pack_string(self, s2);
571}
572
573static void
574pack_define_file(ProfilerObject *self, int fileno, const char *filename)
575{
576 int len = strlen(filename);
577
578 if (len + PISIZE*2 + 1 + self->index >= BUFFERSIZE)
579 (void) flush_data(self);
580 self->buffer[self->index] = WHAT_DEFINE_FILE;
581 self->index++;
582 pack_packed_int(self, fileno);
583 pack_string(self, filename);
584}
585
586static void
587pack_line_times(ProfilerObject *self)
588{
589 if (2 + self->index >= BUFFERSIZE)
590 (void) flush_data(self);
591 self->buffer[self->index] = WHAT_LINE_TIMES;
592 self->buffer[self->index + 1] = self->linetimings ? 1 : 0;
593 self->index += 2;
594}
595
596static inline void
597pack_enter(ProfilerObject *self, int fileno, int tdelta, int lineno)
598{
599 if (MPISIZE + PISIZE*2 + self->index >= BUFFERSIZE)
600 (void) flush_data(self);
601 pack_modified_packed_int(self, fileno, 2, WHAT_ENTER);
602 pack_packed_int(self, tdelta);
603 pack_packed_int(self, lineno);
604}
605
606static inline void
607pack_exit(ProfilerObject *self, int tdelta)
608{
609 if (MPISIZE + self->index >= BUFFERSIZE)
610 (void) flush_data(self);
611 pack_modified_packed_int(self, tdelta, 2, WHAT_EXIT);
612}
613
614static inline void
615pack_lineno(ProfilerObject *self, int lineno)
616{
617 if (MPISIZE + self->index >= BUFFERSIZE)
618 (void) flush_data(self);
619 pack_modified_packed_int(self, lineno, 2, WHAT_LINENO);
620}
621
622static inline void
623pack_lineno_tdelta(ProfilerObject *self, int lineno, int tdelta)
624{
625 if (MPISIZE + PISIZE + self->index >= BUFFERSIZE)
626 (void) flush_data(self);
627 pack_modified_packed_int(self, lineno, 2, WHAT_LINENO);
628 pack_packed_int(self, tdelta);
629}
630
631static inline int
632get_fileno(ProfilerObject *self, PyCodeObject *fcode)
633{
634 PyObject *idobj;
635 int fileno;
636
637 idobj = PyDict_GetItem(self->filemap, fcode->co_filename);
638 if (idobj == NULL) {
639 /* first sighting of this file */
640 fileno = self->next_fileno;
641 idobj = PyInt_FromLong(fileno);
642 if (idobj == NULL) {
643 return -1;
644 }
645 if (PyDict_SetItem(self->filemap, fcode->co_filename, idobj)) {
646 Py_DECREF(idobj);
647 return -1;
648 }
649 self->next_fileno++;
650 Py_DECREF(idobj);
651 pack_define_file(self, fileno, PyString_AS_STRING(fcode->co_filename));
652 }
653 else {
654 /* already know this ID */
655 fileno = PyInt_AS_LONG(idobj);
656 }
657 return fileno;
658}
659
660static inline int
661get_tdelta(ProfilerObject *self)
662{
663 int tdelta;
664#ifdef MS_WIN32
665 hs_time tv;
666 LARGE_INTEGER diff;
667
668 QueryPerformanceCounter(&tv);
669 diff = LargeIntegerSubtract(tv, self->prev_timeofday);
670
671 tdelta = diff.LowPart;
672#else
673 struct timeval tv;
674
675 GETTIMEOFDAY(&tv);
676
677 if (tv.tv_sec == self->prev_timeofday.tv_sec)
678 tdelta = tv.tv_usec - self->prev_timeofday.tv_usec;
679 else
680 tdelta = ((tv.tv_sec - self->prev_timeofday.tv_sec) * 1000000
681 + tv.tv_usec);
682#endif
683 self->prev_timeofday = tv;
684 return tdelta;
685}
686
687
688/* The workhorse: the profiler callback function. */
689
690static int
691profiler_callback(ProfilerObject *self, PyFrameObject *frame, int what,
692 PyObject *arg)
693{
694 int tdelta = get_tdelta(self);
695 int fileno;
696
697 self->callcount++;
698 switch (what) {
699 case PyTrace_CALL:
700 fileno = get_fileno(self, frame->f_code);
701 if (fileno < 0)
702 return -1;
703 pack_enter(self, fileno, tdelta,
704 frame->f_code->co_firstlineno);
705 break;
706 case PyTrace_RETURN:
707 pack_exit(self, tdelta);
708 break;
709 default:
710 /* should never get here */
711 break;
712 }
713 return 0;
714}
715
716
717/* Alternate callback when we want PyTrace_LINE events */
718
719static int
720tracer_callback(ProfilerObject *self, PyFrameObject *frame, int what,
721 PyObject *arg)
722{
723 int fileno;
724
725 self->callcount++;
726 switch (what) {
727 case PyTrace_CALL:
728 fileno = get_fileno(self, frame->f_code);
729 if (fileno < 0)
730 return -1;
731 pack_enter(self, fileno, get_tdelta(self),
732 frame->f_code->co_firstlineno);
733 break;
734 case PyTrace_RETURN:
735 pack_exit(self, get_tdelta(self));
736 break;
Tim Peters1566a172001-10-12 22:08:39 +0000737 case PyTrace_LINE:
Fred Drake8c081a12001-10-12 20:57:55 +0000738 if (self->linetimings)
739 pack_lineno_tdelta(self, frame->f_lineno, get_tdelta(self));
740 else
741 pack_lineno(self, frame->f_lineno);
742 break;
743 default:
744 /* ignore PyTrace_EXCEPTION */
745 break;
746 }
747 return 0;
748}
749
750
751/* A couple of useful helper functions. */
752
753#ifdef MS_WIN32
Tim Petersfeab23f2001-10-13 00:11:10 +0000754static LARGE_INTEGER frequency = {0, 0};
Fred Drake8c081a12001-10-12 20:57:55 +0000755#endif
756
757static unsigned long timeofday_diff = 0;
758static unsigned long rusage_diff = 0;
759
760static void
761calibrate(void)
762{
763 hs_time tv1, tv2;
764
765#ifdef MS_WIN32
766 LARGE_INTEGER diff;
767 QueryPerformanceFrequency(&frequency);
768#endif
769
770 GETTIMEOFDAY(&tv1);
771 while (1) {
772 GETTIMEOFDAY(&tv2);
773#ifdef MS_WIN32
774 diff = LargeIntegerSubtract(tv2, tv1);
775 if (!LargeIntegerEqualToZero(diff)) {
776 timeofday_diff = diff.LowPart;
777 break;
778 }
779#else
780 if (tv1.tv_sec != tv2.tv_sec || tv1.tv_usec != tv2.tv_usec) {
781 if (tv1.tv_sec == tv2.tv_sec)
782 timeofday_diff = tv2.tv_usec - tv1.tv_usec;
783 else
784 timeofday_diff = (1000000 - tv1.tv_usec) + tv2.tv_usec;
785 break;
786 }
787#endif
788 }
789#ifdef MS_WIN32
790 rusage_diff = -1;
791#else
792 {
793 struct rusage ru1, ru2;
794
795 getrusage(RUSAGE_SELF, &ru1);
796 while (1) {
797 getrusage(RUSAGE_SELF, &ru2);
798 if (ru1.ru_utime.tv_sec != ru2.ru_utime.tv_sec) {
799 rusage_diff = ((1000000 - ru1.ru_utime.tv_usec)
800 + ru2.ru_utime.tv_usec);
801 break;
802 }
803 else if (ru1.ru_utime.tv_usec != ru2.ru_utime.tv_usec) {
804 rusage_diff = ru2.ru_utime.tv_usec - ru1.ru_utime.tv_usec;
805 break;
806 }
807 else if (ru1.ru_stime.tv_sec != ru2.ru_stime.tv_sec) {
808 rusage_diff = ((1000000 - ru1.ru_stime.tv_usec)
809 + ru2.ru_stime.tv_usec);
810 break;
811 }
812 else if (ru1.ru_stime.tv_usec != ru2.ru_stime.tv_usec) {
813 rusage_diff = ru2.ru_stime.tv_usec - ru1.ru_stime.tv_usec;
814 break;
815 }
816 }
817 }
818#endif
819}
820
821static void
822do_start(ProfilerObject *self)
823{
824 self->active = 1;
825 GETTIMEOFDAY(&self->prev_timeofday);
826 if (self->lineevents)
827 PyEval_SetTrace((Py_tracefunc) tracer_callback, (PyObject *)self);
828 else
829 PyEval_SetProfile((Py_tracefunc) profiler_callback, (PyObject *)self);
830}
831
832static void
833do_stop(ProfilerObject *self)
834{
835 if (self->active) {
836 self->active = 0;
837 if (self->lineevents)
838 PyEval_SetTrace(NULL, NULL);
839 else
840 PyEval_SetProfile(NULL, NULL);
841 }
842 if (self->index > 0) {
843 /* Best effort to dump out any remaining data. */
844 flush_data(self);
845 }
846}
847
848static int
849is_available(ProfilerObject *self)
850{
851 if (self->active) {
852 PyErr_SetString(ProfilerError, "profiler already active");
853 return 0;
854 }
855 if (self->logfp == NULL) {
856 PyErr_SetString(ProfilerError, "profiler already closed");
857 return 0;
858 }
859 return 1;
860}
861
862
863/* Profiler object interface methods. */
864
Tim Petersfeab23f2001-10-13 00:11:10 +0000865static char close__doc__[] =
866"close()\n"
867"Shut down this profiler and close the log files, even if its active.";
Fred Drake8c081a12001-10-12 20:57:55 +0000868
869static PyObject *
870profiler_close(ProfilerObject *self, PyObject *args)
871{
872 PyObject *result = NULL;
873
874 if (PyArg_ParseTuple(args, ":close")) {
875 do_stop(self);
876 if (self->logfp != NULL) {
877 fclose(self->logfp);
878 self->logfp = NULL;
879 }
880 Py_INCREF(Py_None);
881 result = Py_None;
882 }
883 return result;
884}
885
Tim Petersfeab23f2001-10-13 00:11:10 +0000886static char runcall__doc__[] =
887"runcall(callable[, args[, kw]]) -> callable()\n"
888"Profile a specific function call, returning the result of that call.";
Fred Drake8c081a12001-10-12 20:57:55 +0000889
890static PyObject *
891profiler_runcall(ProfilerObject *self, PyObject *args)
892{
893 PyObject *result = NULL;
894 PyObject *callargs = NULL;
895 PyObject *callkw = NULL;
896 PyObject *callable;
897
898 if (PyArg_ParseTuple(args, "O|OO:runcall",
899 &callable, &callargs, &callkw)) {
900 if (is_available(self)) {
901 do_start(self);
902 result = PyEval_CallObjectWithKeywords(callable, callargs, callkw);
903 do_stop(self);
904 }
905 }
906 return result;
907}
908
Tim Petersfeab23f2001-10-13 00:11:10 +0000909static char runcode__doc__[] =
910"runcode(code, globals[, locals])\n"
911"Execute a code object while collecting profile data. If locals is\n"
912"omitted, globals is used for the locals as well.";
Fred Drake8c081a12001-10-12 20:57:55 +0000913
914static PyObject *
915profiler_runcode(ProfilerObject *self, PyObject *args)
916{
917 PyObject *result = NULL;
918 PyCodeObject *code;
919 PyObject *globals;
920 PyObject *locals = NULL;
921
922 if (PyArg_ParseTuple(args, "O!O!|O:runcode",
923 &PyCode_Type, &code,
924 &PyDict_Type, &globals,
925 &locals)) {
926 if (is_available(self)) {
927 if (locals == NULL || locals == Py_None)
928 locals = globals;
929 else if (!PyDict_Check(locals)) {
930 PyErr_SetString(PyExc_TypeError,
931 "locals must be a dictionary or None");
932 return NULL;
933 }
934 do_start(self);
935 result = PyEval_EvalCode(code, globals, locals);
936 do_stop(self);
937#if 0
938 if (!PyErr_Occurred()) {
939 result = Py_None;
940 Py_INCREF(result);
941 }
942#endif
943 }
944 }
945 return result;
946}
947
Tim Petersfeab23f2001-10-13 00:11:10 +0000948static char start__doc__[] =
949"start()\n"
950"Install this profiler for the current thread.";
Fred Drake8c081a12001-10-12 20:57:55 +0000951
952static PyObject *
953profiler_start(ProfilerObject *self, PyObject *args)
954{
955 PyObject *result = NULL;
956
957 if (PyArg_ParseTuple(args, ":start")) {
958 if (is_available(self))
959 do_start(self);
960 }
961 return result;
962}
963
Tim Petersfeab23f2001-10-13 00:11:10 +0000964static char stop__doc__[] =
965"stop()\n"
966"Remove this profiler from the current thread.";
Fred Drake8c081a12001-10-12 20:57:55 +0000967
968static PyObject *
969profiler_stop(ProfilerObject *self, PyObject *args)
970{
971 PyObject *result = NULL;
972
973 if (PyArg_ParseTuple(args, ":stop")) {
974 if (!self->active)
975 PyErr_SetString(ProfilerError, "profiler not active");
976 else
977 do_stop(self);
978 }
979 return result;
980}
981
982
983/* Python API support. */
984
985static void
986profiler_dealloc(ProfilerObject *self)
987{
988 do_stop(self);
989 if (self->logfp != NULL)
990 fclose(self->logfp);
991 Py_XDECREF(self->filemap);
992 Py_XDECREF(self->logfilename);
993 PyObject_Del((PyObject *)self);
994}
995
996/* Always use METH_VARARGS even though some of these could be METH_NOARGS;
997 * this allows us to maintain compatibility with Python versions < 2.2
998 * more easily, requiring only the changes to the dispatcher to be made.
999 */
1000static PyMethodDef profiler_methods[] = {
1001 {"close", (PyCFunction)profiler_close, METH_VARARGS, close__doc__},
1002 {"runcall", (PyCFunction)profiler_runcall, METH_VARARGS, runcall__doc__},
1003 {"runcode", (PyCFunction)profiler_runcode, METH_VARARGS, runcode__doc__},
1004 {"start", (PyCFunction)profiler_start, METH_VARARGS, start__doc__},
1005 {"stop", (PyCFunction)profiler_stop, METH_VARARGS, stop__doc__},
1006 {NULL, NULL}
1007};
1008
1009/* Use a table even though there's only one "simple" member; this allows
1010 * __members__ and therefore dir() to work.
1011 */
1012static struct memberlist profiler_members[] = {
1013 {"callcount", T_LONG, offsetof(ProfilerObject, callcount), READONLY},
1014 {"closed", T_INT, -1, READONLY},
1015 {"lineevents", T_LONG, offsetof(ProfilerObject, lineevents), READONLY},
1016 {"linetimings", T_LONG, offsetof(ProfilerObject, linetimings), READONLY},
1017 {NULL}
1018};
1019
1020static PyObject *
1021profiler_getattr(ProfilerObject *self, char *name)
1022{
1023 PyObject *result;
1024 if (strcmp(name, "closed") == 0) {
1025 result = (self->logfp == NULL) ? Py_True : Py_False;
1026 Py_INCREF(result);
1027 }
1028 else {
1029 result = PyMember_Get((char *)self, profiler_members, name);
1030 if (result == NULL) {
1031 PyErr_Clear();
1032 result = Py_FindMethod(profiler_methods, (PyObject *)self, name);
1033 }
1034 }
1035 return result;
1036}
1037
1038
Tim Petersfeab23f2001-10-13 00:11:10 +00001039static char profiler_object__doc__[] =
1040"High-performance profiler object.\n"
1041"\n"
1042"Methods:\n"
1043"\n"
1044"close(): Stop the profiler and close the log files.\n"
1045"runcall(): Run a single function call with profiling enabled.\n"
1046"runcode(): Execute a code object with profiling enabled.\n"
1047"start(): Install the profiler and return.\n"
1048"stop(): Remove the profiler.\n"
1049"\n"
1050"Attributes (read-only):\n"
1051"\n"
1052"callcount: The number of low-level calls to the profiler.\n"
1053"closed: True if the profiler has already been closed.\n"
1054"lineevents: True if SET_LINENO events are reported to the profiler.\n"
1055"linetimings: True if SET_LINENO events collect timing information.";
Fred Drake8c081a12001-10-12 20:57:55 +00001056
1057static PyTypeObject ProfilerType = {
1058 PyObject_HEAD_INIT(NULL)
1059 0, /* ob_size */
1060 "HotShot-profiler", /* tp_name */
1061 (int) sizeof(ProfilerObject), /* tp_basicsize */
1062 0, /* tp_itemsize */
1063 (destructor)profiler_dealloc, /* tp_dealloc */
1064 0, /* tp_print */
1065 (getattrfunc)profiler_getattr, /* tp_getattr */
1066 0, /* tp_setattr */
1067 0, /* tp_compare */
1068 0, /* tp_repr */
1069 0, /* tp_as_number */
1070 0, /* tp_as_sequence */
1071 0, /* tp_as_mapping */
1072 0, /* tp_hash */
1073 0, /* tp_call */
1074 0, /* tp_str */
1075 0, /* tp_getattro */
1076 0, /* tp_setattro */
1077 0, /* tp_as_buffer */
1078 0, /* tp_flags */
1079 profiler_object__doc__, /* tp_doc */
1080};
1081
1082
1083static PyMethodDef logreader_methods[] = {
1084 {"close", (PyCFunction)logreader_close, METH_VARARGS,
1085 logreader_close__doc__},
1086 {"next", (PyCFunction)logreader_next, METH_VARARGS,
1087 next__doc__},
1088 {NULL, NULL}
1089};
1090
1091static PyObject *
1092logreader_getattr(ProfilerObject *self, char *name)
1093{
1094 return Py_FindMethod(logreader_methods, (PyObject *)self, name);
1095}
1096
1097
1098static char logreader__doc__[] = "\
1099logreader(filename) --> log-iterator\n\
1100Create a log-reader for the timing information file.";
1101
1102static PySequenceMethods logreader_as_sequence = {
1103 0, /* sq_length */
1104 0, /* sq_concat */
1105 0, /* sq_repeat */
1106 (intargfunc)logreader_sq_item, /* sq_item */
1107 0, /* sq_slice */
1108 0, /* sq_ass_item */
1109 0, /* sq_ass_slice */
1110 0, /* sq_contains */
1111 0, /* sq_inplace_concat */
1112 0, /* sq_inplace_repeat */
1113};
1114
1115static PyTypeObject LogReaderType = {
1116 PyObject_HEAD_INIT(NULL)
1117 0, /* ob_size */
1118 "HotShot-logreader", /* tp_name */
1119 (int) sizeof(LogReaderObject), /* tp_basicsize */
1120 0, /* tp_itemsize */
1121 (destructor)logreader_dealloc, /* tp_dealloc */
1122 0, /* tp_print */
1123 (getattrfunc)logreader_getattr, /* tp_getattr */
1124 0, /* tp_setattr */
1125 0, /* tp_compare */
1126 0, /* tp_repr */
1127 0, /* tp_as_number */
1128 &logreader_as_sequence, /* tp_as_sequence */
1129 0, /* tp_as_mapping */
1130 0, /* tp_hash */
1131 0, /* tp_call */
1132 0, /* tp_str */
1133 0, /* tp_getattro */
1134 0, /* tp_setattro */
1135 0, /* tp_as_buffer */
1136 0, /* tp_flags */
1137 logreader__doc__, /* tp_doc */
1138#if Py_TPFLAGS_HAVE_ITER
1139 0, /* tp_traverse */
1140 0, /* tp_clear */
1141 0, /* tp_richcompare */
1142 0, /* tp_weaklistoffset */
1143 (getiterfunc)logreader_tp_iter, /* tp_iter */
1144 (iternextfunc)logreader_tp_iternext,/* tp_iternext */
1145#endif
1146};
1147
1148static PyObject *
1149hotshot_logreader(PyObject *unused, PyObject *args)
1150{
1151 LogReaderObject *self = NULL;
1152 char *filename;
1153
1154 if (PyArg_ParseTuple(args, "s:logreader", &filename)) {
1155 self = PyObject_New(LogReaderObject, &LogReaderType);
1156 if (self != NULL) {
1157 self->filled = 0;
1158 self->index = 0;
1159 self->linetimings = 0;
1160 self->logfp = fopen(filename, "rb");
1161 if (self->logfp == NULL) {
1162 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
1163 Py_DECREF(self);
1164 self = NULL;
1165 }
1166 }
1167 }
1168 return (PyObject *) self;
1169}
1170
1171
1172/* Return a Python string that represents the version number without the
1173 * extra cruft added by revision control, even if the right options were
1174 * given to the "cvs export" command to make it not include the extra
1175 * cruft.
1176 */
1177static char *
1178get_version_string(void)
1179{
1180 static char *rcsid = "$Revision$";
1181 char *rev = rcsid;
1182 char *buffer;
1183 int i = 0;
1184
1185 while (*rev && !isdigit(*rev))
1186 ++rev;
1187 while (rev[i] != ' ' && rev[i] != '\0')
1188 ++i;
1189 buffer = malloc(i + 1);
1190 if (buffer != NULL) {
1191 memmove(buffer, rev, i);
1192 buffer[i] = '\0';
1193 }
1194 return buffer;
1195}
1196
1197/* Write out a RFC 822-style header with various useful bits of
1198 * information to make the output easier to manage.
1199 */
1200static int
1201write_header(ProfilerObject *self)
1202{
1203 char *buffer;
1204 char cwdbuffer[PATH_MAX];
1205 PyObject *temp;
1206 int i, len;
1207
1208 buffer = get_version_string();
1209 if (buffer == NULL) {
1210 PyErr_NoMemory();
1211 return -1;
1212 }
1213 pack_add_info(self, "HotShot-Version", buffer);
1214 pack_add_info(self, "Requested-Line-Events",
1215 (self->lineevents ? "yes" : "no"));
1216 pack_add_info(self, "Platform", Py_GetPlatform());
1217 pack_add_info(self, "Executable", Py_GetProgramFullPath());
1218 buffer = (char *) Py_GetVersion();
1219 if (buffer == NULL)
1220 PyErr_Clear();
1221 else
1222 pack_add_info(self, "Executable-Version", buffer);
1223
1224#ifdef MS_WIN32
1225 sprintf(cwdbuffer, "%I64d", frequency.QuadPart);
1226 pack_add_info(self, "Reported-Performance-Frequency", cwdbuffer);
1227#else
1228 sprintf(cwdbuffer, "%lu", rusage_diff);
1229 pack_add_info(self, "Observed-Interval-getrusage", cwdbuffer);
1230 sprintf(cwdbuffer, "%lu", timeofday_diff);
1231 pack_add_info(self, "Observed-Interval-gettimeofday", cwdbuffer);
1232#endif
1233 pack_line_times(self);
1234
1235 pack_add_info(self, "Current-Directory",
1236 getcwd(cwdbuffer, sizeof cwdbuffer));
1237
1238 temp = PySys_GetObject("path");
1239 len = PyList_GET_SIZE(temp);
1240 for (i = 0; i < len; ++i) {
1241 PyObject *item = PyList_GET_ITEM(temp, i);
1242 buffer = PyString_AsString(item);
1243 if (buffer == NULL)
1244 return -1;
1245 pack_add_info(self, "Sys-Path-Entry", buffer);
1246 }
1247 return 0;
1248}
1249
1250static char profiler__doc__[] = "\
1251profiler(logfilename[, lineevents[, linetimes]]) -> profiler\n\
1252Create a new profiler object.";
1253
1254static PyObject *
1255hotshot_profiler(PyObject *unused, PyObject *args)
1256{
1257 char *logfilename;
1258 ProfilerObject *self = NULL;
1259 int lineevents = 0;
1260 int linetimings = 1;
1261
1262 if (PyArg_ParseTuple(args, "s|ii:profiler", &logfilename,
1263 &lineevents, &linetimings)) {
1264 self = PyObject_New(ProfilerObject, &ProfilerType);
1265 if (self == NULL)
1266 return NULL;
1267 self->callcount = 0;
1268 self->lineevents = lineevents ? 1 : 0;
1269 self->linetimings = (lineevents && linetimings) ? 1 : 0;
1270 self->index = 0;
1271 self->active = 0;
1272 self->next_fileno = 0;
Tim Peters1566a172001-10-12 22:08:39 +00001273 self->logfp = NULL;
Fred Drake8c081a12001-10-12 20:57:55 +00001274 self->logfilename = PyTuple_GET_ITEM(args, 0);
1275 Py_INCREF(self->logfilename);
1276 self->filemap = PyDict_New();
1277 if (self->filemap == NULL) {
1278 Py_DECREF(self);
1279 return NULL;
1280 }
1281 self->logfp = fopen(logfilename, "wb");
1282 if (self->logfp == NULL) {
1283 Py_DECREF(self);
1284 PyErr_SetFromErrnoWithFilename(PyExc_IOError, logfilename);
1285 return NULL;
1286 }
1287 if (timeofday_diff == 0) {
1288 /* Run this several times since sometimes the first
1289 * doesn't give the lowest values, and we're really trying
1290 * to determine the lowest.
1291 */
1292 calibrate();
1293 calibrate();
1294 calibrate();
1295 }
1296 if (write_header(self))
1297 /* some error occurred, exception has been set */
1298 self = NULL;
1299 }
1300 return (PyObject *) self;
1301}
1302
1303static char resolution__doc__[] =
1304#ifdef MS_WIN32
Tim Petersfeab23f2001-10-13 00:11:10 +00001305"resolution() -> (performance-counter-ticks, update-frequency)\n"
1306"Return the resolution of the timer provided by the QueryPerformanceCounter()\n"
1307"function. The first value is the smallest observed change, and the second\n"
1308"is the result of QueryPerformanceFrequency().";
Fred Drake8c081a12001-10-12 20:57:55 +00001309#else
Tim Petersfeab23f2001-10-13 00:11:10 +00001310"resolution() -> (gettimeofday-usecs, getrusage-usecs)\n"
1311"Return the resolution of the timers provided by the gettimeofday() and\n"
1312"getrusage() system calls, or -1 if the call is not supported.";
Fred Drake8c081a12001-10-12 20:57:55 +00001313#endif
1314
1315static PyObject *
1316hotshot_resolution(PyObject *unused, PyObject *args)
1317{
1318 PyObject *result = NULL;
1319
1320 if (PyArg_ParseTuple(args, ":resolution")) {
1321 if (timeofday_diff == 0) {
1322 calibrate();
1323 calibrate();
1324 calibrate();
1325 }
1326#ifdef MS_WIN32
1327 result = Py_BuildValue("ii", timeofday_diff, frequency.LowPart);
1328#else
1329 result = Py_BuildValue("ii", timeofday_diff, rusage_diff);
1330#endif
1331 }
1332 return result;
1333}
1334
1335
1336static PyMethodDef functions[] = {
1337 {"profiler", hotshot_profiler, METH_VARARGS, profiler__doc__},
1338 {"logreader", hotshot_logreader, METH_VARARGS, logreader__doc__},
1339 {"resolution", hotshot_resolution, METH_VARARGS, resolution__doc__},
1340 {NULL, NULL}
1341};
1342
1343
1344void
1345init_hotshot(void)
1346{
1347 PyObject *module;
1348
1349 LogReaderType.ob_type = &PyType_Type;
1350 ProfilerType.ob_type = &PyType_Type;
1351 module = Py_InitModule("_hotshot", functions);
1352 if (module != NULL) {
1353 char *s = get_version_string();
1354
1355 PyModule_AddStringConstant(module, "__version__", s);
1356 free(s);
1357 Py_INCREF(&LogReaderType);
1358 PyModule_AddObject(module, "LogReaderType",
1359 (PyObject *)&LogReaderType);
1360 Py_INCREF(&ProfilerType);
1361 PyModule_AddObject(module, "ProfilerType",
1362 (PyObject *)&ProfilerType);
1363
1364 if (ProfilerError == NULL)
1365 ProfilerError = PyErr_NewException("hotshot.ProfilerError",
1366 NULL, NULL);
1367 if (ProfilerError != NULL) {
1368 Py_INCREF(ProfilerError);
1369 PyModule_AddObject(module, "ProfilerError", ProfilerError);
1370 }
1371 PyModule_AddIntConstant(module, "WHAT_ENTER", WHAT_ENTER);
1372 PyModule_AddIntConstant(module, "WHAT_EXIT", WHAT_EXIT);
1373 PyModule_AddIntConstant(module, "WHAT_LINENO", WHAT_LINENO);
1374 PyModule_AddIntConstant(module, "WHAT_OTHER", WHAT_OTHER);
1375 PyModule_AddIntConstant(module, "WHAT_ADD_INFO", WHAT_ADD_INFO);
1376 PyModule_AddIntConstant(module, "WHAT_DEFINE_FILE", WHAT_DEFINE_FILE);
1377 PyModule_AddIntConstant(module, "WHAT_LINE_TIMES", WHAT_LINE_TIMES);
1378 }
1379}