contrib/plugins: fix coverity warning in lockstep
[qemu/ar7.git] / migration / migration-stats.c
blob84e11e6dd8848f0bc24bd6a054fbecd7a64ae2d7
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_start = stat64_get(&mig_stats.rate_limit_start);
28 uint64_t rate_limit_current = migration_transferred_bytes(f);
29 uint64_t rate_limit_used = rate_limit_current - rate_limit_start;
30 uint64_t rate_limit_max = stat64_get(&mig_stats.rate_limit_max);
32 if (rate_limit_max == RATE_LIMIT_DISABLED) {
33 return false;
35 if (rate_limit_max > 0 && rate_limit_used > rate_limit_max) {
36 return true;
38 return false;
41 uint64_t migration_rate_get(void)
43 return stat64_get(&mig_stats.rate_limit_max);
46 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
48 void migration_rate_set(uint64_t limit)
51 * 'limit' is per second. But we check it each BUFFER_DELAY milliseconds.
53 stat64_set(&mig_stats.rate_limit_max, limit / XFER_LIMIT_RATIO);
56 void migration_rate_reset(QEMUFile *f)
58 stat64_set(&mig_stats.rate_limit_start, migration_transferred_bytes(f));
61 uint64_t migration_transferred_bytes(QEMUFile *f)
63 uint64_t multifd = stat64_get(&mig_stats.multifd_bytes);
64 uint64_t rdma = stat64_get(&mig_stats.rdma_bytes);
65 uint64_t qemu_file = qemu_file_transferred(f);
67 trace_migration_transferred_bytes(qemu_file, multifd, rdma);
68 return qemu_file + multifd + rdma;