David's final changes: more profiler features, fixes.
[ipdf/code.git] / src / profiler.cpp
1 #include "profiler.h"
2 #include "log.h"
3
4 #include "SDL.h" 
5
6 using namespace IPDF;
7
8 Profiler IPDF::g_profiler;
9
10 void Profiler::BeginZone(std::string name)
11 {
12         if (!m_zones.count(name))
13                 m_zones[name] = ProfileZone{0,0,0,0,0,0};
14         m_zones[name].tics_begin = SDL_GetPerformanceCounter();
15         m_zone_stack.push(name);
16 }
17
18 void Profiler::AddCounter(std::string name, uint64_t amt)
19 {
20         if (!m_counters.count(name))
21                 m_counters[name] = amt;
22         else
23                 m_counters[name] += amt;
24 }
25
26 void Profiler::EndZone()
27 {
28         std::string name = m_zone_stack.top();
29         m_zone_stack.pop();
30         m_zones[name].tics_end = SDL_GetPerformanceCounter();
31         m_zones[name].tics_frame += m_zones[name].tics_end - m_zones[name].tics_begin;
32         m_zones[name].tics_total += m_zones[name].tics_end - m_zones[name].tics_begin;
33         m_zones[name].calls_frame++;
34         m_zones[name].calls_total++;
35 }
36
37 void Profiler::EndFrame()
38 {
39         // Zero all of the frame counts
40         for (auto& it : m_zones)
41         {
42                 if (m_enabled)
43                 {
44                         printf("perf_zone\t\"%s\"\t%lu %lu\t%lu %lu\n", it.first.c_str(), it.second.tics_frame * 1000 / SDL_GetPerformanceFrequency(), it.second.calls_frame, it.second.tics_total * 1000 / SDL_GetPerformanceFrequency(), it.second.calls_total);
45                 }
46                 it.second.tics_frame = 0;
47                 it.second.calls_frame = 0;
48         }
49
50         for (auto& it : m_counters)
51         {
52                 if (m_enabled)
53                 {
54                         printf("perf_counter\t\"%s\"\t%lu\n", it.first.c_str(), it.second);
55                 }
56                 it.second = 0;
57         }
58 }

UCC git Repository :: git.ucc.asn.au