Skip to content

Commit 6e35d0c

Browse files
committed
ADEvent now stores the event indices associated with func entry/exit and comm send/recv to avoid having to do string comparisons for every event
Other minor improvements to ADEvent that may boost performance Fixed some ADEvent unit tests incorrectly assigning different eids to the same event types and added catch for future errors of this type
1 parent dc8cacb commit 6e35d0c

3 files changed

Lines changed: 94 additions & 41 deletions

File tree

include/chimbuko/ad/ADEvent.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,12 @@ namespace chimbuko {
312312
*/
313313
const std::unordered_map<int, std::string> *m_counterMap;
314314

315+
int m_eidx_func_entry; /**< If previously seen, the eid corresponding to the function entry event (-1 otherwise)*/
316+
int m_eidx_func_exit; /**< If previously seen, the eid corresponding to the function exit event (-1 otherwise)*/
317+
int m_eidx_comm_send; /**< If previously seen, the eid corresponding to the comm send event (-1 otherwise)*/
318+
int m_eidx_comm_recv; /**< If previously seen, the eid corresponding to the comm recv event (-1 otherwise)*/
319+
320+
315321

316322
/**
317323
* @brief communication event stack. Once a function call has exited, all comms events are associated with that call and the stack is cleared

src/ad/ADEvent.cpp

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
using namespace chimbuko;
99

1010
ADEvent::ADEvent(bool verbose)
11-
: m_funcMap(nullptr), m_eventType(nullptr), m_counterMap(nullptr), m_verbose(verbose)
11+
: m_funcMap(nullptr), m_eventType(nullptr), m_counterMap(nullptr), m_verbose(verbose),
12+
m_eidx_func_entry(-1), m_eidx_func_exit(-1), m_eidx_comm_send(-1), m_eidx_comm_recv(-1)
1213
{
1314

1415
}
@@ -123,31 +124,49 @@ CallListIterator_t ADEvent::addCall(const ExecData_t &exec){
123124

124125

125126
EventError ADEvent::addFunc(const Event_t& event) {
127+
//Determine the event type. Use the known event indices if previously determined, otherwise find them
126128
if (m_eventType == nullptr) {
127129
std::cerr << "Uninitialized eventType\n";
128130
return EventError::UnknownEvent;
129131
}
130-
131-
std::string eventType;
132132
int eid = static_cast<int>(event.eid());
133-
if (m_eventType->count(eid) == 0) {
134-
std::cerr << "Unknown event in eventType: " << eid << std::endl;
135-
return EventError::UnknownEvent;
133+
134+
bool is_entry(false), is_exit(false);
135+
136+
if(eid == m_eidx_func_entry){
137+
is_entry = true;
138+
}else if(eid == m_eidx_func_exit){
139+
is_exit = true;
140+
}else{
141+
//Event might be an unknown type *or* the m_eidx* members have not yet been set
142+
auto it = m_eventType->find(eid);
143+
if(it == m_eventType->end()){ //event index is not in the map??
144+
std::cerr << "Unknown event in eventType: " << eid << std::endl;
145+
return EventError::UnknownEvent;
146+
}
147+
if(m_eidx_func_entry == -1 && it->second == "ENTRY"){
148+
m_eidx_func_entry = eid;
149+
is_entry = true;
150+
}else if(m_eidx_func_exit == -1 && it->second == "EXIT"){
151+
m_eidx_func_exit = eid;
152+
is_exit = true;
153+
}
136154
}
137155

156+
//Get the iterator to the function name
138157
if (m_funcMap == nullptr) {
139158
std::cerr << "Uninitialized function map\n";
140159
return EventError::UnknownEvent;
141160
}
142-
143-
if (m_funcMap->count(event.fid()) == 0) {
161+
auto func_name_it = m_funcMap->find(event.fid());
162+
163+
if(func_name_it == m_funcMap->end()){
144164
std::cerr << "Unknown function event\n";
145165
return EventError::UnknownFunc;
146166
}
167+
const std::string &func_name = func_name_it->second;
147168

148-
eventType = m_eventType->find(eid)->second;
149-
150-
if (eventType.compare("ENTRY") == 0){
169+
if(is_entry){
151170
//Create a new ExecData_t object with function entry information and push onto the call list
152171
CallList_t& cl = m_callList[event.pid()][event.rid()][event.tid()];
153172

@@ -159,20 +178,20 @@ EventError ADEvent::addFunc(const Event_t& event) {
159178
it->set_parent(cs.top()->get_id());
160179
cs.top()->inc_n_children();
161180
}
162-
it->set_funcname(m_funcMap->find(event.fid())->second);
181+
it->set_funcname(func_name);
163182
cs.push(it);
164183

165184
//Add the new call to the map of call index string
166185
m_callIDMap[it->get_id()] = it;
167186

168187
return EventError::OK;
169-
}else if (eventType.compare("EXIT") == 0){
188+
}else if(is_exit){
170189
CallStack_t& cs = m_callStack[event.pid()][event.rid()][event.tid()];
171190
if (cs.size() == 0) { //Expect to have at least one entry; that created when the ENTRY was encountered
172191
std::stringstream ss;
173192
ss << "\n***** Empty call stack! *****" << std::endl
174193
<< "Event information: " << event.get_json().dump() << std::endl
175-
<< "Event type: " << m_eventType->find(event.eid())->second << " Function name: " << m_funcMap->find(event.fid())->second << std::endl;
194+
<< "Event type: EXIT Function name: " << func_name << std::endl;
176195
recoverable_error(ss.str());
177196
return EventError::EmptyCallStack;
178197
}
@@ -183,13 +202,8 @@ EventError ADEvent::addFunc(const Event_t& event) {
183202
std::stringstream ss;
184203
ss << "\n***** Invalid EXIT event! *****" << std::endl
185204
<< "Event information: " << event.get_json().dump() << std::endl
186-
<< "Event type: " << m_eventType->find(event.eid())->second << " Function name: " << m_funcMap->find(event.fid())->second << std::endl;
205+
<< "Event type: EXIT Function name: " << func_name << std::endl;
187206
recoverable_error(ss.str());
188-
189-
// while (!cs.empty()) {
190-
// std::cerr << *cs.top() << std::endl;
191-
// cs.pop();
192-
// }
193207
return EventError::CallStackViolation;
194208
}
195209
//Remove the object from the stack (it still lives in the CallList)
@@ -249,22 +263,41 @@ EventError ADEvent::addFunc(const Event_t& event) {
249263
}
250264

251265
EventError ADEvent::addComm(const Event_t& event) {
252-
if (m_eventType == nullptr)
266+
if (m_eventType == nullptr) {
267+
std::cerr << "Uninitialized eventType\n";
253268
return EventError::UnknownEvent;
254-
269+
}
255270
int eid = static_cast<int>(event.eid());
256-
if (m_eventType->count(eid) == 0)
257-
return EventError::UnknownEvent;
258271

259-
std::string eventType = m_eventType->find(eid)->second;
272+
bool is_send(false), is_recv(false);
273+
274+
if(eid == m_eidx_comm_send){
275+
is_send = true;
276+
}else if(eid == m_eidx_comm_recv){
277+
is_recv = true;
278+
}else{
279+
//Event might be an unknown type *or* the m_eidx* members have not yet been set
280+
auto it = m_eventType->find(eid);
281+
if(it == m_eventType->end()){ //event index is not in the map??
282+
std::cerr << "Unknown event in eventType: " << eid << std::endl;
283+
return EventError::UnknownEvent;
284+
}
285+
if(m_eidx_comm_send == -1 && it->second == "SEND"){
286+
m_eidx_comm_send = eid;
287+
is_send = true;
288+
}else if(m_eidx_comm_recv == -1 && it->second == "RECV"){
289+
m_eidx_comm_recv = eid;
290+
is_recv = true;
291+
}
292+
}
260293

261-
if (eventType.compare("SEND") == 0 || eventType.compare("RECV") == 0) {
294+
if (is_send || is_recv) {
262295
CommStack_t& cs = m_commStack[event.pid()][event.rid()][event.tid()];
263-
cs.push(CommData_t(event, eventType));
296+
cs.push(CommData_t(event, is_send ? "SEND" : "RECV"));
297+
return EventError::OK;
264298
}
265-
else return EventError::UnknownEvent;
266-
267-
return EventError::OK;
299+
300+
return EventError::UnknownEvent;
268301
}
269302

270303
EventError ADEvent::addCounter(const Event_t& event){
@@ -278,7 +311,7 @@ EventError ADEvent::addCounter(const Event_t& event){
278311
if (it == m_counterMap->end())
279312
return EventError::UnknownEvent;
280313

281-
std::string counterName = it->second;
314+
const std::string &counterName = it->second;
282315
CounterStack_t &cs = m_counterStack[event.pid()][event.rid()][event.tid()];
283316
cs.push(CounterData_t(event, counterName));
284317

test/unit_tests/ad/ADEvent.cpp

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,20 @@ struct ADFuncEventContainer{
3838
int addEvent(const FuncEvent &ev){ events.push_back(ev); return events.size() - 1; } //returns index of last added event
3939

4040
void registerEvents(){
41-
for(int i=0;i<events.size();i++) event_types[ events[i].data[IDX_E] ] = events[i].event_type;
41+
for(int i=0;i<events.size();i++){
42+
int eid = events[i].data[IDX_E];
43+
if(event_types.count(eid) && event_types[eid] != events[i].event_type)
44+
throw("registerEvents: multiple event types have same eid!");
45+
event_types[eid] = events[i].event_type;
46+
}
4247
}
4348
void registerFuncs(){
44-
for(int i=0;i<events.size();i++) func_map[ events[i].data[FUNC_IDX_F] ] = events[i].func_name;
49+
for(int i=0;i<events.size();i++){
50+
int fid = events[i].data[FUNC_IDX_F];
51+
if(func_map.count(fid) && func_map[fid] != events[i].func_name)
52+
throw("registerFuncs: multiple funcs have same fid!");
53+
func_map[fid] = events[i].func_name;
54+
}
4555
}
4656
void linkEventType(){
4757
event_manager.linkEventType(&event_types);
@@ -221,13 +231,15 @@ TEST(ADEventTesttrimCallList, trimsCorrectly){
221231
ADFuncEventContainer ad;
222232
long time = 100;
223233

224-
int event_idx = 0;
225234
int fid1=0;
226235
int fid2=1;
227236

228-
int idx_entry = ad.addEvent(FuncEvent("ENTRY","MYFUNC" ,event_idx++,fid1, 0 ));
229-
int idx_exit = ad.addEvent(FuncEvent("EXIT","MYFUNC" ,event_idx++,fid1, time ));
230-
int idx_entry2 = ad.addEvent(FuncEvent("ENTRY","MYFUNC",event_idx++,fid2, time+50 )); //different function
237+
int eid_entry = 0;
238+
int eid_exit = 1;
239+
240+
int idx_entry = ad.addEvent(FuncEvent("ENTRY","MYFUNC" ,eid_entry,fid1, 0 ));
241+
int idx_exit = ad.addEvent(FuncEvent("EXIT","MYFUNC" ,eid_exit,fid1, time ));
242+
int idx_entry2 = ad.addEvent(FuncEvent("ENTRY","MYFUNC2",eid_entry,fid2, time+50 )); //different function
231243
ad.initializeAD();
232244
EXPECT_EQ( ad.event_manager.addFunc(ad[idx_entry]), EventError::OK );
233245
EXPECT_EQ( ad.event_manager.addFunc(ad[idx_exit]), EventError::OK );
@@ -263,13 +275,15 @@ TEST(ADEventTest, purgeCallList){
263275
ADFuncEventContainer ad;
264276
long time = 100;
265277

266-
int event_idx = 0;
267278
int fid1=0;
268279
int fid2=1;
269280

270-
int idx_entry = ad.addEvent(FuncEvent("ENTRY","MYFUNC" ,event_idx++,fid1, 0 ));
271-
int idx_exit = ad.addEvent(FuncEvent("EXIT","MYFUNC" ,event_idx++,fid1, time ));
272-
int idx_entry2 = ad.addEvent(FuncEvent("ENTRY","MYFUNC",event_idx++,fid2, time+50 )); //different function
281+
int eid_entry = 0;
282+
int eid_exit = 1;
283+
284+
int idx_entry = ad.addEvent(FuncEvent("ENTRY","MYFUNC" ,eid_entry,fid1, 0 ));
285+
int idx_exit = ad.addEvent(FuncEvent("EXIT","MYFUNC" ,eid_exit,fid1, time ));
286+
int idx_entry2 = ad.addEvent(FuncEvent("ENTRY","MYFUNC2",eid_entry,fid2, time+50 )); //different function
273287
ad.initializeAD();
274288
EXPECT_EQ( ad.event_manager.addFunc(ad[idx_entry]), EventError::OK );
275289
EXPECT_EQ( ad.event_manager.addFunc(ad[idx_exit]), EventError::OK );

0 commit comments

Comments
 (0)