summaryrefslogtreecommitdiffstats
path: root/xbmc/platform/linux/CPUInfoLinux.cpp
blob: ef34bbd0c65fabb4b77217d011fa8551215ca5d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "CPUInfoLinux.h"

#include "utils/StringUtils.h"
#include "utils/Temperature.h"

#include "platform/linux/SysfsPath.h"

#include <exception>
#include <fstream>
#include <regex>
#include <sstream>
#include <vector>

#if (defined(__arm__) && defined(HAS_NEON)) || defined(__aarch64__)
#include <asm/hwcap.h>
#include <sys/auxv.h>
#elif defined(__i386__) || defined(__x86_64__)
#include <cpuid.h>
#endif

#include <unistd.h>

namespace
{
enum CpuStates
{
  STATE_USER,
  STATE_NICE,
  STATE_SYSTEM,
  STATE_IDLE,
  STATE_IOWAIT,
  STATE_IRQ,
  STATE_SOFTIRQ,
  STATE_STEAL,
  STATE_GUEST,
  STATE_GUEST_NICE,
  STATE_MAX
};

struct CpuData
{
public:
  std::size_t GetActiveTime() const
  {
    return state[STATE_USER] + state[STATE_NICE] + state[STATE_SYSTEM] + state[STATE_IRQ] +
           state[STATE_SOFTIRQ] + state[STATE_STEAL] + state[STATE_GUEST] + state[STATE_GUEST_NICE];
  }

  std::size_t GetIdleTime() const { return state[STATE_IDLE] + state[STATE_IOWAIT]; }

  std::size_t GetTotalTime() const { return GetActiveTime() + GetIdleTime(); }

  std::string cpu;
  std::size_t state[STATE_MAX];
};
} // namespace

std::shared_ptr<CCPUInfo> CCPUInfo::GetCPUInfo()
{
  return std::make_shared<CCPUInfoLinux>();
}

