Browse Source

Counting bytes in tcp read write (#267)

* Counting bytes in tcp read write

* Bytes counter moved to separate class because of clang tidy

* Delete moves for ByteCounter for clang tidy

* Copyright headers for ByteCounter

* Default destructor for ByteCounter specified for clang tidy
pull/271/head
Boris 1 month ago
committed by GitHub
parent
commit
66764acb29
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 40
      include/libp2p/transport/tcp/bytes_counter.hpp
  2. 3
      include/libp2p/transport/tcp/tcp_connection.hpp
  3. 2
      src/transport/tcp/CMakeLists.txt
  4. 32
      src/transport/tcp/bytes_counter.cpp
  5. 11
      src/transport/tcp/tcp_connection.cpp

40
include/libp2p/transport/tcp/bytes_counter.hpp

@ -0,0 +1,40 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <atomic>
namespace libp2p::transport {
class ByteCounter {
private:
std::atomic<uint64_t> bytes_read_{0};
std::atomic<uint64_t> bytes_written_{0};
ByteCounter() = default;
public:
~ByteCounter() = default;
void incrementBytesRead(uint64_t bytes);
void incrementBytesWritten(uint64_t bytes);
uint64_t getBytesRead() const;
uint64_t getBytesWritten() const;
static ByteCounter &getInstance();
ByteCounter(const ByteCounter &) = delete;
ByteCounter &operator=(const ByteCounter &) = delete;
ByteCounter(ByteCounter &&) = delete;
ByteCounter &operator=(ByteCounter &&) = delete;
};
} // namespace libp2p::transport

3
include/libp2p/transport/tcp/tcp_connection.hpp

@ -91,6 +91,9 @@ namespace libp2p::transport {
return debug_str_;
}
static uint64_t getBytesRead();
static uint64_t getBytesWritten();
private:
outcome::result<void> saveMultiaddresses();

2
src/transport/tcp/CMakeLists.txt

@ -4,7 +4,7 @@
# SPDX-License-Identifier: Apache-2.0
#
libp2p_add_library(p2p_tcp_connection tcp_connection.cpp)
libp2p_add_library(p2p_tcp_connection tcp_connection.cpp bytes_counter.cpp)
target_link_libraries(p2p_tcp_connection
Boost::boost
p2p_multiaddress

32
src/transport/tcp/bytes_counter.cpp

@ -0,0 +1,32 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#include <libp2p/transport/tcp/bytes_counter.hpp>
namespace libp2p::transport {
ByteCounter &ByteCounter::getInstance() {
static ByteCounter instance;
return instance;
}
void ByteCounter::incrementBytesRead(uint64_t bytes) {
bytes_read_.fetch_add(bytes);
}
void ByteCounter::incrementBytesWritten(uint64_t bytes) {
bytes_written_.fetch_add(bytes);
}
uint64_t ByteCounter::getBytesRead() const {
return bytes_read_.load();
}
uint64_t ByteCounter::getBytesWritten() const {
return bytes_written_.load();
}
} // namespace libp2p::transport

11
src/transport/tcp/tcp_connection.cpp

@ -9,6 +9,7 @@
#include <libp2p/basic/read_return_size.hpp>
#include <libp2p/common/ambigous_size.hpp>
#include <libp2p/common/asio_buffer.hpp>
#include <libp2p/transport/tcp/bytes_counter.hpp>
#include <libp2p/transport/tcp/tcp_util.hpp>
#define TRACE_ENABLED 0
@ -184,6 +185,7 @@ namespace libp2p::transport {
void TcpConnection::readSome(BytesOut out,
size_t bytes,
TcpConnection::ReadCallbackFunc cb) {
ByteCounter::getInstance().incrementBytesRead(bytes);
ambigousSize(out, bytes);
TRACE("{} read some up to {}", debug_str_, bytes);
socket_.async_read_some(asioBuffer(out),
@ -193,6 +195,7 @@ namespace libp2p::transport {
void TcpConnection::writeSome(BytesIn in,
size_t bytes,
TcpConnection::WriteCallbackFunc cb) {
ByteCounter::getInstance().incrementBytesWritten(bytes);
ambigousSize(in, bytes);
TRACE("{} write some up to {}", debug_str_, bytes);
socket_.async_write_some(asioBuffer(in),
@ -269,4 +272,12 @@ namespace libp2p::transport {
return outcome::success();
}
uint64_t TcpConnection::getBytesRead() {
return ByteCounter::getInstance().getBytesRead();
}
uint64_t TcpConnection::getBytesWritten() {
return ByteCounter::getInstance().getBytesWritten();
}
} // namespace libp2p::transport

Loading…
Cancel
Save