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