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