1 #ifndef CPUCounters_LSPCI_H
2 #define CPUCounters_LSPCI_H
9 #define PCI_IDS_PATH "pci.ids"
10 #define PCI_IDS_NOT_FOUND "pci.ids file is not available. Download it from" \
11 " https://raw.githubusercontent.com/pciutils/pciids/master/pci.ids."
12 #elif defined (__FreeBSD__) || defined(__DragonFly__)
13 #define PCI_IDS_PATH "/usr/local/share/pciids/pci.ids"
14 #define PCI_IDS_NOT_FOUND "/usr/local/share/pciids/pci.ids file is not available." \
15 " Ensure that the \"pciids\" package is properly installed or download" \
16 " https://raw.githubusercontent.com/pciutils/pciids/master/pci.ids and" \
17 " copy it to the current directory."
19 #define PCI_IDS_PATH "/usr/share/hwdata/pci.ids"
20 #define PCI_IDS_NOT_FOUND "/usr/share/hwdata/pci.ids file is not available." \
21 " Ensure that the \"hwdata\" package is properly installed or download" \
22 " https://raw.githubusercontent.com/pciutils/pciids/master/pci.ids and" \
23 " copy it to the current directory."
27 typedef uint32_t h_id;
28 typedef uint32_t v_id;
29 typedef map<std::pair<h_id,v_id>,uint64_t> ctr_data;
30 typedef vector<ctr_data> stack_content;
31 typedef vector<stack_content> result_content;
52 uint8_t primary_bus_number;
53 uint8_t secondary_bus_number;
54 uint8_t subordinate_bus_number;
64 uint16_t link_speed : 4;
65 uint16_t link_width : 6;
66 uint16_t undefined : 1;
67 uint16_t link_trained : 1;
85 vector<result_content>
data;
91 struct pci root_pci_dev;
92 vector<struct pci> child_pci_devs;
96 vector<uint64_t> values;
101 bool operator < (
const bdf &l,
const bdf &r) {
102 if (l.busno < r.busno)
104 if (l.busno > r.busno)
106 if (l.devno < r.devno)
108 if (l.devno > r.devno)
110 if (l.funcno < r.funcno)
112 if (l.funcno > r.funcno)
118 void probe_capability_pci_express(
struct pci *p, uint32_t cap_ptr)
134 PciHandleType h(0, p->bdf.busno, p->bdf.devno, p->bdf.funcno);
135 h.read32(cap_ptr, &value);
137 if (cap.id != 0x10 && cap.next != 0x00) {
138 probe_capability_pci_express(p, cap.cap_ptr);
140 if (cap.id == 0x10) {
141 h.read32(cap_ptr+0x10, &value);
142 p->link_info = value;
147 void probe_pci(
struct pci *p)
150 struct bdf *
bdf = &p->bdf;
151 if (PciHandleType::exists(0, bdf->busno, bdf->devno, bdf->funcno)) {
153 PciHandleType h(0, bdf->busno, bdf->devno, bdf->funcno);
154 h.read32(0x0, &value);
156 h.read32(0xc, &value);
157 p->header_type = (value >> 16) & 0x7f;
158 if (p->header_type == 0) {
159 h.read32(0x4, &value);
160 if (value & 0x100000) {
161 h.read32(0x34, &value);
162 probe_capability_pci_express(p, value);
164 }
else if (p->header_type == 1) {
165 h.read32(0x18, &value);
166 p->offset_18 = value;
177 typedef std::pair< std::map<int, std::string> ,std::map< int, std::map<int, std::string> > > PCIDB;
179 void print_pci(
struct pci p,
const PCIDB & pciDB)
181 printf(
"Parent bridge info:");
182 printf(
"%x:%x.%d [%04x:%04x] %s %s %d P:%x S:%x S:%x ",
183 p.bdf.busno, p.bdf.devno, p.bdf.funcno,
184 p.vendor_id, p.device_id,
185 (pciDB.first.count(p.vendor_id) > 0)?pciDB.first.at(p.vendor_id).c_str():
"unknown vendor",
186 (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",
188 p.primary_bus_number, p.secondary_bus_number, p.subordinate_bus_number);
189 printf(
"Device info:");
190 printf(
"%x:%x.%d [%04x:%04x] %s %s %d Gen%d x%d\n",
191 p.bdf.busno, p.bdf.devno, p.bdf.funcno,
192 p.vendor_id, p.device_id,
193 (pciDB.first.count(p.vendor_id) > 0)?pciDB.first.at(p.vendor_id).c_str():
"unknown vendor",
194 (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",
196 p.link_speed, p.link_width);
199 void load_PCIDB(PCIDB & pciDB)
201 std::ifstream in(PCI_IDS_PATH);
202 std::string line, item;
214 std::cerr << PCI_IDS_NOT_FOUND << endl;
220 while (std::getline(in, line)) {
222 if (line.size() == 0 || line[0] ==
'#')
225 if (line[0] ==
'\t' && line[1] ==
'\t')
232 int deviceID = stoi(line.substr(1,4),0,16);
234 pciDB.second[vendorID][deviceID] = line.substr(7);
238 vendorID = stoi(line.substr(0,4),0,16);
239 pciDB.first[vendorID] = line.substr(6);
Main CPU counters header.
Definition: pcm-iio.cpp:56