]> git.saurik.com Git - redis.git/blob - src/zmalloc.c
81fc4c044a68ba4ef425e4fa24703ff8ed20991b
[redis.git] / src / zmalloc.c
1 /* zmalloc - total amount of allocated memory aware version of malloc()
2 *
3 * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
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.
17 *
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.
29 */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <pthread.h>
35 #include <unistd.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39 #include "config.h"
40
41 #if defined(__sun)
42 #define PREFIX_SIZE sizeof(long long)
43 #else
44 #define PREFIX_SIZE sizeof(size_t)
45 #endif
46
47 #define increment_used_memory(__n) do { \
48 size_t _n = (__n); \
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); \
52 used_memory += _n; \
53 pthread_mutex_unlock(&used_memory_mutex); \
54 } else { \
55 used_memory += _n; \
56 } \
57 } while(0)
58
59 #define decrement_used_memory(__n) do { \
60 size_t _n = (__n); \
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); \
64 used_memory -= _n; \
65 pthread_mutex_unlock(&used_memory_mutex); \
66 } else { \
67 used_memory -= _n; \
68 } \
69 } while(0)
70
71 static size_t used_memory = 0;
72 static int zmalloc_thread_safe = 0;
73 pthread_mutex_t used_memory_mutex = PTHREAD_MUTEX_INITIALIZER;
74
75 static void zmalloc_oom(size_t size) {
76 fprintf(stderr, "zmalloc: Out of memory trying to allocate %zu bytes\n",
77 size);
78 fflush(stderr);
79 abort();
80 }
81
82 void *zmalloc(size_t size) {
83 void *ptr = malloc(size+PREFIX_SIZE);
84
85 if (!ptr) zmalloc_oom(size);
86 #ifdef HAVE_MALLOC_SIZE
87 increment_used_memory(redis_malloc_size(ptr));
88 return ptr;
89 #else
90 *((size_t*)ptr) = size;
91 increment_used_memory(size+PREFIX_SIZE);
92 return (char*)ptr+PREFIX_SIZE;
93 #endif
94 }
95
96 void *zcalloc(size_t size) {
97 void *ptr = calloc(1, size+PREFIX_SIZE);
98
99 if (!ptr) zmalloc_oom(size);
100 #ifdef HAVE_MALLOC_SIZE
101 increment_used_memory(redis_malloc_size(ptr));
102 return ptr;
103 #else
104 *((size_t*)ptr) = size;
105 increment_used_memory(size+PREFIX_SIZE);
106 return (char*)ptr+PREFIX_SIZE;
107 #endif
108 }
109
110 void *zrealloc(void *ptr, size_t size) {
111 #ifndef HAVE_MALLOC_SIZE
112 void *realptr;
113 #endif
114 size_t oldsize;
115 void *newptr;
116
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);
122
123 decrement_used_memory(oldsize);
124 increment_used_memory(redis_malloc_size(newptr));
125 return newptr;
126 #else
127 realptr = (char*)ptr-PREFIX_SIZE;
128 oldsize = *((size_t*)realptr);
129 newptr = realloc(realptr,size+PREFIX_SIZE);
130 if (!newptr) zmalloc_oom(size);
131
132 *((size_t*)newptr) = size;
133 decrement_used_memory(oldsize);
134 increment_used_memory(size);
135 return (char*)newptr+PREFIX_SIZE;
136 #endif
137 }
138
139 void zfree(void *ptr) {
140 #ifndef HAVE_MALLOC_SIZE
141 void *realptr;
142 size_t oldsize;
143 #endif
144
145 if (ptr == NULL) return;
146 #ifdef HAVE_MALLOC_SIZE
147 decrement_used_memory(redis_malloc_size(ptr));
148 free(ptr);
149 #else
150 realptr = (char*)ptr-PREFIX_SIZE;
151 oldsize = *((size_t*)realptr);
152 decrement_used_memory(oldsize+PREFIX_SIZE);
153 free(realptr);
154 #endif
155 }
156
157 char *zstrdup(const char *s) {
158 size_t l = strlen(s)+1;
159 char *p = zmalloc(l);
160
161 memcpy(p,s,l);
162 return p;
163 }
164
165 size_t zmalloc_used_memory(void) {
166 size_t um;
167
168 if (zmalloc_thread_safe) pthread_mutex_lock(&used_memory_mutex);
169 um = used_memory;
170 if (zmalloc_thread_safe) pthread_mutex_unlock(&used_memory_mutex);
171 return um;
172 }
173
174 void zmalloc_enable_thread_safeness(void) {
175 zmalloc_thread_safe = 1;
176 }
177
178 /* Fragmentation = RSS / allocated-bytes */
179 float zmalloc_get_fragmentation_ratio(void) {
180 #ifdef HAVE_PROCFS
181 size_t allocated = zmalloc_used_memory();
182 int page = sysconf(_SC_PAGESIZE);
183 size_t rss;
184 char buf[4096];
185 char filename[256];
186 int fd, count;
187 char *p, *x;
188
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) {
192 close(fd);
193 return 0;
194 }
195 close(fd);
196
197 p = buf;
198 count = 23; /* RSS is the 24th field in /proc/<pid>/stat */
199 while(p && count--) {
200 p = strchr(p,' ');
201 if (p) p++;
202 }
203 if (!p) return 0;
204 x = strchr(p,' ');
205 if (!x) return 0;
206 *x = '\0';
207
208 rss = strtoll(p,NULL,10);
209 rss *= page;
210 return (float)rss/allocated;
211 #else
212 return 0;
213 #endif
214 }