Intel(r) Performance Counter Monitor
cpuasynchcounter.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2009-2012, Intel Corporation
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 
7  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9  * Neither the name of Intel Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10 
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 */
13 //
14 // asynchonous CPU conters
15 //
16 // contact: Thomas Willhalm
17 
18 #ifndef CPUASYNCHCOUNTER_HEADER
19 #define CPUASYNCHCOUNTER_HEADER
20 
21 
26 #include <pthread.h>
27 #include <stdlib.h>
28 #include "cpucounters.h"
29 
30 #define DELAY 1 // in seconds
31 
32 using namespace std;
33 
34 
35 void * UpdateCounters(void *);
36 
38  PCM * m;
39 
40  CoreCounterState * cstates1, * cstates2;
41  SocketCounterState * skstates1, * skstates2;
42  SystemCounterState sstate1, sstate2;
43 
44  pthread_t UpdateThread;
45  pthread_mutex_t CounterMutex;
46 
47  friend void * UpdateCounters(void *);
48 
49 // AsynchronCounterState(const& AsynchronCounterState); //unimplemeted
50 // const& AsynchronCounterState operator=(const& AsynchronCounterState); //unimplemented
51 
52 public:
54  {
55  m = PCM::getInstance();
56  PCM::ErrorCode status = m->program();
57  if(status != PCM::Success)
58  {
59  cout << "\nCan not access CPU counters. Try to run pcm.x 1 to check the PMU access status.\n" << endl;
60  exit(-1);
61  }
62 
63  cstates1 = new CoreCounterState[m->getNumCores()];
64  cstates2 = new CoreCounterState[m->getNumCores()];
65  skstates1 = new SocketCounterState[m->getNumSockets()];
66  skstates2 = new SocketCounterState[m->getNumSockets()];
67 
68  for (uint32 i = 0; i < m->getNumCores(); ++i) {
69  cstates1[i] = getCoreCounterState(i);
70  cstates2[i] = getCoreCounterState(i);
71  }
72 
73  for (uint32 i = 0; i < m->getNumSockets(); ++i) {
74  skstates1[i] = getSocketCounterState(i);
75  skstates2[i] = getSocketCounterState(i);
76  }
77 
78  pthread_mutex_init(&CounterMutex, NULL);
79  pthread_create(&UpdateThread, NULL, UpdateCounters, this);
80  }
82  {
83  pthread_cancel(UpdateThread);
84  pthread_mutex_destroy(&CounterMutex);
85  m->cleanup();
86  delete[] cstates1;
87  delete[] cstates2;
88  delete[] skstates1;
89  delete[] skstates2;
90  }
91 
92  uint32 getNumCores()
93  { return m->getNumCores(); }
94 
95  uint32 getNumSockets()
96  { return m->getNumSockets(); }
97 
98  uint32 getQPILinksPerSocket()
99  {
100  return m->getQPILinksPerSocket();
101  }
102 
103  uint32 getSocketId(uint32 c)
104  {
105  return m->getSocketId(c);
106  }
107 
108  template <typename T, T func(CoreCounterState const &, CoreCounterState const &)>
109  T get(uint32 core)
110  {
111  pthread_mutex_lock(&CounterMutex);
112  T value = func(cstates1[core], cstates2[core]);
113  pthread_mutex_unlock(&CounterMutex);
114  return value;
115  }
116 
117  template <typename T, T func(int, CoreCounterState const &, CoreCounterState const &)>
118  T get(int param, uint32 core)
119  {
120  pthread_mutex_lock(&CounterMutex);
121  T value = func(param, cstates1[core], cstates2[core]);
122  pthread_mutex_unlock(&CounterMutex);
123  return value;
124  }
125 
126  template <typename T, T func(SocketCounterState const &, SocketCounterState const &)>
127  T getSocket(uint32 socket)
128  {
129  pthread_mutex_lock(&CounterMutex);
130  T value = func(skstates1[socket], skstates2[socket]);
131  pthread_mutex_unlock(&CounterMutex);
132  return value;
133  }
134 
135  template <typename T, T func(int, SocketCounterState const &, SocketCounterState const &)>
136  T getSocket(int param, uint32 socket)
137  {
138  pthread_mutex_lock(&CounterMutex);
139  T value = func(param, skstates1[socket], skstates2[socket]);
140  pthread_mutex_unlock(&CounterMutex);
141  return value;
142  }
143 
144  template <typename T, T func(uint32, uint32, SystemCounterState const &, SystemCounterState const &)>
145  T getSocket(uint32 socket, uint32 param)
146  {
147  pthread_mutex_lock(&CounterMutex);
148  T value = func(socket, param, sstate1, sstate2);
149  pthread_mutex_unlock(&CounterMutex);
150  return value;
151  }
152 
153  template <typename T, T func(SystemCounterState const &, SystemCounterState const &)>
154  T getSystem()
155  {
156  pthread_mutex_lock(&CounterMutex);
157  T value = func(sstate1, sstate2);
158  pthread_mutex_unlock(&CounterMutex);
159  return value;
160  }
161 
162  template <typename T, T func(int, SystemCounterState const &, SystemCounterState const &)>
163  T getSystem(int param)
164  {
165  pthread_mutex_lock(&CounterMutex);
166  T value = func(param, sstate1, sstate2);
167  pthread_mutex_unlock(&CounterMutex);
168  return value;
169  }
170 
171 };
172 
173 void * UpdateCounters(void * state)
174 {
176 
177  while (true) {
178  pthread_mutex_lock(&(s->CounterMutex));
179  for (uint32 core = 0; core < s->m->getNumCores(); ++core) {
180  s->cstates1[core] = s->cstates2[core];
181  s->cstates2[core] = s->m->getCoreCounterState(core);
182  }
183 
184  for (uint32 socket = 0; socket < s->m->getNumSockets(); ++socket) {
185  s->skstates1[socket] = s->skstates2[socket];
186  s->skstates2[socket] = s->m->getSocketCounterState(socket);
187  }
188 
189  s->sstate1 = s->sstate2;
190  s->sstate2 = s->m->getSystemCounterState();
191 
192  pthread_mutex_unlock(&(s->CounterMutex));
193  sleep(1);
194  }
195  return NULL;
196 }
197 
198 #endif
Definition: memoptest.cpp:35
Socket-wide counter state.
Definition: cpucounters.h:1339
SocketCounterState getSocketCounterState(uint32 socket)
Reads the counter state of a socket.
Definition: cpucounters.cpp:3234
SystemCounterState getSystemCounterState()
Reads the counter state of the system.
Definition: cpucounters.cpp:2998
Definition: cpuasynchcounter.h:37
System-wide counter state.
Definition: cpucounters.h:1358
uint32 getNumSockets()
Reads number of sockets (CPUs) in the system.
Definition: cpucounters.cpp:3313
CoreCounterState getCoreCounterState(uint32 core)
Reads the counter state of a (logical) core.
Definition: cpucounters.cpp:2673
Main CPU counters header.
SocketCounterState getSocketCounterState(uint32 socket)
Reads the counter state of a socket.
Definition: cpucounters.cpp:2665
uint32 getNumCores()
Reads number of logical cores in the system.
Definition: cpucounters.cpp:3303
CPU Performance Monitor.
Definition: cpucounters.h:212
CoreCounterState getCoreCounterState(uint32 core)
Reads the counter state of a (logical) core.
Definition: cpucounters.cpp:3296
(Logical) core-wide counter state
Definition: cpucounters.h:1331
ErrorCode
Return codes (e.g. for program(..) method)
Definition: cpucounters.h:323
static PCM * getInstance()
Returns PCM object.
Definition: cpucounters.cpp:196