<div style="font-family: arial, helvetica, sans-serif"><font size="2"><div class="gmail_quote">On Mon, Jun 25, 2012 at 5:40 PM, Dmitry Vyukov <span dir="ltr"><<a href="mailto:dvyukov@google.com" target="_blank">dvyukov@google.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="font-family:arial,helvetica,sans-serif"><font size="2"><div><div class="h5"><div class="gmail_quote">On Fri, Jun 22, 2012 at 7:11 PM, Dmitry Vyukov <span dir="ltr"><<a href="mailto:dvyukov@google.com" target="_blank">dvyukov@google.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="font-family:arial,helvetica,sans-serif"><font size="2"><div class="gmail_quote"><div>On Fri, Jun 22, 2012 at 5:00 PM, Kostya Serebryany <span dir="ltr"><<a href="mailto:kcc@google.com" target="_blank">kcc@google.com</a>></span> wrote:<br>
</div><div><div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: kcc<br>
Date: Fri Jun 22 08:00:50 2012<br>
New Revision: 158991<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=158991&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=158991&view=rev</a><br>
Log:<br>
[tsan] more code for a specialized tsan allocator<br>
<br>
Modified:<br>
compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator64.h<br>
compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h<br>
compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator64_test.cc<br>
<br>
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator64.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator64.h?rev=158991&r1=158990&r2=158991&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator64.h?rev=158991&r1=158990&r2=158991&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator64.h (original)<br>
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_allocator64.h Fri Jun 22 08:00:50 2012<br>
@@ -22,7 +22,7 @@<br>
<br>
namespace __sanitizer {<br>
<br>
-// Maps size class to size and back.<br>
+// Maps size class id to size and back.<br>
class DefaultSizeClassMap {<br>
private:<br>
// Here we use a spline composed of 5 polynomials of oder 1.<br>
@@ -53,19 +53,20 @@<br>
public:<br>
static const uptr kNumClasses = u4 + 1;<br>
static const uptr kMaxSize = l5;<br>
+ static const uptr kMinSize = l0;<br>
<br>
COMPILER_CHECK(kNumClasses <= 256);<br>
COMPILER_CHECK((kMaxSize & (kMaxSize - 1)) == 0);<br>
<br>
- static uptr Size(uptr size_class) {<br>
- if (size_class <= u0) return l0 + s0 * (size_class - 0);<br>
- if (size_class <= u1) return l1 + s1 * (size_class - u0);<br>
- if (size_class <= u2) return l2 + s2 * (size_class - u1);<br>
- if (size_class <= u3) return l3 + s3 * (size_class - u2);<br>
- if (size_class <= u4) return l4 + s4 * (size_class - u3);<br>
+ static uptr Size(uptr class_id) {<br>
+ if (class_id <= u0) return l0 + s0 * (class_id - 0);<br>
+ if (class_id <= u1) return l1 + s1 * (class_id - u0);<br>
+ if (class_id <= u2) return l2 + s2 * (class_id - u1);<br>
+ if (class_id <= u3) return l3 + s3 * (class_id - u2);<br>
+ if (class_id <= u4) return l4 + s4 * (class_id - u3);<br>
return 0;<br>
}<br>
- static uptr Class(uptr size) {<br>
+ static uptr ClassID(uptr size) {<br>
if (size <= l1) return 0 + (size - l0 + s0 - 1) / s0;<br>
if (size <= l2) return u0 + (size - l1 + s1 - 1) / s1;<br>
if (size <= l3) return u1 + (size - l2 + s2 - 1) / s2;<br>
@@ -75,6 +76,129 @@<br>
}<br>
};<br>
<br>
+// Space: a portion of address space of kSpaceSize bytes starting at<br>
+// a fixed address (kSpaceBeg). Both constants are powers of two and<br>
+// kSpaceBeg is kSpaceSize-aligned.<br>
+//<br>
+// Region: a part of Space dedicated to a single size class.<br>
+// There are kNumClasses Regions of equal size.<br>
+//<br>
+// UserChunk: a piece of memory returned to user.<br>
+// MetaChunk: kMetadataSize bytes of metadata associated with a UserChunk.<br>
+//<br>
+// A Region looks like this:<br>
+// UserChunk1 ... UserChunkN <gap> MetaChunkN ... MetaChunk1<br>
+template <const uptr kSpaceBeg, const uptr kSpaceSize,<br>
+ const uptr kMetadataSize, class SizeClassMap><br>
+class SizeClassAllocator64 {<br>
+ public:<br>
+ void Init() {<br>
+ CHECK_EQ(AllocBeg(), reinterpret_cast<uptr>(MmapFixedNoReserve(<br>
+ AllocBeg(), AllocSize())));<br>
+ }<br>
+ void *Allocate(uptr size) {<br>
+ CHECK_LE(size, SizeClassMap::kMaxSize);<br>
+ return AllocateBySizeClass(SizeClassMap::ClassID(size));<br>
+ }<br>
+ void Deallocate(void *p) {<br></blockquote><div><br></div></div></div><div>It needs to ensure that PointerIsMine(p).</div><div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ DeallocateBySizeClass(p, GetSizeClass(p));<br>
+ }<br>
+ bool PointerIsMine(void *p) {<br>
+ return reinterpret_cast<uptr>(p) / kSpaceSize == kSpaceBeg / kSpaceSize;<br>
+ }<br>
+ uptr GetSizeClass(void *p) {<br>
+ return (reinterpret_cast<uptr>(p) / kRegionSize) % kNumClasses;<br>
+ }<br>
+<br>
+ uptr TotalMemoryUsedIncludingFreeLists() {<br></blockquote><div><br></div></div><div>Can we express the public name in public terms? It makes no sense for me as a user. FreeLists are no more than impl detail, so one day we may not have them at all.</div>
<div><div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ uptr res = 0;<br>
+ for (uptr i = 0; i < kNumClasses; i++)<br>
+ res += GetRegionInfo(i)->allocated;<br>
+ return res;<br>
+ }<br>
+<br>
+ // Test-only.<br>
+ void TestOnlyUnmap() {<br>
+ UnmapOrDie(reinterpret_cast<void*>(AllocBeg()), AllocSize());<br>
+ }<br>
+ private:<br>
+ static const uptr kNumClasses = 256; // Power of two <= 256<br>
+ COMPILER_CHECK(kNumClasses <= SizeClassMap::kNumClasses);<br>
+ static const uptr kRegionSize = kSpaceSize / kNumClasses;<br>
+ // Populate the free list with at most this number of bytes at once<br>
+ // or with one element if its size is greater.<br>
+ static const uptr kPopulateSize = 1 << 18;<br>
+<br>
+ struct LifoListNode {<br>
+ LifoListNode *next;<br>
+ };<br>
+<br>
+ struct RegionInfo {<br>
+ uptr mutex; // FIXME<br>
+ LifoListNode *free_list;<br>
+ uptr allocated;<br>
+ char padding[kCacheLineSize -<br>
+ sizeof(mutex) - sizeof(free_list) - sizeof(allocated)];<br>
+ };<br>
+ COMPILER_CHECK(sizeof(RegionInfo) == kCacheLineSize);<br>
+<br>
+ uptr AdditionalSize() { return sizeof(RegionInfo) * kNumClasses; }<br>
+ uptr AllocBeg() { return kSpaceBeg - AdditionalSize(); }<br></blockquote><div><br></div></div></div><div>Assert that AdditionalSize() % kPageSize == 0. Otherwise mmap check will fail.</div><div><div> </div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ uptr AllocSize() { return kSpaceSize + AdditionalSize(); }<br>
+<br>
+ RegionInfo *GetRegionInfo(uptr class_id) {<br>
+ CHECK_LT(class_id, kNumClasses);<br>
+ RegionInfo *regions = reinterpret_cast<RegionInfo*>(kSpaceBeg);<br>
+ return ®ions[-1 - class_id];<br>
+ }<br>
+<br>
+ void PushLifoList(LifoListNode **list, LifoListNode *node) {<br>
+ node->next = *list;<br>
+ *list = node;<br>
+ }<br>
+<br>
+ LifoListNode *PopLifoList(LifoListNode **list) {<br>
+ LifoListNode *res = *list;<br>
+ *list = (*list)->next;<br></blockquote><div><br></div></div><div> /\/\/\/\/\/\/\</div><div> res->next</div><div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ return res;<br>
+ }<br>
+<br>
+ LifoListNode *PopulateFreeList(uptr class_id, RegionInfo *region) {<br>
+ uptr size = SizeClassMap::Size(class_id);<br>
+ uptr beg_idx = region->allocated;<br>
+ uptr end_idx = beg_idx + kPopulateSize;<br>
+ LifoListNode *res = 0;<br>
+ uptr region_beg = kSpaceBeg + kRegionSize * class_id;<br>
+ uptr idx = beg_idx;<br>
+ do { // do-while loop because we need to put at least one item.<br>
+ uptr p = region_beg + idx;<br>
+ PushLifoList(&res, reinterpret_cast<LifoListNode*>(p));<br>
+ idx += size;<br>
+ } while (idx < end_idx);<br>
+ CHECK_LT(idx, kRegionSize);<br></blockquote><div><br></div></div><div>Better to place this check after calculation of <span style="color:rgb(34,34,34);font-size:12.571428298950195px;font-family:arial,sans-serif">end_idx, because at this point we've already corrupted some memory.</span></div>
<div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ region->allocated += idx - beg_idx;<br>
+ return res;<br>
+ }<br>
+<br>
+ void *AllocateBySizeClass(uptr class_id) {<br>
+ CHECK_LT(class_id, kNumClasses);<br>
+ RegionInfo *region = GetRegionInfo(class_id);<br>
+ // FIXME: Lock region->mutex;<br>
+ if (!region->free_list) {<br>
+ region->free_list = PopulateFreeList(class_id, region);<br>
+ }<br>
+ CHECK_NE(region->free_list, 0);<br>
+ LifoListNode *node = PopLifoList(®ion->free_list);<br>
+ return reinterpret_cast<void*>(node);<br></blockquote></div><div><div><br></div><div>static_cast is enough here</div></div><div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ }<br>
+<br>
+ void DeallocateBySizeClass(void *p, uptr class_id) {<br>
+ RegionInfo *region = GetRegionInfo(class_id);<br>
+ // FIXME: Lock region->mutex;<br>
+ PushLifoList(®ion->free_list, reinterpret_cast<LifoListNode*>(p));<br></blockquote><div><br></div></div><div>static_cast is enough here</div><div><div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ }<br>
+};<br>
+<br>
} // namespace __sanitizer<br>
<br>
#endif // SANITIZER_ALLOCATOR_H<br>
<br>
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h?rev=158991&r1=158990&r2=158991&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h?rev=158991&r1=158990&r2=158991&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h (original)<br>
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h Fri Jun 22 08:00:50 2012<br>
@@ -25,6 +25,7 @@<br>
const uptr kWordSizeInBits = 8 * kWordSize;<br>
const uptr kPageSizeBits = 12;<br>
const uptr kPageSize = 1UL << kPageSizeBits;<br>
+const uptr kCacheLineSize = 64;<br>
#ifndef _WIN32<br>
const uptr kMmapGranularity = kPageSize;<br>
#else<br>
<br>
Modified: compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator64_test.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator64_test.cc?rev=158991&r1=158990&r2=158991&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator64_test.cc?rev=158991&r1=158990&r2=158991&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator64_test.cc (original)<br>
+++ compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_allocator64_test.cc Fri Jun 22 08:00:50 2012<br>
@@ -11,9 +11,13 @@<br>
#include "sanitizer_common/sanitizer_allocator64.h"<br>
#include "gtest/gtest.h"<br>
<br>
+#include <algorithm><br>
+#include <vector><br>
+<br>
TEST(SanitizerCommon, DefaultSizeClassMap) {<br>
typedef DefaultSizeClassMap SCMap;<br>
<br>
+#if 0<br>
for (uptr i = 0; i < SCMap::kNumClasses; i++) {<br>
// printf("% 3ld: % 5ld (%4lx); ", i, SCMap::Size(i), SCMap::Size(i));<br>
printf("c%ld => %ld ", i, SCMap::Size(i));<br>
@@ -21,23 +25,69 @@<br>
printf("\n");<br>
}<br>
printf("\n");<br>
+#endif<br>
<br>
for (uptr c = 0; c < SCMap::kNumClasses; c++) {<br>
uptr s = SCMap::Size(c);<br>
- CHECK_EQ(SCMap::Class(s), c);<br>
+ CHECK_EQ(SCMap::ClassID(s), c);<br>
if (c != SCMap::kNumClasses - 1)<br>
- CHECK_EQ(SCMap::Class(s + 1), c + 1);<br>
- CHECK_EQ(SCMap::Class(s - 1), c);<br>
+ CHECK_EQ(SCMap::ClassID(s + 1), c + 1);<br>
+ CHECK_EQ(SCMap::ClassID(s - 1), c);<br>
if (c)<br>
CHECK_GT(SCMap::Size(c), SCMap::Size(c-1));<br>
}<br>
- CHECK_EQ(SCMap::Class(SCMap::kMaxSize + 1), 0);<br>
+ CHECK_EQ(SCMap::ClassID(SCMap::kMaxSize + 1), 0);<br>
<br>
for (uptr s = 1; s <= SCMap::kMaxSize; s++) {<br>
- uptr c = SCMap::Class(s);<br>
+ uptr c = SCMap::ClassID(s);<br>
CHECK_LT(c, SCMap::kNumClasses);<br>
CHECK_GE(SCMap::Size(c), s);<br>
if (c > 0)<br>
CHECK_LT(SCMap::Size(c-1), s);<br>
}<br>
}<br>
+<br>
+TEST(SanitizerCommon, SizeClassAllocator64) {<br>
+ const uptr space_beg = 0x600000000000ULL;<br>
+ const uptr space_size = 0x10000000000; // 1T<br>
+ const uptr metadata_size = 16;<br>
+ typedef DefaultSizeClassMap SCMap;<br>
+ typedef SizeClassAllocator64<space_beg, space_size,<br>
+ metadata_size, SCMap> Allocator;<br>
+<br>
+ Allocator a;<br>
+ a.Init();<br>
+<br>
+ static const uptr sizes[] = {1, 16, 30, 40, 100, 1000, 10000,<br>
+ 50000, 60000, 100000, 300000, 500000, 1000000, 2000000};<br>
+<br>
+ std::vector<void *> allocated;<br>
+<br>
+ uptr last_total_allocated = 0;<br>
+ for (int i = 0; i < 5; i++) {<br>
+ // Allocate a bunch of chunks.<br>
+ for (uptr s = 0; s < sizeof(sizes) /sizeof(sizes[0]); s++) {<br>
+ uptr size = sizes[s];<br>
+ // printf("s = %ld\n", size);<br>
+ uptr n_iter = std::max((uptr)2, 1000000 / size);<br>
+ for (uptr i = 0; i < n_iter; i++) {<br>
+ void *x = a.Allocate(size);<br>
+ allocated.push_back(x);<br>
+ CHECK(a.PointerIsMine(x));<br>
+ uptr class_id = a.GetSizeClass(x);<br>
+ CHECK_EQ(class_id, SCMap::ClassID(size));<br>
+ }<br>
+ }<br>
+ // Deallocate all.<br>
+ for (uptr i = 0; i < allocated.size(); i++) {<br>
+ a.Deallocate(allocated[i]);<br>
+ }<br>
+ allocated.clear();<br>
+ uptr total_allocated = a.TotalMemoryUsedIncludingFreeLists();<br>
+ if (last_total_allocated == 0)<br>
+ last_total_allocated = total_allocated;<br>
+ CHECK_EQ(last_total_allocated, total_allocated);<br>
+ }<br>
+<br>
+ a.TestOnlyUnmap();<br>
+}<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div></div></div><br></font></div>
</blockquote></div><br><div><br></div></div></div><div>Bot is broken:</div><div><pre><span style="font-family:'Courier New',courier,monotype">In file included from ../sanitizer_common/tests/sanitizer_allocator64_test.cc:11:
../sanitizer_common/sanitizer_allocator64.h:153:25: error: invalid use of non-static data member 'mutex'
sizeof(mutex) - sizeof(free_list) -
^~~~~
../sanitizer_common/sanitizer_allocator64.h:153:41: error: invalid use of non-static data member 'free_list'
sizeof(mutex) - sizeof(free_list) -
^~~~~~~~~
../sanitizer_common/sanitizer_allocator64.h:154:25: error: invalid use of non-static data member 'allocated_user'
sizeof(allocated_user) - sizeof(allocated_meta)];
^~~~~~~~~~~~~~
../sanitizer_common/sanitizer_allocator64.h:154:50: error: invalid use of non-static data member 'allocated_meta'
sizeof(allocated_user) - sizeof(allocated_meta)];
^~~~~~~~~~~~~~</span></pre><pre><span style="font-family:'Courier New',courier,monotype"><br></span></pre></div></font></div>
</blockquote></div><br><div>Another one, not yet fired but will fire:</div><div><div>../sanitizer_common/tests/sanitizer_allocator64_test.cc: In member function ‘virtual void SanitizerCommon_SizeClassAllocator64MetadataStress_Test::TestBody()’:</div>
<div>../sanitizer_common/tests/sanitizer_allocator64_test.cc:109:24: error: variable ‘sink’ set but not used [-Werror=unused-but-set-variable]</div></div><div><br></div></font></div>