/src/qemu/migration/page_cache.c
Line | Count | Source |
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 | | |
15 | | #include "qemu/osdep.h" |
16 | | |
17 | | #include "qapi/qmp/qerror.h" |
18 | | #include "qapi/error.h" |
19 | | #include "qemu/host-utils.h" |
20 | | #include "page_cache.h" |
21 | | #include "trace.h" |
22 | | |
23 | | /* the page in cache will not be replaced in two cycles */ |
24 | 0 | #define CACHED_PAGE_LIFETIME 2 |
25 | | |
26 | | typedef struct CacheItem CacheItem; |
27 | | |
28 | | struct CacheItem { |
29 | | uint64_t it_addr; |
30 | | uint64_t it_age; |
31 | | uint8_t *it_data; |
32 | | }; |
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 | | |
41 | | PageCache *cache_init(uint64_t new_size, size_t page_size, Error **errp) |
42 | 0 | { |
43 | 0 | int64_t i; |
44 | 0 | size_t num_pages = new_size / page_size; |
45 | 0 | PageCache *cache; |
46 | |
|
47 | 0 | if (new_size < page_size) { |
48 | 0 | error_setg(errp, "cache size is smaller than target page size"); |
49 | 0 | return NULL; |
50 | 0 | } |
51 | | |
52 | | /* round down to the nearest power of 2 */ |
53 | 0 | if (!is_power_of_2(num_pages)) { |
54 | 0 | error_setg(errp, "number of pages is not a power of two"); |
55 | 0 | return NULL; |
56 | 0 | } |
57 | | |
58 | | /* We prefer not to abort if there is no memory */ |
59 | 0 | cache = g_try_malloc(sizeof(*cache)); |
60 | 0 | if (!cache) { |
61 | 0 | error_setg(errp, "Failed to allocate cache"); |
62 | 0 | return NULL; |
63 | 0 | } |
64 | 0 | cache->page_size = page_size; |
65 | 0 | cache->num_items = 0; |
66 | 0 | cache->max_num_items = num_pages; |
67 | |
|
68 | 0 | trace_migration_pagecache_init(cache->max_num_items); |
69 | | |
70 | | /* We prefer not to abort if there is no memory */ |
71 | 0 | cache->page_cache = g_try_malloc((cache->max_num_items) * |
72 | 0 | sizeof(*cache->page_cache)); |
73 | 0 | if (!cache->page_cache) { |
74 | 0 | error_setg(errp, "Failed to allocate page cache"); |
75 | 0 | g_free(cache); |
76 | 0 | return NULL; |
77 | 0 | } |
78 | | |
79 | 0 | for (i = 0; i < cache->max_num_items; i++) { |
80 | 0 | cache->page_cache[i].it_data = NULL; |
81 | 0 | cache->page_cache[i].it_age = 0; |
82 | 0 | cache->page_cache[i].it_addr = -1; |
83 | 0 | } |
84 | |
|
85 | 0 | return cache; |
86 | 0 | } |
87 | | |
88 | | void cache_fini(PageCache *cache) |
89 | 0 | { |
90 | 0 | int64_t i; |
91 | |
|
92 | 0 | g_assert(cache); |
93 | 0 | g_assert(cache->page_cache); |
94 | | |
95 | 0 | for (i = 0; i < cache->max_num_items; i++) { |
96 | 0 | g_free(cache->page_cache[i].it_data); |
97 | 0 | } |
98 | |
|
99 | 0 | g_free(cache->page_cache); |
100 | 0 | cache->page_cache = NULL; |
101 | 0 | g_free(cache); |
102 | 0 | } |
103 | | |
104 | | static size_t cache_get_cache_pos(const PageCache *cache, |
105 | | uint64_t address) |
106 | 0 | { |
107 | 0 | g_assert(cache->max_num_items); |
108 | 0 | return (address / cache->page_size) & (cache->max_num_items - 1); |
109 | 0 | } |
110 | | |
111 | | static CacheItem *cache_get_by_addr(const PageCache *cache, uint64_t addr) |
112 | 0 | { |
113 | 0 | size_t pos; |
114 | |
|
115 | 0 | g_assert(cache); |
116 | 0 | g_assert(cache->page_cache); |
117 | | |
118 | 0 | pos = cache_get_cache_pos(cache, addr); |
119 | |
|
120 | 0 | return &cache->page_cache[pos]; |
121 | 0 | } |
122 | | |
123 | | uint8_t *get_cached_data(const PageCache *cache, uint64_t addr) |
124 | 0 | { |
125 | 0 | return cache_get_by_addr(cache, addr)->it_data; |
126 | 0 | } |
127 | | |
128 | | bool cache_is_cached(const PageCache *cache, uint64_t addr, |
129 | | uint64_t current_age) |
130 | 0 | { |
131 | 0 | CacheItem *it; |
132 | |
|
133 | 0 | it = cache_get_by_addr(cache, addr); |
134 | |
|
135 | 0 | if (it->it_addr == addr) { |
136 | | /* update the it_age when the cache hit */ |
137 | 0 | it->it_age = current_age; |
138 | 0 | return true; |
139 | 0 | } |
140 | 0 | return false; |
141 | 0 | } |
142 | | |
143 | | int cache_insert(PageCache *cache, uint64_t addr, const uint8_t *pdata, |
144 | | uint64_t current_age) |
145 | 0 | { |
146 | |
|
147 | 0 | CacheItem *it; |
148 | | |
149 | | /* actual update of entry */ |
150 | 0 | it = cache_get_by_addr(cache, addr); |
151 | |
|
152 | 0 | if (it->it_data && it->it_addr != addr && |
153 | 0 | it->it_age + CACHED_PAGE_LIFETIME > current_age) { |
154 | | /* the cache page is fresh, don't replace it */ |
155 | 0 | return -1; |
156 | 0 | } |
157 | | /* allocate page */ |
158 | 0 | if (!it->it_data) { |
159 | 0 | it->it_data = g_try_malloc(cache->page_size); |
160 | 0 | if (!it->it_data) { |
161 | 0 | trace_migration_pagecache_insert(); |
162 | 0 | return -1; |
163 | 0 | } |
164 | 0 | cache->num_items++; |
165 | 0 | } |
166 | | |
167 | 0 | memcpy(it->it_data, pdata, cache->page_size); |
168 | |
|
169 | 0 | it->it_age = current_age; |
170 | 0 | it->it_addr = addr; |
171 | |
|
172 | 0 | return 0; |
173 | 0 | } |