diff --git a/helpers/argumentParsers.go b/helpers/argumentParsers.go index 9519efeb..1c763bc6 100644 --- a/helpers/argumentParsers.go +++ b/helpers/argumentParsers.go @@ -6,6 +6,7 @@ import ( "net" "strconv" "strings" + "sync/atomic" "golang.org/x/sys/unix" ) @@ -3158,6 +3159,51 @@ func ParseLegacyGUPFlags(rawValue uint64) LegacyGUPFlag { return LegacyGUPFlag{stringValue: strings.Join(f, "|"), rawValue: uint32(rawValue)} } +var useLegacyGUPFlagsParse atomic.Bool +var skipDetermineGUPFlagsFunc atomic.Bool + +// ParseGUPFlagsCurrentOS parse the GUP flags received according to current machine OS version. +// It uses optimizations to perform better than ParseGUPFlagsForOS +func ParseGUPFlagsCurrentOS(rawValue uint64) (SystemFunctionArgument, error) { + if !skipDetermineGUPFlagsFunc.Load() { + osInfo, err := GetOSInfo() + if err != nil { + return nil, fmt.Errorf("error getting current OS info - %v", err) + } + compare, err := osInfo.CompareOSBaseKernelRelease("6.3.0") + if err != nil { + return nil, fmt.Errorf("error comparing OS versions to determine how to parse GUP flags - %v", err) + } + if !skipDetermineGUPFlagsFunc.Swap(true) { + if compare == KernelVersionOlder { + useLegacyGUPFlagsParse.Store(true) + } else { + useLegacyGUPFlagsParse.Store(false) + } + } + } + + if useLegacyGUPFlagsParse.Load() { + return ParseLegacyGUPFlags(rawValue), nil + } else { + return ParseGUPFlags(rawValue), nil + } +} + +// ParseGUPFlagsForOS parse the GUP flags received according to given OS version. +func ParseGUPFlagsForOS(osInfo *OSInfo, rawValue uint64) (SystemFunctionArgument, error) { + compare, err := osInfo.CompareOSBaseKernelRelease("6.3.0") + if err != nil { + return nil, fmt.Errorf("error comparing OS versions to determine how to parse GUP flags - %v", err) + } + + if compare == KernelVersionOlder { + return ParseLegacyGUPFlags(rawValue), nil + } else { + return ParseGUPFlags(rawValue), nil + } +} + // ===================================================== // VmFlag represents the flags in the `vm_area_struct` in x86 64bit architecture