YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026-2026 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include <yarp/os/Network.h>
7#include <yarp/os/LogStream.h>
9#include <yarp/os/RFModule.h>
10#include <yarp/stats/Stats.h>
11
12#include <iomanip>
13#include <iostream>
14#include <sstream>
15
16using namespace std;
17using namespace yarp::os;
18
19namespace
20{
21 YARP_LOG_COMPONENT(YARPCONNINFO, "yarp.yarpconnectionsstats")
22}
23
24//---------------------------------------------------------------
25
27{
28 void display_help ()
29 {
30 yCInfo(YARPCONNINFO) << "This is the tool yarpconnectionsstats, a tool that displays the frequency at which YARP ports broadcast data";
31 yCInfo(YARPCONNINFO) << "";
32 yCInfo(YARPCONNINFO) << "In order to work properly, the connection we want to inspect must be monitored by a `stats_monitor` portmonitor.";
33 yCInfo(YARPCONNINFO) << "e.g. yarp connect /sender /recv tcp+send.portmonitor+file.stats_monitor+type.dll";
34 yCInfo(YARPCONNINFO) << "";
35 yCInfo(YARPCONNINFO) << "Additional options:";
36 yCInfo(YARPCONNINFO) << "--display_backward (default: false) if enabled";
37 yCInfo(YARPCONNINFO) << "--display_zero_bps (default: true) if enabled, displays also if the connection is not transmitting anything";
38 yCInfo(YARPCONNINFO) << "--display_by_source_name order results by source port name";
39 yCInfo(YARPCONNINFO) << "--display_by_dest_name order results by destination port name";
40 yCInfo(YARPCONNINFO) << "--display_by_bps order results by data size";
41 yCInfo(YARPCONNINFO) << "--display_screen_frequency (default value: 1) refresh frequency";
42 }
43
44 enum class SortMode
45 {
46 Source,
47 Destination,
48 BytesPerSecondDesc
49 };
50
51 bool display_backward = false;
52 bool display_zero_bps = true;
53 bool display_by_source_name = false;
54 bool display_by_dest_name = false;
55 bool display_by_bps = false;
56 double display_screen_frequency = 1.0;
57
58 yarp::stats::StatsEngine m_engine;
59 std::list<std::string> m_ports;
60 yarp::stats::StatsEngine::PortInfoMap m_portsmap;
61
62 bool configure(yarp::os::ResourceFinder &rf) override
63 {
64 if (rf.check("help")) { display_help(); return false; }
65 if (rf.check("display_backward")) { display_backward = rf.find("display_backward").asBool(); }
66 if (rf.check("display_zero_bps")) { display_zero_bps = rf.find("display_zero_bps").asBool(); }
67 if (rf.check("display_by_source_name")) { display_by_source_name = rf.find("display_by_source_name").asBool(); }
68 if (rf.check("display_by_dest_name")) { display_by_dest_name = rf.find("display_by_dest_name").asBool(); }
69 if (rf.check("display_by_bps")) { display_by_bps = rf.find("display_by_bps").asBool(); }
70 if (rf.check("display_screen_frequency")) { display_screen_frequency = rf.find("display_screen_frequency").asFloat64(); }
71
72 m_engine.m_display_backward = display_backward;
73 m_engine.m_display_zero_bps = display_zero_bps;
74
75 m_engine.discover(m_ports, m_portsmap);
76 m_engine.connect(m_ports);
77
78 return true;
79 }
80
81 bool close() override
82 {
83 m_engine.disconnect();
84 return true;
85 }
86
87 double getPeriod() override
88 {
89 return display_screen_frequency;
90 }
91
92 bool updateModule() override
93 {
94 std::cout << "\033[2J\033[H"; //clear the screen
95
96 std::list<yarp::stats::ConnectionStats> stats;
97 m_engine.update(stats);
98
99 yCDebug(YARPCONNINFO) << "Processed data:" << stats.size();
100
101 // Compute total bytes per second for each source/destination IP pair
102 std::map<std::pair<std::string, std::string>, double> ip_pair_bytes;
103 for (const auto& stat : stats)
104 {
105 // Get IP addresses from port names
106 std::string source_ip;
107 std::string dest_ip;
108
109 auto source_it = m_portsmap.find(stat.source);
110 if (source_it != m_portsmap.end()) {
111 source_ip = source_it->second.ip;
112 }
113 auto dest_it = m_portsmap.find(stat.destination);
114 if (dest_it != m_portsmap.end()) {
115 dest_ip = dest_it->second.ip;
116 }
117
118 // Accumulate bytes per second for this IP pair
119 if (!source_ip.empty() && !dest_ip.empty()) {
120 auto ip_pair = std::make_pair(source_ip, dest_ip);
121 ip_pair_bytes[ip_pair] += stat.bytes_per_second;
122 }
123 }
124
125 // Sort the stats based on the selected sorting mode
126 if (display_by_source_name) {sortConnections(stats, SortMode::Source);}
127 else if (display_by_dest_name) {sortConnections(stats, SortMode::Destination);}
128 else if (display_by_bps) {sortConnections(stats, SortMode::BytesPerSecondDesc);}
129
130 //Print the stats on screen
131 yCInfo(YARPCONNINFO) << "=== Connection statistics ===";
132 size_t longest_source = 0;
133 size_t longest_destination = 0;
134 for (auto it=stats.begin(); it!=stats.end(); it++)
135 {
136 if (it->source.length() > longest_source) { longest_source = it->source.length();}
137 if (it->destination.length() > longest_destination) { longest_destination = it->destination.length(); }
138 }
139 for (auto it=stats.begin(); it!=stats.end(); it++)
140 {
141 std::ostringstream line;
142
143 line << std::left
144 << std::setw(longest_source+2) << it->source
145 << " -> "
146 << std::setw(longest_destination+2) << it->destination
147 << " | "
148 << std::right
149 << std::setw(12) << it->bytes_per_second << " B/s"
150 << " | "
151 << std::setw(8) << it->frequency << " pkt/s"
152 << " | "
153 // << (it->isForward ? "forward" : "backward")
154 // << " | "
155 << std::setw(8) << it->carrier
156 << " | "
157 << std::setw(8) << std::fixed << std::setprecision(3) << it->timestamp;
158
159 yInfo() << line.str();
160 }
161
162 // Print IP pair totals
164 yCInfo(YARPCONNINFO) << "=== Bytes per second by IP address pairs ===";
165 for (const auto& [ip_pair, total_bytes] : ip_pair_bytes)
166 {
167 std::ostringstream line;
168 line << std::left
169 << std::setw(15) << ip_pair.first
170 << " -> "
171 << std::setw(15) << ip_pair.second
172 << " | "
173 << std::right
174 << std::setw(12) << total_bytes << " B/s";
175 yInfo() << line.str();
176 }
177
178 return true;
179 }
180
181 void sortConnections(std::list<yarp::stats::ConnectionStats>& connections, SortMode mode)
182 {
183 switch (mode)
184 {
185 case SortMode::Source:
186 connections.sort(
187 [](const auto& a, const auto& b)
188 {
189 return a.source < b.source;
190 });
191 break;
192
193 case SortMode::Destination:
194 connections.sort(
195 [](const auto& a, const auto& b)
196 {
197 return a.destination < b.destination;
198 });
199 break;
200
201 case SortMode::BytesPerSecondDesc:
202 connections.sort(
203 [](const auto& a, const auto& b)
204 {
205 return a.bytes_per_second > b.bytes_per_second;
206 });
207 break;
208 }
209 }
210};
211
212//---------------------------------------------------------------
213
214int main(int argc, char *argv[])
215{
217 if (!yarp.checkNetwork())
218 {
219 fprintf(stderr,"ERROR: check YARP network.\n");
220 return -1;
221 }
222
224 //rf.setDefaultConfigFile("yarpconnectionsstats.ini"); //overridden by --from parameter
225 //rf.setDefaultContext("yarpconnectionsstats"); //overridden by --context parameter
226 rf.configure(argc,argv);
227
229 return statsm.runModule(rf);
230}
#define yInfo(...)
Definition Log.h:319
A mini-server for performing network communication in the background.
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition Network.h:718
A base-class for standard YARP modules that supports ResourceFinder.
Definition RFModule.h:20
Helper class for finding config files and other external resources.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
bool configure(int argc, char *argv[], bool skipFirstArgument=true)
Sets up the ResourceFinder.
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
Definition Value.cpp:228
virtual bool asBool() const
Get boolean value.
Definition Value.cpp:192
int main(int argc, char *argv[])
Definition main.cpp:214
struct stats_struct stats
#define yCInfo(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
STL namespace.
An interface to the operating system, including Port based communication.
Definition OSTestMsg.cpp:13
int stat(const char *path)
Portable wrapper for the stat() function.
Definition Os.cpp:78
The main, catch-all namespace for YARP.
Definition dirs.h:16