1919#include "casync.h"
2020#include "def.h"
2121#include "realloc-buffer.h"
22+ #include "time-util.h"
2223#include "util.h"
2324
2425/* #undef EINVAL */
@@ -47,7 +48,7 @@ typedef enum CaCacheState {
4748
4849struct CaSync {
4950 CaDirection direction ;
50- bool started ;
51+ uint64_t start_nsec ;
5152
5253 CaEncoder * encoder ;
5354 CaDecoder * decoder ;
@@ -148,8 +149,13 @@ struct CaSync {
148149 uint64_t chunk_size_max ;
149150
150151 CaCompressionType compression_type ;
152+
153+ uint64_t first_chunk_request_nsec ;
154+ uint64_t last_chunk_request_nsec ;
151155};
152156
157+ #define CA_SYNC_IS_STARTED (s ) ((s)->start_nsec != 0)
158+
153159static CaSync * ca_sync_new (void ) {
154160 CaSync * s ;
155161
@@ -1281,7 +1287,7 @@ static int ca_sync_start(CaSync *s) {
12811287
12821288 assert (s );
12831289
1284- if (s -> started )
1290+ if (CA_SYNC_IS_STARTED ( s ) )
12851291 return 0 ;
12861292
12871293 if (s -> direction == CA_SYNC_ENCODE && s -> archive_path && s -> archive_fd < 0 ) {
@@ -1587,7 +1593,7 @@ static int ca_sync_start(CaSync *s) {
15871593 }
15881594
15891595 s -> cache_state = ca_sync_use_cache (s ) ? CA_SYNC_CACHE_CHECK : CA_SYNC_CACHE_OFF ;
1590- s -> started = true ;
1596+ s -> start_nsec = now ( CLOCK_MONOTONIC ) ;
15911597
15921598 return 1 ;
15931599}
@@ -2327,6 +2333,9 @@ static int ca_sync_process_decoder_request(CaSync *s) {
23272333 if (r < 0 )
23282334 return log_debug_errno (r , "Failed to put decoder EOF: %m" );
23292335
2336+ if (s -> last_chunk_request_nsec == 0 )
2337+ s -> last_chunk_request_nsec = now (CLOCK_MONOTONIC );
2338+
23302339 return CA_SYNC_STEP ;
23312340 }
23322341
@@ -2345,6 +2354,9 @@ static int ca_sync_process_decoder_request(CaSync *s) {
23452354 if (!ca_sync_seed_ready (s ))
23462355 return CA_SYNC_POLL ;
23472356
2357+ if (s -> first_chunk_request_nsec == 0 )
2358+ s -> first_chunk_request_nsec = now (CLOCK_MONOTONIC );
2359+
23482360 r = ca_sync_get (s , & s -> next_chunk , CA_CHUNK_UNCOMPRESSED , & p , & chunk_size , NULL , & origin );
23492361 if (r == - EAGAIN ) /* Don't have this right now, but requested it now */
23502362 return CA_SYNC_STEP ;
@@ -4252,6 +4264,31 @@ int ca_sync_get_seed_request_bytes(CaSync *s, uint64_t *ret) {
42524264 return 0 ;
42534265}
42544266
4267+ int ca_sync_get_seed_seeding_time_nsec (CaSync * s , uint64_t * ret ) {
4268+ uint64_t sum = 0 ;
4269+ size_t i ;
4270+ int r ;
4271+
4272+ if (!s )
4273+ return - EINVAL ;
4274+ if (!ret )
4275+ return - EINVAL ;
4276+
4277+ /* We can sum seeding times since seeds are processed one after another */
4278+ for (i = 0 ; i < s -> n_seeds ; i ++ ) {
4279+ uint64_t x ;
4280+
4281+ r = ca_seed_get_seeding_time_nsec (s -> seeds [i ], & x );
4282+ if (r < 0 )
4283+ return r ;
4284+
4285+ sum += x ;
4286+ }
4287+
4288+ * ret = sum ;
4289+ return 0 ;
4290+ }
4291+
42554292int ca_sync_get_local_requests (CaSync * s , uint64_t * ret ) {
42564293 uint64_t sum ;
42574294 size_t i ;
@@ -4376,14 +4413,42 @@ int ca_sync_get_remote_request_bytes(CaSync *s, uint64_t *ret) {
43764413 return 0 ;
43774414}
43784415
4416+ int ca_sync_get_decoding_time_nsec (CaSync * s , uint64_t * ret ) {
4417+ if (!s )
4418+ return - EINVAL ;
4419+ if (!ret )
4420+ return - EINVAL ;
4421+
4422+ if (s -> first_chunk_request_nsec == 0 || s -> last_chunk_request_nsec == 0 )
4423+ return - ENODATA ;
4424+ if (s -> first_chunk_request_nsec > s -> last_chunk_request_nsec )
4425+ return - ENODATA ;
4426+
4427+ * ret = s -> last_chunk_request_nsec - s -> first_chunk_request_nsec ;
4428+ return 0 ;
4429+ }
4430+
4431+ int ca_sync_get_runtime_nsec (CaSync * s , uint64_t * ret ) {
4432+ if (!s )
4433+ return - EINVAL ;
4434+ if (!ret )
4435+ return - EINVAL ;
4436+
4437+ if (s -> start_nsec == 0 )
4438+ return - ENODATA ;
4439+
4440+ * ret = now (CLOCK_MONOTONIC ) - s -> start_nsec ;
4441+ return 0 ;
4442+ }
4443+
43794444int ca_sync_set_compression_type (CaSync * s , CaCompressionType compression ) {
43804445 if (!s )
43814446 return - EINVAL ;
43824447 if (compression < 0 )
43834448 return - EINVAL ;
43844449 if (compression >= _CA_COMPRESSION_TYPE_MAX )
43854450 return - EOPNOTSUPP ;
4386- if (s -> started )
4451+ if (CA_SYNC_IS_STARTED ( s ) )
43874452 return - EBUSY ;
43884453
43894454 s -> compression_type = compression ;
0 commit comments