-
Couldn't load subscription status.
- Fork 314
Improve OS type and architecture detection #9637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| package datadog.environment; | ||
|
|
||
| import static datadog.environment.OperatingSystem.Type.LINUX; | ||
| import static datadog.environment.OperatingSystem.Type.MACOS; | ||
| import static datadog.environment.OperatingSystem.Type.WINDOWS; | ||
| import static java.util.Locale.ROOT; | ||
|
|
||
| import java.io.BufferedReader; | ||
|
|
@@ -10,11 +13,15 @@ | |
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.util.Arrays; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| /** Detects operating systems and libc library. */ | ||
| public final class OperatingSystem { | ||
| private static final String OS_NAME_PROPERTY = "os.name"; | ||
| private static final String OS_ARCH_PROPERTY = "os.arch"; | ||
| private static final Type TYPE = Type.current(); | ||
| private static final Architecture ARCHITECTURE = Architecture.current(); | ||
|
|
||
| private OperatingSystem() {} | ||
|
|
||
|
|
@@ -24,7 +31,7 @@ private OperatingSystem() {} | |
| * @return @{@code true} if operating system is Linux based, {@code false} otherwise. | ||
| */ | ||
| public static boolean isLinux() { | ||
| return propertyContains(OS_NAME_PROPERTY, "linux"); | ||
| return TYPE == LINUX; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -33,8 +40,7 @@ public static boolean isLinux() { | |
| * @return @{@code true} if operating system is Windows, {@code false} otherwise. | ||
| */ | ||
| public static boolean isWindows() { | ||
| // https://mkyong.com/java/how-to-detect-os-in-java-systemgetpropertyosname/ | ||
| return propertyContains(OS_NAME_PROPERTY, "win"); | ||
| return TYPE == WINDOWS; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -43,20 +49,21 @@ public static boolean isWindows() { | |
| * @return @{@code true} if operating system is macOS, {@code false} otherwise. | ||
| */ | ||
| public static boolean isMacOs() { | ||
| return propertyContains(OS_NAME_PROPERTY, "mac"); | ||
| return TYPE == MACOS; | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether the architecture is AArch64. | ||
| * Gets the operating system type. | ||
| * | ||
| * @return {@code true} if the architecture is AArch64, {@code false} otherwise. | ||
| * @return The operating system type, {@link Type#UNKNOWN} if not properly detected or supported. | ||
| */ | ||
| public static boolean isAarch64() { | ||
| return propertyContains(OS_ARCH_PROPERTY, "aarch64"); | ||
| public static Type type() { | ||
| return TYPE; | ||
| } | ||
|
|
||
| private static boolean propertyContains(String property, String content) { | ||
| return SystemProperties.getOrDefault(property, "").toLowerCase(ROOT).contains(content); | ||
| /** Gets the operating system architecture . */ | ||
| public static Architecture architecture() { | ||
| return ARCHITECTURE; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -146,4 +153,65 @@ private static boolean containsArray(byte[] container, int offset, byte[] contai | |
| } | ||
| return true; | ||
| } | ||
|
|
||
| public enum Type { | ||
| WINDOWS("Windows"), | ||
| MACOS("MacOS"), | ||
| LINUX("Linux"), | ||
| UNKNOWN("unknown"); | ||
|
|
||
| private final String name; | ||
|
|
||
| Type(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| static Type current() { | ||
| String property = SystemProperties.getOrDefault(OS_NAME_PROPERTY, "").toLowerCase(ROOT); | ||
| // https://mkyong.com/java/how-to-detect-os-in-java-systemgetpropertyosname/ | ||
| if (property.contains("linux")) { | ||
| return LINUX; | ||
| } else if (property.contains("win")) { | ||
| return WINDOWS; | ||
| } else if (property.contains("mac")) { | ||
| return MACOS; | ||
| } else { | ||
| return UNKNOWN; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return this.name; | ||
| } | ||
| } | ||
|
|
||
| /** Detects the operating system architecture. */ | ||
| public enum Architecture { | ||
| X64("x86_64", "amd64", "k8"), | ||
| X86("x86", "i386", "i486", "i586", "i686"), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Out of curiosity as I didn't followed what Intel did these last years, is there later architectures, if that matter for us ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should ask to profiling about it. This PR is only code relocation. |
||
| ARM("arm", "aarch32"), | ||
| ARM64("arm64", "aarch64"), | ||
| UNKNOWN(); | ||
|
|
||
| private final Set<String> identifiers; | ||
|
|
||
| Architecture(String... identifiers) { | ||
| this.identifiers = new HashSet<>(Arrays.asList(identifiers)); | ||
| } | ||
|
|
||
| static Architecture of(String identifier) { | ||
| for (Architecture architecture : Architecture.values()) { | ||
| if (architecture.identifiers.contains(identifier)) { | ||
| return architecture; | ||
| } | ||
| } | ||
| return UNKNOWN; | ||
| } | ||
|
|
||
| static Architecture current() { | ||
| String property = SystemProperties.getOrDefault(OS_ARCH_PROPERTY, "").toLowerCase(ROOT); | ||
| return Architecture.of(property); | ||
| } | ||
| } | ||
| } | ||
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought: Thinking about this, what about renaming
OperatingSystemtoPlatform, andTypetoOS(orOperatingSystem) ?The reason for this naeing change is that architecture is not really a property of the operating system. I believe that using the notion of platform is much more close to the native world.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I initially design the arch outside the
OperatingSystem. But I rely onos.archproperty, I moved is inOperatingSystem.