change the way to detect loops
[c11tester.git] / history.cc
1 #include <inttypes.h>
2 #include "history.h"
3 #include "action.h"
4 #include "funcnode.h"
5 #include "common.h"
6
7 #include "model.h"
8 #include "execution.h"
9
10
11 /** @brief Constructor */
12 ModelHistory::ModelHistory() :
13         func_counter(1),        /* function id starts with 1 */
14         func_map(),
15         func_map_rev(),
16         func_nodes(),
17         write_history(),
18         write_locations()
19 {}
20
21 void ModelHistory::enter_function(const uint32_t func_id, thread_id_t tid)
22 {
23         //model_print("thread %d entering func %d\n", tid, func_id);
24         uint id = id_to_int(tid);
25         SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
26         SnapVector< SnapList<action_list_t *> *> *
27                 thrd_func_act_lists = model->get_execution()->get_thrd_func_act_lists();
28
29         if ( thrd_func_list->size() <= id ) {
30                 uint oldsize = thrd_func_list->size();
31                 thrd_func_list->resize( id + 1 );
32                 for (uint i = oldsize; i < id + 1; i++) {
33                         new (&(*thrd_func_list)[i]) func_id_list_t();
34                         // push 0 as a dummy function id to a void seg fault
35                         (*thrd_func_list)[i].push_back(0);
36                 }
37
38                 thrd_func_act_lists->resize( id + 1 );
39                 for (uint i = oldsize; i < id + 1; i++) {
40                         (*thrd_func_act_lists)[i] = new SnapList<action_list_t *>();
41                 }
42         }
43
44         SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
45
46         (*thrd_func_list)[id].push_back(func_id);
47         func_act_lists->push_back( new action_list_t() );
48
49         if ( func_nodes.size() <= func_id )
50                 resize_func_nodes( func_id + 1 );
51 }
52
53 /* @param func_id a non-zero value */
54 void ModelHistory::exit_function(const uint32_t func_id, thread_id_t tid)
55 {
56         uint32_t id = id_to_int(tid);
57         SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
58         SnapVector< SnapList<action_list_t *> *> *
59                 thrd_func_act_lists = model->get_execution()->get_thrd_func_act_lists();
60
61         SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
62         uint32_t last_func_id = (*thrd_func_list)[id].back();
63
64         if (last_func_id == func_id) {
65                 FuncNode * func_node = func_nodes[func_id];
66                 func_node->clear_read_map(tid);
67
68                 action_list_t * curr_act_list = func_act_lists->back();
69                 func_node->incr_exit_count();
70
71                 /* defer the processing of curr_act_list until the function has exits a few times 
72                  * (currently 2 times) so that more information can be gathered to infer nullity predicates.
73                  */
74                 if (func_node->get_exit_count() >= 2) {
75                         ModelList<action_list_t *> * action_list_buffer = func_node->get_action_list_buffer();
76                         while (action_list_buffer->size() > 0) {
77                                 action_list_t * act_list = action_list_buffer->back();
78                                 action_list_buffer->pop_back();
79                                 func_node->update_tree(act_list);
80                         }
81
82                         func_node->update_tree(curr_act_list);
83                 } else
84                         func_node->get_action_list_buffer()->push_front(curr_act_list);
85
86                 (*thrd_func_list)[id].pop_back();
87                 func_act_lists->pop_back();
88         } else {
89                 model_print("trying to exit with a wrong function id\n");
90                 model_print("--- last_func: %d, func_id: %d\n", last_func_id, func_id);
91         }
92         //model_print("thread %d exiting func %d\n", tid, func_id);
93 }
94
95 void ModelHistory::resize_func_nodes(uint32_t new_size)
96 {
97         uint32_t old_size = func_nodes.size();
98
99         if ( old_size < new_size )
100                 func_nodes.resize(new_size);
101
102         for (uint32_t id = old_size;id < new_size;id++) {
103                 const char * func_name = func_map_rev[id];
104                 FuncNode * func_node = new FuncNode();
105                 func_node->set_func_id(id);
106                 func_node->set_func_name(func_name);
107                 func_nodes[id] = func_node;
108         }
109 }
110
111 void ModelHistory::process_action(ModelAction *act, thread_id_t tid)
112 {
113         /* return if thread i has not entered any function or has exited
114            from all functions */
115         SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
116         SnapVector< SnapList<action_list_t *> *> *
117                 thrd_func_act_lists = model->get_execution()->get_thrd_func_act_lists();
118
119         uint32_t id = id_to_int(tid);
120         if ( thrd_func_list->size() <= id )
121                 return;
122
123         /* get the function id that thread i is currently in */
124         uint32_t func_id = (*thrd_func_list)[id].back();
125         SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
126
127         if (func_id == 0)
128                 return;
129         else if ( func_nodes.size() <= func_id )
130                 resize_func_nodes( func_id + 1 );
131
132         FuncNode * func_node = func_nodes[func_id];
133
134         /* do not care about actions without a position */
135         if (act->get_position() == NULL)
136                 return;
137
138         if (act->is_read())
139                 func_node->store_read(act, tid);
140
141         if (act->is_write())
142                 add_to_write_history(act->get_location(), act->get_write_value());
143
144         /* add to curr_inst_list */
145         bool second_part_of_rmw = act->is_rmwc() || act->is_rmw();
146         if (!second_part_of_rmw) {
147                 action_list_t * curr_act_list = func_act_lists->back();
148                 ASSERT(curr_act_list != NULL);
149
150                 ModelAction * last_act;
151                 if (curr_act_list->size() != 0)
152                         last_act = curr_act_list->back();
153
154                 // do not add actions with the same sequence number twice
155                 if (last_act != NULL && last_act->get_seq_number() == act->get_seq_number())
156                         return;
157
158                 curr_act_list->push_back(act);
159                 func_node->add_inst(act);
160         }
161 }
162
163 /* return the FuncNode given its func_id  */
164 FuncNode * ModelHistory::get_func_node(uint32_t func_id)
165 {
166         if (func_nodes.size() <= func_id)       // this node has not been added to func_nodes
167                 return NULL;
168
169         return func_nodes[func_id];
170 }
171
172 uint64_t ModelHistory::query_last_read(void * location, thread_id_t tid)
173 {
174         SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
175         uint32_t id = id_to_int(tid);
176
177         ASSERT( thrd_func_list->size() > id );
178         uint32_t func_id = (*thrd_func_list)[id].back();
179         FuncNode * func_node = func_nodes[func_id];
180
181         uint64_t last_read_val = 0xdeadbeef;
182         if (func_node != NULL) {
183                 last_read_val = func_node->query_last_read(location, tid);
184         }
185
186         return last_read_val;
187 }
188
189 void ModelHistory::add_to_write_history(void * location, uint64_t write_val)
190 {
191         write_set_t * write_set = write_history.get(location);
192
193         if (write_set == NULL) {
194                 write_set = new write_set_t();
195                 write_history.put(location, write_set);
196         }
197
198         write_set->add(write_val);
199         write_locations.add(location);
200 }
201
202 void ModelHistory::print_write()
203 {
204 }
205
206 void ModelHistory::print_func_node()
207 {
208         /* function id starts with 1 */
209         for (uint32_t i = 1; i < func_nodes.size(); i++) {
210                 FuncNode * func_node = func_nodes[i];
211
212                 func_inst_list_mt * entry_insts = func_node->get_entry_insts();
213                 model_print("function %s has entry actions\n", func_node->get_func_name());
214
215                 mllnode<FuncInst*>* it;
216                 for (it = entry_insts->begin();it != NULL;it=it->getNext()) {
217                         FuncInst *inst = it->getVal();
218                         model_print("type: %d, at: %s\n", inst->get_type(), inst->get_position());
219                 }
220 /*
221                 func_inst_list_mt * inst_list = funcNode->get_inst_list();
222
223                 model_print("function %s has following actions\n", funcNode->get_func_name());
224                 func_inst_list_mt::iterator it;
225                 for (it = inst_list->begin(); it != inst_list->end(); it++) {
226                         FuncInst *inst = *it;
227                         model_print("type: %d, at: %s\n", inst->get_type(), inst->get_position());
228                 }
229 */
230         }
231 }