Intel(r) Performance 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 
34 class MsrHandle
35 {
36 #ifdef _MSC_VER
37  HANDLE hDriver;
38 #elif __APPLE__
39  static MSRAccessor* driver;
40  static int num_handles;
41 #else
42  int32 fd;
43 #endif
44  uint32 cpu_id;
45  MsrHandle(); // forbidden
46  MsrHandle(MsrHandle &); // forbidden
47 
48 public:
49  MsrHandle(uint32 cpu);
50  int32 read(uint64 msr_number, uint64 * value);
51  int32 write(uint64 msr_number, uint64 value);
52  int32 getCoreId() { return cpu_id; }
53 #ifdef __APPLE__
54  int32 buildTopology(uint32 num_cores, void*);
55  uint32 getNumInstances();
56  uint32 incrementNumInstances();
57  uint32 decrementNumInstances();
58 #endif
59  virtual ~MsrHandle();
60 };
61 
63 {
64  MsrHandle * pHandle;
65 
66  SafeMsrHandle(SafeMsrHandle &); // forbidden
67 
68  public:
69  SafeMsrHandle() : pHandle(NULL) {}
70 
71  SafeMsrHandle(uint32 core_id)
72  {
73  pHandle = new MsrHandle(core_id);
74  }
75 
76  int32 read(uint64 msr_number, uint64 * value)
77  {
78  if(pHandle)
79  return pHandle->read(msr_number, value);
80 
81  *value = 0;
82 
83  return sizeof(uint64);
84  }
85 
86  int32 write(uint64 msr_number, uint64 value)
87  {
88  if(pHandle)
89  return pHandle->write(msr_number, value);
90 
91  return sizeof(uint64);
92  }
93  int32 getCoreId()
94  {
95  if(pHandle)
96  return pHandle->getCoreId();
97 
98  throw std::exception();
99  return -1;
100  }
101 #ifdef __APPLE__
102  int32 buildTopology(uint32 num_cores, void* p)
103  {
104  if(pHandle)
105  return pHandle->buildTopology(num_cores,p);
106 
107  throw std::exception();
108  return 0;
109  }
110  uint32 getNumInstances()
111  {
112  if(pHandle)
113  return pHandle->getNumInstances();
114 
115  throw std::exception();
116  return 0;
117  }
118  uint32 incrementNumInstances()
119  {
120  if(pHandle)
121  return pHandle->incrementNumInstances();
122 
123  throw std::exception();
124  return 0;
125  }
126  uint32 decrementNumInstances()
127  {
128  if(pHandle)
129  return pHandle->decrementNumInstances();
130 
131  throw std::exception();
132  return 0;
133  }
134 #endif
135  virtual ~SafeMsrHandle()
136  {
137  if(pHandle)
138  {
139  delete pHandle;
140  pHandle = NULL;
141  }
142  }
143 };
144 
145 #endif
Internal type and constant definitions.
Definition: msr.h:34
Definition: msr.h:62