-
Notifications
You must be signed in to change notification settings - Fork 480
/
Copy pathMSRAccessor.cpp
166 lines (138 loc) · 4.99 KB
/
MSRAccessor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2012, Intel Corporation
// written by Austen Ott
//
#include "MSRAccessor.h"
#include <exception>
#include <iostream>
#include <iomanip>
using namespace std;
MSRAccessor::MSRAccessor()
{
service = IOServiceGetMatchingService(kIOMainPortDefault,
IOServiceMatching(kPcmMsrDriverClassName));
openConnection();
}
int32_t MSRAccessor::buildTopology(uint32_t num_cores, void* pTopos)
{
size_t topology_struct_size = sizeof(TopologyEntry)*num_cores;
kern_return_t ret = IOConnectCallStructMethod(connect, kBuildTopology,
NULL, 0,
pTopos, &topology_struct_size);
return (ret == KERN_SUCCESS) ? 0 : -1;
}
int32_t MSRAccessor::read(uint32_t core_num, uint64_t msr_num, uint64_t * value)
{
pcm_msr_data_t idatas, odatas;
size_t struct_size = sizeof(pcm_msr_data_t);
idatas.msr_num = (uint32_t)msr_num;
idatas.cpu_num = core_num;
kern_return_t ret = IOConnectCallStructMethod(connect, kReadMSR,
&idatas, struct_size,
&odatas, &struct_size);
if(ret == KERN_SUCCESS)
{
*value = odatas.value;
return sizeof(uint64_t);
} else {
return -1;
}
}
int32_t MSRAccessor::write(uint32_t core_num, uint64_t msr_num, uint64_t value){
pcm_msr_data_t idatas;
idatas.value = value;
idatas.msr_num = (uint32_t)msr_num;
idatas.cpu_num = core_num;
kern_return_t ret = IOConnectCallStructMethod(connect, kWriteMSR,
&idatas, sizeof(pcm_msr_data_t),
NULL, NULL);
if(ret == KERN_SUCCESS)
{
return sizeof(uint64_t);
} else {
return -1;
}
}
uint32_t MSRAccessor::getNumInstances()
{
kern_return_t kernResult;
uint32_t output_count = 1;
uint64_t knum_insts = 0;
kernResult = IOConnectCallScalarMethod(connect,
kGetNumInstances,
NULL, 0,
&knum_insts, &output_count);
if (kernResult != KERN_SUCCESS)
{
cerr << "IOConnectCallScalarMethod returned 0x" << hex << setw(8) << kernResult << dec << endl;
}
// TODO add error handling; also, number-of-instance related
// functions may go away as they do not appear to be used.
return knum_insts;
}
uint32_t MSRAccessor::incrementNumInstances()
{
kern_return_t kernResult;
uint32_t output_count = 1;
uint64_t knum_insts = 0;
kernResult = IOConnectCallScalarMethod(connect,
kIncrementNumInstances,
NULL, 0,
&knum_insts, &output_count);
if (kernResult != KERN_SUCCESS)
{
cerr << "IOConnectCallScalarMethod returned 0x" << hex << setw(8) << kernResult << dec << endl;
}
// TODO add error handling; also, these functions may go away as
// they do not appear to be used.
return knum_insts;
}
uint32_t MSRAccessor::decrementNumInstances()
{
kern_return_t kernResult;
uint32_t output_count = 1;
uint64_t knum_insts = 0;
kernResult = IOConnectCallScalarMethod(connect, kDecrementNumInstances,
NULL, 0,
&knum_insts, &output_count);
if (kernResult != KERN_SUCCESS)
{
cerr << "IOConnectCallScalarMethod returned 0x" << hex << setw(8) << kernResult << dec << endl;
}
// TODO add error handling; also, these functions may go away as
// they do not appear to be used.
return knum_insts;
}
MSRAccessor::~MSRAccessor()
{
closeConnection();
}
kern_return_t MSRAccessor::openConnection()
{
kern_return_t kernResult = IOServiceOpen(service, mach_task_self(), 0, &connect);
if (kernResult != KERN_SUCCESS)
{
cerr << "IOServiceOpen returned 0x" << hex << setw(8) << kernResult << dec << endl;
} else {
kernResult = IOConnectCallScalarMethod(connect, kOpenDriver, NULL, 0, NULL, NULL);
if (kernResult != KERN_SUCCESS)
{
cerr << "kOpenDriver returned 0x" << hex << setw(8) << kernResult << dec << endl;
}
}
return kernResult;
}
void MSRAccessor::closeConnection()
{
kern_return_t kernResult = IOConnectCallScalarMethod(connect, kCloseDriver,
NULL, 0, NULL, NULL);
if (kernResult != KERN_SUCCESS)
{
cerr << "kCloseDriver returned 0x" << hex << setw(8) << kernResult << dec << endl;
}
kernResult = IOServiceClose(connect);
if (kernResult != KERN_SUCCESS)
{
cerr << "IOServiceClose returned 0x" << hex << setw(8) << kernResult << dec << endl;
}
}