YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
StatsMonitor.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 "StatsMonitor.h"
7
8#include <map>
9#include <sstream>
10
11#include <yarp/os/Bottle.h>
13#include <yarp/os/LogStream.h>
14#include <yarp/os/Things.h>
16
17#include <yarp/stats/ConnectionStats.h>
18
19//yarp connect /mysender /myreceiver tcp+send.portmonitor+file.stats_monitor+type.dll
20
21// Monitor: log component
22namespace {
23YARP_LOG_COMPONENT(STATSMONITOR,
24 "yarp.portmonitor.StatsMonitor",
28 nullptr)
29} // namespace
30
31// Monitor: create
33{
34 m_isSender = options.find("sender_side").asBool();
35 m_source = options.find("source").asString();
36 m_destination = options.find("destination").asString();
37 m_carrier = options.find("carrier").asString();
38
39 if (!m_isSender) {
40 yCError(STATSMONITOR, "Attaching on receiver side is not supported yet.");
41 return false;
42 }
43
44 double period = 1.0;
45 m_stats_thread_forward = std::make_unique<StatsThread>(period, m_source, m_destination, m_carrier, true);
46 if (!m_stats_thread_forward->start())
47 {
48 return false;
49 }
50 m_stats_thread_backward = std::make_unique<StatsThread>(period, m_destination, m_source, m_carrier, false);
51 if (!m_stats_thread_backward->start())
52 {
53 return false;
54 }
55
56 return true;
57}
58
59// Monitor: update
61{
62 //serialize the data, compute its size
64 thing.write(writer);
65 size_t bytes = writer.dataSize();
66
67 //update statistics
68 m_stats_thread_forward->setData(bytes);
69
70 //propagate data transmission
71 return thing;
72}
73
74// Monitor: updateReply
76{
77 //serialize the data, compute its size
79 thing.write(writer);
80 size_t bytes = writer.dataSize();
81
82 //update statistics
83 m_stats_thread_backward->setData(bytes);
84
85 //propagate data transmission
86 return thing;
87}
88
89void StatsThread::setData(size_t datasize)
90{
91 std::lock_guard lock(m_mutex);
92 m_datasize_count++;
93 m_datasize_sum += datasize;
94
95 //yDebug() << datasize << " " << m_datasize_sum << " " << m_datasize_count;
96}
97
99{
100 double local_sum;
101 int local_count;
102 {
103 std::lock_guard lock(m_mutex);
104 local_sum = m_datasize_sum;
105 local_count = m_datasize_count;
106 m_datasize_count = 0;
107 m_datasize_sum = 0;
108 }
109
110 yarp::stats::ConnectionStats msg;
111 msg.timestamp = yarp::os::SystemClock::nowSystem();
112 msg.source = m_source;
113 msg.destination = m_destination;
114 msg.carrier = m_carrier;
115 msg.isForward = m_isForward;
116 msg.bytes_per_second = local_sum/m_period;
117 msg.frequency = local_count/m_period;
118 m_stats_port.write(msg);
119
120 //yDebug("Writing: %s", msg.toString().c_str());
121}
122
124{
125 if (!m_stats_port.open(m_stats_port_name))
126 {
127 yCError(STATSMONITOR, "Could not open port %s.", m_stats_port_name.c_str());
128 return false;
129 }
130 return true;
131}
132
134{
135 m_stats_port.close();
136}
yarp::os::Things & update(yarp::os::Things &thing) override
After data get accpeted in the accept() callback, an instance of that is given to the update function...
yarp::os::Things & updateReply(yarp::os::Things &thing) override
The updateReply makes it possible to modify a reply from a port when the portmonitor object is attach...
bool create(const yarp::os::Property &options) override
This will be called when the dll is properly loaded by the portmonitor carrier.
void run() override
Loop function.
bool threadInit() override
Initialization method.
void setData(size_t datasize)
void threadRelease() override
Release method.
static LogCallback printCallback()
Get current print callback.
Definition Log.cpp:892
static LogType minimumPrintLevel()
Get current minimum print level.
Definition Log.cpp:852
@ LogTypeReserved
Definition Log.h:98
bool write(const PortWriter &writer, const PortWriter *callback=nullptr) const override
Write an object to the port.
Definition Port.cpp:395
void close() override
Stop port activity.
Definition Port.cpp:330
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition Port.cpp:79
A class for storing options and configuration information.
Definition Property.h:32
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
static double nowSystem()
Base class for generic things.
Definition Things.h:18
bool write(yarp::os::ConnectionWriter &connection)
Definition Things.cpp:54
virtual bool asBool() const
Get boolean value.
Definition Value.cpp:186
virtual std::string asString() const
Get string value.
Definition Value.cpp:240
A helper for creating cached object descriptions.
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)