CCPUInfoLinux::CCPUInfoLinux()
{
  CSysfsPath machinePath{"/sys/bus/soc/devices/soc0/machine"};
  if (machinePath.Exists())
    m_cpuHardware = machinePath.Get<std::string>().value_or("");

  CSysfsPath familyPath{"/sys/bus/soc/devices/soc0/family"};
  if (familyPath.Exists())
    m_cpuSoC = familyPath.Get<std::string>().value_or("");

  CSysfsPath socPath{"/sys/bus/soc/devices/soc0/soc_id"};
  if (socPath.Exists())
    m_cpuSoC += " " + socPath.Get<std::string>().value_or("");

  CSysfsPath revisionPath{"/sys/bus/soc/devices/soc0/revision"};
  if (revisionPath.Exists())
    m_cpuRevision = revisionPath.Get<std::string>().value_or("");

  CSysfsPath serialPath{"/sys/bus/soc/devices/soc0/serial_number"};
  if (serialPath.Exists())
    m_cpuSerial = serialPath.Get<std::string>().value_or("");

  const std::string freqStr{"/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"};
  CSysfsPath freqPath{freqStr};
  if (freqPath.Exists())
    m_freqPath = freqStr;

  const std::array<std::string, 4> modules = {
      "coretemp",
      "k10temp",
      "scpi_sensors",
      "imx_thermal_zone",
  };

  for (int i = 0; i < 20; i++)
  {
    CSysfsPath path{"/sys/class/hwmon/hwmon" + std::to_string(i) + "/name"};
    if (!path.Exists())
      continue;

    auto name = path.Get<std::string>();

    if (!name.has_value())
      continue;

    for (const auto& module : modules)
    {
      if (module == name)
      {
        std::string tempStr{"/sys/class/hwmon/hwmon" + std::to_string(i) + "/temp1_input"};
        CSysfsPath tempPath{tempStr};
        if (!tempPath.Exists())
          continue;

        m_tempPath = tempStr;
        break;
      }
    }

    if (!m_tempPath.empty())
      break;
  }

  m_cpuCount = sysconf(_SC_NPROCESSORS_ONLN);

  for (int core = 0; core < m_cpuCount; core++)
  {
    CoreInfo coreInfo;
    coreInfo.m_id = core;
    m_cores.emplace_back(coreInfo);
  }

#if defined(__i386__) || defined(__x86_64__)
  unsigned int eax;
  unsigned int ebx;
  unsigned int ecx;
  unsigned int edx;

  m_cpuVendor.clear();

  if (__get_cpuid(CPUID_INFOTYPE_MANUFACTURER, &eax, &ebx, &ecx, &edx))
  {
    m_cpuVendor.append(reinterpret_cast<const char*>(&ebx), 4);
    m_cpuVendor.append(reinterpret_cast<const char*>(&edx), 4);
    m_cpuVendor.append(reinterpret_cast<const char*>(&ecx), 4);
  }

  if (__get_cpuid(CPUID_INFOTYPE_EXTENDED_IMPLEMENTED, &eax, &ebx, &ecx, &edx))
  {
    if (eax >= CPUID_INFOTYPE_PROCESSOR_3)
    {
      m_cpuModel.clear();

      if (__get_cpuid(CPUID_INFOTYPE_PROCESSOR_1, &eax, &ebx, &ecx, &edx))
      {
        m_cpuModel.append(reinterpret_cast<const char*>(&eax), 4);
        m_cpuModel.append(reinterpret_cast<const char*>(&ebx), 4);
        m_cpuModel.append(reinterpret_cast<const char*>(&ecx), 4);
        m_cpuModel.append(reinterpret_cast<const char*>(&edx), 4);
      }

      if (__get_cpuid(CPUID_INFOTYPE_PROCESSOR_2, &eax, &ebx, &ecx, &edx))
      {
        m_cpuModel.append(reinterpret_cast<const char*>(&eax), 4);
        m_cpuModel.append(reinterpret_cast<const char*>(&ebx), 4);
        m_cpuModel.append(reinterpret_cast<const char*>(&ecx), 4);
        m_cpuModel.append(reinterpret_cast<const char*>(&edx), 4);
      }

      if (__get_cpuid(CPUID_INFOTYPE_PROCESSOR_3, &eax, &ebx, &ecx, &edx))
      {
        m_cpuModel.append(reinterpret_cast<const char*>(&eax), 4);
        m_cpuModel.append(reinterpret_cast<const char*>(&ebx), 4);
        m_cpuModel.append(reinterpret_cast<const char*>(&ecx), 4);
        m_cpuModel.append(reinterpret_cast<const char*>(&edx), 4);
      }
    }
  }

  if (__get_cpuid(CPUID_INFOTYPE_STANDARD, &eax, &eax, &ecx, &edx))
  {
    if (edx & CPUID_00000001_EDX_MMX)
      m_cpuFeatures |= CPU_FEATURE_MMX;

    if (edx & CPUID_00000001_EDX_SSE)
      m_cpuFeatures |= CPU_FEATURE_SSE;

    if (edx & CPUID_00000001_EDX_SSE2)
      m_cpuFeatures |= CPU_FEATURE_SSE2;

    if (ecx & CPUID_00000001_ECX_SSE3)
      m_cpuFeatures |= CPU_FEATURE_SSE3;

    if (ecx & CPUID_00000001_ECX_SSSE3)
      m_cpuFeatures |= CPU_FEATURE_SSSE3;

    if (ecx & CPUID_00000001_ECX_SSE4)
      m_cpuFeatures |= CPU_FEATURE_SSE4;

    if (ecx & CPUID_00000001_ECX_SSE42)
      m_cpuFeatures |= CPU_FEATURE_SSE42;
  }

  if (__get_cpuid(CPUID_INFOTYPE_EXTENDED_IMPLEMENTED, &eax, &eax, &ecx, &edx))
  {
    if (eax >= CPUID_INFOTYPE_EXTENDED)
    {
      if (edx & CPUID_80000001_EDX_MMX)
        m_cpuFeatures |= CPU_FEATURE_MMX;

      if (edx & CPUID_80000001_EDX_MMX2)
        m_cpuFeatures |= CPU_FEATURE_MMX2;

      if (edx & CPUID_80000001_EDX_3DNOW)
        m_cpuFeatures |= CPU_FEATURE_3DNOW;

      if (edx & CPUID_80000001_EDX_3DNOWEXT)
        m_cpuFeatures |= CPU_FEATURE_3DNOWEXT;
    }
  }
#else
  std::ifstream cpuinfo("/proc/cpuinfo");
  std::regex re(".*: (.*)$");

  for (std::string line; std::getline(cpuinfo, line);)
  {
    std::smatch match;

    if (std::regex_match(line, match, re))
    {
      if (match.size() == 2)
      {
        std::ssub_match value = match[1];

        if (line.find("model name") != std::string::npos)
        {
          if (m_cpuModel.empty())
            m_cpuModel = value.str();
        }

        if (line.find("BogoMIPS") != std::string::npos)
        {
          if (m_cpuBogoMips.empty())
            m_cpuBogoMips = value.str();
        }

        if (line.find("Hardware") != std::string::npos)
        {
          if (m_cpuHardware.empty())
            m_cpuHardware = value.str();
        }

        if (line.find("Serial") != std::string::npos)
        {
          if (m_cpuSerial.empty())
            m_cpuSerial = value.str();
        }

        if (line.find("Revision") != std::string::npos)
        {
          if (m_cpuRevision.empty())
            m_cpuRevision = value.str();
        }
      }
    }
  }
#endif

  m_cpuModel = m_cpuModel.substr(0, m_cpuModel.find(char(0))); // remove extra null terminations

#if defined(HAS_NEON) && defined(__arm__)
  if (getauxval(AT_HWCAP) & HWCAP_NEON)
    m_cpuFeatures |= CPU_FEATURE_NEON;
#endif

#if defined(HAS_NEON) && defined(__aarch64__)
  if (getauxval(AT_HWCAP) & HWCAP_ASIMD)
    m_cpuFeatures |= CPU_FEATURE_NEON;
#endif

  // Set MMX2 when SSE is present as SSE is a superset of MMX2 and Intel doesn't set the MMX2 cap
  if (m_cpuFeatures & CPU_FEATURE_SSE)
    m_cpuFeatures |= CPU_FEATURE_MMX2;
}

