1
1
// This file is a part of Julia. License is MIT: https://julialang.org/license
2
2
3
+ #include < cstdint>
4
+ #include < pthread.h>
3
5
#include < string>
4
6
#include < fstream>
5
7
#include < map>
15
17
16
18
using namespace llvm ;
17
19
18
- static int codegen_imaging_mode (void )
20
+ static int codegen_imaging_mode (void ) JL_NOTSAFEPOINT
19
21
{
20
22
return jl_options.image_codegen || (jl_generating_output () && jl_options.use_pkgimages );
21
23
}
@@ -26,7 +28,9 @@ const int logdata_blocksize = 32; // target getting nearby lines in the same gen
26
28
typedef uint64_t logdata_block[logdata_blocksize];
27
29
typedef StringMap< SmallVector<logdata_block*, 0 > > logdata_t ;
28
30
29
- static uint64_t *allocLine (SmallVector<logdata_block*, 0 > &vec, int line)
31
+ pthread_mutex_t coverage_lock = PTHREAD_MUTEX_INITIALIZER;
32
+
33
+ static uint64_t *allocLine (SmallVector<logdata_block*, 0 > &vec, int line) JL_NOTSAFEPOINT
30
34
{
31
35
unsigned block = line / logdata_blocksize;
32
36
line = line % logdata_blocksize;
@@ -45,39 +49,49 @@ static uint64_t *allocLine(SmallVector<logdata_block*, 0> &vec, int line)
45
49
46
50
static logdata_t coverageData;
47
51
48
- JL_DLLEXPORT void jl_coverage_alloc_line (StringRef filename, int line)
52
+ JL_DLLEXPORT void jl_coverage_alloc_line (StringRef filename, int line) JL_NOTSAFEPOINT
49
53
{
50
54
assert (!codegen_imaging_mode ());
51
55
if (filename == " " || filename == " none" || filename == " no file" || filename == " <missing>" || line < 0 )
52
56
return ;
57
+ pthread_mutex_lock (&coverage_lock);
53
58
allocLine (coverageData[filename], line);
59
+ pthread_mutex_unlock (&coverage_lock);
54
60
}
55
61
56
- JL_DLLEXPORT uint64_t *jl_coverage_data_pointer (StringRef filename, int line)
62
+ JL_DLLEXPORT uint64_t *jl_coverage_data_pointer (StringRef filename, int line) JL_NOTSAFEPOINT
57
63
{
58
- return allocLine (coverageData[filename], line);
64
+ pthread_mutex_lock (&coverage_lock);
65
+ uint64_t * ret = allocLine (coverageData[filename], line);
66
+ pthread_mutex_unlock (&coverage_lock);
67
+ return ret;
59
68
}
60
69
61
- extern " C" JL_DLLEXPORT void jl_coverage_visit_line (const char *filename_, size_t len_filename, int line)
70
+ extern " C" JL_DLLEXPORT void jl_coverage_visit_line (const char *filename_, size_t len_filename, int line) JL_NOTSAFEPOINT
62
71
{
63
72
StringRef filename = StringRef (filename_, len_filename);
64
73
if (codegen_imaging_mode () || filename == " " || filename == " none" || filename == " no file" || filename == " <missing>" || line < 0 )
65
74
return ;
75
+ pthread_mutex_lock (&coverage_lock);
66
76
SmallVector<logdata_block*, 0 > &vec = coverageData[filename];
67
77
uint64_t *ptr = allocLine (vec, line);
68
78
(*ptr)++;
79
+ pthread_mutex_unlock (&coverage_lock);
69
80
}
70
81
71
82
// Memory allocation log (malloc_log)
72
83
73
84
static logdata_t mallocData;
74
85
75
- JL_DLLEXPORT uint64_t *jl_malloc_data_pointer (StringRef filename, int line)
86
+ JL_DLLEXPORT uint64_t *jl_malloc_data_pointer (StringRef filename, int line) JL_NOTSAFEPOINT
76
87
{
77
- return allocLine (mallocData[filename], line);
88
+ pthread_mutex_lock (&coverage_lock);
89
+ uint64_t * ret = allocLine (mallocData[filename], line);
90
+ pthread_mutex_unlock (&coverage_lock);
91
+ return ret;
78
92
}
79
93
80
- static void clear_log_data (logdata_t &logData, int resetValue)
94
+ static void clear_log_data (logdata_t &logData, int resetValue) JL_NOTSAFEPOINT
81
95
{
82
96
logdata_t ::iterator it = logData.begin ();
83
97
for (; it != logData.end (); it++) {
@@ -97,18 +111,22 @@ static void clear_log_data(logdata_t &logData, int resetValue)
97
111
}
98
112
99
113
// Resets the malloc counts.
100
- extern " C" JL_DLLEXPORT void jl_clear_malloc_data (void )
114
+ extern " C" JL_DLLEXPORT void jl_clear_malloc_data (void ) JL_NOTSAFEPOINT
101
115
{
116
+ pthread_mutex_lock (&coverage_lock);
102
117
clear_log_data (mallocData, 1 );
118
+ pthread_mutex_unlock (&coverage_lock);
103
119
}
104
120
105
121
// Resets the code coverage
106
- extern " C" JL_DLLEXPORT void jl_clear_coverage_data (void )
122
+ extern " C" JL_DLLEXPORT void jl_clear_coverage_data (void ) JL_NOTSAFEPOINT
107
123
{
124
+ pthread_mutex_lock (&coverage_lock);
108
125
clear_log_data (coverageData, 0 );
126
+ pthread_mutex_unlock (&coverage_lock);
109
127
}
110
128
111
- static void write_log_data (logdata_t &logData, const char *extension)
129
+ static void write_log_data (logdata_t &logData, const char *extension) JL_NOTSAFEPOINT
112
130
{
113
131
std::string base = std::string (jl_options.julia_bindir );
114
132
base = base + " /../share/julia/base/" ;
@@ -163,7 +181,7 @@ static void write_log_data(logdata_t &logData, const char *extension)
163
181
}
164
182
}
165
183
166
- static void write_lcov_data (logdata_t &logData, const std::string &outfile)
184
+ static void write_lcov_data (logdata_t &logData, const std::string &outfile) JL_NOTSAFEPOINT
167
185
{
168
186
std::ofstream outf (outfile.c_str (), std::ofstream::ate | std::ofstream::out | std::ofstream::binary);
169
187
// std::string base = std::string(jl_options.julia_bindir);
@@ -203,8 +221,9 @@ static void write_lcov_data(logdata_t &logData, const std::string &outfile)
203
221
outf.close ();
204
222
}
205
223
206
- extern " C" JL_DLLEXPORT void jl_write_coverage_data (const char *output)
224
+ extern " C" JL_DLLEXPORT void jl_write_coverage_data (const char *output) JL_NOTSAFEPOINT
207
225
{
226
+ pthread_mutex_lock (&coverage_lock);
208
227
if (output) {
209
228
StringRef output_pattern (output);
210
229
if (output_pattern.ends_with (" .info" ))
@@ -215,11 +234,14 @@ extern "C" JL_DLLEXPORT void jl_write_coverage_data(const char *output)
215
234
raw_string_ostream (stm) << " ." << uv_os_getpid () << " .cov" ;
216
235
write_log_data (coverageData, stm.c_str ());
217
236
}
237
+ pthread_mutex_unlock (&coverage_lock);
218
238
}
219
239
220
- extern " C" void jl_write_malloc_log (void )
240
+ extern " C" void jl_write_malloc_log (void ) JL_NOTSAFEPOINT
221
241
{
242
+ pthread_mutex_lock (&coverage_lock);
222
243
std::string stm;
223
244
raw_string_ostream (stm) << " ." << uv_os_getpid () << " .mem" ;
224
245
write_log_data (mallocData, stm.c_str ());
246
+ pthread_mutex_unlock (&coverage_lock);
225
247
}
0 commit comments