[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-changelog] [xen-unstable] Multiple tapdisk2 processes may use the same parent disk images (later used to



# HG changeset patch
# User Keir Fraser <keir.fraser@xxxxxxxxxx>
# Date 1261031277 0
# Node ID d3b569b2a2b9ba13d458acf58ef4e953808074c4
# Parent  054042ba73b68711be62eb0a46f6af40858a37b2
Multiple tapdisk2 processes may use the same parent disk images (later used to
detect sharable memory pages). This patch establishes unique id for each disk
image opened by tapdisk2, and stores it in shared memory region, thus making it
available to the remaining tapdisk2s.

Signed-off-by: Grzegorz Milos <Grzegorz.Milos@xxxxxxxxxx>
---
 tools/blktap2/drivers/tapdisk-image.c |   11 ++--
 tools/blktap2/drivers/tapdisk-image.h |    1 
 tools/memshr/interface.c              |   20 +++++++
 tools/memshr/memshr.h                 |    2 
 tools/memshr/shm.c                    |   87 ++++++++++++++++++++++++++++++++--
 tools/memshr/shm.h                    |   23 +++++++-
 6 files changed, 133 insertions(+), 11 deletions(-)

diff -r 054042ba73b6 -r d3b569b2a2b9 tools/blktap2/drivers/tapdisk-image.c
--- a/tools/blktap2/drivers/tapdisk-image.c     Thu Dec 17 06:27:57 2009 +0000
+++ b/tools/blktap2/drivers/tapdisk-image.c     Thu Dec 17 06:27:57 2009 +0000
@@ -28,6 +28,7 @@
 #include <errno.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <memshr.h>
 
 #include "tapdisk-image.h"
 #include "tapdisk-driver.h"
@@ -52,10 +53,11 @@ tapdisk_image_allocate(char *file, int t
                return NULL;
        }
 
-       image->type    = type;
-       image->flags   = flags;
-       image->storage = storage;
-       image->private = private;
+       image->type      = type;
+       image->flags     = flags;
+       image->storage   = storage;
+       image->private   = private;
+    image->memshr_id = memshr_vbd_image_get(file);
        INIT_LIST_HEAD(&image->next);
 
        return image;
@@ -69,6 +71,7 @@ tapdisk_image_free(td_image_t *image)
 
        list_del(&image->next);
 
+    memshr_vbd_image_put(image->memshr_id);
        free(image->name);
        tapdisk_driver_free(image->driver);
        free(image);
diff -r 054042ba73b6 -r d3b569b2a2b9 tools/blktap2/drivers/tapdisk-image.h
--- a/tools/blktap2/drivers/tapdisk-image.h     Thu Dec 17 06:27:57 2009 +0000
+++ b/tools/blktap2/drivers/tapdisk-image.h     Thu Dec 17 06:27:57 2009 +0000
@@ -34,6 +34,7 @@ struct td_image_handle {
 struct td_image_handle {
        int                          type;
        char                        *name;
+    uint16_t                     memshr_id;
 
        td_flag_t                    flags;
        int                          storage;
diff -r 054042ba73b6 -r d3b569b2a2b9 tools/memshr/interface.c
--- a/tools/memshr/interface.c  Thu Dec 17 06:27:57 2009 +0000
+++ b/tools/memshr/interface.c  Thu Dec 17 06:27:57 2009 +0000
@@ -106,3 +106,23 @@ void memshr_vbd_initialize(void)
     vbd_info.enabled = 1;
 }
 
+uint16_t memshr_vbd_image_get(char* file)
+{
+    uint16_t id;
+
+    if(pthread_mutex_lock(&SHARED_INFO->lock)) goto error_out;
+    id = shm_vbd_image_get(file, SHARED_INFO->vbd_images);
+    if(pthread_mutex_unlock(&SHARED_INFO->lock)) goto error_out;
+
+    return id;
+error_out:
+    return 0;    
+}
+
+void memshr_vbd_image_put(uint16_t memshr_id)
+{
+    if(pthread_mutex_lock(&SHARED_INFO->lock)) return;
+    shm_vbd_image_put(memshr_id, SHARED_INFO->vbd_images);
+    if(pthread_mutex_unlock(&SHARED_INFO->lock)) return;
+}
+
diff -r 054042ba73b6 -r d3b569b2a2b9 tools/memshr/memshr.h
--- a/tools/memshr/memshr.h     Thu Dec 17 06:27:57 2009 +0000
+++ b/tools/memshr/memshr.h     Thu Dec 17 06:27:57 2009 +0000
@@ -26,5 +26,7 @@ extern void memshr_set_domid(int domid);
 extern void memshr_set_domid(int domid);
 extern void memshr_daemon_initialize(void);
 extern void memshr_vbd_initialize(void);
+extern uint16_t memshr_vbd_image_get(char* file);
+extern void memshr_vbd_image_put(uint16_t memshr_id);
 
 #endif /* __MEMSHR_H__ */
diff -r 054042ba73b6 -r d3b569b2a2b9 tools/memshr/shm.c
--- a/tools/memshr/shm.c        Thu Dec 17 06:27:57 2009 +0000
+++ b/tools/memshr/shm.c        Thu Dec 17 06:27:57 2009 +0000
@@ -92,10 +92,11 @@ shared_memshr_info_t * shm_shared_info_o
 {
     shared_memshr_info_t *shared_info;
     pthread_mutexattr_t  lock_attr;
-
-    assert(sizeof(shared_memshr_info_t) < XC_PAGE_SIZE);
+    int nr_pages, i;
+
+    nr_pages = (sizeof(shared_memshr_info_t) >> XC_PAGE_SHIFT) + 1;
     if(shm_area_open(MEMSHR_INFO_SHM_FILE, 
-                     XC_PAGE_SIZE,
+                     nr_pages * XC_PAGE_SIZE,
                      unlink, 
                      &(shm_info.shared_info_area)) < 0)
     {
@@ -115,6 +116,12 @@ shared_memshr_info_t * shm_shared_info_o
             DPRINTF("Failed to init shared info lock.\n");
             return NULL;
         }
+        strcpy(shared_info->vbd_images[0].file, "list-head");
+        for(i=1; i<MAX_NR_VBD_IMAGES; i++)
+        {
+            shared_info->vbd_images[i].next = i; 
+            shared_info->vbd_images[i].prev = i; 
+        }
         shared_info->magic = MEMSHR_INFO_MAGIC;
     } 
     else
@@ -180,3 +187,77 @@ struct blockshr_hash * shm_blockshr_hash
     return h;
 } 
 
+uint16_t shm_vbd_image_get(char* file, vbd_image_info_t *vbd_imgs)
+{
+    vbd_image_info_t *img, *next_img;
+    int i, img_id;
+
+    /* Try to find the file in the existing list first */ 
+    img = vbd_imgs;
+    while(img->next != 0)
+    {
+        img = vbd_imgs + img->next;
+        if(strncmp(img->file, file, MAX_NAME_LEN) == 0)
+        {
+            img->ref_cnt++;
+            return (uint16_t)(img - vbd_imgs); 
+        }
+    }
+    
+    /* Couldn't find an existing entry. We need to add one. Find empty slot */ 
+    for(i=1; i<MAX_NR_VBD_IMAGES; i++)
+    {
+        img = vbd_imgs + i;
+        if((img->next == i) && (img->prev == i))
+            break;
+    }
+    /* No entries left! */
+    if(i == MAX_NR_VBD_IMAGES)
+    {
+        DPRINTF("No space in vbds table.\n");
+        return 0;
+    }
+    if(strlen(file) > MAX_NAME_LEN)
+    {
+        DPRINTF("Filename: %s too long (>%d).\n", file, MAX_NAME_LEN);
+        return 0; 
+    }
+    /* Init the entry */
+    img_id = (img - vbd_imgs);
+    next_img = vbd_imgs + vbd_imgs[0].next;
+    strcpy(img->file, file);
+    img->ref_cnt = 1;
+    img->next = vbd_imgs[0].next;
+    img->prev = 0;
+    next_img->prev = img_id;
+    vbd_imgs[0].next = img_id;
+
+    return img_id;
+}
+
+
+void shm_vbd_image_put(uint16_t memshr_id, vbd_image_info_t *vbd_imgs)
+{
+    vbd_image_info_t *img, *next_img, *prev_img;
+    
+    img = vbd_imgs + memshr_id;
+    if(img->ref_cnt == 0)
+    {
+        DPRINTF("Incorrect image put.\n");
+        return;
+    }
+
+    img->ref_cnt--; 
+
+    /* Remove from list if ref_cnt is zero */
+    if(img->ref_cnt == 0)
+    {
+        next_img = vbd_imgs + img->next;
+        prev_img = vbd_imgs + img->prev;
+        prev_img->next = img->next;
+        next_img->prev = img->prev;
+        img->next = img->prev = (img - vbd_imgs);
+        memset(img->file, 0, MAX_NAME_LEN);
+    }
+}
+
diff -r 054042ba73b6 -r d3b569b2a2b9 tools/memshr/shm.h
--- a/tools/memshr/shm.h        Thu Dec 17 06:27:57 2009 +0000
+++ b/tools/memshr/shm.h        Thu Dec 17 06:27:57 2009 +0000
@@ -20,16 +20,31 @@
 #define __SHM_H__
 
 #include <pthread.h>
+#include <unistd.h>
 
+#define MAX_NAME_LEN  1000
+
+typedef struct vbd_image_info {
+    char     file[MAX_NAME_LEN];
+    int      ref_cnt;
+    uint16_t next;
+    uint16_t prev;
+} vbd_image_info_t;
+
+#define MAX_NR_VBD_IMAGES   4096
+ 
 typedef struct shared_memshr_info {
-    unsigned long   magic;
-    pthread_mutex_t lock;
-    int             fgprtshr_hash_inited;
-    int             blockshr_hash_inited;
+    unsigned long    magic;
+    pthread_mutex_t  lock;
+    int              fgprtshr_hash_inited;
+    int              blockshr_hash_inited;
+    vbd_image_info_t vbd_images[MAX_NR_VBD_IMAGES];
 } shared_memshr_info_t;
 
 shared_memshr_info_t * shm_shared_info_open(int unlink);
 struct fgprtshr_hash * shm_fgprtshr_hash_open(int unlink);
 struct blockshr_hash * shm_blockshr_hash_open(int unlink);
+uint16_t shm_vbd_image_get(char* file, vbd_image_info_t *vbd_imgs);
+void     shm_vbd_image_put(uint16_t memshr_id, vbd_image_info_t *vbd_imgs);
 
 #endif /* __SHM_H__ */

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.