xref: /haiku/src/apps/cortex/support/ProfileTarget.cpp (revision 2f470aec1c92ce6917b8a903e343795dc77af41f)
1 // ProfileTarget.cpp
2 
3 #include "ProfileTarget.h"
4 #include <cstdio>
5 #include <cstring>
6 #include <list>
7 #include <algorithm>
8 
9 __USE_CORTEX_NAMESPACE
10 
11 // -------------------------------------------------------- //
12 // ctor/dtor
13 // -------------------------------------------------------- //
14 
15 ProfileTarget::~ProfileTarget() {}
16 ProfileTarget::ProfileTarget() {}
17 
18 // -------------------------------------------------------- //
19 // user operations
20 // -------------------------------------------------------- //
21 
22 void ProfileTarget::clear() {
23 	m_blockEntryMap.clear();
24 }
25 
26 // [e.moon 14oct99] moved prototype out of header
27 bool operator<(const ProfileTarget::block_entry& a, const ProfileTarget::block_entry& b);
28 
29 bool operator<(const ProfileTarget::block_entry& a, const ProfileTarget::block_entry& b) {
30 	return b.elapsed < a.elapsed;
31 }
32 
33 class fnDumpEntry { public:
34 	uint32 nameLength;
35 	BString maxPad;
36 	fnDumpEntry(uint32 _n) : nameLength(_n) {
37 		maxPad.SetTo(' ', nameLength);
38 		fprintf(stderr,
39 			"  BLOCK%s COUNT         ELAPSED        AVERAGE\n"
40 			"  ----------------------------------------------------------------------------\n",
41 			maxPad.String());
42 	}
43 	void operator()(ProfileTarget::block_entry& entry) const {
44 		BString namePad;
45 		namePad.SetTo(' ', nameLength-strlen(entry.name));
46 		fprintf(stderr,
47 			"  %s:%s  %8ld        %8Ld        %.4f\n",
48 			entry.name,
49 			namePad.String(),
50 			entry.count,
51 			entry.elapsed,
52 			(float)entry.elapsed/entry.count);
53 	}
54 };
55 
56 
57 void ProfileTarget::dump() const {
58 	fprintf(stderr, "\nProfileTarget::dump()\n\n");
59 
60 	list<block_entry> sorted;
61 	uint32 nameLength = 0;
62 
63 	for(block_entry_map::const_iterator it = m_blockEntryMap.begin();
64 		it != m_blockEntryMap.end();
65 		it++) {
66 		if((*it).first.Length() > nameLength)
67 			nameLength = (*it).first.Length();
68 		sorted.push_back(block_entry());
69 		sorted.back() = (*it).second;
70 		sorted.back().name = (*it).first.String();
71 	}
72 
73 	sorted.sort();
74 	for_each(sorted.begin(), sorted.end(), fnDumpEntry(nameLength));
75 }
76 
77 // -------------------------------------------------------- //
78 // profile-source operations
79 // -------------------------------------------------------- //
80 
81 inline void ProfileTarget::addBlockEntry(
82 	const char* blockName, bigtime_t elapsed) {
83 
84 	block_entry& e = m_blockEntryMap[blockName];
85 	e.count++;
86 	e.elapsed += elapsed;
87 }
88 
89 // END -- ProfileTarget.cpp --
90 
91