int CCPUInfoLinux::GetUsedPercentage()
{
  if (!m_nextUsedReadTime.IsTimePast())
    return m_lastUsedPercentage;

  std::vector<CpuData> cpuData;

  std::ifstream infile("/proc/stat");

  for (std::string line; std::getline(infile, line);)
  {
    if (line.find("cpu") != std::string::npos)
    {
      std::istringstream ss(line);
      CpuData info;

      ss >> info.cpu;

      for (int i = 0; i < STATE_MAX; i++)
      {
        ss >> info.state[i];
      }

      cpuData.emplace_back(info);
    }
  }

  auto activeTime = cpuData.front().GetActiveTime() - m_activeTime;
  auto idleTime = cpuData.front().GetIdleTime() - m_idleTime;
  auto totalTime = cpuData.front().GetTotalTime() - m_totalTime;

  m_activeTime += activeTime;
  m_idleTime += idleTime;
  m_totalTime += totalTime;

  m_lastUsedPercentage = activeTime * 100.0f / totalTime;
  m_nextUsedReadTime.Set(MINIMUM_TIME_BETWEEN_READS);

  cpuData.erase(cpuData.begin());

  for (std::size_t core = 0; core < cpuData.size(); core++)
  {
    auto activeTime = cpuData[core].GetActiveTime() - m_cores[core].m_activeTime;
    auto idleTime = cpuData[core].GetIdleTime() - m_cores[core].m_idleTime;
    auto totalTime = cpuData[core].GetTotalTime() - m_cores[core].m_totalTime;

    m_cores[core].m_usagePercent = activeTime * 100.0 / totalTime;

    m_cores[core].m_activeTime += activeTime;
    m_cores[core].m_idleTime += idleTime;
    m_cores[core].m_totalTime += totalTime;
  }

  return static_cast<int>(m_lastUsedPercentage);
}

float CCPUInfoLinux::GetCPUFrequency()
{
  if (m_freqPath.empty())
    return 0;

  auto freq = CSysfsPath(m_freqPath).Get<float>();
  return freq.has_value() ? *freq / 1000.0f : 0.0f;
}

bool CCPUInfoLinux::GetTemperature(CTemperature& temperature)
{
  if (CheckUserTemperatureCommand(temperature))
    return true;

  if (m_tempPath.empty())
    return false;

  auto temp = CSysfsPath(m_tempPath).Get<double>();
  if (!temp.has_value())
    return false;

  double value = *temp / 1000.0;

  temperature = CTemperature::CreateFromCelsius(value);
  temperature.SetValid(true);

  return true;
}