Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: third_party/tcmalloc/chromium/src/memfs_malloc.cc

Issue 9311003: Update the tcmalloc chromium branch to r144 (gperftools 2.0), and merge chromium-specific changes. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Rebasec Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 #endif 47 #endif
48 #include <stdio.h> // for snprintf 48 #include <stdio.h> // for snprintf
49 #include <stdlib.h> // for mkstemp 49 #include <stdlib.h> // for mkstemp
50 #include <string.h> // for strerror 50 #include <string.h> // for strerror
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 <gperftools/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
63 // TODO(sanjay): Move the code below into the tcmalloc namespace
64 using tcmalloc::kLog;
65 using tcmalloc::kCrash;
66 using tcmalloc::Log;
64 using std::string; 67 using std::string;
65 68
66 DEFINE_string(memfs_malloc_path, EnvToString("TCMALLOC_MEMFS_MALLOC_PATH", ""), 69 DEFINE_string(memfs_malloc_path, EnvToString("TCMALLOC_MEMFS_MALLOC_PATH", ""),
67 "Path where hugetlbfs or tmpfs is mounted. The caller is " 70 "Path where hugetlbfs or tmpfs is mounted. The caller is "
68 "responsible for ensuring that the path is unique and does " 71 "responsible for ensuring that the path is unique and does "
69 "not conflict with another process"); 72 "not conflict with another process");
70 DEFINE_int64(memfs_malloc_limit_mb, 73 DEFINE_int64(memfs_malloc_limit_mb,
71 EnvToInt("TCMALLOC_MEMFS_LIMIT_MB", 0), 74 EnvToInt("TCMALLOC_MEMFS_LIMIT_MB", 0),
72 "Limit total allocation size to the " 75 "Limit total allocation size to the "
73 "specified number of MiB. 0 == no limit."); 76 "specified number of MiB. 0 == no limit.");
74 DEFINE_bool(memfs_malloc_abort_on_fail, 77 DEFINE_bool(memfs_malloc_abort_on_fail,
75 EnvToBool("TCMALLOC_MEMFS_ABORT_ON_FAIL", false), 78 EnvToBool("TCMALLOC_MEMFS_ABORT_ON_FAIL", false),
76 "abort() whenever memfs_malloc fails to satisfy an allocation " 79 "abort() whenever memfs_malloc fails to satisfy an allocation "
77 "for any reason."); 80 "for any reason.");
78 DEFINE_bool(memfs_malloc_ignore_mmap_fail, 81 DEFINE_bool(memfs_malloc_ignore_mmap_fail,
79 EnvToBool("TCMALLOC_MEMFS_IGNORE_MMAP_FAIL", false), 82 EnvToBool("TCMALLOC_MEMFS_IGNORE_MMAP_FAIL", false),
80 "Ignore failures from mmap"); 83 "Ignore failures from mmap");
81 DEFINE_bool(memfs_malloc_map_private, 84 DEFINE_bool(memfs_malloc_map_private,
82 EnvToBool("TCMALLOC_MEMFS_MAP_PRIVATE", false), 85 EnvToBool("TCMALLOC_MEMFS_MAP_PRIVATE", false),
83 "Use MAP_PRIVATE with mmap"); 86 "Use MAP_PRIVATE with mmap");
84 87
85 // Hugetlbfs based allocator for tcmalloc 88 // Hugetlbfs based allocator for tcmalloc
86 class HugetlbSysAllocator: public SysAllocator { 89 class HugetlbSysAllocator: public SysAllocator {
87 public: 90 public:
88 explicit HugetlbSysAllocator(SysAllocator* fallback) 91 explicit HugetlbSysAllocator(SysAllocator* fallback)
89 : failed_(true), // Unusable until FlagsInitialized() is called 92 : failed_(true), // To disable allocator until Initialize() is called.
90 big_page_size_(0), 93 big_page_size_(0),
91 hugetlb_fd_(-1), 94 hugetlb_fd_(-1),
92 hugetlb_base_(0), 95 hugetlb_base_(0),
93 fallback_(fallback) { 96 fallback_(fallback) {
94 } 97 }
95 98
96 void* Alloc(size_t size, size_t *actual_size, size_t alignment); 99 void* Alloc(size_t size, size_t *actual_size, size_t alignment);
97 100 bool Initialize();
98 void FlagsInitialized();
99 101
100 bool failed_; // Whether failed to allocate memory. 102 bool failed_; // Whether failed to allocate memory.
103
101 private: 104 private:
102 void* AllocInternal(size_t size, size_t *actual_size, size_t alignment); 105 void* AllocInternal(size_t size, size_t *actual_size, size_t alignment);
103 106
104 int64 big_page_size_; 107 int64 big_page_size_;
105 int hugetlb_fd_; // file descriptor for hugetlb 108 int hugetlb_fd_; // file descriptor for hugetlb
106 off_t hugetlb_base_; 109 off_t hugetlb_base_;
107 110
108 SysAllocator* fallback_; // Default system allocator to fall back to. 111 SysAllocator* fallback_; // Default system allocator to fall back to.
109 }; 112 };
110 static char hugetlb_space[sizeof(HugetlbSysAllocator)]; 113 static char hugetlb_space[sizeof(HugetlbSysAllocator)];
(...skipping 18 matching lines...) Expand all
129 size_t aligned_size = ((size + new_alignment - 1) / 132 size_t aligned_size = ((size + new_alignment - 1) /
130 new_alignment) * new_alignment; 133 new_alignment) * new_alignment;
131 if (aligned_size < size) { 134 if (aligned_size < size) {
132 return fallback_->Alloc(size, actual_size, alignment); 135 return fallback_->Alloc(size, actual_size, alignment);
133 } 136 }
134 137
135 void* result = AllocInternal(aligned_size, actual_size, new_alignment); 138 void* result = AllocInternal(aligned_size, actual_size, new_alignment);
136 if (result != NULL) { 139 if (result != NULL) {
137 return result; 140 return result;
138 } 141 }
139 TCMalloc_MESSAGE(__FILE__, __LINE__, 142 Log(kLog, __FILE__, __LINE__,
140 "HugetlbSysAllocator: failed_=%d allocated=%"PRId64"\n", 143 "HugetlbSysAllocator: (failed, allocated)", failed_, hugetlb_base_);
141 failed_, static_cast<int64_t>(hugetlb_base_));
142 if (FLAGS_memfs_malloc_abort_on_fail) { 144 if (FLAGS_memfs_malloc_abort_on_fail) {
143 CRASH("memfs_malloc_abort_on_fail is set\n"); 145 Log(kCrash, __FILE__, __LINE__,
146 "memfs_malloc_abort_on_fail is set");
144 } 147 }
145 return fallback_->Alloc(size, actual_size, alignment); 148 return fallback_->Alloc(size, actual_size, alignment);
146 } 149 }
147 150
148 void* HugetlbSysAllocator::AllocInternal(size_t size, size_t* actual_size, 151 void* HugetlbSysAllocator::AllocInternal(size_t size, size_t* actual_size,
149 size_t alignment) { 152 size_t alignment) {
150 // Ask for extra memory if alignment > pagesize 153 // Ask for extra memory if alignment > pagesize
151 size_t extra = 0; 154 size_t extra = 0;
152 if (alignment > big_page_size_) { 155 if (alignment > big_page_size_) {
153 extra = alignment - big_page_size_; 156 extra = alignment - big_page_size_;
154 } 157 }
155 158
156 // Test if this allocation would put us over the limit. 159 // Test if this allocation would put us over the limit.
157 off_t limit = FLAGS_memfs_malloc_limit_mb*1024*1024; 160 off_t limit = FLAGS_memfs_malloc_limit_mb*1024*1024;
158 if (limit > 0 && hugetlb_base_ + size + extra > limit) { 161 if (limit > 0 && hugetlb_base_ + size + extra > limit) {
159 // Disable the allocator when there's less than one page left. 162 // Disable the allocator when there's less than one page left.
160 if (limit - hugetlb_base_ < big_page_size_) { 163 if (limit - hugetlb_base_ < big_page_size_) {
161 TCMalloc_MESSAGE(__FILE__, __LINE__, "reached memfs_malloc_limit_mb\n"); 164 Log(kLog, __FILE__, __LINE__, "reached memfs_malloc_limit_mb");
162 failed_ = true; 165 failed_ = true;
163 } 166 }
164 else { 167 else {
165 TCMalloc_MESSAGE(__FILE__, __LINE__, "alloc size=%"PRIuS 168 Log(kLog, __FILE__, __LINE__,
166 " too large while %"PRId64" bytes remain\n", 169 "alloc too large (size, bytes left)", size, limit-hugetlb_base_);
167 size, static_cast<int64_t>(limit - hugetlb_base_));
168 } 170 }
169 return NULL; 171 return NULL;
170 } 172 }
171 173
172 // This is not needed for hugetlbfs, but needed for tmpfs. Annoyingly 174 // This is not needed for hugetlbfs, but needed for tmpfs. Annoyingly
173 // hugetlbfs returns EINVAL for ftruncate. 175 // hugetlbfs returns EINVAL for ftruncate.
174 int ret = ftruncate(hugetlb_fd_, hugetlb_base_ + size + extra); 176 int ret = ftruncate(hugetlb_fd_, hugetlb_base_ + size + extra);
175 if (ret != 0 && errno != EINVAL) { 177 if (ret != 0 && errno != EINVAL) {
176 TCMalloc_MESSAGE(__FILE__, __LINE__, "ftruncate failed: %s\n", 178 Log(kLog, __FILE__, __LINE__,
177 strerror(errno)); 179 "ftruncate failed", strerror(errno));
178 failed_ = true; 180 failed_ = true;
179 return NULL; 181 return NULL;
180 } 182 }
181 183
182 // Note: size + extra does not overflow since: 184 // Note: size + extra does not overflow since:
183 // size + alignment < (1<<NBITS). 185 // size + alignment < (1<<NBITS).
184 // and extra <= alignment 186 // and extra <= alignment
185 // therefore size + extra < (1<<NBITS) 187 // therefore size + extra < (1<<NBITS)
186 void *result; 188 void *result;
187 result = mmap(0, size + extra, PROT_WRITE|PROT_READ, 189 result = mmap(0, size + extra, PROT_WRITE|PROT_READ,
188 FLAGS_memfs_malloc_map_private ? MAP_PRIVATE : MAP_SHARED, 190 FLAGS_memfs_malloc_map_private ? MAP_PRIVATE : MAP_SHARED,
189 hugetlb_fd_, hugetlb_base_); 191 hugetlb_fd_, hugetlb_base_);
190 if (result == reinterpret_cast<void*>(MAP_FAILED)) { 192 if (result == reinterpret_cast<void*>(MAP_FAILED)) {
191 if (!FLAGS_memfs_malloc_ignore_mmap_fail) { 193 if (!FLAGS_memfs_malloc_ignore_mmap_fail) {
192 TCMalloc_MESSAGE(__FILE__, __LINE__, "mmap of size %"PRIuS" failed: %s\n", 194 Log(kLog, __FILE__, __LINE__,
193 size + extra, strerror(errno)); 195 "mmap failed (size, error)", size + extra, strerror(errno));
194 failed_ = true; 196 failed_ = true;
195 } 197 }
196 return NULL; 198 return NULL;
197 } 199 }
198 uintptr_t ptr = reinterpret_cast<uintptr_t>(result); 200 uintptr_t ptr = reinterpret_cast<uintptr_t>(result);
199 201
200 // Adjust the return memory so it is aligned 202 // Adjust the return memory so it is aligned
201 size_t adjust = 0; 203 size_t adjust = 0;
202 if ((ptr & (alignment - 1)) != 0) { 204 if ((ptr & (alignment - 1)) != 0) {
203 adjust = alignment - (ptr & (alignment - 1)); 205 adjust = alignment - (ptr & (alignment - 1));
204 } 206 }
205 ptr += adjust; 207 ptr += adjust;
206 hugetlb_base_ += (size + extra); 208 hugetlb_base_ += (size + extra);
207 209
208 if (actual_size) { 210 if (actual_size) {
209 *actual_size = size + extra - adjust; 211 *actual_size = size + extra - adjust;
210 } 212 }
211 213
212 return reinterpret_cast<void*>(ptr); 214 return reinterpret_cast<void*>(ptr);
213 } 215 }
214 216
215 void HugetlbSysAllocator::FlagsInitialized() { 217 bool HugetlbSysAllocator::Initialize() {
216 if (FLAGS_memfs_malloc_path.length()) { 218 char path[PATH_MAX];
217 char path[PATH_MAX]; 219 const int pathlen = FLAGS_memfs_malloc_path.size();
218 int rc = snprintf(path, sizeof(path), "%s.XXXXXX", 220 if (pathlen + 8 > sizeof(path)) {
219 FLAGS_memfs_malloc_path.c_str()); 221 Log(kCrash, __FILE__, __LINE__, "XX fatal: memfs_malloc_path too long");
220 if (rc < 0 || rc >= sizeof(path)) { 222 return false;
221 CRASH("XX fatal: memfs_malloc_path too long\n"); 223 }
222 } 224 memcpy(path, FLAGS_memfs_malloc_path.data(), pathlen);
225 memcpy(path + pathlen, ".XXXXXX", 8); // Also copies terminating \0
223 226
224 int hugetlb_fd = mkstemp(path); 227 int hugetlb_fd = mkstemp(path);
225 if (hugetlb_fd == -1) { 228 if (hugetlb_fd == -1) {
226 TCMalloc_MESSAGE(__FILE__, __LINE__, 229 Log(kLog, __FILE__, __LINE__,
227 "warning: unable to create memfs_malloc_path %s: %s\n", 230 "warning: unable to create memfs_malloc_path",
228 path, strerror(errno)); 231 path, strerror(errno));
229 return; 232 return false;
230 } 233 }
231 234
232 // Cleanup memory on process exit 235 // Cleanup memory on process exit
233 if (unlink(path) == -1) { 236 if (unlink(path) == -1) {
234 CRASH("fatal: error unlinking memfs_malloc_path %s: %s\n", 237 Log(kCrash, __FILE__, __LINE__,
235 path, strerror(errno)); 238 "fatal: error unlinking memfs_malloc_path", path, strerror(errno));
236 } 239 return false;
240 }
237 241
238 // Use fstatfs to figure out the default page size for memfs 242 // Use fstatfs to figure out the default page size for memfs
239 struct statfs sfs; 243 struct statfs sfs;
240 if (fstatfs(hugetlb_fd, &sfs) == -1) { 244 if (fstatfs(hugetlb_fd, &sfs) == -1) {
241 CRASH("fatal: error fstatfs of memfs_malloc_path: %s\n", 245 Log(kCrash, __FILE__, __LINE__,
242 strerror(errno)); 246 "fatal: error fstatfs of memfs_malloc_path", strerror(errno));
243 } 247 return false;
244 int64 page_size = sfs.f_bsize; 248 }
249 int64 page_size = sfs.f_bsize;
245 250
246 hugetlb_fd_ = hugetlb_fd; 251 hugetlb_fd_ = hugetlb_fd;
247 big_page_size_ = page_size; 252 big_page_size_ = page_size;
248 failed_ = false; 253 failed_ = false;
249 } 254 return true;
250 } 255 }
251 256
252 static void InitSystemAllocator() { 257 REGISTER_MODULE_INITIALIZER(memfs_malloc, {
253 SysAllocator *alloc = MallocExtension::instance()->GetSystemAllocator(); 258 if (FLAGS_memfs_malloc_path.length()) {
254 HugetlbSysAllocator *hugetlb = new (hugetlb_space) HugetlbSysAllocator(alloc); 259 SysAllocator* alloc = MallocExtension::instance()->GetSystemAllocator();
255 MallocExtension::instance()->SetSystemAllocator(hugetlb); 260 HugetlbSysAllocator* hp = new (hugetlb_space) HugetlbSysAllocator(alloc);
256 } 261 if (hp->Initialize()) {
257 262 MallocExtension::instance()->SetSystemAllocator(hp);
258 REGISTER_MODULE_INITIALIZER(memfs_malloc, { InitSystemAllocator(); }); 263 }
264 }
265 });
259 266
260 #endif /* ifdef __linux */ 267 #endif /* ifdef __linux */
OLDNEW
« no previous file with comments | « third_party/tcmalloc/chromium/src/maybe_threads.cc ('k') | third_party/tcmalloc/chromium/src/memory_region_map.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698