Version: SMASH-2.0
tsc.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009-2014 Matthias Kretz <kretz@kde.org>
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) version 3.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 
19 */
20 
21 #ifndef SRC_INCLUDE_SMASH_TSC_H_
22 #define SRC_INCLUDE_SMASH_TSC_H_
23 
24 #include <cstdint>
25 #include <iosfwd>
26 
27 #ifdef _MSC_VER
28 #include <intrin.h>
29 #pragma intrinsic(__rdtsc)
30 #endif
31 
32 namespace smash {
33 
41  public:
43  void start();
44 
46  void stop();
47 
49  uint64_t cycles() const;
50 
51  private:
53  union Data {
55  uint64_t a;
57  unsigned int b[2];
58  };
63 };
64 
65 inline void TimeStampCounter::start() {
66 #ifdef VC_IMPL_MIC
67  asm volatile("xor %%eax,%%eax\n\tcpuid\n\trdtsc"
68  : "=a"(m_start.b[0]), "=d"(m_start.b[1])::"ebx", "ecx");
69 #elif defined _MSC_VER
70  unsigned int tmp;
71  m_start.a = __rdtscp(&tmp);
72 #else
73  asm volatile("rdtscp" : "=a"(m_start.b[0]), "=d"(m_start.b[1])::"ecx");
74 #endif
75 }
76 
77 inline void TimeStampCounter::stop() {
78 #ifdef VC_IMPL_MIC
79  asm volatile("xor %%eax,%%eax\n\tcpuid\n\trdtsc"
80  : "=a"(m_end.b[0]), "=d"(m_end.b[1])::"ebx", "ecx");
81 #elif defined _MSC_VER
82  unsigned int tmp;
83  m_end.a = __rdtscp(&tmp);
84 #else
85  asm volatile("rdtscp" : "=a"(m_end.b[0]), "=d"(m_end.b[1])::"ecx");
86 #endif
87 }
88 
89 inline uint64_t TimeStampCounter::cycles() const { return m_end.a - m_start.a; }
90 
91 std::ostream &operator<<(std::ostream &out, const TimeStampCounter &tsc);
92 
93 } // namespace smash
94 
95 #endif // SRC_INCLUDE_SMASH_TSC_H_
smash
Definition: action.h:24
smash::TimeStampCounter::Data
Union that stores cycles,.
Definition: tsc.h:53
smash::operator<<
std::ostream & operator<<(std::ostream &out, const ActionPtr &action)
Definition: action.h:518
smash::TimeStampCounter::Data::b
unsigned int b[2]
Or two 32-bit integers.
Definition: tsc.h:57
smash::TimeStampCounter::start
void start()
Start the counter.
Definition: tsc.h:65
smash::TimeStampCounter::stop
void stop()
Stop the counter.
Definition: tsc.h:77
smash::TimeStampCounter
A low-overhead timer for benchmarking small regions of code.
Definition: tsc.h:40
smash::TimeStampCounter::cycles
uint64_t cycles() const
Definition: tsc.h:89
smash::TimeStampCounter::m_end
Data m_end
Stores end of benchmarking.
Definition: tsc.h:62
smash::TimeStampCounter::m_start
Data m_start
Stores start of benchmarking.
Definition: tsc.h:60
smash::TimeStampCounter::Data::a
uint64_t a
Either one 64-bit integer.
Definition: tsc.h:55