OLD | NEW |
1 // Copyright (c) 2003, Google Inc. | 1 // Copyright (c) 2003, 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 26 matching lines...) Expand all Loading... |
37 #include <stdio.h> | 37 #include <stdio.h> |
38 #ifdef HAVE_SYS_RESOURCE_H | 38 #ifdef HAVE_SYS_RESOURCE_H |
39 #include <sys/time.h> // for struct timeval | 39 #include <sys/time.h> // for struct timeval |
40 #include <sys/resource.h> // for getrusage | 40 #include <sys/resource.h> // for getrusage |
41 #endif | 41 #endif |
42 #ifdef _WIN32 | 42 #ifdef _WIN32 |
43 #include <windows.h> // for GetTickCount() | 43 #include <windows.h> // for GetTickCount() |
44 #endif | 44 #endif |
45 #include <vector> | 45 #include <vector> |
46 #include "base/logging.h" | 46 #include "base/logging.h" |
| 47 #include "common.h" |
47 #include <google/malloc_extension.h> | 48 #include <google/malloc_extension.h> |
48 | 49 |
49 using std::vector; | 50 using std::vector; |
50 | 51 |
51 int main(int argc, char** argv) { | 52 int main(int argc, char** argv) { |
52 static const int kAllocSize = 36<<10; // Bigger than tcmalloc page size | 53 // Make kAllocSize one page larger than the maximum small object size. |
53 static const int kTotalAlloc = 400 << 20; // Allocate 400MB in total | 54 static const int kAllocSize = kMaxSize + kPageSize; |
| 55 // Allocate 400MB in total. |
| 56 static const int kTotalAlloc = 400 << 20; |
54 static const int kAllocIterations = kTotalAlloc / kAllocSize; | 57 static const int kAllocIterations = kTotalAlloc / kAllocSize; |
55 | 58 |
56 // Allocate lots of objects | 59 // Allocate lots of objects |
57 vector<char*> saved(kAllocIterations); | 60 vector<char*> saved(kAllocIterations); |
58 for (int i = 0; i < kAllocIterations; i++) { | 61 for (int i = 0; i < kAllocIterations; i++) { |
59 saved[i] = new char[kAllocSize]; | 62 saved[i] = new char[kAllocSize]; |
60 } | 63 } |
61 | 64 |
| 65 // Check the current "slack". |
| 66 size_t slack_before; |
| 67 MallocExtension::instance()->GetNumericProperty("tcmalloc.slack_bytes", |
| 68 &slack_before); |
| 69 |
62 // Free alternating ones to fragment heap | 70 // Free alternating ones to fragment heap |
63 size_t free_bytes = 0; | 71 size_t free_bytes = 0; |
64 for (int i = 0; i < saved.size(); i += 2) { | 72 for (int i = 0; i < saved.size(); i += 2) { |
65 delete[] saved[i]; | 73 delete[] saved[i]; |
66 free_bytes += kAllocSize; | 74 free_bytes += kAllocSize; |
67 } | 75 } |
68 | 76 |
69 // Check that slack is within 10% of expected | 77 // Check that slack delta is within 10% of expected. |
70 size_t slack; | 78 size_t slack_after; |
71 MallocExtension::instance()->GetNumericProperty("tcmalloc.slack_bytes", | 79 MallocExtension::instance()->GetNumericProperty("tcmalloc.slack_bytes", |
72 &slack); | 80 &slack_after); |
| 81 CHECK_GE(slack_after, slack_before); |
| 82 size_t slack = slack_after - slack_before; |
| 83 |
73 CHECK_GT(double(slack), 0.9*free_bytes); | 84 CHECK_GT(double(slack), 0.9*free_bytes); |
74 CHECK_LT(double(slack), 1.1*free_bytes); | 85 CHECK_LT(double(slack), 1.1*free_bytes); |
75 | 86 |
76 // Dump malloc stats | 87 // Dump malloc stats |
77 static const int kBufSize = 1<<20; | 88 static const int kBufSize = 1<<20; |
78 char* buffer = new char[kBufSize]; | 89 char* buffer = new char[kBufSize]; |
79 MallocExtension::instance()->GetStats(buffer, kBufSize); | 90 MallocExtension::instance()->GetStats(buffer, kBufSize); |
80 VLOG(1, "%s", buffer); | 91 VLOG(1, "%s", buffer); |
81 delete[] buffer; | 92 delete[] buffer; |
82 | 93 |
(...skipping 29 matching lines...) Expand all Loading... |
112 #else | 123 #else |
113 # error No way to calculate time on your system | 124 # error No way to calculate time on your system |
114 #endif | 125 #endif |
115 fprintf(stderr, "getproperty: %6.1f ns/call\n", | 126 fprintf(stderr, "getproperty: %6.1f ns/call\n", |
116 (sumsec * 1e9 + sumusec * 1e3) / kIterations); | 127 (sumsec * 1e9 + sumusec * 1e3) / kIterations); |
117 } | 128 } |
118 | 129 |
119 printf("PASS\n"); | 130 printf("PASS\n"); |
120 return 0; | 131 return 0; |
121 } | 132 } |
OLD | NEW |