page_cache.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Page cache for QEMU
  3. * The cache is base on a hash of the page address
  4. *
  5. * Copyright 2012 Red Hat, Inc. and/or its affiliates
  6. *
  7. * Authors:
  8. * Orit Wasserman <owasserm@redhat.com>
  9. *
  10. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  11. * See the COPYING file in the top-level directory.
  12. *
  13. */
  14. #include "qemu/osdep.h"
  15. #include "qapi/qmp/qerror.h"
  16. #include "qapi/error.h"
  17. #include "qemu/host-utils.h"
  18. #include "page_cache.h"
  19. #include "trace.h"
  20. /* the page in cache will not be replaced in two cycles */
  21. #define CACHED_PAGE_LIFETIME 2
  22. typedef struct CacheItem CacheItem;
  23. struct CacheItem {
  24. uint64_t it_addr;
  25. uint64_t it_age;
  26. uint8_t *it_data;
  27. };
  28. struct PageCache {
  29. CacheItem *page_cache;
  30. size_t page_size;
  31. size_t max_num_items;
  32. size_t num_items;
  33. };
  34. PageCache *cache_init(uint64_t new_size, size_t page_size, Error **errp)
  35. {
  36. int64_t i;
  37. size_t num_pages = new_size / page_size;
  38. PageCache *cache;
  39. if (new_size < page_size) {
  40. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
  41. "is smaller than one target page size");
  42. return NULL;
  43. }
  44. /* round down to the nearest power of 2 */
  45. if (!is_power_of_2(num_pages)) {
  46. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
  47. "is not a power of two number of pages");
  48. return NULL;
  49. }
  50. /* We prefer not to abort if there is no memory */
  51. cache = g_try_malloc(sizeof(*cache));
  52. if (!cache) {
  53. error_setg(errp, "Failed to allocate cache");
  54. return NULL;
  55. }
  56. cache->page_size = page_size;
  57. cache->num_items = 0;
  58. cache->max_num_items = num_pages;
  59. trace_migration_pagecache_init(cache->max_num_items);
  60. /* We prefer not to abort if there is no memory */
  61. cache->page_cache = g_try_malloc((cache->max_num_items) *
  62. sizeof(*cache->page_cache));
  63. if (!cache->page_cache) {
  64. error_setg(errp, "Failed to allocate page cache");
  65. g_free(cache);
  66. return NULL;
  67. }
  68. for (i = 0; i < cache->max_num_items; i++) {
  69. cache->page_cache[i].it_data = NULL;
  70. cache->page_cache[i].it_age = 0;
  71. cache->page_cache[i].it_addr = -1;
  72. }
  73. return cache;
  74. }
  75. void cache_fini(PageCache *cache)
  76. {
  77. int64_t i;
  78. g_assert(cache);
  79. g_assert(cache->page_cache);
  80. for (i = 0; i < cache->max_num_items; i++) {
  81. g_free(cache->page_cache[i].it_data);
  82. }
  83. g_free(cache->page_cache);
  84. cache->page_cache = NULL;
  85. g_free(cache);
  86. }
  87. static size_t cache_get_cache_pos(const PageCache *cache,
  88. uint64_t address)
  89. {
  90. g_assert(cache->max_num_items);
  91. return (address / cache->page_size) & (cache->max_num_items - 1);
  92. }
  93. static CacheItem *cache_get_by_addr(const PageCache *cache, uint64_t addr)
  94. {
  95. size_t pos;
  96. g_assert(cache);
  97. g_assert(cache->page_cache);
  98. pos = cache_get_cache_pos(cache, addr);
  99. return &cache->page_cache[pos];
  100. }
  101. uint8_t *get_cached_data(const PageCache *cache, uint64_t addr)
  102. {
  103. return cache_get_by_addr(cache, addr)->it_data;
  104. }
  105. bool cache_is_cached(const PageCache *cache, uint64_t addr,
  106. uint64_t current_age)
  107. {
  108. CacheItem *it;
  109. it = cache_get_by_addr(cache, addr);
  110. if (it->it_addr == addr) {
  111. /* update the it_age when the cache hit */
  112. it->it_age = current_age;
  113. return true;
  114. }
  115. return false;
  116. }
  117. int cache_insert(PageCache *cache, uint64_t addr, const uint8_t *pdata,
  118. uint64_t current_age)
  119. {
  120. CacheItem *it;
  121. /* actual update of entry */
  122. it = cache_get_by_addr(cache, addr);
  123. if (it->it_data && it->it_addr != addr &&
  124. it->it_age + CACHED_PAGE_LIFETIME > current_age) {
  125. /* the cache page is fresh, don't replace it */
  126. return -1;
  127. }
  128. /* allocate page */
  129. if (!it->it_data) {
  130. it->it_data = g_try_malloc(cache->page_size);
  131. if (!it->it_data) {
  132. trace_migration_pagecache_insert();
  133. return -1;
  134. }
  135. cache->num_items++;
  136. }
  137. memcpy(it->it_data, pdata, cache->page_size);
  138. it->it_age = current_age;
  139. it->it_addr = addr;
  140. return 0;
  141. }