From patchwork Wed Oct 17 12:23:01 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Artem Pisarenko X-Patchwork-Id: 10645399 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1CF5713B0 for ; Wed, 17 Oct 2018 12:27:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 139B72AFE5 for ; Wed, 17 Oct 2018 12:27:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 07D372AFF2; Wed, 17 Oct 2018 12:27:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.7 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 651C82AF9A for ; Wed, 17 Oct 2018 12:27:44 +0000 (UTC) Received: from localhost ([::1]:36345 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gCkvP-0004ho-Kk for patchwork-qemu-devel@patchwork.kernel.org; Wed, 17 Oct 2018 08:27:43 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41322) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gCkse-0002aC-8e for qemu-devel@nongnu.org; Wed, 17 Oct 2018 08:24:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gCksd-00077A-8m for qemu-devel@nongnu.org; Wed, 17 Oct 2018 08:24:52 -0400 Received: from mail-lj1-x235.google.com ([2a00:1450:4864:20::235]:42576) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gCksd-00076I-11 for qemu-devel@nongnu.org; Wed, 17 Oct 2018 08:24:51 -0400 Received: by mail-lj1-x235.google.com with SMTP id y71-v6so24116293lje.9 for ; Wed, 17 Oct 2018 05:24:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:in-reply-to:references:content-transfer-encoding; bh=ddJ5zRtYlVtYtiBfL/OHyIASkhKnfJAXo1rrTA7IZ5c=; b=hJZO+k76AikPflD7FzQd4wqQfOV4jeLvO3mtzK6eF7l4czd+jzznA9vQM2xZFmywKR fqj3Gm50Ut1EUOWP2+HFFObwTTpSvjdmcQUGguiRQhwM9YhMNVRDYkLSMzW1OEG3ZgxF 8aNt8Jd8lnu4sRz4L+GDXpytX84Ag2At7u/ihMKayhCyqn3gzm64XLyw7PL+X2XQqJdE Tl/bTc0ULXYgeLjrRx+wshukEGMAKLKt8tkTc6f5rNM7pZt8oupxTVyCyNCN2Lw4dgqt iwGRGP1zU4tkPGY5Fi7LTjSYWkgTF4yCRnjhtJWTWEodrUPrXvY+ssvwJYO163oF1XGg sBZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:in-reply-to:references :content-transfer-encoding; bh=ddJ5zRtYlVtYtiBfL/OHyIASkhKnfJAXo1rrTA7IZ5c=; b=bXpZ6HlNNSgoqpYr+0g/EMcvHrSOxn2m989j1CtRedBPn910DF/WDL36qNmfh154pC w6iX0mzys2ZpZGdV+GWVHhwRjsGQLk2bfiFTGUbHqRjB4uRVA5ka+o+W3jVplk7DcJfn jf4UHv8EMZCCRAOEEjxEbEbHr6RANFByHV6haBF3u3/xctfUheNdVHX+ekgJLUlwHT2E BHR0JoUxeiIDOzFdQ0aGrkhZFIwvVvXwoBQDyD3UMgFSYM9OPpEK/APYKTwgvx19OZG/ 20FVQ8LdSfrydxUQKDLzwZ8JOM/wzbtO4lhC35LFS6Giczo0i1boBWtH1YQFNmpJ0Mvp nhMg== X-Gm-Message-State: ABuFfog78//ve7ZKMl1Rf7o8nKDvusClmHnNPwtk8UlQNAlo+I6N4lN2 76bxzmkgBbfKcZU/ka2kFX/H5eRpFYU= X-Google-Smtp-Source: ACcGV61zL1EZspkH8s88yTaEpxiJhdHmPn4gxQ0QT+5kAD9vRZXB25E1jouxTg2Gv8GvZj4l8p5Nwg== X-Received: by 2002:a2e:3101:: with SMTP id x1-v6mr16745962ljx.83.1539779089366; Wed, 17 Oct 2018 05:24:49 -0700 (PDT) Received: from localhost.localdomain ([77.221.221.49]) by smtp.gmail.com with ESMTPSA id z67-v6sm3704118lfa.14.2018.10.17.05.24.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 17 Oct 2018 05:24:48 -0700 (PDT) From: Artem Pisarenko To: qemu-devel@nongnu.org Date: Wed, 17 Oct 2018 18:23:01 +0600 Message-Id: <670f9397a16efca2646f571e811d44c0cfc5599d.1539778764.git.artem.k.pisarenko@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: References: MIME-Version: 1.0 In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::235 Subject: [Qemu-devel] [PATCH v2 2/4] vl: refactor -rtc option references X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Artem Pisarenko Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Improve code readability and prepare for fixing bug #1797033 Signed-off-by: Artem Pisarenko --- Notes: v2: fixed compiler warning vl.c | 85 ++++++++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 53 insertions(+), 32 deletions(-) diff --git a/vl.c b/vl.c index 4e25c78..10c4275 100644 --- a/vl.c +++ b/vl.c @@ -147,8 +147,13 @@ bool enable_cpu_pm = false; int nb_nics; NICInfo nd_table[MAX_NICS]; int autostart; -static int rtc_utc = 1; -static int rtc_date_offset = -1; /* -1 means no change */ +static enum { + RTC_BASE_UTC, + RTC_BASE_LOCALTIME, + RTC_BASE_DATETIME, +} rtc_base_type = RTC_BASE_UTC; +static int rtc_host_datetime_offset = -1; /* valid only for host rtc_clock and + rtc_base_type=RTC_BASE_DATETIME */ QEMUClockType rtc_clock; int vga_interface_type = VGA_NONE; static DisplayOptions dpy; @@ -782,26 +787,30 @@ void qemu_system_vmstop_request(RunState state) /***********************************************************/ /* real time host monotonic timer */ -static time_t qemu_time(void) +static time_t qemu_timedate(void) { return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000; } /***********************************************************/ -/* host time/date access */ +/* RTC reference time/date access */ void qemu_get_timedate(struct tm *tm, int offset) { - time_t ti = qemu_time(); + time_t ti = qemu_timedate(); ti += offset; - if (rtc_date_offset == -1) { - if (rtc_utc) - gmtime_r(&ti, tm); - else - localtime_r(&ti, tm); - } else { - ti -= rtc_date_offset; + + switch (rtc_base_type) { + case RTC_BASE_UTC: gmtime_r(&ti, tm); + break; + case RTC_BASE_LOCALTIME: + localtime_r(&ti, tm); + break; + case RTC_BASE_DATETIME: + ti -= rtc_host_datetime_offset; + gmtime_r(&ti, tm); + break; } } @@ -809,23 +818,33 @@ int qemu_timedate_diff(struct tm *tm) { time_t seconds; - if (rtc_date_offset == -1) - if (rtc_utc) - seconds = mktimegm(tm); - else { - struct tm tmp = *tm; - tmp.tm_isdst = -1; /* use timezone to figure it out */ - seconds = mktime(&tmp); - } - else - seconds = mktimegm(tm) + rtc_date_offset; + switch (rtc_base_type) { + case RTC_BASE_UTC: + seconds = mktimegm(tm); + break; + case RTC_BASE_LOCALTIME: + { + struct tm tmp = *tm; + tmp.tm_isdst = -1; /* use timezone to figure it out */ + seconds = mktime(&tmp); + break; + } + case RTC_BASE_DATETIME: + seconds = mktimegm(tm) + rtc_host_datetime_offset; + break; + default: + /* gcc complains: ‘seconds’ may be used uninitialized */ + g_assert_not_reached(); + seconds = -1; + break; + } - return seconds - qemu_time(); + return seconds - qemu_timedate(); } -static void configure_rtc_date_offset(const char *startdate) +static void configure_rtc_host_datetime_offset(const char *startdate) { - time_t rtc_start_date; + time_t rtc_start_datetime; struct tm tm; if (sscanf(startdate, "%d-%d-%dT%d:%d:%d", &tm.tm_year, &tm.tm_mon, @@ -841,15 +860,16 @@ static void configure_rtc_date_offset(const char *startdate) } tm.tm_year -= 1900; tm.tm_mon--; - rtc_start_date = mktimegm(&tm); - if (rtc_start_date == -1) { + rtc_start_datetime = mktimegm(&tm); + if (rtc_start_datetime == -1) { date_fail: - error_report("invalid date format"); + error_report("invalid datetime format"); error_printf("valid formats: " "'2006-06-17T16:01:21' or '2006-06-17'\n"); exit(1); } - rtc_date_offset = qemu_time() - rtc_start_date; + rtc_host_datetime_offset = (qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000) + - rtc_start_datetime; } static void configure_rtc(QemuOpts *opts) @@ -859,15 +879,16 @@ static void configure_rtc(QemuOpts *opts) value = qemu_opt_get(opts, "base"); if (value) { if (!strcmp(value, "utc")) { - rtc_utc = 1; + rtc_base_type = RTC_BASE_UTC; } else if (!strcmp(value, "localtime")) { Error *blocker = NULL; - rtc_utc = 0; + rtc_base_type = RTC_BASE_LOCALTIME; error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "-rtc base=localtime"); replay_add_blocker(blocker); } else { - configure_rtc_date_offset(value); + rtc_base_type = RTC_BASE_DATETIME; + configure_rtc_host_datetime_offset(value); } } value = qemu_opt_get(opts, "clock");