xref: /openbmc/qemu/migration/migration-stats.c (revision 91e11db7)
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_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);
31 
32     if (rate_limit_max == RATE_LIMIT_DISABLED) {
33         return false;
34     }
35     if (rate_limit_max > 0 && rate_limit_used > rate_limit_max) {
36         return true;
37     }
38     return false;
39 }
40 
41 uint64_t migration_rate_get(void)
42 {
43     return stat64_get(&mig_stats.rate_limit_max);
44 }
45 
46 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
47 
48 void migration_rate_set(uint64_t limit)
49 {
50     /*
51      * 'limit' is per second.  But we check it each BUFFER_DELAY milliseconds.
52      */
53     stat64_set(&mig_stats.rate_limit_max, limit / XFER_LIMIT_RATIO);
54 }
55 
56 void migration_rate_reset(QEMUFile *f)
57 {
58     stat64_set(&mig_stats.rate_limit_start, migration_transferred_bytes(f));
59 }
60 
61 uint64_t migration_transferred_bytes(QEMUFile *f)
62 {
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);
66 
67     trace_migration_transferred_bytes(qemu_file, multifd, rdma);
68     return qemu_file + multifd + rdma;
69 }
70