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