page_cache.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "qemu-common.h"
  16. #include "qemu/host-utils.h"
  17. #include "migration/page_cache.h"
  18. #ifdef DEBUG_CACHE
  19. #define DPRINTF(fmt, ...) \
  20. do { fprintf(stdout, "cache: " fmt, ## __VA_ARGS__); } while (0)
  21. #else
  22. #define DPRINTF(fmt, ...) \
  23. do { } while (0)
  24. #endif
  25. /* the page in cache will not be replaced in two cycles */
  26. #define CACHED_PAGE_LIFETIME 2
  27. typedef struct CacheItem CacheItem;
  28. struct CacheItem {
  29. uint64_t it_addr;
  30. uint64_t it_age;
  31. uint8_t *it_data;
  32. };
  33. struct PageCache {
  34. CacheItem *page_cache;
  35. unsigned int page_size;
  36. int64_t max_num_items;
  37. uint64_t max_item_age;
  38. int64_t num_items;
  39. };
  40. PageCache *cache_init(int64_t num_pages, unsigned int page_size)
  41. {
  42. int64_t i;
  43. PageCache *cache;
  44. if (num_pages <= 0) {
  45. DPRINTF("invalid number of pages\n");
  46. return NULL;
  47. }
  48. /* We prefer not to abort if there is no memory */
  49. cache = g_try_malloc(sizeof(*cache));
  50. if (!cache) {
  51. DPRINTF("Failed to allocate cache\n");
  52. return NULL;
  53. }
  54. /* round down to the nearest power of 2 */
  55. if (!is_power_of_2(num_pages)) {
  56. num_pages = pow2floor(num_pages);
  57. DPRINTF("rounding down to %" PRId64 "\n", num_pages);
  58. }
  59. cache->page_size = page_size;
  60. cache->num_items = 0;
  61. cache->max_item_age = 0;
  62. cache->max_num_items = num_pages;
  63. DPRINTF("Setting cache buckets to %" PRId64 "\n", cache->max_num_items);
  64. /* We prefer not to abort if there is no memory */
  65. cache->page_cache = g_try_malloc((cache->max_num_items) *
  66. sizeof(*cache->page_cache));
  67. if (!cache->page_cache) {
  68. DPRINTF("Failed to allocate cache->page_cache\n");
  69. g_free(cache);
  70. return NULL;
  71. }
  72. for (i = 0; i < cache->max_num_items; i++) {
  73. cache->page_cache[i].it_data = NULL;
  74. cache->page_cache[i].it_age = 0;
  75. cache->page_cache[i].it_addr = -1;
  76. }
  77. return cache;
  78. }
  79. void cache_fini(PageCache *cache)
  80. {
  81. int64_t i;
  82. g_assert(cache);
  83. g_assert(cache->page_cache);
  84. for (i = 0; i < cache->max_num_items; i++) {
  85. g_free(cache->page_cache[i].it_data);
  86. }
  87. g_free(cache->page_cache);
  88. cache->page_cache = NULL;
  89. g_free(cache);
  90. }
  91. static size_t cache_get_cache_pos(const PageCache *cache,
  92. uint64_t address)
  93. {
  94. g_assert(cache->max_num_items);
  95. return (address / cache->page_size) & (cache->max_num_items - 1);
  96. }
  97. static CacheItem *cache_get_by_addr(const PageCache *cache, uint64_t addr)
  98. {
  99. size_t pos;
  100. g_assert(cache);
  101. g_assert(cache->page_cache);
  102. pos = cache_get_cache_pos(cache, addr);
  103. return &cache->page_cache[pos];
  104. }
  105. uint8_t *get_cached_data(const PageCache *cache, uint64_t addr)
  106. {
  107. return cache_get_by_addr(cache, addr)->it_data;
  108. }
  109. bool cache_is_cached(const PageCache *cache, uint64_t addr,
  110. uint64_t current_age)
  111. {
  112. CacheItem *it;
  113. it = cache_get_by_addr(cache, addr);
  114. if (it->it_addr == addr) {
  115. /* update the it_age when the cache hit */
  116. it->it_age = current_age;
  117. return true;
  118. }
  119. return false;
  120. }
  121. int cache_insert(PageCache *cache, uint64_t addr, const uint8_t *pdata,
  122. uint64_t current_age)
  123. {
  124. CacheItem *it;
  125. /* actual update of entry */
  126. it = cache_get_by_addr(cache, addr);
  127. if (it->it_data && it->it_addr != addr &&
  128. it->it_age + CACHED_PAGE_LIFETIME > current_age) {
  129. /* the cache page is fresh, don't replace it */
  130. return -1;
  131. }
  132. /* allocate page */
  133. if (!it->it_data) {
  134. it->it_data = g_try_malloc(cache->page_size);
  135. if (!it->it_data) {
  136. DPRINTF("Error allocating page\n");
  137. return -1;
  138. }
  139. cache->num_items++;
  140. }
  141. memcpy(it->it_data, pdata, cache->page_size);
  142. it->it_age = current_age;
  143. it->it_addr = addr;
  144. return 0;
  145. }
  146. int64_t cache_resize(PageCache *cache, int64_t new_num_pages)
  147. {
  148. PageCache *new_cache;
  149. int64_t i;
  150. CacheItem *old_it, *new_it;
  151. g_assert(cache);
  152. /* cache was not inited */
  153. if (cache->page_cache == NULL) {
  154. return -1;
  155. }
  156. /* same size */
  157. if (pow2floor(new_num_pages) == cache->max_num_items) {
  158. return cache->max_num_items;
  159. }
  160. new_cache = cache_init(new_num_pages, cache->page_size);
  161. if (!(new_cache)) {
  162. DPRINTF("Error creating new cache\n");
  163. return -1;
  164. }
  165. /* move all data from old cache */
  166. for (i = 0; i < cache->max_num_items; i++) {
  167. old_it = &cache->page_cache[i];
  168. if (old_it->it_addr != -1) {
  169. /* check for collision, if there is, keep MRU page */
  170. new_it = cache_get_by_addr(new_cache, old_it->it_addr);
  171. if (new_it->it_data && new_it->it_age >= old_it->it_age) {
  172. /* keep the MRU page */
  173. g_free(old_it->it_data);
  174. } else {
  175. if (!new_it->it_data) {
  176. new_cache->num_items++;
  177. }
  178. g_free(new_it->it_data);
  179. new_it->it_data = old_it->it_data;
  180. new_it->it_age = old_it->it_age;
  181. new_it->it_addr = old_it->it_addr;
  182. }
  183. }
  184. }
  185. g_free(cache->page_cache);
  186. cache->page_cache = new_cache->page_cache;
  187. cache->max_num_items = new_cache->max_num_items;
  188. cache->num_items = new_cache->num_items;
  189. g_free(new_cache);
  190. return cache->max_num_items;
  191. }