@@ -40,6 +40,9 @@ const NvFeature = "nv"
40
40
// NumaFeature is the name of the feature set that holds all NUMA related features.
41
41
const NumaFeature = "numa"
42
42
43
+ // SwapFeature is the name of the feature set that holds all Swap related features
44
+ const SwapFeature = "swap"
45
+
43
46
// memorySource implements the FeatureSource and LabelSource interfaces.
44
47
type memorySource struct {
45
48
features * nfdv1alpha1.Features
@@ -68,6 +71,11 @@ func (s *memorySource) GetLabels() (source.FeatureLabels, error) {
68
71
labels ["numa" ] = true
69
72
}
70
73
74
+ // Swap
75
+ if isSwap , ok := features .Attributes [SwapFeature ].Elements ["enabled" ]; ok && isSwap == "true" {
76
+ labels ["swap" ] = true
77
+ }
78
+
71
79
// NVDIMM
72
80
if len (features .Instances [NvFeature ].Elements ) > 0 {
73
81
labels ["nv.present" ] = true
@@ -93,6 +101,13 @@ func (s *memorySource) Discover() error {
93
101
s .features .Attributes [NumaFeature ] = nfdv1alpha1.AttributeFeatureSet {Elements : numa }
94
102
}
95
103
104
+ // Detect Swap
105
+ if swap , err := detectSwap (); err != nil {
106
+ klog .ErrorS (err , "failed to detect Swap nodes" )
107
+ } else {
108
+ s .features .Attributes [SwapFeature ] = nfdv1alpha1.AttributeFeatureSet {Elements : swap }
109
+ }
110
+
96
111
// Detect NVDIMM
97
112
if nv , err := detectNv (); err != nil {
98
113
klog .ErrorS (err , "failed to detect nvdimm devices" )
@@ -113,6 +128,20 @@ func (s *memorySource) GetFeatures() *nfdv1alpha1.Features {
113
128
return s .features
114
129
}
115
130
131
+ // detectSwap detects Swap node information
132
+ func detectSwap () (map [string ]string , error ) {
133
+ procBasePath := hostpath .ProcDir .Path ("swaps" )
134
+ lines , err := getNumberOfLinesFromFile (procBasePath )
135
+ if err != nil {
136
+ return nil , fmt .Errorf ("failed to read swaps file: %w" , err )
137
+ }
138
+ // /proc/swaps has a header row
139
+ // If there is more than a header then we assume we have swap.
140
+ return map [string ]string {
141
+ "enabled" : strconv .FormatBool (lines > 1 ),
142
+ }, nil
143
+ }
144
+
116
145
// detectNuma detects NUMA node information
117
146
func detectNuma () (map [string ]string , error ) {
118
147
sysfsBasePath := hostpath .SysfsDir .Path ("bus/node/devices" )
@@ -166,6 +195,14 @@ func readNdDeviceInfo(path string) nfdv1alpha1.InstanceFeature {
166
195
return * nfdv1alpha1 .NewInstanceFeature (attrs )
167
196
}
168
197
198
+ func getNumberOfLinesFromFile (path string ) (int , error ) {
199
+ data , err := os .ReadFile (path )
200
+ if err != nil {
201
+ return 0 , err
202
+ }
203
+ return len (strings .Split (string (data ), "\n " )), nil
204
+ }
205
+
169
206
func init () {
170
207
source .Register (& src )
171
208
}
0 commit comments