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