Processor 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 &)>
109  T get(uint32 core)
110  {
111  pthread_mutex_lock(&CounterMutex);
112  T value = func(cstates2[core]);
113  pthread_mutex_unlock(&CounterMutex);
114  return value;
115  }
116  template <typename T, T func(CoreCounterState const &, CoreCounterState const &)>
117  T get(uint32 core)
118  {
119  pthread_mutex_lock(&CounterMutex);
120  T value = func(cstates1[core], cstates2[core]);
121  pthread_mutex_unlock(&CounterMutex);
122  return value;
123  }
124 
125  template <typename T, T func(int, CoreCounterState const &, CoreCounterState const &)>
126  T get(int param, uint32 core)
127  {
128  pthread_mutex_lock(&CounterMutex);
129  T value = func(param, cstates1[core], cstates2[core]);
130  pthread_mutex_unlock(&CounterMutex);
131  return value;
132  }
133 
134  template <typename T, T func(SocketCounterState const &)>
135  T getSocket(uint32 socket)
136  {
137  pthread_mutex_lock(&CounterMutex);
138  T value = func(skstates2[socket]);
139  pthread_mutex_unlock(&CounterMutex);
140  return value;
141  }
142 
143  template <typename T, T func(SocketCounterState const &, SocketCounterState const &)>
144  T getSocket(uint32 socket)
145  {
146  pthread_mutex_lock(&CounterMutex);
147  T value = func(skstates1[socket], skstates2[socket]);
148  pthread_mutex_unlock(&CounterMutex);
149  return value;
150  }
151 
152  template <typename T, T func(int, SocketCounterState const &, SocketCounterState const &)>
153  T getSocket(int param, uint32 socket)
154  {
155  pthread_mutex_lock(&CounterMutex);
156  T value = func(param, skstates1[socket], skstates2[socket]);
157  pthread_mutex_unlock(&CounterMutex);
158  return value;
159  }
160 
161  template <typename T, T func(uint32, uint32, SystemCounterState const &, SystemCounterState const &)>
162  T getSocket(uint32 socket, uint32 param)
163  {
164  pthread_mutex_lock(&CounterMutex);
165  T value = func(socket, param, sstate1, sstate2);
166  pthread_mutex_unlock(&CounterMutex);
167  return value;
168  }
169 
170  template <typename T, T func(SystemCounterState const &, SystemCounterState const &)>
171  T getSystem()
172  {
173  pthread_mutex_lock(&CounterMutex);
174  T value = func(sstate1, sstate2);
175  pthread_mutex_unlock(&CounterMutex);
176  return value;
177  }
178 
179  template <typename T, T func(int, SystemCounterState const &, SystemCounterState const &)>
180  T getSystem(int param)
181  {
182  pthread_mutex_lock(&CounterMutex);
183  T value = func(param, sstate1, sstate2);
184  pthread_mutex_unlock(&CounterMutex);
185  return value;
186  }
187 };
188 
189 void * UpdateCounters(void * state)
190 {
192 
193  while (true) {
194  pthread_mutex_lock(&(s->CounterMutex));
195  for (uint32 core = 0; core < s->m->getNumCores(); ++core) {
196  s->cstates1[core] = s->cstates2[core];
197  s->cstates2[core] = s->m->getCoreCounterState(core);
198  }
199 
200  for (uint32 socket = 0; socket < s->m->getNumSockets(); ++socket) {
201  s->skstates1[socket] = s->skstates2[socket];
202  s->skstates2[socket] = s->m->getSocketCounterState(socket);
203  }
204 
205  s->sstate1 = s->sstate2;
206  s->sstate2 = s->m->getSystemCounterState();
207 
208  pthread_mutex_unlock(&(s->CounterMutex));
209  sleep(1);
210  }
211  return NULL;
212 }
213 
214 #endif
Definition: memoptest.cpp:35
Socket-wide counter state.
Definition: cpucounters.h:2289
SocketCounterState getSocketCounterState(uint32 socket)
Reads the counter state of a socket.
Definition: cpucounters.cpp:3981
SystemCounterState getSystemCounterState()
Reads the counter state of the system.
Definition: cpucounters.cpp:3701
Definition: cpuasynchcounter.h:37
System-wide counter state.
Definition: cpucounters.h:2308
uint32 getNumSockets() const
Reads number of sockets (CPUs) in the system.
Definition: cpucounters.cpp:4116
CoreCounterState getCoreCounterState(uint32 core)
Reads the counter state of a (logical) core.
Definition: cpucounters.cpp:3296
Main CPU counters header.
SocketCounterState getSocketCounterState(uint32 socket)
Reads the counter state of a socket.
Definition: cpucounters.cpp:3288
uint32 getNumCores() const
Reads number of logical cores in the system.
Definition: cpucounters.cpp:4106
CPU Performance Monitor.
Definition: cpucounters.h:481
CoreCounterState getCoreCounterState(uint32 core)
Reads the counter state of a (logical) core.
Definition: cpucounters.cpp:4099
(Logical) core-wide counter state
Definition: cpucounters.h:2281
ErrorCode
Return codes (e.g. for program(..) method)
Definition: cpucounters.h:615
static PCM * getInstance()
Returns PCM object.
Definition: cpucounters.cpp:259