@@ -17,6 +17,8 @@ limitations under the License.
1717package pci
1818
1919import (
20+ "bytes"
21+ "encoding/json"
2022 "fmt"
2123 "os"
2224 "path/filepath"
@@ -68,6 +70,11 @@ func readPciDevInfo(devPath string) (*nfdv1alpha1.InstanceFeature, error) {
6870 return nfdv1alpha1 .NewInstanceFeature (attrs ), nil
6971}
7072
73+ type DevGroupedEntry struct {
74+ Count int
75+ Bytes []byte
76+ }
77+
7178// detectPci detects available PCI devices and retrieves their device attributes.
7279// An error is returned if reading any of the mandatory attributes fails.
7380func detectPci () ([]nfdv1alpha1.InstanceFeature , error ) {
@@ -80,13 +87,35 @@ func detectPci() ([]nfdv1alpha1.InstanceFeature, error) {
8087
8188 // Iterate over devices
8289 devInfo := make ([]nfdv1alpha1.InstanceFeature , 0 , len (devices ))
90+ devGrouped := make (map [string ]map [string ]DevGroupedEntry )
8391 for _ , device := range devices {
8492 info , err := readPciDevInfo (filepath .Join (sysfsBasePath , device .Name ()))
8593 if err != nil {
8694 klog .ErrorS (err , "failed to read PCI device info" )
8795 continue
8896 }
89- devInfo = append (devInfo , * info )
97+
98+ b , err := json .Marshal (info .Attributes )
99+ if err != nil {
100+ return nil , err
101+ }
102+
103+ if entry , ok := devGrouped [info .Attributes ["vendor" ]][info .Attributes ["device" ]]; ! ok {
104+ devGrouped [info .Attributes ["vendor" ]] = make (map [string ]DevGroupedEntry )
105+ devGrouped [info .Attributes ["vendor" ]][info .Attributes ["device" ]] = DevGroupedEntry {Bytes : b , Count : 1 }
106+ devInfo = append (devInfo , * info )
107+ } else {
108+ result := bytes .Compare (b , devGrouped [info .Attributes ["vendor" ]][info .Attributes ["device" ]].Bytes )
109+ if result == 0 {
110+ entry .Count += 1
111+ devGrouped [info .Attributes ["vendor" ]][info .Attributes ["device" ]] = entry
112+ }
113+ }
114+ }
115+
116+ for _ , dev := range devInfo {
117+ entry := devGrouped [dev .Attributes ["vendor" ]][dev .Attributes ["device" ]]
118+ dev .Attributes ["count" ] = string (entry .Count )
90119 }
91120
92121 return devInfo , nil
0 commit comments