-
Notifications
You must be signed in to change notification settings - Fork 1
/
rrprofiler.h
97 lines (85 loc) · 2.54 KB
/
rrprofiler.h
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
/* Copyright (C)2004 Landmark Graphics
* Copyright (C)2005, 2006 Sun Microsystems, Inc.
*
* This library is free software and may be redistributed and/or modified under
* the terms of the wxWindows Library License, Version 3 or (at your option)
* any later version. The full license is in the LICENSE.txt file included
* with this distribution.
*
* This library 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
* wxWindows Library License for more details.
*/
#ifndef __RRPROFILER_H__
#define __RRPROFILER_H__
#include <stdlib.h>
#ifdef _MSC_VER
#define snprintf _snprintf
#define strdup _strdup
#endif
#include "rrtimer.h"
#include "rrlog.h"
class rrprofiler
{
public:
rrprofiler(const char *_name="Profiler", double _interval=2.0) :
interval(_interval), mbytes(0.0), mpixels(0.0), totaltime(0.0), start(0.0),
frames(0), lastframe(0.0)
{
profile=false; char *ev=NULL;
setname(_name); freestr=false;
if((ev=getenv("RRPROFILE"))!=NULL && !strncmp(ev, "1", 1))
profile=true;
if((ev=getenv("VGL_PROFILE"))!=NULL && !strncmp(ev, "1", 1))
profile=true;
}
~rrprofiler(void)
{
if(name && freestr) free(name);
}
void setname(char *_name)
{
if(_name) {name=strdup(_name); freestr=true;}
}
void setname(const char *_name)
{
if(_name) name=(char *)_name;
}
void startframe(void)
{
if(!profile) return;
start=timer.time();
}
void endframe(long pixels, long bytes, double incframes)
{
if(!profile) return;
double now=timer.time();
if(start!=0.0)
{
totaltime+=now-start;
if(pixels) mpixels+=(double)pixels/1000000.;
if(bytes) mbytes+=(double)bytes/1000000.;
if(incframes!=0.0) frames+=incframes;
}
if(lastframe==0.0) lastframe=now;
if(totaltime>interval || (now-lastframe)>interval)
{
char temps[256]; int i=0;
snprintf(&temps[i], 255-i, "%s ", name); i=strlen(temps);
if(mpixels) {snprintf(&temps[i], 255-i, "- %7.2f Mpixels/sec", mpixels/totaltime); i=strlen(temps);}
if(frames) {snprintf(&temps[i], 255-i, "- %7.2f fps", frames/totaltime); i=strlen(temps);}
if(mbytes) {snprintf(&temps[i], 255-i, "- %7.2f Mbits/sec (%.1f:1)", mbytes*8.0/totaltime, mpixels*3./mbytes); i=strlen(temps);}
rrout.PRINT("%s\n", temps);
totaltime=0.; mpixels=0.; frames=0.; mbytes=0.; lastframe=now;
}
}
private:
char *name;
double interval;
double mbytes, mpixels, totaltime, start, frames, lastframe;
bool profile;
rrtimer timer;
bool freestr;
};
#endif