View Javadoc
1   /*
2    * Copyright 2020-2024 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.driver.linux.proc;
6   
7   import static oshi.util.platform.linux.ProcPath.CPUINFO;
8   
9   import java.util.List;
10  import java.util.Locale;
11  import java.util.stream.Collectors;
12  
13  import oshi.annotation.concurrent.ThreadSafe;
14  import oshi.util.Constants;
15  import oshi.util.FileUtil;
16  import oshi.util.ParseUtil;
17  import oshi.util.tuples.Quartet;
18  
19  /**
20   * Utility to read CPU info from {@code /proc/cpuinfo}
21   */
22  @ThreadSafe
23  public final class CpuInfo {
24  
25      private CpuInfo() {
26      }
27  
28      /**
29       * Gets the CPU manufacturer from {@code /proc/cpuinfo}
30       *
31       * @return The manufacturer if known, null otherwise
32       */
33      public static String queryCpuManufacturer() {
34          List<String> cpuInfo = FileUtil.readFile(CPUINFO);
35          for (String line : cpuInfo) {
36              if (line.startsWith("CPU implementer")) {
37                  int part = ParseUtil.parseLastInt(line, 0);
38                  switch (part) {
39                  case 0x41:
40                      return "ARM";
41                  case 0x42:
42                      return "Broadcom";
43                  case 0x43:
44                      return "Cavium";
45                  case 0x44:
46                      return "DEC";
47                  case 0x4e:
48                      return "Nvidia";
49                  case 0x50:
50                      return "APM";
51                  case 0x51:
52                      return "Qualcomm";
53                  case 0x53:
54                      return "Samsung";
55                  case 0x56:
56                      return "Marvell";
57                  case 0x66:
58                      return "Faraday";
59                  case 0x69:
60                      return "Intel";
61                  default:
62                      return null;
63                  }
64              }
65          }
66          return null;
67      }
68  
69      /**
70       * Gets the board manufacturer, model, version, and serial number from {@code /proc/cpuinfo}
71       *
72       * @return A quartet of strings for manufacturer, model, version, and serial number. Each one may be null if
73       *         unknown.
74       */
75      public static Quartet<String, String, String, String> queryBoardInfo() {
76          String pcManufacturer = null;
77          String pcModel = null;
78          String pcVersion = null;
79          String pcSerialNumber = null;
80  
81          List<String> cpuInfo = FileUtil.readFile(CPUINFO);
82          for (String line : cpuInfo) {
83              String[] splitLine = ParseUtil.whitespacesColonWhitespace.split(line);
84              if (splitLine.length < 2) {
85                  continue;
86              }
87              switch (splitLine[0]) {
88              case "Hardware":
89                  pcModel = splitLine[1];
90                  break;
91              case "Revision":
92                  pcVersion = splitLine[1];
93                  if (pcVersion.length() > 1) {
94                      pcManufacturer = queryBoardManufacturer(pcVersion.charAt(1));
95                  }
96                  break;
97              case "Serial":
98                  pcSerialNumber = splitLine[1];
99                  break;
100             default:
101                 // Do nothing
102             }
103         }
104         return new Quartet<>(pcManufacturer, pcModel, pcVersion, pcSerialNumber);
105     }
106 
107     private static String queryBoardManufacturer(char digit) {
108         switch (digit) {
109         case '0':
110             return "Sony UK";
111         case '1':
112             return "Egoman";
113         case '2':
114             return "Embest";
115         case '3':
116             return "Sony Japan";
117         case '4':
118             return "Embest";
119         case '5':
120             return "Stadium";
121         default:
122             return Constants.UNKNOWN;
123         }
124     }
125 
126     public static List<String> queryFeatureFlags() {
127         return FileUtil.readFile(CPUINFO).stream().filter(f -> {
128             String s = f.toLowerCase(Locale.ROOT);
129             return s.startsWith("flags") || s.startsWith("features");
130         }).distinct().collect(Collectors.toList());
131     }
132 }