From nobody Sun Feb 8 10:48:48 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) client-ip=207.211.31.81; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1577977013; cv=none; d=zohomail.com; s=zohoarc; b=UL+P0AisJfTPKfY45kPrDHxS+eZs6U4m+b59dVl6HUtg6BSl1Rgzspoh7V5MTdIXYZ3KfSRtjpJgbDwcbDPnTmBH3Zlc9oECkHLvZVGL+zY9bEKlSq/mbYTeFcfQbUax+GRw0JAmIRBD5xVZDRFZjybWHK9r5Ss0n/+R+4DNlNE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1577977013; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=h0xBMr9G0kxIg/HEEgsa93h3tRoOONlhR/oFOLzyzAc=; b=dQHiLEj0iFJp/wCF4k/sOmqmv7SUo9u0JDaWRC05CPaaUH7rjT68bMFdt3d7OEtZDHcFf3ubr2BOh/pXE1deZh27/sdJNnqaQr0sMu3oPDKD4JmJ2TT6Y5UalpG7XcpzwuchpCs+Chp3wh6QhVH/5fZMZWVO0+5a2MhlUBvBBlE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 1577977013930175.74552031200744; Thu, 2 Jan 2020 06:56:53 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-183-YUBHbhraO0CjiyM6tu1Rjg-1; Thu, 02 Jan 2020 09:56:50 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 376C818CA247; Thu, 2 Jan 2020 14:56:45 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0EFA07B9F8; Thu, 2 Jan 2020 14:56:45 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id C36C581E3A; Thu, 2 Jan 2020 14:56:44 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 002EubJr004817 for ; Thu, 2 Jan 2020 09:56:37 -0500 Received: by smtp.corp.redhat.com (Postfix) id 2F96760C87; Thu, 2 Jan 2020 14:56:37 +0000 (UTC) Received: from catbus.gsslab.fab.redhat.com (dhcp-43.gsslab.fab.redhat.com [10.33.9.43]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9383B60BF4; Thu, 2 Jan 2020 14:56:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1577977012; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=h0xBMr9G0kxIg/HEEgsa93h3tRoOONlhR/oFOLzyzAc=; b=i7GXiuKQhveZ211UBTGSvqBVmds02kki3y5Iu6AfAtDGMKQY3hTkzQK+gaPAVq+GJFv2zs 1tovCHuiqqWN7HesJdua1qsWrTMRnFqpI9J8sBL+AsMLH3gTQ0ATZ1Cq9xK9yXzdm8Q6Oj DrTEinp4aJY/IQp/HMXXKCus8R47ns0= From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Thu, 2 Jan 2020 14:53:46 +0000 Message-Id: <20200102145357.6724-13-berrange@redhat.com> In-Reply-To: <20200102145357.6724-1-berrange@redhat.com> References: <20200102145357.6724-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 12/23] src: replace clock_gettime()/gettimeofday() with g_get_real_time() X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-MC-Unique: YUBHbhraO0CjiyM6tu1Rjg-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) g_get_real_time() returns the time since epoch in microseconds. It uses gettimeofday() internally while libvirt used clock_gettime because it is declared async signal safe. In practice gettimeofday is also async signal safe *provided* the timezone parameter is NULL. This is indeed the case in g_get_real_time(). Signed-off-by: Daniel P. Berrang=C3=A9 --- src/conf/moment_conf.c | 7 ++---- src/libxl/libxl_driver.c | 21 +++++++++-------- src/qemu/qemu_backup.c | 5 ++-- src/test/test_driver.c | 51 ++++++++-------------------------------- src/util/virtime.c | 22 +---------------- tests/eventtest.c | 18 +++++--------- tools/virsh-domain.c | 10 ++++---- tools/vsh.c | 11 ++++----- tools/vsh.h | 1 - 9 files changed, 40 insertions(+), 106 deletions(-) diff --git a/src/conf/moment_conf.c b/src/conf/moment_conf.c index d02fcb89e2..fb6f7824cb 100644 --- a/src/conf/moment_conf.c +++ b/src/conf/moment_conf.c @@ -73,13 +73,10 @@ virDomainMomentDefDispose(void *obj) int virDomainMomentDefPostParse(virDomainMomentDefPtr def) { - struct timeval tv; - - gettimeofday(&tv, NULL); + def->creationTime =3D g_get_real_time() / (1000*1000); =20 if (!def->name) - def->name =3D g_strdup_printf("%lld", (long long)tv.tv_sec); + def->name =3D g_strdup_printf("%lld", def->creationTime); =20 - def->creationTime =3D tv.tv_sec; return 0; } diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c index b2f191b2ac..f021ec9c5d 100644 --- a/src/libxl/libxl_driver.c +++ b/src/libxl/libxl_driver.c @@ -235,9 +235,9 @@ libxlTimeoutRegisterEventHook(void *priv, void *xl_priv) { libxlOSEventHookInfoPtr info; - struct timeval now; - struct timeval res; - static struct timeval zero; + gint64 now_us; + gint64 abs_us; + gint64 res_ms; int timeout; =20 if (VIR_ALLOC(info) < 0) @@ -246,15 +246,16 @@ libxlTimeoutRegisterEventHook(void *priv, info->ctx =3D priv; info->xl_priv =3D xl_priv; =20 - gettimeofday(&now, NULL); - timersub(&abs_t, &now, &res); - /* Ensure timeout is not overflowed */ - if (timercmp(&res, &zero, <)) { + now_us =3D g_get_real_time(); + abs_us =3D (abs_t.tv_sec * (1000LL*1000LL)) + abs_t.tv_usec; + if (now_us >=3D abs_us) { timeout =3D 0; - } else if (res.tv_sec > INT_MAX / 1000) { - timeout =3D INT_MAX; } else { - timeout =3D res.tv_sec * 1000 + (res.tv_usec + 999) / 1000; + res_ms =3D (abs_us - now_us) / 1000; + if (res_ms > INT_MAX) + timeout =3D INT_MAX; + else + timeout =3D res_ms; } info->id =3D virEventAddTimeout(timeout, libxlTimerCallback, info, libxlOSEventHookInfoFree); diff --git a/src/qemu/qemu_backup.c b/src/qemu/qemu_backup.c index 294d5999a0..a039beaede 100644 --- a/src/qemu/qemu_backup.c +++ b/src/qemu/qemu_backup.c @@ -715,7 +715,6 @@ qemuBackupBegin(virDomainObjPtr vm, g_autoptr(virQEMUDriverConfig) cfg =3D virQEMUDriverGetConfig(priv->dr= iver); g_autoptr(virDomainBackupDef) def =3D NULL; g_autofree char *suffix =3D NULL; - struct timeval tv; bool pull =3D false; virDomainMomentObjPtr chk =3D NULL; g_autoptr(virDomainCheckpointDef) chkdef =3D NULL; @@ -749,8 +748,8 @@ qemuBackupBegin(virDomainObjPtr vm, =20 suffix =3D g_strdup(chkdef->parent.name); } else { - gettimeofday(&tv, NULL); - suffix =3D g_strdup_printf("%lld", (long long)tv.tv_sec); + gint64 now_us =3D g_get_real_time(); + suffix =3D g_strdup_printf("%lld", (long long)now_us/(1000*1000)); } =20 if (def->type =3D=3D VIR_DOMAIN_BACKUP_TYPE_PULL) diff --git a/src/test/test_driver.c b/src/test/test_driver.c index 2e4ecfd0ff..f8e08dcf07 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -2052,29 +2052,19 @@ testDomainGetHostname(virDomainPtr domain, static int testDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info) { - struct timeval tv; virDomainObjPtr privdom; - int ret =3D -1; =20 if (!(privdom =3D testDomObjFromDomain(domain))) return -1; =20 - if (gettimeofday(&tv, NULL) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("getting time of day")); - goto cleanup; - } - info->state =3D virDomainObjGetState(privdom, NULL); info->memory =3D privdom->def->mem.cur_balloon; info->maxMem =3D virDomainDefGetMemoryTotal(privdom->def); info->nrVirtCpu =3D virDomainDefGetVcpus(privdom->def); - info->cpuTime =3D ((tv.tv_sec * 1000ll * 1000ll * 1000ll) + (tv.tv_us= ec * 1000ll)); - ret =3D 0; + info->cpuTime =3D g_get_real_time() * 1000; =20 - cleanup: virDomainObjEndAPI(&privdom); - return ret; + return 0; } =20 static int @@ -2933,7 +2923,6 @@ static int testDomainGetVcpus(virDomainPtr domain, size_t i; int hostcpus; int ret =3D -1; - struct timeval tv; unsigned long long statbase; virBitmapPtr allcpumap =3D NULL; =20 @@ -2948,13 +2937,7 @@ static int testDomainGetVcpus(virDomainPtr domain, =20 def =3D privdom->def; =20 - if (gettimeofday(&tv, NULL) < 0) { - virReportSystemError(errno, - "%s", _("getting time of day")); - goto cleanup; - } - - statbase =3D (tv.tv_sec * 1000UL * 1000UL) + tv.tv_usec; + statbase =3D g_get_real_time(); =20 hostcpus =3D VIR_NODEINFO_MAXCPUS(privconn->nodeInfo); if (!(allcpumap =3D virBitmapNew(hostcpus))) @@ -4963,7 +4946,6 @@ static int testDomainBlockStats(virDomainPtr domain, virDomainBlockStatsPtr stats) { virDomainObjPtr privdom; - struct timeval tv; unsigned long long statbase; int ret =3D -1; =20 @@ -4985,19 +4967,13 @@ static int testDomainBlockStats(virDomainPtr domain, goto error; } =20 - if (gettimeofday(&tv, NULL) < 0) { - virReportSystemError(errno, - "%s", _("getting time of day")); - goto error; - } - /* No significance to these numbers, just enough to mix it up*/ - statbase =3D (tv.tv_sec * 1000UL * 1000UL) + tv.tv_usec; + statbase =3D g_get_real_time(); stats->rd_req =3D statbase / 10; stats->rd_bytes =3D statbase / 20; stats->wr_req =3D statbase / 30; stats->wr_bytes =3D statbase / 40; - stats->errs =3D tv.tv_sec / 2; + stats->errs =3D statbase / (1000LL * 1000LL * 2); =20 ret =3D 0; error: @@ -5136,7 +5112,6 @@ testDomainInterfaceStats(virDomainPtr domain, virDomainInterfaceStatsPtr stats) { virDomainObjPtr privdom; - struct timeval tv; unsigned long long statbase; virDomainNetDefPtr net =3D NULL; int ret =3D -1; @@ -5151,22 +5126,16 @@ testDomainInterfaceStats(virDomainPtr domain, if (!(net =3D virDomainNetFind(privdom->def, device))) goto error; =20 - if (gettimeofday(&tv, NULL) < 0) { - virReportSystemError(errno, - "%s", _("getting time of day")); - goto error; - } - /* No significance to these numbers, just enough to mix it up*/ - statbase =3D (tv.tv_sec * 1000UL * 1000UL) + tv.tv_usec; + statbase =3D g_get_real_time(); stats->rx_bytes =3D statbase / 10; stats->rx_packets =3D statbase / 100; - stats->rx_errs =3D tv.tv_sec / 1; - stats->rx_drop =3D tv.tv_sec / 2; + stats->rx_errs =3D statbase / (1000LL * 1000LL * 1); + stats->rx_drop =3D statbase / (1000LL * 1000LL * 2); stats->tx_bytes =3D statbase / 20; stats->tx_packets =3D statbase / 110; - stats->tx_errs =3D tv.tv_sec / 3; - stats->tx_drop =3D tv.tv_sec / 4; + stats->tx_errs =3D statbase / (1000LL * 1000LL * 3); + stats->tx_drop =3D statbase / (1000LL * 1000LL * 4); =20 ret =3D 0; error: diff --git a/src/util/virtime.c b/src/util/virtime.c index 701b2f085a..54466b2f39 100644 --- a/src/util/virtime.c +++ b/src/util/virtime.c @@ -43,11 +43,6 @@ =20 VIR_LOG_INIT("util.time"); =20 -/* We prefer clock_gettime if available because that is officially - * async signal safe according to POSIX. Many platforms lack it - * though, so fallback to gettimeofday everywhere else - */ - /** * virTimeMillisNowRaw: * @now: filled with current time in milliseconds @@ -59,22 +54,7 @@ VIR_LOG_INIT("util.time"); */ int virTimeMillisNowRaw(unsigned long long *now) { -#ifdef HAVE_CLOCK_GETTIME - struct timespec ts; - - if (clock_gettime(CLOCK_REALTIME, &ts) < 0) - return -1; - - *now =3D (ts.tv_sec * 1000ull) + (ts.tv_nsec / (1000ull * 1000ull)); -#else - struct timeval tv; - - if (gettimeofday(&tv, NULL) < 0) - return -1; - - *now =3D (tv.tv_sec * 1000ull) + (tv.tv_usec / 1000ull); -#endif - + *now =3D g_get_real_time() / 1000; return 0; } =20 diff --git a/tests/eventtest.c b/tests/eventtest.c index fdd732f7fa..a16922874a 100644 --- a/tests/eventtest.c +++ b/tests/eventtest.c @@ -262,20 +262,14 @@ startJob(void) static int finishJob(const char *name, int handle, int timer) { + unsigned long long now_us; struct timespec waitTime; int rc; -#if HAVE_MACH_CLOCK_ROUTINES - clock_serv_t cclock; - mach_timespec_t mts; - - host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); - clock_get_time(cclock, &mts); - mach_port_deallocate(mach_task_self(), cclock); - waitTime.tv_sec =3D mts.tv_sec; - waitTime.tv_nsec =3D mts.tv_nsec; -#else - clock_gettime(CLOCK_REALTIME, &waitTime); -#endif + + now_us =3D g_get_real_time(); + waitTime.tv_sec =3D now_us / (1000*1000); + waitTime.tv_nsec =3D (now_us % ((now_us / (1000*1000)))) * 1000; + waitTime.tv_sec +=3D 5; rc =3D 0; while (!eventThreadJobDone && rc =3D=3D 0) diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 9d4cdd26dd..9315755990 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -4289,7 +4289,7 @@ virshWatchJob(vshControl *ctl, struct sigaction old_sig_action; struct pollfd pollfd[2] =3D {{.fd =3D pipe_fd, .events =3D POLLIN, .re= vents =3D 0}, {.fd =3D STDIN_FILENO, .events =3D POLLIN, = .revents =3D 0}}; - struct timeval start, curr; + unsigned long long start_us, curr_us; virDomainJobInfo jobinfo; int ret =3D -1; char retchar; @@ -4311,7 +4311,7 @@ virshWatchJob(vshControl *ctl, if (!vshTTYAvailable(ctl)) npollfd =3D 1; =20 - GETTIMEOFDAY(&start); + start_us =3D g_get_real_time(); while (1) { ret =3D poll((struct pollfd *)&pollfd, npollfd, 500); if (ret > 0) { @@ -4345,10 +4345,8 @@ virshWatchJob(vshControl *ctl, goto cleanup; } =20 - GETTIMEOFDAY(&curr); - if (timeout_ms && (((int)(curr.tv_sec - start.tv_sec) * 1000 + - (int)(curr.tv_usec - start.tv_usec) / 1000) > - timeout_ms)) { + curr_us =3D g_get_real_time(); + if (timeout_ms && ((curr_us - start_us)/1000) > timeout_ms) { /* suspend the domain when migration timeouts. */ vshDebug(ctl, VSH_ERR_DEBUG, "%s timeout", label); if (timeout_func) diff --git a/tools/vsh.c b/tools/vsh.c index b982aeb359..a2f33e01aa 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -1286,11 +1286,10 @@ vshCommandRun(vshControl *ctl, const vshCmd *cmd) bool ret =3D true; =20 while (cmd) { - struct timeval before, after; + gint64 before, after; bool enable_timing =3D ctl->timing; =20 - if (enable_timing) - GETTIMEOFDAY(&before); + before =3D g_get_real_time(); =20 if ((cmd->def->flags & VSH_CMD_FLAG_NOCONNECT) || (hooks && hooks->connHandler && hooks->connHandler(ctl))) { @@ -1300,8 +1299,7 @@ vshCommandRun(vshControl *ctl, const vshCmd *cmd) ret =3D false; } =20 - if (enable_timing) - GETTIMEOFDAY(&after); + after =3D g_get_real_time(); =20 /* try to automatically catch disconnections */ if (!ret && @@ -1321,8 +1319,7 @@ vshCommandRun(vshControl *ctl, const vshCmd *cmd) return ret; =20 if (enable_timing) { - double diff_ms =3D (((after.tv_sec - before.tv_sec) * 1000.0) + - ((after.tv_usec - before.tv_usec) / 1000.0)); + double diff_ms =3D (after - before) / 1000.0; =20 vshPrint(ctl, _("\n(Time: %.3f ms)\n\n"), diff_ms); } else { diff --git a/tools/vsh.h b/tools/vsh.h index b6ac070f10..960cae8df0 100644 --- a/tools/vsh.h +++ b/tools/vsh.h @@ -31,7 +31,6 @@ =20 #define VIR_FROM_THIS VIR_FROM_NONE =20 -#define GETTIMEOFDAY(T) gettimeofday(T, NULL) #define VSH_MAX_XML_FILE (10*1024*1024) #define VSH_MATCH(FLAG) (flags & (FLAG)) =20 --=20 2.24.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list