wrapamdsysfs.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* Copyright (C) 1883 Thomas Edison - All Rights Reserved
  2. * You may use, distribute and modify this code under the
  3. * terms of the GPLv3 license, which unfortunately won't be
  4. * written for another century.
  5. *
  6. * You should have received a copy of the LICENSE file with
  7. * this file.
  8. */
  9. #include <stdint.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <sys/types.h>
  13. #if defined(__linux)
  14. #include <dirent.h>
  15. #endif
  16. #include <boost/algorithm/string.hpp>
  17. #include <boost/filesystem.hpp>
  18. #include <algorithm>
  19. #include <climits>
  20. #include <cstdint>
  21. #include <cstring>
  22. #include <fstream>
  23. #include <iostream>
  24. #include <limits>
  25. #include <regex>
  26. #include <string>
  27. #include "wrapamdsysfs.h"
  28. #include "wraphelper.h"
  29. using namespace std;
  30. static bool getFileContentValue(const char* filename, unsigned int& value) {
  31. value = 0;
  32. ifstream ifs(filename, ios::binary);
  33. string line;
  34. getline(ifs, line);
  35. char* p = (char*)line.c_str();
  36. char* p2;
  37. errno = 0;
  38. value = strtoul(p, &p2, 0);
  39. if (errno != 0)
  40. return false;
  41. return (p != p2);
  42. }
  43. wrap_amdsysfs_handle* wrap_amdsysfs_create() {
  44. wrap_amdsysfs_handle* sysfsh = nullptr;
  45. #if defined(__linux)
  46. namespace fs = boost::filesystem;
  47. vector<pciInfo> devices; // Used to collect devices
  48. char dbuf[120];
  49. // Check directory exist
  50. fs::path drm_dir("/sys/class/drm");
  51. if (!fs::exists(drm_dir) || !fs::is_directory(drm_dir))
  52. return nullptr;
  53. // Regex patterns to identify directory elements
  54. regex cardPattern("^card[0-9]{1,}$");
  55. regex hwmonPattern("^hwmon[0-9]{1,}$");
  56. // Loop directory contents
  57. for (fs::directory_iterator dirEnt(drm_dir); dirEnt != fs::directory_iterator(); ++dirEnt) {
  58. // Skip non relevant entries
  59. if (!fs::is_directory(dirEnt->path()) || !regex_match(dirEnt->path().filename().string(), cardPattern))
  60. continue;
  61. string devName = dirEnt->path().filename().string();
  62. unsigned int devIndex = stoi(devName.substr(4), nullptr, 10);
  63. unsigned int vendorId = 0;
  64. unsigned int hwmonIndex = UINT_MAX;
  65. // Get AMD cards only (vendor 4098)
  66. fs::path vendor_file("/sys/class/drm/" + devName + "/device/vendor");
  67. snprintf(dbuf, 120, "/sys/class/drm/%s/device/vendor", devName.c_str());
  68. if (!fs::exists(vendor_file) || !fs::is_regular_file(vendor_file) || !getFileContentValue(dbuf, vendorId) ||
  69. vendorId != 4098)
  70. continue;
  71. // Check it has dependant hwmon directory
  72. fs::path hwmon_dir("/sys/class/drm/" + devName + "/device/hwmon");
  73. if (!fs::exists(hwmon_dir) || !fs::is_directory(hwmon_dir))
  74. continue;
  75. // Loop subelements in hwmon directory
  76. for (fs::directory_iterator hwmonEnt(hwmon_dir); hwmonEnt != fs::directory_iterator(); ++hwmonEnt) {
  77. // Skip non relevant entries
  78. if (!fs::is_directory(hwmonEnt->path()) || !regex_match(hwmonEnt->path().filename().string(), hwmonPattern))
  79. continue;
  80. unsigned int v = stoi(hwmonEnt->path().filename().string().substr(5), nullptr, 10);
  81. hwmonIndex = min(hwmonIndex, v);
  82. }
  83. if (hwmonIndex == UINT_MAX)
  84. continue;
  85. // Detect Pci Id
  86. fs::path uevent_file("/sys/class/drm/" + devName + "/device/uevent");
  87. if (!fs::exists(uevent_file) || !fs::is_regular_file(uevent_file))
  88. continue;
  89. snprintf(dbuf, 120, "/sys/class/drm/card%d/device/uevent", devIndex);
  90. ifstream ifs(dbuf, ios::binary);
  91. string line;
  92. int PciDomain = -1, PciBus = -1, PciDevice = -1, PciFunction = -1;
  93. while (getline(ifs, line)) {
  94. if (line.length() > 24 && line.substr(0, 13) == "PCI_SLOT_NAME") {
  95. string pciId = line.substr(14);
  96. vector<string> pciIdParts;
  97. boost::split(pciIdParts, pciId, [](char c) { return (c == ':' || c == '.'); });
  98. try {
  99. PciDomain = stoi(pciIdParts.at(0), nullptr, 16);
  100. PciBus = stoi(pciIdParts.at(1), nullptr, 16);
  101. PciDevice = stoi(pciIdParts.at(2), nullptr, 16);
  102. PciFunction = stoi(pciIdParts.at(3), nullptr, 16);
  103. } catch (const exception&) {
  104. PciDomain = PciBus = PciDevice = PciFunction = -1;
  105. }
  106. break;
  107. }
  108. }
  109. // If we got an error skip
  110. if (PciDomain == -1)
  111. continue;
  112. // We got all information needed
  113. // push in the list of collected devices
  114. pciInfo pInfo = pciInfo();
  115. pInfo.DeviceId = devIndex;
  116. pInfo.HwMonId = hwmonIndex;
  117. pInfo.PciDomain = PciDomain;
  118. pInfo.PciBus = PciBus;
  119. pInfo.PciDevice = PciDevice;
  120. devices.push_back(pInfo);
  121. }
  122. // Nothing collected - exit
  123. if (!devices.size()) {
  124. cwarn << "Failed to obtain all required AMD file pointers";
  125. cwarn << "AMD hardware monitoring disabled";
  126. return nullptr;
  127. }
  128. unsigned int gpucount = devices.size();
  129. sysfsh = (wrap_amdsysfs_handle*)calloc(1, sizeof(wrap_amdsysfs_handle));
  130. if (sysfsh == nullptr) {
  131. cwarn << "Failed allocate memory";
  132. cwarn << "AMD hardware monitoring disabled";
  133. return sysfsh;
  134. }
  135. sysfsh->sysfs_gpucount = gpucount;
  136. sysfsh->sysfs_device_id = (unsigned int*)calloc(gpucount, sizeof(unsigned int));
  137. sysfsh->sysfs_hwmon_id = (unsigned int*)calloc(gpucount, sizeof(unsigned int));
  138. sysfsh->sysfs_pci_domain_id = (unsigned int*)calloc(gpucount, sizeof(unsigned int));
  139. sysfsh->sysfs_pci_bus_id = (unsigned int*)calloc(gpucount, sizeof(unsigned int));
  140. sysfsh->sysfs_pci_device_id = (unsigned int*)calloc(gpucount, sizeof(unsigned int));
  141. gpucount = 0;
  142. for (auto const& device : devices) {
  143. sysfsh->sysfs_device_id[gpucount] = device.DeviceId;
  144. sysfsh->sysfs_hwmon_id[gpucount] = device.HwMonId;
  145. sysfsh->sysfs_pci_domain_id[gpucount] = device.PciDomain;
  146. sysfsh->sysfs_pci_bus_id[gpucount] = device.PciBus;
  147. sysfsh->sysfs_pci_device_id[gpucount] = device.PciDevice;
  148. gpucount++;
  149. }
  150. #endif
  151. return sysfsh;
  152. }
  153. int wrap_amdsysfs_destroy(wrap_amdsysfs_handle* sysfsh) {
  154. free(sysfsh);
  155. return 0;
  156. }
  157. int wrap_amdsysfs_get_gpucount(wrap_amdsysfs_handle* sysfsh, int* gpucount) {
  158. *gpucount = sysfsh->sysfs_gpucount;
  159. return 0;
  160. }
  161. int wrap_amdsysfs_get_tempC(wrap_amdsysfs_handle* sysfsh, int index, unsigned int* tempC) {
  162. if (index < 0 || index >= sysfsh->sysfs_gpucount)
  163. return -1;
  164. int gpuindex = sysfsh->sysfs_device_id[index];
  165. int hwmonindex = sysfsh->sysfs_hwmon_id[index];
  166. if (hwmonindex < 0)
  167. return -1;
  168. char dbuf[120];
  169. snprintf(dbuf, 120, "/sys/class/drm/card%d/device/hwmon/hwmon%d/temp1_input", gpuindex, hwmonindex);
  170. unsigned int temp = 0;
  171. getFileContentValue(dbuf, temp);
  172. if (temp > 0)
  173. *tempC = temp / 1000;
  174. return 0;
  175. }
  176. int wrap_amdsysfs_get_mem_tempC(wrap_amdsysfs_handle* sysfsh, int index, unsigned int* tempC) {
  177. if (index < 0 || index >= sysfsh->sysfs_gpucount)
  178. return -1;
  179. int gpuindex = sysfsh->sysfs_device_id[index];
  180. int hwmonindex = sysfsh->sysfs_hwmon_id[index];
  181. if (hwmonindex < 0)
  182. return -1;
  183. char dbuf[120];
  184. snprintf(dbuf, 120, "/sys/class/drm/card%d/device/hwmon/hwmon%d/temp3_input", gpuindex, hwmonindex);
  185. unsigned int temp = 0;
  186. getFileContentValue(dbuf, temp);
  187. if (temp > 0)
  188. *tempC = temp / 1000;
  189. return 0;
  190. }
  191. int wrap_amdsysfs_get_fanpcnt(wrap_amdsysfs_handle* sysfsh, int index, unsigned int* fanpcnt) {
  192. if (index < 0 || index >= sysfsh->sysfs_gpucount)
  193. return -1;
  194. int gpuindex = sysfsh->sysfs_device_id[index];
  195. int hwmonindex = sysfsh->sysfs_hwmon_id[index];
  196. if (hwmonindex < 0)
  197. return -1;
  198. unsigned int pwm = 0, pwmMax = 255, pwmMin = 0;
  199. char dbuf[120];
  200. snprintf(dbuf, 120, "/sys/class/drm/card%d/device/hwmon/hwmon%d/pwm1", gpuindex, hwmonindex);
  201. getFileContentValue(dbuf, pwm);
  202. snprintf(dbuf, 120, "/sys/class/drm/card%d/device/hwmon/hwmon%d/pwm1_max", gpuindex, hwmonindex);
  203. getFileContentValue(dbuf, pwmMax);
  204. snprintf(dbuf, 120, "/sys/class/drm/card%d/device/hwmon/hwmon%d/pwm1_min", gpuindex, hwmonindex);
  205. getFileContentValue(dbuf, pwmMin);
  206. *fanpcnt = (unsigned int)(double(pwm - pwmMin) / double(pwmMax - pwmMin) * 100.0);
  207. return 0;
  208. }
  209. int wrap_amdsysfs_get_power_usage(wrap_amdsysfs_handle* sysfsh, int index, unsigned int* milliwatts) {
  210. try {
  211. if (index < 0 || index >= sysfsh->sysfs_gpucount)
  212. return -1;
  213. int gpuindex = sysfsh->sysfs_device_id[index];
  214. char dbuf[120];
  215. snprintf(dbuf, 120, "/sys/kernel/debug/dri/%d/amdgpu_pm_info", gpuindex);
  216. ifstream ifs(dbuf, ios::binary);
  217. string line;
  218. while (getline(ifs, line)) {
  219. smatch sm;
  220. regex regex(R"(([\d|\.]+) W \(average GPU\))");
  221. if (regex_search(line, sm, regex)) {
  222. if (sm.size() == 2) {
  223. double watt = atof(sm.str(1).c_str());
  224. *milliwatts = (unsigned int)(watt * 1000);
  225. return 0;
  226. }
  227. }
  228. }
  229. } catch (const exception& ex) {
  230. cwarn << "Error in amdsysfs_get_power_usage: " << ex.what();
  231. }
  232. return -1;
  233. }