@@ -21,10 +21,10 @@ import (
21
21
"fmt"
22
22
"os"
23
23
"path/filepath"
24
- "regexp"
25
24
"strconv"
26
25
"strings"
27
26
27
+ corev1 "k8s.io/api/core/v1"
28
28
"k8s.io/apimachinery/pkg/api/resource"
29
29
"k8s.io/klog/v2"
30
30
@@ -208,53 +208,39 @@ func detectHugePages() (map[string]string, error) {
208
208
return nil , fmt .Errorf ("unable to read huge pages size: %w" , err )
209
209
}
210
210
211
- reg := regexp .MustCompile (`hugepages-(\d+)` )
212
211
for _ , entry := range subdirs {
213
212
if ! entry .IsDir () {
214
213
continue
215
214
}
216
215
217
- pageSize , err := getHugePageSize (basePath , entry .Name (), reg )
216
+ totalPages , err := getHugePagesTotalCount (basePath , entry .Name ())
218
217
if err != nil {
219
- klog .ErrorS (err , "skipping huge page entry" , "entry" , entry .Name ())
218
+ klog .ErrorS (err , "unable to read hugepages total count" , "hugepages" , entry .Name ())
219
+ }
220
+ pageSize := strings .TrimRight (strings .TrimPrefix (entry .Name (), "hugepages-" ), "kB" )
221
+ quantity , err := resource .ParseQuantity (pageSize + "Ki" )
222
+ if err != nil {
223
+ klog .ErrorS (err , "unable to parse quantity" , "hugepages" , entry .Name (), "pageSize" , pageSize )
220
224
continue
221
225
}
222
- if pageSize != "" {
226
+
227
+ hugePages [corev1 .ResourceHugePagesPrefix + quantity .String ()] = totalPages
228
+ if v , err := strconv .Atoi (totalPages ); err == nil && v > 0 {
223
229
hugePages ["enabled" ] = "true"
224
- hugePages [pageSize ] = "true"
225
230
}
226
231
}
227
232
228
233
return hugePages , nil
229
234
}
230
235
231
- func getHugePageSize (basePath , dirName string , reg * regexp.Regexp ) (string , error ) {
232
- matches := reg .FindStringSubmatch (dirName )
233
- if len (matches ) != 2 {
234
- return "" , fmt .Errorf ("unexpected directory format: %s" , dirName )
235
- }
236
-
237
- totalPagesFile := filepath .Join (basePath , dirName , "nr_hugepages" )
238
- totalPagesRaw , err := os .ReadFile (totalPagesFile )
236
+ func getHugePagesTotalCount (basePath , dirname string ) (string , error ) {
237
+ totalPagesFile := filepath .Join (basePath , dirname , "nr_hugepages" )
238
+ totalPages , err := os .ReadFile (totalPagesFile )
239
239
if err != nil {
240
240
return "" , fmt .Errorf ("unable to read total number of huge pages from the file: %s" , totalPagesFile )
241
241
}
242
242
243
- totalPages , err := strconv .Atoi (strings .TrimSpace (string (totalPagesRaw )))
244
- if err != nil {
245
- return "" , fmt .Errorf ("unable to convert total pages to integer - %s: %s" , totalPagesFile , totalPagesRaw )
246
- }
247
- if totalPages < 1 {
248
- return "" , nil
249
- }
250
-
251
- sizeFormat := matches [1 ] + "Ki"
252
- quantity , err := resource .ParseQuantity (sizeFormat )
253
- if err != nil {
254
- return "" , fmt .Errorf ("invalid size format: %s" , sizeFormat )
255
- }
256
-
257
- return "hugepages-" + quantity .String (), nil
243
+ return strings .TrimSpace (string (totalPages )), nil
258
244
}
259
245
260
246
// ndDevAttrs is the list of sysfs files (under each nd device) that we're trying to read
0 commit comments