Skip to content

Commit

Permalink
add beast_clean_cache() function
Browse files Browse the repository at this point in the history
  • Loading branch information
xusong.lie committed May 24, 2017
1 parent 57b7c46 commit 44b62a9
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 89 deletions.
9 changes: 8 additions & 1 deletion beast.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ typedef struct yy_buffer_state *YY_BUFFER_STATE;
#error php-beast do not support ZTS mode
#endif

#define BEAST_VERSION "2.6"
#define BEAST_VERSION "2.7"
#define DEFAULT_CACHE_SIZE 10485760 /* 10MB */
#define HEADER_MAX_SIZE 256
#define INT_SIZE (sizeof(int))
Expand Down Expand Up @@ -117,6 +117,7 @@ zend_function_entry beast_functions[] = {
PHP_FE(beast_avail_cache, NULL)
PHP_FE(beast_support_filesize, NULL)
PHP_FE(beast_file_expire, NULL)
PHP_FE(beast_clean_cache, NULL)
{NULL, NULL, NULL} /* Must be the last line in beast_functions[] */
};
/* }}} */
Expand Down Expand Up @@ -1525,6 +1526,12 @@ PHP_FUNCTION(beast_support_filesize)
RETURN_LONG(beast_max_filesize);
}


PHP_FUNCTION(beast_clean_cache)
{
beast_cache_flush();
}

/*
* Local variables:
* tab-width: 4
Expand Down
147 changes: 62 additions & 85 deletions beast_mm.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

#define BEAST_SEGMENT_DEFAULT_SIZE (256 * 1024)

#define _BLOCKAT(offset) ((beast_block_t *)((char *)shmaddr + (offset)))
#define _OFFSET(block) ((int)(((char *)(block)) - (char *)shmaddr))
#define _BLOCKAT(addr, offset) ((beast_block_t *)((char *)addr + (offset)))
#define _OFFSET(block, addr) ((int)(((char *)(block)) - (char *)addr))

#ifdef max
#undef max
Expand Down Expand Up @@ -58,7 +58,6 @@ inline void beast_mm_unlock()
beast_spinunlock(mm_lock, beast_pid);
}


/*
* memory align function
* @param bits, align bits
Expand Down Expand Up @@ -86,8 +85,8 @@ static int beast_mm_allocate(void *shmaddr, int size)
int minsize; /* for finding best fit */

/* Realsize must be aligned to a word boundary on some architectures. */
realsize = beast_mm_alignmem(
max(size + beast_mm_alignmem(sizeof(int)), sizeof(beast_block_t)));
realsize = size + beast_mm_alignmem(sizeof(int));
realsize = beast_mm_alignmem(max(realsize, sizeof(beast_block_t)));

/*
* First, insure that the segment contains at least realsize free bytes,
Expand All @@ -100,79 +99,82 @@ static int beast_mm_allocate(void *shmaddr, int size)
return -1;
}

prvbestfit = 0; /* best block prev's node */
prvbestfit = 0; /* Best block prev's node */
minsize = INT_MAX;

prv = _BLOCKAT(sizeof(beast_header_t)); /* free list header */
prv = _BLOCKAT(shmaddr, sizeof(beast_header_t)); /* Free list header */

while (prv->next != 0) {
cur = _BLOCKAT(prv->next); /* current active block */
cur = _BLOCKAT(shmaddr, prv->next); /* Current active block */
if (cur->size == realsize) {
prvbestfit = prv;
break;
}
else if (cur->size > (sizeof(beast_block_t) + realsize) &&
cur->size < minsize)
else if (cur->size > (sizeof(beast_block_t) + realsize)
&& cur->size < minsize)
{
prvbestfit = prv;
minsize = cur->size;
}
prv = cur;
}

if (prvbestfit == 0) { /* not found best block */
if (prvbestfit == 0) { /* Not found best block */
return -1;
}

prv = prvbestfit;
cur = _BLOCKAT(prv->next);
cur = _BLOCKAT(shmaddr, prv->next);

/* update the block header */
header->avail -= realsize;

if (cur->size == realsize) {
prv->next = cur->next;

} else {
beast_block_t *nxt; /* the new block (chopped part of cur) */
int nxtoffset; /* offset of the block currently after cur */
int oldsize; /* size of cur before split */
beast_block_t *nxt; /* The new block (chopped part of cur) */
int nxtoffset; /* Offset of the block currently after cur */
int oldsize; /* Size of cur before split */

/* bestfit is too big; split it into two smaller blocks */
nxtoffset = cur->next;
oldsize = cur->size;
prv->next += realsize;
cur->size = realsize;
nxt = _BLOCKAT(prv->next);
nxt = _BLOCKAT(shmaddr, prv->next);
nxt->next = nxtoffset;
nxt->size = oldsize - realsize;
}

return _OFFSET(cur) + beast_mm_alignmem(sizeof(int)); /* skip size field */
}
/* skip size field */

