smt.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <linux/bitops.h>
  5. #include "api/fs/fs.h"
  6. #include "smt.h"
  7. int smt_on(void)
  8. {
  9. static bool cached;
  10. static int cached_result;
  11. int cpu;
  12. int ncpu;
  13. if (cached)
  14. return cached_result;
  15. if (sysfs__read_int("devices/system/cpu/smt/active", &cached_result) >= 0)
  16. goto done;
  17. ncpu = sysconf(_SC_NPROCESSORS_CONF);
  18. for (cpu = 0; cpu < ncpu; cpu++) {
  19. unsigned long long siblings;
  20. char *str;
  21. size_t strlen;
  22. char fn[256];
  23. snprintf(fn, sizeof fn,
  24. "devices/system/cpu/cpu%d/topology/core_cpus", cpu);
  25. if (sysfs__read_str(fn, &str, &strlen) < 0) {
  26. snprintf(fn, sizeof fn,
  27. "devices/system/cpu/cpu%d/topology/thread_siblings",
  28. cpu);
  29. if (sysfs__read_str(fn, &str, &strlen) < 0)
  30. continue;
  31. }
  32. /* Entry is hex, but does not have 0x, so need custom parser */
  33. siblings = strtoull(str, NULL, 16);
  34. free(str);
  35. if (hweight64(siblings) > 1) {
  36. cached_result = 1;
  37. cached = true;
  38. break;
  39. }
  40. }
  41. if (!cached) {
  42. cached_result = 0;
  43. done:
  44. cached = true;
  45. }
  46. return cached_result;
  47. }