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