@@ -17,6 +17,8 @@ limitations under the License.
17
17
package pci
18
18
19
19
import (
20
+ "bytes"
21
+ "encoding/json"
20
22
"fmt"
21
23
"os"
22
24
"path/filepath"
@@ -68,6 +70,11 @@ func readPciDevInfo(devPath string) (*nfdv1alpha1.InstanceFeature, error) {
68
70
return nfdv1alpha1 .NewInstanceFeature (attrs ), nil
69
71
}
70
72
73
+ type DevGroupedEntry struct {
74
+ Count int
75
+ Bytes []byte
76
+ }
77
+
71
78
// detectPci detects available PCI devices and retrieves their device attributes.
72
79
// An error is returned if reading any of the mandatory attributes fails.
73
80
func detectPci () ([]nfdv1alpha1.InstanceFeature , error ) {
@@ -80,13 +87,35 @@ func detectPci() ([]nfdv1alpha1.InstanceFeature, error) {
80
87
81
88
// Iterate over devices
82
89
devInfo := make ([]nfdv1alpha1.InstanceFeature , 0 , len (devices ))
90
+ devGrouped := make (map [string ]map [string ]DevGroupedEntry )
83
91
for _ , device := range devices {
84
92
info , err := readPciDevInfo (filepath .Join (sysfsBasePath , device .Name ()))
85
93
if err != nil {
86
94
klog .ErrorS (err , "failed to read PCI device info" )
87
95
continue
88
96
}
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 )
90
119
}
91
120
92
121
return devInfo , nil
0 commit comments