Debian Codegraph @

rpython/rlib/rvmprof/src/shared/compat.c

raw symbols SCIP
1 #include "compat.h"
2
3 #include <string.h>
4 #include <assert.h>
5 #if VMPROF_WINDOWS
6 #define WIN32_LEAN_AND_MEAN
7 #include <Windows.h>
8 #else
9 #include <time.h>
10 #include <sys/time.h>
11 #endif
12
13 static int _vmp_profile_fileno = -1;
14
15 int vmp_profile_fileno(void) {
16 return _vmp_profile_fileno;
17 }
18 void vmp_set_profile_fileno(int fileno) {
19 _vmp_profile_fileno = fileno;
20 }
21
22 #ifndef VMPROF_WINDOWS
23 int vmp_write_all(const char *buf, size_t bufsize)
24 {
25 ssize_t count;
26 if (_vmp_profile_fileno == -1) {
27 return -1;
28 }
29 while (bufsize > 0) {
30 count = write(_vmp_profile_fileno, buf, bufsize);
31 if (count <= 0)
32 return -1; /* failed */
33 buf += count;
34 bufsize -= count;
35 }
36 return 0;
37 }
38 #endif
39
40 int vmp_write_meta(const char * key, const char * value)
41 {
42 char marker = MARKER_META;
43 long x = (long)strlen(key);
44 vmp_write_all(&marker, 1);
45 vmp_write_all((char*)&x, sizeof(long));
46 vmp_write_all(key, x);
47 x = (long)strlen(value);
48 vmp_write_all((char*)&x, sizeof(long));
49 vmp_write_all(value, x);
50 return 0;
51 }
52
53 /**
54 * Write the time and zone now.
55 */
56
57 struct timezone_buf {
58 int64_t tv_sec;
59 int64_t tv_usec;
60 };
61 #define __SIZE (1+sizeof(struct timezone_buf)+8)
62
63 #ifdef VMPROF_UNIX
64 int vmp_write_time_now(int marker) {
65 char buffer[__SIZE];
66 struct timezone_buf buf;
67
68 (void)memset(&buffer, 0, __SIZE);
69
70 assert((marker == MARKER_TRAILER || marker == MARKER_TIME_N_ZONE) && \
71 "marker must be either a trailer or time_n_zone!");
72
73 struct timeval tv;
74 time_t now;
75 struct tm tm;
76
77
78 /* copy over to the struct */
79 if (gettimeofday(&tv, NULL) != 0) {
80 return -1;
81 }
82 if (time(&now) == (time_t)-1) {
83 return -1;
84 }
85 if (localtime_r(&now, &tm) == NULL) {
86 return -1;
87 }
88 buf.tv_sec = tv.tv_sec;
89 buf.tv_usec = tv.tv_usec;
90 // IF we really support time zones:
91 // use a cross platform datetime library that outputs iso8601 strings
92 // strncpy(((char*)buffer)+__SIZE-8, tm.tm_zone, 8);
93
94 buffer[0] = marker;
95 (void)memcpy(buffer+1, &buf, sizeof(struct timezone_buf));
96 vmp_write_all(buffer, __SIZE);
97 return 0;
98 }
99 #endif
100
101 #ifdef VMPROF_WINDOWS
102 int vmp_write_time_now(int marker) {
103 char buffer[__SIZE];
104 struct timezone_buf buf;
105
106 /**
107 * http://stackoverflow.com/questions/10905892/equivalent-of-gettimeday-for-windows
108 */
109
110 // Note: some broken versions only have 8 trailing zero's, the correct
111 // epoch has 9 trailing zero's
112 static const uint64_t EPOCH = ((uint64_t) 116444736000000000ULL);
113
114 SYSTEMTIME system_time;
115 FILETIME file_time;
116 uint64_t time;
117
118 (void)memset(&buffer, 0, __SIZE);
119
120 assert((marker == MARKER_TRAILER || marker == MARKER_TIME_N_ZONE) && \
121 "marker must be either a trailer or time_n_zone!");
122
123
124 GetSystemTime( &system_time );
125 SystemTimeToFileTime( &system_time, &file_time );
126 time = ((uint64_t)file_time.dwLowDateTime ) ;
127 time += ((uint64_t)file_time.dwHighDateTime) << 32;
128
129 buf.tv_sec = ((time - EPOCH) / 10000000L);
130 buf.tv_usec = (system_time.wMilliseconds * 1000);
131
132 // time zone not implemented on windows
133 // IF we really support time zones:
134 // use a cross platform datetime library that outputs iso8601 strings
135 memset(((char*)buffer)+__SIZE-8, 0, 8);
136
137 buffer[0] = marker;
138 (void)memcpy(buffer+1, &buf, sizeof(struct timezone_buf));
139 vmp_write_all(buffer, __SIZE);
140 return 0;
141 }
142 #endif
143 #undef __SIZE