Arcane  v3.14.10.0
Documentation utilisateur
Chargement...
Recherche...
Aucune correspondance
PerfCounterMng.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2022 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7
8#ifndef ARCANE_PERFCOUNTERMNG_H_
9#define ARCANE_PERFCOUNTERMNG_H_
10
11//GG: Toute cette partie est tres linux/x64 spécifique et n'a rien à faire ici:
12// mettre tous ce qui est spécifique dans PlatformUtils
13#ifdef ARCANE_OS_LINUX
14
15#include <string>
16#include <vector>
17#include <iostream>
18#include <iomanip>
19
20//#define ACTIVATE_PERF_COUNTER
21#ifdef ACTIVATE_PERF_COUNTER
22#define CHECKPERF(instruction) (instruction) ;
23#else
24#define CHECKPERF(instruction)
25#endif
26
27ARCANE_BEGIN_NAMESPACE
28
29#if defined(__x86_64__)
30#ifndef WIN32
31static inline void rdtsc(volatile unsigned long long int *counter){
32
33 asm volatile ("rdtsc \n\t"
34 "movl %%eax,%0 \n\t"
35 "movl %%edx,%1 \n\t"
36 : "=m" (((unsigned *)counter)[0]), "=m" (((unsigned *)counter)[1])
37 :
38 : "eax" , "edx");
39}
40
41#define RDTSC(X) asm volatile ("rdtsc \n\t"\
42 "movl %%eax,%0 \n\t" \
43 "movl %%edx,%1 \n\t" \
44 : "=m" (((unsigned *)(X))[0]), "=m" (((unsigned *)(X))[1]) \
45 : \
46 : "eax" , "edx")
47#else
48
49#include <intrin.h>
50#pragma intrinsic(__rdtsc)
51
52static inline void rdtsc(volatile unsigned long long int *counter){
53 *counter = __rdtsc();
54}
55
56#define RDTSC(X) *X=__rdtsc()
57
58#endif // WIN32
59
60#else // defined(__x86_64__)
61
62// IMPLEMENTATION NE FAISANT RIEN: A supprimer avec ce fichier.
63static inline void rdtsc(volatile unsigned long long int *counter)
64{
65 *counter = 1;
66}
67
68#endif // defined(__x86_64__)
69
70//! Retourne la fréquence du CPU en Mhz
71extern "C++" int arcaneGetCpuBaseFrequency();
72
73template<typename PerfCounterT>
74class PerfCounterMng
75{
76public:
77 typedef unsigned long long int ValueType ;
78 typedef typename PerfCounterT::eType PhaseType;
79 typedef std::pair<ValueType,ValueType> CountType;
80 typedef std::vector<CountType> CountListType;
81
82 PerfCounterMng()
83 : m_last_value(0)
84 {
85 m_cpu_frec = arcaneGetCpuBaseFrequency() ;
86 m_counts.resize(PerfCounterT::NbCounters) ;
87 init() ;
88 }
89
90 virtual ~PerfCounterMng(){}
91
92 void init()
93 {
94 for(std::size_t i=0;i<m_counts.size();++i)
95 {
96 m_counts[i].first = 0 ;
97 m_counts[i].second = 0 ;
98 }
99 }
100
101 void init(PhaseType const& phase)
102 {
103 CountType& count = m_counts[phase];
104 count.first = 0 ;
105 count.second = 0 ;
106 }
107 void start(PhaseType const& phase)
108 {
109 rdtsc(&m_counts[phase].second);
110 }
111 void stop(PhaseType const& phase)
112 {
113 CountType& count = m_counts[phase];
114 rdtsc(&m_last_value) ;
115 m_last_value = m_last_value - count.second;
116 count.first += m_last_value;
117 }
118
119 ValueType getLastValue()
120 {
121 return m_last_value;
122 }
123
124 ValueType getValue(PhaseType const& phase)
125 {
126 return m_counts[phase].first;
127 }
128
129 double getValueInSeconds(PhaseType const& phase)
130 {
131 return m_counts[phase].first/m_cpu_frec*1E-6;
132 }
133
134 void printInfo() const
135 {
136 std::cout<<"PERF INFO : "<<std::endl ;
137 std::cout<<std::setw(10)<<"COUNT"<<" : "<<"VALUE"<<std::endl ;
138 //for(typename CountListType::const_iterator iter = m_counts.begin();iter!=m_counts.end();++iter)
139 for(std::size_t i=0;i<m_counts.size();++i){
140 std::cout<<std::setw(10)<<PerfCounterT::m_names[i]<< " : "<<m_counts[i].first/m_cpu_frec*1E-6<<'\n' ;
141 }
142 }
143
144 void printInfo(std::ostream& stream) const
145 {
146 stream<<"PERF INFO : "<<std::endl ;
147 stream<<std::setw(10)<<"COUNT"<<" : "<<"VALUE"<<std::endl ;
148 //for(typename CountListType::const_iterator iter = m_counts.begin();iter!=m_counts.end();++iter)
149 for(std::size_t i=0;i<m_counts.size();++i){
150 stream<<std::setw(10)<<PerfCounterT::m_names[i]<< " : "<<m_counts[i].first/m_cpu_frec*1E-6<<'\n' ;
151 }
152 }
153
154
155private :
156 ValueType m_last_value;
157 CountListType m_counts;
158 double m_cpu_frec;
159};
160
161ARCANE_END_NAMESPACE
162
163#else // ARCANE_OS_LINUX
164
165#define CHECKPERF(instruction)
166
167#endif // ARCANE_OS_LINUX
168
169#endif /* PERFCOUNTERMNG_H_ */
170
int arcaneGetCpuBaseFrequency()
Retourne la fréquence du CPU en Mhz.