env.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. zfree(&env->hostname);
  8. zfree(&env->os_release);
  9. zfree(&env->version);
  10. zfree(&env->arch);
  11. zfree(&env->cpu_desc);
  12. zfree(&env->cpuid);
  13. zfree(&env->cmdline);
  14. zfree(&env->cmdline_argv);
  15. zfree(&env->sibling_cores);
  16. zfree(&env->sibling_threads);
  17. zfree(&env->numa_nodes);
  18. zfree(&env->pmu_mappings);
  19. zfree(&env->cpu);
  20. }
  21. int perf_env__set_cmdline(struct perf_env *env, int argc, const char *argv[])
  22. {
  23. int i;
  24. /*
  25. * If env->cmdline_argv has already been set, do not override it. This allows
  26. * a command to set the cmdline, parse args and then call another
  27. * builtin function that implements a command -- e.g, cmd_kvm calling
  28. * cmd_record.
  29. */
  30. if (env->cmdline_argv != NULL)
  31. return 0;
  32. /* do not include NULL termination */
  33. env->cmdline_argv = calloc(argc, sizeof(char *));
  34. if (env->cmdline_argv == NULL)
  35. goto out_enomem;
  36. /*
  37. * Must copy argv contents because it gets moved around during option
  38. * parsing:
  39. */
  40. for (i = 0; i < argc ; i++) {
  41. env->cmdline_argv[i] = argv[i];
  42. if (env->cmdline_argv[i] == NULL)
  43. goto out_free;
  44. }
  45. env->nr_cmdline = argc;
  46. return 0;
  47. out_free:
  48. zfree(&env->cmdline_argv);
  49. out_enomem:
  50. return -ENOMEM;
  51. }
  52. int perf_env__read_cpu_topology_map(struct perf_env *env)
  53. {
  54. int cpu, nr_cpus;
  55. if (env->cpu != NULL)
  56. return 0;
  57. if (env->nr_cpus_avail == 0)
  58. env->nr_cpus_avail = sysconf(_SC_NPROCESSORS_CONF);
  59. nr_cpus = env->nr_cpus_avail;
  60. if (nr_cpus == -1)
  61. return -EINVAL;
  62. env->cpu = calloc(nr_cpus, sizeof(env->cpu[0]));
  63. if (env->cpu == NULL)
  64. return -ENOMEM;
  65. for (cpu = 0; cpu < nr_cpus; ++cpu) {
  66. env->cpu[cpu].core_id = cpu_map__get_core_id(cpu);
  67. env->cpu[cpu].socket_id = cpu_map__get_socket_id(cpu);
  68. }
  69. env->nr_cpus_avail = nr_cpus;
  70. return 0;
  71. }