hw/arm/smmuv3: Include missing 'hw/registerfields.h' header
[qemu/ar7.git] / migration / migration-stats.c
blobf690b98a03e76c1742a68f6fe02b881f4daae8b5
1 /*
2 * Migration stats
4 * Copyright (c) 2012-2023 Red Hat Inc
6 * Authors:
7 * Juan Quintela <quintela@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "qemu/stats64.h"
15 #include "qemu-file.h"
16 #include "trace.h"
17 #include "migration-stats.h"
19 MigrationAtomicStats mig_stats;
21 bool migration_rate_exceeded(QEMUFile *f)
23 if (qemu_file_get_error(f)) {
24 return true;
27 uint64_t rate_limit_max = migration_rate_get();
28 if (rate_limit_max == RATE_LIMIT_DISABLED) {
29 return false;
32 uint64_t rate_limit_start = stat64_get(&mig_stats.rate_limit_start);
33 uint64_t rate_limit_current = migration_transferred_bytes();
34 uint64_t rate_limit_used = rate_limit_current - rate_limit_start;
36 if (rate_limit_max > 0 && rate_limit_used > rate_limit_max) {
37 return true;
39 return false;
42 uint64_t migration_rate_get(void)
44 return stat64_get(&mig_stats.rate_limit_max);
47 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
49 void migration_rate_set(uint64_t limit)
52 * 'limit' is per second. But we check it each BUFFER_DELAY milliseconds.
54 stat64_set(&mig_stats.rate_limit_max, limit / XFER_LIMIT_RATIO);
57 void migration_rate_reset(void)
59 stat64_set(&mig_stats.rate_limit_start, migration_transferred_bytes());
62 uint64_t migration_transferred_bytes(void)
64 uint64_t multifd = stat64_get(&mig_stats.multifd_bytes);
65 uint64_t rdma = stat64_get(&mig_stats.rdma_bytes);
66 uint64_t qemu_file = stat64_get(&mig_stats.qemu_file_transferred);
68 trace_migration_transferred_bytes(qemu_file, multifd, rdma);
69 return qemu_file + multifd + rdma;