mirror of
https://codeberg.org/redict/redict.git
synced 2025-01-23 16:48:27 -05:00
bf680b6f8c
A) slave buffers didn't count internal fragmentation and sds unused space, this caused them to induce eviction although we didn't mean for it. B) slave buffers were consuming about twice the memory of what they actually needed. - this was mainly due to sdsMakeRoomFor growing to twice as much as needed each time but networking.c not storing more than 16k (partially fixed recently in 237a38737). - besides it wasn't able to store half of the new string into one buffer and the other half into the next (so the above mentioned fix helped mainly for small items). - lastly, the sds buffers had up to 30% internal fragmentation that was wasted, consumed but not used. C) inefficient performance due to starting from a small string and reallocing many times. what i changed: - creating dedicated buffers for reply list, counting their size with zmalloc_size - when creating a new reply node from, preallocate it to at least 16k. - when appending a new reply to the buffer, first fill all the unused space of the previous node before starting a new one. other changes: - expose mem_not_counted_for_evict info field for the benefit of the test suite - add a test to make sure slave buffers are counted correctly and that they don't cause eviction
107 lines
3.9 KiB
C
107 lines
3.9 KiB
C
/* zmalloc - total amount of allocated memory aware version of malloc()
|
|
*
|
|
* Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions are met:
|
|
*
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
* this list of conditions and the following disclaimer.
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
* * Neither the name of Redis nor the names of its contributors may be used
|
|
* to endorse or promote products derived from this software without
|
|
* specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
|
|
#ifndef __ZMALLOC_H
|
|
#define __ZMALLOC_H
|
|
|
|
/* Double expansion needed for stringification of macro values. */
|
|
#define __xstr(s) __str(s)
|
|
#define __str(s) #s
|
|
|
|
#if defined(USE_TCMALLOC)
|
|
#define ZMALLOC_LIB ("tcmalloc-" __xstr(TC_VERSION_MAJOR) "." __xstr(TC_VERSION_MINOR))
|
|
#include <google/tcmalloc.h>
|
|
#if (TC_VERSION_MAJOR == 1 && TC_VERSION_MINOR >= 6) || (TC_VERSION_MAJOR > 1)
|
|
#define HAVE_MALLOC_SIZE 1
|
|
#define zmalloc_size(p) tc_malloc_size(p)
|
|
#else
|
|
#error "Newer version of tcmalloc required"
|
|
#endif
|
|
|
|
#elif defined(USE_JEMALLOC)
|
|
#define ZMALLOC_LIB ("jemalloc-" __xstr(JEMALLOC_VERSION_MAJOR) "." __xstr(JEMALLOC_VERSION_MINOR) "." __xstr(JEMALLOC_VERSION_BUGFIX))
|
|
#include <jemalloc/jemalloc.h>
|
|
#if (JEMALLOC_VERSION_MAJOR == 2 && JEMALLOC_VERSION_MINOR >= 1) || (JEMALLOC_VERSION_MAJOR > 2)
|
|
#define HAVE_MALLOC_SIZE 1
|
|
#define zmalloc_size(p) je_malloc_usable_size(p)
|
|
#else
|
|
#error "Newer version of jemalloc required"
|
|
#endif
|
|
|
|
#elif defined(__APPLE__)
|
|
#include <malloc/malloc.h>
|
|
#define HAVE_MALLOC_SIZE 1
|
|
#define zmalloc_size(p) malloc_size(p)
|
|
#endif
|
|
|
|
#ifndef ZMALLOC_LIB
|
|
#define ZMALLOC_LIB "libc"
|
|
#ifdef __GLIBC__
|
|
#include <malloc.h>
|
|
#define HAVE_MALLOC_SIZE 1
|
|
#define zmalloc_size(p) malloc_usable_size(p)
|
|
#endif
|
|
#endif
|
|
|
|
/* We can enable the Redis defrag capabilities only if we are using Jemalloc
|
|
* and the version used is our special version modified for Redis having
|
|
* the ability to return per-allocation fragmentation hints. */
|
|
#if defined(USE_JEMALLOC) && defined(JEMALLOC_FRAG_HINT)
|
|
#define HAVE_DEFRAG
|
|
#endif
|
|
|
|
void *zmalloc(size_t size);
|
|
void *zcalloc(size_t size);
|
|
void *zrealloc(void *ptr, size_t size);
|
|
void zfree(void *ptr);
|
|
char *zstrdup(const char *s);
|
|
size_t zmalloc_used_memory(void);
|
|
void zmalloc_set_oom_handler(void (*oom_handler)(size_t));
|
|
size_t zmalloc_get_rss(void);
|
|
int zmalloc_get_allocator_info(size_t *allocated, size_t *active, size_t *resident);
|
|
size_t zmalloc_get_private_dirty(long pid);
|
|
size_t zmalloc_get_smap_bytes_by_field(char *field, long pid);
|
|
size_t zmalloc_get_memory_size(void);
|
|
void zlibc_free(void *ptr);
|
|
|
|
#ifdef HAVE_DEFRAG
|
|
void zfree_no_tcache(void *ptr);
|
|
void *zmalloc_no_tcache(size_t size);
|
|
#endif
|
|
|
|
#ifndef HAVE_MALLOC_SIZE
|
|
size_t zmalloc_size(void *ptr);
|
|
size_t zmalloc_usable(void *ptr);
|
|
#else
|
|
#define zmalloc_usable(p) zmalloc_size(p)
|
|
#endif
|
|
|
|
#endif /* __ZMALLOC_H */
|