mirror of
https://github.com/redis/redis.git
synced 2026-04-21 03:01:35 -04:00
Make full use of aofrwblock's buf (#8975)
Make aof rewrite buffer memory size more accurate, before, there may be 20% deviation with its real memory usage. The implication are both lower memory usage, and also a more accurate INFO.
This commit is contained in:
26
src/aof.c
26
src/aof.c
@@ -61,6 +61,11 @@ void aofClosePipes(void);
|
||||
|
||||
typedef struct aofrwblock {
|
||||
unsigned long used, free, pos;
|
||||
/* Note that 'buf' must be the last field of aofrwblock struct, because
|
||||
* memory allocator may give us more memory than our apply for reducing
|
||||
* fragments, but we want to make full use of given memory, i.e. we may
|
||||
* access the memory after 'buf'. To avoid make others fields corrupt,
|
||||
* 'buf' must be the last one. */
|
||||
char buf[AOF_RW_BUF_BLOCK_SIZE];
|
||||
} aofrwblock;
|
||||
|
||||
@@ -89,6 +94,22 @@ unsigned long aofRewriteBufferSize(void) {
|
||||
return size;
|
||||
}
|
||||
|
||||
/* This function is different from aofRewriteBufferSize, to get memory usage,
|
||||
* we should also count all other fields(except 'buf') of aofrwblock and the
|
||||
* last block's free size. */
|
||||
unsigned long aofRewriteBufferMemoryUsage(void) {
|
||||
unsigned long size = aofRewriteBufferSize();
|
||||
|
||||
listNode *ln = listLast(server.aof_rewrite_buf_blocks);
|
||||
if (ln != NULL) {
|
||||
aofrwblock *block = listNodeValue(ln);
|
||||
size += block->free;
|
||||
size += (offsetof(aofrwblock,buf) *
|
||||
listLength(server.aof_rewrite_buf_blocks));
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
/* Event handler used to send data to the child process doing the AOF
|
||||
* rewrite. We send pieces of our AOF differences buffer so that the final
|
||||
* write when the child finishes the rewrite will be small. */
|
||||
@@ -144,9 +165,10 @@ void aofRewriteBufferAppend(unsigned char *s, unsigned long len) {
|
||||
|
||||
if (len) { /* First block to allocate, or need another block. */
|
||||
int numblocks;
|
||||
size_t usable_size;
|
||||
|
||||
block = zmalloc(sizeof(*block));
|
||||
block->free = AOF_RW_BUF_BLOCK_SIZE;
|
||||
block = zmalloc_usable(sizeof(*block), &usable_size);
|
||||
block->free = usable_size-offsetof(aofrwblock,buf);
|
||||
block->used = 0;
|
||||
block->pos = 0;
|
||||
listAddNodeTail(server.aof_rewrite_buf_blocks,block);
|
||||
|
||||
@@ -342,7 +342,7 @@ size_t freeMemoryGetNotCountedMemory(void) {
|
||||
}
|
||||
}
|
||||
if (server.aof_state != AOF_OFF) {
|
||||
overhead += sdsAllocSize(server.aof_buf)+aofRewriteBufferSize();
|
||||
overhead += sdsAllocSize(server.aof_buf)+aofRewriteBufferMemoryUsage();
|
||||
}
|
||||
return overhead;
|
||||
}
|
||||
|
||||
@@ -1011,7 +1011,7 @@ struct redisMemOverhead *getMemoryOverheadData(void) {
|
||||
mem = 0;
|
||||
if (server.aof_state != AOF_OFF) {
|
||||
mem += sdsZmallocSize(server.aof_buf);
|
||||
mem += aofRewriteBufferSize();
|
||||
mem += aofRewriteBufferMemoryUsage();
|
||||
}
|
||||
mh->aof_buffer = mem;
|
||||
mem_total+=mem;
|
||||
|
||||
@@ -2077,6 +2077,7 @@ int startAppendOnly(void);
|
||||
void backgroundRewriteDoneHandler(int exitcode, int bysignal);
|
||||
void aofRewriteBufferReset(void);
|
||||
unsigned long aofRewriteBufferSize(void);
|
||||
unsigned long aofRewriteBufferMemoryUsage(void);
|
||||
ssize_t aofReadDiffFromParent(void);
|
||||
void killAppendOnlyChild(void);
|
||||
void restartAOFAfterSYNC();
|
||||
|
||||
Reference in New Issue
Block a user