Add "xl uptime" command, a clone of "xm uptime". Signed-off-by: Yang Hongyang <yanghy@cn.fujitsu.com> diff -r d77a88f938c6 tools/libxl/libxl.c --- a/tools/libxl/libxl.c Tue May 11 14:05:28 2010 +0100 +++ b/tools/libxl/libxl.c Fri May 14 01:25:56 2010 +0800 @@ -2629,3 +2629,23 @@ return rc; } + +unsigned long libxl_get_start_time(struct libxl_ctx *ctx, uint32_t domid) +{ + char *dompath = libxl_xs_get_dompath(ctx, domid); + unsigned long s_time = 0; + char *start_time = NULL; + char *vm_path = NULL; + + vm_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/vm", dompath)); + start_time = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/start_time", vm_path)); + if (start_time == NULL) { + XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, -1, + "Can''t get start time of domain ''%d''", domid); + return -1; + } + s_time = strtoul(start_time, NULL, 10); + + return s_time; +} + diff -r d77a88f938c6 tools/libxl/libxl.h --- a/tools/libxl/libxl.h Tue May 11 14:05:28 2010 +0100 +++ b/tools/libxl/libxl.h Fri May 14 01:25:56 2010 +0800 @@ -481,5 +481,6 @@ struct libxl_sched_credit *scinfo); int libxl_send_trigger(struct libxl_ctx *ctx, uint32_t domid, char *trigger_name, uint32_t vcpuid); +unsigned long libxl_get_start_time(struct libxl_ctx *ctx, uint32_t domid); #endif /* LIBXL_H */ diff -r d77a88f938c6 tools/libxl/xl_cmdimpl.c --- a/tools/libxl/xl_cmdimpl.c Tue May 11 14:05:28 2010 +0100 +++ b/tools/libxl/xl_cmdimpl.c Fri May 14 01:25:56 2010 +0800 @@ -3058,3 +3058,160 @@ exit(0); } + +static char *uptime_to_string(unsigned long time, int short_mode) +{ + int sec, min, hour, day; + char *time_string; + + day = (int)(time / 86400); + time -= (day * 86400); + hour = (int)(time / 3600); + time -= (hour * 3600); + min = (int)(time / 60); + time -= (min * 60); + sec = time; + + if (short_mode) + if (day > 1) + asprintf(&time_string, "%d days, %2d:%02d", day, hour, min); + else if (day == 1) + asprintf(&time_string, "%d day, %2d:%02d", day, hour, min); + else + asprintf(&time_string, "%2d:%02d", hour, min); + else + if (day > 1) + asprintf(&time_string, "%d days, %2d:%02d:%02d", day, hour, min, sec); + else if (day == 1) + asprintf(&time_string, "%d day, %2d:%02d:%02d", day, hour, min, sec); + else + asprintf(&time_string, "%2d:%02d:%02d", hour, min, sec); + + return time_string; +} + +static char *current_time_to_string(time_t now) +{ + char now_str[100]; + struct tm *tmp; + + tmp = localtime(&now); + if (tmp == NULL) { + fprintf(stderr, "Get localtime error"); + exit(-1); + } + if (strftime(now_str, sizeof(now_str), "%H:%M:%S", tmp) == 0) { + fprintf(stderr, "strftime returned 0"); + exit(-1); + } + return strdup(now_str); +} + +static void print_dom0_uptime(int short_mode, time_t now) +{ + int fd; + char buf[512]; + unsigned long uptime = 0; + + fd = open("/proc/uptime", ''r''); + if (fd == -1) + goto err; + + if (read(fd, buf, sizeof(buf)) == -1) { + close(fd); + goto err; + } + close(fd); + + strtok(buf, " "); + uptime = strtoul(buf, NULL, 10); + + if (short_mode) + printf(" %s up %s, %s (%d)\n", current_time_to_string(now), + uptime_to_string(uptime, 1), libxl_domid_to_name(&ctx, 0), 0); + else + printf("%-33s %4d %s\n", libxl_domid_to_name(&ctx, 0), + 0, uptime_to_string(uptime, 0)); + + return; +err: + fprintf(stderr, "Can not get Dom0 uptime.\n"); + exit(-1); +} + +static void print_domU_uptime(uint32_t domuid, int short_mode, time_t now) +{ + unsigned long s_time = 0; + unsigned long uptime = 0; + + s_time = libxl_get_start_time(&ctx, domuid); + if (s_time == -1) + return; + uptime = now - s_time; + if (short_mode) + printf(" %s up %s, %s (%d)\n", current_time_to_string(now), + uptime_to_string(uptime, 1), + libxl_domid_to_name(&ctx, domuid), + domuid); + else + printf("%-33s %4d %s\n", libxl_domid_to_name(&ctx, domuid), + domuid, uptime_to_string(uptime, 0)); +} + +static void print_uptime(int short_mode, uint32_t doms[], int nb_doms) +{ + struct libxl_vminfo *info; + time_t now; + int nb_vm, i; + + now = time(NULL); + + if (!short_mode) + printf("%-33s %4s %s\n", "Name", "ID", "Uptime"); + + if (nb_doms == 0) { + print_dom0_uptime(short_mode, now); + info = libxl_list_vm(&ctx, &nb_vm); + for (i = 0; i < nb_vm; i++) + print_domU_uptime(info[i].domid, short_mode, now); + } else { + for (i = 0; i < nb_doms; i++) { + if (doms[i] == 0) + print_dom0_uptime(short_mode, now); + else + print_domU_uptime(doms[i], short_mode, now); + } + } +} + +int main_uptime(int argc, char **argv) +{ + char *dom = NULL; + int short_mode = 0; + uint32_t domains[100]; + int nb_doms = 0; + int opt; + + while ((opt = getopt(argc, argv, "hs")) != -1) { + switch (opt) { + case ''s'': + short_mode = 1; + break; + case ''h'': + help("uptime"); + exit(0); + default: + fprintf(stderr, "option `%c'' not supported.\n", opt); + break; + } + } + + for (;(dom = argv[optind]) != NULL; nb_doms++,optind++) { + find_domain(dom); + domains[nb_doms] = domid; + } + + print_uptime(short_mode, domains, nb_doms); + + exit(0); +} diff -r d77a88f938c6 tools/libxl/xl_cmdimpl.h --- a/tools/libxl/xl_cmdimpl.h Tue May 11 14:05:28 2010 +0100 +++ b/tools/libxl/xl_cmdimpl.h Fri May 14 01:25:56 2010 +0800 @@ -39,5 +39,6 @@ int main_domname(int argc, char **argv); int main_rename(int argc, char **argv); int main_trigger(int argc, char **argv); +int main_uptime(int argc, char **argv); void help(char *command); diff -r d77a88f938c6 tools/libxl/xl_cmdtable.c --- a/tools/libxl/xl_cmdtable.c Tue May 11 14:05:28 2010 +0100 +++ b/tools/libxl/xl_cmdtable.c Fri May 14 01:25:56 2010 +0800 @@ -172,6 +172,11 @@ "Send a trigger to a domain", "<Domain> <nmi|reset|init|power|sleep> [<VCPU>]", }, + { "uptime", + &main_uptime, + "Print uptime for all/some domains", + "[-s] [Domain ...]", + }, }; int cmdtable_len = sizeof(cmd_table)/sizeof(struct cmd_spec); -- Regards Yang Hongyang _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
Vincent Hanquez
2010-May-13 09:54 UTC
Re: [Xen-devel] [PATCH 1/2]xl: Add "xl uptime" command
On 13/05/10 10:36, Yang Hongyang wrote:> Add "xl uptime" command, a clone of "xm uptime". > > Signed-off-by: Yang Hongyang<yanghy@cn.fujitsu.com> > > diff -r d77a88f938c6 tools/libxl/libxl.c > --- a/tools/libxl/libxl.c Tue May 11 14:05:28 2010 +0100 > +++ b/tools/libxl/libxl.c Fri May 14 01:25:56 2010 +0800 > @@ -2629,3 +2629,23 @@ > > return rc; > } > + > +unsigned long libxl_get_start_time(struct libxl_ctx *ctx, uint32_t domid) > +{ > + char *dompath = libxl_xs_get_dompath(ctx, domid); > + unsigned long s_time = 0; > + char *start_time = NULL; > + char *vm_path = NULL; > + > + vm_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/vm", dompath)); > + start_time = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/start_time", vm_path)); > + if (start_time == NULL) { > + XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, -1, > + "Can''t get start time of domain ''%d''", domid); > + return -1; > + } > + s_time = strtoul(start_time, NULL, 10); > + > + return s_time; > +}this function should more than likely return a uint64_t or a uint32_t, but certainly not an unsigned long. and since it apply to a domain it should be call libxl_domain_get_start_time. I find this command basically useless, since on restore the "start_time" is not kept. do we actually need this ? -- Vincent _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
Yang Hongyang
2010-May-13 10:10 UTC
Re: [Xen-devel] [PATCH 1/2]xl: Add "xl uptime" command
Hi Vincent, On 05/13/2010 05:54 PM, Vincent Hanquez wrote:> On 13/05/10 10:36, Yang Hongyang wrote: >> Add "xl uptime" command, a clone of "xm uptime". >> >> Signed-off-by: Yang Hongyang<yanghy@cn.fujitsu.com> >> >> diff -r d77a88f938c6 tools/libxl/libxl.c >> --- a/tools/libxl/libxl.c Tue May 11 14:05:28 2010 +0100 >> +++ b/tools/libxl/libxl.c Fri May 14 01:25:56 2010 +0800 >> @@ -2629,3 +2629,23 @@ >> >> return rc; >> } >> + >> +unsigned long libxl_get_start_time(struct libxl_ctx *ctx, uint32_t >> domid) >> +{ >> + char *dompath = libxl_xs_get_dompath(ctx, domid); >> + unsigned long s_time = 0; >> + char *start_time = NULL; >> + char *vm_path = NULL; >> + >> + vm_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, >> "%s/vm", dompath)); >> + start_time = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, >> "%s/start_time", vm_path)); >> + if (start_time == NULL) { >> + XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, -1, >> + "Can''t get start time of domain ''%d''", domid); >> + return -1; >> + } >> + s_time = strtoul(start_time, NULL, 10); >> + >> + return s_time; >> +} > > this function should more than likely return a uint64_t or a uint32_t, > but certainly not an unsigned long. and since it apply to a domain it > should be call libxl_domain_get_start_time.Thank you for your review,I''ll fix that.> > I find this command basically useless, since on restore the "start_time" > is not kept. do we actually need this ? >I think it''s sometimes useful. I noticed that just the day before yesterday, somebody asked in the xen-api list about "uptime" command. -------- Original Message -------- Subject: [Xen-API] Want to find out the uptime of individual VM running on Citrix Xen Date: Tue, 11 May 2010 04:00:13 -0600 From: Sane, Abhijeet <Abhijeet_Sane@bmc.com> To: xen-api@lists.xensource.com <xen-api@lists.xensource.com> Hi, I want to know if it is possible to get the uptime of individual VM that are running on the Citrix host using the Citrix Xen python API.. Something on the lines of ‘xm uptime’ Regards Abhijeet Sane -- Regards Yang Hongyang _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
Vincent Hanquez
2010-May-13 10:18 UTC
Re: [Xen-devel] [PATCH 1/2]xl: Add "xl uptime" command
On 13/05/10 11:10, Yang Hongyang wrote:>> I find this command basically useless, since on restore the "start_time" >> is not kept. do we actually need this ? >> > > I think it''s sometimes useful. I noticed that just the day before yesterday, > somebody asked in the xen-api list about "uptime" command.it is useful if it works :) however this version of start_time means "when was this xen domain started" which is not the VM start time (i.e. "when was my vm booted"). To be useful start_time need to preserved across migration and suspend/restore. xen-api does the right thing and returns the later. -- Vincent _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
v1->v2: 1.libxl_get_start_time() is apply to a vm so use libxl_vm_get_start_time instead. 2.replace unsigned long with uint32_t. ========================================================================== Add "xl uptime" command, a clone of "xm uptime". Signed-off-by: Yang Hongyang <yanghy@cn.fujitsu.com> diff -r d77a88f938c6 tools/libxl/libxl.c --- a/tools/libxl/libxl.c Tue May 11 14:05:28 2010 +0100 +++ b/tools/libxl/libxl.c Fri May 14 02:27:59 2010 +0800 @@ -2629,3 +2629,23 @@ return rc; } + +uint32_t libxl_vm_get_start_time(struct libxl_ctx *ctx, uint32_t domid) +{ + char *dompath = libxl_xs_get_dompath(ctx, domid); + unsigned long s_time = 0; + char *start_time = NULL; + char *vm_path = NULL; + + vm_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/vm", dompath)); + start_time = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/start_time", vm_path)); + if (start_time == NULL) { + XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, -1, + "Can''t get start time of domain ''%d''", domid); + return -1; + } + s_time = strtoul(start_time, NULL, 10); + + return s_time; +} + diff -r d77a88f938c6 tools/libxl/libxl.h --- a/tools/libxl/libxl.h Tue May 11 14:05:28 2010 +0100 +++ b/tools/libxl/libxl.h Fri May 14 02:27:59 2010 +0800 @@ -481,5 +481,6 @@ struct libxl_sched_credit *scinfo); int libxl_send_trigger(struct libxl_ctx *ctx, uint32_t domid, char *trigger_name, uint32_t vcpuid); +uint32_t libxl_vm_get_start_time(struct libxl_ctx *ctx, uint32_t domid); #endif /* LIBXL_H */ diff -r d77a88f938c6 tools/libxl/xl_cmdimpl.c --- a/tools/libxl/xl_cmdimpl.c Tue May 11 14:05:28 2010 +0100 +++ b/tools/libxl/xl_cmdimpl.c Fri May 14 02:27:59 2010 +0800 @@ -3058,3 +3058,160 @@ exit(0); } + +static char *uptime_to_string(unsigned long time, int short_mode) +{ + int sec, min, hour, day; + char *time_string; + + day = (int)(time / 86400); + time -= (day * 86400); + hour = (int)(time / 3600); + time -= (hour * 3600); + min = (int)(time / 60); + time -= (min * 60); + sec = time; + + if (short_mode) + if (day > 1) + asprintf(&time_string, "%d days, %2d:%02d", day, hour, min); + else if (day == 1) + asprintf(&time_string, "%d day, %2d:%02d", day, hour, min); + else + asprintf(&time_string, "%2d:%02d", hour, min); + else + if (day > 1) + asprintf(&time_string, "%d days, %2d:%02d:%02d", day, hour, min, sec); + else if (day == 1) + asprintf(&time_string, "%d day, %2d:%02d:%02d", day, hour, min, sec); + else + asprintf(&time_string, "%2d:%02d:%02d", hour, min, sec); + + return time_string; +} + +static char *current_time_to_string(time_t now) +{ + char now_str[100]; + struct tm *tmp; + + tmp = localtime(&now); + if (tmp == NULL) { + fprintf(stderr, "Get localtime error"); + exit(-1); + } + if (strftime(now_str, sizeof(now_str), "%H:%M:%S", tmp) == 0) { + fprintf(stderr, "strftime returned 0"); + exit(-1); + } + return strdup(now_str); +} + +static void print_dom0_uptime(int short_mode, time_t now) +{ + int fd; + char buf[512]; + uint32_t uptime = 0; + + fd = open("/proc/uptime", ''r''); + if (fd == -1) + goto err; + + if (read(fd, buf, sizeof(buf)) == -1) { + close(fd); + goto err; + } + close(fd); + + strtok(buf, " "); + uptime = strtoul(buf, NULL, 10); + + if (short_mode) + printf(" %s up %s, %s (%d)\n", current_time_to_string(now), + uptime_to_string(uptime, 1), libxl_domid_to_name(&ctx, 0), 0); + else + printf("%-33s %4d %s\n", libxl_domid_to_name(&ctx, 0), + 0, uptime_to_string(uptime, 0)); + + return; +err: + fprintf(stderr, "Can not get Dom0 uptime.\n"); + exit(-1); +} + +static void print_domU_uptime(uint32_t domuid, int short_mode, time_t now) +{ + uint32_t s_time = 0; + uint32_t uptime = 0; + + s_time = libxl_vm_get_start_time(&ctx, domuid); + if (s_time == -1) + return; + uptime = now - s_time; + if (short_mode) + printf(" %s up %s, %s (%d)\n", current_time_to_string(now), + uptime_to_string(uptime, 1), + libxl_domid_to_name(&ctx, domuid), + domuid); + else + printf("%-33s %4d %s\n", libxl_domid_to_name(&ctx, domuid), + domuid, uptime_to_string(uptime, 0)); +} + +static void print_uptime(int short_mode, uint32_t doms[], int nb_doms) +{ + struct libxl_vminfo *info; + time_t now; + int nb_vm, i; + + now = time(NULL); + + if (!short_mode) + printf("%-33s %4s %s\n", "Name", "ID", "Uptime"); + + if (nb_doms == 0) { + print_dom0_uptime(short_mode, now); + info = libxl_list_vm(&ctx, &nb_vm); + for (i = 0; i < nb_vm; i++) + print_domU_uptime(info[i].domid, short_mode, now); + } else { + for (i = 0; i < nb_doms; i++) { + if (doms[i] == 0) + print_dom0_uptime(short_mode, now); + else + print_domU_uptime(doms[i], short_mode, now); + } + } +} + +int main_uptime(int argc, char **argv) +{ + char *dom = NULL; + int short_mode = 0; + uint32_t domains[100]; + int nb_doms = 0; + int opt; + + while ((opt = getopt(argc, argv, "hs")) != -1) { + switch (opt) { + case ''s'': + short_mode = 1; + break; + case ''h'': + help("uptime"); + exit(0); + default: + fprintf(stderr, "option `%c'' not supported.\n", opt); + break; + } + } + + for (;(dom = argv[optind]) != NULL; nb_doms++,optind++) { + find_domain(dom); + domains[nb_doms] = domid; + } + + print_uptime(short_mode, domains, nb_doms); + + exit(0); +} diff -r d77a88f938c6 tools/libxl/xl_cmdimpl.h --- a/tools/libxl/xl_cmdimpl.h Tue May 11 14:05:28 2010 +0100 +++ b/tools/libxl/xl_cmdimpl.h Fri May 14 02:27:59 2010 +0800 @@ -39,5 +39,6 @@ int main_domname(int argc, char **argv); int main_rename(int argc, char **argv); int main_trigger(int argc, char **argv); +int main_uptime(int argc, char **argv); void help(char *command); diff -r d77a88f938c6 tools/libxl/xl_cmdtable.c --- a/tools/libxl/xl_cmdtable.c Tue May 11 14:05:28 2010 +0100 +++ b/tools/libxl/xl_cmdtable.c Fri May 14 02:27:59 2010 +0800 @@ -172,6 +172,11 @@ "Send a trigger to a domain", "<Domain> <nmi|reset|init|power|sleep> [<VCPU>]", }, + { "uptime", + &main_uptime, + "Print uptime for all/some domains", + "[-s] [Domain]", + }, }; int cmdtable_len = sizeof(cmd_table)/sizeof(struct cmd_spec); -- Regards Yang Hongyang _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel
Yang Hongyang
2010-May-13 10:42 UTC
Re: [Xen-devel] [PATCH 1/2]xl: Add "xl uptime" command
On 05/13/2010 06:18 PM, Vincent Hanquez wrote:> On 13/05/10 11:10, Yang Hongyang wrote: >>> I find this command basically useless, since on restore the "start_time" >>> is not kept. do we actually need this ? >>> >> >> I think it''s sometimes useful. I noticed that just the day before >> yesterday, >> somebody asked in the xen-api list about "uptime" command. > > it is useful if it works :) > > however this version of start_time means "when was this xen domain > started" which is not the VM start time (i.e. "when was my vm booted"). > To be useful start_time need to preserved across migration and > suspend/restore. > > xen-api does the right thing and returns the later. >So more work has to be done to get xl exactly right as xen-api.:) And I noticed that if use xl create, start_time will not be set. xm create will set the start_time properly, I wonder if it is xend that set the start_time or is this a bug? -- Regards Yang Hongyang _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel