1313// You should have received a copy of the GNU General Public License
1414// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515
16+ #include < category/async/io.hpp>
1617#include < category/core/assert.h>
1718#include < category/core/basic_formatter.hpp>
1819#include < category/core/byte_string.hpp>
@@ -69,7 +70,8 @@ byte_string from_prefix(uint64_t const prefix, size_t const n_bytes)
6970
7071bool send_deletion (
7172 monad_statesync_server *const sync, monad_sync_request const &rq,
72- monad_statesync_server_context &ctx)
73+ monad_statesync_server_context &ctx, uint64_t *const num_upserts,
74+ uint64_t *const upsert_bytes)
7375{
7476 MONAD_ASSERT (
7577 rq.old_target <= rq.target || rq.old_target == INVALID_BLOCK_NUM);
@@ -78,8 +80,10 @@ bool send_deletion(
7880 return true ;
7981 }
8082
81- auto const fn = [sync, prefix = from_prefix (rq.prefix , rq.prefix_bytes )](
82- Deletion const &deletion) {
83+ auto const fn = [sync,
84+ prefix = from_prefix (rq.prefix , rq.prefix_bytes ),
85+ num_upserts,
86+ upsert_bytes](Deletion const &deletion) {
8387 auto const &[addr, key] = deletion;
8488 auto const hash = keccak256 (addr.bytes );
8589 byte_string_view const view{hash.bytes , sizeof (hash.bytes )};
@@ -94,6 +98,8 @@ bool send_deletion(
9498 sizeof (addr),
9599 nullptr ,
96100 0 );
101+ ++(*num_upserts);
102+ *upsert_bytes += sizeof (addr);
97103 }
98104 else {
99105 auto const skey = rlp::encode_bytes32_compact (key.value ());
@@ -104,6 +110,8 @@ bool send_deletion(
104110 sizeof (addr),
105111 skey.data (),
106112 skey.size ());
113+ ++(*num_upserts);
114+ *upsert_bytes += sizeof (addr) + skey.size ();
107115 }
108116 };
109117
@@ -118,6 +126,19 @@ bool send_deletion(
118126bool statesync_server_handle_request (
119127 monad_statesync_server *const sync, monad_sync_request const rq)
120128{
129+ uint64_t disk_ios_start = 0 ;
130+ uint64_t disk_bytes_start = 0 ;
131+ auto *const ctx = sync->context ;
132+ auto &db = *ctx->ro ;
133+ auto const *io = monad::async::AsyncIO::thread_instance ();
134+ if (io != nullptr ) {
135+ disk_ios_start = io->total_reads_submitted ();
136+ disk_bytes_start = io->total_bytes_read ();
137+ }
138+
139+ uint64_t num_upserts = 0 ;
140+ uint64_t upsert_bytes = 0 ;
141+
121142 struct Traverse final : public TraverseMachine
122143 {
123144 unsigned char nibble;
@@ -127,16 +148,21 @@ bool statesync_server_handle_request(
127148 NibblesView prefix;
128149 uint64_t from;
129150 uint64_t until;
151+ uint64_t *num_upserts;
152+ uint64_t *upsert_bytes;
130153
131154 Traverse (
132155 monad_statesync_server *const sync, NibblesView const prefix,
133- uint64_t const from, uint64_t const until)
156+ uint64_t const from, uint64_t const until,
157+ uint64_t *const num_upserts, uint64_t *const upsert_bytes)
134158 : nibble{INVALID_BRANCH}
135159 , depth{0 }
136160 , sync{sync}
137161 , prefix{prefix}
138162 , from{from}
139163 , until{until}
164+ , num_upserts{num_upserts}
165+ , upsert_bytes{upsert_bytes}
140166 {
141167 }
142168
@@ -188,13 +214,11 @@ bool statesync_server_handle_request(
188214 unsigned char const *const v1 =
189215 nullptr ,
190216 uint64_t const size1 = 0 ) {
217+ uint64_t const size2 = node.value ().size ();
191218 sync->statesync_server_send_upsert (
192- sync->net ,
193- type,
194- v1,
195- size1,
196- node.value ().data (),
197- node.value ().size ());
219+ sync->net , type, v1, size1, node.value ().data (), size2);
220+ ++(*num_upserts);
221+ *upsert_bytes += size1 + size2;
198222 };
199223
200224 if (nibble == CODE_NIBBLE) {
@@ -253,8 +277,6 @@ bool statesync_server_handle_request(
253277 };
254278
255279 [[maybe_unused]] auto const start = std::chrono::steady_clock::now ();
256- auto *const ctx = sync->context ;
257- auto &db = *ctx->ro ;
258280 if (rq.prefix < 256 && rq.target > rq.prefix ) {
259281 auto const version = rq.target - rq.prefix - 1 ;
260282 NodeCursor const root{db.load_root_for_version (version)};
@@ -275,9 +297,11 @@ bool statesync_server_handle_request(
275297 val.size (),
276298 nullptr ,
277299 0 );
300+ ++num_upserts;
301+ upsert_bytes += val.size ();
278302 }
279303
280- if (!send_deletion (sync, rq, *ctx)) {
304+ if (!send_deletion (sync, rq, *ctx, &num_upserts, &upsert_bytes )) {
281305 return false ;
282306 }
283307
@@ -297,12 +321,25 @@ bool statesync_server_handle_request(
297321 }
298322
299323 [[maybe_unused]] auto const begin = std::chrono::steady_clock::now ();
300- Traverse traverse (sync, NibblesView{bytes}, rq.from , rq.until );
324+ Traverse traverse (
325+ sync,
326+ NibblesView{bytes},
327+ rq.from ,
328+ rq.until ,
329+ &num_upserts,
330+ &upsert_bytes);
301331 if (!db.traverse (finalized_root, traverse, rq.target )) {
302332 return false ;
303333 }
304334 [[maybe_unused]] auto const end = std::chrono::steady_clock::now ();
305335
336+ uint64_t disk_ios_submitted = 0 ;
337+ uint64_t disk_bytes_total = 0 ;
338+ if (io != nullptr ) {
339+ disk_ios_submitted = io->total_reads_submitted () - disk_ios_start;
340+ disk_bytes_total = io->total_bytes_read () - disk_bytes_start;
341+ }
342+
306343 LOG_INFO (
307344 " processed request prefix={} prefix_bytes={} target={} from={} "
308345 " until={} "
@@ -316,6 +353,33 @@ bool statesync_server_handle_request(
316353 std::chrono::duration_cast<std::chrono::microseconds>(end - start),
317354 std::chrono::duration_cast<std::chrono::microseconds>(end - begin));
318355
356+ auto const elapsed_seconds =
357+ std::chrono::duration_cast<std::chrono::duration<double >>(end - start)
358+ .count ();
359+ double disk_ios_per_sec = 0.0 ;
360+ double disk_bytes_per_sec = 0.0 ;
361+ double upsert_bytes_per_sec = 0.0 ;
362+ if (elapsed_seconds > 0.0 ) {
363+ disk_ios_per_sec =
364+ static_cast <double >(disk_ios_submitted) / elapsed_seconds;
365+ disk_bytes_per_sec =
366+ static_cast <double >(disk_bytes_total) / elapsed_seconds;
367+ upsert_bytes_per_sec =
368+ static_cast <double >(upsert_bytes) / elapsed_seconds;
369+ }
370+
371+ LOG_INFO (
372+ " session metrics: disk_ios={} disk_bytes={} num_upserts={} "
373+ " upsert_bytes={} | disk_ios/s={:.1f} disk_bytes/s={:.1f} "
374+ " upsert_bytes/s={:.1f}" ,
375+ disk_ios_submitted,
376+ disk_bytes_total,
377+ num_upserts,
378+ upsert_bytes,
379+ disk_ios_per_sec,
380+ disk_bytes_per_sec,
381+ upsert_bytes_per_sec);
382+
319383 return true ;
320384}
321385
0 commit comments