1 #include <kvm/util.h> 2 #include <kvm/kvm-cmd.h> 3 #include <kvm/builtin-stat.h> 4 #include <kvm/kvm.h> 5 #include <kvm/parse-options.h> 6 7 #include <stdio.h> 8 #include <string.h> 9 #include <signal.h> 10 11 static bool mem; 12 static bool all; 13 static u64 instance_pid; 14 static const char *instance_name; 15 16 static const char * const stat_usage[] = { 17 "kvm stat [command] [--all] [-n name] [-p pid]", 18 NULL 19 }; 20 21 static const struct option stat_options[] = { 22 OPT_GROUP("Commands options:"), 23 OPT_BOOLEAN('m', "memory", &mem, "Display memory statistics"), 24 OPT_GROUP("Instance options:"), 25 OPT_BOOLEAN('a', "all", &all, "All instances"), 26 OPT_STRING('n', "name", &instance_name, "name", "Instance name"), 27 OPT_U64('p', "pid", &instance_pid, "Instance pid"), 28 OPT_END() 29 }; 30 31 static void parse_stat_options(int argc, const char **argv) 32 { 33 while (argc != 0) { 34 argc = parse_options(argc, argv, stat_options, stat_usage, 35 PARSE_OPT_STOP_AT_NON_OPTION); 36 if (argc != 0) 37 kvm_stat_help(); 38 } 39 } 40 41 void kvm_stat_help(void) 42 { 43 usage_with_options(stat_usage, stat_options); 44 } 45 46 static int do_memstat(const char *name, int pid) 47 { 48 printf("Sending memstat command to %s, output should be on the targets' terminal.\n", name); 49 return kill(pid, SIGKVMMEMSTAT); 50 } 51 52 int kvm_cmd_stat(int argc, const char **argv, const char *prefix) 53 { 54 parse_stat_options(argc, argv); 55 56 if (!mem) 57 usage_with_options(stat_usage, stat_options); 58 59 if (mem && all) 60 return kvm__enumerate_instances(do_memstat); 61 62 if (instance_name == NULL && 63 instance_pid == 0) 64 kvm_stat_help(); 65 66 if (instance_name) 67 instance_pid = kvm__get_pid_by_instance(instance_name); 68 69 if (instance_pid <= 0) 70 die("Failed locating instance"); 71 72 if (mem) { 73 printf("Sending memstat command to designated instance, output should be on the targets' terminal.\n"); 74 75 return kill(instance_pid, SIGKVMMEMSTAT); 76 } 77 78 return 0; 79 } 80