migration-stats.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #include "qemu/osdep.h"
  13. #include "qemu/stats64.h"
  14. #include "qemu-file.h"
  15. #include "trace.h"
  16. #include "migration-stats.h"
  17. MigrationAtomicStats mig_stats;
  18. bool migration_rate_exceeded(QEMUFile *f)
  19. {
  20. if (qemu_file_get_error(f)) {
  21. return true;
  22. }
  23. uint64_t rate_limit_max = migration_rate_get();
  24. if (rate_limit_max == RATE_LIMIT_DISABLED) {
  25. return false;
  26. }
  27. uint64_t rate_limit_start = stat64_get(&mig_stats.rate_limit_start);
  28. uint64_t rate_limit_current = migration_transferred_bytes();
  29. uint64_t rate_limit_used = rate_limit_current - rate_limit_start;
  30. if (rate_limit_max > 0 && rate_limit_used > rate_limit_max) {
  31. return true;
  32. }
  33. return false;
  34. }
  35. uint64_t migration_rate_get(void)
  36. {
  37. return stat64_get(&mig_stats.rate_limit_max);
  38. }
  39. #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY)
  40. void migration_rate_set(uint64_t limit)
  41. {
  42. /*
  43. * 'limit' is per second. But we check it each BUFFER_DELAY milliseconds.
  44. */
  45. stat64_set(&mig_stats.rate_limit_max, limit / XFER_LIMIT_RATIO);
  46. }
  47. void migration_rate_reset(void)
  48. {
  49. stat64_set(&mig_stats.rate_limit_start, migration_transferred_bytes());
  50. }
  51. uint64_t migration_transferred_bytes(void)
  52. {
  53. uint64_t multifd = stat64_get(&mig_stats.multifd_bytes);
  54. uint64_t rdma = stat64_get(&mig_stats.rdma_bytes);
  55. uint64_t qemu_file = stat64_get(&mig_stats.qemu_file_transferred);
  56. trace_migration_transferred_bytes(qemu_file, multifd, rdma);
  57. return qemu_file + multifd + rdma;
  58. }