22
33#include < spdlog/fmt/fmt.h>
44
5- #include < algorithm>
6-
75#include " logger/logger.hpp"
86#include " scheduler/scheduler.hpp"
97#include " utils/filesystem.hpp"
@@ -32,6 +30,7 @@ void Link::schedule_arrival(const Packet& packet) {
3230 m_id, packet.to_string ()));
3331 return ;
3432 }
33+ record_activity ();
3534
3635 if (empty_before_push) {
3736 start_head_packet_sending ();
@@ -137,11 +136,9 @@ void Link::transmit() {
137136 return ;
138137 }
139138
140- TimeNs current_time = Scheduler::get_instance (). get_current_time ();
139+ record_activity ();
141140
142- if (!m_ctx.m_first_activity_time .has_value ()) {
143- m_ctx.m_first_activity_time = current_time;
144- }
141+ TimeNs current_time = Scheduler::get_instance ().get_current_time ();
145142
146143 Scheduler::get_instance ().add (
147144 current_time + m_ctx.latency ,
@@ -163,14 +160,8 @@ void Link::arrive(const Packet& packet) {
163160 m_id, packet.to_string ()));
164161 return ;
165162 }
166-
163+ record_activity ();
167164 m_to.lock ()->notify_about_arrival ();
168- if (!m_ctx.m_last_activiti_time .has_value ()) {
169- m_ctx.m_last_activiti_time = std::make_optional<TimeNs>(0ul );
170- }
171- m_ctx.m_last_activiti_time .value () =
172- std::max (m_ctx.m_last_activiti_time .value (),
173- Scheduler::get_instance ().get_current_time ());
174165
175166 LOG_INFO (" Packet arrived to the next device. Packet: " +
176167 packet.to_string ());
@@ -183,49 +174,18 @@ void Link::start_head_packet_sending() {
183174 [link = shared_from_this ()]() { link->transmit (); });
184175}
185176
186- void Link::write_queue_metrics_to_csv (std::ofstream& out,
187- const LinkQueue& queue) const {
188- if (queue.get_type () == LinkQueueType::FromEgress) {
189- out << " Egress buffer (switch)\n " ;
190- } else {
191- out << " Ingress buffer (host)\n " ;
192- }
193-
194- out << " Maximal size"
195- << " , Average size"
196- << " , Peak size"
197- << " , Packets transmitted"
198- << " , Packets dropped"
199- << " , Drop percent\n " ;
200-
201- std::optional<SizeByte> average_opt =
202- queue.get_ctx ().size_statistics .get_mean ();
203- SizeByte average =
204- (average_opt.has_value () ? average_opt.value () : SizeByte (0ul ));
205- double drop_percent =
206- queue.get_ctx ().packets_dropped /
207- static_cast <double >(queue.get_ctx ().packets_dropped +
208- queue.get_ctx ().packets_transmitted );
209-
210- out << queue.get_ctx ().size << " , " << average << " , "
211- << queue.get_max_size () << " , " << queue.get_ctx ().packets_transmitted
212- << " , " << queue.get_ctx ().packets_dropped << " , " << drop_percent
213- << " \n\n " ;
214- }
215-
216177void Link::write_inner_metrics (std::filesystem::path output_dir) const {
217- std::filesystem::path output_path = output_dir / " metrics .csv" ;
178+ std::filesystem::path output_path = output_dir / " summary .csv" ;
218179 utils::create_all_directories (output_path);
219180 std::ofstream out (output_path);
220181 if (!out) {
221182 throw std::runtime_error (fmt::format (
222- " Failed to create file for summary: {}" , output_path.string ()));
183+ " Failed to create file for report of link '{}': (file path: {})" ,
184+ m_id, output_path.string ()));
223185 }
224186 write_thoughput_to_csv (out);
225-
226- write_queue_metrics_to_csv (out, m_to_ingress);
227-
228- write_queue_metrics_to_csv (out, m_from_egress);
187+ m_to_ingress.write_queue_metrics_to_csv (out);
188+ m_from_egress.write_queue_metrics_to_csv (out);
229189}
230190
231191void Link::write_thoughput_to_csv (std::ofstream& out) const {
@@ -236,28 +196,36 @@ void Link::write_thoughput_to_csv(std::ofstream& out) const {
236196 << " , Utilization"
237197 << " , Latency\n " ;
238198
239- if (!m_ctx.m_first_activity_time || !m_ctx. m_last_activiti_time ) {
199+ if (!m_ctx.activity_time . has_value () ) {
240200 out << m_ctx.speed << " , " << 0 << " , " << 0 << " , " << m_ctx.latency
241201 << " \n\n " ;
242202 return ;
243203 }
244204
245- TimeNs elapsed_time = m_ctx.m_last_activiti_time .value () -
246- m_ctx.m_first_activity_time .value ();
205+ TimeNs elapsed_time = m_ctx.activity_time .value ().active_time ();
247206
248- if (elapsed_time. value () == 0 ) {
207+ if (elapsed_time == TimeNs ( 0 ) ) {
249208 out << m_ctx.speed << " , " << 0 << " , " << 0 << " , " << m_ctx.latency
250209 << " \n\n " ;
251210 return ;
252211 }
253212
254- uint64_t actual = m_ctx.m_total_data . value () / elapsed_time. value () ;
213+ SpeedGbps actual = m_ctx.total_data_transferred / elapsed_time;
255214
256- uint64_t utilization =
257- (m_ctx.speed . value () == 0 ? 0 : actual / m_ctx.speed . value () );
215+ double utilization =
216+ (m_ctx.speed == SpeedGbps ( 0 ) ? 0.0 : actual / m_ctx.speed );
258217
259218 out << m_ctx.speed << " , " << actual << " , " << utilization << " , "
260219 << m_ctx.latency << " \n\n " ;
261220}
262221
222+ void Link::record_activity () {
223+ TimeNs now = Scheduler::get_instance ().get_current_time ();
224+ if (!m_ctx.activity_time .has_value ()) {
225+ m_ctx.activity_time = LinkContext::ActivityTime{now, now};
226+ } else {
227+ m_ctx.activity_time ->last = now;
228+ }
229+ }
230+
263231} // namespace sim
0 commit comments