Skip to content

Commit 37bb473

Browse files
committed
Refactor energy profiler region management by removing exception handling in start and end region functions
1 parent d1a9727 commit 37bb473

File tree

1 file changed

+45
-62
lines changed

1 file changed

+45
-62
lines changed

profiling/energy-profiler/kp_energy_profiler.cpp

Lines changed: 45 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -46,82 +46,65 @@ void log_verbose(const std::string& message) {
4646

4747
// Start a region
4848
void start_region(const std::string& name, RegionType type, uint64_t id) {
49-
try {
50-
TimingInfo region;
51-
region.name = name;
52-
region.type = type;
53-
region.start_time = std::chrono::high_resolution_clock::now();
54-
region.id = id;
55-
auto& state = EnergyProfilerState::get_instance();
56-
std::lock_guard<std::mutex> lock(state.get_mutex());
57-
state.get_active_regions().push_back(region);
58-
} catch (const std::exception& e) {
59-
std::cerr << "Error in start_region: " << e.what() << std::endl;
60-
}
49+
TimingInfo region;
50+
region.name = name;
51+
region.type = type;
52+
region.start_time = std::chrono::high_resolution_clock::now();
53+
region.id = id;
54+
auto& state = EnergyProfilerState::get_instance();
55+
std::lock_guard<std::mutex> lock(state.get_mutex());
56+
state.get_active_regions().push_back(region);
6157
}
6258

6359
// End last region of given type
6460
void end_region_by_type(RegionType type_to_end) {
65-
try {
66-
auto& state = EnergyProfilerState::get_instance();
67-
std::lock_guard<std::mutex> lock(state.get_mutex());
68-
auto& active_regions = state.get_active_regions();
69-
if (active_regions.empty()) return;
70-
auto it = std::find_if(active_regions.rbegin(), active_regions.rend(),
71-
[type_to_end](const TimingInfo& region) {
72-
return region.type == type_to_end;
73-
});
74-
if (it != active_regions.rend()) {
75-
auto region = *it;
76-
active_regions.erase(std::next(it).base());
77-
region.end_time = std::chrono::high_resolution_clock::now();
78-
state.get_completed_timings().push_back(region);
79-
}
80-
} catch (const std::exception& e) {
81-
std::cerr << "Error in end_region_by_type: " << e.what() << std::endl;
61+
auto& state = EnergyProfilerState::get_instance();
62+
std::lock_guard<std::mutex> lock(state.get_mutex());
63+
auto& active_regions = state.get_active_regions();
64+
if (active_regions.empty()) return;
65+
auto it = std::find_if(active_regions.rbegin(), active_regions.rend(),
66+
[type_to_end](const TimingInfo& region) {
67+
return region.type == type_to_end;
68+
});
69+
if (it != active_regions.rend()) {
70+
auto region = *it;
71+
active_regions.erase(std::next(it).base());
72+
region.end_time = std::chrono::high_resolution_clock::now();
73+
state.get_completed_timings().push_back(region);
8274
}
8375
}
8476

8577
// End region by id
8678
void end_region_with_id(uint64_t expected_id) {
87-
try {
88-
auto end_time = std::chrono::high_resolution_clock::now();
89-
auto& state = EnergyProfilerState::get_instance();
90-
std::lock_guard<std::mutex> lock(state.get_mutex());
91-
auto& active_regions = state.get_active_regions();
92-
auto it = std::find_if(active_regions.begin(), active_regions.end(),
93-
[expected_id](const TimingInfo& region) {
94-
return region.id == expected_id;
95-
});
96-
if (it != active_regions.end()) {
97-
auto region = *it;
98-
region.end_time = end_time;
99-
active_regions.erase(it);
100-
state.get_completed_timings().push_back(region);
101-
} else {
102-
std::cerr << "Warning: No active region found with ID " << expected_id
103-
<< "\n";
104-
}
105-
} catch (const std::exception& e) {
106-
std::cerr << "Error in end_region_with_id: " << e.what() << std::endl;
79+
auto end_time = std::chrono::high_resolution_clock::now();
80+
auto& state = EnergyProfilerState::get_instance();
81+
std::lock_guard<std::mutex> lock(state.get_mutex());
82+
auto& active_regions = state.get_active_regions();
83+
auto it = std::find_if(active_regions.begin(), active_regions.end(),
84+
[expected_id](const TimingInfo& region) {
85+
return region.id == expected_id;
86+
});
87+
if (it != active_regions.end()) {
88+
auto region = *it;
89+
region.end_time = end_time;
90+
active_regions.erase(it);
91+
state.get_completed_timings().push_back(region);
92+
} else {
93+
std::cerr << "Warning: No active region found with ID " << expected_id
94+
<< "\n";
10795
}
10896
}
10997

11098
// Get all completed timings
11199
std::vector<TimingInfo> get_all_timings() {
112-
try {
113-
auto& state = EnergyProfilerState::get_instance();
114-
std::lock_guard<std::mutex> lock(state.get_mutex());
115-
std::vector<TimingInfo> all_timings = state.get_completed_timings();
116-
std::sort(all_timings.begin(), all_timings.end(),
117-
[](const TimingInfo& a, const TimingInfo& b) {
118-
return a.start_time < b.start_time;
119-
});
120-
return all_timings;
121-
} catch (const std::exception& e) {
122-
std::cerr << "Error in get_all_timings: " << e.what() << std::endl;
123-
return {};
124-
}
100+
auto& state = EnergyProfilerState::get_instance();
101+
std::lock_guard<std::mutex> lock(state.get_mutex());
102+
std::vector<TimingInfo> all_timings = state.get_completed_timings();
103+
std::sort(all_timings.begin(), all_timings.end(),
104+
[](const TimingInfo& a, const TimingInfo& b) {
105+
return a.start_time < b.start_time;
106+
});
107+
return all_timings;
125108
}
126109

127110
} // namespace EnergyProfiler

0 commit comments

Comments
 (0)