~ubuntu-branches/ubuntu/jaunty/google-perftools/jaunty

« back to all changes in this revision

Viewing changes to src/memfs_malloc.cc

  • Committer: Bazaar Package Importer
  • Author(s): Daigo Moriwaki
  • Date: 2008-06-15 23:41:36 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20080615234136-al5gawvdvt5vhdtz
Tags: 0.98-1
* New upstream release. (Closes: #425147)
* Compiled with GCC 4.3. (Closes: #454841)
* debian/watch: can now report upstream's version (Closes: #450294)
* Because of a file conflict between tau and libgoogle-perftools the
  binary pprof is renamed as google-pprof. (Closes: #404001)
  Great thanks to Michael Mende.
* debian/rules: autoconf files are now generated at the build time.
* Bumped up Standards-Version to 3.7.3, no changes are required.
* Split a new package, libtcmallc_minimal0. The upstream supports
  this module for wider platforms. So I leave its architecture to be
  `any'.
* libgoogle-perftools0's architecture is now i386. The upstream
  supports this module for x86 and x86_64. However, x86_64 requires
  libunwind's development head, which Debian does not have yet.
* Removed an unnecessary patch, debian/patches/02_profiler.cc_alpha.diff.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2007, Google Inc.
 
2
// All rights reserved.
 
3
// 
 
4
// Redistribution and use in source and binary forms, with or without
 
5
// modification, are permitted provided that the following conditions are
 
6
// met:
 
7
// 
 
8
//     * Redistributions of source code must retain the above copyright
 
9
// notice, this list of conditions and the following disclaimer.
 
10
//     * Redistributions in binary form must reproduce the above
 
11
// copyright notice, this list of conditions and the following disclaimer
 
12
// in the documentation and/or other materials provided with the
 
13
// distribution.
 
14
//     * Neither the name of Google Inc. nor the names of its
 
15
// contributors may be used to endorse or promote products derived from
 
16
// this software without specific prior written permission.
 
17
// 
 
18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
19
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
20
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
21
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
22
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
23
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
24
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
25
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
26
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
27
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
28
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 
 
30
// ---
 
31
// Author: Arun Sharma
 
32
//
 
33
// A tcmalloc system allocator that uses a memory based filesystem such as
 
34
// tmpfs or hugetlbfs
 
35
//
 
36
// Since these only exist on linux, we only register this allocator there.
 
37
 
 
38
#ifdef __linux
 
39
 
 
40
#include "config.h"
 
41
#include <errno.h>
 
42
#include <fcntl.h>
 
43
#include <unistd.h>
 
44
#include <inttypes.h>
 
45
#include <sys/mman.h>
 
46
#include <sys/param.h>
 
47
#include <sys/types.h>
 
48
#include <sys/vfs.h>           // for statfs
 
49
#include <string>
 
50
 
 
51
#include "base/basictypes.h"
 
52
#include "base/googleinit.h"
 
53
#include "base/logging.h"
 
54
#include "base/sysinfo.h"
 
55
#include "system-alloc.h"
 
56
 
 
57
using std::string;
 
58
 
 
59
DEFINE_string(memfs_malloc_path, EnvToString("TCMALLOC_MEMFS_MALLOC_PATH", ""),
 
60
              "Path where hugetlbfs or tmpfs is mounted. The caller is "
 
61
              "responsible for ensuring that the path is unique and does "
 
62
              "not conflict with another process");
 
63
DEFINE_int64(memfs_malloc_limit_mb, 0, "Limit total allocation size to the "
 
64
             "specified number of MiB.  0 == no limit.");
 
65
 
 
66
// Hugetlbfs based allocator for tcmalloc
 
67
class HugetlbSysAllocator: public SysAllocator {
 
68
public:
 
69
  HugetlbSysAllocator(int fd, int page_size)
 
70
    : big_page_size_(page_size),
 
71
      hugetlb_fd_(fd),
 
72
      hugetlb_base_(0) {
 
73
  }
 
74
 
 
75
  void* Alloc(size_t size, size_t *actual_size, size_t alignment);
 
76
 
 
77
  void DumpStats(TCMalloc_Printer* printer);
 
78
 
 
79
private:
 
80
  int64 big_page_size_;
 
81
  int hugetlb_fd_;      // file descriptor for hugetlb
 
82
  off_t hugetlb_base_;
 
83
};
 
84
 
 
85
void HugetlbSysAllocator::DumpStats(TCMalloc_Printer* printer) {
 
86
  printer->printf("HugetlbSysAllocator: failed_=%d allocated=%"PRId64"\n",
 
87
                  failed_, static_cast<int64_t>(hugetlb_base_));
 
88
}
 
89
 
 
90
// No locking needed here since we assume that tcmalloc calls
 
91
// us with an internal lock held (see tcmalloc/system-alloc.cc).
 
92
void* HugetlbSysAllocator::Alloc(size_t size, size_t *actual_size,
 
93
                                 size_t alignment) {
 
94
 
 
95
  // We don't respond to allocation requests smaller than big_page_size_ unless
 
96
  // the caller is willing to take more than they asked for.
 
97
  if (actual_size == NULL && size < big_page_size_) {
 
98
    return NULL;
 
99
  }
 
100
 
 
101
  // Enforce huge page alignment.  Be careful to deal with overflow.
 
102
  if (alignment < big_page_size_) alignment = big_page_size_;
 
103
  size_t aligned_size = ((size + alignment - 1) / alignment) * alignment;
 
104
  if (aligned_size < size) {
 
105
    return NULL;
 
106
  }
 
107
  size = aligned_size;
 
108
 
 
109
  // Ask for extra memory if alignment > pagesize
 
110
  size_t extra = 0;
 
111
  if (alignment > big_page_size_) {
 
112
    extra = alignment - big_page_size_;
 
113
  }
 
114
 
 
115
  // Test if this allocation would put us over the limit.
 
116
  off_t limit = FLAGS_memfs_malloc_limit_mb*1024*1024;
 
117
  if (limit > 0 && hugetlb_base_ + size + extra > limit) {
 
118
    // Disable the allocator when there's less than one page left.
 
119
    if (limit - hugetlb_base_ < big_page_size_) {
 
120
      failed_ = true;
 
121
    }
 
122
    return NULL;
 
123
  }
 
124
 
 
125
  // This is not needed for hugetlbfs, but needed for tmpfs.  Annoyingly
 
126
  // hugetlbfs returns EINVAL for ftruncate.
 
127
  int ret = ftruncate(hugetlb_fd_, hugetlb_base_ + size + extra);
 
128
  if (ret != 0 && errno != EINVAL) {
 
129
    failed_ = true;
 
130
    return NULL;
 
131
  }
 
132
 
 
133
  // Note: size + extra does not overflow since:
 
134
  //            size + alignment < (1<<NBITS).
 
135
  // and        extra <= alignment
 
136
  // therefore  size + extra < (1<<NBITS)
 
137
  void *result = mmap(0, size + extra, PROT_WRITE|PROT_READ,
 
138
                      MAP_SHARED, hugetlb_fd_, hugetlb_base_);
 
139
  if (result == reinterpret_cast<void*>(MAP_FAILED)) {
 
140
    failed_ = true;
 
141
    return NULL;
 
142
  }
 
143
  uintptr_t ptr = reinterpret_cast<uintptr_t>(result);
 
144
 
 
145
  // Adjust the return memory so it is aligned
 
146
  size_t adjust = 0;
 
147
  if ((ptr & (alignment - 1)) != 0) {
 
148
    adjust = alignment - (ptr & (alignment - 1));
 
149
  }
 
150
  ptr += adjust;
 
151
  hugetlb_base_ += (size + extra);
 
152
 
 
153
  if (actual_size) {
 
154
    *actual_size = size + extra - adjust;
 
155
  }
 
156
 
 
157
  return reinterpret_cast<void*>(ptr);
 
158
}
 
159
 
 
160
static void InitSystemAllocator() {
 
161
  if (FLAGS_memfs_malloc_path.length()) {
 
162
    // Don't rely on the caller to ensure unique path name
 
163
    char pid[64];              // pids are smaller than this!
 
164
    int n = snprintf(pid, sizeof(pid), "%u", (unsigned int)(getpid()));
 
165
    CHECK(n < sizeof(pid));
 
166
    string hugetlbfs_path = FLAGS_memfs_malloc_path
 
167
      + "." + pid;
 
168
 
 
169
    int hugetlb_fd = open(hugetlbfs_path.c_str(), O_RDWR | O_CREAT | O_EXCL,
 
170
                          0600);
 
171
    if (hugetlb_fd == -1) {
 
172
      RAW_LOG(WARNING, "unable to create memfs_malloc_path file %s: %s",
 
173
              hugetlbfs_path.c_str(), strerror(errno));
 
174
      return;
 
175
    }
 
176
 
 
177
    // Cleanup memory on process exit
 
178
    CHECK_ERR(unlink(hugetlbfs_path.c_str()));
 
179
 
 
180
    // Use fstatfs to figure out the default page size for memfs
 
181
    struct statfs sfs;
 
182
    CHECK_ERR(fstatfs(hugetlb_fd, &sfs));
 
183
    int64 page_size = sfs.f_bsize;
 
184
 
 
185
    SysAllocator *alloc = new HugetlbSysAllocator(hugetlb_fd, page_size);
 
186
    // Register ourselves with tcmalloc
 
187
    RegisterSystemAllocator(alloc, 0);
 
188
  }
 
189
}
 
190
 
 
191
REGISTER_MODULE_INITIALIZER(memfs_malloc, { InitSystemAllocator(); });
 
192
 
 
193
#endif   /* ifdef __linux */