Home | History | Annotate | Line # | Download | only in raidframe
rf_alloclist.c revision 1.1
      1  1.1  oster /*	$NetBSD: rf_alloclist.c,v 1.1 1998/11/13 04:20:26 oster Exp $	*/
      2  1.1  oster /*
      3  1.1  oster  * Copyright (c) 1995 Carnegie-Mellon University.
      4  1.1  oster  * All rights reserved.
      5  1.1  oster  *
      6  1.1  oster  * Author: Mark Holland
      7  1.1  oster  *
      8  1.1  oster  * Permission to use, copy, modify and distribute this software and
      9  1.1  oster  * its documentation is hereby granted, provided that both the copyright
     10  1.1  oster  * notice and this permission notice appear in all copies of the
     11  1.1  oster  * software, derivative works or modified versions, and any portions
     12  1.1  oster  * thereof, and that both notices appear in supporting documentation.
     13  1.1  oster  *
     14  1.1  oster  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15  1.1  oster  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     16  1.1  oster  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17  1.1  oster  *
     18  1.1  oster  * Carnegie Mellon requests users of this software to return to
     19  1.1  oster  *
     20  1.1  oster  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21  1.1  oster  *  School of Computer Science
     22  1.1  oster  *  Carnegie Mellon University
     23  1.1  oster  *  Pittsburgh PA 15213-3890
     24  1.1  oster  *
     25  1.1  oster  * any improvements or extensions that they make and grant Carnegie the
     26  1.1  oster  * rights to redistribute these changes.
     27  1.1  oster  */
     28  1.1  oster 
     29  1.1  oster /*
     30  1.1  oster  * Log: rf_alloclist.c,v
     31  1.1  oster  * Revision 1.28  1996/07/27 23:36:08  jimz
     32  1.1  oster  * Solaris port of simulator
     33  1.1  oster  *
     34  1.1  oster  * Revision 1.27  1996/06/12  03:29:54  jimz
     35  1.1  oster  * don't barf just because we can't create an alloclist
     36  1.1  oster  *
     37  1.1  oster  * Revision 1.26  1996/06/10  11:55:47  jimz
     38  1.1  oster  * Straightened out some per-array/not-per-array distinctions, fixed
     39  1.1  oster  * a couple bugs related to confusion. Added shutdown lists. Removed
     40  1.1  oster  * layout shutdown function (now subsumed by shutdown lists).
     41  1.1  oster  *
     42  1.1  oster  * Revision 1.25  1996/06/09  02:36:46  jimz
     43  1.1  oster  * lots of little crufty cleanup- fixup whitespace
     44  1.1  oster  * issues, comment #ifdefs, improve typing in some
     45  1.1  oster  * places (esp size-related)
     46  1.1  oster  *
     47  1.1  oster  * Revision 1.24  1996/06/05  18:06:02  jimz
     48  1.1  oster  * Major code cleanup. The Great Renaming is now done.
     49  1.1  oster  * Better modularity. Better typing. Fixed a bunch of
     50  1.1  oster  * synchronization bugs. Made a lot of global stuff
     51  1.1  oster  * per-desc or per-array. Removed dead code.
     52  1.1  oster  *
     53  1.1  oster  * Revision 1.23  1996/05/30  23:22:16  jimz
     54  1.1  oster  * bugfixes of serialization, timing problems
     55  1.1  oster  * more cleanup
     56  1.1  oster  *
     57  1.1  oster  * Revision 1.22  1996/05/27  18:56:37  jimz
     58  1.1  oster  * more code cleanup
     59  1.1  oster  * better typing
     60  1.1  oster  * compiles in all 3 environments
     61  1.1  oster  *
     62  1.1  oster  * Revision 1.21  1996/05/23  21:46:35  jimz
     63  1.1  oster  * checkpoint in code cleanup (release prep)
     64  1.1  oster  * lots of types, function names have been fixed
     65  1.1  oster  *
     66  1.1  oster  * Revision 1.20  1996/05/20  16:15:59  jimz
     67  1.1  oster  * switch to rf_{mutex,cond}_{init,destroy}
     68  1.1  oster  *
     69  1.1  oster  * Revision 1.19  1996/05/18  19:51:34  jimz
     70  1.1  oster  * major code cleanup- fix syntax, make some types consistent,
     71  1.1  oster  * add prototypes, clean out dead code, et cetera
     72  1.1  oster  *
     73  1.1  oster  * Revision 1.18  1996/05/16  22:27:45  jimz
     74  1.1  oster  * get rid of surreal_MakeAllocList (what was that, anyway?)
     75  1.1  oster  *
     76  1.1  oster  * Revision 1.17  1995/12/12  18:10:06  jimz
     77  1.1  oster  * MIN -> RF_MIN, MAX -> RF_MAX, ASSERT -> RF_ASSERT
     78  1.1  oster  * fix 80-column brain damage in comments
     79  1.1  oster  *
     80  1.1  oster  * Revision 1.16  1995/11/30  16:27:07  wvcii
     81  1.1  oster  * added copyright info
     82  1.1  oster  *
     83  1.1  oster  * Revision 1.15  1995/10/05  20:37:56  jimz
     84  1.1  oster  * assert non-NULLness of pointer to FREE in FreeAllocList()
     85  1.1  oster  *
     86  1.1  oster  * Revision 1.14  1995/06/11  20:11:24  holland
     87  1.1  oster  * changed fl_hist,miss_count from long to int to get around weird kernel bug
     88  1.1  oster  *
     89  1.1  oster  * Revision 1.13  1995/05/01  13:28:00  holland
     90  1.1  oster  * parity range locks, locking disk requests, recon+parityscan in kernel, etc.
     91  1.1  oster  *
     92  1.1  oster  * Revision 1.12  1995/04/21  19:13:04  holland
     93  1.1  oster  * minor change to avoid a syntax error on DO_FREE
     94  1.1  oster  *
     95  1.1  oster  * Revision 1.11  1995/02/17  19:39:56  holland
     96  1.1  oster  * added size param to all calls to Free().
     97  1.1  oster  * this is ignored at user level, but necessary in the kernel.
     98  1.1  oster  *
     99  1.1  oster  * Revision 1.10  1995/02/10  18:08:07  holland
    100  1.1  oster  * added DO_FREE macro to fix what I broke during kernelization
    101  1.1  oster  *
    102  1.1  oster  * Revision 1.9  1995/02/10  17:34:10  holland
    103  1.1  oster  * kernelization changes
    104  1.1  oster  *
    105  1.1  oster  * Revision 1.8  1995/02/03  22:31:36  holland
    106  1.1  oster  * many changes related to kernelization
    107  1.1  oster  *
    108  1.1  oster  * Revision 1.7  1995/02/01  15:13:05  holland
    109  1.1  oster  * moved #include of general.h out of raid.h and into each file
    110  1.1  oster  *
    111  1.1  oster  * Revision 1.6  1995/01/11  19:27:02  holland
    112  1.1  oster  * many changes related to performance tuning
    113  1.1  oster  *
    114  1.1  oster  * Revision 1.5  1994/11/29  20:53:10  danner
    115  1.1  oster  * Marks mods
    116  1.1  oster  *
    117  1.1  oster  * Revision 1.3  1994/11/19  21:01:07  danner
    118  1.1  oster  * First merge with mark
    119  1.1  oster  *
    120  1.1  oster  * Revision 1.1.1.1  1994/11/19  20:23:38  danner
    121  1.1  oster  * First PQ checkin
    122  1.1  oster  *
    123  1.1  oster  * Revision 1.2  1994/11/16  15:45:35  danner
    124  1.1  oster  * fixed free bug in FreeAllocList
    125  1.1  oster  *
    126  1.1  oster  *
    127  1.1  oster  */
    128  1.1  oster 
    129  1.1  oster /****************************************************************************
    130  1.1  oster  *
    131  1.1  oster  * Alloclist.c -- code to manipulate allocation lists
    132  1.1  oster  *
    133  1.1  oster  * an allocation list is just a list of AllocListElem structures.  Each
    134  1.1  oster  * such structure contains a fixed-size array of pointers.  Calling
    135  1.1  oster  * FreeAList() causes each pointer to be freed.
    136  1.1  oster  *
    137  1.1  oster  ***************************************************************************/
    138  1.1  oster 
    139  1.1  oster #include "rf_types.h"
    140  1.1  oster #include "rf_threadstuff.h"
    141  1.1  oster #include "rf_alloclist.h"
    142  1.1  oster #include "rf_debugMem.h"
    143  1.1  oster #include "rf_etimer.h"
    144  1.1  oster #include "rf_general.h"
    145  1.1  oster #include "rf_shutdown.h"
    146  1.1  oster #include "rf_sys.h"
    147  1.1  oster 
    148  1.1  oster RF_DECLARE_STATIC_MUTEX(alist_mutex)
    149  1.1  oster static unsigned int fl_hit_count, fl_miss_count;
    150  1.1  oster 
    151  1.1  oster static RF_AllocListElem_t *al_free_list=NULL;
    152  1.1  oster static int al_free_list_count;
    153  1.1  oster 
    154  1.1  oster #define RF_AL_FREELIST_MAX 256
    155  1.1  oster 
    156  1.1  oster #ifndef KERNEL
    157  1.1  oster #define DO_FREE(_p,_sz) free((_p))
    158  1.1  oster #else /* !KERNEL */
    159  1.1  oster #define DO_FREE(_p,_sz) RF_Free((_p),(_sz))
    160  1.1  oster #endif /* !KERNEL */
    161  1.1  oster 
    162  1.1  oster static void rf_ShutdownAllocList(void *);
    163  1.1  oster 
    164  1.1  oster static void rf_ShutdownAllocList(ignored)
    165  1.1  oster   void  *ignored;
    166  1.1  oster {
    167  1.1  oster   RF_AllocListElem_t *p, *pt;
    168  1.1  oster 
    169  1.1  oster   for (p = al_free_list; p; ) {
    170  1.1  oster     pt = p;
    171  1.1  oster     p = p->next;
    172  1.1  oster     DO_FREE(pt, sizeof(*pt));
    173  1.1  oster   }
    174  1.1  oster   rf_mutex_destroy(&alist_mutex);
    175  1.1  oster   /*
    176  1.1  oster   printf("Alloclist: Free list hit count %lu (%lu %%) miss count %lu (%lu %%)\n",
    177  1.1  oster 	 fl_hit_count, (100*fl_hit_count)/(fl_hit_count+fl_miss_count),
    178  1.1  oster 	 fl_miss_count, (100*fl_miss_count)/(fl_hit_count+fl_miss_count));
    179  1.1  oster   */
    180  1.1  oster }
    181  1.1  oster 
    182  1.1  oster int rf_ConfigureAllocList(listp)
    183  1.1  oster   RF_ShutdownList_t  **listp;
    184  1.1  oster {
    185  1.1  oster   int rc;
    186  1.1  oster 
    187  1.1  oster   rc = rf_mutex_init(&alist_mutex);
    188  1.1  oster   if (rc) {
    189  1.1  oster     RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    190  1.1  oster       __LINE__, rc);
    191  1.1  oster     return(rc);
    192  1.1  oster   }
    193  1.1  oster   al_free_list = NULL;
    194  1.1  oster   fl_hit_count = fl_miss_count = al_free_list_count = 0;
    195  1.1  oster   rc = rf_ShutdownCreate(listp, rf_ShutdownAllocList, NULL);
    196  1.1  oster   if (rc) {
    197  1.1  oster     RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
    198  1.1  oster       __FILE__, __LINE__, rc);
    199  1.1  oster     rf_mutex_destroy(&alist_mutex);
    200  1.1  oster     return(rc);
    201  1.1  oster   }
    202  1.1  oster   return(0);
    203  1.1  oster }
    204  1.1  oster 
    205  1.1  oster 
    206  1.1  oster /* we expect the lists to have at most one or two elements, so we're willing
    207  1.1  oster  * to search for the end.  If you ever observe the lists growing longer,
    208  1.1  oster  * increase POINTERS_PER_ALLOC_LIST_ELEMENT.
    209  1.1  oster  */
    210  1.1  oster void rf_real_AddToAllocList(l, p, size, lockflag)
    211  1.1  oster   RF_AllocListElem_t  *l;
    212  1.1  oster   void                *p;
    213  1.1  oster   int                  size;
    214  1.1  oster   int                  lockflag;
    215  1.1  oster {
    216  1.1  oster   RF_AllocListElem_t *newelem;
    217  1.1  oster 
    218  1.1  oster   for ( ; l->next; l=l->next)
    219  1.1  oster 	  RF_ASSERT(l->numPointers == RF_POINTERS_PER_ALLOC_LIST_ELEMENT);  /* find end of list */
    220  1.1  oster 
    221  1.1  oster   RF_ASSERT(l->numPointers >= 0 && l->numPointers <= RF_POINTERS_PER_ALLOC_LIST_ELEMENT);
    222  1.1  oster   if (l->numPointers == RF_POINTERS_PER_ALLOC_LIST_ELEMENT) {
    223  1.1  oster     newelem = rf_real_MakeAllocList(lockflag);
    224  1.1  oster     l->next = newelem;
    225  1.1  oster     l = newelem;
    226  1.1  oster   }
    227  1.1  oster   l->pointers[ l->numPointers ] = p;
    228  1.1  oster   l->sizes   [ l->numPointers ] = size;
    229  1.1  oster   l->numPointers++;
    230  1.1  oster 
    231  1.1  oster }
    232  1.1  oster 
    233  1.1  oster 
    234  1.1  oster /* we use the debug_mem_mutex here because we need to lock it anyway to call free.
    235  1.1  oster  * this is probably a bug somewhere else in the code, but when I call malloc/free
    236  1.1  oster  * outside of any lock I have endless trouble with malloc appearing to return the
    237  1.1  oster  * same pointer twice.  Since we have to lock it anyway, we might as well use it
    238  1.1  oster  * as the lock around the al_free_list.  Note that we can't call Free with the
    239  1.1  oster  * debug_mem_mutex locked.
    240  1.1  oster  */
    241  1.1  oster void rf_FreeAllocList(l)
    242  1.1  oster   RF_AllocListElem_t  *l;
    243  1.1  oster {
    244  1.1  oster   int i;
    245  1.1  oster   RF_AllocListElem_t *temp, *p;
    246  1.1  oster 
    247  1.1  oster   for (p=l; p; p=p->next) {
    248  1.1  oster     RF_ASSERT(p->numPointers >= 0 && p->numPointers <= RF_POINTERS_PER_ALLOC_LIST_ELEMENT);
    249  1.1  oster     for (i=0; i<p->numPointers; i++) {
    250  1.1  oster       RF_ASSERT(p->pointers[i]);
    251  1.1  oster       RF_Free(p->pointers[i], p->sizes[i]);
    252  1.1  oster     }
    253  1.1  oster   }
    254  1.1  oster #ifndef KERNEL
    255  1.1  oster   RF_LOCK_MUTEX(rf_debug_mem_mutex);
    256  1.1  oster #endif /* !KERNEL */
    257  1.1  oster   while (l) {
    258  1.1  oster     temp = l;
    259  1.1  oster     l = l->next;
    260  1.1  oster     if (al_free_list_count > RF_AL_FREELIST_MAX) {DO_FREE(temp, sizeof(*temp));}
    261  1.1  oster     else {temp->next = al_free_list; al_free_list = temp; al_free_list_count++;}
    262  1.1  oster   }
    263  1.1  oster #ifndef KERNEL
    264  1.1  oster   RF_UNLOCK_MUTEX(rf_debug_mem_mutex);
    265  1.1  oster #endif /* !KERNEL */
    266  1.1  oster }
    267  1.1  oster 
    268  1.1  oster RF_AllocListElem_t *rf_real_MakeAllocList(lockflag)
    269  1.1  oster   int  lockflag;
    270  1.1  oster {
    271  1.1  oster   RF_AllocListElem_t *p;
    272  1.1  oster 
    273  1.1  oster #ifndef KERNEL
    274  1.1  oster   if (lockflag) {  RF_LOCK_MUTEX(rf_debug_mem_mutex); }
    275  1.1  oster #endif /* !KERNEL */
    276  1.1  oster   if (al_free_list) {fl_hit_count++; p = al_free_list; al_free_list = p->next; al_free_list_count--;}
    277  1.1  oster   else {
    278  1.1  oster     fl_miss_count++;
    279  1.1  oster #ifndef KERNEL
    280  1.1  oster     p = (RF_AllocListElem_t *) malloc(sizeof(RF_AllocListElem_t));  /* can't use Malloc at user level b/c we already locked the mutex */
    281  1.1  oster #else /* !KERNEL */
    282  1.1  oster     RF_Malloc(p, sizeof(RF_AllocListElem_t), (RF_AllocListElem_t *));  /* no allocation locking in kernel, so this is fine */
    283  1.1  oster #endif /* !KERNEL */
    284  1.1  oster   }
    285  1.1  oster #ifndef KERNEL
    286  1.1  oster   if (lockflag) {  RF_UNLOCK_MUTEX(rf_debug_mem_mutex); }
    287  1.1  oster #endif /* !KERNEL */
    288  1.1  oster   if (p == NULL) {
    289  1.1  oster     return(NULL);
    290  1.1  oster   }
    291  1.1  oster   bzero((char *)p, sizeof(RF_AllocListElem_t));
    292  1.1  oster   return(p);
    293  1.1  oster }
    294