16
16
#include < windows.h>
17
17
#else
18
18
#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
+
19
23
#endif
20
24
25
+ #include " printf.h"
26
+
21
27
#define PAGE_SIZE 4096
22
28
23
29
#ifndef OBJECT_DISTANCE
@@ -70,14 +76,17 @@ int litter(std::array<void*, MAX_OBJECTS>& toBeFreed,
70
76
std::default_random_engine::result_type seed = std::random_device()(),
71
77
std::size_t pageSize = PAGE_SIZE)
72
78
{
79
+ printf_ (" Littering begins.\n " );
80
+
73
81
auto nAllocations = guess (objectSize, 0 , 0 , nPages, pageSize);
74
82
std::array<void *, MAX_OBJECTS> allocated;
75
83
int nAllocated = 0 ;
76
84
int nFreed = 0 ;
77
85
std::size_t PagesFilled = 0 ;
78
86
79
87
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;
81
90
while ((nAllocated < nAllocations) && (nAllocated < MAX_OBJECTS)) {
82
91
auto ptr = std::malloc (objectSize);
83
92
// std::cout << " allocated " << objectSize << " : " << (uintptr_t) ptr << std::endl;
@@ -102,6 +111,7 @@ int litter(std::array<void*, MAX_OBJECTS>& toBeFreed,
102
111
103
112
nAllocations = guess (objectSize, nAllocations, PagesFilled, nPages, pageSize);
104
113
}
114
+ printf_ (" Littering: done allocating.\n " );
105
115
106
116
auto previous = reinterpret_cast <std::uintptr_t >(allocated[0 ]);
107
117
for (std::size_t i = 1 ; i < nAllocated; ++i) {
@@ -110,13 +120,15 @@ int litter(std::array<void*, MAX_OBJECTS>& toBeFreed,
110
120
previous = reinterpret_cast <std::uintptr_t >(allocated[i]);
111
121
}
112
122
}
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));
116
126
for (auto i = 0 ; i < nFreed; i++) {
117
127
// std::cout << " freeing " << (uintptr_t) ptr << std::endl;
118
128
std::free (toBeFreed[i]);
119
129
}
130
+ printf_ (" Littering: done freeing.\n " );
131
+ printf_ (" *** DONE LITTERING ***\n " );
120
132
return nFreed;
121
133
}
122
134
@@ -149,11 +161,13 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv)
149
161
std::array<void *, N> objects;
150
162
// objects.reserve(N);
151
163
164
+ printf_ (" allocating %d objects\n " , N);
152
165
for (std::size_t i = 0 ; i < N; ++i) {
153
166
auto ptr = std::malloc (OBJECT_SIZE);
154
167
// std::cout << "object " << OBJECT_SIZE << " pushing " << (uintptr_t) ptr << std::endl;
155
168
objects[i] = ptr;
156
169
}
170
+ printf_ (" done allocating.\n " );
157
171
158
172
std::sort (objects.begin (), objects.end ());
159
173
@@ -202,7 +216,8 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv)
202
216
}
203
217
204
218
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;
206
221
207
222
const auto avgDistance = (double ) sumDistances / (objects.size () - 1 );
208
223
@@ -215,40 +230,44 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv)
215
230
std::cout << " Avg distance: " << avgDistance << std::endl;
216
231
217
232
233
+ printf_ (" Starting benchmark.\n " );
234
+
218
235
volatile unsigned long count = 0 ;
219
- auto start = std::chrono::high_resolution_clock::now ();
236
+ decltype ( std::chrono::high_resolution_clock::now ()) start, end ;
220
237
238
+ start = std::chrono::high_resolution_clock::now ();
239
+
221
240
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
+ }
227
246
}
228
-
229
- auto end = std::chrono::high_resolution_clock::now ();
247
+
248
+ end = std::chrono::high_resolution_clock::now ();
230
249
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);
233
251
234
252
std::array<void *, N> newObjects;
253
+ volatile char buf[N * OBJECT_SIZE];
235
254
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);
237
256
}
257
+ printf_ (" Starting contiguous.\n " );
238
258
239
259
start = std::chrono::high_resolution_clock::now ();
240
-
260
+
241
261
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
+ }
247
267
}
248
-
268
+
249
269
end = std::chrono::high_resolution_clock::now ();
250
270
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);
254
273
}
0 commit comments