-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCertainMode.cpp
More file actions
616 lines (450 loc) · 17.5 KB
/
CertainMode.cpp
File metadata and controls
616 lines (450 loc) · 17.5 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#include "std.h"
#include "rope.h"
#include "CertainMode.h"
#include "lock.h"
#include "Message.h"
#include "SpmtThread.h"
#include "RopeVM.h"
#include "interp.h"
#include "Helper.h"
#include "Loggers.h"
#include "DebugScaffold.h"
#include "Snapshot.h"
#include "frame.h"
#include "Stat.h"
using namespace std;
CertainMode::CertainMode()
:
Mode("CERT mode")
{
}
uint32_t
CertainMode::mode_read(uint32_t* addr)
{
return *addr;
}
void
CertainMode::mode_write(uint32_t* addr, uint32_t value)
{
*addr = value;
}
void
CertainMode::step()
{
fetch_and_interpret_an_instruction();
}
// DO NOT forget function 'invoke' in reflect.cpp!!!
void*
CertainMode::do_execute_method(Object* target_object,
MethodBlock* new_mb,
std::vector<uintptr_t>& jargs, DummyFrame* dummy)
{
assert(frame); // see also: initialiseJavaStack
// save (pc, frame, sp) of outer drive_loop
CodePntr old_pc = pc;
Frame* old_frame = frame;
uintptr_t* old_sp = sp;
// dummy frame is used to receive RV of top_frame
// dummy->prev is current frame
//Frame* dummy = create_dummy_frame(frame);
dummy->prev = frame;
void *ret;
ret = dummy->ostack_base;
//????frame->last_pc = pc;
assert(target_object);
SpmtThread* target_st = target_object->get_st();
assert(target_st->get_thread() == g_get_current_thread());
MINILOG(top_method_logger, "#" << m_st->m_id
<< " top-invoke " << "#" << target_st->m_id << " " << new_mb);
if (target_st == m_st or new_mb->is_rope_invoker_execute()) {
// 对top method的调用是从native代码中发出,所以caller的pc设为0
// dummy frame作为顶级方法的上级,用来接收top frame的返回值
invoke_impl(target_object, new_mb, &jargs[0],
m_st, 0, dummy, dummy->ostack_base, true);
}
else {
MINILOG(inter_st_logger, "#" << m_st->m_id
<< " inter-invoke(top) " << "#" << target_st->m_id << " " << new_mb);
// 构造确定性的invoke_msg发送给目标线程
InvokeMsg* invoke_msg = new InvokeMsg(m_st, target_st,
target_object, new_mb, &jargs[0],
0, dummy, dummy->ostack_base,
true);
//frame->last_pc = pc;
m_st->send_msg(invoke_msg);
// 虽然进入了推测模式,但没有异步消息,推测执行无法继续。注意,
//因为对本条指令的解释过程还没结束,使不能推测执行该指令后边的
//指令的。
m_st->m_spec_mode.pc = 0;
m_st->m_spec_mode.frame = 0;
m_st->m_spec_mode.sp = 0;
m_st->halt(RunningState::halt_no_asyn_msg);
}
// 进入内层drive_loop
m_st->drive_loop();
// 已从内层drive_loop退出
//assert(m_st->m_mode->is_certain_mode());
// restore (pc, frame, sp) of outer drive_loop
pc = old_pc;
frame = old_frame;
sp = old_sp;
return ret;
}
void
CertainMode::do_invoke_method(Object* target_object, MethodBlock* new_mb)
{
assert(target_object);
STAT_CODE\
(
// if (debug_scaffold::is_client_code
// //and is_app_obj(mb->classobj)) {
// ) {
// RopeVM::instance()->m_certain_instr_count++;
// }
) // STAT_CODE
if (RopeVM::graph_enabled and is_client_code) {
Object* s = frame->get_object();
Object* t = target_object;
if (s != t and is_app_obj(s) and is_app_obj(t)) {
ofs_graph << type_name(s) << " " << s << " => " << type_name(t) << " " << t << endl;
}
}
if (RopeVM::event_enabled and is_client_code) {
Object* s = frame->get_object();
Object* t = target_object;
if (s != t and is_app_obj(s) and is_app_obj(t)) {
ofs_event << type_name(s) << " " << s << " => " << type_name(t) << " " << t << " " << op_no
<< " " << frame->frame_no << " " << frame->prev->frame_no
<< " " << frame->mb->name << " " << new_mb->name
<< " " << mapPC2LineNo(frame->mb, pc)
<< endl;
op_no++;
}
}
MINILOG_IF((m_st->m_id == 0 or m_st->m_id >= 5), order_logger, "invoke " << new_mb);
frame->last_pc = pc;
SpmtThread* target_st = target_object->get_st();
assert(target_st->m_thread == m_st->m_thread);
if (target_st == m_st or new_mb->is_rope_invoker_execute()) {
sp -= new_mb->args_count;
invoke_impl(target_object, new_mb, sp,
m_st, pc, frame, sp,
false);
}
else {
MINILOG(inter_st_logger, "#" << m_st->m_id
<< " inter-invoke " << "#" << target_st->m_id << " " << new_mb);
// pop up arguments
sp -= new_mb->args_count;
// 构造确定性的invoke_msg发送给目标线程
InvokeMsg* invoke_msg = new InvokeMsg(m_st, target_st,
target_object, new_mb, sp,
pc, frame, sp);
m_st->send_msg(invoke_msg);
if (RopeVM::model < 3) // 模型3以上才支持推测执行。
return;
MethodBlock* rvp_method = get_rvp_method(new_mb);
if (rvp_method == nullptr) { // 若无rvp方法就不推测执行了,停机。
MINILOG(rvp_logger, "#" << m_st->m_id
<< "no rvp-method for " << new_mb);
m_st->halt(RunningState::halt_no_syn_msg);
return;
}
// MINILOG0("#" << m_st->m_id
// << " (C)invokes rvp-method: " << *rvp_method);
m_st->m_rvp_mode.invoke_impl(target_object,
rvp_method,
sp,
m_st,
pc, frame, sp,
false);
m_st->switch_to_rvp_mode();
}
}
void
CertainMode::do_method_return(int len)
{
assert(len == 0 || len == 1 || len == 2);
// if (RopeVM::graph_enabled and is_client_code) {
// Object* s = frame->get_object();
// Object* t = target_object;
// if (s != t and is_app_obj(s) and is_app_obj(t)) {
// ofs_graph << type_name(s) << " " << s << " => " << type_name(t) << " " << t << endl;
// }
// }
Frame* current_frame = frame; // 因为后边的处理可能会改变frame,所以我们先保存一下
MINILOG_IF((m_st->m_id == 0 or m_st->m_id >= 5), order_logger, "return " << current_frame->mb);
// 给同步方法解锁
if (current_frame->mb->is_synchronized()) {
assert(current_frame->get_object() == current_frame->mb->classobj
or current_frame->get_object() == (Object*)current_frame->lvars[0]);
Object* sync_ob = current_frame->get_object();
objectUnlock(sync_ob);
}
SpmtThread* target_st = current_frame->caller;
assert(target_st->m_thread == m_st->m_thread);
MINILOG_IF(current_frame->is_top_frame(), top_method_logger, "#" << m_st->m_id
<< " top-return " << "#" << target_st->m_id << " " << current_frame->mb);
/*
if 目标线程是当前线程
返回值写入上级frame的ostack(if 是从top frame返回,上级frame为dummy frame,并结束drive_loop)
else
构造return确定消息(if 是从top frame返回,则带有top标志)
*/
if (target_st == m_st) {
uintptr_t* rv = sp - len; // 返回值在ostack中占用了几个slot,我们让rv指向其起始位置
sp -= len;
// 将返回值写入主调方法的ostack
uintptr_t* caller_sp = current_frame->caller_sp;
for (int i = 0; i < len; ++i) {
*caller_sp++ = rv[i];
}
if (current_frame->is_top_frame()) {
// 因为推测模式下从顶级方法返回时,推测执行暂停,所以当确定模式下返回时要恢复推测执行为前进状态。
m_st->m_spec_running_state = RunningState::ongoing;
m_st->signal_quit_drive_loop();
}
else { // top frame之上是无代码的dummy frame,这些对于dummy frame都无意义。
pc = current_frame->caller_pc;
frame = current_frame->prev;
sp = caller_sp;
pc += (*pc == OPC_INVOKEINTERFACE_QUICK ? 5 : 3);
}
pop_frame(current_frame);
}
else {
MINILOG(inter_st_logger, "#" << m_st->m_id
<< " inter-return" << (current_frame->is_top_frame()? "(top) " : " ") << "#" << target_st->m_id << " " << current_frame->mb);
uintptr_t* rv = sp - len;
sp -= len;
assert(not current_frame->is_top_frame() or current_frame->caller_pc == 0);
// 构造确定性的return_msg发送给目标线程
ReturnMsg* return_msg = new ReturnMsg(target_st,
rv, len,
current_frame->caller_pc,
current_frame->prev,
current_frame->caller_sp,
current_frame->is_top_frame());
pop_frame(current_frame);
m_st->send_msg(return_msg);
m_st->m_spec_running_state = RunningState::ongoing_but_need_launch_new_msg;
m_st->m_spec_mode.reset_context();
}
}
void
CertainMode::before_signal_exception(Class *exception_class)
{
MINILOG(c_exception_logger, "#" << m_st->id() << " (C) before signal exception "
<< exception_class->name() << " in: " << info(frame));
// do nothing
}
Frame*
CertainMode::push_frame(Object* object, MethodBlock* new_mb, uintptr_t* args,
SpmtThread* caller, CodePntr caller_pc, Frame* caller_frame, uintptr_t* caller_sp,
bool is_top)
{
Frame* new_frame = g_create_frame(m_st, object, new_mb, args,
caller, caller_pc, caller_frame, caller_sp,
is_top);
return new_frame;
}
void
CertainMode::pop_frame(Frame* frame)
{
MINILOG_IF(is_client_code && is_app_obj(frame->mb->classobj),
c_pop_frame_logger, "#" << m_st->id()
<< " (C) destroy frame " << frame);
g_destroy_frame(frame);
}
/*
before and after do_get_field
getfield
_____ _____
| obj | | val |
| |<--sp => | |<--sp
| | | |
getstatic
_____ _____
| |<--sp | val |
| | => | |<--sp
| | | |
*/
void
CertainMode::do_get_field(Object* target_object, FieldBlock* fb,
uintptr_t* addr, int size, bool is_static)
{
assert(size == 1 || size == 2);
MINILOG_IF((m_st->m_id == 0 or m_st->m_id >= 5), order_logger, "get " << fb);
if (RopeVM::graph_enabled and is_client_code) {
Object* s = frame->get_object();
Object* t = target_object;
if (s != t and is_app_obj(s) and is_app_obj(t)) {
ofs_graph << type_name(s) << " " << s << " => " << type_name(t) << " " << t << endl;
}
}
if (RopeVM::support_self_read) { // 若支持自行read,就无所谓目标对象是否由本线程负责了。
sp -= is_static ? 0 : 1;
for (int i = 0; i < size; ++i) {
write(sp, read(addr + i));
sp++;
}
pc += 3;
return;
}
SpmtThread* target_st = target_object->get_st();
assert(target_st->m_thread == m_st->m_thread);
if (target_st == m_st) {
sp -= is_static ? 0 : 1;
for (int i = 0; i < size; ++i) {
write(sp, read(addr + i));
sp++;
}
pc += 3;
}
else {
sp -= is_static ? 0 : 1;
// 构造确定性的get_msg发送给目标线程
GetMsg* get_msg = new GetMsg(m_st, target_st, target_object, fb);
m_st->halt(RunningState::halt_worthless_to_execute);
m_st->send_msg(get_msg);
}
}
/*
before and after do_put_field
putfield
_____ _____
| obj | | obj |<--sp
| val | => | val |
| |<--sp | |
putstatic
_____ _____
| val | | val |<--sp
| |<--sp => | |
| | | |
*/
// size表示占几个ostack位置。
// 就算字段的类型大小只有一个字节,但在存储时,总是有四个或八个字节的位置保留给它。数组则不然,数组是紧致存储的。
void
CertainMode::do_put_field(Object* target_object, FieldBlock* fb,
uintptr_t* addr, int size, bool is_static)
{
assert(size == 1 || size == 2);
MINILOG_IF((m_st->m_id == 0 or m_st->m_id >= 5), order_logger, "put " << fb);
if (RopeVM::graph_enabled and is_client_code) {
Object* s = frame->get_object();
Object* t = target_object;
if (s != t and is_app_obj(s) and is_app_obj(t)) {
ofs_graph << type_name(s) << " " << s << " => " << type_name(t) << " " << t << endl;
}
}
SpmtThread* target_st = target_object->get_st();
assert(target_st->m_thread == m_st->m_thread);
if (target_st == m_st) {
sp -= size;
for (int i = 0; i < size; ++i) {
write(addr + i, read(sp + i));
}
sp -= is_static ? 0 : 1;
pc += 3;
}
else {
sp -= size;
// 构造确定性的put_msg发送给目标线程
PutMsg* put_msg = new PutMsg(m_st, target_st, target_object, fb, sp);
sp -= is_static ? 0 : 1;
m_st->halt(RunningState::halt_worthless_to_execute);
m_st->send_msg(put_msg);
}
}
void
CertainMode::do_array_load(Object* array, int index, int type_size)
{
MINILOG_IF((m_st->m_id == 0 or m_st->m_id >= 5), order_logger, "arrayload " << index << ", " << type_size);
Object* target_object = array;
SpmtThread* target_st = target_object->get_st();
assert(target_st->m_thread == m_st->m_thread);
int nslots = type_size > 4 ? 2 : 1; // number of slots for value
if (RopeVM::support_self_read) { // 若支持自行read,就无所谓目标对象是否由本线程负责了。
sp -= 2; // pop up arrayref and index
load_from_array(sp, array, index, type_size);
sp += nslots;
pc += 1;
return;
}
if (target_st == m_st) {
sp -= 2; // pop up arrayref and index
load_from_array(sp, array, index, type_size);
sp += nslots;
pc += 1;
}
else {
sp -= 2; // pop up arrayref and index
// 构造确定性的aload_msg发送给目标线程
ALoadMsg* aload_msg = new ALoadMsg(m_st, target_st,
array, type_size, index);
m_st->halt(RunningState::halt_worthless_to_execute);
m_st->send_msg(aload_msg);
}
}
/*
type_size 表示元素类型的大小,单位字节。
nslots 表示占几个ostack的位置,一个位置四个字节。
就算数组元素的类型大小只有一个字节,但读到ostack中之后,仍然是占一个ostack位置,即四个字节。
但数组是紧致存储的。
*/
void
CertainMode::do_array_store(Object* array, int index, int type_size)
{
MINILOG_IF((m_st->m_id == 0 or m_st->m_id >= 5), order_logger, "arraystore " << index << ", " << type_size);
Object* target_object = array;
SpmtThread* target_st = target_object->get_st();
assert(target_st->m_thread == m_st->m_thread);
//void* addr = array_elem_addr(array, index, type_size);
int nslots = size2nslots(type_size); // number of slots for value
if (target_st == m_st) {
sp -= nslots;
store_to_array(sp, array, index, type_size);
sp -= 2; // pop up arrayref and index
pc += 1;
}
else {
sp -= nslots;
// 构造确定性的astore_msg发送给目标线程
AStoreMsg* astore_msg = new AStoreMsg(m_st, target_st,
array,
type_size, index, sp);
sp -= 2; // pop up arrayref and index
m_st->halt(RunningState::halt_worthless_to_execute);
m_st->send_msg(astore_msg);
}
}
/*
转交确定模式,必须自己先放弃确定模式,然后再发出确定消息。如果你先发
出确定消息,然后才放弃确定模式,在多os线程实现方式下,这之间其他spmt
线程可能会运行,检测到确定消息从而进入确定模式,从而导致出现两个具有
确定模式的spmt线程。
*/
void
CertainMode::send_msg(Message* msg)
{
assert(RopeVM::model >= 2); // 模型2以上才有消息
if (RopeVM::model < 3) // 模型2失去确定控制后停机,模型3、模型4则不。
m_st->halt(RunningState::halt_model2_requirement);
MINILOG(certain_msg_logger, "#" << m_st->id()
<< " send cert msg to "
<< "#" << msg->get_target_st()->id()
<< " " << msg);
MINILOG(control_transfer_logger, "#" << m_st->id()
<< " transfer control to "
<< "#" << msg->get_target_st()->id());
m_st->switch_to_speculative_mode();
m_st->start_afresh_spec_execution();
msg->get_target_st()->set_certain_msg(msg);
}
void
CertainMode::do_spec_barrier()
{
MINILOG(spec_barrier_logger, "#" << m_st->id()
<< " pass spec barrier");
pc += 3; // 越过推测路障
}