blob: b17be9fa57c3a8743bf4b3327af08e2492e7a569 [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() */
Tim Peters7d99ff22001-10-13 07:37:52 +000023typedef __int64 hs_time;
24#define GETTIMEOFDAY(P_HS_TIME) \
25 { LARGE_INTEGER _temp; \
26 QueryPerformanceCounter(&_temp); \
27 *(P_HS_TIME) = _temp.QuadPart; }
28
Tim Petersfeab23f2001-10-13 00:11:10 +000029
Fred Drake8c081a12001-10-12 20:57:55 +000030#else
31#ifndef HAVE_GETTIMEOFDAY
32#error "This module requires gettimeofday() on non-Windows platforms!"
33#endif
Jack Jansen963659a2001-10-23 22:26:16 +000034#ifdef macintosh
35#include <sys/time.h>
36#else
Fred Drake8c081a12001-10-12 20:57:55 +000037#include <sys/resource.h>
38#include <sys/times.h>
Jack Jansen963659a2001-10-23 22:26:16 +000039#endif
Fred Drake8c081a12001-10-12 20:57:55 +000040typedef struct timeval hs_time;
41#endif
42
43#if !defined(__cplusplus) && !defined(inline)
44#ifdef __GNUC__
45#define inline __inline
46#endif
47#endif
48
49#ifndef inline
50#define inline
51#endif
52
53#define BUFFERSIZE 10240
54
Jack Jansen963659a2001-10-23 22:26:16 +000055#ifdef macintosh
56#define PATH_MAX 254
57#endif
58
Tim Peters1566a172001-10-12 22:08:39 +000059#ifndef PATH_MAX
60# ifdef MAX_PATH
61# define PATH_MAX MAX_PATH
62# else
63# error "Need a defn. for PATH_MAX in _hotshot.c"
64# endif
65#endif
66
Fred Drake8c081a12001-10-12 20:57:55 +000067typedef struct {
68 PyObject_HEAD
69 PyObject *filemap;
70 PyObject *logfilename;
71 int index;
72 unsigned char buffer[BUFFERSIZE];
73 FILE *logfp;
74 int lineevents;
75 int linetimings;
Fred Drake30d1c752001-10-15 22:11:02 +000076 int frametimings;
Fred Drake8c081a12001-10-12 20:57:55 +000077 /* size_t filled; */
78 int active;
79 int next_fileno;
Fred Drake8c081a12001-10-12 20:57:55 +000080 hs_time prev_timeofday;
81} ProfilerObject;
82
83typedef struct {
84 PyObject_HEAD
Fred Drake4c2e1af2001-10-29 20:45:57 +000085 PyObject *info;
Fred Drake8c081a12001-10-12 20:57:55 +000086 FILE *logfp;
87 int filled;
88 int index;
89 int linetimings;
Fred Drake30d1c752001-10-15 22:11:02 +000090 int frametimings;
Fred Drake8c081a12001-10-12 20:57:55 +000091 unsigned char buffer[BUFFERSIZE];
92} LogReaderObject;
93
94static PyObject * ProfilerError = NULL;
95
96
97#ifndef MS_WIN32
98#ifdef GETTIMEOFDAY_NO_TZ
99#define GETTIMEOFDAY(ptv) gettimeofday((ptv))
100#else
101#define GETTIMEOFDAY(ptv) gettimeofday((ptv), (struct timezone *)NULL)
102#endif
103#endif
104
105
106/* The log reader... */
107
Tim Petersfeab23f2001-10-13 00:11:10 +0000108static char logreader_close__doc__[] =
109"close()\n"
110"Close the log file, preventing additional records from being read.";
Fred Drake8c081a12001-10-12 20:57:55 +0000111
112static PyObject *
113logreader_close(LogReaderObject *self, PyObject *args)
114{
115 PyObject *result = NULL;
116 if (PyArg_ParseTuple(args, ":close")) {
117 if (self->logfp != NULL) {
118 fclose(self->logfp);
119 self->logfp = NULL;
120 }
121 result = Py_None;
122 Py_INCREF(result);
123 }
124 return result;
125}
126
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.
131 */
132static PyObject *
133logreader_tp_iter(LogReaderObject *self)
134{
135 Py_INCREF(self);
136 return (PyObject *) self;
137}
138#endif
139
140
141/* Log File Format
142 * ---------------
143 *
144 * The log file consists of a sequence of variable-length records.
145 * Each record is identified with a record type identifier in two
146 * bits of the first byte. The two bits are the "least significant"
147 * bits of the byte.
148 *
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
154 *
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.
160 *
161 * The OTHER records have the following identifiers:
162 *
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
Fred Drake30d1c752001-10-15 22:11:02 +0000167 * 0x43 DEFINE_FUNC define a (fileno,lineno)->funcname mapping
168 * 0x53 FRAME_TIMES indicates if ENTER/EXIT events have tdeltas
Fred Drake8c081a12001-10-12 20:57:55 +0000169 *
170 * Packed Integers
171 *
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.
178 *
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.
185 *
186 * ENTER records:
187 *
188 * MPI(2,type) fileno -- type is 0x00
Fred Drake8c081a12001-10-12 20:57:55 +0000189 * PI lineno
Fred Drake30d1c752001-10-15 22:11:02 +0000190 * PI tdelta -- iff frame times are enabled
Fred Drake8c081a12001-10-12 20:57:55 +0000191 *
192 * EXIT records
193 *
Fred Drake30d1c752001-10-15 22:11:02 +0000194 * MPI(2,type) tdelta -- type is 0x01; tdelta will be 0
195 * if frame times are disabled
Fred Drake8c081a12001-10-12 20:57:55 +0000196 *
197 * LINENO records
198 *
199 * MPI(2,type) lineno -- type is 0x02
200 * PI tdelta -- iff LINENO includes it
201 *
202 * ADD_INFO records
203 *
Fred Drake30d1c752001-10-15 22:11:02 +0000204 * BYTE type -- always 0x13
Fred Drake8c081a12001-10-12 20:57:55 +0000205 * 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
209 *
210 * DEFINE_FILE records
211 *
Fred Drake30d1c752001-10-15 22:11:02 +0000212 * BYTE type -- always 0x23
Fred Drake8c081a12001-10-12 20:57:55 +0000213 * PI fileno
214 * PI len -- length of filename
215 * BYTE filename[len] -- len bytes of string data
216 *
Fred Drake30d1c752001-10-15 22:11:02 +0000217 * DEFINE_FUNC records
218 *
219 * BYTE type -- always 0x43
220 * PI fileno
221 * PI lineno
222 * PI len -- length of funcname
223 * BYTE funcname[len] -- len bytes of string data
224 *
Fred Drake8c081a12001-10-12 20:57:55 +0000225 * LINE_TIMES records
Fred Drake30d1c752001-10-15 22:11:02 +0000226 *
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.
231 *
232 * BYTE type -- always 0x33
Fred Drake8c081a12001-10-12 20:57:55 +0000233 * BYTE have_tdelta -- 0 if LINENO does *not* have
234 * timing information
Fred Drake30d1c752001-10-15 22:11:02 +0000235 * FRAME_TIMES records
236 *
237 * This record can be used only before the start of ENTER/EXIT/LINENO
238 * records. If have_tdelta is true, ENTER and EXIT records will
239 * include the tdelta field, otherwise it will be omitted. If this
240 * record is not given, ENTER and EXIT records will contain the tdelta
241 * field.
242 *
243 * BYTE type -- always 0x53
244 * BYTE have_tdelta -- 0 if ENTER/EXIT do *not* have
245 * timing information
Fred Drake8c081a12001-10-12 20:57:55 +0000246 */
247
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
Fred Drake30d1c752001-10-15 22:11:02 +0000255#define WHAT_DEFINE_FUNC 0x43
256#define WHAT_FRAME_TIMES 0x53
Fred Drake8c081a12001-10-12 20:57:55 +0000257
258#define ERR_NONE 0
259#define ERR_EOF -1
260#define ERR_EXCEPTION -2
Fred Drake4c2e1af2001-10-29 20:45:57 +0000261#define ERR_BAD_RECTYPE -3
Fred Drake8c081a12001-10-12 20:57:55 +0000262
263#define PISIZE (sizeof(int) + 1)
264#define MPISIZE (PISIZE + 1)
265
266/* Maximum size of "normal" events -- nothing that contains string data */
267#define MAXEVENTSIZE (MPISIZE + PISIZE*2)
268
269
270/* Unpack a packed integer; if "discard" is non-zero, unpack a modified
271 * packed integer with "discard" discarded bits.
272 */
273static int
274unpack_packed_int(LogReaderObject *self, int *pvalue, int discard)
275{
276 int accum = 0;
277 int bits = 0;
278 int index = self->index;
279 int cont;
280
281 do {
282 if (index >= self->filled)
283 return ERR_EOF;
284 /* read byte */
285 accum |= ((self->buffer[index] & 0x7F) >> discard) << bits;
286 bits += (7 - discard);
287 cont = self->buffer[index] & 0x80;
288 /* move to next */
289 discard = 0;
290 index++;
291 } while (cont);
292
293 /* save state */
294 self->index = index;
295 *pvalue = accum;
296
297 return 0;
298}
299
300/* Unpack a string, which is encoded as a packed integer giving the
301 * length of the string, followed by the string data.
302 */
303static int
304unpack_string(LogReaderObject *self, PyObject **pvalue)
305{
306 int len;
307 int oldindex = self->index;
308 int err = unpack_packed_int(self, &len, 0);
309
310 if (!err) {
311 /* need at least len bytes in buffer */
312 if (len > (self->filled - self->index)) {
313 self->index = oldindex;
314 err = ERR_EOF;
315 }
316 else {
Jack Jansen963659a2001-10-23 22:26:16 +0000317 *pvalue = PyString_FromStringAndSize((char *)self->buffer + self->index,
Fred Drake8c081a12001-10-12 20:57:55 +0000318 len);
319 if (*pvalue == NULL) {
320 self->index = oldindex;
321 err = ERR_EXCEPTION;
322 }
323 else
324 self->index += len;
325 }
326 }
327 return err;
328}
329
330
Fred Drake4c2e1af2001-10-29 20:45:57 +0000331static int
332unpack_add_info(LogReaderObject *self, int skip_opcode)
333{
334 PyObject *key;
335 PyObject *value = NULL;
336 int err;
337
338 if (skip_opcode) {
339 if (self->buffer[self->index] != WHAT_ADD_INFO)
340 return ERR_BAD_RECTYPE;
341 self->index++;
342 }
343 err = unpack_string(self, &key);
344 if (!err) {
345 err = unpack_string(self, &value);
346 if (err)
347 Py_DECREF(key);
348 else {
349 PyObject *list = PyDict_GetItem(self->info, key);
350 if (list == NULL) {
351 list = PyList_New(0);
352 if (list == NULL) {
353 err = ERR_EXCEPTION;
354 goto finally;
355 }
356 if (PyDict_SetItem(self->info, key, list)) {
357 err = ERR_EXCEPTION;
358 goto finally;
359 }
360 }
361 if (PyList_Append(list, value))
362 err = ERR_EXCEPTION;
363 }
364 }
365 finally:
366 Py_XDECREF(key);
367 Py_XDECREF(value);
368 return err;
369}
370
371
372static void
373logreader_refill(LogReaderObject *self)
374{
375 int needed;
376 size_t res;
377
378 if (self->index) {
379 memmove(self->buffer, &self->buffer[self->index],
380 self->filled - self->index);
381 self->filled = self->filled - self->index;
382 self->index = 0;
383 }
384 needed = BUFFERSIZE - self->filled;
385 if (needed > 0) {
386 res = fread(&self->buffer[self->filled], 1, needed, self->logfp);
387 self->filled += res;
388 }
389}
390
391static void
392eof_error(void)
393{
394 PyErr_SetString(PyExc_EOFError,
395 "end of file with incomplete profile record");
396}
397
Fred Drake8c081a12001-10-12 20:57:55 +0000398static PyObject *
399logreader_tp_iternext(LogReaderObject *self)
400{
401 int what, oldindex;
402 int err = ERR_NONE;
403 int lineno = -1;
404 int fileno = -1;
405 int tdelta = -1;
406 PyObject *s1 = NULL, *s2 = NULL;
407 PyObject *result = NULL;
408#if 0
409 unsigned char b0, b1;
410#endif
411
412 if (self->logfp == NULL) {
413 PyErr_SetString(ProfilerError,
414 "cannot iterate over closed LogReader object");
415 return NULL;
416 }
417 restart:
Fred Drake4c2e1af2001-10-29 20:45:57 +0000418 if ((self->filled - self->index) < MAXEVENTSIZE)
419 logreader_refill(self);
420
Fred Drake8c081a12001-10-12 20:57:55 +0000421 /* end of input */
422 if (self->filled == 0)
423 return NULL;
424
425 oldindex = self->index;
426
427 /* decode the record type */
428 what = self->buffer[self->index] & WHAT_OTHER;
429 if (what == WHAT_OTHER) {
430 what = self->buffer[self->index];
431 self->index++;
432 }
433 switch (what) {
434 case WHAT_ENTER:
435 err = unpack_packed_int(self, &fileno, 2);
436 if (!err) {
Fred Drake30d1c752001-10-15 22:11:02 +0000437 err = unpack_packed_int(self, &lineno, 0);
438 if (self->frametimings && !err)
439 err = unpack_packed_int(self, &tdelta, 0);
Fred Drake8c081a12001-10-12 20:57:55 +0000440 }
441 break;
442 case WHAT_EXIT:
443 err = unpack_packed_int(self, &tdelta, 2);
444 break;
445 case WHAT_LINENO:
446 err = unpack_packed_int(self, &lineno, 2);
447 if (self->linetimings && !err)
448 err = unpack_packed_int(self, &tdelta, 0);
449 break;
450 case WHAT_ADD_INFO:
Fred Drake4c2e1af2001-10-29 20:45:57 +0000451 err = unpack_add_info(self, 0);
Fred Drake8c081a12001-10-12 20:57:55 +0000452 break;
453 case WHAT_DEFINE_FILE:
454 err = unpack_packed_int(self, &fileno, 0);
455 if (!err) {
456 err = unpack_string(self, &s1);
457 if (!err) {
458 Py_INCREF(Py_None);
459 s2 = Py_None;
460 }
461 }
462 break;
Fred Drake30d1c752001-10-15 22:11:02 +0000463 case WHAT_DEFINE_FUNC:
464 err = unpack_packed_int(self, &fileno, 0);
465 if (!err) {
466 err = unpack_packed_int(self, &lineno, 0);
467 if (!err)
468 err = unpack_string(self, &s1);
469 }
470 break;
Fred Drake8c081a12001-10-12 20:57:55 +0000471 case WHAT_LINE_TIMES:
472 if (self->index >= self->filled)
473 err = ERR_EOF;
474 else {
475 self->linetimings = self->buffer[self->index] ? 1 : 0;
476 self->index++;
477 goto restart;
478 }
Fred Drake4c2e1af2001-10-29 20:45:57 +0000479 break;
Fred Drake30d1c752001-10-15 22:11:02 +0000480 case WHAT_FRAME_TIMES:
481 if (self->index >= self->filled)
482 err = ERR_EOF;
483 else {
484 self->frametimings = self->buffer[self->index] ? 1 : 0;
485 self->index++;
486 goto restart;
487 }
Fred Drake4c2e1af2001-10-29 20:45:57 +0000488 break;
Fred Drake8c081a12001-10-12 20:57:55 +0000489 default:
Fred Drake4c2e1af2001-10-29 20:45:57 +0000490 err = ERR_BAD_RECTYPE;
Fred Drake8c081a12001-10-12 20:57:55 +0000491 }
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.
496 */
497 err = ERR_NONE;
Fred Drake4c2e1af2001-10-29 20:45:57 +0000498 logreader_refill(self);
Fred Drake8c081a12001-10-12 20:57:55 +0000499 }
Fred Drake4c2e1af2001-10-29 20:45:57 +0000500 if (err == ERR_BAD_RECTYPE) {
501 PyErr_SetString(PyExc_ValueError,
502 "unknown record type in log file");
503 }
504 else if (err == ERR_EOF) {
Fred Drake8c081a12001-10-12 20:57:55 +0000505 /* Could not avoid end-of-buffer error. */
Fred Drake4c2e1af2001-10-29 20:45:57 +0000506 eof_error();
Fred Drake8c081a12001-10-12 20:57:55 +0000507 }
508 else if (!err) {
509 result = PyTuple_New(4);
510 PyTuple_SET_ITEM(result, 0, PyInt_FromLong(what));
511 PyTuple_SET_ITEM(result, 2, PyInt_FromLong(fileno));
Fred Drake30d1c752001-10-15 22:11:02 +0000512 if (s1 == NULL)
Fred Drake8c081a12001-10-12 20:57:55 +0000513 PyTuple_SET_ITEM(result, 1, PyInt_FromLong(tdelta));
Fred Drake30d1c752001-10-15 22:11:02 +0000514 else
Fred Drake8c081a12001-10-12 20:57:55 +0000515 PyTuple_SET_ITEM(result, 1, s1);
Fred Drake30d1c752001-10-15 22:11:02 +0000516 if (s2 == NULL)
517 PyTuple_SET_ITEM(result, 3, PyInt_FromLong(lineno));
518 else
Fred Drake8c081a12001-10-12 20:57:55 +0000519 PyTuple_SET_ITEM(result, 3, s2);
Fred Drake8c081a12001-10-12 20:57:55 +0000520 }
521 /* The only other case is err == ERR_EXCEPTION, in which case the
522 * exception is already set.
523 */
524#if 0
525 b0 = self->buffer[self->index];
526 b1 = self->buffer[self->index + 1];
527 if (b0 & 1) {
528 /* This is a line-number event. */
529 what = PyTrace_LINE;
530 lineno = ((b0 & ~1) << 7) + b1;
531 self->index += 2;
532 }
533 else {
534 what = (b0 & 0x0E) >> 1;
535 tdelta = ((b0 & 0xF0) << 4) + b1;
536 if (what == PyTrace_CALL) {
537 /* we know there's a 2-byte file ID & 2-byte line number */
538 fileno = ((self->buffer[self->index + 2] << 8)
539 + self->buffer[self->index + 3]);
540 lineno = ((self->buffer[self->index + 4] << 8)
541 + self->buffer[self->index + 5]);
542 self->index += 6;
543 }
544 else
545 self->index += 2;
546 }
547#endif
548 return result;
549}
550
551static void
552logreader_dealloc(LogReaderObject *self)
553{
554 if (self->logfp != NULL) {
555 fclose(self->logfp);
556 self->logfp = NULL;
557 }
558 PyObject_Del(self);
559}
560
561static PyObject *
562logreader_sq_item(LogReaderObject *self, int index)
563{
564 PyObject *result = logreader_tp_iternext(self);
565 if (result == NULL && !PyErr_Occurred()) {
566 PyErr_SetString(PyExc_IndexError, "no more events in log");
567 return NULL;
568 }
569 return result;
570}
571
Tim Petersfeab23f2001-10-13 00:11:10 +0000572static char next__doc__[] =
573"next() -> event-info\n"
574"Return the next event record from the log file.";
Fred Drake8c081a12001-10-12 20:57:55 +0000575
576static PyObject *
577logreader_next(LogReaderObject *self, PyObject *args)
578{
579 PyObject *result = NULL;
580
581 if (PyArg_ParseTuple(args, ":next")) {
582 result = logreader_tp_iternext(self);
583 /* XXX return None if there's nothing left */
584 /* tp_iternext does the right thing, though */
585 if (result == NULL && !PyErr_Occurred()) {
586 result = Py_None;
587 Py_INCREF(result);
588 }
589 }
590 return result;
591}
592
593
594static int
595flush_data(ProfilerObject *self)
596{
597 /* Need to dump data to the log file... */
598 size_t written = fwrite(self->buffer, 1, self->index, self->logfp);
Tim Peters1566a172001-10-12 22:08:39 +0000599 if (written == (size_t)self->index)
Fred Drake8c081a12001-10-12 20:57:55 +0000600 self->index = 0;
601 else {
602 memmove(self->buffer, &self->buffer[written],
603 self->index - written);
604 self->index -= written;
605 if (written == 0) {
606 char *s = PyString_AsString(self->logfilename);
607 PyErr_SetFromErrnoWithFilename(PyExc_IOError, s);
608 return -1;
609 }
610 }
611 if (written > 0) {
612 if (fflush(self->logfp)) {
613 char *s = PyString_AsString(self->logfilename);
614 PyErr_SetFromErrnoWithFilename(PyExc_IOError, s);
615 return -1;
616 }
617 }
618 return 0;
619}
620
621static inline void
622pack_packed_int(ProfilerObject *self, int value)
623{
624 unsigned char partial;
625
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}
635
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.
639 */
640static inline void
641pack_modified_packed_int(ProfilerObject *self, int value,
642 int modsize, int subfield)
643{
644 const int maxvalues[] = {-1, 1, 3, 7, 15, 31, 63, 127};
645
646 int bits = 7 - modsize;
647 int partial = value & maxvalues[bits];
648 unsigned char b = subfield | (partial << modsize);
649
650 if (partial != value) {
651 b |= 0x80;
652 self->buffer[self->index] = b;
653 self->index++;
654 pack_packed_int(self, value >> bits);
655 }
656 else {
657 self->buffer[self->index] = b;
658 self->index++;
659 }
660}
661
662static void
Fred Drake30d1c752001-10-15 22:11:02 +0000663pack_string(ProfilerObject *self, const char *s, int len)
Fred Drake8c081a12001-10-12 20:57:55 +0000664{
Fred Drake30d1c752001-10-15 22:11:02 +0000665 if (len + PISIZE + self->index >= BUFFERSIZE)
Fred Drake8c081a12001-10-12 20:57:55 +0000666 (void) flush_data(self);
Fred Drake30d1c752001-10-15 22:11:02 +0000667 pack_packed_int(self, len);
Fred Drake8c081a12001-10-12 20:57:55 +0000668 memcpy(self->buffer + self->index, s, len);
669 self->index += len;
670}
671
672static void
673pack_add_info(ProfilerObject *self, const char *s1, const char *s2)
674{
675 int len1 = strlen(s1);
676 int len2 = strlen(s2);
677
678 if (len1 + len2 + PISIZE*2 + 1 + self->index >= BUFFERSIZE)
679 (void) flush_data(self);
680 self->buffer[self->index] = WHAT_ADD_INFO;
681 self->index++;
Fred Drake30d1c752001-10-15 22:11:02 +0000682 pack_string(self, s1, len1);
683 pack_string(self, s2, len2);
Fred Drake8c081a12001-10-12 20:57:55 +0000684}
685
686static void
687pack_define_file(ProfilerObject *self, int fileno, const char *filename)
688{
689 int len = strlen(filename);
690
691 if (len + PISIZE*2 + 1 + self->index >= BUFFERSIZE)
692 (void) flush_data(self);
693 self->buffer[self->index] = WHAT_DEFINE_FILE;
694 self->index++;
695 pack_packed_int(self, fileno);
Fred Drake30d1c752001-10-15 22:11:02 +0000696 pack_string(self, filename, len);
697}
698
699static void
700pack_define_func(ProfilerObject *self, int fileno, int lineno,
701 const char *funcname)
702{
703 int len = strlen(funcname);
704
705 if (len + PISIZE*3 + 1 + self->index >= BUFFERSIZE)
706 (void) flush_data(self);
707 self->buffer[self->index] = WHAT_DEFINE_FUNC;
708 self->index++;
709 pack_packed_int(self, fileno);
710 pack_packed_int(self, lineno);
711 pack_string(self, funcname, len);
Fred Drake8c081a12001-10-12 20:57:55 +0000712}
713
714static void
715pack_line_times(ProfilerObject *self)
716{
717 if (2 + self->index >= BUFFERSIZE)
718 (void) flush_data(self);
719 self->buffer[self->index] = WHAT_LINE_TIMES;
720 self->buffer[self->index + 1] = self->linetimings ? 1 : 0;
721 self->index += 2;
722}
723
Fred Drake30d1c752001-10-15 22:11:02 +0000724static void
725pack_frame_times(ProfilerObject *self)
726{
727 if (2 + self->index >= BUFFERSIZE)
728 (void) flush_data(self);
729 self->buffer[self->index] = WHAT_FRAME_TIMES;
730 self->buffer[self->index + 1] = self->frametimings ? 1 : 0;
731 self->index += 2;
732}
733
Fred Drake8c081a12001-10-12 20:57:55 +0000734static inline void
735pack_enter(ProfilerObject *self, int fileno, int tdelta, int lineno)
736{
737 if (MPISIZE + PISIZE*2 + self->index >= BUFFERSIZE)
738 (void) flush_data(self);
739 pack_modified_packed_int(self, fileno, 2, WHAT_ENTER);
Fred Drake8c081a12001-10-12 20:57:55 +0000740 pack_packed_int(self, lineno);
Fred Drake30d1c752001-10-15 22:11:02 +0000741 if (self->frametimings)
742 pack_packed_int(self, tdelta);
Fred Drake8c081a12001-10-12 20:57:55 +0000743}
744
745static inline void
746pack_exit(ProfilerObject *self, int tdelta)
747{
748 if (MPISIZE + self->index >= BUFFERSIZE)
749 (void) flush_data(self);
Fred Drake30d1c752001-10-15 22:11:02 +0000750 if (self->frametimings)
751 pack_modified_packed_int(self, tdelta, 2, WHAT_EXIT);
752 else {
753 self->buffer[self->index] = WHAT_EXIT;
754 self->index++;
755 }
Fred Drake8c081a12001-10-12 20:57:55 +0000756}
757
758static inline void
759pack_lineno(ProfilerObject *self, int lineno)
760{
761 if (MPISIZE + self->index >= BUFFERSIZE)
762 (void) flush_data(self);
763 pack_modified_packed_int(self, lineno, 2, WHAT_LINENO);
764}
765
766static inline void
767pack_lineno_tdelta(ProfilerObject *self, int lineno, int tdelta)
768{
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);
773}
774
775static inline int
776get_fileno(ProfilerObject *self, PyCodeObject *fcode)
777{
Fred Drake30d1c752001-10-15 22:11:02 +0000778 /* This is only used for ENTER events. */
779
780 PyObject *obj;
781 PyObject *dict;
Fred Drake8c081a12001-10-12 20:57:55 +0000782 int fileno;
783
Fred Drake30d1c752001-10-15 22:11:02 +0000784 obj = PyDict_GetItem(self->filemap, fcode->co_filename);
785 if (obj == NULL) {
Fred Drake8c081a12001-10-12 20:57:55 +0000786 /* first sighting of this file */
Fred Drake30d1c752001-10-15 22:11:02 +0000787 dict = PyDict_New();
788 if (dict == NULL) {
Fred Drake8c081a12001-10-12 20:57:55 +0000789 return -1;
790 }
Fred Drake30d1c752001-10-15 22:11:02 +0000791 fileno = self->next_fileno;
792 obj = Py_BuildValue("iN", fileno, dict);
793 if (obj == NULL) {
794 return -1;
795 }
796 if (PyDict_SetItem(self->filemap, fcode->co_filename, obj)) {
797 Py_DECREF(obj);
Fred Drake8c081a12001-10-12 20:57:55 +0000798 return -1;
799 }
800 self->next_fileno++;
Fred Drake30d1c752001-10-15 22:11:02 +0000801 Py_DECREF(obj);
Fred Drake8c081a12001-10-12 20:57:55 +0000802 pack_define_file(self, fileno, PyString_AS_STRING(fcode->co_filename));
803 }
804 else {
805 /* already know this ID */
Fred Drake30d1c752001-10-15 22:11:02 +0000806 fileno = PyInt_AS_LONG(PyTuple_GET_ITEM(obj, 0));
807 dict = PyTuple_GET_ITEM(obj, 1);
808 }
809 /* make sure we save a function name for this (fileno, lineno) */
810 obj = PyInt_FromLong(fcode->co_firstlineno);
811 if (obj == NULL) {
812 /* We just won't have it saved; too bad. */
813 PyErr_Clear();
814 }
815 else {
816 PyObject *name = PyDict_GetItem(dict, obj);
817 if (name == NULL) {
818 pack_define_func(self, fileno, fcode->co_firstlineno,
819 PyString_AS_STRING(fcode->co_name));
820 if (PyDict_SetItem(dict, obj, fcode->co_name))
821 return -1;
822 }
Fred Drake8c081a12001-10-12 20:57:55 +0000823 }
824 return fileno;
825}
826
827static inline int
828get_tdelta(ProfilerObject *self)
829{
830 int tdelta;
831#ifdef MS_WIN32
832 hs_time tv;
Tim Peters7d99ff22001-10-13 07:37:52 +0000833 hs_time diff;
Fred Drake8c081a12001-10-12 20:57:55 +0000834
Tim Peters7d99ff22001-10-13 07:37:52 +0000835 GETTIMEOFDAY(&tv);
836 diff = tv - self->prev_timeofday;
837 tdelta = (int)diff;
Fred Drake8c081a12001-10-12 20:57:55 +0000838#else
839 struct timeval tv;
840
841 GETTIMEOFDAY(&tv);
842
843 if (tv.tv_sec == self->prev_timeofday.tv_sec)
844 tdelta = tv.tv_usec - self->prev_timeofday.tv_usec;
845 else
846 tdelta = ((tv.tv_sec - self->prev_timeofday.tv_sec) * 1000000
847 + tv.tv_usec);
848#endif
849 self->prev_timeofday = tv;
850 return tdelta;
851}
852
853
854/* The workhorse: the profiler callback function. */
855
856static int
857profiler_callback(ProfilerObject *self, PyFrameObject *frame, int what,
858 PyObject *arg)
859{
Fred Drake30d1c752001-10-15 22:11:02 +0000860 int tdelta = -1;
Fred Drake8c081a12001-10-12 20:57:55 +0000861 int fileno;
862
Fred Drake30d1c752001-10-15 22:11:02 +0000863 if (self->frametimings)
864 tdelta = get_tdelta(self);
Fred Drake8c081a12001-10-12 20:57:55 +0000865 switch (what) {
866 case PyTrace_CALL:
867 fileno = get_fileno(self, frame->f_code);
868 if (fileno < 0)
869 return -1;
870 pack_enter(self, fileno, tdelta,
871 frame->f_code->co_firstlineno);
872 break;
873 case PyTrace_RETURN:
874 pack_exit(self, tdelta);
875 break;
876 default:
877 /* should never get here */
878 break;
879 }
880 return 0;
881}
882
883
884/* Alternate callback when we want PyTrace_LINE events */
885
886static int
887tracer_callback(ProfilerObject *self, PyFrameObject *frame, int what,
888 PyObject *arg)
889{
890 int fileno;
891
Fred Drake8c081a12001-10-12 20:57:55 +0000892 switch (what) {
893 case PyTrace_CALL:
894 fileno = get_fileno(self, frame->f_code);
895 if (fileno < 0)
896 return -1;
Fred Drake30d1c752001-10-15 22:11:02 +0000897 pack_enter(self, fileno, self->frametimings ? get_tdelta(self) : -1,
Fred Drake8c081a12001-10-12 20:57:55 +0000898 frame->f_code->co_firstlineno);
899 break;
900 case PyTrace_RETURN:
901 pack_exit(self, get_tdelta(self));
902 break;
Tim Peters1566a172001-10-12 22:08:39 +0000903 case PyTrace_LINE:
Fred Drake8c081a12001-10-12 20:57:55 +0000904 if (self->linetimings)
905 pack_lineno_tdelta(self, frame->f_lineno, get_tdelta(self));
906 else
907 pack_lineno(self, frame->f_lineno);
908 break;
909 default:
910 /* ignore PyTrace_EXCEPTION */
911 break;
912 }
913 return 0;
914}
915
916
917/* A couple of useful helper functions. */
918
919#ifdef MS_WIN32
Tim Petersfeab23f2001-10-13 00:11:10 +0000920static LARGE_INTEGER frequency = {0, 0};
Fred Drake8c081a12001-10-12 20:57:55 +0000921#endif
922
923static unsigned long timeofday_diff = 0;
924static unsigned long rusage_diff = 0;
925
926static void
927calibrate(void)
928{
929 hs_time tv1, tv2;
930
931#ifdef MS_WIN32
Tim Peters7d99ff22001-10-13 07:37:52 +0000932 hs_time diff;
Fred Drake8c081a12001-10-12 20:57:55 +0000933 QueryPerformanceFrequency(&frequency);
934#endif
935
936 GETTIMEOFDAY(&tv1);
937 while (1) {
938 GETTIMEOFDAY(&tv2);
939#ifdef MS_WIN32
Tim Peters7d99ff22001-10-13 07:37:52 +0000940 diff = tv2 - tv1;
941 if (diff != 0) {
942 timeofday_diff = (unsigned long)diff;
Fred Drake8c081a12001-10-12 20:57:55 +0000943 break;
944 }
945#else
946 if (tv1.tv_sec != tv2.tv_sec || tv1.tv_usec != tv2.tv_usec) {
947 if (tv1.tv_sec == tv2.tv_sec)
948 timeofday_diff = tv2.tv_usec - tv1.tv_usec;
949 else
950 timeofday_diff = (1000000 - tv1.tv_usec) + tv2.tv_usec;
951 break;
952 }
953#endif
954 }
Jack Jansen963659a2001-10-23 22:26:16 +0000955#if defined(MS_WIN32) || defined(macintosh)
Fred Drake8c081a12001-10-12 20:57:55 +0000956 rusage_diff = -1;
957#else
958 {
959 struct rusage ru1, ru2;
960
961 getrusage(RUSAGE_SELF, &ru1);
962 while (1) {
963 getrusage(RUSAGE_SELF, &ru2);
964 if (ru1.ru_utime.tv_sec != ru2.ru_utime.tv_sec) {
965 rusage_diff = ((1000000 - ru1.ru_utime.tv_usec)
966 + ru2.ru_utime.tv_usec);
967 break;
968 }
969 else if (ru1.ru_utime.tv_usec != ru2.ru_utime.tv_usec) {
970 rusage_diff = ru2.ru_utime.tv_usec - ru1.ru_utime.tv_usec;
971 break;
972 }
973 else if (ru1.ru_stime.tv_sec != ru2.ru_stime.tv_sec) {
974 rusage_diff = ((1000000 - ru1.ru_stime.tv_usec)
975 + ru2.ru_stime.tv_usec);
976 break;
977 }
978 else if (ru1.ru_stime.tv_usec != ru2.ru_stime.tv_usec) {
979 rusage_diff = ru2.ru_stime.tv_usec - ru1.ru_stime.tv_usec;
980 break;
981 }
982 }
983 }
984#endif
985}
986
987static void
988do_start(ProfilerObject *self)
989{
990 self->active = 1;
991 GETTIMEOFDAY(&self->prev_timeofday);
992 if (self->lineevents)
993 PyEval_SetTrace((Py_tracefunc) tracer_callback, (PyObject *)self);
994 else
995 PyEval_SetProfile((Py_tracefunc) profiler_callback, (PyObject *)self);
996}
997
998static void
999do_stop(ProfilerObject *self)
1000{
1001 if (self->active) {
1002 self->active = 0;
1003 if (self->lineevents)
1004 PyEval_SetTrace(NULL, NULL);
1005 else
1006 PyEval_SetProfile(NULL, NULL);
1007 }
1008 if (self->index > 0) {
1009 /* Best effort to dump out any remaining data. */
1010 flush_data(self);
1011 }
1012}
1013
1014static int
1015is_available(ProfilerObject *self)
1016{
1017 if (self->active) {
1018 PyErr_SetString(ProfilerError, "profiler already active");
1019 return 0;
1020 }
1021 if (self->logfp == NULL) {
1022 PyErr_SetString(ProfilerError, "profiler already closed");
1023 return 0;
1024 }
1025 return 1;
1026}
1027
1028
1029/* Profiler object interface methods. */
1030
Fred Drake4c2e1af2001-10-29 20:45:57 +00001031static char addinfo__doc__[] =
1032"addinfo(key, value)\n"
1033"Insert an ADD_INFO record into the log.";
1034
1035static PyObject *
1036profiler_addinfo(ProfilerObject *self, PyObject *args)
1037{
1038 PyObject *result = NULL;
1039 char *key, *value;
1040
1041 if (PyArg_ParseTuple(args, "ss:addinfo", &key, &value)) {
1042 if (self->logfp == NULL)
1043 PyErr_SetString(ProfilerError, "profiler already closed");
1044 else {
1045 pack_add_info(self, key, value);
1046 result = Py_None;
1047 Py_INCREF(result);
1048 }
1049 }
1050 return result;
1051}
1052
Tim Petersfeab23f2001-10-13 00:11:10 +00001053static char close__doc__[] =
1054"close()\n"
1055"Shut down this profiler and close the log files, even if its active.";
Fred Drake8c081a12001-10-12 20:57:55 +00001056
1057static PyObject *
1058profiler_close(ProfilerObject *self, PyObject *args)
1059{
1060 PyObject *result = NULL;
1061
1062 if (PyArg_ParseTuple(args, ":close")) {
1063 do_stop(self);
1064 if (self->logfp != NULL) {
1065 fclose(self->logfp);
1066 self->logfp = NULL;
1067 }
1068 Py_INCREF(Py_None);
1069 result = Py_None;
1070 }
1071 return result;
1072}
1073
Tim Petersfeab23f2001-10-13 00:11:10 +00001074static char runcall__doc__[] =
1075"runcall(callable[, args[, kw]]) -> callable()\n"
1076"Profile a specific function call, returning the result of that call.";
Fred Drake8c081a12001-10-12 20:57:55 +00001077
1078static PyObject *
1079profiler_runcall(ProfilerObject *self, PyObject *args)
1080{
1081 PyObject *result = NULL;
1082 PyObject *callargs = NULL;
1083 PyObject *callkw = NULL;
1084 PyObject *callable;
1085
1086 if (PyArg_ParseTuple(args, "O|OO:runcall",
1087 &callable, &callargs, &callkw)) {
1088 if (is_available(self)) {
1089 do_start(self);
1090 result = PyEval_CallObjectWithKeywords(callable, callargs, callkw);
1091 do_stop(self);
1092 }
1093 }
1094 return result;
1095}
1096
Tim Petersfeab23f2001-10-13 00:11:10 +00001097static 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.";
Fred Drake8c081a12001-10-12 20:57:55 +00001101
1102static PyObject *
1103profiler_runcode(ProfilerObject *self, PyObject *args)
1104{
1105 PyObject *result = NULL;
1106 PyCodeObject *code;
1107 PyObject *globals;
1108 PyObject *locals = NULL;
1109
1110 if (PyArg_ParseTuple(args, "O!O!|O:runcode",
1111 &PyCode_Type, &code,
1112 &PyDict_Type, &globals,
1113 &locals)) {
1114 if (is_available(self)) {
1115 if (locals == NULL || locals == Py_None)
1116 locals = globals;
1117 else if (!PyDict_Check(locals)) {
1118 PyErr_SetString(PyExc_TypeError,
1119 "locals must be a dictionary or None");
1120 return NULL;
1121 }
1122 do_start(self);
1123 result = PyEval_EvalCode(code, globals, locals);
1124 do_stop(self);
1125#if 0
1126 if (!PyErr_Occurred()) {
1127 result = Py_None;
1128 Py_INCREF(result);
1129 }
1130#endif
1131 }
1132 }
1133 return result;
1134}
1135
Tim Petersfeab23f2001-10-13 00:11:10 +00001136static char start__doc__[] =
1137"start()\n"
1138"Install this profiler for the current thread.";
Fred Drake8c081a12001-10-12 20:57:55 +00001139
1140static PyObject *
1141profiler_start(ProfilerObject *self, PyObject *args)
1142{
1143 PyObject *result = NULL;
1144
1145 if (PyArg_ParseTuple(args, ":start")) {
1146 if (is_available(self))
1147 do_start(self);
1148 }
1149 return result;
1150}
1151
Tim Petersfeab23f2001-10-13 00:11:10 +00001152static char stop__doc__[] =
1153"stop()\n"
1154"Remove this profiler from the current thread.";
Fred Drake8c081a12001-10-12 20:57:55 +00001155
1156static PyObject *
1157profiler_stop(ProfilerObject *self, PyObject *args)
1158{
1159 PyObject *result = NULL;
1160
1161 if (PyArg_ParseTuple(args, ":stop")) {
1162 if (!self->active)
1163 PyErr_SetString(ProfilerError, "profiler not active");
1164 else
1165 do_stop(self);
1166 }
1167 return result;
1168}
1169
1170
1171/* Python API support. */
1172
1173static void
1174profiler_dealloc(ProfilerObject *self)
1175{
1176 do_stop(self);
1177 if (self->logfp != NULL)
1178 fclose(self->logfp);
1179 Py_XDECREF(self->filemap);
1180 Py_XDECREF(self->logfilename);
1181 PyObject_Del((PyObject *)self);
1182}
1183
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.
1187 */
1188static PyMethodDef profiler_methods[] = {
Fred Drake4c2e1af2001-10-29 20:45:57 +00001189 {"addinfo", (PyCFunction)profiler_addinfo, METH_VARARGS, addinfo__doc__},
Fred Drake8c081a12001-10-12 20:57:55 +00001190 {"close", (PyCFunction)profiler_close, METH_VARARGS, close__doc__},
1191 {"runcall", (PyCFunction)profiler_runcall, METH_VARARGS, runcall__doc__},
1192 {"runcode", (PyCFunction)profiler_runcode, METH_VARARGS, runcode__doc__},
1193 {"start", (PyCFunction)profiler_start, METH_VARARGS, start__doc__},
1194 {"stop", (PyCFunction)profiler_stop, METH_VARARGS, stop__doc__},
1195 {NULL, NULL}
1196};
1197
1198/* Use a table even though there's only one "simple" member; this allows
1199 * __members__ and therefore dir() to work.
1200 */
1201static struct memberlist profiler_members[] = {
Fred Drake30d1c752001-10-15 22:11:02 +00001202 {"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},
Fred Drake8c081a12001-10-12 20:57:55 +00001206 {NULL}
1207};
1208
1209static PyObject *
1210profiler_getattr(ProfilerObject *self, char *name)
1211{
1212 PyObject *result;
1213 if (strcmp(name, "closed") == 0) {
1214 result = (self->logfp == NULL) ? Py_True : Py_False;
1215 Py_INCREF(result);
1216 }
1217 else {
1218 result = PyMember_Get((char *)self, profiler_members, name);
1219 if (result == NULL) {
1220 PyErr_Clear();
1221 result = Py_FindMethod(profiler_methods, (PyObject *)self, name);
1222 }
1223 }
1224 return result;
1225}
1226
1227
Tim Petersfeab23f2001-10-13 00:11:10 +00001228static char profiler_object__doc__[] =
1229"High-performance profiler object.\n"
1230"\n"
1231"Methods:\n"
1232"\n"
Fred Drake30d1c752001-10-15 22:11:02 +00001233"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"
Tim Petersfeab23f2001-10-13 00:11:10 +00001238"\n"
1239"Attributes (read-only):\n"
1240"\n"
Fred Drake30d1c752001-10-15 22:11:02 +00001241"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.";
Fred Drake8c081a12001-10-12 20:57:55 +00001245
1246static PyTypeObject ProfilerType = {
1247 PyObject_HEAD_INIT(NULL)
1248 0, /* ob_size */
Fred Drake4c2e1af2001-10-29 20:45:57 +00001249 "_hotshot.ProfilerType", /* tp_name */
Fred Drake8c081a12001-10-12 20:57:55 +00001250 (int) sizeof(ProfilerObject), /* tp_basicsize */
1251 0, /* tp_itemsize */
1252 (destructor)profiler_dealloc, /* tp_dealloc */
1253 0, /* tp_print */
1254 (getattrfunc)profiler_getattr, /* tp_getattr */
1255 0, /* tp_setattr */
1256 0, /* tp_compare */
1257 0, /* tp_repr */
1258 0, /* tp_as_number */
1259 0, /* tp_as_sequence */
1260 0, /* tp_as_mapping */
1261 0, /* tp_hash */
1262 0, /* tp_call */
1263 0, /* tp_str */
1264 0, /* tp_getattro */
1265 0, /* tp_setattro */
1266 0, /* tp_as_buffer */
Fred Drake4c2e1af2001-10-29 20:45:57 +00001267 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake8c081a12001-10-12 20:57:55 +00001268 profiler_object__doc__, /* tp_doc */
1269};
1270
1271
1272static PyMethodDef logreader_methods[] = {
1273 {"close", (PyCFunction)logreader_close, METH_VARARGS,
1274 logreader_close__doc__},
1275 {"next", (PyCFunction)logreader_next, METH_VARARGS,
1276 next__doc__},
1277 {NULL, NULL}
1278};
1279
1280static PyObject *
Fred Drake4c2e1af2001-10-29 20:45:57 +00001281logreader_getattr(LogReaderObject *self, char *name)
Fred Drake8c081a12001-10-12 20:57:55 +00001282{
Fred Drake4c2e1af2001-10-29 20:45:57 +00001283 if (strcmp(name, "info") == 0) {
1284 Py_INCREF(self->info);
1285 return self->info;
1286 }
Fred Drake8c081a12001-10-12 20:57:55 +00001287 return Py_FindMethod(logreader_methods, (PyObject *)self, name);
1288}
1289
1290
1291static char logreader__doc__[] = "\
1292logreader(filename) --> log-iterator\n\
1293Create a log-reader for the timing information file.";
1294
1295static PySequenceMethods logreader_as_sequence = {
1296 0, /* sq_length */
1297 0, /* sq_concat */
1298 0, /* sq_repeat */
1299 (intargfunc)logreader_sq_item, /* sq_item */
1300 0, /* sq_slice */
1301 0, /* sq_ass_item */
1302 0, /* sq_ass_slice */
1303 0, /* sq_contains */
1304 0, /* sq_inplace_concat */
1305 0, /* sq_inplace_repeat */
1306};
1307
1308static PyTypeObject LogReaderType = {
1309 PyObject_HEAD_INIT(NULL)
1310 0, /* ob_size */
Fred Drake4c2e1af2001-10-29 20:45:57 +00001311 "_hotshot.LogReaderType", /* tp_name */
Fred Drake8c081a12001-10-12 20:57:55 +00001312 (int) sizeof(LogReaderObject), /* tp_basicsize */
1313 0, /* tp_itemsize */
1314 (destructor)logreader_dealloc, /* tp_dealloc */
1315 0, /* tp_print */
1316 (getattrfunc)logreader_getattr, /* tp_getattr */
1317 0, /* tp_setattr */
1318 0, /* tp_compare */
1319 0, /* tp_repr */
1320 0, /* tp_as_number */
1321 &logreader_as_sequence, /* tp_as_sequence */
1322 0, /* tp_as_mapping */
1323 0, /* tp_hash */
1324 0, /* tp_call */
1325 0, /* tp_str */
1326 0, /* tp_getattro */
1327 0, /* tp_setattro */
1328 0, /* tp_as_buffer */
Fred Drake4c2e1af2001-10-29 20:45:57 +00001329 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake8c081a12001-10-12 20:57:55 +00001330 logreader__doc__, /* tp_doc */
1331#if Py_TPFLAGS_HAVE_ITER
1332 0, /* tp_traverse */
1333 0, /* tp_clear */
1334 0, /* tp_richcompare */
1335 0, /* tp_weaklistoffset */
1336 (getiterfunc)logreader_tp_iter, /* tp_iter */
1337 (iternextfunc)logreader_tp_iternext,/* tp_iternext */
1338#endif
1339};
1340
1341static PyObject *
1342hotshot_logreader(PyObject *unused, PyObject *args)
1343{
1344 LogReaderObject *self = NULL;
1345 char *filename;
1346
1347 if (PyArg_ParseTuple(args, "s:logreader", &filename)) {
1348 self = PyObject_New(LogReaderObject, &LogReaderType);
1349 if (self != NULL) {
1350 self->filled = 0;
1351 self->index = 0;
Fred Drake30d1c752001-10-15 22:11:02 +00001352 self->frametimings = 1;
Fred Drake8c081a12001-10-12 20:57:55 +00001353 self->linetimings = 0;
Fred Drake4c2e1af2001-10-29 20:45:57 +00001354 self->info = NULL;
Fred Drake8c081a12001-10-12 20:57:55 +00001355 self->logfp = fopen(filename, "rb");
1356 if (self->logfp == NULL) {
1357 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
1358 Py_DECREF(self);
1359 self = NULL;
Fred Drake4c2e1af2001-10-29 20:45:57 +00001360 goto finally;
1361 }
1362 self->info = PyDict_New();
1363 if (self->info == NULL) {
1364 Py_DECREF(self);
1365 goto finally;
1366 }
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.
1370 */
1371 logreader_refill(self);
1372 while (self->filled > self->index
1373 && self->buffer[self->index] == WHAT_ADD_INFO) {
1374 int err = unpack_add_info(self, 1);
1375 if (err) {
1376 if (err == ERR_EOF)
1377 eof_error();
1378 else
1379 PyErr_SetString(PyExc_RuntimeError,
1380 "unexpected error");
1381 break;
1382 }
1383 /* Refill agressively so we can avoid EOF during
1384 * initialization unless there's a real EOF condition
1385 * (the tp_iternext handler loops attempts to refill
1386 * and try again).
1387 */
1388 logreader_refill(self);
Fred Drake8c081a12001-10-12 20:57:55 +00001389 }
1390 }
1391 }
Fred Drake4c2e1af2001-10-29 20:45:57 +00001392 finally:
Fred Drake8c081a12001-10-12 20:57:55 +00001393 return (PyObject *) self;
1394}
1395
1396
1397/* Return a Python string that represents the version number without the
1398 * extra cruft added by revision control, even if the right options were
1399 * given to the "cvs export" command to make it not include the extra
1400 * cruft.
1401 */
1402static char *
1403get_version_string(void)
1404{
1405 static char *rcsid = "$Revision$";
1406 char *rev = rcsid;
1407 char *buffer;
1408 int i = 0;
1409
1410 while (*rev && !isdigit(*rev))
1411 ++rev;
1412 while (rev[i] != ' ' && rev[i] != '\0')
1413 ++i;
1414 buffer = malloc(i + 1);
1415 if (buffer != NULL) {
1416 memmove(buffer, rev, i);
1417 buffer[i] = '\0';
1418 }
1419 return buffer;
1420}
1421
1422/* Write out a RFC 822-style header with various useful bits of
1423 * information to make the output easier to manage.
1424 */
1425static int
1426write_header(ProfilerObject *self)
1427{
1428 char *buffer;
1429 char cwdbuffer[PATH_MAX];
1430 PyObject *temp;
1431 int i, len;
1432
1433 buffer = get_version_string();
1434 if (buffer == NULL) {
1435 PyErr_NoMemory();
1436 return -1;
1437 }
Fred Drake4c2e1af2001-10-29 20:45:57 +00001438 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",
Fred Drake8c081a12001-10-12 20:57:55 +00001442 (self->lineevents ? "yes" : "no"));
Fred Drake4c2e1af2001-10-29 20:45:57 +00001443 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());
Fred Drake8c081a12001-10-12 20:57:55 +00001447 buffer = (char *) Py_GetVersion();
1448 if (buffer == NULL)
1449 PyErr_Clear();
1450 else
Fred Drake4c2e1af2001-10-29 20:45:57 +00001451 pack_add_info(self, "executable-version", buffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001452
1453#ifdef MS_WIN32
1454 sprintf(cwdbuffer, "%I64d", frequency.QuadPart);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001455 pack_add_info(self, "reported-performance-frequency", cwdbuffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001456#else
1457 sprintf(cwdbuffer, "%lu", rusage_diff);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001458 pack_add_info(self, "observed-interval-getrusage", cwdbuffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001459 sprintf(cwdbuffer, "%lu", timeofday_diff);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001460 pack_add_info(self, "observed-interval-gettimeofday", cwdbuffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001461#endif
Fred Drake8c081a12001-10-12 20:57:55 +00001462
Fred Drake4c2e1af2001-10-29 20:45:57 +00001463 pack_add_info(self, "current-directory",
Fred Drake8c081a12001-10-12 20:57:55 +00001464 getcwd(cwdbuffer, sizeof cwdbuffer));
1465
1466 temp = PySys_GetObject("path");
1467 len = PyList_GET_SIZE(temp);
1468 for (i = 0; i < len; ++i) {
1469 PyObject *item = PyList_GET_ITEM(temp, i);
1470 buffer = PyString_AsString(item);
1471 if (buffer == NULL)
1472 return -1;
Fred Drake4c2e1af2001-10-29 20:45:57 +00001473 pack_add_info(self, "sys-path-entry", buffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001474 }
Fred Drake4c2e1af2001-10-29 20:45:57 +00001475 pack_frame_times(self);
1476 pack_line_times(self);
1477
Fred Drake8c081a12001-10-12 20:57:55 +00001478 return 0;
1479}
1480
1481static char profiler__doc__[] = "\
1482profiler(logfilename[, lineevents[, linetimes]]) -> profiler\n\
1483Create a new profiler object.";
1484
1485static PyObject *
1486hotshot_profiler(PyObject *unused, PyObject *args)
1487{
1488 char *logfilename;
1489 ProfilerObject *self = NULL;
1490 int lineevents = 0;
1491 int linetimings = 1;
1492
1493 if (PyArg_ParseTuple(args, "s|ii:profiler", &logfilename,
1494 &lineevents, &linetimings)) {
1495 self = PyObject_New(ProfilerObject, &ProfilerType);
1496 if (self == NULL)
1497 return NULL;
Fred Drake30d1c752001-10-15 22:11:02 +00001498 self->frametimings = 1;
Fred Drake8c081a12001-10-12 20:57:55 +00001499 self->lineevents = lineevents ? 1 : 0;
1500 self->linetimings = (lineevents && linetimings) ? 1 : 0;
1501 self->index = 0;
1502 self->active = 0;
1503 self->next_fileno = 0;
Tim Peters1566a172001-10-12 22:08:39 +00001504 self->logfp = NULL;
Fred Drake8c081a12001-10-12 20:57:55 +00001505 self->logfilename = PyTuple_GET_ITEM(args, 0);
1506 Py_INCREF(self->logfilename);
1507 self->filemap = PyDict_New();
1508 if (self->filemap == NULL) {
1509 Py_DECREF(self);
1510 return NULL;
1511 }
1512 self->logfp = fopen(logfilename, "wb");
1513 if (self->logfp == NULL) {
1514 Py_DECREF(self);
1515 PyErr_SetFromErrnoWithFilename(PyExc_IOError, logfilename);
1516 return NULL;
1517 }
1518 if (timeofday_diff == 0) {
1519 /* Run this several times since sometimes the first
1520 * doesn't give the lowest values, and we're really trying
1521 * to determine the lowest.
1522 */
1523 calibrate();
1524 calibrate();
1525 calibrate();
1526 }
1527 if (write_header(self))
1528 /* some error occurred, exception has been set */
1529 self = NULL;
1530 }
1531 return (PyObject *) self;
1532}
1533
Fred Drake30d1c752001-10-15 22:11:02 +00001534static char coverage__doc__[] = "\
1535coverage(logfilename) -> profiler\n\
1536Returns a profiler that doesn't collect any timing information, which is\n\
1537useful in building a coverage analysis tool.";
1538
1539static PyObject *
1540hotshot_coverage(PyObject *unused, PyObject *args)
1541{
1542 char *logfilename;
1543 PyObject *result = NULL;
1544
1545 if (PyArg_ParseTuple(args, "s:coverage", &logfilename)) {
1546 result = hotshot_profiler(unused, args);
1547 if (result != NULL) {
1548 ProfilerObject *self = (ProfilerObject *) result;
1549 self->frametimings = 0;
1550 self->linetimings = 0;
1551 self->lineevents = 1;
1552 }
1553 }
1554 return result;
1555}
1556
Fred Drake8c081a12001-10-12 20:57:55 +00001557static char resolution__doc__[] =
1558#ifdef MS_WIN32
Tim Petersfeab23f2001-10-13 00:11:10 +00001559"resolution() -> (performance-counter-ticks, update-frequency)\n"
1560"Return the resolution of the timer provided by the QueryPerformanceCounter()\n"
1561"function. The first value is the smallest observed change, and the second\n"
1562"is the result of QueryPerformanceFrequency().";
Fred Drake8c081a12001-10-12 20:57:55 +00001563#else
Tim Petersfeab23f2001-10-13 00:11:10 +00001564"resolution() -> (gettimeofday-usecs, getrusage-usecs)\n"
1565"Return the resolution of the timers provided by the gettimeofday() and\n"
1566"getrusage() system calls, or -1 if the call is not supported.";
Fred Drake8c081a12001-10-12 20:57:55 +00001567#endif
1568
1569static PyObject *
1570hotshot_resolution(PyObject *unused, PyObject *args)
1571{
1572 PyObject *result = NULL;
1573
1574 if (PyArg_ParseTuple(args, ":resolution")) {
1575 if (timeofday_diff == 0) {
1576 calibrate();
1577 calibrate();
1578 calibrate();
1579 }
1580#ifdef MS_WIN32
1581 result = Py_BuildValue("ii", timeofday_diff, frequency.LowPart);
1582#else
1583 result = Py_BuildValue("ii", timeofday_diff, rusage_diff);
1584#endif
1585 }
1586 return result;
1587}
1588
1589
1590static PyMethodDef functions[] = {
Fred Drake30d1c752001-10-15 22:11:02 +00001591 {"coverage", hotshot_coverage, METH_VARARGS, coverage__doc__},
Fred Drake8c081a12001-10-12 20:57:55 +00001592 {"profiler", hotshot_profiler, METH_VARARGS, profiler__doc__},
1593 {"logreader", hotshot_logreader, METH_VARARGS, logreader__doc__},
1594 {"resolution", hotshot_resolution, METH_VARARGS, resolution__doc__},
1595 {NULL, NULL}
1596};
1597
1598
1599void
1600init_hotshot(void)
1601{
1602 PyObject *module;
1603
1604 LogReaderType.ob_type = &PyType_Type;
1605 ProfilerType.ob_type = &PyType_Type;
1606 module = Py_InitModule("_hotshot", functions);
1607 if (module != NULL) {
1608 char *s = get_version_string();
1609
1610 PyModule_AddStringConstant(module, "__version__", s);
1611 free(s);
1612 Py_INCREF(&LogReaderType);
1613 PyModule_AddObject(module, "LogReaderType",
1614 (PyObject *)&LogReaderType);
1615 Py_INCREF(&ProfilerType);
1616 PyModule_AddObject(module, "ProfilerType",
1617 (PyObject *)&ProfilerType);
1618
1619 if (ProfilerError == NULL)
1620 ProfilerError = PyErr_NewException("hotshot.ProfilerError",
1621 NULL, NULL);
1622 if (ProfilerError != NULL) {
1623 Py_INCREF(ProfilerError);
1624 PyModule_AddObject(module, "ProfilerError", ProfilerError);
1625 }
1626 PyModule_AddIntConstant(module, "WHAT_ENTER", WHAT_ENTER);
1627 PyModule_AddIntConstant(module, "WHAT_EXIT", WHAT_EXIT);
1628 PyModule_AddIntConstant(module, "WHAT_LINENO", WHAT_LINENO);
1629 PyModule_AddIntConstant(module, "WHAT_OTHER", WHAT_OTHER);
1630 PyModule_AddIntConstant(module, "WHAT_ADD_INFO", WHAT_ADD_INFO);
1631 PyModule_AddIntConstant(module, "WHAT_DEFINE_FILE", WHAT_DEFINE_FILE);
Fred Drake30d1c752001-10-15 22:11:02 +00001632 PyModule_AddIntConstant(module, "WHAT_DEFINE_FUNC", WHAT_DEFINE_FUNC);
Fred Drake8c081a12001-10-12 20:57:55 +00001633 PyModule_AddIntConstant(module, "WHAT_LINE_TIMES", WHAT_LINE_TIMES);
1634 }
1635}