blob: 162a3197445e1fc8e086624625312b0f88d91b54 [file] [log] [blame]
Fred Drake8c081a12001-10-12 20:57:55 +00001/*
2 * This is the High Performance Python Profiler portion of HotShot.
3 */
4
Tim Peters885d4572001-11-28 20:27:42 +00005#include "Python.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "code.h"
Tim Peters885d4572001-11-28 20:27:42 +00007#include "eval.h"
8#include "frameobject.h"
9#include "structmember.h"
Fred Drake8c081a12001-10-12 20:57:55 +000010
Fred Drake8c081a12001-10-12 20:57:55 +000011/*
12 * Which timer to use should be made more configurable, but that should not
Tim Petersfeab23f2001-10-13 00:11:10 +000013 * be difficult. This will do for now.
Fred Drake8c081a12001-10-12 20:57:55 +000014 */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000015#ifdef MS_WINDOWS
Fred Drake8c081a12001-10-12 20:57:55 +000016#include <windows.h>
Tim Peters1566a172001-10-12 22:08:39 +000017#include <direct.h> /* for getcwd() */
Tim Peters7d99ff22001-10-13 07:37:52 +000018typedef __int64 hs_time;
19#define GETTIMEOFDAY(P_HS_TIME) \
20 { LARGE_INTEGER _temp; \
21 QueryPerformanceCounter(&_temp); \
22 *(P_HS_TIME) = _temp.QuadPart; }
23
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
Jack Janseneddc1442003-11-20 01:44:59 +000029#if (defined(PYOS_OS2) && defined(PYCC_GCC))
Jack Jansen963659a2001-10-23 22:26:16 +000030#include <sys/time.h>
31#else
Fred Drake8c081a12001-10-12 20:57:55 +000032#include <sys/resource.h>
33#include <sys/times.h>
Jack Jansen963659a2001-10-23 22:26:16 +000034#endif
Fred Drake8c081a12001-10-12 20:57:55 +000035typedef struct timeval hs_time;
36#endif
37
38#if !defined(__cplusplus) && !defined(inline)
39#ifdef __GNUC__
40#define inline __inline
41#endif
42#endif
43
44#ifndef inline
45#define inline
46#endif
47
48#define BUFFERSIZE 10240
49
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000050#if defined(PYOS_OS2) && defined(PYCC_GCC)
51#define PATH_MAX 260
52#endif
53
Martin v. Löwis8eb92a02002-09-19 08:03:21 +000054#if defined(__sgi) && _COMPILER_VERSION>700 && !defined(PATH_MAX)
55/* fix PATH_MAX not being defined with MIPSPro 7.x
56 if mode is ANSI C (default) */
57#define PATH_MAX 1024
58#endif
59
Tim Peters1566a172001-10-12 22:08:39 +000060#ifndef PATH_MAX
61# ifdef MAX_PATH
62# define PATH_MAX MAX_PATH
Martin v. Löwis21ee4092002-09-30 16:19:48 +000063# elif defined (_POSIX_PATH_MAX)
64# define PATH_MAX _POSIX_PATH_MAX
Tim Peters1566a172001-10-12 22:08:39 +000065# else
66# error "Need a defn. for PATH_MAX in _hotshot.c"
67# endif
68#endif
69
Fred Drake8c081a12001-10-12 20:57:55 +000070typedef struct {
71 PyObject_HEAD
72 PyObject *filemap;
73 PyObject *logfilename;
Martin v. Löwis18e16552006-02-15 17:27:45 +000074 Py_ssize_t index;
Fred Drake8c081a12001-10-12 20:57:55 +000075 unsigned char buffer[BUFFERSIZE];
76 FILE *logfp;
77 int lineevents;
78 int linetimings;
Fred Drake30d1c752001-10-15 22:11:02 +000079 int frametimings;
Fred Drake8c081a12001-10-12 20:57:55 +000080 /* size_t filled; */
81 int active;
82 int next_fileno;
Fred Drake8c081a12001-10-12 20:57:55 +000083 hs_time prev_timeofday;
84} ProfilerObject;
85
86typedef struct {
87 PyObject_HEAD
Fred Drake4c2e1af2001-10-29 20:45:57 +000088 PyObject *info;
Fred Drake8c081a12001-10-12 20:57:55 +000089 FILE *logfp;
Fred Drake8c081a12001-10-12 20:57:55 +000090 int linetimings;
Fred Drake30d1c752001-10-15 22:11:02 +000091 int frametimings;
Fred Drake8c081a12001-10-12 20:57:55 +000092} LogReaderObject;
93
94static PyObject * ProfilerError = NULL;
95
96
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000097#ifndef MS_WINDOWS
Fred Drake8c081a12001-10-12 20:57:55 +000098#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
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000108PyDoc_STRVAR(logreader_close__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +0000109"close()\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000110"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{
Fred Drake666bf522002-07-18 19:11:44 +0000115 if (self->logfp != NULL) {
116 fclose(self->logfp);
117 self->logfp = NULL;
Fred Drake8c081a12001-10-12 20:57:55 +0000118 }
Fred Drake666bf522002-07-18 19:11:44 +0000119 Py_INCREF(Py_None);
120
121 return Py_None;
122}
123
124PyDoc_STRVAR(logreader_fileno__doc__,
125"fileno() -> file descriptor\n"
126"Returns the file descriptor for the log file, if open.\n"
127"Raises ValueError if the log file is closed.");
128
129static PyObject *
130logreader_fileno(LogReaderObject *self)
131{
132 if (self->logfp == NULL) {
133 PyErr_SetString(PyExc_ValueError,
134 "logreader's file object already closed");
135 return NULL;
136 }
137 return PyInt_FromLong(fileno(self->logfp));
Fred Drake8c081a12001-10-12 20:57:55 +0000138}
139
Fred Drake8c081a12001-10-12 20:57:55 +0000140
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
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000152 * 0x02 LINENO execution moved onto a different line
Fred Drake8c081a12001-10-12 20:57:55 +0000153 * 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{
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000276 int c;
Fred Drake8c081a12001-10-12 20:57:55 +0000277 int accum = 0;
278 int bits = 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000279 int cont;
280
281 do {
Fred Drake8c081a12001-10-12 20:57:55 +0000282 /* read byte */
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000283 if ((c = fgetc(self->logfp)) == EOF)
284 return ERR_EOF;
285 accum |= ((c & 0x7F) >> discard) << bits;
Fred Drake8c081a12001-10-12 20:57:55 +0000286 bits += (7 - discard);
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000287 cont = c & 0x80;
Fred Drake8c081a12001-10-12 20:57:55 +0000288 discard = 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000289 } while (cont);
290
Fred Drake8c081a12001-10-12 20:57:55 +0000291 *pvalue = accum;
292
293 return 0;
294}
295
296/* Unpack a string, which is encoded as a packed integer giving the
297 * length of the string, followed by the string data.
298 */
299static int
300unpack_string(LogReaderObject *self, PyObject **pvalue)
301{
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000302 int i;
Fred Drake8c081a12001-10-12 20:57:55 +0000303 int len;
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000304 int err;
Guido van Rossum0b624f62002-07-20 00:38:01 +0000305 int ch;
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000306 char *buf;
307
308 if ((err = unpack_packed_int(self, &len, 0)))
309 return err;
Fred Drake8c081a12001-10-12 20:57:55 +0000310
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000311 buf = malloc(len);
312 for (i=0; i < len; i++) {
Guido van Rossum0b624f62002-07-20 00:38:01 +0000313 ch = fgetc(self->logfp);
314 buf[i] = ch;
315 if (ch == EOF) {
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000316 free(buf);
317 return ERR_EOF;
Fred Drake8c081a12001-10-12 20:57:55 +0000318 }
319 }
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000320 *pvalue = PyString_FromStringAndSize(buf, len);
321 free(buf);
322 if (*pvalue == NULL) {
323 return ERR_EXCEPTION;
324 }
325 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000326}
327
328
Fred Drake4c2e1af2001-10-29 20:45:57 +0000329static int
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000330unpack_add_info(LogReaderObject *self)
Fred Drake4c2e1af2001-10-29 20:45:57 +0000331{
332 PyObject *key;
333 PyObject *value = NULL;
334 int err;
335
Fred Drake4c2e1af2001-10-29 20:45:57 +0000336 err = unpack_string(self, &key);
337 if (!err) {
338 err = unpack_string(self, &value);
339 if (err)
340 Py_DECREF(key);
341 else {
342 PyObject *list = PyDict_GetItem(self->info, key);
343 if (list == NULL) {
344 list = PyList_New(0);
345 if (list == NULL) {
346 err = ERR_EXCEPTION;
347 goto finally;
348 }
349 if (PyDict_SetItem(self->info, key, list)) {
Armin Rigoa4127692004-08-03 08:33:55 +0000350 Py_DECREF(list);
Fred Drake4c2e1af2001-10-29 20:45:57 +0000351 err = ERR_EXCEPTION;
352 goto finally;
353 }
Armin Rigoa4127692004-08-03 08:33:55 +0000354 Py_DECREF(list);
Fred Drake4c2e1af2001-10-29 20:45:57 +0000355 }
356 if (PyList_Append(list, value))
357 err = ERR_EXCEPTION;
358 }
359 }
360 finally:
361 Py_XDECREF(key);
362 Py_XDECREF(value);
363 return err;
364}
365
366
367static void
Fred Drake666bf522002-07-18 19:11:44 +0000368eof_error(LogReaderObject *self)
Fred Drake4c2e1af2001-10-29 20:45:57 +0000369{
Fred Drake666bf522002-07-18 19:11:44 +0000370 fclose(self->logfp);
371 self->logfp = NULL;
Fred Drake4c2e1af2001-10-29 20:45:57 +0000372 PyErr_SetString(PyExc_EOFError,
373 "end of file with incomplete profile record");
374}
375
Fred Drake8c081a12001-10-12 20:57:55 +0000376static PyObject *
377logreader_tp_iternext(LogReaderObject *self)
378{
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000379 int c;
380 int what;
Fred Drake8c081a12001-10-12 20:57:55 +0000381 int err = ERR_NONE;
382 int lineno = -1;
383 int fileno = -1;
384 int tdelta = -1;
385 PyObject *s1 = NULL, *s2 = NULL;
386 PyObject *result = NULL;
387#if 0
388 unsigned char b0, b1;
389#endif
390
391 if (self->logfp == NULL) {
392 PyErr_SetString(ProfilerError,
393 "cannot iterate over closed LogReader object");
394 return NULL;
395 }
Fred Drake4c2e1af2001-10-29 20:45:57 +0000396
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000397restart:
398 /* decode the record type */
Fred Drake666bf522002-07-18 19:11:44 +0000399 if ((c = fgetc(self->logfp)) == EOF) {
400 fclose(self->logfp);
401 self->logfp = NULL;
Fred Drake8c081a12001-10-12 20:57:55 +0000402 return NULL;
Fred Drake666bf522002-07-18 19:11:44 +0000403 }
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000404 what = c & WHAT_OTHER;
405 if (what == WHAT_OTHER)
406 what = c; /* need all the bits for type */
407 else
408 ungetc(c, self->logfp); /* type byte includes packed int */
Fred Drake8c081a12001-10-12 20:57:55 +0000409
Fred Drake8c081a12001-10-12 20:57:55 +0000410 switch (what) {
411 case WHAT_ENTER:
412 err = unpack_packed_int(self, &fileno, 2);
413 if (!err) {
Fred Drake30d1c752001-10-15 22:11:02 +0000414 err = unpack_packed_int(self, &lineno, 0);
415 if (self->frametimings && !err)
416 err = unpack_packed_int(self, &tdelta, 0);
Fred Drake8c081a12001-10-12 20:57:55 +0000417 }
418 break;
419 case WHAT_EXIT:
420 err = unpack_packed_int(self, &tdelta, 2);
421 break;
422 case WHAT_LINENO:
423 err = unpack_packed_int(self, &lineno, 2);
424 if (self->linetimings && !err)
425 err = unpack_packed_int(self, &tdelta, 0);
426 break;
427 case WHAT_ADD_INFO:
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000428 err = unpack_add_info(self);
Fred Drake8c081a12001-10-12 20:57:55 +0000429 break;
430 case WHAT_DEFINE_FILE:
431 err = unpack_packed_int(self, &fileno, 0);
432 if (!err) {
433 err = unpack_string(self, &s1);
434 if (!err) {
435 Py_INCREF(Py_None);
436 s2 = Py_None;
437 }
438 }
439 break;
Fred Drake30d1c752001-10-15 22:11:02 +0000440 case WHAT_DEFINE_FUNC:
441 err = unpack_packed_int(self, &fileno, 0);
442 if (!err) {
443 err = unpack_packed_int(self, &lineno, 0);
444 if (!err)
445 err = unpack_string(self, &s1);
446 }
447 break;
Fred Drake8c081a12001-10-12 20:57:55 +0000448 case WHAT_LINE_TIMES:
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000449 if ((c = fgetc(self->logfp)) == EOF)
Fred Drake8c081a12001-10-12 20:57:55 +0000450 err = ERR_EOF;
451 else {
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000452 self->linetimings = c ? 1 : 0;
453 goto restart;
454 }
Fred Drake4c2e1af2001-10-29 20:45:57 +0000455 break;
Fred Drake30d1c752001-10-15 22:11:02 +0000456 case WHAT_FRAME_TIMES:
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000457 if ((c = fgetc(self->logfp)) == EOF)
Fred Drake30d1c752001-10-15 22:11:02 +0000458 err = ERR_EOF;
459 else {
Neil Schemenauer8b6b4912002-05-29 18:19:14 +0000460 self->frametimings = c ? 1 : 0;
461 goto restart;
462 }
Fred Drake4c2e1af2001-10-29 20:45:57 +0000463 break;
Fred Drake8c081a12001-10-12 20:57:55 +0000464 default:
Fred Drake4c2e1af2001-10-29 20:45:57 +0000465 err = ERR_BAD_RECTYPE;
Fred Drake8c081a12001-10-12 20:57:55 +0000466 }
Fred Drake4c2e1af2001-10-29 20:45:57 +0000467 if (err == ERR_BAD_RECTYPE) {
468 PyErr_SetString(PyExc_ValueError,
469 "unknown record type in log file");
470 }
471 else if (err == ERR_EOF) {
Fred Drake666bf522002-07-18 19:11:44 +0000472 eof_error(self);
Fred Drake8c081a12001-10-12 20:57:55 +0000473 }
474 else if (!err) {
475 result = PyTuple_New(4);
476 PyTuple_SET_ITEM(result, 0, PyInt_FromLong(what));
477 PyTuple_SET_ITEM(result, 2, PyInt_FromLong(fileno));
Fred Drake30d1c752001-10-15 22:11:02 +0000478 if (s1 == NULL)
Fred Drake8c081a12001-10-12 20:57:55 +0000479 PyTuple_SET_ITEM(result, 1, PyInt_FromLong(tdelta));
Fred Drake30d1c752001-10-15 22:11:02 +0000480 else
Fred Drake8c081a12001-10-12 20:57:55 +0000481 PyTuple_SET_ITEM(result, 1, s1);
Fred Drake30d1c752001-10-15 22:11:02 +0000482 if (s2 == NULL)
483 PyTuple_SET_ITEM(result, 3, PyInt_FromLong(lineno));
484 else
Fred Drake8c081a12001-10-12 20:57:55 +0000485 PyTuple_SET_ITEM(result, 3, s2);
Fred Drake8c081a12001-10-12 20:57:55 +0000486 }
487 /* The only other case is err == ERR_EXCEPTION, in which case the
488 * exception is already set.
489 */
490#if 0
491 b0 = self->buffer[self->index];
492 b1 = self->buffer[self->index + 1];
493 if (b0 & 1) {
494 /* This is a line-number event. */
495 what = PyTrace_LINE;
496 lineno = ((b0 & ~1) << 7) + b1;
497 self->index += 2;
498 }
499 else {
500 what = (b0 & 0x0E) >> 1;
501 tdelta = ((b0 & 0xF0) << 4) + b1;
502 if (what == PyTrace_CALL) {
503 /* we know there's a 2-byte file ID & 2-byte line number */
504 fileno = ((self->buffer[self->index + 2] << 8)
505 + self->buffer[self->index + 3]);
506 lineno = ((self->buffer[self->index + 4] << 8)
507 + self->buffer[self->index + 5]);
508 self->index += 6;
509 }
510 else
511 self->index += 2;
512 }
513#endif
514 return result;
515}
516
517static void
518logreader_dealloc(LogReaderObject *self)
519{
520 if (self->logfp != NULL) {
521 fclose(self->logfp);
522 self->logfp = NULL;
523 }
Armin Rigoa4127692004-08-03 08:33:55 +0000524 Py_XDECREF(self->info);
Fred Drake8c081a12001-10-12 20:57:55 +0000525 PyObject_Del(self);
526}
527
528static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000529logreader_sq_item(LogReaderObject *self, Py_ssize_t index)
Fred Drake8c081a12001-10-12 20:57:55 +0000530{
531 PyObject *result = logreader_tp_iternext(self);
532 if (result == NULL && !PyErr_Occurred()) {
533 PyErr_SetString(PyExc_IndexError, "no more events in log");
534 return NULL;
535 }
536 return result;
537}
538
Fred Drake62c1e3c2001-12-04 21:40:53 +0000539static void
540do_stop(ProfilerObject *self);
Fred Drake8c081a12001-10-12 20:57:55 +0000541
542static int
543flush_data(ProfilerObject *self)
544{
545 /* Need to dump data to the log file... */
546 size_t written = fwrite(self->buffer, 1, self->index, self->logfp);
Tim Peters1566a172001-10-12 22:08:39 +0000547 if (written == (size_t)self->index)
Fred Drake8c081a12001-10-12 20:57:55 +0000548 self->index = 0;
549 else {
550 memmove(self->buffer, &self->buffer[written],
551 self->index - written);
552 self->index -= written;
553 if (written == 0) {
554 char *s = PyString_AsString(self->logfilename);
555 PyErr_SetFromErrnoWithFilename(PyExc_IOError, s);
Fred Drake62c1e3c2001-12-04 21:40:53 +0000556 do_stop(self);
Fred Drake8c081a12001-10-12 20:57:55 +0000557 return -1;
558 }
559 }
560 if (written > 0) {
561 if (fflush(self->logfp)) {
562 char *s = PyString_AsString(self->logfilename);
563 PyErr_SetFromErrnoWithFilename(PyExc_IOError, s);
Fred Drake62c1e3c2001-12-04 21:40:53 +0000564 do_stop(self);
Fred Drake8c081a12001-10-12 20:57:55 +0000565 return -1;
566 }
567 }
568 return 0;
569}
570
Fred Drake62c1e3c2001-12-04 21:40:53 +0000571static inline int
Fred Drake8c081a12001-10-12 20:57:55 +0000572pack_packed_int(ProfilerObject *self, int value)
573{
574 unsigned char partial;
575
576 do {
577 partial = value & 0x7F;
578 value >>= 7;
579 if (value)
580 partial |= 0x80;
581 self->buffer[self->index] = partial;
582 self->index++;
583 } while (value);
Fred Drake62c1e3c2001-12-04 21:40:53 +0000584 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000585}
586
587/* Encode a modified packed integer, with a subfield of modsize bits
588 * containing the value "subfield". The value of subfield is not
589 * checked to ensure it actually fits in modsize bits.
590 */
Fred Drake62c1e3c2001-12-04 21:40:53 +0000591static inline int
Fred Drake8c081a12001-10-12 20:57:55 +0000592pack_modified_packed_int(ProfilerObject *self, int value,
593 int modsize, int subfield)
594{
595 const int maxvalues[] = {-1, 1, 3, 7, 15, 31, 63, 127};
596
597 int bits = 7 - modsize;
598 int partial = value & maxvalues[bits];
599 unsigned char b = subfield | (partial << modsize);
600
601 if (partial != value) {
602 b |= 0x80;
603 self->buffer[self->index] = b;
604 self->index++;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000605 return pack_packed_int(self, value >> bits);
Fred Drake8c081a12001-10-12 20:57:55 +0000606 }
Fred Drake62c1e3c2001-12-04 21:40:53 +0000607 self->buffer[self->index] = b;
608 self->index++;
609 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000610}
611
Fred Drake62c1e3c2001-12-04 21:40:53 +0000612static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000613pack_string(ProfilerObject *self, const char *s, Py_ssize_t len)
Fred Drake8c081a12001-10-12 20:57:55 +0000614{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000615 if (len + PISIZE + self->index >= BUFFERSIZE) {
616 if (flush_data(self) < 0)
617 return -1;
618 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000619 assert(len < INT_MAX);
620 if (pack_packed_int(self, (int)len) < 0)
Fred Drake62c1e3c2001-12-04 21:40:53 +0000621 return -1;
Fred Drake8c081a12001-10-12 20:57:55 +0000622 memcpy(self->buffer + self->index, s, len);
623 self->index += len;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000624 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000625}
626
Fred Drake62c1e3c2001-12-04 21:40:53 +0000627static int
Fred Drake8c081a12001-10-12 20:57:55 +0000628pack_add_info(ProfilerObject *self, const char *s1, const char *s2)
629{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000630 Py_ssize_t len1 = strlen(s1);
631 Py_ssize_t len2 = strlen(s2);
Fred Drake8c081a12001-10-12 20:57:55 +0000632
Fred Drake62c1e3c2001-12-04 21:40:53 +0000633 if (len1 + len2 + PISIZE*2 + 1 + self->index >= BUFFERSIZE) {
634 if (flush_data(self) < 0)
635 return -1;
636 }
Fred Drake8c081a12001-10-12 20:57:55 +0000637 self->buffer[self->index] = WHAT_ADD_INFO;
638 self->index++;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000639 if (pack_string(self, s1, len1) < 0)
640 return -1;
641 return pack_string(self, s2, len2);
Fred Drake8c081a12001-10-12 20:57:55 +0000642}
643
Fred Drake62c1e3c2001-12-04 21:40:53 +0000644static int
Fred Drake8c081a12001-10-12 20:57:55 +0000645pack_define_file(ProfilerObject *self, int fileno, const char *filename)
646{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000647 Py_ssize_t len = strlen(filename);
Fred Drake8c081a12001-10-12 20:57:55 +0000648
Fred Drake62c1e3c2001-12-04 21:40:53 +0000649 if (len + PISIZE*2 + 1 + self->index >= BUFFERSIZE) {
650 if (flush_data(self) < 0)
651 return -1;
652 }
Fred Drake8c081a12001-10-12 20:57:55 +0000653 self->buffer[self->index] = WHAT_DEFINE_FILE;
654 self->index++;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000655 if (pack_packed_int(self, fileno) < 0)
656 return -1;
657 return pack_string(self, filename, len);
Fred Drake30d1c752001-10-15 22:11:02 +0000658}
659
Fred Drake62c1e3c2001-12-04 21:40:53 +0000660static int
Fred Drake30d1c752001-10-15 22:11:02 +0000661pack_define_func(ProfilerObject *self, int fileno, int lineno,
662 const char *funcname)
663{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000664 Py_ssize_t len = strlen(funcname);
Fred Drake30d1c752001-10-15 22:11:02 +0000665
Fred Drake62c1e3c2001-12-04 21:40:53 +0000666 if (len + PISIZE*3 + 1 + self->index >= BUFFERSIZE) {
667 if (flush_data(self) < 0)
668 return -1;
669 }
Fred Drake30d1c752001-10-15 22:11:02 +0000670 self->buffer[self->index] = WHAT_DEFINE_FUNC;
671 self->index++;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000672 if (pack_packed_int(self, fileno) < 0)
673 return -1;
674 if (pack_packed_int(self, lineno) < 0)
675 return -1;
676 return pack_string(self, funcname, len);
Fred Drake8c081a12001-10-12 20:57:55 +0000677}
678
Fred Drake62c1e3c2001-12-04 21:40:53 +0000679static int
Fred Drake8c081a12001-10-12 20:57:55 +0000680pack_line_times(ProfilerObject *self)
681{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000682 if (2 + self->index >= BUFFERSIZE) {
683 if (flush_data(self) < 0)
684 return -1;
685 }
Fred Drake8c081a12001-10-12 20:57:55 +0000686 self->buffer[self->index] = WHAT_LINE_TIMES;
687 self->buffer[self->index + 1] = self->linetimings ? 1 : 0;
688 self->index += 2;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000689 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000690}
691
Fred Drake62c1e3c2001-12-04 21:40:53 +0000692static int
Fred Drake30d1c752001-10-15 22:11:02 +0000693pack_frame_times(ProfilerObject *self)
694{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000695 if (2 + self->index >= BUFFERSIZE) {
696 if (flush_data(self) < 0)
697 return -1;
698 }
Fred Drake30d1c752001-10-15 22:11:02 +0000699 self->buffer[self->index] = WHAT_FRAME_TIMES;
700 self->buffer[self->index + 1] = self->frametimings ? 1 : 0;
701 self->index += 2;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000702 return 0;
Fred Drake30d1c752001-10-15 22:11:02 +0000703}
704
Fred Drake62c1e3c2001-12-04 21:40:53 +0000705static inline int
Fred Drake8c081a12001-10-12 20:57:55 +0000706pack_enter(ProfilerObject *self, int fileno, int tdelta, int lineno)
707{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000708 if (MPISIZE + PISIZE*2 + self->index >= BUFFERSIZE) {
709 if (flush_data(self) < 0)
710 return -1;
711 }
Fred Drake8c081a12001-10-12 20:57:55 +0000712 pack_modified_packed_int(self, fileno, 2, WHAT_ENTER);
Fred Drake8c081a12001-10-12 20:57:55 +0000713 pack_packed_int(self, lineno);
Fred Drake30d1c752001-10-15 22:11:02 +0000714 if (self->frametimings)
Fred Drake62c1e3c2001-12-04 21:40:53 +0000715 return pack_packed_int(self, tdelta);
716 else
717 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000718}
719
Fred Drake62c1e3c2001-12-04 21:40:53 +0000720static inline int
Fred Drake8c081a12001-10-12 20:57:55 +0000721pack_exit(ProfilerObject *self, int tdelta)
722{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000723 if (MPISIZE + self->index >= BUFFERSIZE) {
724 if (flush_data(self) < 0)
725 return -1;
Fred Drake30d1c752001-10-15 22:11:02 +0000726 }
Fred Drake62c1e3c2001-12-04 21:40:53 +0000727 if (self->frametimings)
728 return pack_modified_packed_int(self, tdelta, 2, WHAT_EXIT);
729 self->buffer[self->index] = WHAT_EXIT;
730 self->index++;
731 return 0;
Fred Drake8c081a12001-10-12 20:57:55 +0000732}
733
Fred Drake62c1e3c2001-12-04 21:40:53 +0000734static inline int
Fred Drake8c081a12001-10-12 20:57:55 +0000735pack_lineno(ProfilerObject *self, int lineno)
736{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000737 if (MPISIZE + self->index >= BUFFERSIZE) {
738 if (flush_data(self) < 0)
739 return -1;
740 }
741 return pack_modified_packed_int(self, lineno, 2, WHAT_LINENO);
Fred Drake8c081a12001-10-12 20:57:55 +0000742}
743
Fred Drake62c1e3c2001-12-04 21:40:53 +0000744static inline int
Fred Drake8c081a12001-10-12 20:57:55 +0000745pack_lineno_tdelta(ProfilerObject *self, int lineno, int tdelta)
746{
Fred Drake62c1e3c2001-12-04 21:40:53 +0000747 if (MPISIZE + PISIZE + self->index >= BUFFERSIZE) {
748 if (flush_data(self) < 0)
749 return 0;
750 }
751 if (pack_modified_packed_int(self, lineno, 2, WHAT_LINENO) < 0)
752 return -1;
753 return pack_packed_int(self, tdelta);
Fred Drake8c081a12001-10-12 20:57:55 +0000754}
755
756static inline int
757get_fileno(ProfilerObject *self, PyCodeObject *fcode)
758{
Fred Drake30d1c752001-10-15 22:11:02 +0000759 /* This is only used for ENTER events. */
760
761 PyObject *obj;
762 PyObject *dict;
Fred Drake8c081a12001-10-12 20:57:55 +0000763 int fileno;
764
Fred Drake30d1c752001-10-15 22:11:02 +0000765 obj = PyDict_GetItem(self->filemap, fcode->co_filename);
766 if (obj == NULL) {
Fred Drake8c081a12001-10-12 20:57:55 +0000767 /* first sighting of this file */
Fred Drake30d1c752001-10-15 22:11:02 +0000768 dict = PyDict_New();
769 if (dict == NULL) {
Fred Drake8c081a12001-10-12 20:57:55 +0000770 return -1;
771 }
Fred Drake30d1c752001-10-15 22:11:02 +0000772 fileno = self->next_fileno;
773 obj = Py_BuildValue("iN", fileno, dict);
774 if (obj == NULL) {
775 return -1;
776 }
777 if (PyDict_SetItem(self->filemap, fcode->co_filename, obj)) {
778 Py_DECREF(obj);
Fred Drake8c081a12001-10-12 20:57:55 +0000779 return -1;
780 }
781 self->next_fileno++;
Fred Drake30d1c752001-10-15 22:11:02 +0000782 Py_DECREF(obj);
Fred Drake62c1e3c2001-12-04 21:40:53 +0000783 if (pack_define_file(self, fileno,
784 PyString_AS_STRING(fcode->co_filename)) < 0)
785 return -1;
Fred Drake8c081a12001-10-12 20:57:55 +0000786 }
787 else {
788 /* already know this ID */
Fred Drake30d1c752001-10-15 22:11:02 +0000789 fileno = PyInt_AS_LONG(PyTuple_GET_ITEM(obj, 0));
790 dict = PyTuple_GET_ITEM(obj, 1);
791 }
792 /* make sure we save a function name for this (fileno, lineno) */
793 obj = PyInt_FromLong(fcode->co_firstlineno);
794 if (obj == NULL) {
795 /* We just won't have it saved; too bad. */
796 PyErr_Clear();
797 }
798 else {
799 PyObject *name = PyDict_GetItem(dict, obj);
800 if (name == NULL) {
Fred Drake62c1e3c2001-12-04 21:40:53 +0000801 if (pack_define_func(self, fileno, fcode->co_firstlineno,
Armin Rigoa4127692004-08-03 08:33:55 +0000802 PyString_AS_STRING(fcode->co_name)) < 0) {
803 Py_DECREF(obj);
Fred Drake62c1e3c2001-12-04 21:40:53 +0000804 return -1;
Armin Rigoa4127692004-08-03 08:33:55 +0000805 }
806 if (PyDict_SetItem(dict, obj, fcode->co_name)) {
807 Py_DECREF(obj);
Fred Drake30d1c752001-10-15 22:11:02 +0000808 return -1;
Armin Rigoa4127692004-08-03 08:33:55 +0000809 }
Fred Drake30d1c752001-10-15 22:11:02 +0000810 }
Armin Rigoa4127692004-08-03 08:33:55 +0000811 Py_DECREF(obj);
Fred Drake8c081a12001-10-12 20:57:55 +0000812 }
813 return fileno;
814}
815
816static inline int
817get_tdelta(ProfilerObject *self)
818{
819 int tdelta;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000820#ifdef MS_WINDOWS
Fred Drake8c081a12001-10-12 20:57:55 +0000821 hs_time tv;
Tim Peters7d99ff22001-10-13 07:37:52 +0000822 hs_time diff;
Fred Drake8c081a12001-10-12 20:57:55 +0000823
Tim Peters7d99ff22001-10-13 07:37:52 +0000824 GETTIMEOFDAY(&tv);
825 diff = tv - self->prev_timeofday;
826 tdelta = (int)diff;
Fred Drake8c081a12001-10-12 20:57:55 +0000827#else
828 struct timeval tv;
829
830 GETTIMEOFDAY(&tv);
831
Neal Norwitz7a135162004-06-13 20:45:11 +0000832 tdelta = tv.tv_usec - self->prev_timeofday.tv_usec;
833 if (tv.tv_sec != self->prev_timeofday.tv_sec)
834 tdelta += (tv.tv_sec - self->prev_timeofday.tv_sec) * 1000000;
Fred Drake8c081a12001-10-12 20:57:55 +0000835#endif
Neal Norwitz7a135162004-06-13 20:45:11 +0000836 /* time can go backwards on some multiprocessor systems or by NTP */
837 if (tdelta < 0)
838 return 0;
839
Fred Drake8c081a12001-10-12 20:57:55 +0000840 self->prev_timeofday = tv;
841 return tdelta;
842}
843
844
845/* The workhorse: the profiler callback function. */
846
847static int
Fred Drake8c081a12001-10-12 20:57:55 +0000848tracer_callback(ProfilerObject *self, PyFrameObject *frame, int what,
849 PyObject *arg)
850{
851 int fileno;
852
Fred Drake8c081a12001-10-12 20:57:55 +0000853 switch (what) {
854 case PyTrace_CALL:
855 fileno = get_fileno(self, frame->f_code);
856 if (fileno < 0)
857 return -1;
Fred Drake62c1e3c2001-12-04 21:40:53 +0000858 return pack_enter(self, fileno,
859 self->frametimings ? get_tdelta(self) : -1,
860 frame->f_code->co_firstlineno);
861
Fred Drake8c081a12001-10-12 20:57:55 +0000862 case PyTrace_RETURN:
Fred Drake62c1e3c2001-12-04 21:40:53 +0000863 return pack_exit(self, get_tdelta(self));
864
Armin Rigode5f05f2005-12-06 14:07:39 +0000865 case PyTrace_LINE: /* we only get these events if we asked for them */
Fred Drake8c081a12001-10-12 20:57:55 +0000866 if (self->linetimings)
Michael W. Hudson806d1c82002-09-11 17:09:45 +0000867 return pack_lineno_tdelta(self, frame->f_lineno,
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000868 get_tdelta(self));
Fred Drake8c081a12001-10-12 20:57:55 +0000869 else
Michael W. Hudson02ff6a92002-09-11 15:36:32 +0000870 return pack_lineno(self, frame->f_lineno);
Fred Drake62c1e3c2001-12-04 21:40:53 +0000871
Fred Drake8c081a12001-10-12 20:57:55 +0000872 default:
873 /* ignore PyTrace_EXCEPTION */
874 break;
875 }
876 return 0;
877}
878
879
880/* A couple of useful helper functions. */
881
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000882#ifdef MS_WINDOWS
Tim Petersfeab23f2001-10-13 00:11:10 +0000883static LARGE_INTEGER frequency = {0, 0};
Fred Drake8c081a12001-10-12 20:57:55 +0000884#endif
885
886static unsigned long timeofday_diff = 0;
887static unsigned long rusage_diff = 0;
888
889static void
890calibrate(void)
891{
892 hs_time tv1, tv2;
893
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000894#ifdef MS_WINDOWS
Tim Peters7d99ff22001-10-13 07:37:52 +0000895 hs_time diff;
Fred Drake8c081a12001-10-12 20:57:55 +0000896 QueryPerformanceFrequency(&frequency);
897#endif
898
899 GETTIMEOFDAY(&tv1);
900 while (1) {
901 GETTIMEOFDAY(&tv2);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000902#ifdef MS_WINDOWS
Tim Peters7d99ff22001-10-13 07:37:52 +0000903 diff = tv2 - tv1;
904 if (diff != 0) {
905 timeofday_diff = (unsigned long)diff;
Fred Drake8c081a12001-10-12 20:57:55 +0000906 break;
907 }
908#else
909 if (tv1.tv_sec != tv2.tv_sec || tv1.tv_usec != tv2.tv_usec) {
910 if (tv1.tv_sec == tv2.tv_sec)
911 timeofday_diff = tv2.tv_usec - tv1.tv_usec;
912 else
913 timeofday_diff = (1000000 - tv1.tv_usec) + tv2.tv_usec;
914 break;
915 }
916#endif
917 }
Jack Janseneddc1442003-11-20 01:44:59 +0000918#if defined(MS_WINDOWS) || defined(PYOS_OS2) || \
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000919 defined(__VMS)
Fred Drake8c081a12001-10-12 20:57:55 +0000920 rusage_diff = -1;
921#else
922 {
923 struct rusage ru1, ru2;
924
925 getrusage(RUSAGE_SELF, &ru1);
926 while (1) {
927 getrusage(RUSAGE_SELF, &ru2);
928 if (ru1.ru_utime.tv_sec != ru2.ru_utime.tv_sec) {
929 rusage_diff = ((1000000 - ru1.ru_utime.tv_usec)
930 + ru2.ru_utime.tv_usec);
931 break;
932 }
933 else if (ru1.ru_utime.tv_usec != ru2.ru_utime.tv_usec) {
934 rusage_diff = ru2.ru_utime.tv_usec - ru1.ru_utime.tv_usec;
935 break;
936 }
937 else if (ru1.ru_stime.tv_sec != ru2.ru_stime.tv_sec) {
938 rusage_diff = ((1000000 - ru1.ru_stime.tv_usec)
939 + ru2.ru_stime.tv_usec);
940 break;
941 }
942 else if (ru1.ru_stime.tv_usec != ru2.ru_stime.tv_usec) {
943 rusage_diff = ru2.ru_stime.tv_usec - ru1.ru_stime.tv_usec;
944 break;
945 }
946 }
947 }
948#endif
949}
950
951static void
952do_start(ProfilerObject *self)
953{
954 self->active = 1;
955 GETTIMEOFDAY(&self->prev_timeofday);
956 if (self->lineevents)
957 PyEval_SetTrace((Py_tracefunc) tracer_callback, (PyObject *)self);
958 else
Armin Rigode5f05f2005-12-06 14:07:39 +0000959 PyEval_SetProfile((Py_tracefunc) tracer_callback, (PyObject *)self);
Fred Drake8c081a12001-10-12 20:57:55 +0000960}
961
962static void
963do_stop(ProfilerObject *self)
964{
965 if (self->active) {
966 self->active = 0;
967 if (self->lineevents)
968 PyEval_SetTrace(NULL, NULL);
969 else
970 PyEval_SetProfile(NULL, NULL);
971 }
972 if (self->index > 0) {
973 /* Best effort to dump out any remaining data. */
974 flush_data(self);
975 }
976}
977
978static int
979is_available(ProfilerObject *self)
980{
981 if (self->active) {
982 PyErr_SetString(ProfilerError, "profiler already active");
983 return 0;
984 }
985 if (self->logfp == NULL) {
986 PyErr_SetString(ProfilerError, "profiler already closed");
987 return 0;
988 }
989 return 1;
990}
991
992
993/* Profiler object interface methods. */
994
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000995PyDoc_STRVAR(addinfo__doc__,
Fred Drake4c2e1af2001-10-29 20:45:57 +0000996"addinfo(key, value)\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000997"Insert an ADD_INFO record into the log.");
Fred Drake4c2e1af2001-10-29 20:45:57 +0000998
999static PyObject *
1000profiler_addinfo(ProfilerObject *self, PyObject *args)
1001{
1002 PyObject *result = NULL;
1003 char *key, *value;
1004
1005 if (PyArg_ParseTuple(args, "ss:addinfo", &key, &value)) {
1006 if (self->logfp == NULL)
1007 PyErr_SetString(ProfilerError, "profiler already closed");
1008 else {
Fred Drake62c1e3c2001-12-04 21:40:53 +00001009 if (pack_add_info(self, key, value) == 0) {
1010 result = Py_None;
1011 Py_INCREF(result);
1012 }
Fred Drake4c2e1af2001-10-29 20:45:57 +00001013 }
1014 }
1015 return result;
1016}
1017
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001018PyDoc_STRVAR(close__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +00001019"close()\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001020"Shut down this profiler and close the log files, even if its active.");
Fred Drake8c081a12001-10-12 20:57:55 +00001021
1022static PyObject *
Fred Drake666bf522002-07-18 19:11:44 +00001023profiler_close(ProfilerObject *self)
Fred Drake8c081a12001-10-12 20:57:55 +00001024{
Fred Drake666bf522002-07-18 19:11:44 +00001025 do_stop(self);
1026 if (self->logfp != NULL) {
1027 fclose(self->logfp);
1028 self->logfp = NULL;
Fred Drake8c081a12001-10-12 20:57:55 +00001029 }
Fred Drake666bf522002-07-18 19:11:44 +00001030 Py_INCREF(Py_None);
1031 return Py_None;
1032}
1033
1034#define fileno__doc__ logreader_fileno__doc__
1035
1036static PyObject *
1037profiler_fileno(ProfilerObject *self)
1038{
1039 if (self->logfp == NULL) {
1040 PyErr_SetString(PyExc_ValueError,
1041 "profiler's file object already closed");
1042 return NULL;
1043 }
1044 return PyInt_FromLong(fileno(self->logfp));
Fred Drake8c081a12001-10-12 20:57:55 +00001045}
1046
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001047PyDoc_STRVAR(runcall__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +00001048"runcall(callable[, args[, kw]]) -> callable()\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001049"Profile a specific function call, returning the result of that call.");
Fred Drake8c081a12001-10-12 20:57:55 +00001050
1051static PyObject *
1052profiler_runcall(ProfilerObject *self, PyObject *args)
1053{
1054 PyObject *result = NULL;
1055 PyObject *callargs = NULL;
1056 PyObject *callkw = NULL;
1057 PyObject *callable;
1058
1059 if (PyArg_ParseTuple(args, "O|OO:runcall",
1060 &callable, &callargs, &callkw)) {
1061 if (is_available(self)) {
1062 do_start(self);
1063 result = PyEval_CallObjectWithKeywords(callable, callargs, callkw);
1064 do_stop(self);
1065 }
1066 }
1067 return result;
1068}
1069
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001070PyDoc_STRVAR(runcode__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +00001071"runcode(code, globals[, locals])\n"
1072"Execute a code object while collecting profile data. If locals is\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001073"omitted, globals is used for the locals as well.");
Fred Drake8c081a12001-10-12 20:57:55 +00001074
1075static PyObject *
1076profiler_runcode(ProfilerObject *self, PyObject *args)
1077{
1078 PyObject *result = NULL;
1079 PyCodeObject *code;
1080 PyObject *globals;
1081 PyObject *locals = NULL;
1082
1083 if (PyArg_ParseTuple(args, "O!O!|O:runcode",
1084 &PyCode_Type, &code,
1085 &PyDict_Type, &globals,
1086 &locals)) {
1087 if (is_available(self)) {
1088 if (locals == NULL || locals == Py_None)
1089 locals = globals;
1090 else if (!PyDict_Check(locals)) {
1091 PyErr_SetString(PyExc_TypeError,
1092 "locals must be a dictionary or None");
1093 return NULL;
1094 }
1095 do_start(self);
1096 result = PyEval_EvalCode(code, globals, locals);
1097 do_stop(self);
1098#if 0
1099 if (!PyErr_Occurred()) {
1100 result = Py_None;
1101 Py_INCREF(result);
1102 }
1103#endif
1104 }
1105 }
1106 return result;
1107}
1108
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001109PyDoc_STRVAR(start__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +00001110"start()\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001111"Install this profiler for the current thread.");
Fred Drake8c081a12001-10-12 20:57:55 +00001112
1113static PyObject *
1114profiler_start(ProfilerObject *self, PyObject *args)
1115{
1116 PyObject *result = NULL;
1117
Fred Drake666bf522002-07-18 19:11:44 +00001118 if (is_available(self)) {
1119 do_start(self);
1120 result = Py_None;
1121 Py_INCREF(result);
Fred Drake8c081a12001-10-12 20:57:55 +00001122 }
1123 return result;
1124}
1125
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001126PyDoc_STRVAR(stop__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +00001127"stop()\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001128"Remove this profiler from the current thread.");
Fred Drake8c081a12001-10-12 20:57:55 +00001129
1130static PyObject *
1131profiler_stop(ProfilerObject *self, PyObject *args)
1132{
1133 PyObject *result = NULL;
1134
Fred Drake666bf522002-07-18 19:11:44 +00001135 if (!self->active)
1136 PyErr_SetString(ProfilerError, "profiler not active");
1137 else {
1138 do_stop(self);
1139 result = Py_None;
1140 Py_INCREF(result);
Fred Drake8c081a12001-10-12 20:57:55 +00001141 }
1142 return result;
1143}
1144
1145
1146/* Python API support. */
1147
1148static void
1149profiler_dealloc(ProfilerObject *self)
1150{
1151 do_stop(self);
1152 if (self->logfp != NULL)
1153 fclose(self->logfp);
1154 Py_XDECREF(self->filemap);
1155 Py_XDECREF(self->logfilename);
1156 PyObject_Del((PyObject *)self);
1157}
1158
Fred Drake8c081a12001-10-12 20:57:55 +00001159static PyMethodDef profiler_methods[] = {
Fred Drake4c2e1af2001-10-29 20:45:57 +00001160 {"addinfo", (PyCFunction)profiler_addinfo, METH_VARARGS, addinfo__doc__},
Fred Drake666bf522002-07-18 19:11:44 +00001161 {"close", (PyCFunction)profiler_close, METH_NOARGS, close__doc__},
1162 {"fileno", (PyCFunction)profiler_fileno, METH_NOARGS, fileno__doc__},
Fred Drake8c081a12001-10-12 20:57:55 +00001163 {"runcall", (PyCFunction)profiler_runcall, METH_VARARGS, runcall__doc__},
1164 {"runcode", (PyCFunction)profiler_runcode, METH_VARARGS, runcode__doc__},
Fred Drake666bf522002-07-18 19:11:44 +00001165 {"start", (PyCFunction)profiler_start, METH_NOARGS, start__doc__},
1166 {"stop", (PyCFunction)profiler_stop, METH_NOARGS, stop__doc__},
Fred Drake8c081a12001-10-12 20:57:55 +00001167 {NULL, NULL}
1168};
1169
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001170static PyMemberDef profiler_members[] = {
Fred Drake30d1c752001-10-15 22:11:02 +00001171 {"frametimings", T_LONG, offsetof(ProfilerObject, linetimings), READONLY},
1172 {"lineevents", T_LONG, offsetof(ProfilerObject, lineevents), READONLY},
1173 {"linetimings", T_LONG, offsetof(ProfilerObject, linetimings), READONLY},
Fred Drake8c081a12001-10-12 20:57:55 +00001174 {NULL}
1175};
1176
1177static PyObject *
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001178profiler_get_closed(ProfilerObject *self, void *closure)
Fred Drake8c081a12001-10-12 20:57:55 +00001179{
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001180 PyObject *result = (self->logfp == NULL) ? Py_True : Py_False;
1181 Py_INCREF(result);
Fred Drake8c081a12001-10-12 20:57:55 +00001182 return result;
1183}
1184
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001185static PyGetSetDef profiler_getsets[] = {
Fred Draked1eb8b62002-07-17 18:54:20 +00001186 {"closed", (getter)profiler_get_closed, NULL,
Fred Drake5c3ed3d2002-07-17 19:38:05 +00001187 PyDoc_STR("True if the profiler's output file has already been closed.")},
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001188 {NULL}
1189};
1190
Fred Drake8c081a12001-10-12 20:57:55 +00001191
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001192PyDoc_STRVAR(profiler_object__doc__,
Tim Petersfeab23f2001-10-13 00:11:10 +00001193"High-performance profiler object.\n"
1194"\n"
1195"Methods:\n"
1196"\n"
Fred Drake30d1c752001-10-15 22:11:02 +00001197"close(): Stop the profiler and close the log files.\n"
Fred Drake666bf522002-07-18 19:11:44 +00001198"fileno(): Returns the file descriptor of the log file.\n"
Fred Drake30d1c752001-10-15 22:11:02 +00001199"runcall(): Run a single function call with profiling enabled.\n"
1200"runcode(): Execute a code object with profiling enabled.\n"
1201"start(): Install the profiler and return.\n"
1202"stop(): Remove the profiler.\n"
Tim Petersfeab23f2001-10-13 00:11:10 +00001203"\n"
1204"Attributes (read-only):\n"
1205"\n"
Fred Drake30d1c752001-10-15 22:11:02 +00001206"closed: True if the profiler has already been closed.\n"
1207"frametimings: True if ENTER/EXIT events collect timing information.\n"
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001208"lineevents: True if line events are reported to the profiler.\n"
1209"linetimings: True if line events collect timing information.");
Fred Drake8c081a12001-10-12 20:57:55 +00001210
1211static PyTypeObject ProfilerType = {
1212 PyObject_HEAD_INIT(NULL)
1213 0, /* ob_size */
Fred Drake4c2e1af2001-10-29 20:45:57 +00001214 "_hotshot.ProfilerType", /* tp_name */
Fred Drake8c081a12001-10-12 20:57:55 +00001215 (int) sizeof(ProfilerObject), /* tp_basicsize */
1216 0, /* tp_itemsize */
1217 (destructor)profiler_dealloc, /* tp_dealloc */
1218 0, /* tp_print */
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001219 0, /* tp_getattr */
Fred Drake8c081a12001-10-12 20:57:55 +00001220 0, /* tp_setattr */
1221 0, /* tp_compare */
1222 0, /* tp_repr */
1223 0, /* tp_as_number */
1224 0, /* tp_as_sequence */
1225 0, /* tp_as_mapping */
1226 0, /* tp_hash */
1227 0, /* tp_call */
1228 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00001229 PyObject_GenericGetAttr, /* tp_getattro */
Fred Drake8c081a12001-10-12 20:57:55 +00001230 0, /* tp_setattro */
1231 0, /* tp_as_buffer */
Fred Drake4c2e1af2001-10-29 20:45:57 +00001232 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake8c081a12001-10-12 20:57:55 +00001233 profiler_object__doc__, /* tp_doc */
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001234 0, /* tp_traverse */
1235 0, /* tp_clear */
1236 0, /* tp_richcompare */
1237 0, /* tp_weaklistoffset */
1238 0, /* tp_iter */
1239 0, /* tp_iternext */
1240 profiler_methods, /* tp_methods */
1241 profiler_members, /* tp_members */
1242 profiler_getsets, /* tp_getset */
1243 0, /* tp_base */
1244 0, /* tp_dict */
1245 0, /* tp_descr_get */
1246 0, /* tp_descr_set */
Fred Drake8c081a12001-10-12 20:57:55 +00001247};
1248
1249
1250static PyMethodDef logreader_methods[] = {
Fred Drake666bf522002-07-18 19:11:44 +00001251 {"close", (PyCFunction)logreader_close, METH_NOARGS,
Fred Drake8c081a12001-10-12 20:57:55 +00001252 logreader_close__doc__},
Fred Drake666bf522002-07-18 19:11:44 +00001253 {"fileno", (PyCFunction)logreader_fileno, METH_NOARGS,
1254 logreader_fileno__doc__},
Fred Drake8c081a12001-10-12 20:57:55 +00001255 {NULL, NULL}
1256};
1257
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001258static PyMemberDef logreader_members[] = {
Fred Drake5c3ed3d2002-07-17 19:38:05 +00001259 {"info", T_OBJECT, offsetof(LogReaderObject, info), RO,
1260 PyDoc_STR("Dictionary mapping informational keys to lists of values.")},
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001261 {NULL}
1262};
Fred Drake8c081a12001-10-12 20:57:55 +00001263
1264
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001265PyDoc_STRVAR(logreader__doc__,
1266"logreader(filename) --> log-iterator\n\
1267Create a log-reader for the timing information file.");
Fred Drake8c081a12001-10-12 20:57:55 +00001268
1269static PySequenceMethods logreader_as_sequence = {
1270 0, /* sq_length */
1271 0, /* sq_concat */
1272 0, /* sq_repeat */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001273 (ssizeargfunc)logreader_sq_item, /* sq_item */
Fred Drake8c081a12001-10-12 20:57:55 +00001274 0, /* sq_slice */
1275 0, /* sq_ass_item */
1276 0, /* sq_ass_slice */
1277 0, /* sq_contains */
1278 0, /* sq_inplace_concat */
1279 0, /* sq_inplace_repeat */
1280};
1281
Fred Drake666bf522002-07-18 19:11:44 +00001282static PyObject *
1283logreader_get_closed(LogReaderObject *self, void *closure)
1284{
1285 PyObject *result = (self->logfp == NULL) ? Py_True : Py_False;
1286 Py_INCREF(result);
1287 return result;
1288}
1289
1290static PyGetSetDef logreader_getsets[] = {
1291 {"closed", (getter)logreader_get_closed, NULL,
1292 PyDoc_STR("True if the logreader's input file has already been closed.")},
1293 {NULL}
1294};
1295
Fred Drake8c081a12001-10-12 20:57:55 +00001296static PyTypeObject LogReaderType = {
1297 PyObject_HEAD_INIT(NULL)
1298 0, /* ob_size */
Fred Drake4c2e1af2001-10-29 20:45:57 +00001299 "_hotshot.LogReaderType", /* tp_name */
Fred Drake8c081a12001-10-12 20:57:55 +00001300 (int) sizeof(LogReaderObject), /* tp_basicsize */
1301 0, /* tp_itemsize */
1302 (destructor)logreader_dealloc, /* tp_dealloc */
1303 0, /* tp_print */
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001304 0, /* tp_getattr */
Fred Drake8c081a12001-10-12 20:57:55 +00001305 0, /* tp_setattr */
1306 0, /* tp_compare */
1307 0, /* tp_repr */
1308 0, /* tp_as_number */
1309 &logreader_as_sequence, /* tp_as_sequence */
1310 0, /* tp_as_mapping */
1311 0, /* tp_hash */
1312 0, /* tp_call */
1313 0, /* tp_str */
Jason Tishlerfb8595d2003-01-06 12:41:26 +00001314 PyObject_GenericGetAttr, /* tp_getattro */
Fred Drake8c081a12001-10-12 20:57:55 +00001315 0, /* tp_setattro */
1316 0, /* tp_as_buffer */
Fred Drake4c2e1af2001-10-29 20:45:57 +00001317 Py_TPFLAGS_DEFAULT, /* tp_flags */
Fred Drake8c081a12001-10-12 20:57:55 +00001318 logreader__doc__, /* tp_doc */
Fred Drake8c081a12001-10-12 20:57:55 +00001319 0, /* tp_traverse */
1320 0, /* tp_clear */
1321 0, /* tp_richcompare */
1322 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +00001323 PyObject_SelfIter, /* tp_iter */
Fred Drake8c081a12001-10-12 20:57:55 +00001324 (iternextfunc)logreader_tp_iternext,/* tp_iternext */
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001325 logreader_methods, /* tp_methods */
1326 logreader_members, /* tp_members */
Fred Drake666bf522002-07-18 19:11:44 +00001327 logreader_getsets, /* tp_getset */
Guido van Rossum9cb64b92002-07-17 16:15:35 +00001328 0, /* tp_base */
1329 0, /* tp_dict */
1330 0, /* tp_descr_get */
1331 0, /* tp_descr_set */
Fred Drake8c081a12001-10-12 20:57:55 +00001332};
1333
1334static PyObject *
1335hotshot_logreader(PyObject *unused, PyObject *args)
1336{
1337 LogReaderObject *self = NULL;
1338 char *filename;
Neil Schemenauer8b6b4912002-05-29 18:19:14 +00001339 int c;
1340 int err = 0;
Fred Drake8c081a12001-10-12 20:57:55 +00001341
1342 if (PyArg_ParseTuple(args, "s:logreader", &filename)) {
1343 self = PyObject_New(LogReaderObject, &LogReaderType);
1344 if (self != NULL) {
Fred Drake30d1c752001-10-15 22:11:02 +00001345 self->frametimings = 1;
Fred Drake8c081a12001-10-12 20:57:55 +00001346 self->linetimings = 0;
Fred Drake4c2e1af2001-10-29 20:45:57 +00001347 self->info = NULL;
Fred Drake8c081a12001-10-12 20:57:55 +00001348 self->logfp = fopen(filename, "rb");
1349 if (self->logfp == NULL) {
1350 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
1351 Py_DECREF(self);
1352 self = NULL;
Fred Drake4c2e1af2001-10-29 20:45:57 +00001353 goto finally;
1354 }
1355 self->info = PyDict_New();
1356 if (self->info == NULL) {
1357 Py_DECREF(self);
1358 goto finally;
1359 }
Neil Schemenauer8b6b4912002-05-29 18:19:14 +00001360 /* read initial info */
1361 for (;;) {
1362 if ((c = fgetc(self->logfp)) == EOF) {
Fred Drake666bf522002-07-18 19:11:44 +00001363 eof_error(self);
Neil Schemenauer8b6b4912002-05-29 18:19:14 +00001364 break;
1365 }
1366 if (c != WHAT_ADD_INFO) {
1367 ungetc(c, self->logfp);
1368 break;
1369 }
1370 err = unpack_add_info(self);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001371 if (err) {
1372 if (err == ERR_EOF)
Fred Drake666bf522002-07-18 19:11:44 +00001373 eof_error(self);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001374 else
1375 PyErr_SetString(PyExc_RuntimeError,
1376 "unexpected error");
1377 break;
1378 }
Fred Drake8c081a12001-10-12 20:57:55 +00001379 }
1380 }
1381 }
Fred Drake4c2e1af2001-10-29 20:45:57 +00001382 finally:
Fred Drake8c081a12001-10-12 20:57:55 +00001383 return (PyObject *) self;
1384}
1385
1386
1387/* Return a Python string that represents the version number without the
1388 * extra cruft added by revision control, even if the right options were
1389 * given to the "cvs export" command to make it not include the extra
1390 * cruft.
1391 */
1392static char *
1393get_version_string(void)
1394{
1395 static char *rcsid = "$Revision$";
1396 char *rev = rcsid;
1397 char *buffer;
1398 int i = 0;
1399
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00001400 while (*rev && !isdigit(Py_CHARMASK(*rev)))
Fred Drake8c081a12001-10-12 20:57:55 +00001401 ++rev;
1402 while (rev[i] != ' ' && rev[i] != '\0')
1403 ++i;
1404 buffer = malloc(i + 1);
1405 if (buffer != NULL) {
1406 memmove(buffer, rev, i);
1407 buffer[i] = '\0';
1408 }
1409 return buffer;
1410}
1411
1412/* Write out a RFC 822-style header with various useful bits of
1413 * information to make the output easier to manage.
1414 */
1415static int
1416write_header(ProfilerObject *self)
1417{
1418 char *buffer;
1419 char cwdbuffer[PATH_MAX];
1420 PyObject *temp;
1421 int i, len;
1422
1423 buffer = get_version_string();
1424 if (buffer == NULL) {
1425 PyErr_NoMemory();
1426 return -1;
1427 }
Fred Drake4c2e1af2001-10-29 20:45:57 +00001428 pack_add_info(self, "hotshot-version", buffer);
1429 pack_add_info(self, "requested-frame-timings",
1430 (self->frametimings ? "yes" : "no"));
1431 pack_add_info(self, "requested-line-events",
Fred Drake8c081a12001-10-12 20:57:55 +00001432 (self->lineevents ? "yes" : "no"));
Fred Drake4c2e1af2001-10-29 20:45:57 +00001433 pack_add_info(self, "requested-line-timings",
1434 (self->linetimings ? "yes" : "no"));
1435 pack_add_info(self, "platform", Py_GetPlatform());
1436 pack_add_info(self, "executable", Py_GetProgramFullPath());
Fred Drakef12a68c2001-11-09 15:59:36 +00001437 free(buffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001438 buffer = (char *) Py_GetVersion();
1439 if (buffer == NULL)
1440 PyErr_Clear();
1441 else
Fred Drake4c2e1af2001-10-29 20:45:57 +00001442 pack_add_info(self, "executable-version", buffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001443
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001444#ifdef MS_WINDOWS
Tim Peters885d4572001-11-28 20:27:42 +00001445 PyOS_snprintf(cwdbuffer, sizeof(cwdbuffer), "%I64d", frequency.QuadPart);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001446 pack_add_info(self, "reported-performance-frequency", cwdbuffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001447#else
Tim Peters885d4572001-11-28 20:27:42 +00001448 PyOS_snprintf(cwdbuffer, sizeof(cwdbuffer), "%lu", rusage_diff);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001449 pack_add_info(self, "observed-interval-getrusage", cwdbuffer);
Tim Peters885d4572001-11-28 20:27:42 +00001450 PyOS_snprintf(cwdbuffer, sizeof(cwdbuffer), "%lu", timeofday_diff);
Fred Drake4c2e1af2001-10-29 20:45:57 +00001451 pack_add_info(self, "observed-interval-gettimeofday", cwdbuffer);
Fred Drake8c081a12001-10-12 20:57:55 +00001452#endif
Fred Drake8c081a12001-10-12 20:57:55 +00001453
Fred Drake4c2e1af2001-10-29 20:45:57 +00001454 pack_add_info(self, "current-directory",
Fred Drake8c081a12001-10-12 20:57:55 +00001455 getcwd(cwdbuffer, sizeof cwdbuffer));
1456
1457 temp = PySys_GetObject("path");
1458 len = PyList_GET_SIZE(temp);
1459 for (i = 0; i < len; ++i) {
1460 PyObject *item = PyList_GET_ITEM(temp, i);
1461 buffer = PyString_AsString(item);
Fred Draked1eb8b62002-07-17 18:54:20 +00001462 if (buffer == NULL) {
1463 pack_add_info(self, "sys-path-entry", "<non-string-path-entry>");
1464 PyErr_Clear();
1465 }
1466 else {
1467 pack_add_info(self, "sys-path-entry", buffer);
1468 }
Fred Drake8c081a12001-10-12 20:57:55 +00001469 }
Fred Drake4c2e1af2001-10-29 20:45:57 +00001470 pack_frame_times(self);
1471 pack_line_times(self);
1472
Fred Drake8c081a12001-10-12 20:57:55 +00001473 return 0;
1474}
1475
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001476PyDoc_STRVAR(profiler__doc__,
1477"profiler(logfilename[, lineevents[, linetimes]]) -> profiler\n\
1478Create a new profiler object.");
Fred Drake8c081a12001-10-12 20:57:55 +00001479
1480static PyObject *
1481hotshot_profiler(PyObject *unused, PyObject *args)
1482{
1483 char *logfilename;
1484 ProfilerObject *self = NULL;
1485 int lineevents = 0;
1486 int linetimings = 1;
1487
1488 if (PyArg_ParseTuple(args, "s|ii:profiler", &logfilename,
1489 &lineevents, &linetimings)) {
1490 self = PyObject_New(ProfilerObject, &ProfilerType);
1491 if (self == NULL)
1492 return NULL;
Fred Drake30d1c752001-10-15 22:11:02 +00001493 self->frametimings = 1;
Fred Drake8c081a12001-10-12 20:57:55 +00001494 self->lineevents = lineevents ? 1 : 0;
1495 self->linetimings = (lineevents && linetimings) ? 1 : 0;
1496 self->index = 0;
1497 self->active = 0;
1498 self->next_fileno = 0;
Tim Peters1566a172001-10-12 22:08:39 +00001499 self->logfp = NULL;
Fred Drake8c081a12001-10-12 20:57:55 +00001500 self->logfilename = PyTuple_GET_ITEM(args, 0);
1501 Py_INCREF(self->logfilename);
1502 self->filemap = PyDict_New();
1503 if (self->filemap == NULL) {
1504 Py_DECREF(self);
1505 return NULL;
1506 }
1507 self->logfp = fopen(logfilename, "wb");
1508 if (self->logfp == NULL) {
1509 Py_DECREF(self);
1510 PyErr_SetFromErrnoWithFilename(PyExc_IOError, logfilename);
1511 return NULL;
1512 }
1513 if (timeofday_diff == 0) {
1514 /* Run this several times since sometimes the first
1515 * doesn't give the lowest values, and we're really trying
1516 * to determine the lowest.
1517 */
1518 calibrate();
1519 calibrate();
1520 calibrate();
1521 }
1522 if (write_header(self))
1523 /* some error occurred, exception has been set */
1524 self = NULL;
1525 }
1526 return (PyObject *) self;
1527}
1528
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001529PyDoc_STRVAR(coverage__doc__,
1530"coverage(logfilename) -> profiler\n\
Fred Drake30d1c752001-10-15 22:11:02 +00001531Returns a profiler that doesn't collect any timing information, which is\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001532useful in building a coverage analysis tool.");
Fred Drake30d1c752001-10-15 22:11:02 +00001533
1534static PyObject *
1535hotshot_coverage(PyObject *unused, PyObject *args)
1536{
1537 char *logfilename;
1538 PyObject *result = NULL;
1539
1540 if (PyArg_ParseTuple(args, "s:coverage", &logfilename)) {
1541 result = hotshot_profiler(unused, args);
1542 if (result != NULL) {
1543 ProfilerObject *self = (ProfilerObject *) result;
1544 self->frametimings = 0;
1545 self->linetimings = 0;
1546 self->lineevents = 1;
1547 }
1548 }
1549 return result;
1550}
1551
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001552PyDoc_VAR(resolution__doc__) =
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001553#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001554PyDoc_STR(
Tim Petersfeab23f2001-10-13 00:11:10 +00001555"resolution() -> (performance-counter-ticks, update-frequency)\n"
1556"Return the resolution of the timer provided by the QueryPerformanceCounter()\n"
1557"function. The first value is the smallest observed change, and the second\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001558"is the result of QueryPerformanceFrequency()."
1559)
Fred Drake8c081a12001-10-12 20:57:55 +00001560#else
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001561PyDoc_STR(
Tim Petersfeab23f2001-10-13 00:11:10 +00001562"resolution() -> (gettimeofday-usecs, getrusage-usecs)\n"
1563"Return the resolution of the timers provided by the gettimeofday() and\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001564"getrusage() system calls, or -1 if the call is not supported."
1565)
Fred Drake8c081a12001-10-12 20:57:55 +00001566#endif
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001567;
Fred Drake8c081a12001-10-12 20:57:55 +00001568
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 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001580#ifdef MS_WINDOWS
Fred Drake8c081a12001-10-12 20:57:55 +00001581 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}