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 "migration-stats.h" 17 18 MigrationAtomicStats mig_stats; 19 20 bool migration_rate_exceeded(QEMUFile *f) 21 { 22 if (qemu_file_get_error(f)) { 23 return true; 24 } 25 26 uint64_t rate_limit_used = stat64_get(&mig_stats.rate_limit_used); 27 uint64_t rate_limit_max = stat64_get(&mig_stats.rate_limit_max); 28 29 if (rate_limit_max == RATE_LIMIT_DISABLED) { 30 return false; 31 } 32 if (rate_limit_max > 0 && rate_limit_used > rate_limit_max) { 33 return true; 34 } 35 return false; 36 } 37 38 uint64_t migration_rate_get(void) 39 { 40 return stat64_get(&mig_stats.rate_limit_max); 41 } 42 43 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY) 44 45 void migration_rate_set(uint64_t limit) 46 { 47 /* 48 * 'limit' is per second. But we check it each BUFER_DELAY miliseconds. 49 */ 50 stat64_set(&mig_stats.rate_limit_max, limit / XFER_LIMIT_RATIO); 51 } 52 53 void migration_rate_reset(void) 54 { 55 stat64_set(&mig_stats.rate_limit_used, 0); 56 } 57 58 void migration_rate_account(uint64_t len) 59 { 60 stat64_add(&mig_stats.rate_limit_used, len); 61 } 62