Processor Counter Monitor
msr.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 // written by Roman Dementiev
14 // Austen Ott
15 
16 #ifndef CPUCounters_MSR_H
17 #define CPUCounters_MSR_H
18 
25 #include "types.h"
26 
27 #ifdef _MSC_VER
28 #include "windows.h"
29 #elif __APPLE__
30 #include <MSRAccessor.h>
31 #endif
32 
33 #include "mutex.h"
34 #include <memory>
35 
36 class MsrHandle
37 {
38 #ifdef _MSC_VER
39  HANDLE hDriver;
40 #elif __APPLE__
41  static MSRAccessor * driver;
42  static int num_handles;
43 #else
44  int32 fd;
45 #endif
46  uint32 cpu_id;
47  MsrHandle(); // forbidden
48  MsrHandle(const MsrHandle &); // forbidden
49  MsrHandle & operator = (const MsrHandle &); // forbidden
50 
51 public:
52  MsrHandle(uint32 cpu);
53  int32 read(uint64 msr_number, uint64 * value);
54  int32 write(uint64 msr_number, uint64 value);
55  int32 getCoreId() { return (int32)cpu_id; }
56 #ifdef __APPLE__
57  int32 buildTopology(uint32 num_cores, void *);
58  uint32 getNumInstances();
59  uint32 incrementNumInstances();
60  uint32 decrementNumInstances();
61 #endif
62  virtual ~MsrHandle();
63 };
64 
66 {
67  std::shared_ptr<MsrHandle> pHandle;
68  PCM_Util::Mutex mutex;
69 
70  SafeMsrHandle(const SafeMsrHandle &); // forbidden
71  SafeMsrHandle & operator = (const SafeMsrHandle &); // forbidden
72 
73 public:
74  SafeMsrHandle() { }
75 
76  SafeMsrHandle(uint32 core_id) : pHandle(new MsrHandle(core_id))
77  { }
78 
79  int32 read(uint64 msr_number, uint64 * value)
80  {
81  if (pHandle)
82  return pHandle->read(msr_number, value);
83 
84  *value = 0;
85 
86  return (int32)sizeof(uint64);
87  }
88 
89  int32 write(uint64 msr_number, uint64 value)
90  {
91  if (pHandle)
92  return pHandle->write(msr_number, value);
93 
94  return (int32)sizeof(uint64);
95  }
96  int32 getCoreId()
97  {
98  if (pHandle)
99  return pHandle->getCoreId();
100 
101  throw std::exception();
102  return -1;
103  }
104 
105  void lock()
106  {
107  mutex.lock();
108  }
109 
110  void unlock()
111  {
112  mutex.unlock();
113  }
114 
115 #ifdef __APPLE__
116  int32 buildTopology(uint32 num_cores, void * p)
117  {
118  if (pHandle)
119  return pHandle->buildTopology(num_cores, p);
120 
121  throw std::exception();
122  return 0;
123  }
124  uint32 getNumInstances()
125  {
126  if (pHandle)
127  return pHandle->getNumInstances();
128 
129  throw std::exception();
130  return 0;
131  }
132  uint32 incrementNumInstances()
133  {
134  if (pHandle)
135  return pHandle->incrementNumInstances();
136 
137  throw std::exception();
138  return 0;
139  }
140  uint32 decrementNumInstances()
141  {
142  if (pHandle)
143  return pHandle->decrementNumInstances();
144 
145  throw std::exception();
146  return 0;
147  }
148 #endif
149  virtual ~SafeMsrHandle()
150  { }
151 };
152 
153 #endif
Internal type and constant definitions.
Definition: mutex.h:14
Definition: msr.h:36
Definition: msr.h:65