| 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 CRASH("Invalid class index %d for size 0\n", ClassIndex(0)); | 102 Log(kCrash, __FILE__, __LINE__, |
| 103 "Invalid class index for size 0", ClassIndex(0)); |
| 103 } | 104 } |
| 104 if (ClassIndex(kMaxSize) >= sizeof(class_array_)) { | 105 if (ClassIndex(kMaxSize) >= sizeof(class_array_)) { |
| 105 CRASH("Invalid class index %d for kMaxSize\n", ClassIndex(kMaxSize)); | 106 Log(kCrash, __FILE__, __LINE__, |
| 107 "Invalid class index for kMaxSize", ClassIndex(kMaxSize)); |
| 106 } | 108 } |
| 107 | 109 |
| 108 // Compute the size classes we want to use | 110 // Compute the size classes we want to use |
| 109 int sc = 1; // Next size class to assign | 111 int sc = 1; // Next size class to assign |
| 110 int alignment = kAlignment; | 112 int alignment = kAlignment; |
| 111 CHECK_CONDITION(kAlignment <= 16); | 113 CHECK_CONDITION(kAlignment <= 16); |
| 112 for (size_t size = kAlignment; size <= kMaxSize; size += alignment) { | 114 for (size_t size = kAlignment; size <= kMaxSize; size += alignment) { |
| 113 alignment = AlignmentForSize(size); | 115 alignment = AlignmentForSize(size); |
| 114 CHECK_CONDITION((size % alignment) == 0); | 116 CHECK_CONDITION((size % alignment) == 0); |
| 115 | 117 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 140 continue; | 142 continue; |
| 141 } | 143 } |
| 142 } | 144 } |
| 143 | 145 |
| 144 // Add new class | 146 // Add new class |
| 145 class_to_pages_[sc] = my_pages; | 147 class_to_pages_[sc] = my_pages; |
| 146 class_to_size_[sc] = size; | 148 class_to_size_[sc] = size; |
| 147 sc++; | 149 sc++; |
| 148 } | 150 } |
| 149 if (sc != kNumClasses) { | 151 if (sc != kNumClasses) { |
| 150 CRASH("wrong number of size classes: found %d instead of %d\n", | 152 Log(kCrash, __FILE__, __LINE__, |
| 151 sc, int(kNumClasses)); | 153 "wrong number of size classes: (found vs. expected )", sc, kNumClasses); |
| 152 } | 154 } |
| 153 | 155 |
| 154 // Initialize the mapping arrays | 156 // Initialize the mapping arrays |
| 155 int next_size = 0; | 157 int next_size = 0; |
| 156 for (int c = 1; c < kNumClasses; c++) { | 158 for (int c = 1; c < kNumClasses; c++) { |
| 157 const int max_size_in_class = class_to_size_[c]; | 159 const int max_size_in_class = class_to_size_[c]; |
| 158 for (int s = next_size; s <= max_size_in_class; s += kAlignment) { | 160 for (int s = next_size; s <= max_size_in_class; s += kAlignment) { |
| 159 class_array_[ClassIndex(s)] = c; | 161 class_array_[ClassIndex(s)] = c; |
| 160 } | 162 } |
| 161 next_size = max_size_in_class + kAlignment; | 163 next_size = max_size_in_class + kAlignment; |
| 162 } | 164 } |
| 163 | 165 |
| 164 // Double-check sizes just to be safe | 166 // Double-check sizes just to be safe |
| 165 for (size_t size = 0; size <= kMaxSize; size++) { | 167 for (size_t size = 0; size <= kMaxSize; size++) { |
| 166 const int sc = SizeClass(size); | 168 const int sc = SizeClass(size); |
| 167 if (sc <= 0 || sc >= kNumClasses) { | 169 if (sc <= 0 || sc >= kNumClasses) { |
| 168 CRASH("Bad size class %d for %" PRIuS "\n", sc, size); | 170 Log(kCrash, __FILE__, __LINE__, |
| 171 "Bad size class (class, size)", sc, size); |
| 169 } | 172 } |
| 170 if (sc > 1 && size <= class_to_size_[sc-1]) { | 173 if (sc > 1 && size <= class_to_size_[sc-1]) { |
| 171 CRASH("Allocating unnecessarily large class %d for %" PRIuS | 174 Log(kCrash, __FILE__, __LINE__, |
| 172 "\n", sc, size); | 175 "Allocating unnecessarily large class (class, size)", sc, size); |
| 173 } | 176 } |
| 174 const size_t s = class_to_size_[sc]; | 177 const size_t s = class_to_size_[sc]; |
| 175 if (size > s) { | 178 if (size > s || s == 0) { |
| 176 CRASH("Bad size %" PRIuS " for %" PRIuS " (sc = %d)\n", s, size, sc); | 179 Log(kCrash, __FILE__, __LINE__, |
| 177 } | 180 "Bad (class, size, requested)", sc, s, size); |
| 178 if (s == 0) { | |
| 179 CRASH("Bad size %" PRIuS " for %" PRIuS " (sc = %d)\n", s, size, sc); | |
| 180 } | 181 } |
| 181 } | 182 } |
| 182 | 183 |
| 183 // Initialize the num_objects_to_move array. | 184 // Initialize the num_objects_to_move array. |
| 184 for (size_t cl = 1; cl < kNumClasses; ++cl) { | 185 for (size_t cl = 1; cl < kNumClasses; ++cl) { |
| 185 num_objects_to_move_[cl] = NumMoveSize(ByteSizeForClass(cl)); | 186 num_objects_to_move_[cl] = NumMoveSize(ByteSizeForClass(cl)); |
| 186 } | 187 } |
| 187 } | 188 } |
| 188 | 189 |
| 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 | |
| 206 // Metadata allocator -- keeps stats about how many bytes allocated. | 190 // Metadata allocator -- keeps stats about how many bytes allocated. |
| 207 static uint64_t metadata_system_bytes_ = 0; | 191 static uint64_t metadata_system_bytes_ = 0; |
| 208 void* MetaDataAlloc(size_t bytes) { | 192 void* MetaDataAlloc(size_t bytes) { |
| 209 void* result = TCMalloc_SystemAlloc(bytes, NULL); | 193 void* result = TCMalloc_SystemAlloc(bytes, NULL); |
| 210 if (result != NULL) { | 194 if (result != NULL) { |
| 211 metadata_system_bytes_ += bytes; | 195 metadata_system_bytes_ += bytes; |
| 212 } | 196 } |
| 213 return result; | 197 return result; |
| 214 } | 198 } |
| 215 | 199 |
| 216 uint64_t metadata_system_bytes() { return metadata_system_bytes_; } | 200 uint64_t metadata_system_bytes() { return metadata_system_bytes_; } |
| 217 | 201 |
| 218 } // namespace tcmalloc | 202 } // namespace tcmalloc |
| OLD | NEW |