Processor Counter Monitor
width_extender.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2009-2018, 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 // written by Roman Dementiev
14 // Austen Ott
15 
16 #ifndef WIDTH_EXTENDER_HEADER_
17 #define WIDTH_EXTENDER_HEADER_
18 
23 #include <stdlib.h>
24 #include "cpucounters.h"
25 #include "utils.h"
26 #include "client_bw.h"
27 #include "mutex.h"
28 #include <memory>
29 #ifndef _MSC_VER
30 // the header can not be included into code using CLR
31 #include <thread>
32 #else
33 namespace std {
34  class thread;
35  }
36 #endif
37 
39 {
40 public:
42  {
43  virtual uint64 operator () () = 0;
44  virtual ~AbstractRawCounter() { }
45  };
46 
48  {
49  std::shared_ptr<SafeMsrHandle> msr;
50  uint64 msr_addr;
51  MsrHandleCounter(std::shared_ptr<SafeMsrHandle> msr_, uint64 msr_addr_) : msr(msr_), msr_addr(msr_addr_) { }
52  uint64 operator () ()
53  {
54  uint64 value = 0;
55  msr->read(msr_addr, &value);
56  return value;
57  }
58  };
59 
61  {
62  std::shared_ptr<ClientBW> clientBW;
63  ClientImcReadsCounter(std::shared_ptr<ClientBW> clientBW_) : clientBW(clientBW_) { }
64  uint64 operator () () { return clientBW->getImcReads(); }
65  };
66 
68  {
69  std::shared_ptr<ClientBW> clientBW;
70  ClientImcWritesCounter(std::shared_ptr<ClientBW> clientBW_) : clientBW(clientBW_) { }
71  uint64 operator () () { return clientBW->getImcWrites(); }
72  };
73 
75  {
76  std::shared_ptr<ClientBW> clientBW;
77  ClientIoRequestsCounter(std::shared_ptr<ClientBW> clientBW_) : clientBW(clientBW_) { }
78  uint64 operator () () { return clientBW->getIoRequests(); }
79  };
80 
82  {
83  std::shared_ptr<SafeMsrHandle> msr;
84  MBLCounter(std::shared_ptr<SafeMsrHandle> msr_) : msr(msr_) { }
85  uint64 operator () ()
86  {
87  msr->lock();
88  uint64 event = 3; // L3 Local External Bandwidth
89  uint64 msr_qm_evtsel = 0, value = 0;
90  msr->read(IA32_QM_EVTSEL, &msr_qm_evtsel);
91  //std::cout << "MBLCounter reading IA32_QM_EVTSEL 0x"<< std::hex << msr_qm_evtsel << std::dec << std::endl;
92  msr_qm_evtsel &= 0xfffffffffffffff0ULL;
93  msr_qm_evtsel |= event & ((1ULL << 8) - 1ULL);
94  //std::cout << "MBL event " << msr_qm_evtsel << "\n";
95  //std::cout << "MBLCounter writing IA32_QM_EVTSEL 0x"<< std::hex << msr_qm_evtsel << std::dec << std::endl;
96  msr->write(IA32_QM_EVTSEL, msr_qm_evtsel);
97  msr->read(IA32_QM_CTR, &value);
98  //std::cout << "MBLCounter reading IA32_QM_CTR "<< std::dec << value << std::dec << std::endl;
99  msr->unlock();
100  return value;
101  }
102  };
103 
105  {
106  std::shared_ptr<SafeMsrHandle> msr;
107  MBTCounter(std::shared_ptr<SafeMsrHandle> msr_) : msr(msr_) { }
108  uint64 operator () ()
109  {
110  msr->lock();
111  uint64 event = 2; // L3 Total External Bandwidth
112  uint64 msr_qm_evtsel = 0, value = 0;
113  msr->read(IA32_QM_EVTSEL, &msr_qm_evtsel);
114  //std::cout << "MBTCounter reading IA32_QM_EVTSEL 0x"<< std::hex << msr_qm_evtsel << std::dec << std::endl;
115  msr_qm_evtsel &= 0xfffffffffffffff0ULL;
116  msr_qm_evtsel |= event & ((1ULL << 8) - 1ULL);
117  //std::cout << "MBR event " << msr_qm_evtsel << "\n";
118  //std::cout << "MBTCounter writing IA32_QM_EVTSEL 0x"<< std::hex << msr_qm_evtsel << std::dec << std::endl;
119  msr->write(IA32_QM_EVTSEL, msr_qm_evtsel);
120  msr->read(IA32_QM_CTR, &value);
121  //std::cout << "MBTCounter reading IA32_QM_CTR "<< std::dec << value << std::dec << std::endl;
122  msr->unlock();
123  return value;
124  }
125  };
126 
127 private:
128  std::thread * UpdateThread;
129 
130  PCM_Util::Mutex CounterMutex;
131 
132  AbstractRawCounter * raw_counter;
133  uint64 extended_value;
134  uint64 last_raw_value;
135  uint64 counter_width;
136  uint32 watchdog_delay_ms;
137 
138  CounterWidthExtender(); // forbidden
140  CounterWidthExtender & operator = (const CounterWidthExtender &); // forbidden
141 
142 
143  uint64 internal_read()
144  {
145  uint64 result = 0, new_raw_value = 0;
146  CounterMutex.lock();
147 
148  new_raw_value = (*raw_counter)();
149  if (new_raw_value < last_raw_value)
150  {
151  extended_value += ((1ULL << counter_width) - last_raw_value) + new_raw_value;
152  }
153  else
154  {
155  extended_value += (new_raw_value - last_raw_value);
156  }
157 
158  last_raw_value = new_raw_value;
159 
160  result = extended_value;
161 
162  CounterMutex.unlock();
163  return result;
164  }
165 
166 public:
167 
168  CounterWidthExtender(AbstractRawCounter * raw_counter_, uint64 counter_width_, uint32 watchdog_delay_ms_);
169  virtual ~CounterWidthExtender();
170 
171  uint64 read() // read extended value
172  {
173  return internal_read();
174  }
175  void reset()
176  {
177  CounterMutex.lock();
178  extended_value = last_raw_value = (*raw_counter)();
179  CounterMutex.unlock();
180  }
181 };
182 
183 
184 #endif
Definition: width_extender.h:41
Definition: width_extender.h:67
Definition: mutex.h:14
Interface to access client bandwidth counters.
Some common utility routines.
Definition: width_extender.h:104
Main CPU counters header.
Definition: width_extender.h:74
Definition: width_extender.h:38
Definition: width_extender.h:81
Definition: width_extender.h:60
Definition: width_extender.h:47