return _OFFSET(cur, shmaddr) + beast_mm_alignmem(sizeof(int));
}

static int beast_mm_deallocate(void *shmaddr, int offset)
{
beast_header_t *header; /* header of shared memory segment */
beast_block_t *cur; /* the new block to insert */
beast_block_t *prv; /* the block before cur */
beast_block_t *nxt; /* the block after cur */
int size; /* size of deallocated block */
beast_header_t *header; /* Header of shared memory segment */
beast_block_t *cur; /* The new block to insert */
beast_block_t *prv; /* The block before cur */
beast_block_t *nxt; /* The block after cur */
int size; /* Size of deallocated block */

offset -= beast_mm_alignmem(sizeof(int)); /* really offset */
offset -= beast_mm_alignmem(sizeof(int)); /* Really offset */

/* Find position of new block in free list */
prv = _BLOCKAT(shmaddr, sizeof(beast_header_t));

/* find position of new block in free list */
prv = _BLOCKAT(sizeof(beast_header_t));
while (prv->next != 0 && prv->next < offset) {
prv = _BLOCKAT(prv->next);
prv = _BLOCKAT(shmaddr, prv->next);
}

/* insert new block after prv */
cur = _BLOCKAT(offset);
/* Insert new block after prv */
cur = _BLOCKAT(shmaddr, offset);
cur->next = prv->next;
prv->next = offset;

/* update the block header */
/* Update the block header */
header = (beast_header_t *)shmaddr;
header->avail += cur->size;
size = cur->size;
Expand All @@ -184,7 +186,7 @@ static int beast_mm_deallocate(void *shmaddr, int offset)
cur = prv;
}

nxt = _BLOCKAT(cur->next);
nxt = _BLOCKAT(shmaddr, cur->next);
if (((char *)cur) + cur->size == (char *) nxt) {
/* cur and nxt shared an edge, combine them */
cur->size += nxt->size;
Expand All @@ -194,27 +196,46 @@ static int beast_mm_deallocate(void *shmaddr, int offset)
return size;
}

void beast_mm_reinit()
{
beast_header_t *header;
beast_block_t *block;

header = (beast_header_t *)beast_mm_block;
header->segsize = beast_mm_block_size;
header->avail = beast_mm_block_size
- sizeof(beast_header_t)
- sizeof(beast_block_t)
- beast_mm_alignmem(sizeof(int));

/* the free list head block node */
block = _BLOCKAT(beast_mm_block, sizeof(beast_header_t));
block->size = 0;
block->next = sizeof(beast_header_t) + sizeof(beast_block_t);

/* the avail block */
block = _BLOCKAT(beast_mm_block, block->next);
block->size = header->avail;
block->next = 0;
}

/*
* init memory manager
*/
int beast_mm_init(int block_size)
{
beast_header_t *header;
beast_block_t *block;
void *shmaddr;

if (beast_mm_initialized) {
return 0;
}

/* init memory manager lock */
mm_lock = (int *)beast_shm_alloc(sizeof(int));
mm_lock = (int *)beast_shm_alloc(sizeof(beast_atomic_t));
if (!mm_lock) {
beast_write_log(beast_log_error,
"Unable alloc share memory for memory manager lock");
return -1;
}

*mm_lock = 0;

/* init share memory for beast */
Expand All @@ -224,38 +245,21 @@ int beast_mm_init(int block_size)
beast_mm_block_size = block_size;
}

shmaddr = beast_mm_block = (void *)beast_shm_alloc(beast_mm_block_size);
beast_mm_block = (void *)beast_shm_alloc(beast_mm_block_size);
if (!beast_mm_block) {
beast_write_log(beast_log_error,
"Unable alloc share memory for beast");
beast_shm_free((void *)mm_lock, sizeof(int));
beast_shm_free((void *)mm_lock, sizeof(beast_atomic_t));
return -1;
}

