xref: /openbmc/qemu/migration/migration-stats.c (revision e9206163)
1 /*
2  * Migration stats
3  *
4  * Copyright (c) 2012-2023 Red Hat Inc
5  *
6  * Authors:
7  *  Juan Quintela <quintela@redhat.com>
8  *
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.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "qemu/stats64.h"
15 #include "qemu-file.h"
16 #include "trace.h"
17 #include "migration-stats.h"
18 
19 MigrationAtomicStats mig_stats;
20 
21 bool migration_rate_exceeded(QEMUFile *f)
22 {
23     if (qemu_file_get_error(f)) {
24         return true;
25     }
26 
27     uint64_t rate_limit_max = migration_rate_get();
28     if (rate_limit_max == RATE_LIMIT_DISABLED) {
29         return false;
30     }
31 
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;
35 
36     if (rate_limit_max > 0 && rate_limit_used > rate_limit_max) {
37         return true;
38     }
39     return false;
40 }
41 
42 uint64_t migration_rate_get(void)
43 {
44     return stat64_get(&mig_stats.rate_limit_max);
45 }
46 
47 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
48 
49 void migration_rate_set(uint64_t limit)
50 {
51     /*
52      * 'limit' is per second.  But we check it each BUFFER_DELAY milliseconds.
53      */
54     stat64_set(&mig_stats.rate_limit_max, limit / XFER_LIMIT_RATIO);
55 }
56 
57 void migration_rate_reset(void)
58 {
59     stat64_set(&mig_stats.rate_limit_start, migration_transferred_bytes());
60 }
61 
62 uint64_t migration_transferred_bytes(void)
63 {
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);
67 
68     trace_migration_transferred_bytes(qemu_file, multifd, rdma);
69     return qemu_file + multifd + rdma;
70 }
71