builtin-kvm.c 3.5 KB

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