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