ia64/xen-unstable

changeset 4297:2587eb489863

bitkeeper revision 1.1236.43.3 (42430c00lAWor_gGz6AYWnPE5wc0XQ)

Added concurrency niceness to the bottom end of the blockstore.

Signed-off-by: James Bulpin <James.Bulpin@cl.cam.ac.uk>
author jrb44@swoop.cl.cam.ac.uk
date Thu Mar 24 18:50:40 2005 +0000 (2005-03-24)
parents 7e0520c972fb
children 86d00dbf81dd bf5323c5b2d1
files tools/blktap/blockstore.c tools/blktap/blockstore.h
line diff
     1.1 --- a/tools/blktap/blockstore.c	Thu Mar 24 17:27:22 2005 +0000
     1.2 +++ b/tools/blktap/blockstore.c	Thu Mar 24 18:50:40 2005 +0000
     1.3 @@ -15,10 +15,11 @@
     1.4  #include <sys/stat.h>
     1.5  #include <stdarg.h>
     1.6  #include "blockstore.h"
     1.7 +#include <pthread.h>
     1.8  #include "parallax-threaded.h"
     1.9  
    1.10  #define BLOCKSTORE_REMOTE
    1.11 -//#define BSDEBUG
    1.12 +#define BSDEBUG
    1.13  
    1.14  /*****************************************************************************
    1.15   * Debugging
    1.16 @@ -27,7 +28,7 @@
    1.17  void DB(char *format, ...)
    1.18  {
    1.19      va_list args;
    1.20 -    
    1.21 +    fprintf(stderr, "[%05u] ", (int)pthread_getspecific(tid_key));
    1.22      va_start(args, format);
    1.23      vfprintf(stderr, format, args);
    1.24      va_end(args);
    1.25 @@ -44,10 +45,6 @@ void DB(char *format, ...)
    1.26  #include <netdb.h>
    1.27  
    1.28  /*****************************************************************************
    1.29 - *                                                                           *
    1.30 - *****************************************************************************/
    1.31 -
    1.32 -/*****************************************************************************
    1.33   * Network state                                                             *
    1.34   *****************************************************************************/
    1.35  
    1.36 @@ -71,8 +68,30 @@ int bssock = 0;
    1.37  
    1.38  /* Protects the queue manipulation critcal regions.
    1.39   */
    1.40 -#define ENTER_QUEUE_CR (void)0
    1.41 -#define LEAVE_QUEUE_CR (void)0
    1.42 +pthread_mutex_t ptmutex_queue;
    1.43 +#define ENTER_QUEUE_CR pthread_mutex_lock(&ptmutex_queue)
    1.44 +#define LEAVE_QUEUE_CR pthread_mutex_unlock(&ptmutex_queue)
    1.45 +
    1.46 +pthread_mutex_t ptmutex_recv;
    1.47 +#define ENTER_RECV_CR pthread_mutex_lock(&ptmutex_recv)
    1.48 +#define LEAVE_RECV_CR pthread_mutex_unlock(&ptmutex_recv)
    1.49 +
    1.50 +int notify = 0;
    1.51 +pthread_mutex_t ptmutex_notify;
    1.52 +pthread_cond_t ptcv_notify;
    1.53 +#define RECV_NOTIFY { \
    1.54 +    pthread_mutex_lock(&ptmutex_notify); \
    1.55 +    notify = 1; \
    1.56 +    pthread_cond_signal(&ptcv_notify); \
    1.57 +    pthread_mutex_unlock(&ptmutex_notify); }
    1.58 +#define RECV_AWAIT { \
    1.59 +    pthread_mutex_lock(&ptmutex_notify); \
    1.60 +    if (notify) \
    1.61 +        notify = 0; \
    1.62 +    else \
    1.63 +        pthread_cond_wait(&ptcv_notify, &ptmutex_notify); \
    1.64 +    pthread_mutex_unlock(&ptmutex_notify); }
    1.65 +    
    1.66  
    1.67  /* A message queue entry. We allocate one of these for every request we send.
    1.68   * Asynchronous reply reception also used one of these.
    1.69 @@ -91,8 +110,9 @@ typedef struct bsq_t_struct {
    1.70  
    1.71  #define BSQ_STATUS_MATCHED 1
    1.72  
    1.73 -#define ENTER_LUID_CR (void)0
    1.74 -#define LEAVE_LUID_CR (void)0
    1.75 +pthread_mutex_t ptmutex_luid;
    1.76 +#define ENTER_LUID_CR pthread_mutex_lock(&ptmutex_luid)
    1.77 +#define LEAVE_LUID_CR pthread_mutex_unlock(&ptmutex_luid)
    1.78  
    1.79  static u64 luid_cnt = 0x1000ULL;
    1.80  u64 new_luid(void) {
    1.81 @@ -218,6 +238,10 @@ bsq_t *queuesearch(bsq_t *qe) {
    1.82      return q;
    1.83  }
    1.84  
    1.85 +/*****************************************************************************
    1.86 + * Network communication                                                     *
    1.87 + *****************************************************************************/
    1.88 +
    1.89  int send_message(bsq_t *qe) {
    1.90      int rc;
    1.91  
    1.92 @@ -331,7 +355,7 @@ bsq_t rx_qe;
    1.93  bsq_t *recv_any(void) {
    1.94      struct sockaddr_in from;
    1.95      int rc;
    1.96 -
    1.97 +    
    1.98      DB("ENTER recv_any\n");
    1.99  
   1.100      rx_qe.msghdr.msg_name = &from;
   1.101 @@ -361,6 +385,7 @@ bsq_t *recv_any(void) {
   1.102          perror("recv_any");
   1.103          return NULL;
   1.104      }
   1.105 +
   1.106      rx_qe.length = rc;    
   1.107      rx_qe.server = get_server_number(&from);
   1.108  
   1.109 @@ -395,8 +420,13 @@ int wait_recv(bsq_t **reqs, int numreqs)
   1.110          return numreqs;
   1.111      }
   1.112  
   1.113 +    RECV_AWAIT;
   1.114 +
   1.115 +    /*
   1.116      rxagain:
   1.117 +    ENTER_RECV_CR;
   1.118      q = recv_any();
   1.119 +    LEAVE_RECV_CR;
   1.120      if (!q)
   1.121          return -1;
   1.122  
   1.123 @@ -406,11 +436,42 @@ int wait_recv(bsq_t **reqs, int numreqs)
   1.124          fprintf(stderr, "Unmatched RX\n");
   1.125          goto rxagain;
   1.126      }
   1.127 +    */
   1.128  
   1.129      goto checkmatch;
   1.130  
   1.131  }
   1.132  
   1.133 +/* receive loop
   1.134 + */
   1.135 +void *receive_loop(void *arg)
   1.136 +{
   1.137 +    bsq_t *q, *m;
   1.138 +
   1.139 +    for(;;) {
   1.140 +        q = recv_any();
   1.141 +        if (!q) {
   1.142 +            fprintf(stderr, "recv_any error\n");
   1.143 +        }
   1.144 +        else {
   1.145 +            m = queuesearch(q);
   1.146 +            recv_recycle_buffer(q);
   1.147 +            if (!m) {
   1.148 +                fprintf(stderr, "Unmatched RX\n");
   1.149 +            }
   1.150 +            else {
   1.151 +                DB("RX MATCH");
   1.152 +                RECV_NOTIFY;
   1.153 +            }
   1.154 +        }
   1.155 +    }
   1.156 +}
   1.157 +pthread_t pthread_recv;
   1.158 +
   1.159 +/*****************************************************************************
   1.160 + * Reading                                                                   *
   1.161 + *****************************************************************************/
   1.162 +
   1.163  void *readblock_indiv(int server, u64 id) {
   1.164      void *block;
   1.165      bsq_t *qe;
   1.166 @@ -538,6 +599,10 @@ void *readblock(u64 id) {
   1.167      return block;
   1.168  }
   1.169  
   1.170 +/*****************************************************************************
   1.171 + * Writing                                                                   *
   1.172 + *****************************************************************************/
   1.173 +
   1.174  bsq_t *writeblock_indiv(int server, u64 id, void *block) {
   1.175  
   1.176      bsq_t *qe;
   1.177 @@ -663,6 +728,10 @@ int writeblock(u64 id, void *block) {
   1.178      return -1;
   1.179  }
   1.180  
   1.181 +/*****************************************************************************
   1.182 + * Allocation                                                                *
   1.183 + *****************************************************************************/
   1.184 +
   1.185  /**
   1.186   * allocblock: write a new block to disk
   1.187   *   @block: pointer to block
   1.188 @@ -791,6 +860,9 @@ u64 allocblock_hint(void *block, u64 hin
   1.189  
   1.190  #else /* /BLOCKSTORE_REMOTE */
   1.191  
   1.192 +/*****************************************************************************
   1.193 + * Local storage version                                                     *
   1.194 + *****************************************************************************/
   1.195   
   1.196  /**
   1.197   * readblock: read a block from disk
   1.198 @@ -923,6 +995,10 @@ u64 allocblock_hint(void *block, u64 hin
   1.199  
   1.200  #endif /* BLOCKSTORE_REMOTE */
   1.201  
   1.202 +/*****************************************************************************
   1.203 + * Memory management                                                         *
   1.204 + *****************************************************************************/
   1.205 +
   1.206  /**
   1.207   * newblock: get a new in-memory block set to zeros
   1.208   *
   1.209 @@ -1053,6 +1129,10 @@ void freelist_count(int print_each)
   1.210      printf("Total of %Ld ids on freelist.\n", total);
   1.211  }
   1.212  
   1.213 +/*****************************************************************************
   1.214 + * Initialisation                                                            *
   1.215 + *****************************************************************************/
   1.216 +
   1.217  int __init_blockstore(void)
   1.218  {
   1.219      int i;
   1.220 @@ -1062,6 +1142,13 @@ int __init_blockstore(void)
   1.221      
   1.222  #ifdef BLOCKSTORE_REMOTE
   1.223      struct hostent *addr;
   1.224 +
   1.225 +    pthread_mutex_init(&ptmutex_queue, NULL);
   1.226 +    pthread_mutex_init(&ptmutex_luid, NULL);
   1.227 +    pthread_mutex_init(&ptmutex_recv, NULL);
   1.228 +    pthread_mutex_init(&ptmutex_notify, NULL);
   1.229 +    pthread_cond_init(&ptcv_notify, NULL);
   1.230 +
   1.231      bsservers[0].hostname = "firebug.cl.cam.ac.uk";
   1.232      bsservers[1].hostname = "planb.cl.cam.ac.uk";
   1.233      bsservers[2].hostname = "simcity.cl.cam.ac.uk";
   1.234 @@ -1137,6 +1224,8 @@ int __init_blockstore(void)
   1.235          return -1;
   1.236      }
   1.237  
   1.238 +    pthread_create(&pthread_recv, NULL, receive_loop, NULL);
   1.239 +
   1.240  #else /* /BLOCKSTORE_REMOTE */
   1.241      block_fp = open("blockstore.dat", O_RDWR | O_CREAT | O_LARGEFILE, 0644);
   1.242  
   1.243 @@ -1170,3 +1259,12 @@ int __init_blockstore(void)
   1.244  #endif /*  BLOCKSTORE_REMOTE */   
   1.245      return 0;
   1.246  }
   1.247 +
   1.248 +void __exit_blockstore(void)
   1.249 +{
   1.250 +    pthread_mutex_destroy(&ptmutex_recv);
   1.251 +    pthread_mutex_destroy(&ptmutex_luid);
   1.252 +    pthread_mutex_destroy(&ptmutex_queue);
   1.253 +    pthread_mutex_destroy(&ptmutex_notify);
   1.254 +    pthread_cond_destroy(&ptcv_notify);
   1.255 +}
     2.1 --- a/tools/blktap/blockstore.h	Thu Mar 24 17:27:22 2005 +0000
     2.2 +++ b/tools/blktap/blockstore.h	Thu Mar 24 18:50:40 2005 +0000
     2.3 @@ -22,7 +22,7 @@
     2.4  #endif
     2.5  
     2.6  #define FREEBLOCK_SIZE  (BLOCK_SIZE / sizeof(u64)) - (3 * sizeof(u64))
     2.7 -#define FREEBLOCK_MAGIC 0x0fee0fee0fee0fee
     2.8 +#define FREEBLOCK_MAGIC 0x0fee0fee0fee0feeULL
     2.9  
    2.10  typedef struct {
    2.11      u64 magic;
    2.12 @@ -31,7 +31,7 @@ typedef struct {
    2.13      u64 list[FREEBLOCK_SIZE];
    2.14  } freeblock_t; 
    2.15  
    2.16 -#define BLOCKSTORE_MAGIC 0xaaaaaaa00aaaaaaa
    2.17 +#define BLOCKSTORE_MAGIC 0xaaaaaaa00aaaaaaaULL
    2.18  #define BLOCKSTORE_SUPER 1ULL
    2.19  
    2.20  typedef struct {