Skip to content

Commit e009fbe

Browse files
authored
Merge pull request #192 from elboulangero/mr/extract-time-stats
casync extract: Add time statistics
2 parents 7821ec6 + 2cc73ed commit e009fbe

21 files changed

+324
-43
lines changed

src/cadecoder.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "reflink.h"
3030
#include "rm-rf.h"
3131
#include "siphash24.h"
32+
#include "time-util.h"
3233
#include "util.h"
3334

3435
/* #undef EINVAL */

src/caencoder.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "quota-projid.h"
4040
#include "realloc-buffer.h"
4141
#include "siphash24.h"
42+
#include "time-util.h"
4243
#include "util.h"
4344

4445
/* #undef EINVAL */

src/cafuse.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "cafuse.h"
1212
#include "notify.h"
1313
#include "signal-handler.h"
14+
#include "time-util.h"
1415
#include "util.h"
1516

1617
static CaSync *instance = NULL;

src/calocation.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <sys/stat.h>
77

88
#include "calocation.h"
9+
#include "time-util.h"
910
#include "util.h"
1011
#include "realloc-buffer.h"
1112

src/canbd.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <unistd.h>
1717

1818
#include "canbd.h"
19+
#include "time-util.h"
1920
#include "util.h"
2021

2122
#define NBD_MAX 1024

src/caremote.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "def.h"
1616
#include "realloc-buffer.h"
1717
#include "rm-rf.h"
18+
#include "time-util.h"
1819
#include "util.h"
1920

2021
/* #undef EBADMSG */

src/caseed.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "def.h"
1515
#include "realloc-buffer.h"
1616
#include "rm-rf.h"
17+
#include "time-util.h"
1718
#include "util.h"
1819

1920
/* #undef EINVAL */
@@ -48,6 +49,9 @@ struct CaSeed {
4849

4950
uint64_t n_requests;
5051
uint64_t n_request_bytes;
52+
53+
uint64_t first_step_nsec;
54+
uint64_t last_step_nsec;
5155
};
5256

5357
CaSeed *ca_seed_new(void) {
@@ -471,10 +475,16 @@ int ca_seed_step(CaSeed *s) {
471475
return -EALREADY;
472476

473477
if (!s->cache_chunks && !s->cache_hardlink) {
478+
if (s->last_step_nsec == 0)
479+
s->last_step_nsec = now(CLOCK_MONOTONIC);
480+
474481
s->ready = true;
475482
return CA_SEED_READY;
476483
}
477484

485+
if (s->first_step_nsec == 0)
486+
s->first_step_nsec = now(CLOCK_MONOTONIC);
487+
478488
r = ca_seed_open(s);
479489
if (r < 0)
480490
return r;
@@ -494,6 +504,9 @@ int ca_seed_step(CaSeed *s) {
494504
if (r < 0)
495505
return r;
496506

507+
if (s->last_step_nsec == 0)
508+
s->last_step_nsec = now(CLOCK_MONOTONIC);
509+
497510
s->ready = true;
498511
return CA_SEED_READY;
499512

@@ -925,3 +938,21 @@ int ca_seed_get_request_bytes(CaSeed *s, uint64_t *ret) {
925938
*ret = s->n_request_bytes;
926939
return 0;
927940
}
941+
942+
int ca_seed_get_seeding_time_nsec(CaSeed *s, uint64_t *ret) {
943+
if (!s)
944+
return -EINVAL;
945+
if (!ret)
946+
return -EINVAL;
947+
948+
if (!s->cache_chunks)
949+
return -ENOTTY;
950+
951+
if (s->first_step_nsec == 0 || s->last_step_nsec == 0)
952+
return -ENODATA;
953+
if (s->first_step_nsec > s->last_step_nsec)
954+
return -ENODATA;
955+
956+
*ret = s->last_step_nsec - s->first_step_nsec;
957+
return 0;
958+
}

src/caseed.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@ int ca_seed_get_file_root(CaSeed *s, CaFileRoot **ret);
4747

4848
int ca_seed_get_requests(CaSeed *s, uint64_t *ret);
4949
int ca_seed_get_request_bytes(CaSeed *s, uint64_t *ret);
50+
int ca_seed_get_seeding_time_nsec(CaSeed *s, uint64_t *ret);
5051

5152
#endif

src/casync-tool.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "notify.h"
2929
#include "parse-util.h"
3030
#include "signal-handler.h"
31+
#include "time-util.h"
3132
#include "util.h"
3233

3334
#if HAVE_UDEV
@@ -997,6 +998,7 @@ static int verbose_print_done_extract(CaSync *s) {
997998
uint64_t n_local_requests = UINT64_MAX, n_seed_requests = UINT64_MAX, n_remote_requests = UINT64_MAX;
998999
uint64_t n_local_bytes = UINT64_MAX, n_seed_bytes = UINT64_MAX, n_remote_bytes = UINT64_MAX;
9991000
uint64_t total_requests = 0, total_bytes = 0;
1001+
uint64_t nsec = 0, runtime_nsec = 0;
10001002
int r;
10011003

10021004
if (!arg_verbose)
@@ -1105,6 +1107,34 @@ static int verbose_print_done_extract(CaSync *s) {
11051107
format_bytes(buffer, sizeof(buffer), n_remote_bytes),
11061108
total_bytes > 0 ? n_remote_bytes * 100U / total_bytes : 0);
11071109

1110+
r = ca_sync_get_runtime_nsec(s, &runtime_nsec);
1111+
if (!IN_SET(r, -ENODATA)) {
1112+
if (r < 0)
1113+
return log_error_errno(r, "Failed to determine runtime: %m");
1114+
}
1115+
1116+
r = ca_sync_get_seed_seeding_time_nsec(s, &nsec);
1117+
if (!IN_SET(r, -ENODATA, -ENOTTY)) {
1118+
if (r < 0)
1119+
return log_error_errno(r, "Failed to determine seeding time: %m");
1120+
1121+
log_info("Time spent seeding: %s (%" PRIu64 "%%)",
1122+
format_timespan(buffer, sizeof(buffer), nsec, NSEC_PER_MSEC),
1123+
runtime_nsec > 0 ? nsec * 100U / runtime_nsec : 0);
1124+
}
1125+
1126+
r = ca_sync_get_decoding_time_nsec(s, &nsec);
1127+
if (!IN_SET(r, -ENODATA, -ENOTTY)) {
1128+
if (r < 0)
1129+
return log_error_errno(r, "Failed to determine decoding time: %m");
1130+
1131+
log_info("Time spent decoding: %s (%" PRIu64 "%%)",
1132+
format_timespan(buffer, sizeof(buffer), nsec, NSEC_PER_MSEC),
1133+
runtime_nsec > 0 ? nsec * 100U / runtime_nsec : 0);
1134+
}
1135+
1136+
log_info("Total extract time: %s", format_timespan(buffer, sizeof(buffer), runtime_nsec, NSEC_PER_MSEC));
1137+
11081138
return 1;
11091139
}
11101140

src/casync.c

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
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

4849
struct 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+
153159
static 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+
42554292
int 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+
43794444
int 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

Comments
 (0)