qa_bb_memmgr.cpp

00001 
00002 /***************************************************************************
00003  *  memory_manager.h - BlackBoard memory manager QA
00004  *
00005  *  Generated: Thu Oct 05 16:09:25 2006
00006  *  Copyright  2006  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 
00025 /// @cond QA
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   //  BlackBoardMemoryManager *mm = new BlackBoardMemoryManager( BLACKBOARD_MEMORY_SIZE,
00063   //                                                        BLACKBOARD_VERSION,
00064   //                                                        "FawkesBBMemMgrQA" /* token */ );
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       // alloc
00132       unsigned int s = (rand() % BLACKBOARD_MEMORY_SIZE) / 1000;
00133       if ( s < 20 ) {
00134         // min 20 bytes
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       // free
00153       if ( ptrs.size() > 0 ) {
00154         // there is something to delete
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       // sleep(10);
00189     }
00190     ++modcount;
00191     usleep(0);
00192   }
00193 
00194   delete mm;
00195 }
00196 
00197 
00198 /// @endcond