smt.c 966 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. ncpu = sysconf(_SC_NPROCESSORS_CONF);
  16. for (cpu = 0; cpu < ncpu; cpu++) {
  17. unsigned long long siblings;
  18. char *str;
  19. size_t strlen;
  20. char fn[256];
  21. snprintf(fn, sizeof fn,
  22. "devices/system/cpu/cpu%d/topology/core_cpus", cpu);
  23. if (access(fn, F_OK) == -1) {
  24. snprintf(fn, sizeof fn,
  25. "devices/system/cpu/cpu%d/topology/thread_siblings",
  26. cpu);
  27. }
  28. if (sysfs__read_str(fn, &str, &strlen) < 0)
  29. continue;
  30. /* Entry is hex, but does not have 0x, so need custom parser */
  31. siblings = strtoull(str, NULL, 16);
  32. free(str);
  33. if (hweight64(siblings) > 1) {
  34. cached_result = 1;
  35. cached = true;
  36. break;
  37. }
  38. }
  39. if (!cached) {
  40. cached_result = 0;
  41. cached = true;
  42. }
  43. return cached_result;
  44. }