OLD | NEW |
1 // Copyright (c) 2007, Google Inc. | 1 // Copyright (c) 2007, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 #include <sys/mman.h> // for mmap, MAP_FAILED, etc | 51 #include <sys/mman.h> // for mmap, MAP_FAILED, etc |
52 #include <sys/statfs.h> // for fstatfs, statfs | 52 #include <sys/statfs.h> // for fstatfs, statfs |
53 #include <unistd.h> // for ftruncate, off_t, unlink | 53 #include <unistd.h> // for ftruncate, off_t, unlink |
54 #include <new> // for operator new | 54 #include <new> // for operator new |
55 #include <string> | 55 #include <string> |
56 | 56 |
57 #include <google/malloc_extension.h> | 57 #include <google/malloc_extension.h> |
58 #include "base/basictypes.h" | 58 #include "base/basictypes.h" |
59 #include "base/googleinit.h" | 59 #include "base/googleinit.h" |
60 #include "base/sysinfo.h" | 60 #include "base/sysinfo.h" |
61 #include "system-alloc.h" | |
62 #include "internal_logging.h" | 61 #include "internal_logging.h" |
63 | 62 |
64 using std::string; | 63 using std::string; |
65 | 64 |
66 DEFINE_string(memfs_malloc_path, EnvToString("TCMALLOC_MEMFS_MALLOC_PATH", ""), | 65 DEFINE_string(memfs_malloc_path, EnvToString("TCMALLOC_MEMFS_MALLOC_PATH", ""), |
67 "Path where hugetlbfs or tmpfs is mounted. The caller is " | 66 "Path where hugetlbfs or tmpfs is mounted. The caller is " |
68 "responsible for ensuring that the path is unique and does " | 67 "responsible for ensuring that the path is unique and does " |
69 "not conflict with another process"); | 68 "not conflict with another process"); |
70 DEFINE_int64(memfs_malloc_limit_mb, | 69 DEFINE_int64(memfs_malloc_limit_mb, |
71 EnvToInt("TCMALLOC_MEMFS_LIMIT_MB", 0), | 70 EnvToInt("TCMALLOC_MEMFS_LIMIT_MB", 0), |
72 "Limit total allocation size to the " | 71 "Limit total allocation size to the " |
73 "specified number of MiB. 0 == no limit."); | 72 "specified number of MiB. 0 == no limit."); |
74 DEFINE_bool(memfs_malloc_abort_on_fail, | 73 DEFINE_bool(memfs_malloc_abort_on_fail, |
75 EnvToBool("TCMALLOC_MEMFS_ABORT_ON_FAIL", false), | 74 EnvToBool("TCMALLOC_MEMFS_ABORT_ON_FAIL", false), |
76 "abort() whenever memfs_malloc fails to satisfy an allocation " | 75 "abort() whenever memfs_malloc fails to satisfy an allocation " |
77 "for any reason."); | 76 "for any reason."); |
78 DEFINE_bool(memfs_malloc_ignore_mmap_fail, | 77 DEFINE_bool(memfs_malloc_ignore_mmap_fail, |
79 EnvToBool("TCMALLOC_MEMFS_IGNORE_MMAP_FAIL", false), | 78 EnvToBool("TCMALLOC_MEMFS_IGNORE_MMAP_FAIL", false), |
80 "Ignore failures from mmap"); | 79 "Ignore failures from mmap"); |
81 DEFINE_bool(memfs_malloc_map_private, | 80 DEFINE_bool(memfs_malloc_map_private, |
82 EnvToBool("TCMALLOC_MEMFS_MAP_PRIVATE", false), | 81 EnvToBool("TCMALLOC_MEMFS_MAP_PRIVATE", false), |
83 "Use MAP_PRIVATE with mmap"); | 82 "Use MAP_PRIVATE with mmap"); |
84 | 83 |
85 // Hugetlbfs based allocator for tcmalloc | 84 // Hugetlbfs based allocator for tcmalloc |
86 class HugetlbSysAllocator: public SysAllocator { | 85 class HugetlbSysAllocator: public SysAllocator { |
87 public: | 86 public: |
88 explicit HugetlbSysAllocator(SysAllocator* fallback) | 87 explicit HugetlbSysAllocator(SysAllocator* fallback) |
89 : failed_(true), // Unusable until FlagsInitialized() is called | 88 : failed_(true), // To disable allocator until Initialize() is called. |
90 big_page_size_(0), | 89 big_page_size_(0), |
91 hugetlb_fd_(-1), | 90 hugetlb_fd_(-1), |
92 hugetlb_base_(0), | 91 hugetlb_base_(0), |
93 fallback_(fallback) { | 92 fallback_(fallback) { |
94 } | 93 } |
95 | 94 |
96 void* Alloc(size_t size, size_t *actual_size, size_t alignment); | 95 void* Alloc(size_t size, size_t *actual_size, size_t alignment); |
97 | 96 bool Initialize(); |
98 void FlagsInitialized(); | |
99 | 97 |
100 bool failed_; // Whether failed to allocate memory. | 98 bool failed_; // Whether failed to allocate memory. |
| 99 |
101 private: | 100 private: |
102 void* AllocInternal(size_t size, size_t *actual_size, size_t alignment); | 101 void* AllocInternal(size_t size, size_t *actual_size, size_t alignment); |
103 | 102 |
104 int64 big_page_size_; | 103 int64 big_page_size_; |
105 int hugetlb_fd_; // file descriptor for hugetlb | 104 int hugetlb_fd_; // file descriptor for hugetlb |
106 off_t hugetlb_base_; | 105 off_t hugetlb_base_; |
107 | 106 |
108 SysAllocator* fallback_; // Default system allocator to fall back to. | 107 SysAllocator* fallback_; // Default system allocator to fall back to. |
109 }; | 108 }; |
110 static char hugetlb_space[sizeof(HugetlbSysAllocator)]; | 109 static char hugetlb_space[sizeof(HugetlbSysAllocator)]; |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 ptr += adjust; | 204 ptr += adjust; |
206 hugetlb_base_ += (size + extra); | 205 hugetlb_base_ += (size + extra); |
207 | 206 |
208 if (actual_size) { | 207 if (actual_size) { |
209 *actual_size = size + extra - adjust; | 208 *actual_size = size + extra - adjust; |
210 } | 209 } |
211 | 210 |
212 return reinterpret_cast<void*>(ptr); | 211 return reinterpret_cast<void*>(ptr); |
213 } | 212 } |
214 | 213 |
215 void HugetlbSysAllocator::FlagsInitialized() { | 214 bool HugetlbSysAllocator::Initialize() { |
216 if (FLAGS_memfs_malloc_path.length()) { | 215 char path[PATH_MAX]; |
217 char path[PATH_MAX]; | 216 int rc = snprintf(path, sizeof(path), "%s.XXXXXX", |
218 int rc = snprintf(path, sizeof(path), "%s.XXXXXX", | 217 FLAGS_memfs_malloc_path.c_str()); |
219 FLAGS_memfs_malloc_path.c_str()); | 218 if (rc < 0 || rc >= sizeof(path)) { |
220 if (rc < 0 || rc >= sizeof(path)) { | 219 CRASH("XX fatal: memfs_malloc_path too long\n"); |
221 CRASH("XX fatal: memfs_malloc_path too long\n"); | 220 return false; |
222 } | 221 } |
223 | 222 |
224 int hugetlb_fd = mkstemp(path); | 223 int hugetlb_fd = mkstemp(path); |
225 if (hugetlb_fd == -1) { | 224 if (hugetlb_fd == -1) { |
226 TCMalloc_MESSAGE(__FILE__, __LINE__, | 225 TCMalloc_MESSAGE(__FILE__, __LINE__, |
227 "warning: unable to create memfs_malloc_path %s: %s\n", | 226 "warning: unable to create memfs_malloc_path %s: %s\n", |
228 path, strerror(errno)); | 227 path, strerror(errno)); |
229 return; | 228 return false; |
230 } | 229 } |
231 | 230 |
232 // Cleanup memory on process exit | 231 // Cleanup memory on process exit |
233 if (unlink(path) == -1) { | 232 if (unlink(path) == -1) { |
234 CRASH("fatal: error unlinking memfs_malloc_path %s: %s\n", | 233 CRASH("fatal: error unlinking memfs_malloc_path %s: %s\n", |
235 path, strerror(errno)); | 234 path, strerror(errno)); |
236 } | 235 return false; |
| 236 } |
237 | 237 |
238 // Use fstatfs to figure out the default page size for memfs | 238 // Use fstatfs to figure out the default page size for memfs |
239 struct statfs sfs; | 239 struct statfs sfs; |
240 if (fstatfs(hugetlb_fd, &sfs) == -1) { | 240 if (fstatfs(hugetlb_fd, &sfs) == -1) { |
241 CRASH("fatal: error fstatfs of memfs_malloc_path: %s\n", | 241 CRASH("fatal: error fstatfs of memfs_malloc_path: %s\n", |
242 strerror(errno)); | 242 strerror(errno)); |
243 } | 243 return false; |
244 int64 page_size = sfs.f_bsize; | 244 } |
| 245 int64 page_size = sfs.f_bsize; |
245 | 246 |
246 hugetlb_fd_ = hugetlb_fd; | 247 hugetlb_fd_ = hugetlb_fd; |
247 big_page_size_ = page_size; | 248 big_page_size_ = page_size; |
248 failed_ = false; | 249 failed_ = false; |
249 } | 250 return true; |
250 } | 251 } |
251 | 252 |
252 static void InitSystemAllocator() { | 253 REGISTER_MODULE_INITIALIZER(memfs_malloc, { |
253 SysAllocator *alloc = MallocExtension::instance()->GetSystemAllocator(); | 254 if (FLAGS_memfs_malloc_path.length()) { |
254 HugetlbSysAllocator *hugetlb = new (hugetlb_space) HugetlbSysAllocator(alloc); | 255 SysAllocator* alloc = MallocExtension::instance()->GetSystemAllocator(); |
255 MallocExtension::instance()->SetSystemAllocator(hugetlb); | 256 HugetlbSysAllocator* hp = new (hugetlb_space) HugetlbSysAllocator(alloc); |
256 } | 257 if (hp->Initialize()) { |
257 | 258 MallocExtension::instance()->SetSystemAllocator(hp); |
258 REGISTER_MODULE_INITIALIZER(memfs_malloc, { InitSystemAllocator(); }); | 259 } |
| 260 } |
| 261 }); |
259 | 262 |
260 #endif /* ifdef __linux */ | 263 #endif /* ifdef __linux */ |
OLD | NEW |