little fixes
[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         uint32_t 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         }
40
41         SnapList<action_list_t *> * func_act_lists = thrd_func_act_lists->at(id);
42
43         if (func_act_lists == NULL) {
44                 func_act_lists = new SnapList<action_list_t *>();
45                 thrd_func_act_lists->at(id) = func_act_lists;
46         }
47
48         (*thrd_func_list)[id].push_back(func_id);
49         func_act_lists->push_back( new action_list_t() );
50
51         if ( func_nodes.size() <= func_id )
52                 resize_func_nodes( func_id + 1 );
53 }
54
55 /* @param func_id a non-zero value */
56 void ModelHistory::exit_function(const uint32_t func_id, thread_id_t tid)
57 {
58         uint32_t id = id_to_int(tid);
59         SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
60         SnapVector< SnapList<action_list_t *> *> *
61                 thrd_func_act_lists = model->get_execution()->get_thrd_func_act_lists();
62
63         SnapList<action_list_t *> * func_act_lists = thrd_func_act_lists->at(id);
64         uint32_t last_func_id = (*thrd_func_list)[id].back();
65
66         if (last_func_id == func_id) {
67                 FuncNode * func_node = func_nodes[func_id];
68                 func_node->clear_read_map(tid);
69
70                 action_list_t * curr_act_list = func_act_lists->back();
71                 func_node->update_inst_tree(curr_act_list);
72
73                 (*thrd_func_list)[id].pop_back();
74                 func_act_lists->pop_back();
75         } else {
76                 model_print("trying to exit with a wrong function id\n");
77                 model_print("--- last_func: %d, func_id: %d\n", last_func_id, func_id);
78         }
79         //model_print("thread %d exiting func %d\n", tid, func_id);
80 }
81
82 void ModelHistory::resize_func_nodes(uint32_t new_size)
83 {
84         uint32_t old_size = func_nodes.size();
85
86         if ( old_size < new_size )
87                 func_nodes.resize(new_size);
88
89         for (uint32_t id = old_size;id < new_size;id++) {
90                 const char * func_name = func_map_rev[id];
91                 FuncNode * func_node = new FuncNode();
92                 func_node->set_func_id(id);
93                 func_node->set_func_name(func_name);
94                 func_nodes[id] = func_node;
95         }
96 }
97
98 void ModelHistory::process_action(ModelAction *act, thread_id_t tid)
99 {
100         /* return if thread i has not entered any function or has exited
101            from all functions */
102         SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
103         SnapVector< SnapList<action_list_t *> *> *
104                 thrd_func_act_lists = model->get_execution()->get_thrd_func_act_lists();
105
106         uint32_t id = id_to_int(tid);
107         if ( thrd_func_list->size() <= id )
108                 return;
109
110         /* get the function id that thread i is currently in */
111         uint32_t func_id = (*thrd_func_list)[id].back();
112         SnapList<action_list_t *> * func_act_lists = thrd_func_act_lists->at(id);
113
114         if (func_id == 0)
115                 return;
116         else if ( func_nodes.size() <= func_id )
117                 resize_func_nodes( func_id + 1 );
118
119         FuncNode * func_node = func_nodes[func_id];
120         ASSERT (func_node != NULL);
121
122         /* do not care actions without a position */
123         if (act->get_position() == NULL)
124                 return;
125
126         if (act->is_read())
127                 func_node->store_read(act, tid);
128
129         if (act->is_write())
130                 add_to_write_history(act->get_location(), act->get_write_value());
131
132         /* add to curr_inst_list */
133         action_list_t * curr_act_list = func_act_lists->back();
134         ASSERT(curr_act_list != NULL);
135         curr_act_list->push_back(act);
136 }
137
138 /* return the FuncNode given its func_id  */
139 FuncNode * ModelHistory::get_func_node(uint32_t func_id)
140 {
141         if (func_nodes.size() <= func_id)       // this node has not been added
142                 return NULL;
143
144         return func_nodes[func_id];
145 }
146
147 uint64_t ModelHistory::query_last_read(void * location, thread_id_t tid)
148 {
149         SnapVector<func_id_list_t> * thrd_func_list = model->get_execution()->get_thrd_func_list();
150         uint32_t id = id_to_int(tid);
151
152         ASSERT( thrd_func_list->size() > id );
153         uint32_t func_id = (*thrd_func_list)[id].back();
154         FuncNode * func_node = func_nodes[func_id];
155
156         uint64_t last_read_val = 0xdeadbeef;
157         if (func_node != NULL) {
158                 last_read_val = func_node->query_last_read(location, tid);
159         }
160
161         return last_read_val;
162 }
163
164 void ModelHistory::add_to_write_history(void * location, uint64_t write_val)
165 {
166         write_set_t * write_set = write_history.get(location);
167
168         if (write_set == NULL) {
169                 write_set = new write_set_t();
170                 write_history.put(location, write_set);
171         }
172
173         write_set->add(write_val);
174         write_locations.add(location);
175 }
176
177 void ModelHistory::print_write()
178 {
179 }
180
181 void ModelHistory::print_func_node()
182 {
183         /* function id starts with 1 */
184         for (uint32_t i = 1; i < func_nodes.size(); i++) {
185                 FuncNode * func_node = func_nodes[i];
186
187                 func_inst_list_mt * entry_insts = func_node->get_entry_insts();
188                 model_print("function %s has entry actions\n", func_node->get_func_name());
189
190                 mllnode<FuncInst*>* it;
191                 for (it = entry_insts->begin();it != NULL;it=it->getNext()) {
192                         FuncInst *inst = it->getVal();
193                         model_print("type: %d, at: %s\n", inst->get_type(), inst->get_position());
194                 }
195 /*
196                 func_inst_list_mt * inst_list = funcNode->get_inst_list();
197
198                 model_print("function %s has following actions\n", funcNode->get_func_name());
199                 func_inst_list_mt::iterator it;
200                 for (it = inst_list->begin(); it != inst_list->end(); it++) {
201                         FuncInst *inst = *it;
202                         model_print("type: %d, at: %s\n", inst->get_type(), inst->get_position());
203                 }
204 */
205         }
206 }