| OLD | NEW |
| 1 // Copyright (c) 2005, Google Inc. | 1 // Copyright (c) 2005, 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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 #ifdef HAVE_MALLOC_H | 81 #ifdef HAVE_MALLOC_H |
| 82 #include <malloc.h> // defines pvalloc/etc on cygwin | 82 #include <malloc.h> // defines pvalloc/etc on cygwin |
| 83 #endif | 83 #endif |
| 84 #include <assert.h> | 84 #include <assert.h> |
| 85 #include <vector> | 85 #include <vector> |
| 86 #include <algorithm> | 86 #include <algorithm> |
| 87 #include <string> | 87 #include <string> |
| 88 #include <new> | 88 #include <new> |
| 89 #include "base/logging.h" | 89 #include "base/logging.h" |
| 90 #include "base/simple_mutex.h" | 90 #include "base/simple_mutex.h" |
| 91 #include "google/malloc_hook.h" | 91 #include "gperftools/malloc_hook.h" |
| 92 #include "google/malloc_extension.h" | 92 #include "gperftools/malloc_extension.h" |
| 93 #include "google/tcmalloc.h" | 93 #include "gperftools/tcmalloc.h" |
| 94 #include "thread_cache.h" | 94 #include "thread_cache.h" |
| 95 #include "tests/testutil.h" | 95 #include "tests/testutil.h" |
| 96 | 96 |
| 97 // Windows doesn't define pvalloc and a few other obsolete unix | 97 // Windows doesn't define pvalloc and a few other obsolete unix |
| 98 // functions; nor does it define posix_memalign (which is not obsolete). | 98 // functions; nor does it define posix_memalign (which is not obsolete). |
| 99 #if defined(_MSC_VER) || defined(__MINGW32__) | 99 #if defined(_WIN32) |
| 100 # define cfree free // don't bother to try to test these obsolete fns | 100 # define cfree free // don't bother to try to test these obsolete fns |
| 101 # define valloc malloc | 101 # define valloc malloc |
| 102 # define pvalloc malloc | 102 # define pvalloc malloc |
| 103 // I'd like to map posix_memalign to _aligned_malloc, but _aligned_malloc | 103 // I'd like to map posix_memalign to _aligned_malloc, but _aligned_malloc |
| 104 // must be paired with _aligned_free (not normal free), which is too | 104 // must be paired with _aligned_free (not normal free), which is too |
| 105 // invasive a change to how we allocate memory here. So just bail | 105 // invasive a change to how we allocate memory here. So just bail |
| 106 # include <errno.h> | 106 static bool kOSSupportsMemalign = false; |
| 107 # define memalign(alignment, size) malloc(size) | 107 static inline void* Memalign(size_t align, size_t size) { |
| 108 # define posix_memalign(pptr, align, size) ((*(pptr)=malloc(size)) ? 0 : ENOMEM) | 108 //LOG(FATAL) << "memalign not supported on windows"; |
| 109 exit(1); |
| 110 return NULL; |
| 111 } |
| 112 static inline int PosixMemalign(void** ptr, size_t align, size_t size) { |
| 113 //LOG(FATAL) << "posix_memalign not supported on windows"; |
| 114 exit(1); |
| 115 return -1; |
| 116 } |
| 117 |
| 118 // OS X defines posix_memalign in some OS versions but not others; |
| 119 // it's confusing enough to check that it's easiest to just not to test. |
| 120 #elif defined(__APPLE__) |
| 121 static bool kOSSupportsMemalign = false; |
| 122 static inline void* Memalign(size_t align, size_t size) { |
| 123 //LOG(FATAL) << "memalign not supported on OS X"; |
| 124 exit(1); |
| 125 return NULL; |
| 126 } |
| 127 static inline int PosixMemalign(void** ptr, size_t align, size_t size) { |
| 128 //LOG(FATAL) << "posix_memalign not supported on OS X"; |
| 129 exit(1); |
| 130 return -1; |
| 131 } |
| 132 |
| 133 #else |
| 134 static bool kOSSupportsMemalign = true; |
| 135 static inline void* Memalign(size_t align, size_t size) { |
| 136 return memalign(align, size); |
| 137 } |
| 138 static inline int PosixMemalign(void** ptr, size_t align, size_t size) { |
| 139 return posix_memalign(ptr, align, size); |
| 140 } |
| 141 |
| 109 #endif | 142 #endif |
| 110 | 143 |
| 111 // On systems (like freebsd) that don't define MAP_ANONYMOUS, use the old | 144 // On systems (like freebsd) that don't define MAP_ANONYMOUS, use the old |
| 112 // form of the name instead. | 145 // form of the name instead. |
| 113 #ifndef MAP_ANONYMOUS | 146 #ifndef MAP_ANONYMOUS |
| 114 # define MAP_ANONYMOUS MAP_ANON | 147 # define MAP_ANONYMOUS MAP_ANON |
| 115 #endif | 148 #endif |
| 116 | 149 |
| 117 #define LOGSTREAM stdout | 150 #define LOGSTREAM stdout |
| 118 | 151 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 142 static const int FLAGS_allocweight = 50; // Weight for picking allocation | 175 static const int FLAGS_allocweight = 50; // Weight for picking allocation |
| 143 static const int FLAGS_freeweight = 50; // Weight for picking free | 176 static const int FLAGS_freeweight = 50; // Weight for picking free |
| 144 static const int FLAGS_updateweight = 10; // Weight for picking update | 177 static const int FLAGS_updateweight = 10; // Weight for picking update |
| 145 static const int FLAGS_passweight = 1; // Weight for passing object | 178 static const int FLAGS_passweight = 1; // Weight for passing object |
| 146 | 179 |
| 147 static const int kSizeBits = 8 * sizeof(size_t); | 180 static const int kSizeBits = 8 * sizeof(size_t); |
| 148 static const size_t kMaxSize = ~static_cast<size_t>(0); | 181 static const size_t kMaxSize = ~static_cast<size_t>(0); |
| 149 static const size_t kMaxSignedSize = ((size_t(1) << (kSizeBits-1)) - 1); | 182 static const size_t kMaxSignedSize = ((size_t(1) << (kSizeBits-1)) - 1); |
| 150 | 183 |
| 151 static const size_t kNotTooBig = 100000; | 184 static const size_t kNotTooBig = 100000; |
| 152 static const size_t kTooBig = kMaxSize; | 185 // We want an allocation that is definitely more than main memory. OS |
| 186 // X has special logic to discard very big allocs before even passing |
| 187 // the request along to the user-defined memory allocator; we're not |
| 188 // interested in testing their logic, so we have to make sure we're |
| 189 // not *too* big. |
| 190 static const size_t kTooBig = kMaxSize - 100000; |
| 153 | 191 |
| 154 static int news_handled = 0; | 192 static int news_handled = 0; |
| 155 | 193 |
| 156 // Global array of threads | 194 // Global array of threads |
| 157 class TesterThread; | 195 class TesterThread; |
| 158 static TesterThread** threads; | 196 static TesterThread** threads; |
| 159 | 197 |
| 160 // To help with generating random numbers | 198 // To help with generating random numbers |
| 161 class TestHarness { | 199 class TestHarness { |
| 162 private: | 200 private: |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 assert(i < types_->size()); | 277 assert(i < types_->size()); |
| 240 if ((num_tests_ % FLAGS_log_every_n_tests) == 0) { | 278 if ((num_tests_ % FLAGS_log_every_n_tests) == 0) { |
| 241 fprintf(LOGSTREAM, " Test %d out of %d: %s\n", | 279 fprintf(LOGSTREAM, " Test %d out of %d: %s\n", |
| 242 num_tests_, FLAGS_numtests, (*types_)[i].name.c_str()); | 280 num_tests_, FLAGS_numtests, (*types_)[i].name.c_str()); |
| 243 } | 281 } |
| 244 return (*types_)[i].type; | 282 return (*types_)[i].type; |
| 245 } | 283 } |
| 246 | 284 |
| 247 class AllocatorState : public TestHarness { | 285 class AllocatorState : public TestHarness { |
| 248 public: | 286 public: |
| 249 explicit AllocatorState(int seed) : TestHarness(seed) { | 287 explicit AllocatorState(int seed) : TestHarness(seed), memalign_fraction_(0) { |
| 250 CHECK_GE(FLAGS_memalign_max_fraction, 0); | 288 if (kOSSupportsMemalign) { |
| 251 CHECK_LE(FLAGS_memalign_max_fraction, 1); | 289 CHECK_GE(FLAGS_memalign_max_fraction, 0); |
| 252 CHECK_GE(FLAGS_memalign_min_fraction, 0); | 290 CHECK_LE(FLAGS_memalign_max_fraction, 1); |
| 253 CHECK_LE(FLAGS_memalign_min_fraction, 1); | 291 CHECK_GE(FLAGS_memalign_min_fraction, 0); |
| 254 double delta = FLAGS_memalign_max_fraction - FLAGS_memalign_min_fraction; | 292 CHECK_LE(FLAGS_memalign_min_fraction, 1); |
| 255 CHECK_GE(delta, 0); | 293 double delta = FLAGS_memalign_max_fraction - FLAGS_memalign_min_fraction; |
| 256 memalign_fraction_ = (Uniform(10000)/10000.0 * delta + | 294 CHECK_GE(delta, 0); |
| 257 FLAGS_memalign_min_fraction); | 295 memalign_fraction_ = (Uniform(10000)/10000.0 * delta + |
| 258 //fprintf(LOGSTREAM, "memalign fraction: %f\n", memalign_fraction_); | 296 FLAGS_memalign_min_fraction); |
| 297 //fprintf(LOGSTREAM, "memalign fraction: %f\n", memalign_fraction_); |
| 298 } |
| 259 } | 299 } |
| 260 virtual ~AllocatorState() {} | 300 virtual ~AllocatorState() {} |
| 261 | 301 |
| 262 // Allocate memory. Randomly choose between malloc() or posix_memalign(). | 302 // Allocate memory. Randomly choose between malloc() or posix_memalign(). |
| 263 void* alloc(size_t size) { | 303 void* alloc(size_t size) { |
| 264 if (Uniform(100) < memalign_fraction_ * 100) { | 304 if (Uniform(100) < memalign_fraction_ * 100) { |
| 265 // Try a few times to find a reasonable alignment, or fall back on malloc. | 305 // Try a few times to find a reasonable alignment, or fall back on malloc. |
| 266 for (int i = 0; i < 5; i++) { | 306 for (int i = 0; i < 5; i++) { |
| 267 size_t alignment = 1 << Uniform(FLAGS_lg_max_memalign); | 307 size_t alignment = 1 << Uniform(FLAGS_lg_max_memalign); |
| 268 if (alignment >= sizeof(intptr_t) && | 308 if (alignment >= sizeof(intptr_t) && |
| 269 (size < sizeof(intptr_t) || | 309 (size < sizeof(intptr_t) || |
| 270 alignment < FLAGS_memalign_max_alignment_ratio * size)) { | 310 alignment < FLAGS_memalign_max_alignment_ratio * size)) { |
| 271 void *result = reinterpret_cast<void*>(static_cast<intptr_t>(0x1234)); | 311 void *result = reinterpret_cast<void*>(static_cast<intptr_t>(0x1234)); |
| 272 int err = posix_memalign(&result, alignment, size); | 312 int err = PosixMemalign(&result, alignment, size); |
| 273 if (err != 0) { | 313 if (err != 0) { |
| 274 CHECK_EQ(err, ENOMEM); | 314 CHECK_EQ(err, ENOMEM); |
| 275 } | 315 } |
| 276 return err == 0 ? result : NULL; | 316 return err == 0 ? result : NULL; |
| 277 } | 317 } |
| 278 } | 318 } |
| 279 } | 319 } |
| 280 return malloc(size); | 320 return malloc(size); |
| 281 } | 321 } |
| 282 | 322 |
| (...skipping 601 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 884 volatile bool g_no_memory = false; | 924 volatile bool g_no_memory = false; |
| 885 std::new_handler g_old_handler = NULL; | 925 std::new_handler g_old_handler = NULL; |
| 886 static void OnNoMemory() { | 926 static void OnNoMemory() { |
| 887 g_no_memory = true; | 927 g_no_memory = true; |
| 888 std::set_new_handler(g_old_handler); | 928 std::set_new_handler(g_old_handler); |
| 889 } | 929 } |
| 890 | 930 |
| 891 static void TestSetNewMode() { | 931 static void TestSetNewMode() { |
| 892 int old_mode = tc_set_new_mode(1); | 932 int old_mode = tc_set_new_mode(1); |
| 893 | 933 |
| 894 // DebugAllocation will try to catch huge allocations. We need to avoid this | |
| 895 // by requesting a smaller malloc block, that still can't be satisfied. | |
| 896 const size_t kHugeRequest = kTooBig - 1024; | |
| 897 | |
| 898 g_old_handler = std::set_new_handler(&OnNoMemory); | 934 g_old_handler = std::set_new_handler(&OnNoMemory); |
| 899 g_no_memory = false; | 935 g_no_memory = false; |
| 900 void* ret = malloc(kHugeRequest); | 936 void* ret = malloc(kTooBig); |
| 901 EXPECT_EQ(NULL, ret); | 937 EXPECT_EQ(NULL, ret); |
| 902 EXPECT_TRUE(g_no_memory); | 938 EXPECT_TRUE(g_no_memory); |
| 903 | 939 |
| 904 g_old_handler = std::set_new_handler(&OnNoMemory); | 940 g_old_handler = std::set_new_handler(&OnNoMemory); |
| 905 g_no_memory = false; | 941 g_no_memory = false; |
| 906 ret = calloc(1, kHugeRequest); | 942 ret = calloc(1, kTooBig); |
| 907 EXPECT_EQ(NULL, ret); | 943 EXPECT_EQ(NULL, ret); |
| 908 EXPECT_TRUE(g_no_memory); | 944 EXPECT_TRUE(g_no_memory); |
| 909 | 945 |
| 910 g_old_handler = std::set_new_handler(&OnNoMemory); | 946 g_old_handler = std::set_new_handler(&OnNoMemory); |
| 911 g_no_memory = false; | 947 g_no_memory = false; |
| 912 ret = realloc(NULL, kHugeRequest); | 948 ret = realloc(NULL, kTooBig); |
| 913 EXPECT_EQ(NULL, ret); | 949 EXPECT_EQ(NULL, ret); |
| 914 EXPECT_TRUE(g_no_memory); | 950 EXPECT_TRUE(g_no_memory); |
| 915 | 951 |
| 916 // Not really important, but must be small enough such that kAlignment + | 952 if (kOSSupportsMemalign) { |
| 917 // kHugeRequest does not overflow. | 953 // Not really important, but must be small enough such that |
| 918 const int kAlignment = 1 << 5; | 954 // kAlignment + kTooBig does not overflow. |
| 955 const int kAlignment = 1 << 5; |
| 919 | 956 |
| 920 g_old_handler = std::set_new_handler(&OnNoMemory); | 957 g_old_handler = std::set_new_handler(&OnNoMemory); |
| 921 g_no_memory = false; | 958 g_no_memory = false; |
| 922 ret = memalign(kAlignment, kHugeRequest); | 959 ret = Memalign(kAlignment, kTooBig); |
| 923 EXPECT_EQ(NULL, ret); | 960 EXPECT_EQ(NULL, ret); |
| 924 EXPECT_TRUE(g_no_memory); | 961 EXPECT_TRUE(g_no_memory); |
| 925 | 962 |
| 926 g_old_handler = std::set_new_handler(&OnNoMemory); | 963 g_old_handler = std::set_new_handler(&OnNoMemory); |
| 927 g_no_memory = false; | 964 g_no_memory = false; |
| 928 EXPECT_EQ(ENOMEM, | 965 EXPECT_EQ(ENOMEM, |
| 929 posix_memalign(&ret, kAlignment, kHugeRequest)); | 966 PosixMemalign(&ret, kAlignment, kTooBig)); |
| 930 EXPECT_EQ(NULL, ret); | 967 EXPECT_EQ(NULL, ret); |
| 931 EXPECT_TRUE(g_no_memory); | 968 EXPECT_TRUE(g_no_memory); |
| 969 } |
| 932 | 970 |
| 933 tc_set_new_mode(old_mode); | 971 tc_set_new_mode(old_mode); |
| 934 } | 972 } |
| 935 | 973 |
| 936 static int RunAllTests(int argc, char** argv) { | 974 static int RunAllTests(int argc, char** argv) { |
| 937 // Optional argv[1] is the seed | 975 // Optional argv[1] is the seed |
| 938 AllocatorState rnd(argc > 1 ? atoi(argv[1]) : 100); | 976 AllocatorState rnd(argc > 1 ? atoi(argv[1]) : 100); |
| 939 | 977 |
| 940 SetTestResourceLimit(); | 978 SetTestResourceLimit(); |
| 941 | 979 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1025 // We make sure we realloc to a big size, since some systems (OS | 1063 // We make sure we realloc to a big size, since some systems (OS |
| 1026 // X) will notice if the realloced size continues to fit into the | 1064 // X) will notice if the realloced size continues to fit into the |
| 1027 // malloc-block and make this a noop if so. | 1065 // malloc-block and make this a noop if so. |
| 1028 p1 = realloc(p1, 30000); | 1066 p1 = realloc(p1, 30000); |
| 1029 CHECK(p1 != NULL); | 1067 CHECK(p1 != NULL); |
| 1030 VerifyNewHookWasCalled(); | 1068 VerifyNewHookWasCalled(); |
| 1031 VerifyDeleteHookWasCalled(); | 1069 VerifyDeleteHookWasCalled(); |
| 1032 cfree(p1); // synonym for free | 1070 cfree(p1); // synonym for free |
| 1033 VerifyDeleteHookWasCalled(); | 1071 VerifyDeleteHookWasCalled(); |
| 1034 | 1072 |
| 1035 CHECK_EQ(posix_memalign(&p1, sizeof(p1), 40), 0); | 1073 if (kOSSupportsMemalign) { |
| 1036 CHECK(p1 != NULL); | 1074 CHECK_EQ(PosixMemalign(&p1, sizeof(p1), 40), 0); |
| 1037 VerifyNewHookWasCalled(); | 1075 CHECK(p1 != NULL); |
| 1038 free(p1); | 1076 VerifyNewHookWasCalled(); |
| 1039 VerifyDeleteHookWasCalled(); | 1077 free(p1); |
| 1078 VerifyDeleteHookWasCalled(); |
| 1040 | 1079 |
| 1041 p1 = memalign(sizeof(p1) * 2, 50); | 1080 p1 = Memalign(sizeof(p1) * 2, 50); |
| 1042 CHECK(p1 != NULL); | 1081 CHECK(p1 != NULL); |
| 1043 VerifyNewHookWasCalled(); | 1082 VerifyNewHookWasCalled(); |
| 1044 free(p1); | 1083 free(p1); |
| 1045 VerifyDeleteHookWasCalled(); | 1084 VerifyDeleteHookWasCalled(); |
| 1085 } |
| 1046 | 1086 |
| 1047 // Windows has _aligned_malloc. Let's test that that's captured too. | 1087 // Windows has _aligned_malloc. Let's test that that's captured too. |
| 1048 #if (defined(_MSC_VER) || defined(__MINGW32__)) && !defined(PERFTOOLS_NO_ALIGNED
_MALLOC) | 1088 #if (defined(_MSC_VER) || defined(__MINGW32__)) && !defined(PERFTOOLS_NO_ALIGNED
_MALLOC) |
| 1049 p1 = _aligned_malloc(sizeof(p1) * 2, 64); | 1089 p1 = _aligned_malloc(sizeof(p1) * 2, 64); |
| 1090 CHECK(p1 != NULL); |
| 1050 VerifyNewHookWasCalled(); | 1091 VerifyNewHookWasCalled(); |
| 1051 _aligned_free(p1); | 1092 _aligned_free(p1); |
| 1052 VerifyDeleteHookWasCalled(); | 1093 VerifyDeleteHookWasCalled(); |
| 1053 #endif | 1094 #endif |
| 1054 | 1095 |
| 1055 p1 = valloc(60); | 1096 p1 = valloc(60); |
| 1056 CHECK(p1 != NULL); | 1097 CHECK(p1 != NULL); |
| 1057 VerifyNewHookWasCalled(); | 1098 VerifyNewHookWasCalled(); |
| 1058 free(p1); | 1099 free(p1); |
| 1059 VerifyDeleteHookWasCalled(); | 1100 VerifyDeleteHookWasCalled(); |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1277 int minor; | 1318 int minor; |
| 1278 const char* patch; | 1319 const char* patch; |
| 1279 char mmp[64]; | 1320 char mmp[64]; |
| 1280 const char* human_version = tc_version(&major, &minor, &patch); | 1321 const char* human_version = tc_version(&major, &minor, &patch); |
| 1281 snprintf(mmp, sizeof(mmp), "%d.%d%s", major, minor, patch); | 1322 snprintf(mmp, sizeof(mmp), "%d.%d%s", major, minor, patch); |
| 1282 CHECK(!strcmp(PACKAGE_STRING, human_version)); | 1323 CHECK(!strcmp(PACKAGE_STRING, human_version)); |
| 1283 CHECK(!strcmp(PACKAGE_VERSION, mmp)); | 1324 CHECK(!strcmp(PACKAGE_VERSION, mmp)); |
| 1284 | 1325 |
| 1285 fprintf(LOGSTREAM, "PASS\n"); | 1326 fprintf(LOGSTREAM, "PASS\n"); |
| 1286 } | 1327 } |
| OLD | NEW |