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

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

Issue 9701040: Revert 126715 - Update the tcmalloc vendor branch to r144 (gperftools 2.0). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: 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 <gperftools/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 "internal_logging.h" 61 #include "internal_logging.h"
62 62
63 // TODO(sanjay): Move the code below into the tcmalloc namespace
64 using tcmalloc::kLog;
65 using tcmalloc::kCrash;
66 using tcmalloc::Log;
67 using std::string; 63 using std::string;
68 64
69 DEFINE_string(memfs_malloc_path, EnvToString("TCMALLOC_MEMFS_MALLOC_PATH", ""), 65 DEFINE_string(memfs_malloc_path, EnvToString("TCMALLOC_MEMFS_MALLOC_PATH", ""),
70 "Path where hugetlbfs or tmpfs is mounted. The caller is " 66 "Path where hugetlbfs or tmpfs is mounted. The caller is "
71 "responsible for ensuring that the path is unique and does " 67 "responsible for ensuring that the path is unique and does "
72 "not conflict with another process"); 68 "not conflict with another process");
73 DEFINE_int64(memfs_malloc_limit_mb, 69 DEFINE_int64(memfs_malloc_limit_mb,
74 EnvToInt("TCMALLOC_MEMFS_LIMIT_MB", 0), 70 EnvToInt("TCMALLOC_MEMFS_LIMIT_MB", 0),
75 "Limit total allocation size to the " 71 "Limit total allocation size to the "
76 "specified number of MiB. 0 == no limit."); 72 "specified number of MiB. 0 == no limit.");
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 size_t aligned_size = ((size + new_alignment - 1) / 128 size_t aligned_size = ((size + new_alignment - 1) /
133 new_alignment) * new_alignment; 129 new_alignment) * new_alignment;
134 if (aligned_size < size) { 130 if (aligned_size < size) {
135 return fallback_->Alloc(size, actual_size, alignment); 131 return fallback_->Alloc(size, actual_size, alignment);
136 } 132 }
137 133
138 void* result = AllocInternal(aligned_size, actual_size, new_alignment); 134 void* result = AllocInternal(aligned_size, actual_size, new_alignment);
139 if (result != NULL) { 135 if (result != NULL) {
140 return result; 136 return result;
141 } 137 }
142 Log(kLog, __FILE__, __LINE__, 138 TCMalloc_MESSAGE(__FILE__, __LINE__,
143 "HugetlbSysAllocator: (failed, allocated)", failed_, hugetlb_base_); 139 "HugetlbSysAllocator: failed_=%d allocated=%"PRId64"\n",
140 failed_, static_cast<int64_t>(hugetlb_base_));
144 if (FLAGS_memfs_malloc_abort_on_fail) { 141 if (FLAGS_memfs_malloc_abort_on_fail) {
145 Log(kCrash, __FILE__, __LINE__, 142 CRASH("memfs_malloc_abort_on_fail is set\n");
146 "memfs_malloc_abort_on_fail is set");
147 } 143 }
148 return fallback_->Alloc(size, actual_size, alignment); 144 return fallback_->Alloc(size, actual_size, alignment);
149 } 145 }
150 146
151 void* HugetlbSysAllocator::AllocInternal(size_t size, size_t* actual_size, 147 void* HugetlbSysAllocator::AllocInternal(size_t size, size_t* actual_size,
152 size_t alignment) { 148 size_t alignment) {
153 // Ask for extra memory if alignment > pagesize 149 // Ask for extra memory if alignment > pagesize
154 size_t extra = 0; 150 size_t extra = 0;
155 if (alignment > big_page_size_) { 151 if (alignment > big_page_size_) {
156 extra = alignment - big_page_size_; 152 extra = alignment - big_page_size_;
157 } 153 }
158 154
159 // Test if this allocation would put us over the limit. 155 // Test if this allocation would put us over the limit.
160 off_t limit = FLAGS_memfs_malloc_limit_mb*1024*1024; 156 off_t limit = FLAGS_memfs_malloc_limit_mb*1024*1024;
161 if (limit > 0 && hugetlb_base_ + size + extra > limit) { 157 if (limit > 0 && hugetlb_base_ + size + extra > limit) {
162 // Disable the allocator when there's less than one page left. 158 // Disable the allocator when there's less than one page left.
163 if (limit - hugetlb_base_ < big_page_size_) { 159 if (limit - hugetlb_base_ < big_page_size_) {
164 Log(kLog, __FILE__, __LINE__, "reached memfs_malloc_limit_mb"); 160 TCMalloc_MESSAGE(__FILE__, __LINE__, "reached memfs_malloc_limit_mb\n");
165 failed_ = true; 161 failed_ = true;
166 } 162 }
167 else { 163 else {
168 Log(kLog, __FILE__, __LINE__, 164 TCMalloc_MESSAGE(__FILE__, __LINE__, "alloc size=%"PRIuS
169 "alloc too large (size, bytes left)", size, limit-hugetlb_base_); 165 " too large while %"PRId64" bytes remain\n",
166 size, static_cast<int64_t>(limit - hugetlb_base_));
170 } 167 }
171 return NULL; 168 return NULL;
172 } 169 }
173 170
174 // This is not needed for hugetlbfs, but needed for tmpfs. Annoyingly 171 // This is not needed for hugetlbfs, but needed for tmpfs. Annoyingly
175 // hugetlbfs returns EINVAL for ftruncate. 172 // hugetlbfs returns EINVAL for ftruncate.
176 int ret = ftruncate(hugetlb_fd_, hugetlb_base_ + size + extra); 173 int ret = ftruncate(hugetlb_fd_, hugetlb_base_ + size + extra);
177 if (ret != 0 && errno != EINVAL) { 174 if (ret != 0 && errno != EINVAL) {
178 Log(kLog, __FILE__, __LINE__, 175 TCMalloc_MESSAGE(__FILE__, __LINE__, "ftruncate failed: %s\n",
179 "ftruncate failed", strerror(errno)); 176 strerror(errno));
180 failed_ = true; 177 failed_ = true;
181 return NULL; 178 return NULL;
182 } 179 }
183 180
184 // Note: size + extra does not overflow since: 181 // Note: size + extra does not overflow since:
185 // size + alignment < (1<<NBITS). 182 // size + alignment < (1<<NBITS).
186 // and extra <= alignment 183 // and extra <= alignment
187 // therefore size + extra < (1<<NBITS) 184 // therefore size + extra < (1<<NBITS)
188 void *result; 185 void *result;
189 result = mmap(0, size + extra, PROT_WRITE|PROT_READ, 186 result = mmap(0, size + extra, PROT_WRITE|PROT_READ,
190 FLAGS_memfs_malloc_map_private ? MAP_PRIVATE : MAP_SHARED, 187 FLAGS_memfs_malloc_map_private ? MAP_PRIVATE : MAP_SHARED,
191 hugetlb_fd_, hugetlb_base_); 188 hugetlb_fd_, hugetlb_base_);
192 if (result == reinterpret_cast<void*>(MAP_FAILED)) { 189 if (result == reinterpret_cast<void*>(MAP_FAILED)) {
193 if (!FLAGS_memfs_malloc_ignore_mmap_fail) { 190 if (!FLAGS_memfs_malloc_ignore_mmap_fail) {
194 Log(kLog, __FILE__, __LINE__, 191 TCMalloc_MESSAGE(__FILE__, __LINE__, "mmap of size %"PRIuS" failed: %s\n",
195 "mmap failed (size, error)", size + extra, strerror(errno)); 192 size + extra, strerror(errno));
196 failed_ = true; 193 failed_ = true;
197 } 194 }
198 return NULL; 195 return NULL;
199 } 196 }
200 uintptr_t ptr = reinterpret_cast<uintptr_t>(result); 197 uintptr_t ptr = reinterpret_cast<uintptr_t>(result);
201 198
202 // Adjust the return memory so it is aligned 199 // Adjust the return memory so it is aligned
203 size_t adjust = 0; 200 size_t adjust = 0;
204 if ((ptr & (alignment - 1)) != 0) { 201 if ((ptr & (alignment - 1)) != 0) {
205 adjust = alignment - (ptr & (alignment - 1)); 202 adjust = alignment - (ptr & (alignment - 1));
206 } 203 }
207 ptr += adjust; 204 ptr += adjust;
208 hugetlb_base_ += (size + extra); 205 hugetlb_base_ += (size + extra);
209 206
210 if (actual_size) { 207 if (actual_size) {
211 *actual_size = size + extra - adjust; 208 *actual_size = size + extra - adjust;
212 } 209 }
213 210
214 return reinterpret_cast<void*>(ptr); 211 return reinterpret_cast<void*>(ptr);
215 } 212 }
216 213
217 bool HugetlbSysAllocator::Initialize() { 214 bool HugetlbSysAllocator::Initialize() {
218 char path[PATH_MAX]; 215 char path[PATH_MAX];
219 const int pathlen = FLAGS_memfs_malloc_path.size(); 216 int rc = snprintf(path, sizeof(path), "%s.XXXXXX",
220 if (pathlen + 8 > sizeof(path)) { 217 FLAGS_memfs_malloc_path.c_str());
221 Log(kCrash, __FILE__, __LINE__, "XX fatal: memfs_malloc_path too long"); 218 if (rc < 0 || rc >= sizeof(path)) {
219 CRASH("XX fatal: memfs_malloc_path too long\n");
222 return false; 220 return false;
223 } 221 }
224 memcpy(path, FLAGS_memfs_malloc_path.data(), pathlen);
225 memcpy(path + pathlen, ".XXXXXX", 8); // Also copies terminating \0
226 222
227 int hugetlb_fd = mkstemp(path); 223 int hugetlb_fd = mkstemp(path);
228 if (hugetlb_fd == -1) { 224 if (hugetlb_fd == -1) {
229 Log(kLog, __FILE__, __LINE__, 225 TCMalloc_MESSAGE(__FILE__, __LINE__,
230 "warning: unable to create memfs_malloc_path", 226 "warning: unable to create memfs_malloc_path %s: %s\n",
231 path, strerror(errno)); 227 path, strerror(errno));
232 return false; 228 return false;
233 } 229 }
234 230
235 // Cleanup memory on process exit 231 // Cleanup memory on process exit
236 if (unlink(path) == -1) { 232 if (unlink(path) == -1) {
237 Log(kCrash, __FILE__, __LINE__, 233 CRASH("fatal: error unlinking memfs_malloc_path %s: %s\n",
238 "fatal: error unlinking memfs_malloc_path", path, strerror(errno)); 234 path, strerror(errno));
239 return false; 235 return false;
240 } 236 }
241 237
242 // Use fstatfs to figure out the default page size for memfs 238 // Use fstatfs to figure out the default page size for memfs
243 struct statfs sfs; 239 struct statfs sfs;
244 if (fstatfs(hugetlb_fd, &sfs) == -1) { 240 if (fstatfs(hugetlb_fd, &sfs) == -1) {
245 Log(kCrash, __FILE__, __LINE__, 241 CRASH("fatal: error fstatfs of memfs_malloc_path: %s\n",
246 "fatal: error fstatfs of memfs_malloc_path", strerror(errno)); 242 strerror(errno));
247 return false; 243 return false;
248 } 244 }
249 int64 page_size = sfs.f_bsize; 245 int64 page_size = sfs.f_bsize;
250 246
251 hugetlb_fd_ = hugetlb_fd; 247 hugetlb_fd_ = hugetlb_fd;
252 big_page_size_ = page_size; 248 big_page_size_ = page_size;
253 failed_ = false; 249 failed_ = false;
254 return true; 250 return true;
255 } 251 }
256 252
257 REGISTER_MODULE_INITIALIZER(memfs_malloc, { 253 REGISTER_MODULE_INITIALIZER(memfs_malloc, {
258 if (FLAGS_memfs_malloc_path.length()) { 254 if (FLAGS_memfs_malloc_path.length()) {
259 SysAllocator* alloc = MallocExtension::instance()->GetSystemAllocator(); 255 SysAllocator* alloc = MallocExtension::instance()->GetSystemAllocator();
260 HugetlbSysAllocator* hp = new (hugetlb_space) HugetlbSysAllocator(alloc); 256 HugetlbSysAllocator* hp = new (hugetlb_space) HugetlbSysAllocator(alloc);
261 if (hp->Initialize()) { 257 if (hp->Initialize()) {
262 MallocExtension::instance()->SetSystemAllocator(hp); 258 MallocExtension::instance()->SetSystemAllocator(hp);
263 } 259 }
264 } 260 }
265 }); 261 });
266 262
267 #endif /* ifdef __linux */ 263 #endif /* ifdef __linux */
OLDNEW
« no previous file with comments | « third_party/tcmalloc/vendor/src/malloc_hook_mmap_linux.h ('k') | third_party/tcmalloc/vendor/src/memory_region_map.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698