forked from psi46/psi46test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profiler.cpp
70 lines (51 loc) · 1.17 KB
/
profiler.cpp
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
// profiler.cpp
#ifdef _WIN32
#include <windows.h>
#include <intrin.h>
#include "profiler.h"
long long Watchpoint::frequency = 0;
std::list<Watchpoint*> *Watchpoint::wplist = 0;
Watchpoint::Watchpoint(const char *fname)
{
if (!IsRunning())
{
if (SetThreadAffinityMask(GetCurrentThread(), 1) == 0) // Linux: sched_setaffinity
{
printf("ERROR profiler\n");
exit(1);
}
if (!QueryPerformanceFrequency(PLARGE_INTEGER(&frequency)))
{
printf("ERROR profiler\n");
exit(2);
}
wplist = new std::list<Watchpoint*>;
}
name = fname;
t = 0;
n = 0;
wplist->push_back(this);
}
Watchpoint::~Watchpoint()
{
if (!IsRunning()) return;
Report("profiler_report.txt");
delete wplist;
wplist = 0;
}
void Watchpoint::Report(const char *filename)
{
FILE *f = fopen(filename, "wt");
if (!f) return;
std::list<Watchpoint*>::iterator i;
unsigned int width = 0;
for (i = wplist->begin(); i != wplist->end(); i++)
if ((*i)->name.size() > width) width = (*i)->name.size();
if (width > 300) width = 200;
for (i = wplist->begin(); i != wplist->end(); i++)
{
fprintf(f, "%-*s %7i %11.3f\n", width,
(*i)->name.c_str(), (*i)->n, double((*i)->t)/frequency);
}
}
#endif