1 /* zmalloc - total amount of allocated memory aware version of malloc()
3 * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of Redis nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without
16 * specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/types.h>
42 #define PREFIX_SIZE sizeof(long long)
44 #define PREFIX_SIZE sizeof(size_t)
47 #define increment_used_memory(__n) do { \
49 if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \
50 if (zmalloc_thread_safe) { \
51 pthread_mutex_lock(&used_memory_mutex); \
53 pthread_mutex_unlock(&used_memory_mutex); \
59 #define decrement_used_memory(__n) do { \
61 if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \
62 if (zmalloc_thread_safe) { \
63 pthread_mutex_lock(&used_memory_mutex); \
65 pthread_mutex_unlock(&used_memory_mutex); \
71 static size_t used_memory
= 0;
72 static int zmalloc_thread_safe
= 0;
73 pthread_mutex_t used_memory_mutex
= PTHREAD_MUTEX_INITIALIZER
;
75 static void zmalloc_oom(size_t size
) {
76 fprintf(stderr
, "zmalloc: Out of memory trying to allocate %zu bytes\n",
82 void *zmalloc(size_t size
) {
83 void *ptr
= malloc(size
+PREFIX_SIZE
);
85 if (!ptr
) zmalloc_oom(size
);
86 #ifdef HAVE_MALLOC_SIZE
87 increment_used_memory(redis_malloc_size(ptr
));
90 *((size_t*)ptr
) = size
;
91 increment_used_memory(size
+PREFIX_SIZE
);
92 return (char*)ptr
+PREFIX_SIZE
;
96 void *zcalloc(size_t size
) {
97 void *ptr
= calloc(1, size
+PREFIX_SIZE
);
99 if (!ptr
) zmalloc_oom(size
);
100 #ifdef HAVE_MALLOC_SIZE
101 increment_used_memory(redis_malloc_size(ptr
));
104 *((size_t*)ptr
) = size
;
105 increment_used_memory(size
+PREFIX_SIZE
);
106 return (char*)ptr
+PREFIX_SIZE
;
110 void *zrealloc(void *ptr
, size_t size
) {
111 #ifndef HAVE_MALLOC_SIZE
117 if (ptr
== NULL
) return zmalloc(size
);
118 #ifdef HAVE_MALLOC_SIZE
119 oldsize
= redis_malloc_size(ptr
);
120 newptr
= realloc(ptr
,size
);
121 if (!newptr
) zmalloc_oom(size
);
123 decrement_used_memory(oldsize
);
124 increment_used_memory(redis_malloc_size(newptr
));
127 realptr
= (char*)ptr
-PREFIX_SIZE
;
128 oldsize
= *((size_t*)realptr
);
129 newptr
= realloc(realptr
,size
+PREFIX_SIZE
);
130 if (!newptr
) zmalloc_oom(size
);
132 *((size_t*)newptr
) = size
;
133 decrement_used_memory(oldsize
);
134 increment_used_memory(size
);
135 return (char*)newptr
+PREFIX_SIZE
;
139 void zfree(void *ptr
) {
140 #ifndef HAVE_MALLOC_SIZE
145 if (ptr
== NULL
) return;
146 #ifdef HAVE_MALLOC_SIZE
147 decrement_used_memory(redis_malloc_size(ptr
));
150 realptr
= (char*)ptr
-PREFIX_SIZE
;
151 oldsize
= *((size_t*)realptr
);
152 decrement_used_memory(oldsize
+PREFIX_SIZE
);
157 char *zstrdup(const char *s
) {
158 size_t l
= strlen(s
)+1;
159 char *p
= zmalloc(l
);
165 size_t zmalloc_used_memory(void) {
168 if (zmalloc_thread_safe
) pthread_mutex_lock(&used_memory_mutex
);
170 if (zmalloc_thread_safe
) pthread_mutex_unlock(&used_memory_mutex
);
174 void zmalloc_enable_thread_safeness(void) {
175 zmalloc_thread_safe
= 1;
178 /* Fragmentation = RSS / allocated-bytes */
179 float zmalloc_get_fragmentation_ratio(void) {
181 size_t allocated
= zmalloc_used_memory();
182 int page
= sysconf(_SC_PAGESIZE
);
189 snprintf(filename
,256,"/proc/%d/stat",getpid());
190 if ((fd
= open(filename
,O_RDONLY
)) == -1) return 0;
191 if (read(fd
,buf
,4096) <= 0) {
198 count
= 23; /* RSS is the 24th field in /proc/<pid>/stat */
199 while(p
&& count
--) {
208 rss
= strtoll(p
,NULL
,10);
210 return (float)rss
/allocated
;