2
0

page_cache.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #ifdef DEBUG_CACHE
  20. #define DPRINTF(fmt, ...) \
  21. do { fprintf(stdout, "cache: " fmt, ## __VA_ARGS__); } while (0)
  22. #else
  23. #define DPRINTF(fmt, ...) \
  24. do { } while (0)
  25. #endif
  26. /* the page in cache will not be replaced in two cycles */
  27. #define CACHED_PAGE_LIFETIME 2
  28. typedef struct CacheItem CacheItem;
  29. struct CacheItem {
  30. uint64_t it_addr;
  31. uint64_t it_age;
  32. uint8_t *it_data;
  33. };
  34. struct PageCache {
  35. CacheItem *page_cache;
  36. size_t page_size;
  37. size_t max_num_items;
  38. size_t num_items;
  39. };
  40. PageCache *cache_init(int64_t new_size, size_t page_size, Error **errp)
  41. {
  42. int64_t i;
  43. size_t num_pages = new_size / page_size;
  44. PageCache *cache;
  45. if (new_size < page_size) {
  46. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
  47. "is smaller than one target page size");
  48. return NULL;
  49. }
  50. /* round down to the nearest power of 2 */
  51. if (!is_power_of_2(num_pages)) {
  52. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
  53. "is not a power of two number of pages");
  54. return NULL;
  55. }
  56. /* We prefer not to abort if there is no memory */
  57. cache = g_try_malloc(sizeof(*cache));
  58. if (!cache) {
  59. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
  60. "Failed to allocate cache");
  61. return NULL;
  62. }
  63. cache->page_size = page_size;
  64. cache->num_items = 0;
  65. cache->max_num_items = num_pages;
  66. DPRINTF("Setting cache buckets to %" PRId64 "\n", cache->max_num_items);
  67. /* We prefer not to abort if there is no memory */
  68. cache->page_cache = g_try_malloc((cache->max_num_items) *
  69. sizeof(*cache->page_cache));
  70. if (!cache->page_cache) {
  71. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
  72. "Failed to allocate page cache");
  73. g_free(cache);
  74. return NULL;
  75. }
  76. for (i = 0; i < cache->max_num_items; i++) {
  77. cache->page_cache[i].it_data = NULL;
  78. cache->page_cache[i].it_age = 0;
  79. cache->page_cache[i].it_addr = -1;
  80. }
  81. return cache;
  82. }
  83. void cache_fini(PageCache *cache)
  84. {
  85. int64_t i;
  86. g_assert(cache);
  87. g_assert(cache->page_cache);
  88. for (i = 0; i < cache->max_num_items; i++) {
  89. g_free(cache->page_cache[i].it_data);
  90. }
  91. g_free(cache->page_cache);
  92. cache->page_cache = NULL;
  93. g_free(cache);
  94. }
  95. static size_t cache_get_cache_pos(const PageCache *cache,
  96. uint64_t address)
  97. {
  98. g_assert(cache->max_num_items);
  99. return (address / cache->page_size) & (cache->max_num_items - 1);
  100. }
  101. static CacheItem *cache_get_by_addr(const PageCache *cache, uint64_t addr)
  102. {
  103. size_t pos;
  104. g_assert(cache);
  105. g_assert(cache->page_cache);
  106. pos = cache_get_cache_pos(cache, addr);
  107. return &cache->page_cache[pos];
  108. }
  109. uint8_t *get_cached_data(const PageCache *cache, uint64_t addr)
  110. {
  111. return cache_get_by_addr(cache, addr)->it_data;
  112. }
  113. bool cache_is_cached(const PageCache *cache, uint64_t addr,
  114. uint64_t current_age)
  115. {
  116. CacheItem *it;
  117. it = cache_get_by_addr(cache, addr);
  118. if (it->it_addr == addr) {
  119. /* update the it_age when the cache hit */
  120. it->it_age = current_age;
  121. return true;
  122. }
  123. return false;
  124. }
  125. int cache_insert(PageCache *cache, uint64_t addr, const uint8_t *pdata,
  126. uint64_t current_age)
  127. {
  128. CacheItem *it;
  129. /* actual update of entry */
  130. it = cache_get_by_addr(cache, addr);
  131. if (it->it_data && it->it_addr != addr &&
  132. it->it_age + CACHED_PAGE_LIFETIME > current_age) {
  133. /* the cache page is fresh, don't replace it */
  134. return -1;
  135. }
  136. /* allocate page */
  137. if (!it->it_data) {
  138. it->it_data = g_try_malloc(cache->page_size);
  139. if (!it->it_data) {
  140. DPRINTF("Error allocating page\n");
  141. return -1;
  142. }
  143. cache->num_items++;
  144. }
  145. memcpy(it->it_data, pdata, cache->page_size);
  146. it->it_age = current_age;
  147. it->it_addr = addr;
  148. return 0;
  149. }