|
6 | 6 | "net" |
7 | 7 | "strconv" |
8 | 8 | "strings" |
| 9 | + "sync/atomic" |
9 | 10 |
|
10 | 11 | "golang.org/x/sys/unix" |
11 | 12 | ) |
@@ -3158,6 +3159,67 @@ func ParseLegacyGUPFlags(rawValue uint64) LegacyGUPFlag { |
3158 | 3159 | return LegacyGUPFlag{stringValue: strings.Join(f, "|"), rawValue: uint32(rawValue)} |
3159 | 3160 | } |
3160 | 3161 |
|
| 3162 | +var currentOSGUPFlagsParse uint32 |
| 3163 | +var skipDetermineGUPFlagsFunc uint32 |
| 3164 | + |
| 3165 | +const gupFlagsChangeVersion = "6.3.0" |
| 3166 | + |
| 3167 | +// ParseGUPFlagsCurrentOS parse the GUP flags received according to current machine OS version. |
| 3168 | +// It uses optimizations to perform better than ParseGUPFlagsForOS |
| 3169 | +func ParseGUPFlagsCurrentOS(rawValue uint64) (SystemFunctionArgument, error) { |
| 3170 | + const ( |
| 3171 | + newVersionsParsing = iota |
| 3172 | + legacyParsing |
| 3173 | + ) |
| 3174 | + if atomic.LoadUint32(&skipDetermineGUPFlagsFunc) == 0 { |
| 3175 | + osInfo, err := GetOSInfo() |
| 3176 | + if err != nil { |
| 3177 | + return nil, fmt.Errorf("error getting current OS info - %s", err) |
| 3178 | + } |
| 3179 | + compare, err := osInfo.CompareOSBaseKernelRelease(gupFlagsChangeVersion) |
| 3180 | + if err != nil { |
| 3181 | + return nil, fmt.Errorf( |
| 3182 | + "error comparing OS versions to determine how to parse GUP flags - %s", |
| 3183 | + err, |
| 3184 | + ) |
| 3185 | + } |
| 3186 | + if compare == KernelVersionOlder { |
| 3187 | + atomic.StoreUint32(¤tOSGUPFlagsParse, legacyParsing) |
| 3188 | + } else { |
| 3189 | + atomic.StoreUint32(¤tOSGUPFlagsParse, newVersionsParsing) |
| 3190 | + } |
| 3191 | + // Avoid doing this check in the future |
| 3192 | + atomic.StoreUint32(&skipDetermineGUPFlagsFunc, 1) |
| 3193 | + } |
| 3194 | + |
| 3195 | + // Don't really need to use atomics here, as the value is only used here |
| 3196 | + // and is set in an atomic way |
| 3197 | + switch currentOSGUPFlagsParse { |
| 3198 | + case legacyParsing: |
| 3199 | + return ParseLegacyGUPFlags(rawValue), nil |
| 3200 | + case newVersionsParsing: |
| 3201 | + return ParseGUPFlags(rawValue), nil |
| 3202 | + default: |
| 3203 | + return nil, fmt.Errorf("no parsing function for GUP flags was found to this OD version") |
| 3204 | + } |
| 3205 | +} |
| 3206 | + |
| 3207 | +// ParseGUPFlagsForOS parse the GUP flags received according to given OS version. |
| 3208 | +func ParseGUPFlagsForOS(osInfo *OSInfo, rawValue uint64) (SystemFunctionArgument, error) { |
| 3209 | + compare, err := osInfo.CompareOSBaseKernelRelease(gupFlagsChangeVersion) |
| 3210 | + if err != nil { |
| 3211 | + return nil, fmt.Errorf( |
| 3212 | + "error comparing OS versions to determine how to parse GUP flags - %s", |
| 3213 | + err, |
| 3214 | + ) |
| 3215 | + } |
| 3216 | + |
| 3217 | + if compare == KernelVersionOlder { |
| 3218 | + return ParseLegacyGUPFlags(rawValue), nil |
| 3219 | + } |
| 3220 | + return ParseGUPFlags(rawValue), nil |
| 3221 | +} |
| 3222 | + |
3161 | 3223 | // ===================================================== |
3162 | 3224 |
|
3163 | 3225 | // VmFlag represents the flags in the `vm_area_struct` in x86 64bit architecture |
|
0 commit comments