aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/include/uhdlib/utils/auto_timer.hpp
blob: 227750a2f21cdaac5b93702458313ecb6438dde6 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//
// Copyright 2017 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
//
// NOTE: This is an experimental utility class and Ettus Research
//       reserves the right to make behavioral and interface changes at
//       any time, including removing this file without notice.
// It should not be used in production code.
//

#ifndef INCLUDED_UHD_UTILS_AUTO_TIMER_HPP
#define INCLUDED_UHD_UTILS_AUTO_TIMER_HPP

// for now, only implemented for windows
#ifdef UHD_PLATFORM_WIN32

// Defines struct tm
#include "time.h"

#include <windows.h>

#include <uhd/utils/msg.hpp>

/*!
 * Inserts a timer that logs the duration of its existence (construction to destruction) and the context string to UHD_MSG
 * \param context The context string to log in addition to the duration. String buffer MUST be maintained by caling code throughout lifetime of timer object.
 */
#define PROFILE_TIMING(context) \
   uhd::_auto_timer::auto_timer ___at(context);

/*!
 * Inserts a timer that logs the duration (if exceeds threshold) of its existence (construction to destruction) and the context string to UHD_MSG
 * \param context The context string to log in addition to the duration. String buffer MUST be maintained by caling code throughout lifetime of timer object.
 * \param threshold Only if the lifetime of the timer exceeds this value will it be logged
 */
#define PROFILE_TIMING_WITH_THRESHOLD(context,threshold) \
   uhd::_auto_timer::auto_timer ___at(context,threshold);

 /*!
 * Inserts a timer that logs the duration of its existence (construction to destruction) and the context string to UHD_MSG
 * \param context The context string to log in addition to the duration. String buffer MUST be maintained by caling code throughout lifetime of timer object.
 * \param unitScale Report duration in ms or us (kUnitScaleMS or kUnitScaleUS)
 */
#define PROFILE_TIMING_WITH_SCALE(context,unitScale) \
   uhd::_auto_timer::auto_timer ___at(context,0,unitScale);

 /*!
 * Inserts a timer that logs the duration (if exceeds threshold) of its existence (construction to destruction) and the context string to UHD_MSG
 * \param context The context string to log in addition to the duration. String buffer MUST be maintained by caling code throughout lifetime of timer object.
 * \param threshold Only if the lifetime of the timer exceeds this value will it be logged
 * \param unitScale Report duration in ms or us (kUnitScaleMS or kUnitScaleUS)
 */
#define PROFILE_TIMING_WITH_THRESHOLD_AND_SCALE(context,threshold,unitScale) \
   uhd::_auto_timer::auto_timer ___at(context,threshold,unitScale);

namespace uhd {
   namespace _auto_timer {

static const uint64_t kUnitScaleMS = 1000;
static const uint64_t kUnitScaleUS = 1000000;


class auto_timer
{
public:

   auto_timer(
      const char* context,
      uint64_t reporting_threshold = 0,
      uint64_t unit_scale = kUnitScaleUS) :
      _context(context),
      _reporting_threshold(reporting_threshold),
      _unit_scale(unit_scale)
   {
      ::QueryPerformanceCounter(&_start_time);
      switch (unit_scale)
      {
      case kUnitScaleMS:
         _unit_scale_str = "ms";
         break;
      case kUnitScaleUS:
      default:
         _unit_scale_str = "us";
         break;
      }
   }

   ~auto_timer()
   {
      LARGE_INTEGER freq;
      uint64_t diff_time = 0;

      ::QueryPerformanceCounter(&_end_time);
      QueryPerformanceFrequency(&freq);
      diff_time =
         (uint64_t)(_end_time.QuadPart - _start_time.QuadPart)*
         _unit_scale /
         freq.QuadPart;

      if (diff_time >= _reporting_threshold)
      {
         UHD_MSG(status) << "^ " << _context << "\t" << std::dec << diff_time << _unit_scale_str << std::endl;
      }

   }

private:
   // Usage
   auto_timer();
   auto_timer(const auto_timer&);

   LARGE_INTEGER _start_time;
   LARGE_INTEGER _end_time;
   uint64_t _unit_scale;
   uint64_t _reporting_threshold;
   const char* _context;
   char* _unit_scale_str;

}; // class auto_timer

}} //namespace uhd::_auto_timer

#else //non-windows platforms

#define PROFILE_TIMING(context) 

#define PROFILE_TIMING_WITH_THRESHOLD(context,threshold) 

#define PROFILE_TIMING_WITH_SCALE(context,unitScale) 

#define PROFILE_TIMING_WITH_THRESHOLD_AND_SCALE(context,threshold,unitScale) 

#endif

#endif /* INCLUDED_UHD_UTILS_AUTO_TIMER_HPP */