debuggers.hg

changeset 21141:fe30bd463e84

xl: vcpu-list command

Signed-off-by: Eric Chanudet <eric.chanudet@citrix.com>
Acked-by: Vincent Hanquez <vincent.hanquez@eu.citrix.com>
Acked-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Apr 06 06:54:08 2010 +0100 (2010-04-06)
parents cd6c6c685015
children 98e7aff6ee19
files tools/libxl/libxl.c tools/libxl/libxl.h tools/libxl/xl.c
line diff
     1.1 --- a/tools/libxl/libxl.c	Tue Apr 06 06:52:11 2010 +0100
     1.2 +++ b/tools/libxl/libxl.c	Tue Apr 06 06:54:08 2010 +0100
     1.3 @@ -2221,3 +2221,64 @@ int libxl_button_press(struct libxl_ctx 
     1.4  
     1.5      return rc;
     1.6  }
     1.7 +
     1.8 +int libxl_get_physinfo(struct libxl_ctx *ctx, struct libxl_physinfo *physinfo)
     1.9 +{
    1.10 +    xc_physinfo_t xcphysinfo = { 0 };
    1.11 +    int rc;
    1.12 +
    1.13 +    rc = xc_physinfo(ctx->xch, &xcphysinfo);
    1.14 +    if (rc != 0) {
    1.15 +        return rc;
    1.16 +    }
    1.17 +    physinfo->threads_per_core = xcphysinfo.threads_per_core;
    1.18 +    physinfo->cores_per_socket = xcphysinfo.cores_per_socket;
    1.19 +    physinfo->nr_cpus = xcphysinfo.nr_cpus;
    1.20 +    physinfo->cpu_khz = xcphysinfo.cpu_khz;
    1.21 +    physinfo->total_pages = xcphysinfo.total_pages;
    1.22 +    physinfo->free_pages = xcphysinfo.free_pages;
    1.23 +    physinfo->scrub_pages = xcphysinfo.scrub_pages;
    1.24 +    return 0;
    1.25 +}
    1.26 +
    1.27 +struct libxl_vcpuinfo *libxl_list_vcpu(struct libxl_ctx *ctx, uint32_t domid,
    1.28 +                                       int *nb_vcpu, int *cpusize)
    1.29 +{
    1.30 +    struct libxl_vcpuinfo *ptr, *ret;
    1.31 +    xc_domaininfo_t domaininfo;
    1.32 +    xc_vcpuinfo_t vcpuinfo;
    1.33 +    xc_physinfo_t physinfo = { 0 };
    1.34 +
    1.35 +    if (xc_domain_getinfolist(ctx->xch, domid, 1, &domaininfo) != 1) {
    1.36 +        return NULL;
    1.37 +    }
    1.38 +    if (xc_physinfo(ctx->xch, &physinfo) == -1) {
    1.39 +        return NULL;
    1.40 +    }
    1.41 +    *cpusize = physinfo.max_cpu_id + 1;
    1.42 +    ptr = libxl_calloc(ctx, domaininfo.max_vcpu_id + 1, sizeof (struct libxl_vcpuinfo));
    1.43 +    if (!ptr) {
    1.44 +        return NULL;
    1.45 +    }
    1.46 +
    1.47 +    ret = ptr;
    1.48 +    for (*nb_vcpu = 0; *nb_vcpu <= domaininfo.max_vcpu_id; ++*nb_vcpu, ++ptr) {
    1.49 +        ptr->cpumap = libxl_calloc(ctx, (*cpusize + 63) / 64, sizeof (uint64_t));
    1.50 +        if (!ptr->cpumap) {
    1.51 +            return NULL;
    1.52 +        }
    1.53 +        if (xc_vcpu_getinfo(ctx->xch, domid, *nb_vcpu, &vcpuinfo) == -1) {
    1.54 +            return NULL;
    1.55 +        }
    1.56 +        if (xc_vcpu_getaffinity(ctx->xch, domid, *nb_vcpu, ptr->cpumap, *cpusize) == -1) {
    1.57 +            return NULL;
    1.58 +        }
    1.59 +        ptr->vcpuid = *nb_vcpu;
    1.60 +        ptr->cpu = vcpuinfo.cpu;
    1.61 +        ptr->online = !!vcpuinfo.online;
    1.62 +        ptr->blocked = !!vcpuinfo.blocked;
    1.63 +        ptr->running = !!vcpuinfo.running;
    1.64 +        ptr->vcpu_time = vcpuinfo.cpu_time;
    1.65 +    }
    1.66 +    return ret;
    1.67 +}
     2.1 --- a/tools/libxl/libxl.h	Tue Apr 06 06:52:11 2010 +0100
     2.2 +++ b/tools/libxl/libxl.h	Tue Apr 06 06:54:08 2010 +0100
     2.3 @@ -360,5 +360,31 @@ typedef enum {
     2.4  
     2.5  int libxl_button_press(struct libxl_ctx *ctx, uint32_t domid, libxl_button button);
     2.6  
     2.7 +struct libxl_vcpuinfo {
     2.8 +    uint32_t vcpuid; /* vcpu's id */
     2.9 +    uint32_t cpu; /* current mapping */
    2.10 +    uint8_t online:1; /* currently online (not hotplugged)? */
    2.11 +    uint8_t blocked:1; /* blocked waiting for an event? */
    2.12 +    uint8_t running:1; /* currently scheduled on its CPU? */
    2.13 +    uint64_t vcpu_time; /* total vcpu time ran (ns) */
    2.14 +    uint64_t *cpumap; /* current cpu's affinities */
    2.15 +};
    2.16 +
    2.17 +struct libxl_physinfo {
    2.18 +    uint32_t threads_per_core;
    2.19 +    uint32_t cores_per_socket;
    2.20 +
    2.21 +    uint32_t nr_cpus;
    2.22 +    uint32_t cpu_khz;
    2.23 +
    2.24 +    uint64_t total_pages;
    2.25 +    uint64_t free_pages;
    2.26 +    uint64_t scrub_pages;
    2.27 +};
    2.28 +
    2.29 +int libxl_get_physinfo(struct libxl_ctx *ctx, struct libxl_physinfo *physinfo);
    2.30 +struct libxl_vcpuinfo *libxl_list_vcpu(struct libxl_ctx *ctx, uint32_t domid,
    2.31 +                                       int *nb_vcpu, int *cpusize);
    2.32 +
    2.33  #endif /* LIBXL_H */
    2.34  
     3.1 --- a/tools/libxl/xl.c	Tue Apr 06 06:52:11 2010 +0100
     3.2 +++ b/tools/libxl/xl.c	Tue Apr 06 06:54:08 2010 +0100
     3.3 @@ -876,6 +876,7 @@ static void help(char *command)
     3.4          printf(" cd-eject                      eject a cdrom from a guest's cd drive\n\n");
     3.5          printf(" mem-set                       set the current memory usage for a domain\n\n");
     3.6          printf(" button-press                  indicate an ACPI button press to the domain\n\n");
     3.7 +        printf(" vcpu-list                     list the VCPUs for all/some domains.\n\n");
     3.8      } else if(!strcmp(command, "create")) {
     3.9          printf("Usage: xl create <ConfigFile> [options] [vars]\n\n");
    3.10          printf("Create a domain based on <ConfigFile>.\n\n");
    3.11 @@ -933,6 +934,9 @@ static void help(char *command)
    3.12          printf("Usage: xl button-press <Domain> <Button>\n\n");
    3.13          printf("Indicate <Button> press to a domain.\n");
    3.14          printf("<Button> may be 'power' or 'sleep'.\n\n");
    3.15 +    } else if (!strcmp(command, "vcpu-list")) {
    3.16 +        printf("Usage: xl vcpu-list [Domain, ...]\n\n");
    3.17 +        printf("List the VCPUs for all/some domains.\n\n");
    3.18      }
    3.19  }
    3.20  
    3.21 @@ -1721,6 +1725,144 @@ int main_button_press(int argc, char **a
    3.22      exit(0);
    3.23  }
    3.24  
    3.25 +static void print_vcpuinfo(struct libxl_ctx *ctx, uint32_t domid,
    3.26 +                           const struct libxl_vcpuinfo *vcpuinfo,
    3.27 +                           uint32_t nr_cpus)
    3.28 +{
    3.29 +    int i, l;
    3.30 +    uint64_t *cpumap;
    3.31 +    uint64_t pcpumap;
    3.32 +
    3.33 +    /*      NAME  ID  VCPU */
    3.34 +    printf("%-32s %5u %5u",
    3.35 +           libxl_domid_to_name(ctx, domid), domid, vcpuinfo->vcpuid);
    3.36 +    if (!vcpuinfo->online) {
    3.37 +        /*      CPU STA */
    3.38 +        printf("%5c %3c%cp ", '-', '-', '-');
    3.39 +    } else {
    3.40 +        /*      CPU STA */
    3.41 +        printf("%5u %3c%c- ", vcpuinfo->cpu,
    3.42 +               vcpuinfo->running ? 'r' : '-',
    3.43 +               vcpuinfo->blocked ? 'b' : '-');
    3.44 +    }
    3.45 +    /*      TIM */
    3.46 +    printf("%9.1f  ", ((float)vcpuinfo->vcpu_time / 1e9));
    3.47 +    /* CPU AFFINITY */
    3.48 +    pcpumap = nr_cpus > 64 ? -1 : ((1 << nr_cpus) - 1);
    3.49 +    for (cpumap = vcpuinfo->cpumap; nr_cpus; ++cpumap) {
    3.50 +        if (*cpumap < pcpumap) {
    3.51 +            break;
    3.52 +        }
    3.53 +        if (nr_cpus > 64) {
    3.54 +            pcpumap = -1;
    3.55 +            nr_cpus -= 64;
    3.56 +        } else {
    3.57 +            pcpumap = ((1 << nr_cpus) - 1);
    3.58 +            nr_cpus = 0;
    3.59 +        }
    3.60 +    }
    3.61 +    if (!nr_cpus) {
    3.62 +        printf("any cpu\n");
    3.63 +    } else {
    3.64 +        for (cpumap = vcpuinfo->cpumap; nr_cpus; ++cpumap) {
    3.65 +            pcpumap = *cpumap;
    3.66 +            for (i = 0; !(pcpumap & 1); ++i, pcpumap >>= 1)
    3.67 +                ;
    3.68 +            printf("%u", i);
    3.69 +            for (l = i, pcpumap = (pcpumap >> 1); (pcpumap & 1); ++i, pcpumap >>= 1)
    3.70 +                ;
    3.71 +            if (l < i) {
    3.72 +                printf("-%u", i);
    3.73 +            }
    3.74 +            for (++i; pcpumap; ++i, pcpumap >>= 1) {
    3.75 +                if (pcpumap & 1) {
    3.76 +                    printf(",%u", i);
    3.77 +                    for (l = i, pcpumap = (pcpumap >> 1); (pcpumap & 1); ++i, pcpumap >>= 1)
    3.78 +                        ;
    3.79 +                    if (l < i) {
    3.80 +                        printf("-%u", i);
    3.81 +                    }
    3.82 +                    ++i;
    3.83 +                }
    3.84 +            }
    3.85 +            printf("\n");
    3.86 +            nr_cpus = nr_cpus > 64 ? nr_cpus - 64 : 0;
    3.87 +        }
    3.88 +    }
    3.89 +}
    3.90 +
    3.91 +void vcpulist(int argc, char **argv)
    3.92 +{
    3.93 +    struct libxl_ctx ctx;
    3.94 +    struct libxl_dominfo *dominfo;
    3.95 +    uint32_t domid;
    3.96 +    struct libxl_vcpuinfo *vcpuinfo;
    3.97 +    struct libxl_physinfo physinfo;
    3.98 +    int nb_vcpu, nb_domain, cpusize;
    3.99 +
   3.100 +    if (libxl_ctx_init(&ctx, LIBXL_VERSION)) {
   3.101 +        fprintf(stderr, "cannot init xl context\n");
   3.102 +        return;
   3.103 +    }
   3.104 +    libxl_ctx_set_log(&ctx, log_callback, NULL);
   3.105 +
   3.106 +    if (libxl_get_physinfo(&ctx, &physinfo) != 0) {
   3.107 +        fprintf(stderr, "libxl_physinfo failed.\n");
   3.108 +        goto vcpulist_out;
   3.109 +    }
   3.110 +    printf("%-32s %5s %5s %5s %5s %9s %s\n",
   3.111 +           "Name", "ID", "VCPU", "CPU", "State", "Time(s)", "CPU Affinity");
   3.112 +    if (!argc) {
   3.113 +        if (!(dominfo = libxl_list_domain(&ctx, &nb_domain))) {
   3.114 +            fprintf(stderr, "libxl_list_domain failed.\n");
   3.115 +            goto vcpulist_out;
   3.116 +        }
   3.117 +        for (; nb_domain > 0; --nb_domain, ++dominfo) {
   3.118 +            if (!(vcpuinfo = libxl_list_vcpu(&ctx, dominfo->domid, &nb_vcpu, &cpusize))) {
   3.119 +                fprintf(stderr, "libxl_list_vcpu failed.\n");
   3.120 +                goto vcpulist_out;
   3.121 +            }
   3.122 +            for (; nb_vcpu > 0; --nb_vcpu, ++vcpuinfo) {
   3.123 +                print_vcpuinfo(&ctx, dominfo->domid, vcpuinfo, physinfo.nr_cpus);
   3.124 +            }
   3.125 +        }
   3.126 +    } else {
   3.127 +        for (; argc > 0; ++argv, --argc) {
   3.128 +            if (domain_qualifier_to_domid(&ctx, *argv, &domid) < 0) {
   3.129 +                fprintf(stderr, "%s is an invalid domain identifier\n", *argv);
   3.130 +            }
   3.131 +            if (!(vcpuinfo = libxl_list_vcpu(&ctx, domid, &nb_vcpu, &cpusize))) {
   3.132 +                fprintf(stderr, "libxl_list_vcpu failed.\n");
   3.133 +                goto vcpulist_out;
   3.134 +            }
   3.135 +            for (; nb_vcpu > 0; --nb_vcpu, ++vcpuinfo) {
   3.136 +                print_vcpuinfo(&ctx, domid, vcpuinfo, physinfo.nr_cpus);
   3.137 +            }
   3.138 +        }
   3.139 +    }
   3.140 +  vcpulist_out:
   3.141 +    libxl_ctx_free(&ctx);
   3.142 +}
   3.143 +
   3.144 +void main_vcpulist(int argc, char **argv)
   3.145 +{
   3.146 +    int opt;
   3.147 +
   3.148 +    while ((opt = getopt(argc, argv, "h")) != -1) {
   3.149 +        switch (opt) {
   3.150 +        case 'h':
   3.151 +            help("vcpu-list");
   3.152 +            exit(0);
   3.153 +        default:
   3.154 +            fprintf(stderr, "option `%c' not supported.\n", opt);
   3.155 +            break;
   3.156 +        }
   3.157 +    }
   3.158 +
   3.159 +    vcpulist(argc - 1, argv + 1);
   3.160 +    exit(0);
   3.161 +}
   3.162 +
   3.163  int main(int argc, char **argv)
   3.164  {
   3.165      if (argc < 2) {
   3.166 @@ -1762,6 +1904,8 @@ int main(int argc, char **argv)
   3.167          main_memset(argc - 1, argv + 1);
   3.168      } else if (!strcmp(argv[1], "button-press")) {
   3.169          main_button_press(argc - 1, argv + 1);
   3.170 +    } else if (!strcmp(argv[1], "vcpu-list")) {
   3.171 +        main_vcpulist(argc - 1, argv + 1);
   3.172      } else if (!strcmp(argv[1], "help")) {
   3.173          if (argc > 2)
   3.174              help(argv[2]);
   3.175 @@ -1773,4 +1917,3 @@ int main(int argc, char **argv)
   3.176          exit(1);
   3.177      }
   3.178  }
   3.179 -