aboutsummaryrefslogtreecommitdiff
path: root/src/zenutil/filteredrate.cpp
blob: de01af57bb4a705b371a2855650aa10944d52939 (plain) (blame)
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
// Copyright Epic Games, Inc. All Rights Reserved.

#include <zenutil/filteredrate.h>

namespace zen {

void
FilteredRate::Start()
{
	if (StartTimeUS == (uint64_t)-1)
	{
		uint64_t Expected = (uint64_t)-1;
		if (StartTimeUS.compare_exchange_strong(Expected, Timer.GetElapsedTimeUs()))
		{
			LastTimeUS = StartTimeUS.load();
		}
	}
}

void
FilteredRate::Stop()
{
	if (EndTimeUS == (uint64_t)-1)
	{
		uint64_t Expected = (uint64_t)-1;
		EndTimeUS.compare_exchange_strong(Expected, Timer.GetElapsedTimeUs());
	}
}

void
FilteredRate::Update(uint64_t Count)
{
	if (LastTimeUS == (uint64_t)-1)
	{
		return;
	}
	uint64_t TimeUS		 = Timer.GetElapsedTimeUs();
	uint64_t TimeDeltaUS = TimeUS - LastTimeUS;
	if (TimeDeltaUS >= 2000000)
	{
		uint64_t Delta	   = Count - LastCount;
		uint64_t PerSecond = (Delta * 1000000) / TimeDeltaUS;

		FilteredPerSecond = (PerSecond + (LastPerSecond * 7)) / 8;

		LastPerSecond = PerSecond;
		LastCount	  = Count;
		LastTimeUS	  = TimeUS;
	}
}

uint64_t
FilteredRate::GetCurrent() const
{
	if (LastTimeUS == (uint64_t)-1)
	{
		return 0;
	}
	return FilteredPerSecond;
}

uint64_t
FilteredRate::GetElapsedTimeUS() const
{
	if (StartTimeUS == (uint64_t)-1)
	{
		return 0;
	}
	if (EndTimeUS == (uint64_t)-1)
	{
		return 0;
	}
	return EndTimeUS - StartTimeUS;
}

bool
FilteredRate::IsActive() const
{
	return (StartTimeUS != (uint64_t)-1) && (EndTimeUS == (uint64_t)-1);
}

uint64_t
GetBytesPerSecond(uint64_t ElapsedWallTimeUS, uint64_t Count)
{
	if (ElapsedWallTimeUS == 0)
	{
		return 0;
	}
	return Count * 1000000 / ElapsedWallTimeUS;
}

}  // namespace zen