1 #ifndef CPUCounters_LSPCI_H 2 #define CPUCounters_LSPCI_H 10 #define PCI_IDS_PATH "pci.ids" 11 #define PCI_IDS_NOT_FOUND "pci.ids file is not available. Download it from" \ 12 " https://raw.githubusercontent.com/pciutils/pciids/master/pci.ids." 13 #elif defined (__FreeBSD__) || defined(__DragonFly__) 14 #define PCI_IDS_PATH "/usr/local/share/pciids/pci.ids" 15 #define PCI_IDS_NOT_FOUND "/usr/local/share/pciids/pci.ids file is not available." \ 16 " Ensure that the \"pciids\" package is properly installed or download" \ 17 " https://raw.githubusercontent.com/pciutils/pciids/master/pci.ids and" \ 18 " copy it to the current directory." 20 #define PCI_IDS_PATH "/usr/share/hwdata/pci.ids" 21 #define PCI_IDS_NOT_FOUND "/usr/share/hwdata/pci.ids file is not available." \ 22 " Ensure that the \"hwdata\" package is properly installed or download" \ 23 " https://raw.githubusercontent.com/pciutils/pciids/master/pci.ids and" \ 24 " copy it to the current directory." 29 typedef uint32_t h_id;
30 typedef uint32_t v_id;
31 typedef std::map<std::pair<h_id,v_id>,uint64_t> ctr_data;
32 typedef std::vector<ctr_data> stack_content;
33 typedef std::vector<stack_content> result_content;
37 virtual uint64_t get_event_select()
const = 0;
38 virtual void set_event_select(uint64_t value) = 0;
39 virtual uint64_t get_umask()
const = 0;
40 virtual void set_umask(uint64_t value) = 0;
41 virtual uint64_t get_reset()
const = 0;
42 virtual void set_reset(uint64_t value) = 0;
43 virtual uint64_t get_edge()
const = 0;
44 virtual void set_edge(uint64_t value) = 0;
45 virtual uint64_t get_ov_en()
const = 0;
46 virtual void set_ov_en(uint64_t value) = 0;
47 virtual uint64_t get_enable()
const = 0;
48 virtual void set_enable(uint64_t value) = 0;
49 virtual uint64_t get_invert()
const = 0;
50 virtual void set_invert(uint64_t value) = 0;
51 virtual uint64_t get_thresh()
const = 0;
52 virtual void set_thresh(uint64_t value) = 0;
53 virtual uint64_t get_ch_mask()
const = 0;
54 virtual void set_ch_mask(uint64_t value) = 0;
55 virtual uint64_t get_fc_mask()
const = 0;
56 virtual void set_fc_mask(uint64_t value) = 0;
57 virtual uint64_t get_ccr_value()
const = 0;
58 virtual void set_ccr_value(uint64_t value) = 0;
67 virtual uint64_t get_event_select()
const {
68 return (*ccr_value & 0xFF);
70 virtual void set_event_select(uint64_t value) {
73 virtual uint64_t get_umask()
const {
74 return ((*ccr_value >> 8) & 0xFF);
76 virtual void set_umask(uint64_t value) {
77 *ccr_value |= (value << 8);
79 virtual uint64_t get_reset()
const {
80 return ((*ccr_value >> 17) & 0x01);
82 virtual void set_reset(uint64_t value) {
83 *ccr_value |= (value << 17);
85 virtual uint64_t get_edge()
const {
86 return ((*ccr_value >> 18) & 0x01);
88 virtual void set_edge(uint64_t value) {
89 *ccr_value |= (value << 18);
91 virtual uint64_t get_ov_en()
const {
92 return ((*ccr_value >> 20) & 0x01);
94 virtual void set_ov_en(uint64_t value) {
95 *ccr_value |= (value << 20);
97 virtual uint64_t get_enable()
const {
98 return ((*ccr_value >> 22) & 0x01);
100 virtual void set_enable(uint64_t value) {
101 *ccr_value |= (value << 22);
103 virtual uint64_t get_invert()
const {
104 return ((*ccr_value >> 23) & 0x01);
106 virtual void set_invert(uint64_t value) {
107 *ccr_value |= (value << 23);
109 virtual uint64_t get_thresh()
const {
110 return ((*ccr_value >> 24) & 0xFFF);
112 virtual void set_thresh(uint64_t value) {
113 *ccr_value |= (value << 24);
115 virtual uint64_t get_ch_mask()
const {
116 return ((*ccr_value >> 36) & 0xFF);
118 virtual void set_ch_mask(uint64_t value) {
119 *ccr_value |= (value << 36);
121 virtual uint64_t get_fc_mask()
const {
122 return ((*ccr_value >> 44) & 0x07);
124 virtual void set_fc_mask(uint64_t value) {
125 *ccr_value |= (value << 44);
127 virtual uint64_t get_ccr_value()
const {
130 virtual void set_ccr_value(uint64_t value) {
135 uint64_t* ccr_value = NULL;
143 virtual uint64_t get_event_select()
const {
144 return (*ccr_value & 0xFF);
146 virtual void set_event_select(uint64_t value) {
149 virtual uint64_t get_umask()
const {
150 return ((*ccr_value >> 8) & 0xFF);
152 virtual void set_umask(uint64_t value) {
153 *ccr_value |= (value << 8);
155 virtual uint64_t get_reset()
const {
156 return ((*ccr_value >> 17) & 0x01);
158 virtual void set_reset(uint64_t value) {
159 *ccr_value |= (value << 17);
161 virtual uint64_t get_edge()
const {
162 return ((*ccr_value >> 18) & 0x01);
164 virtual void set_edge(uint64_t value) {
165 *ccr_value |= (value << 18);
167 virtual uint64_t get_ov_en()
const {
168 return ((*ccr_value >> 20) & 0x01);
170 virtual void set_ov_en(uint64_t value) {
171 *ccr_value |= (value << 20);
173 virtual uint64_t get_enable()
const {
174 return ((*ccr_value >> 22) & 0x01);
176 virtual void set_enable(uint64_t value) {
177 *ccr_value |= (value << 22);
179 virtual uint64_t get_invert()
const {
180 return ((*ccr_value >> 23) & 0x01);
182 virtual void set_invert(uint64_t value) {
183 *ccr_value |= (value << 23);
185 virtual uint64_t get_thresh()
const {
186 return ((*ccr_value >> 24) & 0xFFF);
188 virtual void set_thresh(uint64_t value) {
189 *ccr_value |= (value << 24);
191 virtual uint64_t get_ch_mask()
const {
192 return ((*ccr_value >> 36) & 0xFFF);
194 virtual void set_ch_mask(uint64_t value) {
195 *ccr_value |= (value << 36);
197 virtual uint64_t get_fc_mask()
const {
198 return ((*ccr_value >> 48) & 0x07);
200 virtual void set_fc_mask(uint64_t value) {
201 *ccr_value |= (value << 48);
203 virtual uint64_t get_ccr_value()
const {
206 virtual void set_ccr_value(uint64_t value) {
211 uint64_t* ccr_value = NULL;
217 bdf () : busno(0), devno(0), funcno(0) {}
233 uint8_t primary_bus_number;
234 uint8_t secondary_bus_number;
235 uint8_t subordinate_bus_number;
245 uint16_t link_speed : 4;
246 uint16_t link_width : 6;
247 uint16_t undefined : 1;
248 uint16_t link_trained : 1;
255 pci () : exist(
false), offset_0(0), header_type(0), offset_18(0), link_info(0) {}
259 std::string h_event_name;
260 std::string v_event_name;
267 std::vector<result_content>
data;
273 struct pci root_pci_dev;
274 std::vector<struct pci> child_pci_devs;
277 std::string stack_name{};
278 std::vector<uint64_t> values{};
279 bool flipped =
false;
281 uint32_t socket_id{};
287 struct pci root_pci_dev;
289 std::vector<struct pci> child_pci_devs;
293 std::vector<struct iio_bifurcated_part> parts{};
294 uint32_t iio_unit_id{};
295 std::string stack_name{};
296 std::vector<uint64_t> values{};
297 bool flipped =
false;
304 return lh.iio_unit_id < rh.iio_unit_id;
308 std::vector<struct iio_stack> stacks{};
309 uint32_t socket_id{};
312 bool operator < (
const bdf &l,
const bdf &r) {
313 if (l.busno < r.busno)
315 if (l.busno > r.busno)
317 if (l.devno < r.devno)
319 if (l.devno > r.devno)
321 if (l.funcno < r.funcno)
323 if (l.funcno > r.funcno)
329 void probe_capability_pci_express(
struct pci *p, uint32_t cap_ptr)
345 PciHandleType h(0, p->bdf.busno, p->bdf.devno, p->bdf.funcno);
346 h.read32(cap_ptr, &value);
348 if (cap.id != 0x10 && cap.next != 0x00) {
349 probe_capability_pci_express(p, cap.cap_ptr);
351 if (cap.id == 0x10) {
352 h.read32(cap_ptr+0x10, &value);
353 p->link_info = value;
358 bool probe_pci(
struct pci *p)
362 struct bdf *
bdf = &p->bdf;
363 if (PciHandleType::exists(0, bdf->busno, bdf->devno, bdf->funcno)) {
364 PciHandleType h(0, bdf->busno, bdf->devno, bdf->funcno);
366 h.read32(0x0, &value);
368 if (value != (std::numeric_limits<unsigned int>::max)()) {
370 h.read32(0xc, &value);
371 p->header_type = (value >> 16) & 0x7f;
372 if (p->header_type == 0) {
374 h.read32(0x4, &value);
376 if (value & 0x100000) {
378 h.read32(0x34, &value);
379 probe_capability_pci_express(p, value);
381 }
else if (p->header_type == 1) {
382 h.read32(0x18, &value);
383 p->offset_18 = value;
396 typedef std::pair< std::map<int, std::string> ,std::map< int, std::map<int, std::string> > > PCIDB;
398 void print_pci(
struct pci p,
const PCIDB & pciDB)
400 printf(
"Parent bridge info:");
401 printf(
"%x:%x.%d [%04x:%04x] %s %s %d P:%x S:%x S:%x ",
402 p.bdf.busno, p.bdf.devno, p.bdf.funcno,
403 p.vendor_id, p.device_id,
404 (pciDB.first.count(p.vendor_id) > 0)?pciDB.first.at(p.vendor_id).c_str():
"unknown vendor",
405 (pciDB.second.count(p.vendor_id) > 0 && pciDB.second.at(p.vendor_id).count(p.device_id) > 0)?pciDB.second.at(p.vendor_id).at(p.device_id).c_str():
"unknown device",
407 p.primary_bus_number, p.secondary_bus_number, p.subordinate_bus_number);
408 printf(
"Device info:");
409 printf(
"%x:%x.%d [%04x:%04x] %s %s %d Gen%d x%d\n",
410 p.bdf.busno, p.bdf.devno, p.bdf.funcno,
411 p.vendor_id, p.device_id,
412 (pciDB.first.count(p.vendor_id) > 0)?pciDB.first.at(p.vendor_id).c_str():
"unknown vendor",
413 (pciDB.second.count(p.vendor_id) > 0 && pciDB.second.at(p.vendor_id).count(p.device_id) > 0)?pciDB.second.at(p.vendor_id).at(p.device_id).c_str():
"unknown device",
415 p.link_speed, p.link_width);
418 void load_PCIDB(PCIDB & pciDB)
420 std::ifstream in(PCI_IDS_PATH);
421 std::string line, item;
433 std::cerr << PCI_IDS_NOT_FOUND <<
"\n";
439 while (std::getline(in, line)) {
441 if (line.size() == 0 || line[0] ==
'#')
444 if (line[0] ==
'\t' && line[1] ==
'\t')
451 int deviceID = stoi(line.substr(1,4),0,16);
453 pciDB.second[vendorID][deviceID] = line.substr(7);
457 vendorID = stoi(line.substr(0,4),0,16);
458 pciDB.first[vendorID] = line.substr(6);
Main CPU counters header.
Definition: pcm-iio.cpp:146