1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
/*
This file is part of the Boson game
Copyright (C) 2004 Andreas Beckermann (b_mann@gmx.de)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "bomemorydialog.h"
#include "bomemorydialog.moc"
#include <config.h>
#include "memnode.h"
#include "memorymanager.h"
#include <bodebug.h>
#include <klocale.h>
#include <klistview.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qptrdict.h>
#include <qptrlist.h>
#include <qdict.h>
class MyMemNode {
public:
MyMemNode()
{
mSize = 0;
mPointer = 0;
mLine = -1;
mFile = 0;
mFunction = 0;
mIsMalloc = false;
}
~MyMemNode()
{
delete[] mFile;
delete[] mFunction;
}
static bool checkNode(MemNode* node)
{
// AB: sometimes there are invalid pointers in our version of
// new() in the strings. i have no idea why and where they come
// from - every // function (including e.g. strlen()) is
// crashing on these pointers.
// they don't occur _every_ time, but very often.
if (((unsigned int)node->mFile) < 0xFF) {
boError() << k_funcinfo << "invalid mFile string pointer:" << (void*)node->mFile << " won't use this node." << endl;
return false;
}
if (((unsigned int)node->mFunction) < 0xFF) {
boError() << k_funcinfo << "invalid mFunction string pointer:" << (void*)node->mFile << " won't use this node." << endl;
return false;
}
if (node->mLine < -1) {
boError() << k_funcinfo << "invalid mLine number:" << node->mLine << " won't use this node." << endl;
}
return true;
}
bool loadNode(MemNode* node)
{
if (!checkNode(node)) {
boError() << k_funcinfo << "something weird happened" << endl;
mFile = new char[1];
mFile[0] = '\0';
mFunction = new char[1];
mFunction[0] = '\0';
mIsMalloc = false;
return false;
}
mSize = node->mSize;
mPointer = node->mPointer;
mLine = node->mLine;
int len;
len = strlen(node->mFile) + 1;
mFile = new char[len];
strncpy(mFile, node->mFile, len);
mFile[len - 1] = '\0';
len = strlen(node->mFunction) + 1;
mFunction = new char[len];
strncpy(mFunction, node->mFunction, len);
mFunction[len - 1] = '\0';
mIsMalloc = node->mIsMalloc;
return true;
}
size_t mSize;
void* mPointer;
int mLine;
char* mFile;
char* mFunction;
bool mIsMalloc;
};
class BoMemoryDialogPrivate
{
public:
BoMemoryDialogPrivate()
{
mNodeCount = 0;
mMemory = 0;
mList = 0;
}
QLabel* mNodeCount;
QLabel* mMemory;
KListView* mList;
};
BoMemoryDialog::BoMemoryDialog(QWidget* parent, bool modal)
: KDialogBase(Plain, i18n("Boson Memory Usage"), Ok,
Ok, parent, "bomemorydialog", modal, true)
{
d = new BoMemoryDialogPrivate;
QVBoxLayout* layout = new QVBoxLayout(plainPage());
d->mNodeCount = new QLabel(plainPage());
layout->addWidget(d->mNodeCount);
d->mMemory = new QLabel(plainPage());
layout->addWidget(d->mMemory);
d->mList = new KListView(plainPage());
d->mList->setAllColumnsShowFocus(true);
d->mList->setRootIsDecorated(true);
layout->addWidget(d->mList);
d->mList->addColumn(i18n("File"));
d->mList->addColumn(i18n("Function"), 100);
d->mList->addColumn(i18n("Line"));
d->mList->addColumn(i18n("Size (Byte)"));
d->mList->addColumn(i18n("Size (KB)"));
d->mList->addColumn(i18n("Size (MB)"));
d->mList->addColumn(i18n("malloc calls"));
d->mList->addColumn(i18n("new calls"));
}
BoMemoryDialog::~BoMemoryDialog()
{
boDebug() << k_funcinfo << endl;
delete d;
}
void BoMemoryDialog::slotUpdate()
{
boDebug() << k_funcinfo << endl;
d->mList->clear();
MemoryManager::createManager();
if (!MemoryManager::manager()) {
boError() << k_funcinfo << "NULL memory manager. probably permanently disabled. cannot show any data" << endl;
return;
}
MemoryManager::manager()->disable(); // do not manage the ptrdict
unsigned int nodeCount = MemoryManager::manager()->allNodes().count();
unsigned int totalMemory = MemoryManager::manager()->memoryInUse();
QPtrDict<MemNode>* _allNodes = new QPtrDict<MemNode>(MemoryManager::manager()->allNodes());
MemoryManager::manager()->enable();
// make sure all nodes are valid and they _stay_ valid (we can't know if Qt
// or something else will delete anything while we are still working on these
// data)
QPtrDict<MyMemNode> allNodes(_allNodes->size());
allNodes.setAutoDelete(true);
{ // start a new scope to make sure _allNodesIt gets destructed
QPtrDictIterator<MemNode> _allNodesIt(*_allNodes);
while (_allNodesIt.current()) {
MemNode* n = _allNodesIt.current();
void* key = _allNodesIt.currentKey();
++_allNodesIt;
if (!MyMemNode::checkNode(n)) {
continue;
}
MyMemNode* node = new MyMemNode();
if (!node->loadNode(n)) {
delete node;
}
allNodes.insert(key, node);
}
}
MemoryManager::manager()->disable();
_allNodes->clear();
delete _allNodes;
_allNodes = 0;
MemoryManager::manager()->enable();
d->mNodeCount->setText(i18n("Total nodes: %1").arg(nodeCount));
d->mMemory->setText(i18n("Memory in use (Bytes/KB/MB): %1 / %2 / %3").arg(totalMemory).
arg( ((double)totalMemory) / (1024.0)).
arg( ((double)totalMemory) / (1024.0 * 1024.0) ));
boDebug() << k_funcinfo << "processing " << allNodes.count() << " nodes" << endl;
unsigned long int mmemory = 0;
unsigned long int nmemory = 0;
// AB: a ptr dict is pretty useless to us, we need something else
QPtrDictIterator<MyMemNode> dictIt(allNodes);
QDict<QPtrList<MyMemNode> > file2NodeList;
file2NodeList.setAutoDelete(true);
while (dictIt.current()) {
MyMemNode* node = dictIt.current();
++dictIt;
if (node->mIsMalloc) {
mmemory += node->mSize;
} else {
nmemory += node->mSize;
}
if (!node->mFile) {
boError() <<k_funcinfo << "NULL file string" << endl;
continue;
}
// AB: sometimes there are invalid pointers in our version of new() in
// the strings. i have no idea why and where they come from - every
// function (including e.g. strlen()) is crashing on these pointers.
// they don't occur _every_ time, but very often.
if (((unsigned int)node->mFile) < 0xFF) {
boError() << k_funcinfo << "invalid mFile string pointer:" << (void*)node->mFile << " won't use this node." << endl;
continue;
}
if (((unsigned int)node->mFunction) < 0xFF) {
boError() << k_funcinfo << "invalid mFunction string pointer:" << (void*)node->mFile << " won't use this node." << endl;
continue;
}
QString file = QString(node->mFile);
QPtrList<MyMemNode>* list = file2NodeList[file];
if (!list) {
list = new QPtrList<MyMemNode>;
file2NodeList.insert(file, list);
}
list->append(node);
}
boWarning() << k_funcinfo << "malloc'ed memory: " << mmemory << endl;
boWarning() << k_funcinfo << "new'ed memory: " << nmemory << endl;
unsigned long int size = 0;
QDictIterator<QPtrList<MyMemNode> > nodeIt(file2NodeList);
while (nodeIt.current()) {
QString fileName = nodeIt.currentKey();
QPtrList<MyMemNode>* list = nodeIt.current();
++nodeIt;
QListViewItem* file = createFileItem(fileName);
// boDebug() << k_funcinfo << "creating items for file " << fileName << endl;
size += createFileList(file, list);
}
boDebug() << k_funcinfo << "total size: " << size << endl;
allNodes.clear();
}
QListViewItem* BoMemoryDialog::createFileItem(const QString& file) const
{
QListViewItem* item = new QListViewItem(d->mList, file);
item->setOpen(false);
return item;
}
QListViewItem* BoMemoryDialog::createFunctionItem(QListViewItem* parent, const QString& function) const
{
QListViewItem* item = new QListViewItem(parent, "", function);
item->setOpen(false);
return item;
}
QListViewItem* BoMemoryDialog::createMemoryItem(QListViewItem* parent, const MyMemNode* node) const
{
if (!node) {
return 0;
}
QString line = QString::number(node->mLine);
QListViewItem* item = new QListViewItem(parent, node->mFile, node->mFunction, line);
setSize(item, node->mSize);
unsigned int mallocCalls = node->mIsMalloc? 1 : 0;
unsigned int newCalls = node->mIsMalloc? 0 : 1;
QString is = (node->mIsMalloc) ? QString("malloc") : QString("new");
item->setText(6, QString::number(mallocCalls));
item->setText(7, QString::number(newCalls));
return item;
}
void BoMemoryDialog::setSize(QListViewItem* item, unsigned long int size_) const
{
QString size;
QString sizeK;
QString sizeM;
size.sprintf("%07d", (int)size_);
sizeK.sprintf("%07f", ((double)size_) / (1024.0));
sizeM.sprintf("%07f", ((double)size_) / (1024.0 * 1024.0));
item->setText(3, size);
item->setText(4, sizeK);
item->setText(5, sizeM);
}
unsigned long int BoMemoryDialog::createFileList(QListViewItem* file, const QPtrList<MyMemNode>* list_)
{
if (!list_ || !file) {
return 0;
}
QPtrList<MyMemNode> list = *list_;
// boDebug() << k_funcinfo << list.count() << endl;
list.sort();
unsigned long int size = 0;
while (!list.isEmpty()) {
MyMemNode* node = list.first();
list.removeFirst();
const char* function = node->mFunction;
// boDebug() << k_funcinfo << "left: " << list.count() << endl;
QPtrList<MyMemNode> sameFunction;
QPtrList<MyMemNode> otherFunction;
sameFunction.append(node);
QPtrListIterator<MyMemNode> listIt(list);
while (listIt.current()) {
MyMemNode* n = listIt.current();
++listIt;
if (strncmp(function, n->mFunction, 256) == 0) {
sameFunction.append(n);
} else {
otherFunction.append(n);
}
}
list = otherFunction;
QListViewItem* item = createFunctionItem(file, QString(function));
size += createFunctionList(item, &sameFunction);
}
// boDebug() << k_funcinfo << "done" << endl;
setSize(file, size);
return size;
}
unsigned long int BoMemoryDialog::createFunctionList(QListViewItem* function, const QPtrList<MyMemNode>* list)
{
if (!function || !list) {
return 0;
}
unsigned long int size = 0;
unsigned int mallocCalls = 0;
unsigned int newCalls = 0;
QPtrListIterator<MyMemNode> it(*list);
while (it.current()) {
MyMemNode* node = it.current();
++it;
#if 0
createMemoryItem(function, node);
#endif
size += node->mSize;
if (node->mIsMalloc) {
mallocCalls++;
} else {
newCalls++;
}
}
setSize(function, size);
function->setText(6, QString::number(mallocCalls));
function->setText(7, QString::number(newCalls));
return size;
}
|