|
| 1 | +#include "syx-cow-cache.h" |
| 2 | + |
| 3 | +#include "sysemu/block-backend.h" |
| 4 | + |
| 5 | +#define IS_POWER_OF_TWO(x) ((x != 0) && ((x & (x - 1)) == 0)) |
| 6 | + |
| 7 | +SyxCowCache* syx_cow_cache_new(void) |
| 8 | +{ |
| 9 | + SyxCowCache* cache = g_new0(SyxCowCache, 2); |
| 10 | + |
| 11 | + QTAILQ_INIT(&cache->layers); |
| 12 | + |
| 13 | + return cache; |
| 14 | +} |
| 15 | + |
| 16 | +static gchar* g_array_element_ptr(GArray* array, guint position) |
| 17 | +{ |
| 18 | + assert(position < array->len); |
| 19 | + return array->data + position * g_array_get_element_size(array); |
| 20 | +} |
| 21 | + |
| 22 | +void syx_cow_cache_push_layer(SyxCowCache* scc, uint64_t chunk_size, uint64_t max_size) |
| 23 | +{ |
| 24 | + SyxCowCacheLayer* new_layer = g_new0(SyxCowCacheLayer, 1); |
| 25 | + |
| 26 | + new_layer->cow_cache_devices = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, NULL); |
| 27 | + new_layer->chunk_size = chunk_size; |
| 28 | + new_layer->max_nb_chunks = max_size; |
| 29 | + |
| 30 | + assert(IS_POWER_OF_TWO(chunk_size)); |
| 31 | + assert(!(max_size % chunk_size)); |
| 32 | + |
| 33 | + QTAILQ_INSERT_HEAD(&scc->layers, new_layer, next); |
| 34 | +} |
| 35 | + |
| 36 | +void syx_cow_cache_pop_layer(SyxCowCache* scc) |
| 37 | +{ |
| 38 | + // TODO |
| 39 | +} |
| 40 | + |
| 41 | +static void flush_device_layer(gpointer _blk_name_hash, gpointer cache_device, gpointer _user_data) |
| 42 | +{ |
| 43 | + SyxCowCacheDevice* sccd = (SyxCowCacheDevice*) cache_device; |
| 44 | + |
| 45 | + g_hash_table_remove_all(sccd->positions); |
| 46 | + g_array_set_size(sccd->data, 0); |
| 47 | +} |
| 48 | + |
| 49 | +void syx_cow_cache_flush_highest_layer(SyxCowCache* scc) |
| 50 | +{ |
| 51 | + SyxCowCacheLayer* highest_layer = QTAILQ_FIRST(&scc->layers); |
| 52 | + |
| 53 | + // highest_layer->cow_cache_devices |
| 54 | + g_hash_table_foreach(highest_layer->cow_cache_devices, flush_device_layer, NULL); |
| 55 | +} |
| 56 | + |
| 57 | +void syx_cow_cache_move(SyxCowCache* lhs, SyxCowCache** rhs) |
| 58 | +{ |
| 59 | + lhs->layers = (*rhs)->layers; |
| 60 | + g_free(*rhs); |
| 61 | + *rhs = NULL; |
| 62 | +} |
| 63 | + |
| 64 | +static bool read_chunk_from_cache_layer_device(SyxCowCacheDevice* sccd, QEMUIOVector* qiov, size_t qiov_offset, uint64_t blk_offset) |
| 65 | +{ |
| 66 | + gpointer data_position = NULL; |
| 67 | + bool found = g_hash_table_lookup_extended(sccd->positions, GUINT_TO_POINTER(blk_offset), NULL, &data_position); |
| 68 | + |
| 69 | + // cache hit |
| 70 | + if (found) { |
| 71 | + void* data_position_ptr = g_array_element_ptr(sccd->data, GPOINTER_TO_UINT(data_position)); |
| 72 | + assert(qemu_iovec_from_buf(qiov, qiov_offset, data_position_ptr, g_array_get_element_size(sccd->data)) == g_array_get_element_size(sccd->data)); |
| 73 | + } |
| 74 | + |
| 75 | + return found; |
| 76 | +} |
| 77 | + |
| 78 | +// len must be smaller than nb bytes to next aligned to chunk of blk_offset. |
| 79 | +// static void write_to_cache_layer_device_unaligned(SyxCowCacheDevice* sccd, QEMUIOVector* qiov, size_t qiov_offset, uint64_t blk_offset, uint64_t len) |
| 80 | +// { |
| 81 | +// const uint64_t chunk_size = g_array_get_element_size(sccd->data); |
| 82 | +// |
| 83 | +// assert(ROUND_UP(blk_offset, chunk_size) - blk_offset <= len); |
| 84 | +// assert(IS_POWER_OF_TWO(chunk_size)); |
| 85 | +// |
| 86 | +// uint64_t blk_offset_aligned = ROUND_DOWN(blk_offset, chunk_size); |
| 87 | +// |
| 88 | +// gpointer data_position = NULL; |
| 89 | +// bool found = g_hash_table_lookup_extended(sccd->positions, GUINT_TO_POINTER(blk_offset_aligned), NULL, &data_position); |
| 90 | +// |
| 91 | +// if (!found) { |
| 92 | +// data_position = GUINT_TO_POINTER(sccd->data->len); |
| 93 | +// sccd->data = g_array_set_size(sccd->data, sccd->data->len + 1); |
| 94 | +// g_hash_table_insert(sccd->positions, GUINT_TO_POINTER(blk_offset), data_position); |
| 95 | +// } |
| 96 | +// |
| 97 | +// void* data_position_ptr = g_array_element_ptr(sccd->data, GPOINTER_TO_UINT(data_position)); |
| 98 | +// |
| 99 | +// assert(qemu_iovec_to_buf(qiov, qiov_offset, data_position_ptr, g_array_get_element_size(sccd->data)) == |
| 100 | +// g_array_get_element_size(sccd->data)); |
| 101 | +// } |
| 102 | + |
| 103 | +// cache layer is allocated and all the basic checks are already done. |
| 104 | +static void write_chunk_to_cache_layer_device(SyxCowCacheDevice* sccd, QEMUIOVector* qiov, size_t qiov_offset, uint64_t blk_offset) |
| 105 | +{ |
| 106 | + const uint64_t chunk_size = g_array_get_element_size(sccd->data); |
| 107 | + |
| 108 | + gpointer data_position = NULL; |
| 109 | + bool found = g_hash_table_lookup_extended(sccd->positions, GUINT_TO_POINTER(blk_offset), NULL, &data_position); |
| 110 | + |
| 111 | + if (!found) { |
| 112 | + data_position = GUINT_TO_POINTER(sccd->data->len); |
| 113 | + sccd->data = g_array_set_size(sccd->data, sccd->data->len + 1); |
| 114 | + g_hash_table_insert(sccd->positions, GUINT_TO_POINTER(blk_offset), data_position); |
| 115 | + } |
| 116 | + |
| 117 | + void* data_position_ptr = g_array_element_ptr(sccd->data, GPOINTER_TO_UINT(data_position)); |
| 118 | + |
| 119 | + assert(qemu_iovec_to_buf(qiov, qiov_offset, data_position_ptr, chunk_size) == |
| 120 | + chunk_size); |
| 121 | +} |
| 122 | + |
| 123 | +static bool read_chunk_from_cache_layer(SyxCowCacheLayer* sccl, BlockBackend* blk, QEMUIOVector* qiov, size_t qiov_offset, uint64_t blk_offset) |
| 124 | +{ |
| 125 | + assert(!(qiov->size % sccl->chunk_size)); |
| 126 | + |
| 127 | + SyxCowCacheDevice* cache_entry = g_hash_table_lookup(sccl->cow_cache_devices, GINT_TO_POINTER(blk_name_hash(blk))); |
| 128 | + |
| 129 | + // return early if nothing is registered |
| 130 | + if (!cache_entry) { |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + assert(cache_entry && cache_entry->data); |
| 135 | + |
| 136 | + // try to read cached pages in current layer if something is registered. |
| 137 | + return read_chunk_from_cache_layer_device(cache_entry, qiov, qiov_offset, blk_offset); |
| 138 | +} |
| 139 | + |
| 140 | +// Returns false if could not write to current layer. |
| 141 | +static bool write_to_cache_layer(SyxCowCacheLayer* sccl, BlockBackend* blk, int64_t offset, int64_t bytes, QEMUIOVector* qiov) |
| 142 | +{ |
| 143 | + if (qiov->size % sccl->chunk_size) { |
| 144 | + // todo: determine if it is worth developing an unaligned access version. |
| 145 | + printf("error: 0x%zx %% 0x%lx == 0x%lx\n", qiov->size, sccl->chunk_size, qiov->size % sccl->chunk_size); |
| 146 | + exit(1); |
| 147 | + } |
| 148 | + |
| 149 | + SyxCowCacheDevice* cache_entry = g_hash_table_lookup(sccl->cow_cache_devices, GINT_TO_POINTER(blk_name_hash(blk))); |
| 150 | + |
| 151 | + if (unlikely(!cache_entry)) { |
| 152 | + cache_entry = g_new0(SyxCowCacheDevice, 1); |
| 153 | + cache_entry->data = g_array_sized_new(false, false, sccl->chunk_size, INITIAL_NB_CHUNKS_PER_DEVICE); |
| 154 | + cache_entry->positions = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, NULL); |
| 155 | + g_hash_table_insert(sccl->cow_cache_devices, GINT_TO_POINTER(blk_name_hash(blk)), cache_entry); |
| 156 | + } |
| 157 | + |
| 158 | + assert(cache_entry && cache_entry->data); |
| 159 | + |
| 160 | + if (cache_entry->data->len + (qiov->size / sccl->chunk_size) > sccl->max_nb_chunks) { |
| 161 | + return false; |
| 162 | + } |
| 163 | + |
| 164 | + // write cached page |
| 165 | + uint64_t blk_offset = offset; |
| 166 | + size_t qiov_offset = 0; |
| 167 | + for (; qiov_offset < qiov->size; blk_offset += sccl->chunk_size, qiov_offset += sccl->chunk_size) { |
| 168 | + write_chunk_to_cache_layer_device(cache_entry, qiov, qiov_offset, blk_offset); |
| 169 | + } |
| 170 | + |
| 171 | + return true; |
| 172 | +} |
| 173 | + |
| 174 | +void syx_cow_cache_read_entry(SyxCowCache* scc, BlockBackend *blk, int64_t offset, int64_t bytes, QEMUIOVector *qiov, size_t _qiov_offset, |
| 175 | + BdrvRequestFlags flags) |
| 176 | +{ |
| 177 | + SyxCowCacheLayer* layer; |
| 178 | + uint64_t blk_offset = offset; |
| 179 | + size_t qiov_offset = 0; |
| 180 | + uint64_t chunk_size = 0; |
| 181 | + |
| 182 | + // printf("[%s] Read 0x%zx bytes @addr %lx\n", blk_name(blk), qiov->size, offset); |
| 183 | + |
| 184 | + // First read the backing block device normally. |
| 185 | + assert(blk_co_preadv(blk, offset, bytes, qiov, flags) >= 0); |
| 186 | + |
| 187 | + // Then fix the chunks that have been read from before. |
| 188 | + if (!QTAILQ_EMPTY(&scc->layers)) { |
| 189 | + for (;qiov_offset < qiov->size; blk_offset += chunk_size, qiov_offset += chunk_size) { |
| 190 | + QTAILQ_FOREACH(layer, &scc->layers, next) { |
| 191 | + chunk_size = layer->chunk_size; |
| 192 | + if (read_chunk_from_cache_layer(layer, blk, qiov, qiov_offset, blk_offset)) { |
| 193 | + break; |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +bool syx_cow_cache_write_entry(SyxCowCache* scc, BlockBackend *blk, int64_t offset, int64_t bytes, QEMUIOVector *qiov, size_t qiov_offset, |
| 201 | + BdrvRequestFlags flags) |
| 202 | +{ |
| 203 | + SyxCowCacheLayer* layer; |
| 204 | + |
| 205 | + // printf("[%s] Write 0x%zx bytes @addr %lx\n", blk_name(blk), qiov->size, offset); |
| 206 | + |
| 207 | + layer = QTAILQ_FIRST(&scc->layers); |
| 208 | + if (layer) { |
| 209 | + assert(write_to_cache_layer(layer, blk, offset, bytes, qiov)); |
| 210 | + return true; |
| 211 | + } else { |
| 212 | + return false; |
| 213 | + } |
| 214 | +} |
0 commit comments