Debian Codegraph @

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

raw symbols SCIP
1 #include "vmprof_mt.h"
2 /* Support for multithreaded write() operations (implementation) */
3
4 #include <assert.h>
5
6 #if defined(__i386__) || defined(__amd64__)
7 static inline void write_fence(void) { asm("" : : : "memory"); }
8 #else
9 static inline void write_fence(void) { __sync_synchronize(); }
10 #endif
11
12 static char volatile profbuf_state[MAX_NUM_BUFFERS];
13 static struct profbuf_s *profbuf_all_buffers = NULL;
14 static int volatile profbuf_write_lock = 2;
15 static long profbuf_pending_write;
16
17
18 static void unprepare_concurrent_bufs(void)
19 {
20 if (profbuf_all_buffers != NULL) {
21 munmap(profbuf_all_buffers, sizeof(struct profbuf_s) * MAX_NUM_BUFFERS);
22 profbuf_all_buffers = NULL;
23 }
24 }
25
26 int prepare_concurrent_bufs(void)
27 {
28 assert(sizeof(struct profbuf_s) == 8192);
29
30 unprepare_concurrent_bufs();
31 profbuf_all_buffers = mmap(NULL, sizeof(struct profbuf_s) * MAX_NUM_BUFFERS,
32 PROT_READ | PROT_WRITE,
33 MAP_PRIVATE | MAP_ANONYMOUS,
34 -1, 0);
35 if (profbuf_all_buffers == MAP_FAILED) {
36 profbuf_all_buffers = NULL;
37 return -1;
38 }
39 memset((char *)profbuf_state, PROFBUF_UNUSED, sizeof(profbuf_state));
40 profbuf_write_lock = 0;
41 profbuf_pending_write = -1;
42 return 0;
43 }
44
45 static int _write_single_ready_buffer(int fd, long i)
46 {
47 /* Try to write to disk the buffer number 'i'. This function must
48 only be called while we hold the write lock. */
49 assert(profbuf_write_lock != 0);
50
51 if (profbuf_pending_write >= 0) {
52 /* A partially written buffer is waiting. We'll write the
53 rest of this buffer now, instead of 'i'. */
54 i = profbuf_pending_write;
55 assert(profbuf_state[i] == PROFBUF_READY);
56 }
57
58 if (profbuf_state[i] != PROFBUF_READY) {
59 /* this used to be a race condition: the buffer was written by a
60 different thread already, nothing to do now */
61 return 0;
62 }
63
64 int err;
65 struct profbuf_s *p = &profbuf_all_buffers[i];
66 ssize_t count = write(fd, p->data + p->data_offset, p->data_size);
67 if (count == p->data_size) {
68 profbuf_state[i] = PROFBUF_UNUSED;
69 profbuf_pending_write = -1;
70 }
71 else {
72 if (count > 0) {
73 p->data_offset += count;
74 p->data_size -= count;
75 }
76 profbuf_pending_write = i;
77 if (count < 0)
78 return -1;
79 }
80 return 0;
81 }
82
83 static void _write_ready_buffers(int fd)
84 {
85 long i;
86 int has_write_lock = 0;
87
88 for (i = 0; i < MAX_NUM_BUFFERS; i++) {
89 if (profbuf_state[i] == PROFBUF_READY) {
90 if (!has_write_lock) {
91 if (!__sync_bool_compare_and_swap(&profbuf_write_lock, 0, 1))
92 return; /* can't acquire the write lock, give up */
93 has_write_lock = 1;
94 }
95 if (_write_single_ready_buffer(fd, i) < 0)
96 break;
97 }
98 }
99 if (has_write_lock)
100 profbuf_write_lock = 0;
101 }
102
103 struct profbuf_s *reserve_buffer(int fd)
104 {
105 /* Tries to enter a region of code that fills one buffer. If
106 successful, returns the profbuf_s. It fails only if the
107 concurrent buffers are all busy (extreme multithreaded usage).
108
109 This might call write() to emit the data sitting in
110 previously-prepared buffers. In case of write() error, the
111 error is ignored but unwritten data stays in the buffers.
112 */
113 long i;
114
115 _write_ready_buffers(fd);
116
117 for (i = 0; i < MAX_NUM_BUFFERS; i++) {
118 if (profbuf_state[i] == PROFBUF_UNUSED &&
119 __sync_bool_compare_and_swap(&profbuf_state[i], PROFBUF_UNUSED,
120 PROFBUF_FILLING)) {
121 struct profbuf_s *p = &profbuf_all_buffers[i];
122 p->data_size = 0;
123 p->data_offset = 0;
124 return p;
125 }
126 }
127 /* no unused buffer found */
128 return NULL;
129 }
130
131 void commit_buffer(int fd, struct profbuf_s *buf)
132 {
133 /* Leaves a region of code that filled 'buf'.
134
135 This might call write() to emit the data now ready. In case of
136 write() error, the error is ignored but unwritten data stays in
137 the buffers.
138 */
139
140 /* Make sure every thread sees the full content of 'buf' */
141 write_fence();
142
143 /* Then set the 'ready' flag */
144 long i = buf - profbuf_all_buffers;
145 assert(profbuf_state[i] == PROFBUF_FILLING);
146 profbuf_state[i] = PROFBUF_READY;
147
148 if (!__sync_bool_compare_and_swap(&profbuf_write_lock, 0, 1)) {
149 /* can't acquire the write lock, ignore */
150 }
151 else {
152 _write_single_ready_buffer(fd, i);
153 profbuf_write_lock = 0;
154 }
155 }
156
157 void cancel_buffer(struct profbuf_s *buf)
158 {
159 long i = buf - profbuf_all_buffers;
160 assert(profbuf_state[i] == PROFBUF_FILLING);
161 profbuf_state[i] = PROFBUF_UNUSED;
162 }
163
164 int shutdown_concurrent_bufs(int fd)
165 {
166 /* no signal handler can be running concurrently here, because we
167 already did vmprof_ignore_signals(1) */
168 assert(profbuf_write_lock == 0);
169 profbuf_write_lock = 2;
170
171 /* last attempt to flush buffers */
172 int i;
173 for (i = 0; i < MAX_NUM_BUFFERS; i++) {
174 while (profbuf_state[i] == PROFBUF_READY) {
175 if (_write_single_ready_buffer(fd, i) < 0)
176 return -1;
177 }
178 }
179 unprepare_concurrent_bufs();
180 return 0;
181 }