header = (beast_header_t *)beast_mm_block;
header->segsize = beast_mm_block_size;
/* avail size */
header->avail = beast_mm_block_size
- sizeof(beast_header_t)
- sizeof(beast_block_t)
- beast_mm_alignmem(sizeof(int));

/* the free list head block node */
block = _BLOCKAT(sizeof(beast_header_t));
block->size = 0;
block->next = sizeof(beast_header_t) + sizeof(beast_block_t);

/* the avail block */
block = _BLOCKAT(block->next);
block->size = header->avail;
block->next = 0;
beast_mm_reinit();

beast_mm_initialized = 1;

return 0;
}


void *beast_mm_malloc(int size)
{
int offset;
Expand All @@ -273,7 +277,6 @@ void *beast_mm_malloc(int size)
return p;
}


void *beast_mm_calloc(int size)
{
int offset;
Expand All @@ -295,7 +298,6 @@ void *beast_mm_calloc(int size)
return p;
}


void beast_mm_free(void *p)
{
int offset;
Expand All @@ -310,36 +312,13 @@ void beast_mm_free(void *p)
beast_mm_unlock();
}


void beast_mm_flush()
{
beast_header_t *header;
beast_block_t *block;
void *shmaddr;

beast_mm_lock();

shmaddr = beast_mm_block;
header = (beast_header_t *)shmaddr;
header->avail = beast_mm_block_size
- sizeof(beast_header_t)
- sizeof(beast_block_t)
- beast_mm_alignmem(sizeof(int));

/* the free list head block node */
block = _BLOCKAT(sizeof(beast_header_t));
block->size = 0;
block->next = sizeof(beast_header_t) + sizeof(beast_block_t);

/* the avail block */
block = _BLOCKAT(block->next);
block->size = header->avail;
block->next = 0;

beast_mm_reinit();
beast_mm_unlock();
}


/*
* Get the avail's memory space
*/
Expand All @@ -355,7 +334,6 @@ int beast_mm_availspace()
return size;
}


/*
* Don't locked here, because the segsize not change forever
*/
Expand All @@ -364,15 +342,14 @@ int beast_mm_realspace()
return ((beast_header_t *)beast_mm_block)->segsize;
}


/*
* Destroy memory's manager
*/
void beast_mm_destroy()
{
if (beast_mm_initialized) {
beast_shm_free((void *)beast_mm_block, beast_mm_block_size);
beast_shm_free((void *)mm_lock, sizeof(int));
beast_shm_free((void *)mm_lock, sizeof(beast_atomic_t));
beast_mm_initialized = 0;
}
}
23 changes: 20 additions & 3 deletions cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "beast_log.h"
#include "shm.h"

#define BUCKETS_DEFAULT_SIZE 1021
#define BUCKETS_DEFAULT_SIZE 8191

static int beast_cache_initialization = 0;
static cache_item_t **beast_cache_buckets = NULL;
Expand Down Expand Up @@ -205,9 +205,9 @@ int beast_cache_destroy()
return 0;
}

beast_mm_destroy(); /* destroy memory manager */
beast_mm_destroy(); /* Destroy memory manager */

/* free cache buckets's mmap memory */
/* Free cache buckets's mmap memory */
beast_shm_free((void *)beast_cache_buckets,
sizeof(cache_item_t *) * BUCKETS_DEFAULT_SIZE);
beast_shm_free((void *)cache_lock, sizeof(int));
Expand Down Expand Up @@ -237,3 +237,20 @@ void beast_cache_info(zval *retval)

beast_cache_unlock();
}

void beast_cache_flush()
{
int index;

beast_cache_lock();

/* Clean hash buckets */
for (index = 0; index < BUCKETS_DEFAULT_SIZE; index++) {
beast_cache_buckets[index] = NULL;
}

/* Clean share memory */
beast_mm_flush();

beast_cache_unlock();
}
1 change: 1 addition & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ cache_item_t *beast_cache_find(cache_key_t *key);
cache_item_t *beast_cache_create(cache_key_t *key);
cache_item_t *beast_cache_push(cache_item_t *item);
int beast_cache_destroy();
void beast_cache_flush();

void beast_cache_lock();
void beast_cache_unlock();
Expand Down
Loading

0 comments on commit 44b62a9

Please sign in to comment.