-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestUtilities.cpp
More file actions
323 lines (291 loc) · 11.4 KB
/
Copy pathTestUtilities.cpp
File metadata and controls
323 lines (291 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <memory>
#include <random>
#include <sstream>
#include <vector>
#include "StackTrace/Utilities.h"
using namespace StackTrace;
// NULL_USE function
#define NULL_USE( variable ) \
do { \
if ( 0 ) { \
auto static temp = (char *) &variable; \
temp++; \
} \
} while ( 0 )
// Include MPI
// clang-format off
#ifdef USE_MPI
#include "mpi.h"
int getRank() {
int rank = 0;
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
return rank;
}
int getSize() {
int size = 0;
MPI_Comm_size( MPI_COMM_WORLD, &size );
return size;
}
void barrier() { MPI_Barrier( MPI_COMM_WORLD ); }
int sumReduce( int x ) {
int y;
MPI_Allreduce( &x, &y, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
return y;
}
int startup( int argc, char *argv[] ) {
int provided_thread_support;
MPI_Init_thread( &argc, &argv, MPI_THREAD_MULTIPLE, &provided_thread_support );
return getRank();
}
void shutdown( ) {
MPI_Barrier( MPI_COMM_WORLD );
MPI_Finalize( );
}
#else
#define MPI_COMM_WORLD 0
void MPI_Finalize() {}
void barrier() {}
int sumReduce( int x ) { return x; }
int startup( int, char*[] ) { return 0; }
void shutdown( ) { }
int getRank() { return 0; }
int getSize() { return 1; }
#endif
// clang-format on
// Class to store pass/failures
class UnitTest
{
public:
void passes( const std::string &msg ) { passes_.push_back( msg ); }
void failure( const std::string &msg ) { failure_.push_back( msg ); }
void expected( const std::string &msg ) { expected_.push_back( msg ); }
void print() const
{
int rank = getRank();
if ( rank == 0 ) {
std::cout << "\nTests passed:" << std::endl;
for ( const auto &msg : passes_ )
std::cout << " " << msg << std::endl;
std::cout << std::endl << "Tests expected failed:" << std::endl;
printAll( expected_ );
std::cout << std::endl << "Tests failed:" << std::endl;
printAll( failure_ );
} else {
printAll( expected_ );
printAll( failure_ );
}
}
static void printAll( std::vector<std::string> messages )
{
int rank = getRank();
int size = getSize();
for ( int i = 0; i < size; i++ ) {
if ( rank == i )
for ( const auto &msg : messages )
std::cout << " Rank " << rank << ": " << msg << std::endl;
barrier();
}
}
int N_failed() const { return sumReduce( failure_.size() ); }
void clear()
{
passes_ = std::vector<std::string>();
failure_ = std::vector<std::string>();
expected_ = std::vector<std::string>();
}
private:
std::vector<std::string> passes_;
std::vector<std::string> failure_;
std::vector<std::string> expected_;
};
// Subract two size_t numbers, returning the absolute value
size_t abs_diff( size_t a, size_t b ) { return ( a >= b ) ? a - b : b - a; }
// Zero and check memory block
void fill( void *x, uint8_t value, size_t bytes )
{
memset( x, value, bytes );
auto y = reinterpret_cast<uint8_t *>( x );
bool pass = true;
for ( size_t i = 0; i < value; i++ )
pass = pass && y[i] == value;
if ( !pass )
throw std::logic_error( "Failed to set memory" );
}
// Test source_location::current
void test_source_location( UnitTest &ut,
const source_location &source = source_location::current() )
{
std::cout << "Testing StackTrace::source_location::current:\n";
auto source2 = SOURCE_LOCATION_CURRENT();
std::cout << " file: " << source.file_name() << "(" << source.line() << ":" << source.column()
<< ") " << source.function_name() << '\n';
std::cout << " file: " << source2.file_name() << "(" << source2.line() << ":"
<< source2.column() << ") " << source2.function_name() << '\n';
bool test1 = std::string( source.function_name() ).find( "main" ) != std::string::npos;
bool test2 =
std::string( source2.function_name() ).find( "test_source_location" ) != std::string::npos;
if ( test1 && test2 )
ut.passes( "source_location::current()" );
else if ( test2 && source.empty() )
ut.expected( "source_location::current()" );
else
ut.failure( "source_location::current()" );
}
/****************************************************************
* Run some basic utility tests *
****************************************************************/
int main( int argc, char *argv[] )
{
int rank = startup( argc, argv );
// Limit the scope of variables
int num_failed = 0;
{
UnitTest ut;
// Check the OS
constexpr auto OS = Utilities::getOS();
if ( OS == Utilities::OS::Linux )
ut.passes( "OS: Linux" );
else if ( OS == Utilities::OS::Windows )
ut.passes( "OS: Windows" );
else if ( OS == Utilities::OS::macOS )
ut.passes( "OS: macOS" );
else
ut.failure( "Known OS" );
// Test source_location
test_source_location( ut );
// Test getSystemMemory
size_t system_bytes = Utilities::getSystemMemory();
std::cout << "Total system bytes = " << system_bytes << std::endl;
if ( system_bytes > 0 )
ut.passes( "getSystemMemory" );
else
ut.failure( "getSystemMemory" );
// Test the memory usage
double t0 = Utilities::time();
size_t n_bytes1 = Utilities::getMemoryUsage();
double time1 = Utilities::time() - t0;
auto *tmp = new uint64_t[0x100000];
fill( tmp, 0xAA, 0x100000 * sizeof( uint64_t ) );
t0 = Utilities::time();
size_t n_bytes2 = Utilities::getMemoryUsage();
double time2 = Utilities::time() - t0;
delete[] tmp;
t0 = Utilities::time();
size_t n_bytes3 = Utilities::getMemoryUsage();
double time3 = Utilities::time() - t0;
if ( rank == 0 ) {
std::cout << "Number of bytes used for a basic test: " << n_bytes1 << ", " << n_bytes2
<< ", " << n_bytes3 << std::endl;
std::cout << " Time to query: " << time1 * 1e6 << " us, " << time2 * 1e6 << " us, "
<< time3 * 1e6 << " us" << std::endl;
}
if ( n_bytes1 == 0 ) {
ut.failure( "getMemoryUsage returns 0" );
} else {
ut.passes( "getMemoryUsage returns non-zero" );
if ( n_bytes2 > n_bytes1 ) {
ut.passes( "getMemoryUsage increases size" );
} else if ( OS == Utilities::OS::macOS ) {
ut.expected( "getMemoryUsage does not increase size" );
} else {
ut.failure( "getMemoryUsage increases size" );
}
if ( n_bytes1 == n_bytes3 ) {
ut.passes( "getMemoryUsage decreases size properly" );
} else if ( OS != Utilities::OS::Linux ) {
ut.expected( "getMemoryUsage does not decrease size properly" );
} else {
ut.failure( "getMemoryUsage does not decrease size properly" );
}
}
// Run large memory test of getMemoryUsage
if ( system_bytes >= 4e9 && rank == 0 ) {
// Test getting the memory usage for 2-4 GB bytes
// Note: we only run this test on machines with more than 4 GB of memory
n_bytes1 = Utilities::getMemoryUsage();
auto *tmp2 = new uint64_t[0x10000001]; // Allocate 2^31+8 bytes
fill( tmp2, 0xAA, 0x10000001 * sizeof( uint64_t ) );
n_bytes2 = Utilities::getMemoryUsage();
for ( int i = 0; i < 10; i++ ) {
if ( ( tmp2[rand() % 0x1000000] & 0xFF ) != 0xAA )
ut.failure( "Internal error" );
}
delete[] tmp2;
tmp2 = nullptr;
n_bytes3 = Utilities::getMemoryUsage();
if ( n_bytes2 > 0x80000000 && n_bytes2 < n_bytes1 + 0x81000000 &&
abs_diff( n_bytes1, n_bytes3 ) < 50e3 ) {
ut.passes( "getMemoryUsage correctly handles 2^31 - 2^32 bytes" );
} else {
std::cout << "Memtest 2-4 GB failes: " << n_bytes1 << " " << n_bytes2 << " "
<< n_bytes3 << std::endl;
ut.failure( "getMemoryUsage correctly handles 2^31 - 2^32 bytes" );
}
}
if ( system_bytes >= 8e9 && rank == 0 ) {
// Test getting the memory usage for > 4 GB bytes
// Note: we only run this test on machines with more than 8 GB of memory
n_bytes1 = Utilities::getMemoryUsage();
size_t size = 0x20000000;
auto *tmp2 = new uint64_t[size]; // Allocate 2^31+8 bytes
if ( tmp2 == nullptr ) {
ut.expected( "Unable to allocate variable of size 4 GB" );
} else {
fill( tmp2, 0xAA, size * sizeof( uint64_t ) );
n_bytes2 = Utilities::getMemoryUsage();
for ( int i = 0; i < 10; i++ ) {
if ( ( tmp2[rand() % size] & 0xFF ) != 0xAA )
ut.failure( "Internal error" );
}
delete[] tmp2;
tmp2 = nullptr;
NULL_USE( tmp2 );
n_bytes3 = Utilities::getMemoryUsage();
if ( n_bytes2 > 0x100000000 && n_bytes2 < n_bytes1 + 0x110000000 &&
abs_diff( n_bytes1, n_bytes3 ) < 50e3 ) {
ut.passes( "getMemoryUsage correctly handles memory > 2^32 bytes" );
} else {
std::cout << "Memtest >4 GB fails: " << n_bytes1 << " " << n_bytes2 << " "
<< n_bytes3 << std::endl;
ut.expected( "getMemoryUsage does not handle memory > 2^32 bytes" );
}
}
}
// Test getting the executable
std::string exe = StackTrace::getExecutable();
if ( rank == 0 )
std::cout << "Executable: " << exe << std::endl;
if ( exe.find( "TestUtilities" ) != std::string::npos )
ut.passes( "getExecutable" );
else
ut.failure( "getExecutable" );
// Test catching an error
try {
Utilities::abort( "test_error", SOURCE_LOCATION_CURRENT() );
ut.failure( "Failed to catch error" );
} catch ( const StackTrace::abort_error &err ) {
if ( err.message == "test_error" )
ut.passes( "Caught error" );
else
ut.failure( "Failed to catch error with proper message" );
} catch ( std::exception &err ) {
ut.failure( "Caught unknown exception type" );
}
// Finished testing, report the results
ut.print();
num_failed = ut.N_failed();
}
// Shutdown
shutdown();
// Finished successfully
return num_failed;
}