X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?p=palacios.git;a=blobdiff_plain;f=palacios%2Fsrc%2Fdevices%2Fsym_swap.c;h=dad0419f6f27931c4e5afe9f3a6ebd16bb617764;hp=35b46cc0c65306e31f62244e07f35e120a3c0520;hb=123a1ba27ea09c8fa77a1b36ce625b43d7c48b14;hpb=6fe9fe338ec19b212b43bb2c25322dce3cdbb490 diff --git a/palacios/src/devices/sym_swap.c b/palacios/src/devices/sym_swap.c index 35b46cc..dad0419 100644 --- a/palacios/src/devices/sym_swap.c +++ b/palacios/src/devices/sym_swap.c @@ -19,11 +19,12 @@ #include #include -#include #include -#define SWAP_CAPACITY (150 * 1024 * 1024) +#ifdef CONFIG_SYMBIOTIC_SWAP_TELEMETRY +#include +#endif /* This is the first page that linux writes to the swap area */ @@ -52,14 +53,18 @@ union swap_header { struct swap_state { int active; - struct vm_device * blk_dev; - uint_t swapped_pages; uint_t unswapped_pages; union swap_header * hdr; +#ifdef CONFIG_SYMBIOTIC_SWAP_TELEMETRY + uint32_t pages_in; + uint32_t pages_out; +#endif + + uint64_t capacity; uint8_t * swap_space; addr_t swap_base_addr; @@ -107,12 +112,12 @@ static inline void * get_swap_entry(uint32_t pg_index, void * private_data) { struct vm_device * dev = (struct vm_device *)private_data; struct swap_state * swap = (struct swap_state *)(dev->private_data); void * pg_addr = NULL; - int ret = 0; + // int ret = 0; - if ((ret = get_index_usage(swap, pg_index))) { - // CAREFUL: The index might be offset by 1, because the first 4K is the header - pg_addr = (void *)(swap->swap_space + (pg_index * 4096)); - } + // if ((ret = get_index_usage(swap, pg_index))) { + // CAREFUL: The index might be offset by 1, because the first 4K is the header + pg_addr = (void *)(swap->swap_space + (pg_index * 4096)); + // } return pg_addr; } @@ -125,7 +130,7 @@ static uint64_t swap_get_capacity(void * private_data) { PrintDebug("SymSwap: Getting Capacity %d\n", (uint32_t)(swap->capacity)); - return swap->capacity / HD_SECTOR_SIZE; + return swap->capacity; } @@ -135,11 +140,11 @@ static struct v3_swap_ops swap_ops = { -static int swap_read(uint8_t * buf, int sector_count, uint64_t lba, void * private_data) { +static int swap_read(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * private_data) { struct vm_device * dev = (struct vm_device *)private_data; struct swap_state * swap = (struct swap_state *)(dev->private_data); - uint32_t offset = lba * HD_SECTOR_SIZE; - uint32_t length = sector_count * HD_SECTOR_SIZE; + uint32_t offset = lba; + uint32_t length = num_bytes; /* @@ -158,7 +163,11 @@ static int swap_read(uint8_t * buf, int sector_count, uint64_t lba, void * priv if ((swap->active == 1) && (offset != 0)) { int i = 0; // Notify the shadow paging layer - PrintDebug("Swapped in %d pages\n", length / 4096); + +#ifdef CONFIG_SYMBIOTIC_SWAP_TELEMETRY + swap->pages_in += length / 4096; +#endif + for (i = 0; i < length; i += 4096) { set_index_usage(swap, get_swap_index_from_offset(offset + i), 0); v3_swap_in_notify(dev->vm, get_swap_index_from_offset(offset + i), swap->hdr->info.type); @@ -171,11 +180,11 @@ static int swap_read(uint8_t * buf, int sector_count, uint64_t lba, void * priv -static int swap_write(uint8_t * buf, int sector_count, uint64_t lba, void * private_data) { +static int swap_write(uint8_t * buf, uint64_t lba, uint64_t num_bytes, void * private_data) { struct vm_device * dev = (struct vm_device *)private_data; struct swap_state * swap = (struct swap_state *)(dev->private_data); - uint32_t offset = lba * HD_SECTOR_SIZE; - uint32_t length = sector_count * HD_SECTOR_SIZE; + uint32_t offset = lba; + uint32_t length = num_bytes; /* PrintDebug("SymSwap: Writing %d bytes to %p from %p\n", length, @@ -210,7 +219,10 @@ static int swap_write(uint8_t * buf, int sector_count, uint64_t lba, void * priv if ((swap->active == 1) && (offset != 0)) { int i = 0; - PrintDebug("Swapped out %d pages\n", length / 4096); + +#ifdef CONFIG_SYMBIOTIC_SWAP_TELEMETRY + swap->pages_out += length / 4096; +#endif for (i = 0; i < length; i += 4096) { set_index_usage(swap, get_swap_index_from_offset(offset + i), 1); @@ -226,7 +238,7 @@ static int swap_free(struct vm_device * dev) { } -static struct v3_hd_ops hd_ops = { +static struct v3_dev_blk_ops blk_ops = { .read = swap_read, .write = swap_write, .get_capacity = swap_get_capacity, @@ -242,30 +254,38 @@ static struct v3_device_ops dev_ops = { }; +#ifdef CONFIG_SYMBIOTIC_SWAP_TELEMETRY +static void telemetry_cb(struct guest_info * info, void * private_data, char * hdr) { + struct vm_device * dev = (struct vm_device *)private_data; + struct swap_state * swap = (struct swap_state *)(dev->private_data); + V3_Print("%sSwap Device:\n", hdr); + V3_Print("%s\tPages Swapped in=%d\n", hdr, swap->pages_in); + V3_Print("%s\tPages Swapped out=%d\n", hdr, swap->pages_out); +} +#endif -static int swap_init(struct guest_info * vm, void * cfg_data) { + + + +static int swap_init(struct guest_info * vm, v3_cfg_tree_t * cfg) { struct swap_state * swap = NULL; - struct vm_device * virtio_blk = v3_find_dev(vm, (char *)cfg_data); + v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend"); + uint32_t capacity = atoi(v3_cfg_val(cfg, "size")) * 1024 * 1024; + char * name = v3_cfg_val(cfg, "name"); - if (!virtio_blk) { - PrintError("could not find Virtio backend\n"); + if (!frontend_cfg) { + PrintError("Initializing sym swap without a frontend device\n"); return -1; } PrintDebug("Creating Swap Device\n"); - if (virtio_blk == NULL) { - PrintError("Swap device requires a virtio block device\n"); - return -1; - } - - swap = (struct swap_state *)V3_Malloc(sizeof(struct swap_state) + ((SWAP_CAPACITY / 4096) / 8)); + swap = (struct swap_state *)V3_Malloc(sizeof(struct swap_state) + ((capacity / 4096) / 8)); - swap->blk_dev = virtio_blk; - swap->capacity = SWAP_CAPACITY; + swap->capacity = capacity; swap->swapped_pages = 0; swap->unswapped_pages = 0; @@ -273,22 +293,31 @@ static int swap_init(struct guest_info * vm, void * cfg_data) { swap->active = 0; swap->hdr = (union swap_header *)swap; - swap->swap_base_addr = (addr_t)V3_AllocPages(swap->capacity / 4096); swap->swap_space = (uint8_t *)V3_VAddr((void *)(swap->swap_base_addr)); - memset(swap->swap_space, 0, SWAP_CAPACITY); + memset(swap->swap_space, 0, swap->capacity); - memset(swap->usage_map, 0, ((SWAP_CAPACITY / 4096) / 8)); + memset(swap->usage_map, 0, ((swap->capacity / 4096) / 8)); - struct vm_device * dev = v3_allocate_device("SYM_SWAP", &dev_ops, swap); + struct vm_device * dev = v3_allocate_device(name, &dev_ops, swap); if (v3_attach_device(vm, dev) == -1) { - PrintError("Could not attach device %s\n", "SYM_SWAP"); + PrintError("Could not attach device %s\n", name); return -1; } + if (v3_dev_connect_blk(vm, v3_cfg_val(frontend_cfg, "tag"), + &blk_ops, frontend_cfg, swap) == -1) { + PrintError("Could not connect %s to frontend %s\n", + name, v3_cfg_val(frontend_cfg, "tag")); + return -1; + } - v3_virtio_register_harddisk(virtio_blk, &hd_ops, dev); +#ifdef CONFIG_SYMBIOTIC_SWAP_TELEMETRY + if (vm->enable_telemetry) { + v3_add_telemetry_cb(vm, telemetry_cb, dev); + } +#endif return 0; }