Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
bb_meminfo.cpp
1 
2 /***************************************************************************
3  * bb_meminfo.cpp - Fawkes BlackBoard memory info
4  *
5  * Generated: Fri Oct 20 13:32:38 2006
6  * Copyright 2005-2006 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include <blackboard/bbconfig.h>
24 #include <blackboard/internal/memory_manager.h>
25 #include <blackboard/internal/interface_mem_header.h>
26 #include <blackboard/exceptions.h>
27 #include <utils/system/console_colors.h>
28 #include <utils/time/time.h>
29 #include <config/sqlite.h>
30 
31 #include <iostream>
32 #include <cstdio>
33 
34 using namespace std;
35 using namespace fawkes;
36 
37 int
38 main(int argc, char **argv)
39 {
40  SQLiteConfiguration config(CONFDIR);
41  config.load();
42 
43  std::string token = "";
44  try {
45  token = config.get_string("/fawkes/mainapp/blackboard_magic_token");
46  } catch (Exception &e) {
47  cout << "Could not read shared memory token for blackboard." << endl;
48  cout << "BlackBoard is probably running without shared memory." << endl;
49  return -1;
50  }
51 
53  try {
54  memmgr = new BlackBoardMemoryManager( config.get_uint("/fawkes/mainapp/blackboard_size"),
55  BLACKBOARD_VERSION,
56  /* master? */ false,
57  token.c_str());
58  } catch (BBMemMgrCannotOpenException &e) {
59  cout << "No BlackBoard shared memory segment found!" << endl;
60  return 1;
61  }
62 
63  cout << endl << cblue << "Fawkes BlackBoard Memory Info" << cnormal << endl
64  << "========================================================================" << endl;
65 
66  printf("Memory Size: %s%8u%s %sB%s BlackBoard version: %s%u%s\n"
67  "Free Memory: %s%8u%s %sB%s Alloc. memory: %s%8u%s %sB%s Overhang: %s%8u%s %sB%s\n"
68  "Free Chunks: %s%8u%s Alloc. chunks: %s%8u%s\n",
69  cdarkgray.c_str(), memmgr->memory_size(), cnormal.c_str(),
70  clightgray.c_str(), cnormal.c_str(),
71  cdarkgray.c_str(), memmgr->version(), cnormal.c_str(),
72  cdarkgray.c_str(), memmgr->free_size(), cnormal.c_str(),
73  clightgray.c_str(), cnormal.c_str(),
74  cdarkgray.c_str(), memmgr->allocated_size(), cnormal.c_str(),
75  clightgray.c_str(), cnormal.c_str(),
76  cdarkgray.c_str(), memmgr->overhang_size(), cnormal.c_str(),
77  clightgray.c_str(), cnormal.c_str(),
78  cdarkgray.c_str(), memmgr->num_free_chunks(), cnormal.c_str(),
79  cdarkgray.c_str(), memmgr->num_allocated_chunks(), cnormal.c_str());
80 
81  if ( ! memmgr->try_lock() ) {
82  timeval a, b;
83  gettimeofday(&a, NULL);
84  cout << "Waiting for lock on shared memory.. " << flush;
85  memmgr->lock();
86  gettimeofday(&b, NULL);
87  cout << "lock aquired. Waited " << time_diff_sec(b, a) << " seconds" << endl;
88  }
89 
90  if ( memmgr->begin() == memmgr->end() ) {
91  cout << "No interfaces allocated." << endl;
92  } else {
93  cout << endl << "Interfaces:" << endl;
94 
95  printf("%sMemSize Overhang Type/ID/Hash Serial Ref W/R%s\n"
96  "------------------------------------------------------------------------\n",
97  cdarkgray.c_str(), cnormal.c_str());
98 
101  for ( cit = memmgr->begin(); cit != memmgr->end(); ++cit ) {
102  if ( *cit == NULL ) {
103  cout << "*cit == NULL" << endl;
104  break;
105  } else {
106  ih = (interface_header_t *)*cit;
107  char tmp_hash[__INTERFACE_HASH_SIZE * 2 + 1];
108  for (size_t s = 0; s < __INTERFACE_HASH_SIZE; ++s) {
109  snprintf(&tmp_hash[s*2], 3, "%02X", ih->hash[s]);
110  }
111  printf("%7u %8u %sT%s %-32s %6u %3u %1d/%-3d\n%18s %sI%s %-32s\n%18s %sH%s %-32s\n",
112  cit.size(), cit.overhang(), clightgray.c_str(), cnormal.c_str(), ih->type,
113  ih->serial, ih->refcount, ih->flag_writer_active, ih->num_readers,
114  "", clightgray.c_str(), cnormal.c_str(), ih->id,
115  "", clightgray.c_str(), cnormal.c_str(), tmp_hash);
116  }
117  }
118  }
119 
120  memmgr->unlock();
121 
122  delete memmgr;
123  return 0;
124 }
virtual std::string get_string(const char *path)
Get value from configuration which is of type string.
Definition: sqlite.cpp:1164
ChunkIterator begin()
Get first element for chunk iteration.
unsigned int overhang_size() const
Get number of overhanging bytes.
Configuration storage using SQLite.
Definition: sqlite.h:39
ChunkIterator end()
Get end of chunk list.
unsigned int size() const
Get size of data segment.
unsigned int overhang() const
Get number of overhanging bytes.
BlackBoard memory manager.
virtual unsigned int get_uint(const char *path)
Get value from configuration which is of type unsigned int.
Definition: sqlite.cpp:1104
unsigned int num_allocated_chunks() const
Get number of allocated chunks.
bool try_lock()
Try to lock memory.
virtual void load(const char *filename, const char *defaults_filename, const char *tag=NULL)
Load configuration.
Definition: sqlite.cpp:611
This struct is used as header for interfaces in memory chunks.
Base class for exceptions in Fawkes.
Definition: exception.h:36
Thrown if shared memory could not be opened.
Definition: exceptions.h:82
double time_diff_sec(const timeval &a, const timeval &b)
Calculate time difference of two time structs.
Definition: time.h:40
unsigned int memory_size() const
Get size of memory.
unsigned int version() const
Get BlackBoard version.
unsigned int allocated_size() const
Get total allocated memory.
unsigned int free_size() const
Get total free memory.
unsigned int num_free_chunks() const
Get number of free chunks.