qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Anthony Liguori <anthony@codemonkey.ws>
To: Blue Swirl <blauwirbel@gmail.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Anthony Liguori <aliguori@us.ibm.com>,
	qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH v4 7/7] qtest: add rtc-test test-case
Date: Fri, 30 Mar 2012 12:49:01 -0500	[thread overview]
Message-ID: <4F75F20D.8010304@codemonkey.ws> (raw)
In-Reply-To: <CAAu8pHun31kLCKwROo7wHA8y-J5y2ayfhKH33CvY+aGVG8JC+w@mail.gmail.com>

On 03/30/2012 12:22 PM, Blue Swirl wrote:
> On Fri, Mar 30, 2012 at 17:15, Blue Swirl<blauwirbel@gmail.com>  wrote:
>> On Wed, Mar 28, 2012 at 13:42, Paolo Bonzini<pbonzini@redhat.com>  wrote:
>>> From: Anthony Liguori<aliguori@us.ibm.com>
>>>
>>> Signed-off-by: Anthony Liguori<aliguori@us.ibm.com>
>>> Signed-off-by: Paolo Bonzini<pbonzini@redhat.com>
>>> ---
>>>   tests/Makefile   |    5 +
>>>   tests/rtc-test.c |  263 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
>>>   2 files changed, 268 insertions(+), 0 deletions(-)
>>>   create mode 100644 tests/rtc-test.c
>>>
>>> diff --git a/tests/Makefile b/tests/Makefile
>>> index 99ca308..42ce2d7 100644
>>> --- a/tests/Makefile
>>> +++ b/tests/Makefile
>>> @@ -16,6 +16,11 @@ check-unit-y += tests/test-coroutine$(EXESUF)
>>>
>>>   check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
>>>
>>> +# All QTests for now are POSIX-only, but the dependencies are
>>> +# really in libqtest, not in the testcases themselves.
>>> +check-qtest-i386-y = tests/rtc-test
>>> +check-qtest-x86_64-y = $(check-qtest-i386-y)
>>
>> I get this error when trying 'make check':
>> GTESTER tests/test-string-output-visitor
>> GTESTER tests/test-coroutine
>> ccache gcc -O2 -g   -Wl,--warn-common -m64 -g
>> /src/qemu/tests/rtc-test.c tests/libqtest.o osdep.o oslib-posix.o
>> qemu-thread-posix.o   -o tests/rtc-test
>> /src/qemu/tests/rtc-test.c:14:33: error: hw/mc146818rtc_regs.h: No
>> such file or directory
>> /src/qemu/tests/rtc-test.c:16:18: error: glib.h: No such file or directory
>> /src/qemu/tests/rtc-test.c:46: warning: 'struct tm' declared inside
>> parameter list
>> /src/qemu/tests/rtc-test.c:46: warning: its scope is only this
>> definition or declaration, which is probably not what you want
>>
>> It looks like the build rules are not correct. This is also an out of
>> tree build. Perhaps the patsubst rules in tests/Makefile do not match
>> rtc-test because it doesn't start with "qtest-"?
>
> This fixes 'make check'. Trace objects are also needed if tracing is enabled.
>
> diff --git a/tests/Makefile b/tests/Makefile
> index 29c1e51..a98a848 100644
> --- a/tests/Makefile
> +++ b/tests/Makefile
> @@ -63,6 +63,8 @@ tests/test-qmp-input-visitor$(EXESUF):
> tests/test-qmp-input-visitor.o $(test-qap
>   tests/test-qmp-input-strict$(EXESUF): tests/test-qmp-input-strict.o
> $(test-qapi-obj-y)
>   tests/test-qmp-commands$(EXESUF): tests/test-qmp-commands.o
> tests/test-qmp-marshal.o $(test-qapi-obj-y)
>
> +tests/rtc-test$(EXESUF): tests/rtc-test.o $(trace-obj-y)
> +
>   # QTest rules
>
>   TARGETS=$(patsubst %-softmmu,%, $(filter %-softmmu,$(TARGET_DIRS)))

Reviewed-by: Anthony Liguori <aliguori@us.ibm.com>

Could you please add a SoB and apply Blue?  Thanks!

Regards,

Anthony Liguori

>
>>> +
>>>   GENERATED_HEADERS += tests/test-qapi-types.h tests/test-qapi-visit.h tests/test-qmp-commands.h
>>>
>>>   test-obj-y = tests/check-qint.o tests/check-qstring.o tests/check-qdict.o \
>>> diff --git a/tests/rtc-test.c b/tests/rtc-test.c
>>> new file mode 100644
>>> index 0000000..22f807c
>>> --- /dev/null
>>> +++ b/tests/rtc-test.c
>>> @@ -0,0 +1,263 @@
>>> +/*
>>> + * QTest testcase for the MC146818 real-time clock
>>> + *
>>> + * Copyright IBM, Corp. 2012
>>> + *
>>> + * Authors:
>>> + *  Anthony Liguori<aliguori@us.ibm.com>
>>> + *
>>> + * This work is licensed under the terms of the GNU GPL, version 2 or later.
>>> + * See the COPYING file in the top-level directory.
>>> + *
>>> + */
>>> +#include "libqtest.h"
>>> +#include "hw/mc146818rtc_regs.h"
>>> +
>>> +#include<glib.h>
>>> +#include<stdio.h>
>>> +#include<string.h>
>>> +#include<stdlib.h>
>>> +#include<unistd.h>
>>> +
>>> +static uint8_t base = 0x70;
>>> +
>>> +static int bcd2dec(int value)
>>> +{
>>> +    return (((value>>  4)&  0x0F) * 10) + (value&  0x0F);
>>> +}
>>> +
>>> +static int dec2bcd(int value)
>>> +{
>>> +    return ((value / 10)<<  4) | (value % 10);
>>> +}
>>> +
>>> +static uint8_t cmos_read(uint8_t reg)
>>> +{
>>> +    outb(base + 0, reg);
>>> +    return inb(base + 1);
>>> +}
>>> +
>>> +static void cmos_write(uint8_t reg, uint8_t val)
>>> +{
>>> +    outb(base + 0, reg);
>>> +    outb(base + 1, val);
>>> +}
>>> +
>>> +static int tm_cmp(struct tm *lhs, struct tm *rhs)
>>> +{
>>> +    time_t a, b;
>>> +    struct tm d1, d2;
>>> +
>>> +    memcpy(&d1, lhs, sizeof(d1));
>>> +    memcpy(&d2, rhs, sizeof(d2));
>>> +
>>> +    a = mktime(&d1);
>>> +    b = mktime(&d2);
>>> +
>>> +    if (a<  b) {
>>> +        return -1;
>>> +    } else if (a>  b) {
>>> +        return 1;
>>> +    }
>>> +
>>> +    return 0;
>>> +}
>>> +
>>> +#if 0
>>> +static void print_tm(struct tm *tm)
>>> +{
>>> +    printf("%04d-%02d-%02d %02d:%02d:%02d\n",
>>> +           tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
>>> +           tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_gmtoff);
>>> +}
>>> +#endif
>>> +
>>> +static void cmos_get_date_time(struct tm *date)
>>> +{
>>> +    int base_year = 2000, hour_offset;
>>> +    int sec, min, hour, mday, mon, year;
>>> +    time_t ts;
>>> +    struct tm dummy;
>>> +
>>> +    sec = cmos_read(RTC_SECONDS);
>>> +    min = cmos_read(RTC_MINUTES);
>>> +    hour = cmos_read(RTC_HOURS);
>>> +    mday = cmos_read(RTC_DAY_OF_MONTH);
>>> +    mon = cmos_read(RTC_MONTH);
>>> +    year = cmos_read(RTC_YEAR);
>>> +
>>> +    if ((cmos_read(RTC_REG_B)&  REG_B_DM) == 0) {
>>> +        sec = bcd2dec(sec);
>>> +        min = bcd2dec(min);
>>> +        hour = bcd2dec(hour);
>>> +        mday = bcd2dec(mday);
>>> +        mon = bcd2dec(mon);
>>> +        year = bcd2dec(year);
>>> +        hour_offset = 80;
>>> +    } else {
>>> +        hour_offset = 0x80;
>>> +    }
>>> +
>>> +    if ((cmos_read(0x0B)&  REG_B_24H) == 0) {
>>> +        if (hour>= hour_offset) {
>>> +            hour -= hour_offset;
>>> +            hour += 12;
>>> +        }
>>> +    }
>>> +
>>> +    ts = time(NULL);
>>> +    localtime_r(&ts,&dummy);
>>> +
>>> +    date->tm_isdst = dummy.tm_isdst;
>>> +    date->tm_sec = sec;
>>> +    date->tm_min = min;
>>> +    date->tm_hour = hour;
>>> +    date->tm_mday = mday;
>>> +    date->tm_mon = mon - 1;
>>> +    date->tm_year = base_year + year - 1900;
>>> +    date->tm_gmtoff = 0;
>>> +
>>> +    ts = mktime(date);
>>> +}
>>> +
>>> +static void check_time(int wiggle)
>>> +{
>>> +    struct tm start, date[4], end;
>>> +    struct tm *datep;
>>> +    time_t ts;
>>> +
>>> +    /*
>>> +     * This check assumes a few things.  First, we cannot guarantee that we get
>>> +     * a consistent reading from the wall clock because we may hit an edge of
>>> +     * the clock while reading.  To work around this, we read four clock readings
>>> +     * such that at least two of them should match.  We need to assume that one
>>> +     * reading is corrupt so we need four readings to ensure that we have at
>>> +     * least two consecutive identical readings
>>> +     *
>>> +     * It's also possible that we'll cross an edge reading the host clock so
>>> +     * simply check to make sure that the clock reading is within the period of
>>> +     * when we expect it to be.
>>> +     */
>>> +
>>> +    ts = time(NULL);
>>> +    gmtime_r(&ts,&start);
>>> +
>>> +    cmos_get_date_time(&date[0]);
>>> +    cmos_get_date_time(&date[1]);
>>> +    cmos_get_date_time(&date[2]);
>>> +    cmos_get_date_time(&date[3]);
>>> +
>>> +    ts = time(NULL);
>>> +    gmtime_r(&ts,&end);
>>> +
>>> +    if (tm_cmp(&date[0],&date[1]) == 0) {
>>> +        datep =&date[0];
>>> +    } else if (tm_cmp(&date[1],&date[2]) == 0) {
>>> +        datep =&date[1];
>>> +    } else if (tm_cmp(&date[2],&date[3]) == 0) {
>>> +        datep =&date[2];
>>> +    } else {
>>> +        g_assert_not_reached();
>>> +    }
>>> +
>>> +    if (!(tm_cmp(&start, datep)<= 0&&  tm_cmp(datep,&end)<= 0)) {
>>> +        time_t t, s;
>>> +
>>> +        start.tm_isdst = datep->tm_isdst;
>>> +
>>> +        t = mktime(datep);
>>> +        s = mktime(&start);
>>> +        if (t<  s) {
>>> +            g_test_message("RTC is %ld second(s) behind wall-clock\n", (s - t));
>>> +        } else {
>>> +            g_test_message("RTC is %ld second(s) ahead of wall-clock\n", (t - s));
>>> +        }
>>> +
>>> +        g_assert_cmpint(ABS(t - s),<=, wiggle);
>>> +    }
>>> +}
>>> +
>>> +static int wiggle = 2;
>>> +
>>> +static void bcd_check_time(void)
>>> +{
>>> +    /* Set BCD mode */
>>> +    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B)&  ~REG_B_DM);
>>> +    check_time(wiggle);
>>> +}
>>> +
>>> +static void dec_check_time(void)
>>> +{
>>> +    /* Set DEC mode */
>>> +    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_DM);
>>> +    check_time(wiggle);
>>> +}
>>> +
>>> +static void set_alarm_time(struct tm *tm)
>>> +{
>>> +    int sec;
>>> +
>>> +    sec = tm->tm_sec;
>>> +
>>> +    if ((cmos_read(RTC_REG_B)&  REG_B_DM) == 0) {
>>> +        sec = dec2bcd(sec);
>>> +    }
>>> +
>>> +    cmos_write(RTC_SECONDS_ALARM, sec);
>>> +    cmos_write(RTC_MINUTES_ALARM, RTC_ALARM_DONT_CARE);
>>> +    cmos_write(RTC_HOURS_ALARM, RTC_ALARM_DONT_CARE);
>>> +}
>>> +
>>> +static void alarm_time(void)
>>> +{
>>> +    struct tm now;
>>> +    time_t ts;
>>> +    int i;
>>> +
>>> +    ts = time(NULL);
>>> +    gmtime_r(&ts,&now);
>>> +
>>> +    /* set DEC mode */
>>> +    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_DM);
>>> +
>>> +    g_assert(!get_irq(RTC_ISA_IRQ));
>>> +    cmos_read(RTC_REG_C);
>>> +
>>> +    now.tm_sec = (now.tm_sec + 2) % 60;
>>> +    set_alarm_time(&now);
>>> +    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_AIE);
>>> +
>>> +    for (i = 0; i<  2 + wiggle; i++) {
>>> +        if (get_irq(RTC_ISA_IRQ)) {
>>> +            break;
>>> +        }
>>> +
>>> +        clock_step(1000000000);
>>> +    }
>>> +
>>> +    g_assert(get_irq(RTC_ISA_IRQ));
>>> +    g_assert((cmos_read(RTC_REG_C)&  REG_C_AF) != 0);
>>> +    g_assert(cmos_read(RTC_REG_C) == 0);
>>> +}
>>> +
>>> +int main(int argc, char **argv)
>>> +{
>>> +    QTestState *s = NULL;
>>> +    int ret;
>>> +
>>> +    g_test_init(&argc,&argv, NULL);
>>> +
>>> +    s = qtest_start("-display none -rtc clock=vm");
>>> +    qtest_irq_intercept_in(s, "ioapic");
>>> +
>>> +    qtest_add_func("/rtc/bcd/check-time", bcd_check_time);
>>> +    qtest_add_func("/rtc/dec/check-time", dec_check_time);
>>> +    qtest_add_func("/rtc/alarm-time", alarm_time);
>>> +    ret = g_test_run();
>>> +
>>> +    if (s) {
>>> +        qtest_quit(s);
>>> +    }
>>> +
>>> +    return ret;
>>> +}
>>> --
>>> 1.7.9.1
>>>
>>>
>

  reply	other threads:[~2012-03-30 17:49 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-28 13:42 [Qemu-devel] [PATCH v4 0/7] new test infrastructure + qtest Paolo Bonzini
2012-03-28 13:42 ` [Qemu-devel] [PATCH v4 1/7] test makefile overhaul Paolo Bonzini
2012-03-28 13:42 ` [Qemu-devel] [PATCH v4 2/7] qtest: add test framework Paolo Bonzini
2012-03-30 17:40   ` Stefan Weil
2012-03-30 17:50     ` Anthony Liguori
2012-03-30 17:52       ` Stefan Weil
2012-03-30 17:53         ` Anthony Liguori
2012-03-30 18:37           ` Paolo Bonzini
2012-03-28 13:42 ` [Qemu-devel] [PATCH v4 3/7] qtest: IRQ interception infrastructure Paolo Bonzini
2012-03-28 13:42 ` [Qemu-devel] [PATCH v4 4/7] qtest: add clock management Paolo Bonzini
2012-03-28 13:42 ` [Qemu-devel] [PATCH v4 5/7] qtest: add C version of test infrastructure Paolo Bonzini
2012-03-28 13:42 ` [Qemu-devel] [PATCH v4 6/7] rtc: split out macros into a header file and use in test case Paolo Bonzini
2012-03-28 13:42 ` [Qemu-devel] [PATCH v4 7/7] qtest: add rtc-test test-case Paolo Bonzini
2012-03-30 17:15   ` Blue Swirl
2012-03-30 17:22     ` Blue Swirl
2012-03-30 17:49       ` Anthony Liguori [this message]
2012-03-30 17:53         ` Blue Swirl
2012-03-30 17:29     ` Anthony Liguori
2012-03-30 17:37       ` Blue Swirl
2012-03-28 17:43 ` [Qemu-devel] [PATCH v4 0/7] new test infrastructure + qtest Anthony Liguori
2012-03-30 14:45 ` Anthony Liguori

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4F75F20D.8010304@codemonkey.ws \
    --to=anthony@codemonkey.ws \
    --cc=aliguori@us.ibm.com \
    --cc=blauwirbel@gmail.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).