View Javadoc
1   /*
2    * Copyright 2016-2023 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.hardware.platform.unix.freebsd;
6   
7   import java.util.Locale;
8   
9   import com.sun.jna.Memory;
10  import com.sun.jna.platform.unix.LibCAPI.size_t;
11  
12  import oshi.annotation.concurrent.ThreadSafe;
13  import oshi.hardware.common.AbstractSensors;
14  import oshi.jna.ByRef.CloseableSizeTByReference;
15  import oshi.jna.platform.unix.FreeBsdLibc;
16  
17  /**
18   * Sensors from coretemp
19   */
20  @ThreadSafe
21  final class FreeBsdSensors extends AbstractSensors {
22  
23      @Override
24      public double queryCpuTemperature() {
25          return queryKldloadCoretemp();
26      }
27  
28      /*
29       * If user has loaded coretemp module via kldload coretemp, sysctl call will return temperature
30       *
31       * @return Temperature if successful, otherwise NaN
32       */
33      private static double queryKldloadCoretemp() {
34          String name = "dev.cpu.%d.temperature";
35          try (CloseableSizeTByReference size = new CloseableSizeTByReference(FreeBsdLibc.INT_SIZE)) {
36              int cpu = 0;
37              double sumTemp = 0d;
38              try (Memory p = new Memory(size.longValue())) {
39                  while (0 == FreeBsdLibc.INSTANCE.sysctlbyname(String.format(Locale.ROOT, name, cpu), p, size, null,
40                          size_t.ZERO)) {
41                      sumTemp += p.getInt(0) / 10d - 273.15;
42                      cpu++;
43                  }
44              }
45              return cpu > 0 ? sumTemp / cpu : Double.NaN;
46          }
47      }
48  
49      @Override
50      public int[] queryFanSpeeds() {
51          // Nothing known on FreeBSD for this.
52          return new int[0];
53      }
54  
55      @Override
56      public double queryCpuVoltage() {
57          // Nothing known on FreeBSD for this.
58          return 0d;
59      }
60  }