forked from intel/pcm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_bw.cpp
226 lines (190 loc) · 7.28 KB
/
client_bw.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
Copyright (c) 2009-2013, Intel Corporation
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* 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.
* 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.
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.
*/
// written by Roman Dementiev,
// Patrick Konsor
//
#include <iostream>
#include <string.h>
#ifndef _MSC_VER
#include <sys/types.h>
#endif
#include <sys/stat.h>
#include <fcntl.h>
#include "pci.h"
#include "client_bw.h"
#ifndef _MSC_VER
#include <sys/mman.h>
#include <errno.h>
#endif
#ifdef _MSC_VER
#include <windows.h>
class PCMPmem : public WinPmem {
protected:
virtual int load_driver_()
{
SYSTEM_INFO sys_info;
ZeroMemory(&sys_info, sizeof(sys_info));
GetCurrentDirectory(MAX_PATH - 10, driver_filename);
GetNativeSystemInfo(&sys_info);
switch (sys_info.wProcessorArchitecture)
{
case PROCESSOR_ARCHITECTURE_AMD64:
wcscat_s(driver_filename, MAX_PATH, L"\\winpmem_x64.sys");
if (GetFileAttributes(driver_filename) == INVALID_FILE_ATTRIBUTES)
{
std::cout << "ERROR: winpmem_x64.sys not found in current directory. Download it from https://github.com/google/rekall/raw/master/tools/pmem/resources/winpmem/winpmem_x64.sys ." << std::endl;
std::cout << "ERROR: Memory bandwidth statistics will not be available." << std::endl;
}
break;
case PROCESSOR_ARCHITECTURE_INTEL:
wcscat_s(driver_filename, MAX_PATH, L"\\winpmem_x86.sys");
if (GetFileAttributes(driver_filename) == INVALID_FILE_ATTRIBUTES)
{
std::cout << "ERROR: winpmem_x86.sys not found in current directory. Download it from https://github.com/google/rekall/raw/master/tools/pmem/resources/winpmem/winpmem_x86.sys ." << std::endl;
std::cout << "ERROR: Memory bandwidth statistics will not be available." << std::endl;
}
break;
default:
return -1;
}
return 1;
}
virtual int write_crashdump_header_(struct PmemMemoryInfo * info)
{
return -1;
}
};
ClientBW::ClientBW() : pmem(new PCMPmem())
{
pmem->install_driver(false);
pmem->set_acquisition_mode(PMEM_MODE_IOSPACE);
PciHandleType imcHandle(0, 0, 0, 0); // memory controller device coordinates: domain 0, bus 0, device 0, function 0
uint64 imcbar = 0;
imcHandle.read64(PCM_CLIENT_IMC_BAR_OFFSET, &imcbar);
// std::cout << "DEBUG: imcbar="<<std::hex << imcbar <<std::endl;
if (!imcbar)
{
std::cerr << "ERROR: imcbar is zero." << std::endl;
throw std::exception();
}
startAddr = imcbar & (~(4096ULL - 1ULL)); // round down to 4K
}
uint64 ClientBW::getImcReads()
{
mutex.lock();
uint32 res = pmem->read32(startAddr + PCM_CLIENT_IMC_DRAM_DATA_READS);
mutex.unlock();
return (uint64)res;
}
uint64 ClientBW::getImcWrites()
{
mutex.lock();
uint32 res = pmem->read32(startAddr + PCM_CLIENT_IMC_DRAM_DATA_WRITES);
mutex.unlock();
return (uint64)res;
}
uint64 ClientBW::getIoRequests()
{
mutex.lock();
uint32 res = pmem->read32(startAddr + PCM_CLIENT_IMC_DRAM_IO_REQESTS);
mutex.unlock();
return (uint64)res;
}
ClientBW::~ClientBW()
{
pmem->uninstall_driver();
}
#elif __APPLE__
#include "PCIDriverInterface.h"
#define CLIENT_BUS 0
#define CLIENT_DEV 0
#define CLIENT_FUNC 0
#define CLIENT_BAR_MASK 0x0007FFFFF8000LL
#define CLIENT_EVENT_BASE 0x5000
ClientBW::ClientBW()
{
uint64_t bar = 0;
uint32_t pci_address = FORM_PCI_ADDR(CLIENT_BUS, CLIENT_DEV, CLIENT_FUNC, PCM_CLIENT_IMC_BAR_OFFSET);
PCIDriver_read64(pci_address, &bar);
uint64_t physical_address = (bar & CLIENT_BAR_MASK) + CLIENT_EVENT_BASE;//bar & (~(4096-1));
mmapAddr = NULL;
if (physical_address) {
PCIDriver_mapMemory((uint32_t)physical_address, (uint8_t **)&mmapAddr);
}
}
uint64 ClientBW::getImcReads()
{
uint32_t val = 0;
PCIDriver_readMemory32((uint8_t *)mmapAddr + PCM_CLIENT_IMC_DRAM_DATA_READS - CLIENT_EVENT_BASE, &val);
return (uint64_t)val;
}
uint64 ClientBW::getImcWrites()
{
uint32_t val = 0;
PCIDriver_readMemory32((uint8_t *)mmapAddr + PCM_CLIENT_IMC_DRAM_DATA_WRITES - CLIENT_EVENT_BASE, &val);
return (uint64_t)val;
}
uint64 ClientBW::getIoRequests()
{
uint32_t val = 0;
PCIDriver_readMemory32((uint8_t *)mmapAddr + PCM_CLIENT_IMC_DRAM_IO_REQESTS - CLIENT_EVENT_BASE, &val);
return (uint64_t)val;
}
ClientBW::~ClientBW()
{
PCIDriver_unmapMemory((uint8_t *)mmapAddr);
}
#else
#if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)
// Linux implementation
ClientBW::ClientBW() :
fd(-1),
mmapAddr(NULL)
{
int handle = ::open("/dev/mem", O_RDONLY);
if (handle < 0) throw std::exception();
fd = handle;
PciHandleType imcHandle(0, 0, 0, 0); // memory controller device coordinates: domain 0, bus 0, device 0, function 0
uint64 imcbar = 0;
imcHandle.read64(PCM_CLIENT_IMC_BAR_OFFSET, &imcbar);
// std::cout << "DEBUG: imcbar="<<std::hex << imcbar <<std::endl;
if (!imcbar)
{
std::cerr << "ERROR: imcbar is zero." << std::endl;
throw std::exception();
}
uint64 startAddr = imcbar & (~(4096 - 1)); // round down to 4K
// std::cout << "DEBUG: startAddr="<<std::hex << startAddr <<std::endl;
mmapAddr = (char *)mmap(NULL, PCM_CLIENT_IMC_MMAP_SIZE, PROT_READ, MAP_SHARED, fd, startAddr);
if (mmapAddr == MAP_FAILED)
{
std::cout << "mmap failed: errno is " << errno << " (" << strerror(errno) << ")" << std::endl;
throw std::exception();
}
}
uint64 ClientBW::getImcReads()
{
return *((uint32 *)(mmapAddr + PCM_CLIENT_IMC_DRAM_DATA_READS));
}
uint64 ClientBW::getImcWrites()
{
return *((uint32 *)(mmapAddr + PCM_CLIENT_IMC_DRAM_DATA_WRITES));
}
uint64 ClientBW::getIoRequests()
{
return *((uint32 *)(mmapAddr + PCM_CLIENT_IMC_DRAM_IO_REQESTS));
}
ClientBW::~ClientBW()
{
if (mmapAddr) munmap(mmapAddr, PCM_CLIENT_IMC_MMAP_SIZE);
if (fd >= 0) ::close(fd);
}
#endif
#endif