summaryrefslogtreecommitdiff
path: root/plugins/utils/mir_profiler.cpp
blob: b788fd2dd261ffac0728c5c9d753c5330020f980 (plain)
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
/*
Copyright (C) 2005 Ricardo Pescuma Domenecci

This is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This 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
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this file; see the file license.txt.  If
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  
*/


// Disable "...truncated to '255' characters in the debug information" warnings
#pragma warning(disable: 4786)


#include "mir_profiler.h"
#include "mir_log.h"

#include <stdio.h>


MProfiler::Block MProfiler::root;
MProfiler::Block * MProfiler::current = &MProfiler::root;


MProfiler::Block::Block()
{
	parent = NULL;
	memset(&total, 0, sizeof(total));
	started = false;
}


MProfiler::Block::~Block()
{
	Reset();
}


void MProfiler::Block::Reset()
{
	for(std::map<std::string, MProfiler::Block *>::iterator it = children.begin(); 
			it != children.end(); ++it)
		delete it->second;
	children.clear();

	memset(&total, 0, sizeof(total));
	started = false;
}


void MProfiler::Block::Start()
{
	if (started)
		return;

	QueryPerformanceCounter(&start);
	last_step = start;
	started = true;
}


void MProfiler::Block::Step(const char *name)
{
	if (!started)
		return;
	
	LARGE_INTEGER end;
	QueryPerformanceCounter(&end);

	GetChild(name)->total.QuadPart += end.QuadPart - last_step.QuadPart;

	last_step = end;
}


void MProfiler::Block::Stop()
{
	if (!started)
		return;
	
	LARGE_INTEGER end;
	QueryPerformanceCounter(&end);

	total.QuadPart += end.QuadPart - start.QuadPart;

	started = false;
}


double MProfiler::Block::GetTimeMS() const
{
	LARGE_INTEGER frequency;
	QueryPerformanceFrequency(&frequency);

	return total.QuadPart * 1000. / frequency.QuadPart;
}


MProfiler::Block * MProfiler::Block::GetChild(const char *name)
{
	MProfiler::Block *ret = children[name];
	if (ret == NULL)
	{
		ret = new MProfiler::Block();
		ret->name = name;
		ret->parent = this;
		children[name] = ret;
	}
	return ret;
}


void MProfiler::Reset()
{
	root.Reset();
}

void MProfiler::Start(const char *name)
{
	current = current->GetChild(name);
	current->Start();
}

void MProfiler::Step(const char *name)
{
	current->Step(name);
}

void MProfiler::End()
{
	current->Stop();

	if (current->parent != NULL)
	{
		current = current->parent;
		QueryPerformanceCounter(&current->last_step);
	}
}

void MProfiler::Dump(const char *module)
{
	Dump(module, "", &root, -1, -1);
}


void MProfiler::Dump(const char *module, std::string prefix, Block *block, double parent, double total)
{
	for(std::map<std::string, MProfiler::Block *>::iterator it = block->children.begin(); 
			it != block->children.end(); ++it)
	{
		Block *child = it->second;
		double elapsed = child->GetTimeMS();

		if (total > 0)
		{
			mlog(module, "Profiler", "%s%-20s\t%5.1lf\t[%3.0lf%%]\t[%3.0lf%%]", prefix.c_str(), it->first.c_str(), 
				elapsed, elapsed / parent * 100, elapsed / total * 100);
		}
		else
		{
			mlog(module, "Profiler", "%s%-20s\t%5.1lf", prefix.c_str(), it->first.c_str(), 
				elapsed);
		}

		Dump(module, prefix + "   ", child, elapsed, total > 0 ? total : elapsed);
	}
}