summaryrefslogtreecommitdiff
path: root/src/ToolBox/superpmi/superpmi/cycletimer.cpp
blob: de0e19b93577c07546af72e89fa3b04ffca925f1 (plain)
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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

#include "standardpch.h"
#include "cycletimer.h"

CycleTimer::CycleTimer()
{
    start = 0;
    stop = 0;
    overhead = QueryOverhead();
}

CycleTimer::~CycleTimer()
{
}

void CycleTimer::Start()
{
    BOOL retVal = QueryThreadCycleTime(GetCurrentThread(), &start);

    if(retVal == FALSE)
    {
        LogError("CycleTimer::Start unable to QPC. error was 0x%08x", ::GetLastError());
        ::__debugbreak();
    }
}

void CycleTimer::Stop()
{
    BOOL retVal = QueryThreadCycleTime(GetCurrentThread(), &stop);

    if(retVal == FALSE)
    {
        LogError("CycleTimer::Stop unable to QPC. error was 0x%08x", ::GetLastError());
        ::__debugbreak();
    }
}

unsigned __int64 CycleTimer::GetCycles()
{
    return stop - start - overhead;
}

unsigned __int64 CycleTimer::QueryOverhead()
{
    unsigned __int64 tot = 0;
    unsigned __int64 startCycles;
    unsigned __int64 endCycles;
    const int N = 1000;
    for (int i = 0; i < N; i++)
    {
        QueryThreadCycleTime(GetCurrentThread(), &startCycles);
        QueryThreadCycleTime(GetCurrentThread(), &endCycles);
        tot += (endCycles-startCycles);
    }
    return tot/N;
}