builtin-kvm.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "builtin.h"
  2. #include "perf.h"
  3. #include "util/util.h"
  4. #include "util/cache.h"
  5. #include "util/symbol.h"
  6. #include "util/thread.h"
  7. #include "util/header.h"
  8. #include "util/session.h"
  9. #include "util/parse-options.h"
  10. #include "util/trace-event.h"
  11. #include "util/debug.h"
  12. #include <sys/prctl.h>
  13. #include <semaphore.h>
  14. #include <pthread.h>
  15. #include <math.h>
  16. static const char *file_name;
  17. static char name_buffer[256];
  18. bool perf_host = 1;
  19. bool perf_guest;
  20. static const char * const kvm_usage[] = {
  21. "perf kvm [<options>] {top|record|report|diff|buildid-list}",
  22. NULL
  23. };
  24. static const struct option kvm_options[] = {
  25. OPT_STRING('i', "input", &file_name, "file",
  26. "Input file name"),
  27. OPT_STRING('o', "output", &file_name, "file",
  28. "Output file name"),
  29. OPT_BOOLEAN(0, "guest", &perf_guest,
  30. "Collect guest os data"),
  31. OPT_BOOLEAN(0, "host", &perf_host,
  32. "Collect guest os data"),
  33. OPT_STRING(0, "guestmount", &symbol_conf.guestmount, "directory",
  34. "guest mount directory under which every guest os"
  35. " instance has a subdir"),
  36. OPT_STRING(0, "guestvmlinux", &symbol_conf.default_guest_vmlinux_name,
  37. "file", "file saving guest os vmlinux"),
  38. OPT_STRING(0, "guestkallsyms", &symbol_conf.default_guest_kallsyms,
  39. "file", "file saving guest os /proc/kallsyms"),
  40. OPT_STRING(0, "guestmodules", &symbol_conf.default_guest_modules,
  41. "file", "file saving guest os /proc/modules"),
  42. OPT_END()
  43. };
  44. static int __cmd_record(int argc, const char **argv)
  45. {
  46. int rec_argc, i = 0, j;
  47. const char **rec_argv;
  48. rec_argc = argc + 2;
  49. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  50. rec_argv[i++] = strdup("record");
  51. rec_argv[i++] = strdup("-o");
  52. rec_argv[i++] = strdup(file_name);
  53. for (j = 1; j < argc; j++, i++)
  54. rec_argv[i] = argv[j];
  55. BUG_ON(i != rec_argc);
  56. return cmd_record(i, rec_argv, NULL);
  57. }
  58. static int __cmd_report(int argc, const char **argv)
  59. {
  60. int rec_argc, i = 0, j;
  61. const char **rec_argv;
  62. rec_argc = argc + 2;
  63. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  64. rec_argv[i++] = strdup("report");
  65. rec_argv[i++] = strdup("-i");
  66. rec_argv[i++] = strdup(file_name);
  67. for (j = 1; j < argc; j++, i++)
  68. rec_argv[i] = argv[j];
  69. BUG_ON(i != rec_argc);
  70. return cmd_report(i, rec_argv, NULL);
  71. }
  72. static int __cmd_buildid_list(int argc, const char **argv)
  73. {
  74. int rec_argc, i = 0, j;
  75. const char **rec_argv;
  76. rec_argc = argc + 2;
  77. rec_argv = calloc(rec_argc + 1, sizeof(char *));
  78. rec_argv[i++] = strdup("buildid-list");
  79. rec_argv[i++] = strdup("-i");
  80. rec_argv[i++] = strdup(file_name);
  81. for (j = 1; j < argc; j++, i++)
  82. rec_argv[i] = argv[j];
  83. BUG_ON(i != rec_argc);
  84. return cmd_buildid_list(i, rec_argv, NULL);
  85. }
  86. int cmd_kvm(int argc, const char **argv, const char *prefix __used)
  87. {
  88. perf_host = perf_guest = 0;
  89. argc = parse_options(argc, argv, kvm_options, kvm_usage,
  90. PARSE_OPT_STOP_AT_NON_OPTION);
  91. if (!argc)
  92. usage_with_options(kvm_usage, kvm_options);
  93. if (!perf_host)
  94. perf_guest = 1;
  95. if (!file_name) {
  96. if (perf_host && !perf_guest)
  97. sprintf(name_buffer, "perf.data.host");
  98. else if (!perf_host && perf_guest)
  99. sprintf(name_buffer, "perf.data.guest");
  100. else
  101. sprintf(name_buffer, "perf.data.kvm");
  102. file_name = name_buffer;
  103. }
  104. if (!strncmp(argv[0], "rec", 3))
  105. return __cmd_record(argc, argv);
  106. else if (!strncmp(argv[0], "rep", 3))
  107. return __cmd_report(argc, argv);
  108. else if (!strncmp(argv[0], "diff", 4))
  109. return cmd_diff(argc, argv, NULL);
  110. else if (!strncmp(argv[0], "top", 3))
  111. return cmd_top(argc, argv, NULL);
  112. else if (!strncmp(argv[0], "buildid-list", 12))
  113. return __cmd_buildid_list(argc, argv);
  114. else
  115. usage_with_options(kvm_usage, kvm_options);
  116. return 0;
  117. }