View Javadoc
1   /*
2    * Copyright 2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.driver.linux.proc;
6   
7   import java.nio.ByteBuffer;
8   import java.util.HashMap;
9   import java.util.Map;
10  
11  import oshi.annotation.concurrent.ThreadSafe;
12  import oshi.util.FileUtil;
13  import oshi.util.platform.linux.ProcPath;
14  
15  /**
16   * Utility to read auxiliary vector from {@code /proc/self/auxv}
17   */
18  @ThreadSafe
19  public final class Auxv {
20  
21      private Auxv() {
22      }
23  
24      public static final int AT_PAGESZ = 6; // system page size
25      public static final int AT_HWCAP = 16; // arch dependent hints at CPU capabilities
26      public static final int AT_CLKTCK = 17; // frequency at which times() increments
27  
28      /**
29       * Retrieve the auxiliary vector for the current process
30       *
31       * @return A map of auxiliary vector keys to their respective values
32       * @see <a href= "https://github.com/torvalds/linux/blob/v3.19/include/uapi/linux/auxvec.h">auxvec.h</a>
33       */
34      public static Map<Integer, Long> queryAuxv() {
35          ByteBuffer buff = FileUtil.readAllBytesAsBuffer(ProcPath.AUXV);
36          Map<Integer, Long> auxvMap = new HashMap<>();
37          int key;
38          do {
39              key = FileUtil.readNativeLongFromBuffer(buff).intValue();
40              if (key > 0) {
41                  auxvMap.put(key, FileUtil.readNativeLongFromBuffer(buff).longValue());
42              }
43          } while (key > 0);
44          return auxvMap;
45  
46      }
47  }