cache.c revision 03609d0b8f6e458d6bd4b460f4eb3f7270f487ef
/*
* 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.35 2001/03/08 01:22:48 tale Exp $ */
#include <config.h>
#include <dns/dbiterator.h>
#include <dns/masterdump.h>
/***
*** 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)
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;
/* 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
{
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;
if (result != ISC_R_SUCCESS)
goto cleanup_filelock;
if (result != ISC_R_SUCCESS)
goto cleanup_db;
return (ISC_R_SUCCESS);
return (result);
}
static void
}
}
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.
*/
/* XXXRTH This is not locked! */
if (cache->live_tasks > 0)
else
}
}
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 == 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
"cache cleaner did not finish "
"in one cleaning-interval");
}
static void
} else {
}
}
/*
* 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;
}
/*
* 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.
*/
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 -- this should perhaps be larger,
* but for now is just meant ensure that hiwater and lowater are
* less than the size.
*/
size = 8;
/*
* This resets the overmem status if already in an overmem condition,
* even though the cache is perhaps still overmem even with new values.
* The overmem condition will be resignaled.
* XXXDCL i am not sure this is entirely right. what happens to
* the active cleaner?
*/
/*
* 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
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)
}