xref: /haiku/src/apps/cortex/support/ProfileTarget.cpp (revision 19ae20e67e91fc09cc9fc5c0e60e21e24e7a53eb)
1 /*
2  * Copyright (c) 1999-2000, Eric Moon.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions, and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 
32 // ProfileTarget.cpp
33 
34 #include "ProfileTarget.h"
35 #include <cstdio>
36 #include <cstring>
37 #include <list>
38 #include <algorithm>
39 
40 __USE_CORTEX_NAMESPACE
41 
42 // -------------------------------------------------------- //
43 // ctor/dtor
44 // -------------------------------------------------------- //
45 
~ProfileTarget()46 ProfileTarget::~ProfileTarget() {}
ProfileTarget()47 ProfileTarget::ProfileTarget() {}
48 
49 // -------------------------------------------------------- //
50 // user operations
51 // -------------------------------------------------------- //
52 
clear()53 void ProfileTarget::clear() {
54 	m_blockEntryMap.clear();
55 }
56 
57 // [e.moon 14oct99] moved prototype out of header
58 bool operator<(const ProfileTarget::block_entry& a, const ProfileTarget::block_entry& b);
59 
operator <(const ProfileTarget::block_entry & a,const ProfileTarget::block_entry & b)60 bool operator<(const ProfileTarget::block_entry& a, const ProfileTarget::block_entry& b) {
61 	return b.elapsed < a.elapsed;
62 }
63 
64 class fnDumpEntry { public:
65 	uint32 nameLength;
66 	BString maxPad;
fnDumpEntry(uint32 _n)67 	fnDumpEntry(uint32 _n) : nameLength(_n) {
68 		maxPad.SetTo(' ', nameLength);
69 		fprintf(stderr,
70 			"  BLOCK%s COUNT         ELAPSED        AVERAGE\n"
71 			"  ----------------------------------------------------------------------------\n",
72 			maxPad.String());
73 	}
operator ()(ProfileTarget::block_entry & entry) const74 	void operator()(ProfileTarget::block_entry& entry) const {
75 		BString namePad;
76 		namePad.SetTo(' ', nameLength-strlen(entry.name));
77 		fprintf(stderr,
78 			"  %s:%s  %8ld        %8Ld        %.4f\n",
79 			entry.name,
80 			namePad.String(),
81 			entry.count,
82 			entry.elapsed,
83 			(float)entry.elapsed/entry.count);
84 	}
85 };
86 
87 
dump() const88 void ProfileTarget::dump() const {
89 	fprintf(stderr, "\nProfileTarget::dump()\n\n");
90 
91 	list<block_entry> sorted;
92 	uint32 nameLength = 0;
93 
94 	for(block_entry_map::const_iterator it = m_blockEntryMap.begin();
95 		it != m_blockEntryMap.end();
96 		it++) {
97 		if((*it).first.Length() > nameLength)
98 			nameLength = (*it).first.Length();
99 		sorted.push_back(block_entry());
100 		sorted.back() = (*it).second;
101 		sorted.back().name = (*it).first.String();
102 	}
103 
104 	sorted.sort();
105 	for_each(sorted.begin(), sorted.end(), fnDumpEntry(nameLength));
106 }
107 
108 // -------------------------------------------------------- //
109 // profile-source operations
110 // -------------------------------------------------------- //
111 
addBlockEntry(const char * blockName,bigtime_t elapsed)112 inline void ProfileTarget::addBlockEntry(
113 	const char* blockName, bigtime_t elapsed) {
114 
115 	block_entry& e = m_blockEntryMap[blockName];
116 	e.count++;
117 	e.elapsed += elapsed;
118 }
119 
120 // END -- ProfileTarget.cpp --
121 
122