Skip to contentSkip to conversation

persistent real-time-clock

PATCH In review Dan Kenigsberg 7 participants 16 messages Apr 27, 2008 – May 6, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 persistent real-time-clock

Real PC lets its user set the real-time-clock and store it on CMOS, which advances the clock even when the PC is offline.

These patches will allow doing the same with VM:
- Before shutting a VM down, use the monitor's info timeoffset to see how much (in seconds) does the rtc differ from the host clock.
- Store this offset somewhere.
- Use it next time with -startdate now+offset.

Please tell me what do you think of it.

Dan.

Quoted 1 line

From 7b5505ef931fb900d365d23026698a6865fd3e83 Mon Sep 17 00:00:00 2001

From: Dan Kenigsberg <[email protected]>
Date: Sun, 27 Apr 2008 13:09:17 +0300
Subject: [PATCH] when specifying -localtime, -startdate is expressed as local time.

---
 vl.c |   30 +++++++++++++-----------------
 1 files changed, 13 insertions(+), 17 deletions(-)
vl.c +13 -17
index 78486cf..7f22152 100644
@@ -181,7 +181,7 @@ int nb_nics;
 NICInfo nd_table[MAX_NICS];
 int vm_running;
 static int rtc_utc = 1;
-static int rtc_date_offset = -1; /* -1 means no change */
+static int rtc_date_offset = 0; /* 0 means no change */
 int cirrus_vga_enabled = 1;
 int vmsvga_enabled = 0;
 #ifdef TARGET_SPARC
@@ -1574,15 +1574,11 @@ void qemu_get_timedate(struct tm *tm, int offset)
 
     time(&ti);
     ti += offset;
-    if (rtc_date_offset == -1) {
-        if (rtc_utc)
-            ret = gmtime(&ti);
-        else
-            ret = localtime(&ti);
-    } else {
-        ti -= rtc_date_offset;
+    ti -= rtc_date_offset;
+    if (rtc_utc)
         ret = gmtime(&ti);
-    }
+    else
+        ret = localtime(&ti);
 
     memcpy(tm, ret, sizeof(struct tm));
 }
@@ -1591,13 +1587,10 @@ int qemu_timedate_diff(struct tm *tm)
 {
     time_t seconds;
 
-    if (rtc_date_offset == -1)
-        if (rtc_utc)
-            seconds = mktimegm(tm);
-        else
-            seconds = mktime(tm);
-    else
+    if (rtc_utc)
         seconds = mktimegm(tm) + rtc_date_offset;
+    else
+        seconds = mktime(tm) + rtc_date_offset;
 
     return seconds - time(NULL);
 }
@@ -8785,7 +8778,7 @@ int main(int argc, char **argv)
                     struct tm tm;
                     time_t rtc_start_date;
                     if (!strcmp(optarg, "now")) {
-                        rtc_date_offset = -1;
+                        rtc_date_offset = 0;
                     } else {
                         if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
                                &tm.tm_year,
@@ -8807,7 +8800,10 @@ int main(int argc, char **argv)
                         }
                         tm.tm_year -= 1900;
                         tm.tm_mon--;
