Fawkes API  Fawkes Development Version
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/exceptions.h>
25 #include <blackboard/internal/interface_mem_header.h>
26 #include <blackboard/internal/memory_manager.h>
27 #include <config/sqlite.h>
28 #include <utils/system/console_colors.h>
29 #include <utils/time/time.h>
30 
31 #include <cstdio>
32 #include <iostream>
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
64  << cblue << "Fawkes BlackBoard Memory Info" << cnormal << endl
65  << "========================================================================" << endl;
66 
67  printf("Memory Size: %s%8u%s %sB%s BlackBoard version: %s%u%s\n"
68  "Free Memory: %s%8u%s %sB%s Alloc. memory: %s%8u%s %sB%s Overhang: %s%8u%s %sB%s\n"
69  "Free Chunks: %s%8u%s Alloc. chunks: %s%8u%s\n",
70  cdarkgray.c_str(),
71  memmgr->memory_size(),
72  cnormal.c_str(),
73  clightgray.c_str(),
74  cnormal.c_str(),
75  cdarkgray.c_str(),
76  memmgr->version(),
77  cnormal.c_str(),
78  cdarkgray.c_str(),
79  memmgr->free_size(),
80  cnormal.c_str(),
81  clightgray.c_str(),
82  cnormal.c_str(),
83  cdarkgray.c_str(),
84  memmgr->allocated_size(),
85  cnormal.c_str(),
86  clightgray.c_str(),
87  cnormal.c_str(),
88  cdarkgray.c_str(),
89  memmgr->overhang_size(),
90  cnormal.c_str(),
91  clightgray.c_str(),
92  cnormal.c_str(),
93  cdarkgray.c_str(),
94  memmgr->num_free_chunks(),
95  cnormal.c_str(),
96  cdarkgray.c_str(),
97  memmgr->num_allocated_chunks(),
98  cnormal.c_str());
99 
100  if (!memmgr->try_lock()) {
101  timeval a, b;
102  gettimeofday(&a, NULL);
103  cout << "Waiting for lock on shared memory.. " << flush;
104  memmgr->lock();
105  gettimeofday(&b, NULL);
106  cout << "lock aquired. Waited " << time_diff_sec(b, a) << " seconds" << endl;
107  }
108 
109  if (memmgr->begin() == memmgr->end()) {
110  cout << "No interfaces allocated." << endl;
111  } else {
112  cout << endl << "Interfaces:" << endl;
113 
114  printf("%sMemSize Overhang Type/ID/Hash Serial Ref W/R%s\n"
115  "------------------------------------------------------------------------\n",
116  cdarkgray.c_str(),
117  cnormal.c_str());
118 
119  interface_header_t * ih;
121  for (cit = memmgr->begin(); cit != memmgr->end(); ++cit) {
122  if (*cit == NULL) {
123  cout << "*cit == NULL" << endl;
124  break;
125  } else {
126  ih = (interface_header_t *)*cit;
127  char tmp_hash[INTERFACE_HASH_SIZE_ * 2 + 1];
128  for (size_t s = 0; s < INTERFACE_HASH_SIZE_; ++s) {
129  snprintf(&tmp_hash[s * 2], 3, "%02X", ih->hash[s]);
130  }
131  printf("%7u %8u %sT%s %-32s %6u %3u %1d/%-3d\n%18s %sI%s %-32s\n%18s %sH%s %-32s\n",
132  cit.size(),
133  cit.overhang(),
134  clightgray.c_str(),
135  cnormal.c_str(),
136  ih->type,
137  ih->serial,
138  ih->refcount,
139  ih->flag_writer_active,
140  ih->num_readers,
141  "",
142  clightgray.c_str(),
143  cnormal.c_str(),
144  ih->id,
145  "",
146  clightgray.c_str(),
147  cnormal.c_str(),
148  tmp_hash);
149  }
150  }
151  }
152 
153  memmgr->unlock();
154 
155  delete memmgr;
156  return 0;
157 }
unsigned int size() const
Get size of data segment.
ChunkIterator begin()
Get first element for chunk iteration.
virtual void load(const char *file_path)=0
Load configuration.
unsigned int allocated_size() const
Get total allocated memory.
Configuration storage using SQLite.
Definition: sqlite.h:40
Fawkes library namespace.
ChunkIterator end()
Get end of chunk list.
BlackBoard memory manager.
unsigned int memory_size() const
Get size of memory.
bool try_lock()
Try to lock memory.
unsigned int version() const
Get BlackBoard version.
unsigned int num_free_chunks() const
Get number of free chunks.
This struct is used as header for interfaces in memory chunks.
Base class for exceptions in Fawkes.
Definition: exception.h:35
Thrown if shared memory could not be opened.
Definition: exceptions.h:83
double time_diff_sec(const timeval &a, const timeval &b)
Calculate time difference of two time structs.
Definition: time.h:41
unsigned int free_size() const
Get total free memory.
unsigned int overhang() const
Get number of overhanging bytes.
virtual unsigned int get_uint(const char *path)=0
Get value from configuration which is of type unsigned int.
unsigned int num_allocated_chunks() const
Get number of allocated chunks.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
unsigned int overhang_size() const
Get number of overhanging bytes.