Skip to content

Commit c64a20a

Browse files
committed
More.
1 parent a6b87c7 commit c64a20a

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

src/microbenchmark-pages-new.cpp

+45-26
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@
1616
#include <windows.h>
1717
#else
1818
#include <unistd.h>
19+
// For use by the replacement printf routines (see
20+
// https://github.com/mpaland/printf)
21+
extern "C" void _putchar(char ch) { ::write(1, (void *)&ch, 1); }
22+
1923
#endif
2024

25+
#include "printf.h"
26+
2127
#define PAGE_SIZE 4096
2228

2329
#ifndef OBJECT_DISTANCE
@@ -70,14 +76,17 @@ int litter(std::array<void*, MAX_OBJECTS>& toBeFreed,
7076
std::default_random_engine::result_type seed = std::random_device()(),
7177
std::size_t pageSize = PAGE_SIZE)
7278
{
79+
printf_("Littering begins.\n");
80+
7381
auto nAllocations = guess(objectSize, 0, 0, nPages, pageSize);
7482
std::array<void*, MAX_OBJECTS> allocated;
7583
int nAllocated = 0;
7684
int nFreed = 0;
7785
std::size_t PagesFilled = 0;
7886

7987
for (;;) {
80-
std::cout << "Allocating " << (nAllocations - nAllocated) << " objects..." << std::endl;
88+
printf_("allocating %d objects...\n", (nAllocations - nAllocated));
89+
// std::cout << "Allocating " << (nAllocations - nAllocated) << " objects..." << std::endl;
8190
while ((nAllocated < nAllocations) && (nAllocated < MAX_OBJECTS)) {
8291
auto ptr = std::malloc(objectSize);
8392
// std::cout << " allocated " << objectSize << " : " << (uintptr_t) ptr << std::endl;
@@ -102,6 +111,7 @@ int litter(std::array<void*, MAX_OBJECTS>& toBeFreed,
102111

103112
nAllocations = guess(objectSize, nAllocations, PagesFilled, nPages, pageSize);
104113
}
114+
printf_("Littering: done allocating.\n");
105115

106116
auto previous = reinterpret_cast<std::uintptr_t>(allocated[0]);
107117
for (std::size_t i = 1; i < nAllocated; ++i) {
@@ -110,13 +120,15 @@ int litter(std::array<void*, MAX_OBJECTS>& toBeFreed,
110120
previous = reinterpret_cast<std::uintptr_t>(allocated[i]);
111121
}
112122
}
113-
114-
// std::cout << "Freeing " << toBeFreed.size() << " objects..." << std::endl;
115-
std::shuffle(toBeFreed.begin(), toBeFreed.begin() + nFreed, std::default_random_engine(seed));
123+
printf_("Littering: freeing.\n");
124+
// std::cout << "Freeing " << toBeFreed.size() << " objects..." << std::endl;
125+
std::shuffle(toBeFreed.begin(), toBeFreed.begin() + nFreed, std::default_random_engine(seed));
116126
for (auto i = 0; i < nFreed; i++) {
117127
// std::cout << " freeing " << (uintptr_t) ptr << std::endl;
118128
std::free(toBeFreed[i]);
119129
}
130+
printf_("Littering: done freeing.\n");
131+
printf_("*** DONE LITTERING ***\n");
120132
return nFreed;
121133
}
122134

@@ -149,11 +161,13 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv)
149161
std::array<void*, N> objects;
150162
// objects.reserve(N);
151163

164+
printf_("allocating %d objects\n", N);
152165
for (std::size_t i = 0; i < N; ++i) {
153166
auto ptr = std::malloc(OBJECT_SIZE);
154167
// std::cout << "object " << OBJECT_SIZE << " pushing " << (uintptr_t) ptr << std::endl;
155168
objects[i] = ptr;
156169
}
170+
printf_("done allocating.\n");
157171

158172
std::sort(objects.begin(), objects.end());
159173

@@ -202,7 +216,8 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv)
202216
}
203217

204218
std::cout << "Intersection (objects): " << intersection << " / " << (objects.size()) << std::endl;
205-
std::cout << "Intersection (bytes): " << intersectionBytes << " / " << (OBJECT_SIZE * objects.size()) << std::endl;
219+
auto ratioBytes = (float) intersectionBytes / (float) (OBJECT_SIZE * objects.size());
220+
std::cout << "Intersection (bytes): " << intersectionBytes << " / " << (OBJECT_SIZE * objects.size()) << " (" << ratioBytes << ")" << std::endl;
206221

207222
const auto avgDistance = (double) sumDistances / (objects.size() - 1);
208223

@@ -215,40 +230,44 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv)
215230
std::cout << "Avg distance: " << avgDistance << std::endl;
216231

217232

233+
printf_("Starting benchmark.\n");
234+
218235
volatile unsigned long count = 0;
219-
auto start = std::chrono::high_resolution_clock::now();
236+
decltype(std::chrono::high_resolution_clock::now()) start, end;
220237

238+
start = std::chrono::high_resolution_clock::now();
239+
221240
for (std::size_t i = 0; i < ITERATIONS; i++) {
222-
for (const auto object : objects) {
223-
volatile char buffer[OBJECT_SIZE];
224-
std::memcpy((void*) buffer, object, OBJECT_SIZE);
225-
count += buffer[OBJECT_SIZE - 1];
226-
}
241+
for (const auto object : objects) {
242+
volatile char buffer[OBJECT_SIZE];
243+
std::memcpy((void*) buffer, object, OBJECT_SIZE);
244+
count += buffer[OBJECT_SIZE - 1];
245+
}
227246
}
228-
229-
auto end = std::chrono::high_resolution_clock::now();
247+
248+
end = std::chrono::high_resolution_clock::now();
230249
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
231-
std::cout << "Elapsed (littered): " << duration << "ms" << std::endl;
232-
250+
printf_("Elapsed (littered): %d ms\n", duration);
233251

234252
std::array<void*, N> newObjects;
253+
volatile char buf[N * OBJECT_SIZE];
235254
for (auto i = 0; i < N; i++) {
236-
newObjects[i] = std::malloc(OBJECT_SIZE);
255+
newObjects[i] = (void *) &buf[i * OBJECT_SIZE]; // std::malloc(OBJECT_SIZE);
237256
}
257+
printf_("Starting contiguous.\n");
238258

239259
start = std::chrono::high_resolution_clock::now();
240-
260+
241261
for (std::size_t i = 0; i < ITERATIONS; i++) {
242-
for (const auto object : newObjects) {
243-
volatile char buffer[OBJECT_SIZE];
244-
std::memcpy((void*) buffer, object, OBJECT_SIZE);
245-
count += buffer[OBJECT_SIZE - 1];
246-
}
262+
for (volatile const auto object : newObjects) {
263+
volatile char buffer[OBJECT_SIZE];
264+
std::memcpy((void*) buffer, object, OBJECT_SIZE);
265+
count += buffer[OBJECT_SIZE - 1];
266+
}
247267
}
248-
268+
249269
end = std::chrono::high_resolution_clock::now();
250270
auto durationContiguous = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
251-
std::cout << "Elapsed (contiguous): " << durationContiguous << "ms" << std::endl;
252-
253-
std::cout << "Ratio = " << (float) duration / (float) durationContiguous << std::endl;
271+
printf_("Elapsed (contiguous): %d ms\n", durationContiguous);
272+
printf_("Ratio = %f\n", (float) duration / (float) durationContiguous);
254273
}

0 commit comments

Comments
 (0)