qa_bb_memmgr.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <blackboard/internal/memory_manager.h>
00028 #include <blackboard/exceptions.h>
00029 #include <blackboard/bbconfig.h>
00030
00031 #include <core/exceptions/system.h>
00032
00033 #include <signal.h>
00034 #include <cstdlib>
00035
00036 #include <iostream>
00037 #include <vector>
00038
00039 using namespace std;
00040 using namespace fawkes;
00041
00042
00043 bool quit = false;
00044
00045 void
00046 signal_handler(int signum)
00047 {
00048 quit = true;
00049 }
00050
00051
00052 #define NUM_CHUNKS 5
00053 #define BLACKBOARD_MEMORY_SIZE 2 * 1024 * 1024
00054
00055 int
00056 main(int argc, char **argv)
00057 {
00058
00059 signal(SIGINT, signal_handler);
00060
00061
00062
00063
00064 BlackBoardMemoryManager *mm = new BlackBoardMemoryManager(BLACKBOARD_MEMORY_SIZE);
00065
00066 void *m[NUM_CHUNKS];
00067
00068 cout << "Running basic tests" << endl;
00069 cout << "=========================================================================" << endl;
00070
00071 mm->print_performance_info();
00072
00073 unsigned int free_before = mm->max_free_size();
00074
00075 for (unsigned int i = 0; i < NUM_CHUNKS; ++i) {
00076 cout << "Allocating m[" << i << "] with " << (i+1) * 1000 << " bytes.." << flush;
00077 m[i] = mm->alloc( (i+1) * 1000 );
00078 cout << "done" << endl;
00079 }
00080
00081 if ( mm->max_allocated_size() != (NUM_CHUNKS * 1000) ) {
00082 cout << "Largest chunk is not " << NUM_CHUNKS * 1000 << " bytes, error, aborting" << endl;
00083 delete mm;
00084 exit(1);
00085 }
00086
00087 cout << "Free chunks:" << endl;
00088 mm->print_free_chunks_info();
00089 cout << "Allocated chunks:" << endl;
00090 mm->print_allocated_chunks_info();
00091 mm->print_performance_info();
00092
00093 for (unsigned int i = 0; i < NUM_CHUNKS; ++i) {
00094 cout << "Freeing m[" << i << "].." << flush;
00095 mm->free( m[i] );
00096 cout << "done" << endl;
00097 }
00098
00099 if ( mm->max_allocated_size() != 0 ) {
00100 cout << "Largest chunk is not 0 bytes, error, aborting" << endl;
00101 delete mm;
00102 exit(2);
00103 }
00104
00105 if ( mm->max_free_size() != free_before ) {
00106 cout << "Max free size after tests differe from before test, error, aborting" << endl;
00107 delete mm;
00108 exit(3);
00109 }
00110
00111 cout << "Free chunks:" << endl;
00112 mm->print_free_chunks_info();
00113 cout << "Allocated chunks:" << endl;
00114 mm->print_allocated_chunks_info();
00115 mm->print_performance_info();
00116
00117 cout << "Basic tests finished" << endl;
00118 cout << "=========================================================================" << endl;
00119
00120 cout << endl << "Running gremlin tests, press Ctrl-C to stop" << endl;
00121 cout << "=========================================================================" << endl;
00122
00123 std::vector< void * > ptrs;
00124 ptrs.clear();
00125
00126 unsigned int modcount = 0;
00127 while ( ! quit ) {
00128 if (rand() < RAND_MAX / 2) {
00129 cout << "a" << flush;
00130
00131 unsigned int s = (rand() % BLACKBOARD_MEMORY_SIZE) / 1000;
00132 if ( s < 20 ) {
00133
00134 s = 20;
00135 }
00136 void *m;
00137 try {
00138 m = mm->alloc(s);
00139 ptrs.push_back( m );
00140 } catch ( OutOfMemoryException &e ) {
00141 cout << "Memory Manager ran out of memory, tried to allocate "
00142 << s << " bytes, detailed info:" << endl;
00143 cout << "Free chunks:" << endl;
00144 mm->print_free_chunks_info();
00145 cout << "Allocated chunks:" << endl;
00146 mm->print_allocated_chunks_info();
00147 mm->print_performance_info();
00148 }
00149 } else {
00150 cout << "f" << flush;
00151
00152 if ( ptrs.size() > 0 ) {
00153
00154 unsigned int erase = rand() % ptrs.size();
00155 try {
00156 mm->free( ptrs[erase] );
00157 ptrs.erase( ptrs.begin() + erase );
00158 } catch ( BlackBoardMemMgrInvalidPointerException &e ) {
00159 cout << "Ouch, tried to free invalid pointer" << endl;
00160 cout << "Allocated chunks:" << endl;
00161 mm->print_allocated_chunks_info();
00162 printf("Pointer tried to free: 0x%lx\n", (long unsigned int)ptrs[erase]);
00163 }
00164 }
00165 }
00166
00167 try {
00168 mm->check();
00169 } catch ( BBInconsistentMemoryException &e ) {
00170 cout << "Inconsistent memory found, printing exception trace" << endl;
00171 e.print_trace();
00172 cout << "Free chunks:" << endl;
00173 mm->print_free_chunks_info();
00174 cout << "Allocated chunks:" << endl;
00175 mm->print_allocated_chunks_info();
00176 mm->print_performance_info();
00177 quit = true;
00178 }
00179
00180 if ( modcount % 10 == 0 ) {
00181 cout << endl;
00182 mm->print_performance_info();
00183 if ( mm->overhang_size() > 0 ) {
00184 cout << "Overhang detected, allocated chunks:" << endl;
00185 mm->print_allocated_chunks_info();
00186 }
00187
00188 }
00189 ++modcount;
00190 usleep(0);
00191 }
00192
00193 delete mm;
00194 }
00195
00196
00197