1 #include "scanalysis.h"
3 #include "threads-model.h"
4 #include "clockvector.h"
7 SCAnalysis::SCAnalysis(const ModelExecution *execution) :
17 SCAnalysis::~SCAnalysis() {
20 void SCAnalysis::print_list(action_list_t *list) {
21 model_print("---------------------------------------------------------------------\n");
23 model_print("Not SC\n");
24 unsigned int hash = 0;
26 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
27 const ModelAction *act = *it;
28 if (act->get_seq_number() > 0) {
29 if (badrfset.contains(act))
32 if (badrfset.contains(act)) {
33 model_print("Desired Rf: %u \n",badrfset.get(act)->get_seq_number());
36 hash = hash ^ (hash << 3) ^ ((*it)->hash());
38 model_print("HASH %u\n", hash);
39 model_print("---------------------------------------------------------------------\n");
42 void SCAnalysis::analyze(action_list_t *actions) {
43 buildVectors(actions);
45 action_list_t *list = generateSC(actions);
50 void SCAnalysis::check_rf(action_list_t *list) {
51 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
52 const ModelAction *act = *it;
54 if (act->get_reads_from()!=lastwrmap.get(act->get_location()))
55 badrfset.put(act,lastwrmap.get(act->get_location()));
58 lastwrmap.put(act->get_location(), act);
62 bool SCAnalysis::merge(ClockVector *cv, const ModelAction *act, const ModelAction *act2) {
63 ClockVector * cv2=cvmap.get(act2);
66 if (cv2->getClock(act->get_tid()) >= act->get_seq_number() && act->get_seq_number() != 0) {
68 //refuse to introduce cycles into clock vectors
72 return cv->merge(cv2);
75 ModelAction * SCAnalysis::getNextAction() {
76 ModelAction *act = NULL;
77 /* Find the earliest in SC ordering */
78 for (int i = 0; i <= maxthreads; i++) {
79 action_list_t *threadlist = &threadlists[i];
80 if (threadlist->empty())
82 ModelAction *first = threadlist->front();
87 ClockVector *cv = cvmap.get(act);
88 if (cv->synchronized_since(first)) {
95 /* Find the model action with the earliest sequence number in case of a cycle.
98 for (int i = 0; i <= maxthreads; i++) {
99 action_list_t *threadlist = &threadlists[i];
100 if (threadlist->empty())
102 ModelAction *first = threadlist->front();
103 ClockVector *cvfirst = cvmap.get(first);
104 if (first->get_seq_number()<act->get_seq_number()) {
106 for (int j = 0; j <= maxthreads; j++) {
107 action_list_t *threadlist2 = &threadlists[j];
108 if (threadlist2->empty())
110 ModelAction *check = threadlist2->front();
111 if ((check!=first) &&
112 cvfirst->synchronized_since(check)) {
122 /* See if hb demands an earlier action. */
123 for (int i = 0; i <= maxthreads; i++) {
124 action_list_t *threadlist = &threadlists[i];
125 if (threadlist->empty())
127 ModelAction *first = threadlist->front();
128 ClockVector *cv = act->get_cv();
129 if (cv->synchronized_since(first)) {
136 action_list_t * SCAnalysis::generateSC(action_list_t *list) {
137 action_list_t *sclist = new action_list_t();
139 ModelAction *act = getNextAction();
142 thread_id_t tid = act->get_tid();
144 threadlists[id_to_int(tid)].pop_front();
145 //add ordering constraints from this choice
146 if (updateConstraints(act)) {
147 //propagate changes if we have them
151 sclist->push_back(act);
156 void SCAnalysis::buildVectors(action_list_t *list) {
158 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
159 ModelAction *act = *it;
160 int threadid = id_to_int(act->get_tid());
161 if (threadid > maxthreads) {
162 threadlists.resize(threadid + 1);
163 maxthreads = threadid;
165 threadlists[threadid].push_back(act);
169 bool SCAnalysis::updateConstraints(ModelAction *act) {
170 bool changed = false;
171 for (int i = 0; i <= maxthreads; i++) {
172 thread_id_t tid = int_to_id(i);
173 if (tid == act->get_tid())
176 action_list_t *list = &threadlists[id_to_int(tid)];
177 for (action_list_t::iterator rit = list->begin(); rit != list->end(); rit++) {
178 ModelAction *write = *rit;
179 if (!write->is_write())
181 ClockVector *writecv = cvmap.get(write);
182 if (writecv->synchronized_since(act))
184 if (write->get_location() == act->get_location()) {
185 //write is sc after act
186 merge(writecv, write, act);
195 bool SCAnalysis::processRead(ModelAction *read, ClockVector *cv) {
196 bool changed = false;
198 /* Merge in the clock vector from the write */
199 const ModelAction *write = read->get_reads_from();
200 ClockVector *writecv = cvmap.get(write);
201 changed |= merge(cv, read, write) && (*read < *write);
203 for (int i = 0; i <= maxthreads; i++) {
204 thread_id_t tid = int_to_id(i);
205 if (tid == read->get_tid())
207 if (tid == write->get_tid())
209 action_list_t *list = execution->get_actions_on_obj(read->get_location(), tid);
212 for (action_list_t::reverse_iterator rit = list->rbegin(); rit != list->rend(); rit++) {
213 ModelAction *write2 = *rit;
214 if (!write2->is_write())
217 ClockVector *write2cv = cvmap.get(write2);
218 if (write2cv == NULL)
221 /* write -sc-> write2 &&
224 if (write2cv->synchronized_since(write)) {
225 changed |= merge(write2cv, write2, read);
228 //looking for earliest write2 in iteration to satisfy this
231 write2 -sc-> write */
232 if (cv->synchronized_since(write2)) {
233 changed |= writecv==NULL || merge(writecv, write, write2);
241 void SCAnalysis::computeCV(action_list_t *list) {
243 bool firsttime = true;
244 ModelAction **last_act = (ModelAction **)model_calloc(1, (maxthreads + 1) * sizeof(ModelAction *));
246 changed = changed&firsttime;
249 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
250 ModelAction *act = *it;
251 ModelAction *lastact = last_act[id_to_int(act->get_tid())];
252 if (act->is_thread_start())
253 lastact = execution->get_thread(act)->get_creation();
254 last_act[id_to_int(act->get_tid())] = act;
255 ClockVector *cv = cvmap.get(act);
257 cv = new ClockVector(NULL, act);
260 if (lastact != NULL) {
261 merge(cv, act, lastact);
263 if (act->is_thread_join()) {
264 Thread *joinedthr = act->get_thread_operand();
265 ModelAction *finish = execution->get_last_action(joinedthr->get_id());
266 changed |= merge(cv, act, finish);
268 if (act->is_read()) {
269 changed |= processRead(act, cv);
272 /* Reset the last action array */
274 bzero(last_act, (maxthreads + 1) * sizeof(ModelAction *));
277 model_free(last_act);