OLD | NEW |
1 // Copyright (c) 2008, Google Inc. | 1 // Copyright (c) 2008, 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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 // This value strikes a balance between the constraints above. | 92 // This value strikes a balance between the constraints above. |
93 if (num > 32) num = 32; | 93 if (num > 32) num = 32; |
94 | 94 |
95 return num; | 95 return num; |
96 } | 96 } |
97 | 97 |
98 // Initialize the mapping arrays | 98 // Initialize the mapping arrays |
99 void SizeMap::Init() { | 99 void SizeMap::Init() { |
100 // Do some sanity checking on add_amount[]/shift_amount[]/class_array[] | 100 // Do some sanity checking on add_amount[]/shift_amount[]/class_array[] |
101 if (ClassIndex(0) < 0) { | 101 if (ClassIndex(0) < 0) { |
102 Log(kCrash, __FILE__, __LINE__, | 102 CRASH("Invalid class index %d for size 0\n", ClassIndex(0)); |
103 "Invalid class index for size 0", ClassIndex(0)); | |
104 } | 103 } |
105 if (ClassIndex(kMaxSize) >= sizeof(class_array_)) { | 104 if (ClassIndex(kMaxSize) >= sizeof(class_array_)) { |
106 Log(kCrash, __FILE__, __LINE__, | 105 CRASH("Invalid class index %d for kMaxSize\n", ClassIndex(kMaxSize)); |
107 "Invalid class index for kMaxSize", ClassIndex(kMaxSize)); | |
108 } | 106 } |
109 | 107 |
110 // Compute the size classes we want to use | 108 // Compute the size classes we want to use |
111 int sc = 1; // Next size class to assign | 109 int sc = 1; // Next size class to assign |
112 int alignment = kAlignment; | 110 int alignment = kAlignment; |
113 CHECK_CONDITION(kAlignment <= 16); | 111 CHECK_CONDITION(kAlignment <= 16); |
114 for (size_t size = kAlignment; size <= kMaxSize; size += alignment) { | 112 for (size_t size = kAlignment; size <= kMaxSize; size += alignment) { |
115 alignment = AlignmentForSize(size); | 113 alignment = AlignmentForSize(size); |
116 CHECK_CONDITION((size % alignment) == 0); | 114 CHECK_CONDITION((size % alignment) == 0); |
117 | 115 |
(...skipping 24 matching lines...) Expand all Loading... |
142 continue; | 140 continue; |
143 } | 141 } |
144 } | 142 } |
145 | 143 |
146 // Add new class | 144 // Add new class |
147 class_to_pages_[sc] = my_pages; | 145 class_to_pages_[sc] = my_pages; |
148 class_to_size_[sc] = size; | 146 class_to_size_[sc] = size; |
149 sc++; | 147 sc++; |
150 } | 148 } |
151 if (sc != kNumClasses) { | 149 if (sc != kNumClasses) { |
152 Log(kCrash, __FILE__, __LINE__, | 150 CRASH("wrong number of size classes: found %d instead of %d\n", |
153 "wrong number of size classes: (found vs. expected )", sc, kNumClasses); | 151 sc, int(kNumClasses)); |
154 } | 152 } |
155 | 153 |
156 // Initialize the mapping arrays | 154 // Initialize the mapping arrays |
157 int next_size = 0; | 155 int next_size = 0; |
158 for (int c = 1; c < kNumClasses; c++) { | 156 for (int c = 1; c < kNumClasses; c++) { |
159 const int max_size_in_class = class_to_size_[c]; | 157 const int max_size_in_class = class_to_size_[c]; |
160 for (int s = next_size; s <= max_size_in_class; s += kAlignment) { | 158 for (int s = next_size; s <= max_size_in_class; s += kAlignment) { |
161 class_array_[ClassIndex(s)] = c; | 159 class_array_[ClassIndex(s)] = c; |
162 } | 160 } |
163 next_size = max_size_in_class + kAlignment; | 161 next_size = max_size_in_class + kAlignment; |
164 } | 162 } |
165 | 163 |
166 // Double-check sizes just to be safe | 164 // Double-check sizes just to be safe |
167 for (size_t size = 0; size <= kMaxSize; size++) { | 165 for (size_t size = 0; size <= kMaxSize; size++) { |
168 const int sc = SizeClass(size); | 166 const int sc = SizeClass(size); |
169 if (sc <= 0 || sc >= kNumClasses) { | 167 if (sc <= 0 || sc >= kNumClasses) { |
170 Log(kCrash, __FILE__, __LINE__, | 168 CRASH("Bad size class %d for %" PRIuS "\n", sc, size); |
171 "Bad size class (class, size)", sc, size); | |
172 } | 169 } |
173 if (sc > 1 && size <= class_to_size_[sc-1]) { | 170 if (sc > 1 && size <= class_to_size_[sc-1]) { |
174 Log(kCrash, __FILE__, __LINE__, | 171 CRASH("Allocating unnecessarily large class %d for %" PRIuS |
175 "Allocating unnecessarily large class (class, size)", sc, size); | 172 "\n", sc, size); |
176 } | 173 } |
177 const size_t s = class_to_size_[sc]; | 174 const size_t s = class_to_size_[sc]; |
178 if (size > s || s == 0) { | 175 if (size > s) { |
179 Log(kCrash, __FILE__, __LINE__, | 176 CRASH("Bad size %" PRIuS " for %" PRIuS " (sc = %d)\n", s, size, sc); |
180 "Bad (class, size, requested)", sc, s, size); | 177 } |
| 178 if (s == 0) { |
| 179 CRASH("Bad size %" PRIuS " for %" PRIuS " (sc = %d)\n", s, size, sc); |
181 } | 180 } |
182 } | 181 } |
183 | 182 |
184 // Initialize the num_objects_to_move array. | 183 // Initialize the num_objects_to_move array. |
185 for (size_t cl = 1; cl < kNumClasses; ++cl) { | 184 for (size_t cl = 1; cl < kNumClasses; ++cl) { |
186 num_objects_to_move_[cl] = NumMoveSize(ByteSizeForClass(cl)); | 185 num_objects_to_move_[cl] = NumMoveSize(ByteSizeForClass(cl)); |
187 } | 186 } |
188 } | 187 } |
189 | 188 |
| 189 void SizeMap::Dump(TCMalloc_Printer* out) { |
| 190 // Dump class sizes and maximum external wastage per size class |
| 191 for (size_t cl = 1; cl < kNumClasses; ++cl) { |
| 192 const int alloc_size = class_to_pages_[cl] << kPageShift; |
| 193 const int alloc_objs = alloc_size / class_to_size_[cl]; |
| 194 const int min_used = (class_to_size_[cl-1] + 1) * alloc_objs; |
| 195 const int max_waste = alloc_size - min_used; |
| 196 out->printf("SC %3d [ %8d .. %8d ] from %8d ; %2.0f%% maxwaste\n", |
| 197 int(cl), |
| 198 int(class_to_size_[cl-1] + 1), |
| 199 int(class_to_size_[cl]), |
| 200 int(class_to_pages_[cl] << kPageShift), |
| 201 max_waste * 100.0 / alloc_size |
| 202 ); |
| 203 } |
| 204 } |
| 205 |
190 // Metadata allocator -- keeps stats about how many bytes allocated. | 206 // Metadata allocator -- keeps stats about how many bytes allocated. |
191 static uint64_t metadata_system_bytes_ = 0; | 207 static uint64_t metadata_system_bytes_ = 0; |
192 void* MetaDataAlloc(size_t bytes) { | 208 void* MetaDataAlloc(size_t bytes) { |
193 void* result = TCMalloc_SystemAlloc(bytes, NULL); | 209 void* result = TCMalloc_SystemAlloc(bytes, NULL); |
194 if (result != NULL) { | 210 if (result != NULL) { |
195 metadata_system_bytes_ += bytes; | 211 metadata_system_bytes_ += bytes; |
196 } | 212 } |
197 return result; | 213 return result; |
198 } | 214 } |
199 | 215 |
200 uint64_t metadata_system_bytes() { return metadata_system_bytes_; } | 216 uint64_t metadata_system_bytes() { return metadata_system_bytes_; } |
201 | 217 |
202 } // namespace tcmalloc | 218 } // namespace tcmalloc |
OLD | NEW |