Skip to content

Commit 66764ac

Browse files
authored
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
1 parent 55a46f3 commit 66764ac

5 files changed

Lines changed: 87 additions & 1 deletion

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright Quadrivium LLC
3+
* All Rights Reserved
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
9+
#include <atomic>
10+
11+
namespace libp2p::transport {
12+
13+
class ByteCounter {
14+
private:
15+
std::atomic<uint64_t> bytes_read_{0};
16+
std::atomic<uint64_t> bytes_written_{0};
17+
18+
ByteCounter() = default;
19+
20+
public:
21+
~ByteCounter() = default;
22+
23+
void incrementBytesRead(uint64_t bytes);
24+
25+
void incrementBytesWritten(uint64_t bytes);
26+
27+
uint64_t getBytesRead() const;
28+
29+
uint64_t getBytesWritten() const;
30+
31+
static ByteCounter &getInstance();
32+
33+
ByteCounter(const ByteCounter &) = delete;
34+
ByteCounter &operator=(const ByteCounter &) = delete;
35+
36+
ByteCounter(ByteCounter &&) = delete;
37+
ByteCounter &operator=(ByteCounter &&) = delete;
38+
};
39+
40+
} // namespace libp2p::transport

include/libp2p/transport/tcp/tcp_connection.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ namespace libp2p::transport {
9191
return debug_str_;
9292
}
9393

94+
static uint64_t getBytesRead();
95+
static uint64_t getBytesWritten();
96+
9497
private:
9598
outcome::result<void> saveMultiaddresses();
9699

src/transport/tcp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# SPDX-License-Identifier: Apache-2.0
55
#
66

7-
libp2p_add_library(p2p_tcp_connection tcp_connection.cpp)
7+
libp2p_add_library(p2p_tcp_connection tcp_connection.cpp bytes_counter.cpp)
88
target_link_libraries(p2p_tcp_connection
99
Boost::boost
1010
p2p_multiaddress
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright Quadrivium LLC
3+
* All Rights Reserved
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <libp2p/transport/tcp/bytes_counter.hpp>
8+
9+
namespace libp2p::transport {
10+
11+
ByteCounter &ByteCounter::getInstance() {
12+
static ByteCounter instance;
13+
return instance;
14+
}
15+
16+
void ByteCounter::incrementBytesRead(uint64_t bytes) {
17+
bytes_read_.fetch_add(bytes);
18+
}
19+
20+
void ByteCounter::incrementBytesWritten(uint64_t bytes) {
21+
bytes_written_.fetch_add(bytes);
22+
}
23+
24+
uint64_t ByteCounter::getBytesRead() const {
25+
return bytes_read_.load();
26+
}
27+
28+
uint64_t ByteCounter::getBytesWritten() const {
29+
return bytes_written_.load();
30+
}
31+
32+
} // namespace libp2p::transport

src/transport/tcp/tcp_connection.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <libp2p/basic/read_return_size.hpp>
1010
#include <libp2p/common/ambigous_size.hpp>
1111
#include <libp2p/common/asio_buffer.hpp>
12+
#include <libp2p/transport/tcp/bytes_counter.hpp>
1213
#include <libp2p/transport/tcp/tcp_util.hpp>
1314

1415
#define TRACE_ENABLED 0
@@ -184,6 +185,7 @@ namespace libp2p::transport {
184185
void TcpConnection::readSome(BytesOut out,
185186
size_t bytes,
186187
TcpConnection::ReadCallbackFunc cb) {
188+
ByteCounter::getInstance().incrementBytesRead(bytes);
187189
ambigousSize(out, bytes);
188190
TRACE("{} read some up to {}", debug_str_, bytes);
189191
socket_.async_read_some(asioBuffer(out),
@@ -193,6 +195,7 @@ namespace libp2p::transport {
193195
void TcpConnection::writeSome(BytesIn in,
194196
size_t bytes,
195197
TcpConnection::WriteCallbackFunc cb) {
198+
ByteCounter::getInstance().incrementBytesWritten(bytes);
196199
ambigousSize(in, bytes);
197200
TRACE("{} write some up to {}", debug_str_, bytes);
198201
socket_.async_write_some(asioBuffer(in),
@@ -269,4 +272,12 @@ namespace libp2p::transport {
269272
return outcome::success();
270273
}
271274

275+
uint64_t TcpConnection::getBytesRead() {
276+
return ByteCounter::getInstance().getBytesRead();
277+
}
278+
279+
uint64_t TcpConnection::getBytesWritten() {
280+
return ByteCounter::getInstance().getBytesWritten();
281+
}
282+
272283
} // namespace libp2p::transport

0 commit comments

Comments
 (0)