2006-07-16 01:46:36 +08:00
|
|
|
/*
|
2010-04-29 05:57:11 +08:00
|
|
|
* Copyright (c) 2004-2006 Hyperic, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2006-07-16 01:46:36 +08:00
|
|
|
*/
|
|
|
|
|
2004-12-05 06:44:55 +08:00
|
|
|
#include "sigar.h"
|
|
|
|
#include "sigar_private.h"
|
|
|
|
#include "sigar_util.h"
|
2005-12-13 09:07:43 +08:00
|
|
|
#include <stdio.h>
|
2004-12-05 06:44:55 +08:00
|
|
|
/*
|
|
|
|
* hash table to cache values where key is a unique number
|
|
|
|
* such as:
|
|
|
|
* pid -> some process data
|
|
|
|
* uid -> user name
|
|
|
|
* gid -> group name
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define ENTRIES_SIZE(n) \
|
|
|
|
(sizeof(sigar_cache_entry_t *) * (n))
|
|
|
|
|
2005-11-10 03:31:02 +08:00
|
|
|
/* wrap free() for use w/ dmalloc */
|
|
|
|
static void free_value(void *ptr)
|
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
2013-09-15 20:00:23 +08:00
|
|
|
sigar_cache_t *sigar_expired_cache_new(int size, sigar_uint64_t cleanup_period_millis, sigar_uint64_t entry_expire_period)
|
2004-12-05 06:44:55 +08:00
|
|
|
{
|
|
|
|
sigar_cache_t *table = malloc(sizeof(*table));
|
|
|
|
table->count = 0;
|
|
|
|
table->size = size;
|
|
|
|
table->entries = malloc(ENTRIES_SIZE(size));
|
|
|
|
memset(table->entries, '\0', ENTRIES_SIZE(size));
|
2005-11-10 03:31:02 +08:00
|
|
|
table->free_value = free_value;
|
2013-09-15 20:00:23 +08:00
|
|
|
table->cleanup_period_millis = cleanup_period_millis;
|
|
|
|
table->last_cleanup_time = sigar_time_now_millis();
|
|
|
|
table->entry_expire_period = entry_expire_period;
|
2004-12-05 06:44:55 +08:00
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
2013-09-15 20:00:23 +08:00
|
|
|
sigar_cache_t *sigar_cache_new(int size)
|
|
|
|
{
|
|
|
|
return sigar_expired_cache_new(size, SIGAR_FIELD_NOTIMPL, SIGAR_FIELD_NOTIMPL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*#ifdef DEBUG_CACHE*/
|
2005-12-13 09:07:43 +08:00
|
|
|
/* see how well entries are distributed */
|
2013-09-15 20:00:23 +08:00
|
|
|
void sigar_cache_dump(sigar_cache_t *table)
|
2005-12-13 09:07:43 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
sigar_cache_entry_t **entries = table->entries;
|
2013-09-15 20:00:23 +08:00
|
|
|
printf("table size %lu\n", (long)table->size);
|
|
|
|
printf("table count %lu\n", (long)table->count);
|
|
|
|
|
2005-12-13 09:07:43 +08:00
|
|
|
for (i=0; i<table->size; i++) {
|
|
|
|
sigar_cache_entry_t *entry = *entries++;
|
|
|
|
|
|
|
|
printf("|");
|
|
|
|
while (entry) {
|
|
|
|
printf("%lld", entry->id);
|
|
|
|
if (entry->next) {
|
|
|
|
printf(",");
|
|
|
|
}
|
|
|
|
entry = entry->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2013-09-15 20:00:23 +08:00
|
|
|
/*#endif*/
|
2005-12-13 09:07:43 +08:00
|
|
|
|
|
|
|
static void sigar_cache_rehash(sigar_cache_t *table)
|
|
|
|
{
|
|
|
|
int i;
|
2013-09-15 20:00:23 +08:00
|
|
|
unsigned int new_size = table->count * 2 + 1;
|
2005-12-13 09:07:43 +08:00
|
|
|
sigar_cache_entry_t **entries = table->entries;
|
|
|
|
sigar_cache_entry_t **new_entries =
|
|
|
|
malloc(ENTRIES_SIZE(new_size));
|
|
|
|
|
|
|
|
memset(new_entries, '\0', ENTRIES_SIZE(new_size));
|
|
|
|
|
|
|
|
for (i=0; i<table->size; i++) {
|
|
|
|
sigar_cache_entry_t *entry = *entries++;
|
|
|
|
|
|
|
|
while (entry) {
|
|
|
|
sigar_cache_entry_t *next = entry->next;
|
|
|
|
sigar_uint64_t hash = entry->id % new_size;
|
|
|
|
|
|
|
|
entry->next = new_entries[hash];
|
|
|
|
new_entries[hash] = entry;
|
|
|
|
entry = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(table->entries);
|
|
|
|
table->entries = new_entries;
|
|
|
|
table->size = new_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SIGAR_CACHE_IX(t, k) \
|
|
|
|
t->entries + (k % t->size)
|
|
|
|
|
2013-09-15 20:00:23 +08:00
|
|
|
void sigar_perform_cleanup_if_necessary(sigar_cache_t *table) {
|
2013-09-15 22:14:42 +08:00
|
|
|
sigar_uint64_t current_time;
|
|
|
|
int i;
|
|
|
|
sigar_cache_entry_t **entries;
|
2013-09-15 20:00:23 +08:00
|
|
|
if (table->cleanup_period_millis == SIGAR_FIELD_NOTIMPL) {
|
2013-09-15 22:14:42 +08:00
|
|
|
/* no cleanup for this cache) */
|
|
|
|
return;
|
2013-09-15 20:00:23 +08:00
|
|
|
}
|
2013-09-15 22:14:42 +08:00
|
|
|
current_time = sigar_time_now_millis();
|
2013-09-15 20:00:23 +08:00
|
|
|
if ((current_time - table->last_cleanup_time) < table->cleanup_period_millis) {
|
|
|
|
/* not enough time has passed since last cleanup */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-15 22:14:42 +08:00
|
|
|
/* performing cleanup */
|
|
|
|
entries = table->entries;
|
2013-09-15 20:00:23 +08:00
|
|
|
|
|
|
|
table->last_cleanup_time = current_time;
|
|
|
|
|
|
|
|
for (i=0; i<table->size; i++) {
|
|
|
|
sigar_cache_entry_t *entry, *ptr, *entry_prev=NULL, **entry_in_table;
|
|
|
|
entry_in_table = entries;
|
|
|
|
entry = *entries++;
|
|
|
|
|
|
|
|
while (entry) {
|
2013-09-15 22:14:42 +08:00
|
|
|
sigar_uint64_t period_with_no_access = current_time - entry->last_access_time;
|
|
|
|
ptr = entry->next;
|
2013-09-15 20:00:23 +08:00
|
|
|
if (table->entry_expire_period < period_with_no_access) {
|
2013-09-15 22:14:42 +08:00
|
|
|
/* no one acess this entry for too long - we can delete it */
|
|
|
|
if (entry->value) {
|
2013-09-15 20:00:23 +08:00
|
|
|
table->free_value(entry->value);
|
|
|
|
}
|
|
|
|
free(entry);
|
2013-09-15 22:14:42 +08:00
|
|
|
table->count--;
|
2013-09-15 20:00:23 +08:00
|
|
|
if (entry_prev != NULL) {
|
|
|
|
entry_prev->next = ptr;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* removing first entry - head of list should point to next entry */
|
|
|
|
*entry_in_table = ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* entry not expired - advance entry_prev to current entry*/
|
|
|
|
entry_prev = entry;
|
2013-09-15 22:14:42 +08:00
|
|
|
}
|
2013-09-15 20:00:23 +08:00
|
|
|
entry = ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (table->count < (table->size/4)) {
|
|
|
|
/* hash table (the array size) too big for the amount of values it contains perform rehash */
|
|
|
|
sigar_cache_rehash(table);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-06-20 08:13:12 +08:00
|
|
|
sigar_cache_entry_t *sigar_cache_find(sigar_cache_t *table,
|
|
|
|
sigar_uint64_t key)
|
|
|
|
{
|
|
|
|
sigar_cache_entry_t *entry, **ptr;
|
2013-09-15 20:00:23 +08:00
|
|
|
sigar_perform_cleanup_if_necessary(table);
|
2006-06-20 08:13:12 +08:00
|
|
|
|
|
|
|
for (ptr = SIGAR_CACHE_IX(table, key), entry = *ptr;
|
|
|
|
entry;
|
|
|
|
ptr = &entry->next, entry = *ptr)
|
|
|
|
{
|
|
|
|
if (entry->id == key) {
|
2013-09-15 20:00:23 +08:00
|
|
|
entry->last_access_time = sigar_time_now_millis();
|
2006-06-20 08:13:12 +08:00
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create entry if it does not exist */
|
2004-12-05 06:44:55 +08:00
|
|
|
sigar_cache_entry_t *sigar_cache_get(sigar_cache_t *table,
|
|
|
|
sigar_uint64_t key)
|
|
|
|
{
|
|
|
|
sigar_cache_entry_t *entry, **ptr;
|
2013-09-15 20:00:23 +08:00
|
|
|
sigar_perform_cleanup_if_necessary(table);
|
2004-12-05 06:44:55 +08:00
|
|
|
|
2005-12-13 09:07:43 +08:00
|
|
|
for (ptr = SIGAR_CACHE_IX(table, key), entry = *ptr;
|
|
|
|
entry;
|
|
|
|
ptr = &entry->next, entry = *ptr)
|
2004-12-05 06:44:55 +08:00
|
|
|
{
|
|
|
|
if (entry->id == key) {
|
2013-09-15 20:00:23 +08:00
|
|
|
entry->last_access_time = sigar_time_now_millis();
|
2004-12-05 06:44:55 +08:00
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-15 20:00:23 +08:00
|
|
|
if (++table->count > table->size) {
|
2005-12-13 09:07:43 +08:00
|
|
|
sigar_cache_rehash(table);
|
2004-12-05 06:44:55 +08:00
|
|
|
|
2005-12-13 09:07:43 +08:00
|
|
|
for (ptr = SIGAR_CACHE_IX(table, key), entry = *ptr;
|
|
|
|
entry;
|
|
|
|
ptr = &entry->next, entry = *ptr)
|
|
|
|
{
|
|
|
|
}
|
2004-12-05 06:44:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
*ptr = entry = malloc(sizeof(*entry));
|
|
|
|
entry->id = key;
|
|
|
|
entry->value = NULL;
|
|
|
|
entry->next = NULL;
|
2013-09-15 20:00:23 +08:00
|
|
|
entry->last_access_time = sigar_time_now_millis();
|
2004-12-05 06:44:55 +08:00
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sigar_cache_destroy(sigar_cache_t *table)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
sigar_cache_entry_t **entries = table->entries;
|
|
|
|
|
2005-12-13 09:07:43 +08:00
|
|
|
#ifdef DEBUG_CACHE
|
|
|
|
sigar_cache_dump(table);
|
|
|
|
#endif
|
|
|
|
|
2004-12-05 06:44:55 +08:00
|
|
|
for (i=0; i<table->size; i++) {
|
|
|
|
sigar_cache_entry_t *entry, *ptr;
|
2005-11-10 03:45:52 +08:00
|
|
|
entry = *entries++;
|
2004-12-05 06:44:55 +08:00
|
|
|
|
2005-11-10 03:45:52 +08:00
|
|
|
while (entry) {
|
|
|
|
if (entry->value) {
|
|
|
|
table->free_value(entry->value);
|
2004-12-05 06:44:55 +08:00
|
|
|
}
|
2005-11-10 03:45:52 +08:00
|
|
|
ptr = entry->next;
|
|
|
|
free(entry);
|
|
|
|
entry = ptr;
|
|
|
|
}
|
2004-12-05 06:44:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
free(table->entries);
|
2004-12-06 05:40:56 +08:00
|
|
|
free(table);
|
2004-12-05 06:44:55 +08:00
|
|
|
}
|