-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRopeVM.cpp
294 lines (234 loc) · 5.96 KB
/
RopeVM.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include "std.h"
#include "rope.h"
#include "RopeVM.h"
#include "Helper.h"
#include "thread.h"
#include "SpmtThread.h"
using namespace std;
RopeVM* RopeVM::m_instance = 0;
int RopeVM::m_next_id = 0;
RopeVM*
RopeVM::instance()
{
if (m_instance == 0) {
m_instance = new RopeVM;
}
return m_instance;
}
RopeVM::RopeVM()
:
m_logger_enabled(true)//,
//m_logger_enabled_backdoor(true)
{
pthread_mutex_init(&m_lock, 0);
// stat
//m_count_control_transfer = 0;
}
// RopeVM::~RopeVM()
// {
// // std::cout << "total cores: " << m_cores.size() << std::endl;
// for (vector<SpmtThread*>::iterator i = m_cores.begin(); i != m_cores.end(); ++i) {
// delete (*i);
// *i = 0;
// }
// }
SpmtThread*
RopeVM::new_spmt_thread()
{
SpmtThread* st = 0;
pthread_mutex_lock(&m_lock);
if (m_sts.size() < 1000) {
st = new SpmtThread(get_next_id());
m_sts.push_back(st);
}
else {
cout << "ROPEVM: too many spmt threads" << endl;
assert(false);
}
pthread_mutex_unlock(&m_lock);
return st;
}
SpmtThread*
RopeVM::create_spmt_thread()
{
SpmtThread* st = new_spmt_thread();
// 为st对象配上os线程(S_threadStart中调用st->drive_loop)
// 但目前是单os线程实现方式,让一个spmt线程运行drive_loop的时候带动其他spmt线程。
//os_api_create_thread(SpmtThread::S_threadStart, st);
return st;
}
bool RopeVM::probe_enabled;
bool RopeVM::graph_enabled;
bool RopeVM::event_enabled;
int RopeVM::model;
bool RopeVM::support_invoker_execute;
bool RopeVM::support_irrevocable;
bool RopeVM::support_spec_safe_native;
bool RopeVM::support_spec_barrier;
bool RopeVM::support_self_read;
void initialiseJvm(InitArgs *args)
{
if (args->do_log) {
RopeVM::instance()->turn_on_log();
}
else {
RopeVM::instance()->turn_off_log();
}
if (args->do_probe) {
RopeVM::instance()->turn_on_probe();
}
else {
RopeVM::instance()->turn_off_probe();
}
if (args->do_graph) {
RopeVM::instance()->turn_on_graph();
}
else {
RopeVM::instance()->turn_off_graph();
}
if (args->do_event) {
RopeVM::instance()->turn_on_event();
}
else {
RopeVM::instance()->turn_off_event();
}
RopeVM::model = args->model;
// 对象协作图只能在串行模式下生成
if (args->do_graph) {
RopeVM::model = 1;
}
// 事件历史只能在串行模式下生成
if (args->do_event) {
RopeVM::model = 1;
}
if (RopeVM::model < 3) // 模型3以下,不支持这些特征。其值皆为false。
return;
RopeVM::support_invoker_execute = args->support_invoker_execute;
RopeVM::support_irrevocable = args->support_irrevocable;
RopeVM::support_spec_safe_native = args->support_spec_safe_native;
RopeVM::support_spec_barrier = args->support_spec_barrier;
RopeVM::support_self_read = args->support_self_read;
}
void
RopeVM::report_stat(std::ostream& os)
{
//Statistic::instance()->report_stat(os);
// os << "JVM" << '\t' << "spmt thread count" << '\t' << m_sts.size()-4 << '\n';
// os << "JVM" << '\t' << "control transfer" << '\t' << m_count_control_transfer << '\n';
for (vector<SpmtThread*>::iterator i = m_sts.begin(); i != m_sts.end(); ++i) {
SpmtThread* st = *i;
if (1 <= st->id() && st->id() <= 4)
continue;
st->report_stat(os);
}
}
void
RopeVM::adjust_log_state()
{
// 由两个变量共同控制,只要有一个为false,就不产生日志。
// MiniLogger::disable_all_loggers = not (m_logger_enabled and
// m_logger_enabled_backdoor);
MiniLogger::disable_all_loggers = not m_logger_enabled;
}
void
RopeVM::turn_on_log()
{
m_logger_enabled = true;
adjust_log_state();
}
void
RopeVM::turn_off_log()
{
m_logger_enabled = false;
adjust_log_state();
}
// void
// RopeVM::turn_on_log_backdoor()
// {
// m_logger_enabled_backdoor = true;
// adjust_log_state();
// }
// void
// RopeVM::turn_off_log_backdoor()
// {
// m_logger_enabled_backdoor = false;
// adjust_log_state();
// }
void
RopeVM::turn_on_probe()
{
probe_enabled = true;
}
void
RopeVM::turn_off_graph()
{
graph_enabled = false;
}
void
RopeVM::turn_on_graph()
{
graph_enabled = true;
}
void
RopeVM::turn_off_event()
{
event_enabled = false;
}
void
RopeVM::turn_on_event()
{
event_enabled = true;
}
void
RopeVM::turn_off_probe()
{
probe_enabled = false;
}
namespace {
template <typename T>
void
print_entry(const char* name, T val)
{
cout << setw(25) << right << name << " " << val << "\n";
}
}
// 变量名跟变量的显示名相同
#define PRINT_ENTRY(name) print_entry(#name, name)
void
RopeVM::dump_rope_params()
{
// 注意:以support_打头的变量值未必等于同名的环境变量的值,它们的取值还与model的取值有关。
// 参考函数initialiseJvm
cout << "\n" << setw(40) << setfill('=') << "\n" << setfill(' ');
PRINT_ENTRY(model);
print_entry("log", m_logger_enabled);
PRINT_ENTRY(support_invoker_execute);
PRINT_ENTRY(support_irrevocable);
PRINT_ENTRY(support_spec_safe_native);
PRINT_ENTRY(support_spec_barrier);
PRINT_ENTRY(support_self_read);
}
void
RopeVM::output_summary()
{
dump_rope_params();
cout<< "\n";
cout << "statistic:\n";
cout << "cycles: " << m_certain_instr_count << endl;
}
// 只统计程序本身的代码,而不统计那庞大但未经并行化的标准库代码
bool
g_should_enable_probe(MethodBlock* mb)
{
if (not RopeVM::probe_enabled)
return false;
if (not is_client_code)
return false;
// if (mb and not is_app_obj(mb->classobj))
// return false;
return true;
}
std::ofstream ofs_graph("graph.txt");
std::ofstream ofs_timeline("timeline.txt");
std::ofstream ofs_event("event.txt");
std::ofstream ofs_ref_name("ref_name.txt");