Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1070)

Side by Side Diff: third_party/tcmalloc/chromium/src/tests/tcmalloc_unittest.cc

Issue 440027: Merge r77 from upstream tcmalloc to the local chromium branch.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 11 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/tcmalloc/chromium/src/tests/profile-handler_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 // form of the name instead. 117 // form of the name instead.
118 #ifndef MAP_ANONYMOUS 118 #ifndef MAP_ANONYMOUS
119 # define MAP_ANONYMOUS MAP_ANON 119 # define MAP_ANONYMOUS MAP_ANON
120 #endif 120 #endif
121 121
122 #define LOGSTREAM stdout 122 #define LOGSTREAM stdout
123 123
124 using std::vector; 124 using std::vector;
125 using std::string; 125 using std::string;
126 126
127 DECLARE_double(tcmalloc_release_rate);
128 DECLARE_int32(max_free_queue_size); // in debugallocation.cc
129
130 namespace testing { 127 namespace testing {
131 128
132 static const int FLAGS_numtests = 50000; 129 static const int FLAGS_numtests = 50000;
133 static const int FLAGS_log_every_n_tests = 50000; // log exactly once 130 static const int FLAGS_log_every_n_tests = 50000; // log exactly once
134 131
135 // Testing parameters 132 // Testing parameters
136 static const int FLAGS_lgmaxsize = 16; // lg() of the max size object to alloc 133 static const int FLAGS_lgmaxsize = 16; // lg() of the max size object to alloc
137 static const int FLAGS_numthreads = 10; // Number of threads 134 static const int FLAGS_numthreads = 10; // Number of threads
138 static const int FLAGS_threadmb = 4; // Max memory size allocated by thread 135 static const int FLAGS_threadmb = 4; // Max memory size allocated by thread
139 static const int FLAGS_lg_max_memalign = 18; // lg of max alignment for memalign 136 static const int FLAGS_lg_max_memalign = 18; // lg of max alignment for memalign
(...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after
743 char** array = new char*[kNum]; 740 char** array = new char*[kNum];
744 for (int i = 0; i < kNum; ++i) { 741 for (int i = 0; i < kNum; ++i) {
745 array[i] = new char[10]; 742 array[i] = new char[10];
746 } 743 }
747 for (int i = 0; i < kNum; ++i) { 744 for (int i = 0; i < kNum; ++i) {
748 delete[] array[i]; 745 delete[] array[i];
749 } 746 }
750 delete[] array; 747 delete[] array;
751 } 748 }
752 749
753 namespace {
754
755 struct RangeCallbackState {
756 uintptr_t ptr;
757 base::MallocRange::Type expected_type;
758 size_t min_size;
759 bool matched;
760 };
761
762 static void RangeCallback(void* arg, const base::MallocRange* r) {
763 RangeCallbackState* state = reinterpret_cast<RangeCallbackState*>(arg);
764 if (state->ptr >= r->address &&
765 state->ptr < r->address + r->length) {
766 CHECK_EQ(r->type, state->expected_type);
767 CHECK_GE(r->length, state->min_size);
768 state->matched = true;
769 }
770 }
771
772 // Check that at least one of the callbacks from Ranges() contains
773 // the specified address with the specified type, and has size
774 // >= min_size.
775 static void CheckRangeCallback(void* ptr, base::MallocRange::Type type,
776 size_t min_size) {
777 RangeCallbackState state;
778 state.ptr = reinterpret_cast<uintptr_t>(ptr);
779 state.expected_type = type;
780 state.min_size = min_size;
781 state.matched = false;
782 MallocExtension::instance()->Ranges(&state, RangeCallback);
783 CHECK(state.matched);
784 }
785
786 }
787
788 static void TestRanges() {
789 static const int MB = 1048576;
790 void* a = malloc(MB);
791 void* b = malloc(MB);
792 CheckRangeCallback(a, base::MallocRange::INUSE, MB);
793 CheckRangeCallback(b, base::MallocRange::INUSE, MB);
794 free(a);
795 CheckRangeCallback(a, base::MallocRange::FREE, MB);
796 CheckRangeCallback(b, base::MallocRange::INUSE, MB);
797 MallocExtension::instance()->ReleaseFreeMemory();
798 CheckRangeCallback(a, base::MallocRange::UNMAPPED, MB);
799 CheckRangeCallback(b, base::MallocRange::INUSE, MB);
800 free(b);
801 CheckRangeCallback(a, base::MallocRange::UNMAPPED, MB);
802 CheckRangeCallback(b, base::MallocRange::FREE, MB);
803 }
804
805 static size_t GetUnmappedBytes() {
806 size_t bytes;
807 CHECK(MallocExtension::instance()->GetNumericProperty(
808 "tcmalloc.pageheap_unmapped_bytes", &bytes));
809 return bytes;
810 }
811
812 static void TestReleaseToSystem() {
813 // Debug allocation mode adds overhead to each allocation which
814 // messes up all the equality tests here. I just disable the
815 // teset in this mode. TODO(csilvers): get it to work for debugalloc?
816 #ifndef DEBUGALLOCATION
817 const double old_tcmalloc_release_rate = FLAGS_tcmalloc_release_rate;
818 FLAGS_tcmalloc_release_rate = 0;
819
820 static const int MB = 1048576;
821 void* a = malloc(MB);
822 void* b = malloc(MB);
823 MallocExtension::instance()->ReleaseFreeMemory();
824 size_t starting_bytes = GetUnmappedBytes();
825
826 // Calling ReleaseFreeMemory() a second time shouldn't do anything.
827 MallocExtension::instance()->ReleaseFreeMemory();
828 EXPECT_EQ(starting_bytes, GetUnmappedBytes());
829
830 // ReleaseToSystem shouldn't do anything either.
831 MallocExtension::instance()->ReleaseToSystem(MB);
832 EXPECT_EQ(starting_bytes, GetUnmappedBytes());
833
834 free(a);
835
836 // Negative numbers should be ignored.
837 MallocExtension::instance()->ReleaseToSystem(-5);
838 EXPECT_EQ(starting_bytes, GetUnmappedBytes());
839
840 // The span to release should be 1MB.
841 MallocExtension::instance()->ReleaseToSystem(MB/2);
842 EXPECT_EQ(starting_bytes + MB, GetUnmappedBytes());
843
844 // Should do nothing since the previous call released too much.
845 MallocExtension::instance()->ReleaseToSystem(MB/4);
846 EXPECT_EQ(starting_bytes + MB, GetUnmappedBytes());
847
848 free(b);
849
850 // Use up the extra MB/4 bytes from 'a' and also release 'b'.
851 MallocExtension::instance()->ReleaseToSystem(MB/2);
852 EXPECT_EQ(starting_bytes + 2*MB, GetUnmappedBytes());
853
854 // Should do nothing since the previous call released too much.
855 MallocExtension::instance()->ReleaseToSystem(MB/2);
856 EXPECT_EQ(starting_bytes + 2*MB, GetUnmappedBytes());
857
858 // Nothing else to release.
859 MallocExtension::instance()->ReleaseFreeMemory();
860 EXPECT_EQ(starting_bytes + 2*MB, GetUnmappedBytes());
861
862 a = malloc(MB);
863 free(a);
864 EXPECT_EQ(starting_bytes + MB, GetUnmappedBytes());
865
866 // Releasing less than a page should still trigger a release.
867 MallocExtension::instance()->ReleaseToSystem(1);
868 EXPECT_EQ(starting_bytes + 2*MB, GetUnmappedBytes());
869
870 FLAGS_tcmalloc_release_rate = old_tcmalloc_release_rate;
871 #endif // #ifndef DEBUGALLOCATION
872 }
873
874 static int RunAllTests(int argc, char** argv) { 750 static int RunAllTests(int argc, char** argv) {
875 // Optional argv[1] is the seed 751 // Optional argv[1] is the seed
876 AllocatorState rnd(argc > 1 ? atoi(argv[1]) : 100); 752 AllocatorState rnd(argc > 1 ? atoi(argv[1]) : 100);
877 753
878 SetTestResourceLimit(); 754 SetTestResourceLimit();
879 755
880 // TODO(odo): This test has been disabled because it is only by luck that it 756 // TODO(odo): This test has been disabled because it is only by luck that it
881 // does not result in fragmentation. When tcmalloc makes an allocation which 757 // does not result in fragmentation. When tcmalloc makes an allocation which
882 // spans previously unused leaves of the pagemap it will allocate and fill in 758 // spans previously unused leaves of the pagemap it will allocate and fill in
883 // the leaves to cover the new allocation. The leaves happen to be 256MiB in 759 // the leaves to cover the new allocation. The leaves happen to be 256MiB in
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
1140 #ifndef DEBUGALLOCATION // debug allocation takes forever for huge allocs 1016 #ifndef DEBUGALLOCATION // debug allocation takes forever for huge allocs
1141 fprintf(LOGSTREAM, "Testing out of memory\n"); 1017 fprintf(LOGSTREAM, "Testing out of memory\n");
1142 for (int s = 0; ; s += (10<<20)) { 1018 for (int s = 0; ; s += (10<<20)) {
1143 void* large_object = rnd.alloc(s); 1019 void* large_object = rnd.alloc(s);
1144 if (large_object == NULL) break; 1020 if (large_object == NULL) break;
1145 free(large_object); 1021 free(large_object);
1146 } 1022 }
1147 #endif 1023 #endif
1148 1024
1149 TestHugeThreadCache(); 1025 TestHugeThreadCache();
1150 TestRanges();
1151 TestReleaseToSystem();
1152 1026
1153 return 0; 1027 return 0;
1154 } 1028 }
1155 1029
1156 } 1030 }
1157 1031
1158 using testing::RunAllTests; 1032 using testing::RunAllTests;
1159 1033
1160 int main(int argc, char** argv) { 1034 int main(int argc, char** argv) {
1161 #ifdef DEBUGALLOCATION // debug allocation takes forever for huge allocs
1162 FLAGS_max_free_queue_size = 0; // return freed blocks to tcmalloc immediately
1163 #endif
1164
1165 RunAllTests(argc, argv); 1035 RunAllTests(argc, argv);
1166 1036
1167 // Test tc_version() 1037 // Test tc_version()
1168 fprintf(LOGSTREAM, "Testing tc_version()\n"); 1038 fprintf(LOGSTREAM, "Testing tc_version()\n");
1169 int major; 1039 int major;
1170 int minor; 1040 int minor;
1171 const char* patch; 1041 const char* patch;
1172 char mmp[64]; 1042 char mmp[64];
1173 const char* human_version = tc_version(&major, &minor, &patch); 1043 const char* human_version = tc_version(&major, &minor, &patch);
1174 snprintf(mmp, sizeof(mmp), "%d.%d%s", major, minor, patch); 1044 snprintf(mmp, sizeof(mmp), "%d.%d%s", major, minor, patch);
1175 CHECK(!strcmp(PACKAGE_STRING, human_version)); 1045 CHECK(!strcmp(PACKAGE_STRING, human_version));
1176 CHECK(!strcmp(PACKAGE_VERSION, mmp)); 1046 CHECK(!strcmp(PACKAGE_VERSION, mmp));
1177 1047
1178 fprintf(LOGSTREAM, "PASS\n"); 1048 fprintf(LOGSTREAM, "PASS\n");
1179 } 1049 }
OLDNEW
« no previous file with comments | « third_party/tcmalloc/chromium/src/tests/profile-handler_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698