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