smt.c 845 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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/thread_siblings",
  23. cpu);
  24. if (sysfs__read_str(fn, &str, &strlen) < 0)
  25. continue;
  26. /* Entry is hex, but does not have 0x, so need custom parser */
  27. siblings = strtoull(str, NULL, 16);
  28. free(str);
  29. if (hweight64(siblings) > 1) {
  30. cached_result = 1;
  31. cached = true;
  32. break;
  33. }
  34. }
  35. if (!cached) {
  36. cached_result = 0;
  37. cached = true;
  38. }
  39. return cached_result;
  40. }