b546cd0abd1bc65e48ec631b6cd47eb3eb832951
[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(),        // snapshot data structure
18         loc_func_nodes_map()    // shapshot data structure
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         FuncNode * func_node = func_nodes[func_id];
53         func_node->init_predicate_tree_position(tid);
54 }
55
56 /* @param func_id a non-zero value */
57 void ModelHistory::exit_function(const uint32_t func_id, thread_id_t tid)
58 {
59         uint32_t id = id_to_int(tid);
60         SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
61         SnapVector< SnapList<action_list_t *> *> *
62                 thrd_func_act_lists = model->get_execution()->get_thrd_func_act_lists();
63
64         SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
65         uint32_t last_func_id = (*thrd_func_list)[id].back();
66
67         if (last_func_id == func_id) {
68                 FuncNode * func_node = func_nodes[func_id];
69                 func_node->unset_predicate_tree_position(tid);
70                 //func_node->clear_read_map(tid);
71
72                 action_list_t * curr_act_list = func_act_lists->back();
73
74                 /* defer the processing of curr_act_list until the function has exits a few times 
75                  * (currently twice) so that more information can be gathered to infer nullity predicates.
76                  */
77                 func_node->incr_exit_count();
78                 if (func_node->get_exit_count() >= 2) {
79                         ModelList<action_list_t *> * action_list_buffer = func_node->get_action_list_buffer();
80                         while (action_list_buffer->size() > 0) {
81                                 action_list_t * act_list = action_list_buffer->back();
82                                 action_list_buffer->pop_back();
83                                 func_node->update_tree(act_list);
84                         }
85
86                         func_node->update_tree(curr_act_list);
87                 } else
88                         func_node->get_action_list_buffer()->push_front(curr_act_list);
89
90                 (*thrd_func_list)[id].pop_back();
91                 func_act_lists->pop_back();
92         } else {
93                 model_print("trying to exit with a wrong function id\n");
94                 model_print("--- last_func: %d, func_id: %d\n", last_func_id, func_id);
95         }
96         //model_print("thread %d exiting func %d\n", tid, func_id);
97 }
98
99 void ModelHistory::resize_func_nodes(uint32_t new_size)
100 {
101         uint32_t old_size = func_nodes.size();
102
103         if ( old_size < new_size )
104                 func_nodes.resize(new_size);
105
106         for (uint32_t id = old_size; id < new_size; id++) {
107                 const char * func_name = func_map_rev[id];
108                 FuncNode * func_node = new FuncNode(this);
109                 func_node->set_func_id(id);
110                 func_node->set_func_name(func_name);
111                 func_nodes[id] = func_node;
112         }
113 }
114
115 void ModelHistory::process_action(ModelAction *act, thread_id_t tid)
116 {
117         /* return if thread i has not entered any function or has exited
118            from all functions */
119         SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
120         SnapVector< SnapList<action_list_t *> *> *
121                 thrd_func_act_lists = model->get_execution()->get_thrd_func_act_lists();
122
123         uint32_t id = id_to_int(tid);
124         if ( thrd_func_list->size() <= id )
125                 return;
126
127         /* get the function id that thread i is currently in */
128         uint32_t func_id = (*thrd_func_list)[id].back();
129         SnapList<action_list_t *> * func_act_lists = (*thrd_func_act_lists)[id];
130
131         if (act->is_write()) {
132                 void * location = act->get_location();
133                 uint64_t value = act->get_write_value();
134                 add_to_write_history(location, value);
135
136                 /* update FuncNodes */
137                 SnapList<FuncNode *> * func_nodes = loc_func_nodes_map.get(location);
138                 sllnode<FuncNode *> * it = NULL;
139                 if (func_nodes != NULL)
140                         it = func_nodes->begin();
141
142                 for (; it != NULL; it = it->getNext()) {
143                         FuncNode * func_node = it->getVal();
144                         func_node->add_to_val_loc_map(value, location);
145                 }
146         }
147
148         /* the following does not care about actions without a position */
149         if (func_id == 0 || act->get_position() == NULL)
150                 return;
151
152         if ( func_nodes.size() <= func_id )
153                 resize_func_nodes( func_id + 1 );
154
155         FuncNode * func_node = func_nodes[func_id];
156
157 //      if (act->is_read())
158 //              func_node->store_read(act, tid);
159
160         /* add to curr_inst_list */
161         bool second_part_of_rmw = act->is_rmwc() || act->is_rmw();
162         if (!second_part_of_rmw) {
163                 action_list_t * curr_act_list = func_act_lists->back();
164                 ASSERT(curr_act_list != NULL);
165
166                 ModelAction * last_act = NULL;
167                 if (curr_act_list->size() != 0)
168                         last_act = curr_act_list->back();
169
170                 // do not add actions with the same sequence number twice
171                 if (last_act != NULL && last_act->get_seq_number() == act->get_seq_number())
172                         return;
173
174                 curr_act_list->push_back(act);
175                 func_node->add_inst(act);
176         }
177 }
178
179 /* return the FuncNode given its func_id  */
180 FuncNode * ModelHistory::get_func_node(uint32_t func_id)
181 {
182         if (func_nodes.size() <= func_id)       // this node has not been added to func_nodes
183                 return NULL;
184
185         return func_nodes[func_id];
186 }
187
188 /*
189 uint64_t ModelHistory::query_last_read(void * location, thread_id_t tid)
190 {
191         SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
192         uint32_t id = id_to_int(tid);
193
194         ASSERT( thrd_func_list->size() > id );
195         uint32_t func_id = (*thrd_func_list)[id].back();
196         FuncNode * func_node = func_nodes[func_id];
197
198         uint64_t last_read_val = 0xdeadbeef;
199         if (func_node != NULL) {
200                 last_read_val = func_node->query_last_read(location, tid);
201         }
202
203         return last_read_val;
204 }
205 */
206
207 void ModelHistory::add_to_write_history(void * location, uint64_t write_val)
208 {
209         value_set_t * write_set = write_history.get(location);
210
211         if (write_set == NULL) {
212                 write_set = new value_set_t();
213                 write_history.put(location, write_set);
214         }
215
216         write_set->add(write_val);
217 }
218
219 void ModelHistory::add_to_loc_func_nodes_map(void * location, FuncNode * node)
220 {
221         SnapList<FuncNode *> * func_node_list = loc_func_nodes_map.get(location);
222         if (func_node_list == NULL) {
223                 func_node_list = new SnapList<FuncNode *>();
224                 loc_func_nodes_map.put(location, func_node_list);
225         }
226
227         func_node_list->push_back(node);
228 }
229
230 /* Reallocate some snapshotted memories when new executions start */
231 void ModelHistory::set_new_exec_flag()
232 {
233         for (uint i = 1; i < func_nodes.size(); i++) {
234                 FuncNode * func_node = func_nodes[i];
235                 func_node->set_new_exec_flag();
236         }
237 }
238
239 void ModelHistory::print_write()
240 {
241 }
242
243 void ModelHistory::print_func_node()
244 {
245         /* function id starts with 1 */
246         for (uint32_t i = 1; i < func_nodes.size(); i++) {
247                 FuncNode * func_node = func_nodes[i];
248
249                 func_inst_list_mt * entry_insts = func_node->get_entry_insts();
250                 model_print("function %s has entry actions\n", func_node->get_func_name());
251
252                 mllnode<FuncInst*>* it;
253                 for (it = entry_insts->begin();it != NULL;it=it->getNext()) {
254                         FuncInst *inst = it->getVal();
255                         model_print("type: %d, at: %s\n", inst->get_type(), inst->get_position());
256                 }
257 /*
258                 func_inst_list_mt * inst_list = funcNode->get_inst_list();
259
260                 model_print("function %s has following actions\n", funcNode->get_func_name());
261                 func_inst_list_mt::iterator it;
262                 for (it = inst_list->begin(); it != inst_list->end(); it++) {
263                         FuncInst *inst = *it;
264                         model_print("type: %d, at: %s\n", inst->get_type(), inst->get_position());
265                 }
266 */
267         }
268 }