summaryrefslogtreecommitdiff
path: root/zen/perf.h
blob: ea8991a868c5691912551d54b36bf91d6cdfa646 (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
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
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html       *
// * Copyright (C) ZenJu (zhnmju123 AT gmx DOT de) - All Rights Reserved    *
// **************************************************************************

#ifndef DEBUG_PERF_HEADER
#define DEBUG_PERF_HEADER

#include "deprecate.h"

#ifdef FFS_WIN
#include <sstream>
#include "win.h" //includes "windows.h"
#else
#include <iostream>
#include <time.h>
#endif

//two macros for quick performance measurements
#define PERF_START CpuTimer perfTest;
#define PERF_STOP  perfTest.showResult();

#ifdef FFS_WIN
class CpuTimer
{
public:
    class TimerError {};

    ZEN_DEPRECATE
    CpuTimer() : frequency(), startTime(), resultShown(false)
    {
        SetThreadAffinity dummy;
        if (!::QueryPerformanceFrequency(&frequency))
            throw TimerError();
        if (!::QueryPerformanceCounter  (&startTime))
            throw TimerError();
    }

    ~CpuTimer()
    {
        if (!resultShown)
            showResult();
    }

    void showResult()
    {
        SetThreadAffinity dummy;
        LARGE_INTEGER currentTime = {};
        if (!::QueryPerformanceCounter(&currentTime))
            throw TimerError();

        const auto delta = static_cast<long>(1000.0 * (currentTime.QuadPart - startTime.QuadPart) / frequency.QuadPart);
        std::ostringstream ss;
        ss << delta << " ms";

        ::MessageBoxA(nullptr, ss.str().c_str(), "Timer", 0);
        resultShown = true;

        if (!::QueryPerformanceCounter(&startTime))
            throw TimerError(); //don't include call to MessageBox()!
    }

private:
    class SetThreadAffinity
    {
    public:
        SetThreadAffinity() : oldmask(::SetThreadAffinityMask(::GetCurrentThread(), 1)) { if (oldmask == 0) throw TimerError(); }
        ~SetThreadAffinity() { ::SetThreadAffinityMask(::GetCurrentThread(), oldmask); }
    private:
        SetThreadAffinity(const SetThreadAffinity&);
        SetThreadAffinity& operator=(const SetThreadAffinity&);
        const DWORD_PTR oldmask;
    };

    LARGE_INTEGER frequency;
    LARGE_INTEGER startTime;
    bool resultShown;
};


#else
class CpuTimer
{
public:
    class TimerError {};

    ZEN_DEPRECATE
    CpuTimer() : startTime(), resultShown(false)
    {
        //clock() seems to give grossly incorrect results: multi core issue?
        //gettimeofday() seems fine but is deprecated
        if (::clock_gettime(CLOCK_MONOTONIC_RAW, &startTime) != 0) //CLOCK_MONOTONIC measures time reliably across processors!
            throw TimerError();
    }

    ~CpuTimer()
    {
        if (!resultShown)
            showResult();
    }

    void showResult()
    {
        timespec currentTime = {};
        if (::clock_gettime(CLOCK_MONOTONIC_RAW, &currentTime) != 0)
            throw TimerError();

        const auto delta = static_cast<long>((currentTime.tv_sec - startTime.tv_sec) * 1000.0 + (currentTime.tv_nsec - startTime.tv_nsec) / 1000000.0);
        std::clog << "Perf: duration: " << delta << " ms\n";
        resultShown = true;

        if (::clock_gettime(CLOCK_MONOTONIC_RAW, &startTime) != 0)
            throw TimerError();
    }

private:
    timespec startTime;
    bool resultShown;
};
#endif

#endif //DEBUG_PERF_HEADER
bgstack15