andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #include "primpl.h" andre@0: andre@0: #include andre@0: andre@0: /*****************************************************************************/ andre@0: /*****************************************************************************/ andre@0: /************************** File descriptor caching **************************/ andre@0: /*****************************************************************************/ andre@0: /*****************************************************************************/ andre@0: andre@0: /* andre@0: ** This code is built into debuggable versions of NSPR to assist in andre@0: ** finding misused file descriptors. Since file descritors (PRFileDesc) andre@0: ** are identified by a pointer to their structure, they can be the andre@0: ** target of dangling references. Furthermore, NSPR caches and tries andre@0: ** to aggressively reuse file descriptors, leading to more ambiguity. andre@0: ** The following code will allow a debugging client to set environment andre@0: ** variables and control the number of file descriptors that will be andre@0: ** preserved before they are recycled. The environment variables are andre@0: ** NSPR_FD_CACHE_SIZE_LOW and NSPR_FD_CACHE_SIZE_HIGH. The former sets andre@0: ** the number of descriptors NSPR will allocate before beginning to andre@0: ** recycle. The latter is the maximum number permitted in the cache andre@0: ** (exclusive of those in use) at a time. andre@0: */ andre@0: typedef struct _PR_Fd_Cache andre@0: { andre@0: PRLock *ml; andre@0: PRIntn count; andre@0: PRStack *stack; andre@0: PRFileDesc *head, *tail; andre@0: PRIntn limit_low, limit_high; andre@0: } _PR_Fd_Cache; andre@0: andre@0: static _PR_Fd_Cache _pr_fd_cache; andre@0: static PRFileDesc **stack2fd = &(((PRFileDesc*)NULL)->higher); andre@0: andre@0: andre@0: /* andre@0: ** Get a FileDescriptor from the cache if one exists. If not allocate andre@0: ** a new one from the heap. andre@0: */ andre@0: PRFileDesc *_PR_Getfd(void) andre@0: { andre@0: PRFileDesc *fd; andre@0: /* andre@0: ** $$$ andre@0: ** This may look a little wasteful. We'll see. Right now I want to andre@0: ** be able to toggle between caching and not at runtime to measure andre@0: ** the differences. If it isn't too annoying, I'll leave it in. andre@0: ** $$$$ andre@0: ** andre@0: ** The test is against _pr_fd_cache.limit_high. If that's zero, andre@0: ** we're not doing the extended cache but going for performance. andre@0: */ andre@0: if (0 == _pr_fd_cache.limit_high) andre@0: { andre@0: PRStackElem *pop; andre@0: PR_ASSERT(NULL != _pr_fd_cache.stack); andre@0: pop = PR_StackPop(_pr_fd_cache.stack); andre@0: if (NULL == pop) goto allocate; andre@0: fd = (PRFileDesc*)((PRPtrdiff)pop - (PRPtrdiff)stack2fd); andre@0: } andre@0: else andre@0: { andre@0: do andre@0: { andre@0: if (NULL == _pr_fd_cache.head) goto allocate; /* nothing there */ andre@0: if (_pr_fd_cache.count < _pr_fd_cache.limit_low) goto allocate; andre@0: andre@0: /* we "should" be able to extract an fd from the cache */ andre@0: PR_Lock(_pr_fd_cache.ml); /* need the lock to do this safely */ andre@0: fd = _pr_fd_cache.head; /* protected extraction */ andre@0: if (NULL == fd) /* unexpected, but not fatal */ andre@0: { andre@0: PR_ASSERT(0 == _pr_fd_cache.count); andre@0: PR_ASSERT(NULL == _pr_fd_cache.tail); andre@0: } andre@0: else andre@0: { andre@0: _pr_fd_cache.count -= 1; andre@0: _pr_fd_cache.head = fd->higher; andre@0: if (NULL == _pr_fd_cache.head) andre@0: { andre@0: PR_ASSERT(0 == _pr_fd_cache.count); andre@0: _pr_fd_cache.tail = NULL; andre@0: } andre@0: PR_ASSERT(&_pr_faulty_methods == fd->methods); andre@0: PR_ASSERT(PR_INVALID_IO_LAYER == fd->identity); andre@0: PR_ASSERT(_PR_FILEDESC_FREED == fd->secret->state); andre@0: } andre@0: PR_Unlock(_pr_fd_cache.ml); andre@0: andre@0: } while (NULL == fd); /* then go around and allocate a new one */ andre@0: } andre@0: andre@0: finished: andre@0: fd->dtor = NULL; andre@0: fd->lower = fd->higher = NULL; andre@0: fd->identity = PR_NSPR_IO_LAYER; andre@0: memset(fd->secret, 0, sizeof(PRFilePrivate)); andre@0: return fd; andre@0: andre@0: allocate: andre@0: fd = PR_NEW(PRFileDesc); andre@0: if (NULL != fd) andre@0: { andre@0: fd->secret = PR_NEW(PRFilePrivate); andre@0: if (NULL == fd->secret) PR_DELETE(fd); andre@0: } andre@0: if (NULL != fd) goto finished; andre@0: else return NULL; andre@0: andre@0: } /* _PR_Getfd */ andre@0: andre@0: /* andre@0: ** Return a file descriptor to the cache unless there are too many in andre@0: ** there already. If put in cache, clear the fields first. andre@0: */ andre@0: void _PR_Putfd(PRFileDesc *fd) andre@0: { andre@0: PR_ASSERT(PR_NSPR_IO_LAYER == fd->identity); andre@0: fd->methods = &_pr_faulty_methods; andre@0: fd->identity = PR_INVALID_IO_LAYER; andre@0: fd->secret->state = _PR_FILEDESC_FREED; andre@0: andre@0: if (0 == _pr_fd_cache.limit_high) andre@0: { andre@0: PR_StackPush(_pr_fd_cache.stack, (PRStackElem*)(&fd->higher)); andre@0: } andre@0: else andre@0: { andre@0: if (_pr_fd_cache.count > _pr_fd_cache.limit_high) andre@0: { andre@0: PR_Free(fd->secret); andre@0: PR_Free(fd); andre@0: } andre@0: else andre@0: { andre@0: PR_Lock(_pr_fd_cache.ml); andre@0: if (NULL == _pr_fd_cache.tail) andre@0: { andre@0: PR_ASSERT(0 == _pr_fd_cache.count); andre@0: PR_ASSERT(NULL == _pr_fd_cache.head); andre@0: _pr_fd_cache.head = _pr_fd_cache.tail = fd; andre@0: } andre@0: else andre@0: { andre@0: PR_ASSERT(NULL == _pr_fd_cache.tail->higher); andre@0: _pr_fd_cache.tail->higher = fd; andre@0: _pr_fd_cache.tail = fd; /* new value */ andre@0: } andre@0: fd->higher = NULL; /* always so */ andre@0: _pr_fd_cache.count += 1; /* count the new entry */ andre@0: PR_Unlock(_pr_fd_cache.ml); andre@0: } andre@0: } andre@0: } /* _PR_Putfd */ andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_SetFDCacheSize(PRIntn low, PRIntn high) andre@0: { andre@0: /* andre@0: ** This can be called at any time, may adjust the cache sizes, andre@0: ** turn the caches off, or turn them on. It is not dependent andre@0: ** on the compilation setting of DEBUG. andre@0: */ andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: if (low > high) low = high; /* sanity check the params */ andre@0: andre@0: PR_Lock(_pr_fd_cache.ml); andre@0: if (0 == high) /* shutting down or staying down */ andre@0: { andre@0: if (0 != _pr_fd_cache.limit_high) /* shutting down */ andre@0: { andre@0: _pr_fd_cache.limit_high = 0; /* stop use */ andre@0: /* andre@0: ** Hold the lock throughout - nobody's going to want it andre@0: ** other than another caller to this routine. Just don't andre@0: ** let that happen. andre@0: ** andre@0: ** Put all the cached fds onto the new cache. andre@0: */ andre@0: while (NULL != _pr_fd_cache.head) andre@0: { andre@0: PRFileDesc *fd = _pr_fd_cache.head; andre@0: _pr_fd_cache.head = fd->higher; andre@0: PR_StackPush(_pr_fd_cache.stack, (PRStackElem*)(&fd->higher)); andre@0: } andre@0: _pr_fd_cache.limit_low = 0; andre@0: _pr_fd_cache.tail = NULL; andre@0: _pr_fd_cache.count = 0; andre@0: } andre@0: } andre@0: else /* starting up or just adjusting parameters */ andre@0: { andre@0: PRBool was_using_stack = (0 == _pr_fd_cache.limit_high); andre@0: _pr_fd_cache.limit_low = low; andre@0: _pr_fd_cache.limit_high = high; andre@0: if (was_using_stack) /* was using stack - feed into cache */ andre@0: { andre@0: PRStackElem *pop; andre@0: while (NULL != (pop = PR_StackPop(_pr_fd_cache.stack))) andre@0: { andre@0: PRFileDesc *fd = (PRFileDesc*) andre@0: ((PRPtrdiff)pop - (PRPtrdiff)stack2fd); andre@0: if (NULL == _pr_fd_cache.tail) _pr_fd_cache.tail = fd; andre@0: fd->higher = _pr_fd_cache.head; andre@0: _pr_fd_cache.head = fd; andre@0: _pr_fd_cache.count += 1; andre@0: } andre@0: } andre@0: } andre@0: PR_Unlock(_pr_fd_cache.ml); andre@0: return PR_SUCCESS; andre@0: } /* PR_SetFDCacheSize */ andre@0: andre@0: void _PR_InitFdCache(void) andre@0: { andre@0: /* andre@0: ** The fd caching is enabled by default for DEBUG builds, andre@0: ** disabled by default for OPT builds. That default can andre@0: ** be overridden at runtime using environment variables andre@0: ** or a super-wiz-bang API. andre@0: */ andre@0: const char *low = PR_GetEnv("NSPR_FD_CACHE_SIZE_LOW"); andre@0: const char *high = PR_GetEnv("NSPR_FD_CACHE_SIZE_HIGH"); andre@0: andre@0: /* andre@0: ** _low is allowed to be zero, _high is not. andre@0: ** If _high is zero, we're not doing the caching. andre@0: */ andre@0: andre@0: _pr_fd_cache.limit_low = 0; andre@0: #if defined(DEBUG) andre@0: _pr_fd_cache.limit_high = FD_SETSIZE; andre@0: #else andre@0: _pr_fd_cache.limit_high = 0; andre@0: #endif /* defined(DEBUG) */ andre@0: andre@0: if (NULL != low) _pr_fd_cache.limit_low = atoi(low); andre@0: if (NULL != high) _pr_fd_cache.limit_high = atoi(high); andre@0: andre@0: if (_pr_fd_cache.limit_low < 0) andre@0: _pr_fd_cache.limit_low = 0; andre@0: if (_pr_fd_cache.limit_low > FD_SETSIZE) andre@0: _pr_fd_cache.limit_low = FD_SETSIZE; andre@0: andre@0: if (_pr_fd_cache.limit_high > FD_SETSIZE) andre@0: _pr_fd_cache.limit_high = FD_SETSIZE; andre@0: andre@0: if (_pr_fd_cache.limit_high < _pr_fd_cache.limit_low) andre@0: _pr_fd_cache.limit_high = _pr_fd_cache.limit_low; andre@0: andre@0: _pr_fd_cache.ml = PR_NewLock(); andre@0: PR_ASSERT(NULL != _pr_fd_cache.ml); andre@0: _pr_fd_cache.stack = PR_CreateStack("FD"); andre@0: PR_ASSERT(NULL != _pr_fd_cache.stack); andre@0: andre@0: } /* _PR_InitFdCache */ andre@0: andre@0: void _PR_CleanupFdCache(void) andre@0: { andre@0: PRFileDesc *fd, *next; andre@0: PRStackElem *pop; andre@0: andre@0: for (fd = _pr_fd_cache.head; fd != NULL; fd = next) andre@0: { andre@0: next = fd->higher; andre@0: PR_DELETE(fd->secret); andre@0: PR_DELETE(fd); andre@0: } andre@0: _pr_fd_cache.head = NULL; andre@0: _pr_fd_cache.tail = NULL; andre@0: _pr_fd_cache.count = 0; andre@0: PR_DestroyLock(_pr_fd_cache.ml); andre@0: _pr_fd_cache.ml = NULL; andre@0: while ((pop = PR_StackPop(_pr_fd_cache.stack)) != NULL) andre@0: { andre@0: fd = (PRFileDesc*)((PRPtrdiff)pop - (PRPtrdiff)stack2fd); andre@0: PR_DELETE(fd->secret); andre@0: PR_DELETE(fd); andre@0: } andre@0: PR_DestroyStack(_pr_fd_cache.stack); andre@0: _pr_fd_cache.stack = NULL; andre@0: } /* _PR_CleanupFdCache */ andre@0: andre@0: /* prfdcach.c */