forked from mmeeks/dwarfprofile
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogging.cxx
More file actions
187 lines (161 loc) · 5.04 KB
/
logging.cxx
File metadata and controls
187 lines (161 loc) · 5.04 KB
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* a De-Dwarfe'd C++ microcosm:
*/
#include <vector>
#include <algorithm>
#include <malloc.h>
#include <assert.h>
#include <string.h>
#include <logging.hxx>
struct FileSystemNode;
struct FileSystemNode {
char *mpName;
FileSystemNode *mpParent;
typedef std::vector< FileSystemNode * > ChildsType; // Hamburg nostalgia
ChildsType maChildren;
FileSystemNode (FileSystemNode *pParent,
const char *pName, int nLength)
{
mpName = strndup (pName, nLength);
mpParent = pParent;
if (mpParent)
mpParent->maChildren.push_back(this);
mnSize = 0;
useCount = 0;
}
~FileSystemNode()
{
free (mpName);
}
static FileSystemNode *gpRoot;
static FileSystemNode *getNode (const char *pPath)
{
assert (pPath != NULL);
if (!gpRoot)
gpRoot = new FileSystemNode (NULL, "", 0);
FileSystemNode *pNode = gpRoot;
for (int last = 0, i = 0; pPath[i]; i++)
{
if (pPath[i] == '/')
{
if (i - last > 0)
{
FileSystemNode *pChild;
pChild = pNode->lookupNode(pPath + last, i - last);
assert (pChild != NULL);
pNode = pChild;
}
last = i + 1;
}
}
return pNode;
}
FileSystemNode *lookupNode (const char *pName, int nLength)
{
// Un-mess-up relative paths etc. hoping that
// symlinks are kind to us.
if (!strncmp (pName, "..", nLength))
return mpParent ? mpParent : gpRoot;
if (!strncmp (pName, ".", nLength))
return this;
// slow as you like etc.
for (ChildsType::iterator it = maChildren.begin();
it != maChildren.end(); ++it)
{
if (!strncmp ((*it)->mpName, pName, nLength))
return *it;
}
return new FileSystemNode(this, pName, nLength);
}
// Payload
size_t mnSize;
size_t useCount;
// Size accumulated down the tree
void addSize (size_t nSize)
{
mnSize += nSize;
useCount++;
if (mpParent != NULL)
mpParent->addSize (nSize);
}
static void accumulate_size (const char *pName, const char *pFunc,
int line, int col, size_t size)
{
if (size == 0)
{
// MJW - checkme - why is this zero so often ? ...
// fprintf (stderr, "odd zero size at '%s' '%s'\n", pName, pFunc);
return;
}
if (pName == NULL) {
return; /* some DIE have no names */
}
(void)line; (void)col; // later
FileSystemNode *pNode = getNode(pName);
if (pFunc)
pNode = pNode->lookupNode(pFunc, strlen(pFunc));
pNode->addSize (size);
}
void dumpAtDepth (int nDepth)
{
if (nDepth < 0)
return;
static const char aIndent[] = "| ";
assert (nDepth < (int)sizeof (aIndent));
const char *pIndent = aIndent + nDepth;
for (ChildsType::iterator it = maChildren.begin();
it != maChildren.end(); ++it)
{
fprintf (stdout, "%10lu %8lu %4lu %s%s\n",
(unsigned long)(*it)->mnSize,
(unsigned long)(*it)->useCount,
(unsigned long)((*it)->useCount > 0?(*it)->mnSize / (*it)->useCount:0),
pIndent,
(*it)->mpName);
(*it)->dumpAtDepth (nDepth-1);
}
}
static bool big_first (FileSystemNode *a, FileSystemNode *b)
{
return a->mnSize > b->mnSize;
}
void sortChildren()
{
std::sort (maChildren.begin(), maChildren.end(), big_first);
for (ChildsType::iterator it = maChildren.begin();
it != maChildren.end(); ++it)
(*it)->sortChildren();
}
};
FileSystemNode *FileSystemNode::gpRoot = NULL;
void
register_compile_unit (const char *name, size_t size)
{
fprintf (stdout, "compile-unit '%s', size: %ld\n",
name, (long)size);
}
void register_file_span (const char *path, const char *func,
int line, int col, size_t size)
{
if (!path)
return;
FileSystemNode::accumulate_size (path, func, line, col, size);
}
void dump_results()
{
FileSystemNode::gpRoot->sortChildren();
for (int i = 2; i <= 14; i+= 6)
{
//int i = 12;
fprintf (stdout,
"\n---\n\n Breakdown at depth %d\n\n"
"Total Size Count Av. M Element\n", i);
FileSystemNode::gpRoot->dumpAtDepth(i);
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */