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 <stdint.h> 34 35 /* This function provide us access to the original libc free(). This is useful 36 * for instance to free results obtained by backtrace_symbols(). We need 37 * to define this function before including zmalloc.h that may shadow the 38 * free implementation if we use jemalloc or another non standard allocator. */ 39 void zlibc_free(void *ptr) { 40 free(ptr); 41 } 42 43 #include <string.h> 44 #include <pthread.h> 45 #include "config.h" 46 #include "zmalloc.h" 47 #include "atomicvar.h" 48 49 #ifdef HAVE_MALLOC_SIZE 50 #define PREFIX_SIZE (0) 51 #else 52 #if defined(__sun) || defined(__sparc) || defined(__sparc__) 53 #define PREFIX_SIZE (sizeof(long long)) 54 #else 55 #define PREFIX_SIZE (sizeof(size_t)) 56 #endif 57 #endif 58 59 /* Explicitly override malloc/free etc when using tcmalloc. */ 60 #if defined(USE_TCMALLOC) 61 #define malloc(size) tc_malloc(size) 62 #define calloc(count,size) tc_calloc(count,size) 63 #define realloc(ptr,size) tc_realloc(ptr,size) 64 #define free(ptr) tc_free(ptr) 65 #elif defined(USE_JEMALLOC) 66 #define malloc(size) je_malloc(size) 67 #define calloc(count,size) je_calloc(count,size) 68 #define realloc(ptr,size) je_realloc(ptr,size) 69 #define free(ptr) je_free(ptr) 70 #define mallocx(size,flags) je_mallocx(size,flags) 71 #define dallocx(ptr,flags) je_dallocx(ptr,flags) 72 #endif 73 74 #define update_zmalloc_stat_alloc(__n) do { \ 75 size_t _n = (__n); \ 76 if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \ 77 atomicIncr(used_memory,__n); \ 78 } while(0) 79 80 #define update_zmalloc_stat_free(__n) do { \ 81 size_t _n = (__n); \ 82 if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \ 83 atomicDecr(used_memory,__n); \ 84 } while(0) 85 86 static size_t used_memory = 0; 87 pthread_mutex_t used_memory_mutex = PTHREAD_MUTEX_INITIALIZER; 88 89 static void zmalloc_default_oom(size_t size) { 90 fprintf(stderr, "zmalloc: Out of memory trying to allocate %zu bytes\n", 91 size); 92 fflush(stderr); 93 abort(); 94 } 95 96 static void (*zmalloc_oom_handler)(size_t) = zmalloc_default_oom; 97 98 void *zmalloc(size_t size) { 99 void *ptr = malloc(size+PREFIX_SIZE); 100 101 if (!ptr) zmalloc_oom_handler(size); 102 #ifdef HAVE_MALLOC_SIZE 103 update_zmalloc_stat_alloc(zmalloc_size(ptr)); 104 return ptr; 105 #else 106 *((size_t*)ptr) = size; 107 update_zmalloc_stat_alloc(size+PREFIX_SIZE); 108 return (char*)ptr+PREFIX_SIZE; 109 #endif 110 } 111 112 /* Allocation and free functions that bypass the thread cache 113 * and go straight to the allocator arena bins. 114 * Currently implemented only for jemalloc. Used for online defragmentation. */ 115 #ifdef HAVE_DEFRAG 116 void *zmalloc_no_tcache(size_t size) { 117 void *ptr = mallocx(size+PREFIX_SIZE, MALLOCX_TCACHE_NONE); 118 if (!ptr) zmalloc_oom_handler(size); 119 update_zmalloc_stat_alloc(zmalloc_size(ptr)); 120 return ptr; 121 } 122 123 void zfree_no_tcache(void *ptr) { 124 if (ptr == NULL) return; 125 update_zmalloc_stat_free(zmalloc_size(ptr)); 126 dallocx(ptr, MALLOCX_TCACHE_NONE); 127 } 128 #endif 129 130 void *zcalloc(size_t size) { 131 void *ptr = calloc(1, size+PREFIX_SIZE); 132 133 if (!ptr) zmalloc_oom_handler(size); 134 #ifdef HAVE_MALLOC_SIZE 135 update_zmalloc_stat_alloc(zmalloc_size(ptr)); 136 return ptr; 137 #else 138 *((size_t*)ptr) = size; 139 update_zmalloc_stat_alloc(size+PREFIX_SIZE); 140 return (char*)ptr+PREFIX_SIZE; 141 #endif 142 } 143 144 void *zrealloc(void *ptr, size_t size) { 145 #ifndef HAVE_MALLOC_SIZE 146 void *realptr; 147 #endif 148 size_t oldsize; 149 void *newptr; 150 151 if (ptr == NULL) return zmalloc(size); 152 #ifdef HAVE_MALLOC_SIZE 153 oldsize = zmalloc_size(ptr); 154 newptr = realloc(ptr,size); 155 if (!newptr) zmalloc_oom_handler(size); 156 157 update_zmalloc_stat_free(oldsize); 158 update_zmalloc_stat_alloc(zmalloc_size(newptr)); 159 return newptr; 160 #else 161 realptr = (char*)ptr-PREFIX_SIZE; 162 oldsize = *((size_t*)realptr); 163 newptr = realloc(realptr,size+PREFIX_SIZE); 164 if (!newptr) zmalloc_oom_handler(size); 165 166 *((size_t*)newptr) = size; 167 update_zmalloc_stat_free(oldsize+PREFIX_SIZE); 168 update_zmalloc_stat_alloc(size+PREFIX_SIZE); 169 return (char*)newptr+PREFIX_SIZE; 170 #endif 171 } 172 173 /* Provide zmalloc_size() for systems where this function is not provided by 174 * malloc itself, given that in that case we store a header with this 175 * information as the first bytes of every allocation. */ 176 #ifndef HAVE_MALLOC_SIZE 177 size_t zmalloc_size(void *ptr) { 178 void *realptr = (char*)ptr-PREFIX_SIZE; 179 size_t size = *((size_t*)realptr); 180 /* Assume at least that all the allocations are padded at sizeof(long) by 181 * the underlying allocator. */ 182 if (size&(sizeof(long)-1)) size += sizeof(long)-(size&(sizeof(long)-1)); 183 return size+PREFIX_SIZE; 184 } 185 size_t zmalloc_usable(void *ptr) { 186 return zmalloc_size(ptr)-PREFIX_SIZE; 187 } 188 #endif 189 190 void zfree(void *ptr) { 191 #ifndef HAVE_MALLOC_SIZE 192 void *realptr; 193 size_t oldsize; 194 #endif 195 196 if (ptr == NULL) return; 197 #ifdef HAVE_MALLOC_SIZE 198 update_zmalloc_stat_free(zmalloc_size(ptr)); 199 free(ptr); 200 #else 201 realptr = (char*)ptr-PREFIX_SIZE; 202 oldsize = *((size_t*)realptr); 203 update_zmalloc_stat_free(oldsize+PREFIX_SIZE); 204 free(realptr); 205 #endif 206 } 207 208 char *zstrdup(const char *s) { 209 size_t l = strlen(s)+1; 210 char *p = zmalloc(l); 211 212 memcpy(p,s,l); 213 return p; 214 } 215 216 size_t zmalloc_used_memory(void) { 217 size_t um; 218 atomicGet(used_memory,um); 219 return um; 220 } 221 222 void zmalloc_set_oom_handler(void (*oom_handler)(size_t)) { 223 zmalloc_oom_handler = oom_handler; 224 } 225 226 /* Get the RSS information in an OS-specific way. 227 * 228 * WARNING: the function zmalloc_get_rss() is not designed to be fast 229 * and may not be called in the busy loops where Redis tries to release 230 * memory expiring or swapping out objects. 231 * 232 * For this kind of "fast RSS reporting" usages use instead the 233 * function RedisEstimateRSS() that is a much faster (and less precise) 234 * version of the function. */ 235 236 #if defined(HAVE_PROC_STAT) 237 #include <unistd.h> 238 #include <sys/types.h> 239 #include <sys/stat.h> 240 #include <fcntl.h> 241 242 size_t zmalloc_get_rss(void) { 243 int page = sysconf(_SC_PAGESIZE); 244 size_t rss; 245 char buf[4096]; 246 char filename[256]; 247 int fd, count; 248 char *p, *x; 249 250 snprintf(filename,256,"/proc/%d/stat",getpid()); 251 if ((fd = open(filename,O_RDONLY)) == -1) return 0; 252 if (read(fd,buf,4096) <= 0) { 253 close(fd); 254 return 0; 255 } 256 close(fd); 257 258 p = buf; 259 count = 23; /* RSS is the 24th field in /proc/<pid>/stat */ 260 while(p && count--) { 261 p = strchr(p,' '); 262 if (p) p++; 263 } 264 if (!p) return 0; 265 x = strchr(p,' '); 266 if (!x) return 0; 267 *x = '\0'; 268 269 rss = strtoll(p,NULL,10); 270 rss *= page; 271 return rss; 272 } 273 #elif defined(HAVE_TASKINFO) 274 #include <unistd.h> 275 #include <stdio.h> 276 #include <stdlib.h> 277 #include <sys/types.h> 278 #include <sys/sysctl.h> 279 #include <mach/task.h> 280 #include <mach/mach_init.h> 281 282 size_t zmalloc_get_rss(void) { 283 task_t task = MACH_PORT_NULL; 284 struct task_basic_info t_info; 285 mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT; 286 287 if (task_for_pid(current_task(), getpid(), &task) != KERN_SUCCESS) 288 return 0; 289 task_info(task, TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count); 290 291 return t_info.resident_size; 292 } 293 #else 294 size_t zmalloc_get_rss(void) { 295 /* If we can't get the RSS in an OS-specific way for this system just 296 * return the memory usage we estimated in zmalloc().. 297 * 298 * Fragmentation will appear to be always 1 (no fragmentation) 299 * of course... */ 300 return zmalloc_used_memory(); 301 } 302 #endif 303 304 #if defined(USE_JEMALLOC) 305 int zmalloc_get_allocator_info(size_t *allocated, 306 size_t *active, 307 size_t *resident) { 308 uint64_t epoch = 1; 309 size_t sz; 310 *allocated = *resident = *active = 0; 311 /* Update the statistics cached by mallctl. */ 312 sz = sizeof(epoch); 313 je_mallctl("epoch", &epoch, &sz, &epoch, sz); 314 sz = sizeof(size_t); 315 /* Unlike RSS, this does not include RSS from shared libraries and other non 316 * heap mappings. */ 317 je_mallctl("stats.resident", resident, &sz, NULL, 0); 318 /* Unlike resident, this doesn't not include the pages jemalloc reserves 319 * for re-use (purge will clean that). */ 320 je_mallctl("stats.active", active, &sz, NULL, 0); 321 /* Unlike zmalloc_used_memory, this matches the stats.resident by taking 322 * into account all allocations done by this process (not only zmalloc). */ 323 je_mallctl("stats.allocated", allocated, &sz, NULL, 0); 324 return 1; 325 } 326 #else 327 int zmalloc_get_allocator_info(size_t *allocated, 328 size_t *active, 329 size_t *resident) { 330 *allocated = *resident = *active = 0; 331 return 1; 332 } 333 #endif 334 335 /* Get the sum of the specified field (converted form kb to bytes) in 336 * /proc/self/smaps. The field must be specified with trailing ":" as it 337 * apperas in the smaps output. 338 * 339 * If a pid is specified, the information is extracted for such a pid, 340 * otherwise if pid is -1 the information is reported is about the 341 * current process. 342 * 343 * Example: zmalloc_get_smap_bytes_by_field("Rss:",-1); 344 */ 345 #if defined(HAVE_PROC_SMAPS) 346 size_t zmalloc_get_smap_bytes_by_field(char *field, long pid) { 347 char line[1024]; 348 size_t bytes = 0; 349 int flen = strlen(field); 350 FILE *fp; 351 352 if (pid == -1) { 353 fp = fopen("/proc/self/smaps","r"); 354 } else { 355 char filename[128]; 356 snprintf(filename,sizeof(filename),"/proc/%ld/smaps",pid); 357 fp = fopen(filename,"r"); 358 } 359 360 if (!fp) return 0; 361 while(fgets(line,sizeof(line),fp) != NULL) { 362 if (strncmp(line,field,flen) == 0) { 363 char *p = strchr(line,'k'); 364 if (p) { 365 *p = '\0'; 366 bytes += strtol(line+flen,NULL,10) * 1024; 367 } 368 } 369 } 370 fclose(fp); 371 return bytes; 372 } 373 #else 374 size_t zmalloc_get_smap_bytes_by_field(char *field, long pid) { 375 ((void) field); 376 ((void) pid); 377 return 0; 378 } 379 #endif 380 381 size_t zmalloc_get_private_dirty(long pid) { 382 return zmalloc_get_smap_bytes_by_field("Private_Dirty:",pid); 383 } 384 385 /* Returns the size of physical memory (RAM) in bytes. 386 * It looks ugly, but this is the cleanest way to achieve cross platform results. 387 * Cleaned up from: 388 * 389 * http://nadeausoftware.com/articles/2012/09/c_c_tip_how_get_physical_memory_size_system 390 * 391 * Note that this function: 392 * 1) Was released under the following CC attribution license: 393 * http://creativecommons.org/licenses/by/3.0/deed.en_US. 394 * 2) Was originally implemented by David Robert Nadeau. 395 * 3) Was modified for Redis by Matt Stancliff. 396 * 4) This note exists in order to comply with the original license. 397 */ 398 size_t zmalloc_get_memory_size(void) { 399 #if defined(__unix__) || defined(__unix) || defined(unix) || \ 400 (defined(__APPLE__) && defined(__MACH__)) 401 #if defined(CTL_HW) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM64)) 402 int mib[2]; 403 mib[0] = CTL_HW; 404 #if defined(HW_MEMSIZE) 405 mib[1] = HW_MEMSIZE; /* OSX. --------------------- */ 406 #elif defined(HW_PHYSMEM64) 407 mib[1] = HW_PHYSMEM64; /* NetBSD, OpenBSD. --------- */ 408 #endif 409 int64_t size = 0; /* 64-bit */ 410 size_t len = sizeof(size); 411 if (sysctl( mib, 2, &size, &len, NULL, 0) == 0) 412 return (size_t)size; 413 return 0L; /* Failed? */ 414 415 #elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE) 416 /* FreeBSD, Linux, OpenBSD, and Solaris. -------------------- */ 417 return (size_t)sysconf(_SC_PHYS_PAGES) * (size_t)sysconf(_SC_PAGESIZE); 418 419 #elif defined(CTL_HW) && (defined(HW_PHYSMEM) || defined(HW_REALMEM)) 420 /* DragonFly BSD, FreeBSD, NetBSD, OpenBSD, and OSX. -------- */ 421 int mib[2]; 422 mib[0] = CTL_HW; 423 #if defined(HW_REALMEM) 424 mib[1] = HW_REALMEM; /* FreeBSD. ----------------- */ 425 #elif defined(HW_PHYSMEM) 426 mib[1] = HW_PHYSMEM; /* Others. ------------------ */ 427 #endif 428 unsigned int size = 0; /* 32-bit */ 429 size_t len = sizeof(size); 430 if (sysctl(mib, 2, &size, &len, NULL, 0) == 0) 431 return (size_t)size; 432 return 0L; /* Failed? */ 433 #else 434 return 0L; /* Unknown method to get the data. */ 435 #endif 436 #else 437 return 0L; /* Unknown OS. */ 438 #endif 439 } 440 441 #ifdef REDIS_TEST 442 #define UNUSED(x) ((void)(x)) 443 int zmalloc_test(int argc, char **argv) { 444 void *ptr; 445 446 UNUSED(argc); 447 UNUSED(argv); 448 printf("Initial used memory: %zu\n", zmalloc_used_memory()); 449 ptr = zmalloc(123); 450 printf("Allocated 123 bytes; used: %zu\n", zmalloc_used_memory()); 451 ptr = zrealloc(ptr, 456); 452 printf("Reallocated to 456 bytes; used: %zu\n", zmalloc_used_memory()); 453 zfree(ptr); 454 printf("Freed pointer; used: %zu\n", zmalloc_used_memory()); 455 return 0; 456 } 457 #endif 458