cache.c revision d358eea93ef0e6a70b6f43b06b3ad24f79f69cd1
/*
* Copyright (C) 1999-2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: cache.c,v 1.41 2001/06/07 18:31:54 gson Exp $ */
#include <config.h>
#include <dns/dbiterator.h>
#include <dns/masterdump.h>
/*
* The following three variables control incremental cleaning.
* MINSIZE is how many bytes is the floor for dns_cache_setcachesize().
* CLEANERINCREMENT is how many nodes are examined in one pass.
* CLEANERINTERVAL is the minimum amount of time between passes.
*/
/***
*** Types
***/
/*
* A cache_cleaner_t encapsulsates the state of the periodic
* cache cleaning.
*/
typedef struct cache_cleaner cache_cleaner_t;
typedef enum {
cleaner_s_idle, /* Waiting for cleaning-interval to expire. */
cleaner_s_busy, /* Currently cleaning. */
cleaner_s_done /* Freed enough memory after being overmem. */
/*
* Convenience macros for comprehensive assertion checking.
*/
(c)->resched_event != NULL)
(c)->resched_event == NULL)
/*
* Accesses to a cache cleaner object are synchronized through
*/
struct cache_cleaner {
unsigned int cleaning_interval; /* The cleaning-interval from
named.conf, in seconds. */
itself to reschedule */
int increment; /* Number of names to
clean in one increment */
};
/*
* The actual cache object.
*/
struct dns_cache {
/* Unlocked. */
unsigned int magic;
/* Locked by 'lock'. */
int references;
int live_tasks;
char *db_type;
int db_argc;
char **db_argv;
/* Locked by 'filelock'. */
char * filename;
/* Access to the on-disk cache file is also locked by 'filelock'. */
};
/***
*** Functions
***/
static isc_result_t
static void
static void
static void
static void
static inline isc_result_t
}
{
int i;
return (ISC_R_NOMEMORY);
if (result != ISC_R_SUCCESS) {
"isc_mutex_init() failed: %s",
goto cleanup_mem;
}
if (result != ISC_R_SUCCESS) {
"isc_mutex_init() failed: %s",
goto cleanup_lock;
}
cache->live_tasks = 0;
goto cleanup_filelock;
}
else {
goto cleanup_dbtype;
}
goto cleanup_dbargv;
}
}
}
if (result != ISC_R_SUCCESS)
goto cleanup_dbargv;
if (result != ISC_R_SUCCESS)
goto cleanup_db;
return (ISC_R_SUCCESS);
return (result);
}
static void
int i;
}
}
}
void
cache->references++;
}
void
cache->references--;
if (cache->references == 0) {
}
if (free_cache) {
/*
* When the cache is shut down, dump it to a file if one is
* specified.
*/
/*
* If the cleaner task exists, let it free the cache.
*/
if (cache->live_tasks > 0) {
}
}
if (free_cache)
}
void
}
char *newname;
return (ISC_R_NOMEMORY);
return (ISC_R_SUCCESS);
}
return (ISC_R_SUCCESS);
return (result);
}
return (ISC_R_SUCCESS);
return (result);
}
void
/*
* It may be the case that the cache has already shut down.
* If so, it has no timer.
*/
goto unlock;
if (t < DNS_CACHE_CLEANERINTERVAL)
if (t == 0) {
} else {
0);
}
}
/*
* Initialize the cache cleaner object at *cleaner.
* Space for the object must be allocated by the caller.
*/
static isc_result_t
{
if (result != ISC_R_SUCCESS) {
"isc_task_create() failed: %s",
goto cleanup;
}
if (result != ISC_R_SUCCESS) {
"cache cleaner: "
"isc_task_onshutdown() failed: %s",
goto cleanup;
}
if (result != ISC_R_SUCCESS) {
"isc_timer_create() failed: %s",
goto cleanup;
}
cleaner, sizeof(isc_event_t));
goto cleanup;
}
cleaner, sizeof(isc_event_t));
goto cleanup;
}
}
return (ISC_R_SUCCESS);
return (result);
}
static void
/*
* Create an iterator and position it at the beginning of the cache.
*/
if (result != ISC_R_SUCCESS)
"cache cleaner could not create "
else {
}
if (result != ISC_R_SUCCESS) {
/*
* If the result is ISC_R_NOMORE, the database is empty,
* so there is nothing to be cleaned.
*/
if (result != ISC_R_NOMORE)
"cache cleaner: "
"dns_dbiterator_first() failed: %s",
} else {
/*
* Pause the iterator to free its lock.
*/
"begin cache cleaning, mem inuse %d",
}
return;
}
static void
}
/*
* This is run once for every cache-cleaning-interval as defined in named.conf.
*/
static void
} else
/*
* incremental_cleaning_action() takes responsibility
* for freeing or preserving the event.
*/
}
/*
* This is called when the cache either surpasses its upper limit
* or shrinks beyond its lower limit.
*/
static void
} else {
/*
* end_cleaning() can't be called here because
* then both cleaner->overmem_event and
* cleaner->resched_event will point to this
* event. Set the state to done, and then
* when the incremental_cleaning_action() event
* is posted, it will handle the end_cleaning.
*/
}
}
/*
* Do incremental cleaning.
*/
static void
int n_names;
return;
}
while (n_names-- > 0) {
NULL);
if (result != ISC_R_SUCCESS) {
"cache cleaner: dns_dbiterator_current() "
return;
}
/*
* The node was not needed, but was required by
* dns_dbiterator_current(). Give up its reference.
*/
/*
* Step to the next node.
*/
if (result != ISC_R_SUCCESS) {
/*
* Either the end was reached (ISC_R_NOMORE) or
* some error was signaled. If the cache is still
* overmem and no error was encountered,
* keep trying to clean it, otherwise stop cleanng.
*/
if (result != ISC_R_NOMORE)
"cache cleaner: "
"dns_dbiterator_next() "
"failed: %s",
iterator);
if (result == ISC_R_SUCCESS) {
ISC_LOG_DEBUG(1),
"cache cleaner: "
"still overmem, "
"reset and try again");
continue;
}
}
return;
}
}
/*
* We have successfully performed a cleaning increment but have
* not gone through the entire cache. Free the iterator locks
* and reschedule another batch. If it fails, just try to continue
* anyway.
*/
"mem inuse %d, sleeping",
/*
* Even though the interval timer might be in use for periodic
* cache cleaning, steal it for scheduling another iteration.
* When end_cleaning() is finally called, it will reset the
* interval for the periodic cleaner.
*/
return;
}
/*
* Do immediate cleaning.
*/
if (result != ISC_R_SUCCESS)
return result;
while (result == ISC_R_SUCCESS) {
(dns_name_t *)NULL);
if (result != ISC_R_SUCCESS)
break;
/*
* Check TTLs, mark expired rdatasets stale.
*/
if (result != ISC_R_SUCCESS) {
"cache cleaner: dns_db_expirenode() "
"failed: %s",
/*
* Continue anyway.
*/
}
/*
* This is where the actual freeing takes place.
*/
}
if (result == ISC_R_NOMORE)
return (result);
}
static void
}
void
/*
* Impose a minumum cache size; pathological things happen if there
* is too little room.
*/
/*
* If the cache was overmem and cleaning, but now with the new limits
* it is no longer in an overmem condition, then the next
* isc_mem_put for cache memory will do the right thing and trigger
* water().
*/
/*
* Disable cache memory limiting.
*/
else
/*
* Establish new cache memory limits (either for the first
* time, or replacing other limits).
*/
}
/*
* The cleaner task is shutting down; do the necessary cleanup.
*/
static void
else
cache->live_tasks--;
if (cache->references == 0)
/*
* By detaching the timer in the context of its task,
* we are guaranteed that there will be no further timer
* events.
*/
/* Make sure we don't reschedule anymore. */
if (should_free)
}
if (result != ISC_R_SUCCESS)
return (result);
return (ISC_R_SUCCESS);
}