env.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "cpumap.h"
  3. #include "env.h"
  4. #include "sane_ctype.h"
  5. #include "util.h"
  6. #include <errno.h>
  7. #include <sys/utsname.h>
  8. struct perf_env perf_env;
  9. void perf_env__exit(struct perf_env *env)
  10. {
  11. int i;
  12. zfree(&env->hostname);
  13. zfree(&env->os_release);
  14. zfree(&env->version);
  15. zfree(&env->arch);
  16. zfree(&env->cpu_desc);
  17. zfree(&env->cpuid);
  18. zfree(&env->cmdline);
  19. zfree(&env->cmdline_argv);
  20. zfree(&env->sibling_cores);
  21. zfree(&env->sibling_threads);
  22. zfree(&env->pmu_mappings);
  23. zfree(&env->cpu);
  24. for (i = 0; i < env->nr_numa_nodes; i++)
  25. cpu_map__put(env->numa_nodes[i].map);
  26. zfree(&env->numa_nodes);
  27. for (i = 0; i < env->caches_cnt; i++)
  28. cpu_cache_level__free(&env->caches[i]);
  29. zfree(&env->caches);
  30. for (i = 0; i < env->nr_memory_nodes; i++)
  31. free(env->memory_nodes[i].set);
  32. zfree(&env->memory_nodes);
  33. }
  34. int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[])
  35. {
  36. int i;
  37. /* do not include NULL termination */
  38. env->cmdline_argv = calloc(argc, sizeof(char *));
  39. if (env->cmdline_argv == NULL)
  40. goto out_enomem;
  41. /*
  42. * Must copy argv contents because it gets moved around during option
  43. * parsing:
  44. */
  45. for (i = 0; i < argc ; i++) {
  46. env->cmdline_argv[i] = argv[i];
  47. if (env->cmdline_argv[i] == NULL)
  48. goto out_free;
  49. }
  50. env->nr_cmdline = argc;
  51. return 0;
  52. out_free:
  53. zfree(&env->cmdline_argv);
  54. out_enomem:
  55. return -ENOMEM;
  56. }
  57. int perf_env__read_cpu_topology_map(struct perf_env *env)
  58. {
  59. int cpu, nr_cpus;
  60. if (env->cpu != NULL)
  61. return 0;
  62. if (env->nr_cpus_avail == 0)
  63. env->nr_cpus_avail = cpu__max_present_cpu();
  64. nr_cpus = env->nr_cpus_avail;
  65. if (nr_cpus == -1)
  66. return -EINVAL;
  67. env->cpu = calloc(nr_cpus, sizeof(env->cpu[0]));
  68. if (env->cpu == NULL)
  69. return -ENOMEM;
  70. for (cpu = 0; cpu < nr_cpus; ++cpu) {
  71. env->cpu[cpu].core_id = cpu_map__get_core_id(cpu);
  72. env->cpu[cpu].socket_id = cpu_map__get_socket_id(cpu);
  73. }
  74. env->nr_cpus_avail = nr_cpus;
  75. return 0;
  76. }
  77. void cpu_cache_level__free(struct cpu_cache_level *cache)
  78. {
  79. free(cache->type);
  80. free(cache->map);
  81. free(cache->size);
  82. }
  83. /*
  84. * Return architecture name in a normalized form.
  85. * The conversion logic comes from the Makefile.
  86. */
  87. static const char *normalize_arch(char *arch)
  88. {
  89. if (!strcmp(arch, "x86_64"))
  90. return "x86";
  91. if (arch[0] == 'i' && arch[2] == '8' && arch[3] == '6')
  92. return "x86";
  93. if (!strcmp(arch, "sun4u") || !strncmp(arch, "sparc", 5))
  94. return "sparc";
  95. if (!strcmp(arch, "aarch64") || !strcmp(arch, "arm64"))
  96. return "arm64";
  97. if (!strncmp(arch, "arm", 3) || !strcmp(arch, "sa110"))
  98. return "arm";
  99. if (!strncmp(arch, "s390", 4))
  100. return "s390";
  101. if (!strncmp(arch, "parisc", 6))
  102. return "parisc";
  103. if (!strncmp(arch, "powerpc", 7) || !strncmp(arch, "ppc", 3))
  104. return "powerpc";
  105. if (!strncmp(arch, "mips", 4))
  106. return "mips";
  107. if (!strncmp(arch, "sh", 2) && isdigit(arch[2]))
  108. return "sh";
  109. return arch;
  110. }
  111. const char *perf_env__arch(struct perf_env *env)
  112. {
  113. struct utsname uts;
  114. char *arch_name;
  115. if (!env) { /* Assume local operation */
  116. if (uname(&uts) < 0)
  117. return NULL;
  118. arch_name = uts.machine;
  119. } else
  120. arch_name = env->arch;
  121. return normalize_arch(arch_name);
  122. }