-                        rtc_start_date = mktimegm(&tm);
+                        if (rtc_utc)
+                            rtc_start_date = mktimegm(&tm);
+                        else
+                            rtc_start_date = mktime(&tm);
                         if (rtc_start_date == -1) {
                         date_fail:
                             fprintf(stderr, "Invalid date format. Valid format are:\n"
Signature · 1 line
1.5.4.5
Quoted 1 line

From 6fb0e482a932ef77683a54cbf19681b22fe31d34 Mon Sep 17 00:00:00 2001

From: Dan Kenigsberg <[email protected]>
Date: Sun, 27 Apr 2008 13:49:47 +0300
Subject: [PATCH] specify startdate offset from the command line

---
 vl.c |   16 ++++++++++++++--
 1 files changed, 14 insertions(+), 2 deletions(-)
vl.c +14 -2
index 7f22152..34d8e68 100644
@@ -8777,7 +8777,19 @@ int main(int argc, char **argv)
                 {
                     struct tm tm;
                     time_t rtc_start_date;
-                    if (!strcmp(optarg, "now")) {
+                    char sign;
+                    if (sscanf(optarg, "now%c%lu", 
+                                        &sign, &rtc_date_offset) == 2) {
+                        switch (sign) {
+                            case '+':
+                                rtc_date_offset = -rtc_date_offset;
+                                break;
+                            case '-':
+                                break;
+                            default:
+                                goto date_fail;
+                        }
+                    } else if (!strcmp(optarg, "now")) {
                         rtc_date_offset = 0;
                     } else {
                         if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
@@ -8807,7 +8819,7 @@ int main(int argc, char **argv)
                         if (rtc_start_date == -1) {
                         date_fail:
                             fprintf(stderr, "Invalid date format. Valid format are:\n"
-                                    "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
+                                    "'now' or 'now{+-}offset' or '2006-06-17T16:01:21' or '2006-06-17'\n");
                             exit(1);
                         }
                         rtc_date_offset = time(NULL) - rtc_start_date;
Signature · 1 line
1.5.4.5
Quoted 1 line

From d8f029819dcf2bf266e93661cf3c2f1503504835 Mon Sep 17 00:00:00 2001

From: Dan Kenigsberg <[email protected]>
Date: Sun, 27 Apr 2008 18:10:00 +0300
Subject: [PATCH] read current rtc offset (guest vs. host) from the monitor.
this allows management software to notice rtc changes from the guest.

---
 console.h        |    5 +++++
 hw/mc146818rtc.c |    5 +++++
 hw/pc.c          |    6 ++++++
 monitor.c        |    4 ++++
 vl.c             |   12 ++++++++++++
 5 files changed, 32 insertions(+), 0 deletions(-)
console.h +5 -0
index c7f29f5..44d0dad 100644
@@ -149,6 +149,11 @@ int vnc_display_open(DisplayState *ds, const char *display);
 int vnc_display_password(DisplayState *ds, const char *password);
 void do_info_vnc(void);
 
+#if defined(TARGET_I386)
+/* pc.c */
+void do_info_timeoffset(void);
+#endif
+
 /* curses.c */
 void curses_display_init(DisplayState *ds, int full_screen);
 
hw/mc146818rtc.c +5 -0
index 30bb044..3294363 100644
@@ -487,6 +487,11 @@ RTCState *rtc_init(int base, qemu_irq irq)
     return s;
 }
 
+int rtc_get_timeoffset(RTCState *s)
+{
+    return qemu_timedate_offset(&s->current_tm);
+}
+
 /* Memory mapped interface */
 static uint32_t cmos_mm_readb (void *opaque, target_phys_addr_t addr)
 {
hw/pc.c +6 -0
index 44a021b..ff440c7 100644
@@ -50,6 +50,12 @@ static PITState *pit;
 static IOAPICState *ioapic;
 static PCIDevice *i440fx_state;
 
+void do_info_timeoffset(void)
+{
+    int rtc_get_timeoffset(RTCState *s);
+    term_printf("now%+d\n", rtc_get_timeoffset(rtc_state));
+}
+
 static void ioport80_write(void *opaque, uint32_t addr, uint32_t data)
 {
 }
monitor.c +4 -0
index 574973d..76817e1 100644
@@ -1406,6 +1406,10 @@ static term_cmd_t info_cmds[] = {
       "", "show which guest mouse is receiving events" },
     { "vnc", "", do_info_vnc,
       "", "show the vnc server status"},
+#if defined(TARGET_I386)
+    { "timeoffset", "", do_info_timeoffset,
+      "", "show how much the VM real time clock differ from host time" },
+#endif
     { "name", "", do_info_name,
       "", "show the current VM name" },
 #if defined(TARGET_PPC)
vl.c +12 -0
index 34d8e68..c3c575c 100644
@@ -1595,6 +1595,18 @@ int qemu_timedate_diff(struct tm *tm)
     return seconds - time(NULL);
 }
 
+int qemu_timedate_offset(struct tm *tm)
+{
+    time_t seconds;
+
+    if (rtc_utc)
+        seconds = mktimegm(tm);
+    else
+        seconds = mktime(tm);
+
+    return seconds - time(NULL);
+}
+
 /***********************************************************/
 /* character device */
 
Signature · 1 line
1.5.4.5
#1 lore raw
Quoted 11 lines · Dan Kenigsberg

Real PC lets its user set the real-time-clock and store it on CMOS, which advances the clock even when the PC is offline.

These patches will allow doing the same with VM:
- Before shutting a VM down, use the monitor's info timeoffset to see how much (in seconds) does the rtc differ from the host clock.
- Store this offset somewhere.
- Use it next time with -startdate now+offset.

Please tell me what do you think of it.

How about (additionally) modifying -startdate to accept an offset directly?

qemu -startdate +0300 ...

Useful for those Windows VMs which have localtime in cmos.

Signature · 1 line
error compiling committee.c: too many arguments to function
#2 lore raw
Quoted 10 lines · Avi Kivity
Quoted 9 lines · Dan Kenigsberg

Real PC lets its user set the real-time-clock and store it on CMOS, which advances the clock even when the PC is offline.

These patches will allow doing the same with VM:
- Before shutting a VM down, use the monitor's info timeoffset to see how much (in seconds) does the rtc differ from the host clock.
- Store this offset somewhere.
- Use it next time with -startdate now+offset.

Quoted 8 lines
Quoted 2 lines

Please tell me what do you think of it.

How about (additionally) modifying -startdate to accept an offset directly?

qemu -startdate +0300 ...

Useful for those Windows VMs which have localtime in cmos.

I'm missing somthing: in what way is it different than the suggested

qemu -startdate now+offset ?

Only in allowing to specify the offset in hours/minutes?

#3 lore raw
Quoted 20 lines · Dan Kenigsberg
Quoted 18 lines · Avi Kivity
Quoted 12 lines · Dan Kenigsberg

Real PC lets its user set the real-time-clock and store it on CMOS, which advances the clock even when the PC is offline.

These patches will allow doing the same with VM:
- Before shutting a VM down, use the monitor's info timeoffset to see how much (in seconds) does the rtc differ from the host clock.
- Store this offset somewhere.
- Use it next time with -startdate now+offset.

Please tell me what do you think of it.

How about (additionally) modifying -startdate to accept an offset directly?

qemu -startdate +0300 ...

Quoted 10 lines
Quoted 2 lines

Useful for those Windows VMs which have localtime in cmos.

I'm missing somthing: in what way is it different than the suggested

qemu -startdate now+offset ?

Only in allowing to specify the offset in hours/minutes?

The difference is that I didn't read your patch, only the description.

I assumed you meant the user calculates now + offset and enters the result as the argument to -startdate, but you're not doing that. Sorry for the noise.

Signature · 1 line
Any sufficiently difficult bug is indistinguishable from a feature.
#4 lore raw
In reply to Avi Kivity
Quoted 6 lines

Dan Kenigsberg wrote:
How about (additionally) modifying -startdate to accept an offset directly?

qemu -startdate +0300 ...

Useful for those Windows VMs which have localtime in cmos.

For that users option -localtime was invented :)

PS But idea is good

Sergey Bychkow
ICQ: 21014758
FTN: 2:450/118.55

#5 lore raw
In reply to Dan Kenigsberg
Quoted 11 lines · Dan Kenigsberg

Real PC lets its user set the real-time-clock and store it on CMOS, which advances the clock even when the PC is offline.

These patches will allow doing the same with VM:
- Before shutting a VM down, use the monitor's info timeoffset to see how much (in seconds) does the rtc differ from the host clock.
- Store this offset somewhere.
- Use it next time with -startdate now+offset.

Please tell me what do you think of it.

I think a more general mechanism that stored nvram in a file would be interesting. Something like like -nvram file.ram. This also allows a guest to determine it's initial boot device and for that setting to persist.

Regards,

Anthony Liguori

Quoted 2 lines

Dan.

#6 lore raw
Quoted 10 lines · Anthony Liguori
Quoted 9 lines · Dan Kenigsberg

Real PC lets its user set the real-time-clock and store it on CMOS, which advances the clock even when the PC is offline.

These patches will allow doing the same with VM:
- Before shutting a VM down, use the monitor's info timeoffset to see how much (in seconds) does the rtc differ from the host clock.
- Store this offset somewhere.
- Use it next time with -startdate now+offset.

Quoted 8 lines
Quoted 2 lines

Please tell me what do you think of it.

I think a more general mechanism that stored nvram in a file would be interesting. Something like like -nvram file.ram. This also allows a guest to determine it's initial boot device and for that setting to persist.

Sounds not dissimilar to migration-to-file of all cmos bits and pieces of all supported architectures, doesn't it? I hope this general mechanism does not block, in your opnion, the suggested specific solution.

Dan.

#7 lore raw
In reply to Anthony Liguori
Quoted 10 lines · Anthony Liguori
Quoted 9 lines · Dan Kenigsberg

Real PC lets its user set the real-time-clock and store it on CMOS, which advances the clock even when the PC is offline.

These patches will allow doing the same with VM:
- Before shutting a VM down, use the monitor's info timeoffset to see how much (in seconds) does the rtc differ from the host clock.
- Store this offset somewhere.
- Use it next time with -startdate now+offset.

Quoted 7 lines
Quoted 2 lines

Please tell me what do you think of it.

I think a more general mechanism that stored nvram in a file would be interesting. Something like like -nvram file.ram. This also allows a guest to determine it's initial boot device and for that setting to persist.

I'm not sure I understood what you suggest here.

Plainly storing the rtc state on file is not enough, as unlike with real hardware, nothing will advance it when the power is off.

Do you suggest to set up a mechanism (in parallel to savevm/loadvm) where each device with non-volatile memory could register to? Probably, in most devices the SaveStateHandler could play the role of SaveNVStateHandler, but some would have to do something else. This would make the file.ram different than a memory dump.

Regards,

Dan

#8 lore raw
Quoted 16 lines · Dan Kenigsberg
Quoted 14 lines · Anthony Liguori
Quoted 12 lines · Dan Kenigsberg

Real PC lets its user set the real-time-clock and store it on CMOS, which advances the clock even when the PC is offline.

These patches will allow doing the same with VM:
- Before shutting a VM down, use the monitor's info timeoffset to see how much (in seconds) does the rtc differ from the host clock.
- Store this offset somewhere.
- Use it next time with -startdate now+offset.

Please tell me what do you think of it.

Quoted 8 lines
Quoted 2 lines

I think a more general mechanism that stored nvram in a file would be interesting. Something like like -nvram file.ram. This also allows a guest to determine it's initial boot device and for that setting to persist.

I'm not sure I understood what you suggest here.

Plainly storing the rtc state on file is not enough, as unlike with real hardware, nothing will advance it when the power is off.

If you made the CMOS non-volatile, what you would store in the CMOS is the clock-offset, not the actual clock time. Then when the VM started up again, it would Just Work.

You would probably have to use a different location in CMOS to store the offset than what the guest relies on to read the current time.

Regards,

Anthony Liguori

Quoted 11 lines

Do you suggest to set up a mechanism (in parallel to savevm/loadvm) where each device with non-volatile memory could register to? Probably, in most devices the SaveStateHandler could play the role of SaveNVStateHandler, but some would have to do something else. This would make the file.ram different than a memory dump.

Regards,

Dan

#9 lore raw
Quoted 10 lines · Anthony Liguori
Quoted 6 lines

I'm not sure I understood what you suggest here.

Plainly storing the rtc state on file is not enough, as unlike with real hardware, nothing will advance it when the power is off.

If you made the CMOS non-volatile, what you would store in the CMOS is the clock-offset, not the actual clock time. Then when the VM started up again, it would Just Work.

You would probably have to use a different location in CMOS to store the offset than what the guest relies on to read the current time.

Under this, the CMOS would not be read by the guest at any time. So why store the CMOS at all? Store the offset somewhere and avoid the CMOS (gaining the ability to work on targets without nonvolatile memory).

It's config file territory, not CMOS.

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
#10 lore raw
Quoted 8 lines · Avi Kivity
Quoted 7 lines · Anthony Liguori
Quoted 6 lines

I'm not sure I understood what you suggest here.

Plainly storing the rtc state on file is not enough, as unlike with real hardware, nothing will advance it when the power is off.

Quoted 7 lines
Quoted 3 lines

If you made the CMOS non-volatile, what you would store in the CMOS is the clock-offset, not the actual clock time. Then when the VM started up again, it would Just Work.

You would probably have to use a different location in CMOS to store the offset than what the guest relies on to read the current time.

Under this, the CMOS would not be read by the guest at any time. So why store the CMOS at all? Store the offset somewhere and avoid the CMOS (gaining the ability to work on targets without nonvolatile memory).

It's config file territory, not CMOS.

It's a bios parameter (just like default boot device). It makes sense to allow the bios to let the user customize bios parameters and have that be saved.

CMOS is non-volatile in real life so making it non-volatile in QEMU seems like the obvious thing to do.

Regards,

Anthony Liguori

#11 lore raw
Quoted 3 lines

It's a bios parameter (just like default boot device). It makes sense to allow the bios to let the user customize bios parameters and have that be saved.

CMOS is non-volatile in real life so making it non-volatile in QEMU seems like the obvious thing to do.

I fully agree. Platforms have many non-volatile storage (CMOS, ethernet eeproms, flashes...). It would be nice if qemu were able to making them non-volatile.

I'd plan to work on this, although not soon.

Tristan.

#12 lore raw
Quoted 4 lines · Tristan Gingold
Quoted 4 lines

It's a bios parameter (just like default boot device). It makes sense to allow the bios to let the user customize bios parameters and have that be saved.

Quoted 8 lines
Quoted 2 lines

CMOS is non-volatile in real life so making it non-volatile in QEMU seems like the obvious thing to do.

I fully agree. Platforms have many non-volatile storage (CMOS, ethernet eeproms, flashes...). It would
be nice if qemu were able to making them non-volatile.

I'd plan to work on this, although not soon.

Hervé Poussineau had already done this kind of work for DS1225Y chip.

The content of the NVRAM is saved into the file "nvram".

I think we should have a more generic way to manage this. I mean:
- a parameter to specify the file
- a default filename per machine (something like "~/.qemu/pc.cmos", "~/.qemu/sun4m.nvram" or "~/.qemu/ppc_oldworld.nvram")

(and if we create a "~/.qemu" directory, perhaps we could store some default configurations ... :-P )

There is also the -prom-env parameter to specify a value of the nvram (for sparc only, but could be generic).

Laurent

Signature · 3 lines
------------- [email protected] ---------------
"The best way to predict the future is to invent it."
- Alan Kay
#13 lore raw
In reply to Dan Kenigsberg
Quoted 5 lines · Dan Kenigsberg

These patches will allow doing the same with VM:
- Before shutting a VM down, use the monitor's info timeoffset to see how much (in seconds) does the rtc differ from the host clock.
- Store this offset somewhere.
- Use it next time with -startdate now+offset.

Another useful one would be store the actual time, and use the same time when starting next time - without including any advance in host's clock in the interval.

That's useful when running some time-limited software in a VM. Then you can only run the VM when you're using the software, and get more use out of it before the time runs out.

-- Jamie

#14 lore raw
Quoted 7 lines · Jamie Lokier
Quoted 5 lines · Dan Kenigsberg

These patches will allow doing the same with VM:
- Before shutting a VM down, use the monitor's info timeoffset to see how much (in seconds) does the rtc differ from the host clock.
- Store this offset somewhere.
- Use it next time with -startdate now+offset.

Quoted 8 lines

Another useful one would be store the actual time, and use the same time when starting next time - without including any advance in host's clock in the interval.

That's useful when running some time-limited software in a VM. Then you can only run the VM when you're using the software, and get more use out of it before the time runs out.

It does not seem hard to add something like that (though it seems quite orthogonal to my original perpose). Did you mean something like the following?

monitor.c +10 -0
index 76817e1..45396aa 100644
@@ -264,6 +264,14 @@ static void do_info_blockstats(void)
     bdrv_info_stats();
 }
 
+void do_info_timedate(void)
+{
+    struct tm tm;
+    qemu_get_timedate(&tm, 0);
+    term_printf("%d-%02d-%02dT%02d:%02d:%02d\n", tm.tm_year + 1900, tm.tm_mon,
+            tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
+}
+
 /* get the current CPU defined by the user */
 static int mon_set_cpu(int cpu_index)
 {
@@ -1410,6 +1418,8 @@ static term_cmd_t info_cmds[] = {
     { "timeoffset", "", do_info_timeoffset,
       "", "show how much the VM real time clock differ from host time" },
 #endif
+    { "timedate", "", do_info_timedate,
+      "", "show the current VM time and date" },
     { "name", "", do_info_name,
       "", "show the current VM name" },
 #if defined(TARGET_PPC)
#15 lore raw
In reply to Jamie Lokier
Quoted 7 lines

Another useful one would be store the actual time, and use the same time when starting next time - without including any advance in host's clock in the interval.

That's useful when running some time-limited software in a VM. Then you can only run the VM when you're using the software, and get more use out of it before the time runs out.

Run it with fixed -startdate and/or in snapshot mode