env.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "cpumap.h"
  2. #include "env.h"
  3. #include "util.h"
  4. struct perf_env perf_env;
  5. void perf_env__exit(struct perf_env *env)
  6. {
  7. int i;
  8. zfree(&env->hostname);
  9. zfree(&env->os_release);
  10. zfree(&env->version);
  11. zfree(&env->arch);
  12. zfree(&env->cpu_desc);
  13. zfree(&env->cpuid);
  14. zfree(&env->cmdline);
  15. zfree(&env->cmdline_argv);
  16. zfree(&env->sibling_cores);
  17. zfree(&env->sibling_threads);
  18. zfree(&env->pmu_mappings);
  19. zfree(&env->cpu);
  20. for (i = 0; i < env->nr_numa_nodes; i++)
  21. cpu_map__put(env->numa_nodes[i].map);
  22. zfree(&env->numa_nodes);
  23. for (i = 0; i < env->caches_cnt; i++)
  24. cpu_cache_level__free(&env->caches[i]);
  25. zfree(&env->caches);
  26. }
  27. int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[])
  28. {
  29. int i;
  30. /* do not include NULL termination */
  31. env->cmdline_argv = calloc(argc, sizeof(char *));
  32. if (env->cmdline_argv == NULL)
  33. goto out_enomem;
  34. /*
  35. * Must copy argv contents because it gets moved around during option
  36. * parsing:
  37. */
  38. for (i = 0; i < argc ; i++) {
  39. env->cmdline_argv[i] = argv[i];
  40. if (env->cmdline_argv[i] == NULL)
  41. goto out_free;
  42. }
  43. env->nr_cmdline = argc;
  44. return 0;
  45. out_free:
  46. zfree(&env->cmdline_argv);
  47. out_enomem:
  48. return -ENOMEM;
  49. }
  50. int perf_env__read_cpu_topology_map(struct perf_env *env)
  51. {
  52. int cpu, nr_cpus;
  53. if (env->cpu != NULL)
  54. return 0;
  55. if (env->nr_cpus_avail == 0)
  56. env->nr_cpus_avail = sysconf(_SC_NPROCESSORS_CONF);
  57. nr_cpus = env->nr_cpus_avail;
  58. if (nr_cpus == -1)
  59. return -EINVAL;
  60. env->cpu = calloc(nr_cpus, sizeof(env->cpu[0]));
  61. if (env->cpu == NULL)
  62. return -ENOMEM;
  63. for (cpu = 0; cpu < nr_cpus; ++cpu) {
  64. env->cpu[cpu].core_id = cpu_map__get_core_id(cpu);
  65. env->cpu[cpu].socket_id = cpu_map__get_socket_id(cpu);
  66. }
  67. env->nr_cpus_avail = nr_cpus;
  68. return 0;
  69. }
  70. void cpu_cache_level__free(struct cpu_cache_level *cache)
  71. {
  72. free(cache->type);
  73. free(cache->map);
  74. free(cache->size);
  75. }