header.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <api/fs/fs.h>
  4. #include "header.h"
  5. #define MIDR "/regs/identification/midr_el1"
  6. #define MIDR_SIZE 19
  7. #define MIDR_REVISION_MASK 0xf
  8. #define MIDR_VARIANT_SHIFT 20
  9. #define MIDR_VARIANT_MASK (0xf << MIDR_VARIANT_SHIFT)
  10. char *get_cpuid_str(struct perf_pmu *pmu)
  11. {
  12. char *buf = NULL;
  13. char path[PATH_MAX];
  14. const char *sysfs = sysfs__mountpoint();
  15. int cpu;
  16. u64 midr = 0;
  17. struct cpu_map *cpus;
  18. FILE *file;
  19. if (!sysfs || !pmu || !pmu->cpus)
  20. return NULL;
  21. buf = malloc(MIDR_SIZE);
  22. if (!buf)
  23. return NULL;
  24. /* read midr from list of cpus mapped to this pmu */
  25. cpus = cpu_map__get(pmu->cpus);
  26. for (cpu = 0; cpu < cpus->nr; cpu++) {
  27. scnprintf(path, PATH_MAX, "%s/devices/system/cpu/cpu%d"MIDR,
  28. sysfs, cpus->map[cpu]);
  29. file = fopen(path, "r");
  30. if (!file) {
  31. pr_debug("fopen failed for file %s\n", path);
  32. continue;
  33. }
  34. if (!fgets(buf, MIDR_SIZE, file)) {
  35. fclose(file);
  36. continue;
  37. }
  38. fclose(file);
  39. /* Ignore/clear Variant[23:20] and
  40. * Revision[3:0] of MIDR
  41. */
  42. midr = strtoul(buf, NULL, 16);
  43. midr &= (~(MIDR_VARIANT_MASK | MIDR_REVISION_MASK));
  44. scnprintf(buf, MIDR_SIZE, "0x%016lx", midr);
  45. /* got midr break loop */
  46. break;
  47. }
  48. if (!midr) {
  49. pr_err("failed to get cpuid string for PMU %s\n", pmu->name);
  50. free(buf);
  51. buf = NULL;
  52. }
  53. cpu_map__put(cpus);
  54. return buf;
  55. }