-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathtw-kp.c
More file actions
235 lines (195 loc) · 5.4 KB
/
Copy pathtw-kp.c
File metadata and controls
235 lines (195 loc) · 5.4 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
#include <ross.h>
void
tw_kp_onpe(tw_kpid id, tw_pe * pe)
{
if(id >= g_tw_nkp)
tw_error(TW_LOC, "ID %d exceeded MAX KPs", id);
if(g_tw_kp[id])
tw_error(TW_LOC, "KP already allocated: %lld\n", id);
g_tw_kp[id] = (tw_kp *) tw_calloc(TW_LOC, "Local KP", sizeof(tw_kp), 1);
g_tw_kp[id]->id = id;
g_tw_kp[id]->pe = pe;
#ifdef ROSS_QUEUE_kp_splay
g_tw_kp[id]->pq = tw_eventpq_create();
#endif
}
#ifdef USE_RAND_TIEBREAKER
void
tw_kp_rollback_to_sig(tw_kp * kp, tw_event_sig const * to_sig)
{
tw_event *e;
tw_clock pq_start;
kp->s_rb_total++;
kp->kp_stats->s_rb_total++;
while (kp->pevent_q.size && tw_event_sig_compare_ptr(&kp->pevent_q.head->sig, to_sig) >= 0)
{
e = tw_eventq_shift(&kp->pevent_q);
// rollback first
tw_event_rollback(e);
// reset kp pointers
if (kp->pevent_q.size == 0)
{
// kp->last_time = kp->pe->GVT;
tw_copy_event_sig(&kp->last_sig, &kp->pe->GVT_sig);
} else
{
// kp->last_time = kp->pevent_q.head->recv_ts;
tw_copy_event_sig(&kp->last_sig, &kp->pevent_q.head->sig);
}
// place event back into priority queue
pq_start = tw_clock_read();
tw_pq_enqueue(kp->pe->pq, e);
kp->pe->stats.s_pq += tw_clock_read() - pq_start;
}
}
#else
void
tw_kp_rollback_to(tw_kp * kp, tw_stime to)
{
tw_event *e;
tw_clock pq_start;
kp->s_rb_total++;
// instrumentation
kp->kp_stats->s_rb_total++;
#if VERIFY_ROLLBACK
printf("%d %d: rb_to %f, now = %f \n",
kp->pe->id, kp->id, TW_STIME_DBL(to), TW_STIME_DBL(kp->last_time));
#endif
while(kp->pevent_q.size && TW_STIME_CMP(kp->pevent_q.head->recv_ts, to) >= 0)
{
e = tw_eventq_shift(&kp->pevent_q);
/*
* rollback first
*/
tw_event_rollback(e);
/*
* reset kp pointers
*/
if (kp->pevent_q.size == 0)
{
kp->last_time = kp->pe->GVT;
} else
{
kp->last_time = kp->pevent_q.head->recv_ts;
}
/*
* place event back into priority queue
*/
pq_start = tw_clock_read();
tw_pq_enqueue(kp->pe->pq, e);
kp->pe->stats.s_pq += tw_clock_read() - pq_start;
}
}
#endif
void
tw_kp_rollback_event(tw_event * event)
{
tw_event *e = NULL;
tw_kp *kp;
tw_pe *pe;
tw_clock pq_start;
kp = event->dest_lp->kp;
pe = kp->pe;
kp->s_rb_total++;
kp->s_rb_secondary++;
// instrumentation
kp->kp_stats->s_rb_total++;
kp->kp_stats->s_rb_secondary++;
#if VERIFY_ROLLBACK
printf("%d %d: rb_event: %f \n", pe->id, kp->id, event->recv_ts);
if(!kp->pevent_q.size)
tw_error(TW_LOC, "Attempting to rollback empty pevent_q!");
#endif
e = tw_eventq_shift(&kp->pevent_q);
while(e != event)
{
#ifdef USE_RAND_TIEBREAKER
kp->last_sig = kp->pevent_q.head->sig;
#else
kp->last_time = kp->pevent_q.head->recv_ts;
#endif
tw_event_rollback(e);
pq_start = tw_clock_read();
tw_pq_enqueue(pe->pq, e);
pe->stats.s_pq += tw_clock_read() - pq_start;
e = tw_eventq_shift(&kp->pevent_q);
}
tw_event_rollback(e);
#ifdef USE_RAND_TIEBREAKER
if (0 == kp->pevent_q.size)
kp->last_sig = kp->pe->GVT_sig;
else
kp->last_sig = kp->pevent_q.head->sig;
#else
if (0 == kp->pevent_q.size)
kp->last_time = kp->pe->GVT;
else
kp->last_time = kp->pevent_q.head->recv_ts;
#endif
}
#ifndef NUM_OUT_MESG
#define NUM_OUT_MESG 2000
#endif
static tw_out*
init_output_messages(tw_kp *kp)
{
int i;
tw_out *ret = (tw_out *) tw_calloc(TW_LOC, "tw_out", sizeof(struct tw_out), NUM_OUT_MESG);
for (i = 0; i < NUM_OUT_MESG - 1; i++) {
ret[i].next = &ret[i + 1];
ret[i].owner = kp;
}
ret[i].next = NULL;
ret[i].owner = kp;
return ret;
}
void
tw_init_kps(tw_pe * me)
{
tw_kpid i;
int j;
for (i = 0; i < g_tw_nkp; i++)
{
tw_kp *kp = tw_getkp(i);
if (kp->pe != me)
continue;
kp->id = i;
kp->s_nevent_processed = 0;
kp->s_e_rbs = 0;
kp->s_rb_total = 0;
kp->s_rb_secondary = 0;
if (g_tw_synchronization_protocol == OPTIMISTIC ||
g_tw_synchronization_protocol == OPTIMISTIC_DEBUG ||
g_tw_synchronization_protocol == OPTIMISTIC_REALTIME) {
kp->output = init_output_messages(kp);
}
// instrumentation setup
kp->kp_stats = (st_kp_stats*) tw_calloc(TW_LOC, "KP instrumentation", sizeof(st_kp_stats), 1);
for (j = 0; j < 3; j++)
kp->last_stats[j] = (st_kp_stats*) tw_calloc(TW_LOC, "KP instrumentation", sizeof(st_kp_stats), 1);
}
}
tw_out *
tw_kp_grab_output_buffer(tw_kp *kp)
{
if (kp->output) {
tw_out *ret = kp->output;
kp->output = kp->output->next;
ret->next = 0;
return ret;
}
return NULL;
}
void
tw_kp_put_back_output_buffer(tw_out *out)
{
tw_kp *kp = out->owner;
if (kp->output) {
out->next = kp->output;
kp->output = out;
}
else {
kp->output = out;
kp->output->next = NULL;
}
}