bb_meminfo.cpp

00001
00002 /***************************************************************************
00003  *  bb_meminfo.cpp - Fawkes BlackBoard memory info
00004  *
00005  *  Generated: Fri Oct 20 13:32:38 2006
00006  *  Copyright  2005-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.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022
00023 #include <blackboard/bbconfig.h>
00024 #include <blackboard/internal/memory_manager.h>
00025 #include <blackboard/internal/interface_mem_header.h>
00026 #include <blackboard/exceptions.h>
00027 #include <utils/system/console_colors.h>
00028 #include <utils/time/time.h>
00029 #include <config/sqlite.h>
00030
00031 #include <iostream>
00032
00033 using namespace std;
00034 using namespace fawkes;
00035
00036 int
00037 main(int argc, char **argv)
00038 {
00039   SQLiteConfiguration config(CONFDIR);
00040   config.load();
00041
00042   std::string token = "";
00043   try {
00044     token = config.get_string("/fawkes/mainapp/blackboard_magic_token");
00045   } catch (Exception &e) {
00046     cout << "Could not read shared memory token for blackboard." << endl;
00047     cout << "BlackBoard is probably running without shared memory." << endl;
00048     return -1;
00049   }
00050
00051   BlackBoardMemoryManager *memmgr;
00052   try {
00053     memmgr = new BlackBoardMemoryManager( config.get_uint("/fawkes/mainapp/blackboard_size"),
00054                                           BLACKBOARD_VERSION,
00055                                           /* master? */ false,
00056                                           token.c_str());
00057   } catch (BBMemMgrCannotOpenException &e) {
00058     cout << "No BlackBoard shared memory segment found!" << endl;
00059     return 1;
00060   }
00061
00062   cout << endl << cblue << "Fawkes BlackBoard Memory Info" << cnormal << endl
00063        << "========================================================================" << endl;
00064
00065   printf("Memory Size: %s%8u%s %sB%s  BlackBoard version: %s%u%s\n"
00066          "Free Memory: %s%8u%s %sB%s  Alloc. memory: %s%8u%s %sB%s  Overhang: %s%8u%s %sB%s\n"
00067          "Free Chunks: %s%8u%s    Alloc. chunks: %s%8u%s\n",
00068          cdarkgray.c_str(), memmgr->memory_size(), cnormal.c_str(),
00069          clightgray.c_str(), cnormal.c_str(),
00070          cdarkgray.c_str(), memmgr->version(), cnormal.c_str(),
00071          cdarkgray.c_str(), memmgr->free_size(), cnormal.c_str(),
00072          clightgray.c_str(), cnormal.c_str(),
00073          cdarkgray.c_str(), memmgr->allocated_size(), cnormal.c_str(),
00074          clightgray.c_str(), cnormal.c_str(),
00075          cdarkgray.c_str(), memmgr->overhang_size(), cnormal.c_str(),
00076          clightgray.c_str(), cnormal.c_str(),
00077          cdarkgray.c_str(), memmgr->num_free_chunks(), cnormal.c_str(),
00078          cdarkgray.c_str(), memmgr->num_allocated_chunks(), cnormal.c_str());
00079
00080   if ( ! memmgr->try_lock() ) {
00081     timeval a, b;
00082     gettimeofday(&a, NULL);
00083     cout << "Waiting for lock on shared memory.. " << flush;
00084     memmgr->lock();
00085     gettimeofday(&b, NULL);
00086     cout << "lock aquired. Waited " << time_diff_sec(b, a) << " seconds" << endl;
00087   }
00088
00089   if ( memmgr->begin() == memmgr->end() ) {
00090     cout << "No interfaces allocated." << endl;
00091   } else {
00092     cout << endl << "Interfaces:" << endl;
00093
00094     printf("%sMemSize  Overhang  Type/ID/Hash                       Serial  Ref  W/R%s\n"
00095            "------------------------------------------------------------------------\n",
00096            cdarkgray.c_str(), cnormal.c_str());
00097
00098     interface_header_t *ih;
00099     BlackBoardMemoryManager::ChunkIterator cit;
00100     for ( cit = memmgr->begin(); cit != memmgr->end(); ++cit ) {
00101       if ( *cit == NULL ) {
00102         cout << "*cit == NULL" << endl;
00103         break;
00104       } else {
00105         ih = (interface_header_t *)*cit;
00106         char tmp_hash[__INTERFACE_HASH_SIZE * 2 + 1];
00107         for (size_t s = 0; s < __INTERFACE_HASH_SIZE; ++s) {
00108           snprintf(&tmp_hash[s*2], 3, "%02X", ih->hash[s]);
00109         }
00110         printf("%7u  %8u  %sT%s %-32s %6u  %3u  %1d/%-3d\n%18s %sI%s %-32s\n%18s %sH%s %-32s\n",
00111                cit.size(), cit.overhang(), clightgray.c_str(), cnormal.c_str(), ih->type,
00112                ih->serial, ih->refcount, ih->flag_writer_active, ih->num_readers,
00113                "", clightgray.c_str(), cnormal.c_str(), ih->id,
00114                "", clightgray.c_str(), cnormal.c_str(), tmp_hash);
00115       }
00116     }
00117   }
00118
00119   memmgr->unlock();
00120
00121   delete memmgr;
00122   return 0;
00123 }