| 1 | #include "common_header.h" |
| 2 | #include <src/instrument.h> |
| 3 | |
| 4 | #ifdef PYPY_INSTRUMENT |
| 5 | |
| 6 | #include <sys/types.h> |
| 7 | #include <sys/stat.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <stdio.h> |
| 11 | #ifndef _WIN32 |
| 12 | #include <sys/mman.h> |
| 13 | #include <unistd.h> |
| 14 | #else |
| 15 | #include <windows.h> |
| 16 | #endif |
| 17 | |
| 18 | typedef Unsigned instrument_count_t; |
| 19 | |
| 20 | instrument_count_t *_instrument_counters = NULL; |
| 21 | |
| 22 | void instrument_setup() { |
| 23 | char *fname = getenv("PYPY_INSTRUMENT_COUNTERS"); |
| 24 | if (fname) { |
| 25 | int fd; |
| 26 | #ifdef _WIN32 |
| 27 | HANDLE map_handle; |
| 28 | HANDLE file_handle; |
| 29 | #endif |
| 30 | void *buf; |
| 31 | size_t sz = sizeof(instrument_count_t)*PYPY_INSTRUMENT_NCOUNTER; |
| 32 | fd = open(fname, O_CREAT|O_TRUNC|O_RDWR, 0744); |
| 33 | if (sz > 0) { |
| 34 | lseek(fd, sz-1, SEEK_SET); |
| 35 | (void)write(fd, "", 1); |
| 36 | #ifndef _WIN32 |
| 37 | buf = mmap(NULL, sz, PROT_WRITE|PROT_READ, MAP_SHARED, |
| 38 | fd, 0); |
| 39 | if (buf == MAP_FAILED) { |
| 40 | fprintf(stderr, "mapping instrument counters file failed\n"); |
| 41 | abort(); |
| 42 | } |
| 43 | #else |
| 44 | file_handle = (HANDLE)_get_osfhandle(fd); |
| 45 | map_handle = CreateFileMapping(file_handle, NULL, PAGE_READWRITE, |
| 46 | 0, sz, ""); |
| 47 | buf = MapViewOfFile(map_handle, FILE_MAP_WRITE, 0, 0, 0); |
| 48 | if (buf == 0) { |
| 49 | fprintf(stderr, "mapping instrument counters file failed\n"); |
| 50 | abort(); |
| 51 | } |
| 52 | #endif |
| 53 | _instrument_counters = (instrument_count_t *)buf; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void instrument_count(Signed label) { |
| 59 | if(_instrument_counters) { |
| 60 | _instrument_counters[label]++; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | #else |
| 65 | |
| 66 | void instrument_setup() { |
| 67 | } |
| 68 | |
| 69 | #endif |