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 cvmap.get(act)->print();
33 if (badrfset.contains(act)) {
34 model_print("DESIRED %u \n",badrfset.get(act)->get_seq_number());
37 hash = hash ^ (hash << 3) ^ ((*it)->hash());
39 model_print("HASH %u\n", hash);
40 model_print("---------------------------------------------------------------------\n");
43 void SCAnalysis::analyze(action_list_t *actions) {
44 buildVectors(actions);
46 action_list_t *list = generateSC(actions);
51 void SCAnalysis::check_rf(action_list_t *list) {
52 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
53 const ModelAction *act = *it;
55 if (act->get_reads_from()!=lastwrmap.get(act->get_location()))
56 badrfset.put(act,lastwrmap.get(act->get_location()));
59 lastwrmap.put(act->get_location(), act);
63 bool SCAnalysis::merge(ClockVector *cv, const ModelAction *act, const ModelAction *act2) {
64 ClockVector * cv2=cvmap.get(act2);
67 if (cv2->getClock(act->get_tid()) >= act->get_seq_number() && act->get_seq_number() != 0) {
69 //refuse to introduce cycles into clock vectors
73 return cv->merge(cv2);
76 ModelAction * SCAnalysis::getNextAction() {
77 ModelAction *act = NULL;
78 /* Find the earliest in SC ordering */
79 for (int i = 0; i <= maxthreads; i++) {
80 action_list_t *threadlist = &threadlists[i];
81 if (threadlist->empty())
83 ModelAction *first = threadlist->front();
88 ClockVector *cv = cvmap.get(act);
89 if (cv->synchronized_since(first)) {
97 /* Find the model action with the earliest sequence number in case of a cycle.
100 for (int i = 0; i <= maxthreads; i++) {
101 action_list_t *threadlist = &threadlists[i];
102 if (threadlist->empty())
104 ModelAction *first = threadlist->front();
105 ClockVector *cv = cvmap.get(act);
106 ClockVector *cvfirst = cvmap.get(first);
107 if (first->get_seq_number()<act->get_seq_number()&&
108 (cv->synchronized_since(first)||!cvfirst->synchronized_since(act))) {
113 /* See if hb demands an earlier action. */
114 for (int i = 0; i <= maxthreads; i++) {
115 action_list_t *threadlist = &threadlists[i];
116 if (threadlist->empty())
118 ModelAction *first = threadlist->front();
119 ClockVector *cv = act->get_cv();
120 if (cv->synchronized_since(first)) {
127 action_list_t * SCAnalysis::generateSC(action_list_t *list) {
128 action_list_t *sclist = new action_list_t();
130 ModelAction *act = getNextAction();
133 thread_id_t tid = act->get_tid();
135 threadlists[id_to_int(tid)].pop_front();
136 //add ordering constraints from this choice
137 if (updateConstraints(act)) {
138 //propagate changes if we have them
142 sclist->push_back(act);
147 void SCAnalysis::buildVectors(action_list_t *list) {
149 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
150 ModelAction *act = *it;
151 int threadid = id_to_int(act->get_tid());
152 if (threadid > maxthreads) {
153 threadlists.resize(threadid + 1);
154 maxthreads = threadid;
156 threadlists[threadid].push_back(act);
160 bool SCAnalysis::updateConstraints(ModelAction *act) {
161 bool changed = false;
162 for (int i = 0; i <= maxthreads; i++) {
163 thread_id_t tid = int_to_id(i);
164 if (tid == act->get_tid())
167 action_list_t *list = &threadlists[id_to_int(tid)];
168 for (action_list_t::iterator rit = list->begin(); rit != list->end(); rit++) {
169 ModelAction *write = *rit;
170 if (!write->is_write())
172 ClockVector *writecv = cvmap.get(write);
173 if (writecv->synchronized_since(act))
175 if (write->get_location() == act->get_location()) {
176 //write is sc after act
177 merge(writecv, write, act);
186 bool SCAnalysis::processRead(ModelAction *read, ClockVector *cv) {
187 bool changed = false;
189 /* Merge in the clock vector from the write */
190 const ModelAction *write = read->get_reads_from();
191 ClockVector *writecv = cvmap.get(write);
192 changed |= merge(cv, read, write) && (*read < *write);
194 for (int i = 0; i <= maxthreads; i++) {
195 thread_id_t tid = int_to_id(i);
196 if (tid == read->get_tid())
198 if (tid == write->get_tid())
200 action_list_t *list = execution->get_actions_on_obj(read->get_location(), tid);
203 for (action_list_t::reverse_iterator rit = list->rbegin(); rit != list->rend(); rit++) {
204 ModelAction *write2 = *rit;
205 if (!write2->is_write())
208 ClockVector *write2cv = cvmap.get(write2);
209 if (write2cv == NULL)
212 /* write -sc-> write2 &&
215 if (write2cv->synchronized_since(write)) {
216 changed |= merge(write2cv, write2, read);
219 //looking for earliest write2 in iteration to satisfy this
222 write2 -sc-> write */
223 if (cv->synchronized_since(write2)) {
224 changed |= writecv==NULL || merge(writecv, write, write2);
232 void SCAnalysis::computeCV(action_list_t *list) {
234 bool firsttime = true;
235 ModelAction **last_act = (ModelAction **)model_calloc(1, (maxthreads + 1) * sizeof(ModelAction *));
237 changed = changed&firsttime;
240 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
241 ModelAction *act = *it;
242 ModelAction *lastact = last_act[id_to_int(act->get_tid())];
243 if (act->is_thread_start())
244 lastact = execution->get_thread(act)->get_creation();
245 last_act[id_to_int(act->get_tid())] = act;
246 ClockVector *cv = cvmap.get(act);
248 cv = new ClockVector(NULL, act);
251 if (lastact != NULL) {
252 merge(cv, act, lastact);
254 if (act->is_thread_join()) {
255 Thread *joinedthr = act->get_thread_operand();
256 ModelAction *finish = execution->get_last_action(joinedthr->get_id());
257 changed |= merge(cv, act, finish);
259 if (act->is_read()) {
260 changed |= processRead(act, cv);
263 /* Reset the last action array */
265 bzero(last_act, (maxthreads + 1) * sizeof(ModelAction *));
268 model_free(last_act);