-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreformanceSampler.cpp
159 lines (129 loc) · 4.76 KB
/
PreformanceSampler.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
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
/* ======== Basic Admin tool ========
* Copyright (C) 2004-2007 Erling K. Sæterdal
* No warranties of any kind
*
* License: zlib/libpng
*
* Author(s): Erling K. Sæterdal ( EKS )
* Credits:
* Menu code based on code from CSDM ( http://www.tcwonline.org/~dvander/cssdm ) Created by BAILOPAN
* Helping on misc errors/functions: BAILOPAN,karma,LDuke,sslice,devicenull,PMOnoTo,cybermind ( most who idle in #sourcemod on GameSurge realy )
* ============================ */
#include "PreformanceSampler.h"
#if PREFORMANCE_SAMPLER == 1
#include <Windows.h>
#include "stdio.h"
#endif
#ifdef WIN32
#else
#include <dlfcn.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#define _snprintf snprintf
#define _vsnprintf vsnprintf
#endif
static __int64 g_QuickSampleList[MAX_SAMPLEITEMS];
static LARGE_INTEGER g_TempSample;
static __int64 g_Freq;
static int g_SampleCount;
static stPreSample g_SampleList[MAX_SAMPLEITEMS];
static bool g_HasBeenConstructed = false;
PreformanceSampler::PreformanceSampler()
{
QPF(&g_TempSample);
g_Freq = ((__int64)g_TempSample.HighPart << 32) + (__int64)g_TempSample.LowPart;
g_HasBeenConstructed = true;
}
PreformanceSampler::~PreformanceSampler()
{
}
int PreformanceSampler::AddPreformaceName(const char *NameOfTimer)
{
if(!g_HasBeenConstructed)
PreformanceSampler();
strcpy(g_SampleList[g_SampleCount].Name,NameOfTimer);
g_SampleList[g_SampleCount].TotalTime = 0;
g_SampleList[g_SampleCount].CalledTimes = 0;
g_SampleList[g_SampleCount].ShortestTime = ULONG_MAX;
g_SampleList[g_SampleCount].LongestTime = 0;
g_SampleCount++;
return g_SampleCount - 1;
}
void PreformanceSampler::SampleStart(int SamplerIndex)
{
QPC(&g_TempSample);
g_QuickSampleList[SamplerIndex] = ((__int64)g_TempSample.HighPart << 32) + (__int64)g_TempSample.LowPart;
(g_QuickSampleList[SamplerIndex] *= 1000000) /= g_Freq;
}
void PreformanceSampler::SampleEnd(int SamplerIndex)
{
QPC(&g_TempSample);
__int64 EndTime = ((__int64)g_TempSample.HighPart << 32) + (__int64)g_TempSample.LowPart;
(EndTime *= 1000000) /= g_Freq;
EndTime -= g_QuickSampleList[SamplerIndex];
g_SampleList[SamplerIndex].CalledTimes++;
g_SampleList[SamplerIndex].TotalTime += EndTime;
if(EndTime > g_SampleList[SamplerIndex].LongestTime)
g_SampleList[SamplerIndex].LongestTime = EndTime;
if(EndTime < g_SampleList[SamplerIndex].ShortestTime)
g_SampleList[SamplerIndex].ShortestTime = EndTime;
}
void PreformanceSampler::WriteLogFile(const char *FileName)
{
FILE *UsersFileStream = fopen(FileName, "wt"); // "wt" clears the file each time
if(!UsersFileStream)
return;
char sTempLine[512];
char MinTime[256];
char MaxTime[256];
char TotalTime[256];
char AvrTime[256];
fputs("<html><head><title>Preformance information</title></head>",UsersFileStream);
fputs("<table border=\"1\">",UsersFileStream);
_snprintf(sTempLine,sizeof(sTempLine),"<tr><td>Preformance item name</td><td>Called times</td><td>Longest time</td><td>Shortest time</td><td>Total time</td><td>Average time</td></b>");
fputs(sTempLine,UsersFileStream);
for(int i=0;i<g_SampleCount;i++)
{
_snprintf(MinTime,sizeof(MinTime),"%s",GetTimeInformationString(g_SampleList[i].ShortestTime));
_snprintf(MaxTime,sizeof(MaxTime),"%s",GetTimeInformationString(g_SampleList[i].LongestTime));
_snprintf(TotalTime,sizeof(TotalTime),"%s",GetTimeInformationString(g_SampleList[i].TotalTime));
_snprintf(AvrTime,sizeof(AvrTime),"%s",GetTimeInformationString(g_SampleList[i].TotalTime / g_SampleList[i].CalledTimes));
_snprintf(sTempLine,sizeof(sTempLine),"<div><tr><td>%s</td><td>%d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr></div>",g_SampleList[i].Name,g_SampleList[i].CalledTimes,MaxTime,MinTime,TotalTime,AvrTime);
fputs(sTempLine,UsersFileStream);
}
fputs("</table></body></html>",UsersFileStream);
fclose(UsersFileStream);
}
const char *PreformanceSampler::GetTimeInformationString(__int64 Time)
{
static char sTempString[256];
long long Seconds = GetSeconds(Time);
long long MiliSeconds = GetMiliseconds(Time);
if(Seconds != 0)
{
_snprintf(sTempString,sizeof(sTempString),"%lld microseconds | %lld milliseconds | %lld seconds",Time,MiliSeconds,Seconds);
return sTempString;
}
else if(MiliSeconds != 0)
{
_snprintf(sTempString,sizeof(sTempString),"%lld microseconds | %lld milliseconds",Time,MiliSeconds);
return sTempString;
}
else
{
_snprintf(sTempString,sizeof(sTempString),"%lld microsecond",Time);
return sTempString;
}
}
long long PreformanceSampler::GetSeconds(__int64 Time)
{
return Time / 1000000;
}
long long PreformanceSampler::GetMiliseconds(__int64 Time)
{
return Time / 1000;
}