From nobody Thu May 16 02:32:51 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 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=1606327493; cv=none; d=zohomail.com; s=zohoarc; b=GODhYHl59GRj0GAJhlW0u2reynpB/dXp+G2eJfxosxwpLXiBldM6QByX3how0bVVOssfHJP3AN5bZPWhhagu7jgHsCCGPPdUHKemv2RNPsBArXMCTfUsU4j8KVMjtY583WX7fLsFTWEHN0fkjyKQBLYHanAniEdk4yZgFvsgaro= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1606327493; h=Content-Type:Content-Transfer-Encoding:Cc: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=fzRVUoBhRfrUbVCEQCieXqKv+9C57QEYjdG5PauMfCc=; b=MQSrMaqHzkzF580gO4U7NzEDVHUr3WI7zplV/3rInkTYJLoMbrnu6scotBneH2NuN4ZmqeHzm4mANcoxoIuZoTU2M9tHILHtKyCzxHiDPffZJmEaq7ECZmr+V2iZl/lquAY3nzawrfpnD98FsBZk0g14vEihKb4Q0TkyeFSXEng= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 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-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 1606327493280218.78979699062438; Wed, 25 Nov 2020 10:04: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-244-RZWIuIidOz23fyGbGBzcKg-1; Wed, 25 Nov 2020 13:04:45 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CABD71005D5E; Wed, 25 Nov 2020 18:04:39 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7BC5260854; Wed, 25 Nov 2020 18:04:38 +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 4F0D3180954D; Wed, 25 Nov 2020 18:04:37 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 0API4acw017721 for ; Wed, 25 Nov 2020 13:04:36 -0500 Received: by smtp.corp.redhat.com (Postfix) id 0D8C160864; Wed, 25 Nov 2020 18:04:36 +0000 (UTC) Received: from domokun.gsslab.fab.redhat.com (gx270-2.gsslab.fab.redhat.com [10.33.8.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2AF1560855; Wed, 25 Nov 2020 18:04:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1606327490; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc: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=fzRVUoBhRfrUbVCEQCieXqKv+9C57QEYjdG5PauMfCc=; b=SfAjWi/EwPMpI5/wCq/9fUi41mwfzyfsV8b2ZkB1RFZmYsyRiqmX+ZnbYBnhlfSaJn0Pmq 9hjLyrBG2Vy+yznOD6LfmYOD+t1wedicqSKQvqbpbl3Ykm5xmJ31vKSldTfy6DKFoBsiex guLBVEU61xwXK4q+HvEG3tPsNtxXewY= X-MC-Unique: RZWIuIidOz23fyGbGBzcKg-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH 1/2] remote: make ssh-helper massively faster Date: Wed, 25 Nov 2020 18:04:26 +0000 Message-Id: <20201125180427.132863-2-berrange@redhat.com> In-Reply-To: <20201125180427.132863-1-berrange@redhat.com> References: <20201125180427.132863-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Cc: Christian Ehrhardt 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.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) It was reported that the performance of tunnelled migration and volume upload/download regressed in 6.9.0, when the virt-ssh-helper is used for remote SSH tunnelling instead of netcat. When seeing data available to read from stdin, or the socket, the current code will allocate at most 1k of extra space in the buffer it has. After writing data to the socket, or stdout, if more than 1k of extra space is in the buffer, it will reallocate to free up that space. This results in a huge number of mallocs when doing I/O, as well as a huge number of syscalls since at most 1k of data will be read/written at a time. Also if writing blocks for some reason, it will continue to read data with no memory bound which is bad. This changes the code to use a 1 MB fixed size buffer in each direction. If that buffer becomes full, it will update the watches to stop reading more data. It will never reallocate the buffer at runtime. This increases the performance by orders of magnitude. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Daniel Henrique Barboza Tested-by: Christian Ehrhardt --- src/remote/remote_ssh_helper.c | 113 ++++++++++++++++++++------------- 1 file changed, 68 insertions(+), 45 deletions(-) diff --git a/src/remote/remote_ssh_helper.c b/src/remote/remote_ssh_helper.c index 0da55c1d1f..8ed7e64507 100644 --- a/src/remote/remote_ssh_helper.c +++ b/src/remote/remote_ssh_helper.c @@ -32,6 +32,8 @@ =20 #define VIR_FROM_THIS VIR_FROM_REMOTE =20 +#define SSH_BUF_SIZE (1024 * 1024) + VIR_LOG_INIT("remote.remote_ssh_helper"); =20 struct virRemoteSSHHelperBuffer { @@ -45,8 +47,11 @@ typedef virRemoteSSHHelper *virRemoteSSHHelperPtr; struct virRemoteSSHHelper { bool quit; virNetSocketPtr sock; + int sockEvents; int stdinWatch; + int stdinEvents; int stdoutWatch; + int stdoutEvents; =20 struct virRemoteSSHHelperBuffer sockToTerminal; struct virRemoteSSHHelperBuffer terminalToSock; @@ -75,6 +80,40 @@ virRemoteSSHHelperShutdown(virRemoteSSHHelperPtr proxy) } =20 =20 +static void +virRemoteSSHHelperUpdateEvents(virRemoteSSHHelperPtr proxy) +{ + int sockEvents =3D 0; + int stdinEvents =3D 0; + int stdoutEvents =3D 0; + + if (proxy->terminalToSock.offset !=3D 0) + sockEvents |=3D VIR_EVENT_HANDLE_WRITABLE; + if (proxy->terminalToSock.offset < proxy->terminalToSock.length) + stdinEvents |=3D VIR_EVENT_HANDLE_READABLE; + + if (proxy->sockToTerminal.offset !=3D 0) + stdoutEvents |=3D VIR_EVENT_HANDLE_WRITABLE; + if (proxy->sockToTerminal.offset < proxy->sockToTerminal.length) + sockEvents |=3D VIR_EVENT_HANDLE_READABLE; + + if (sockEvents !=3D proxy->sockEvents) { + VIR_DEBUG("Update sock events %d -> %d", proxy->sockEvents, sockEv= ents); + virNetSocketUpdateIOCallback(proxy->sock, sockEvents); + proxy->sockEvents =3D sockEvents; + } + if (stdinEvents !=3D proxy->stdinEvents) { + VIR_DEBUG("Update stdin events %d -> %d", proxy->stdinEvents, stdi= nEvents); + virEventUpdateHandle(proxy->stdinWatch, stdinEvents); + proxy->stdinEvents =3D stdinEvents; + } + if (stdoutEvents !=3D proxy->stdoutEvents) { + VIR_DEBUG("Update stdout events %d -> %d", proxy->stdoutEvents, st= doutEvents); + virEventUpdateHandle(proxy->stdoutWatch, stdoutEvents); + proxy->stdoutEvents =3D stdoutEvents; + } +} + static void virRemoteSSHHelperEventOnSocket(virNetSocketPtr sock, int events, @@ -91,14 +130,9 @@ virRemoteSSHHelperEventOnSocket(virNetSocketPtr sock, proxy->sockToTerminal.offset; int got; =20 - if (avail < 1024) { - if (VIR_REALLOC_N(proxy->sockToTerminal.data, - proxy->sockToTerminal.length + 1024) < 0) { - virRemoteSSHHelperShutdown(proxy); - return; - } - proxy->sockToTerminal.length +=3D 1024; - avail +=3D 1024; + if (avail =3D=3D 0) { + VIR_DEBUG("Unexpectedly called with no space in buffer"); + goto cleanup; } =20 got =3D virNetSocketRead(sock, @@ -117,15 +151,11 @@ virRemoteSSHHelperEventOnSocket(virNetSocketPtr sock, return; } proxy->sockToTerminal.offset +=3D got; - if (proxy->sockToTerminal.offset) - virEventUpdateHandle(proxy->stdoutWatch, - VIR_EVENT_HANDLE_WRITABLE); } =20 if (events & VIR_EVENT_HANDLE_WRITABLE && proxy->terminalToSock.offset) { ssize_t done; - size_t avail; done =3D virNetSocketWrite(proxy->sock, proxy->terminalToSock.data, proxy->terminalToSock.offset); @@ -135,26 +165,21 @@ virRemoteSSHHelperEventOnSocket(virNetSocketPtr sock, virRemoteSSHHelperShutdown(proxy); return; } + memmove(proxy->terminalToSock.data, proxy->terminalToSock.data + done, proxy->terminalToSock.offset - done); proxy->terminalToSock.offset -=3D done; - - avail =3D proxy->terminalToSock.length - proxy->terminalToSock.off= set; - if (avail > 1024) { - ignore_value(VIR_REALLOC_N(proxy->terminalToSock.data, - proxy->terminalToSock.offset + 1024= )); - proxy->terminalToSock.length =3D proxy->terminalToSock.offset = + 1024; - } } - if (!proxy->terminalToSock.offset) - virNetSocketUpdateIOCallback(proxy->sock, - VIR_EVENT_HANDLE_READABLE); =20 if (events & VIR_EVENT_HANDLE_ERROR || events & VIR_EVENT_HANDLE_HANGUP) { virRemoteSSHHelperShutdown(proxy); + return; } + + cleanup: + virRemoteSSHHelperUpdateEvents(proxy); } =20 =20 @@ -175,14 +200,9 @@ virRemoteSSHHelperEventOnStdin(int watch G_GNUC_UNUSED, proxy->terminalToSock.offset; int got; =20 - if (avail < 1024) { - if (VIR_REALLOC_N(proxy->terminalToSock.data, - proxy->terminalToSock.length + 1024) < 0) { - virRemoteSSHHelperShutdown(proxy); - return; - } - proxy->terminalToSock.length +=3D 1024; - avail +=3D 1024; + if (avail =3D=3D 0) { + VIR_DEBUG("Unexpectedly called with no space in buffer"); + goto cleanup; } =20 got =3D read(fd, @@ -203,10 +223,6 @@ virRemoteSSHHelperEventOnStdin(int watch G_GNUC_UNUSED, } =20 proxy->terminalToSock.offset +=3D got; - if (proxy->terminalToSock.offset) - virNetSocketUpdateIOCallback(proxy->sock, - VIR_EVENT_HANDLE_READABLE | - VIR_EVENT_HANDLE_WRITABLE); } =20 if (events & VIR_EVENT_HANDLE_ERROR) { @@ -220,6 +236,9 @@ virRemoteSSHHelperEventOnStdin(int watch G_GNUC_UNUSED, virRemoteSSHHelperShutdown(proxy); return; } + + cleanup: + virRemoteSSHHelperUpdateEvents(proxy); } =20 =20 @@ -238,7 +257,6 @@ virRemoteSSHHelperEventOnStdout(int watch G_GNUC_UNUSED, if (events & VIR_EVENT_HANDLE_WRITABLE && proxy->sockToTerminal.offset) { ssize_t done; - size_t avail; done =3D write(fd, proxy->sockToTerminal.data, proxy->sockToTerminal.offset); @@ -253,18 +271,8 @@ virRemoteSSHHelperEventOnStdout(int watch G_GNUC_UNUSE= D, proxy->sockToTerminal.data + done, proxy->sockToTerminal.offset - done); proxy->sockToTerminal.offset -=3D done; - - avail =3D proxy->sockToTerminal.length - proxy->sockToTerminal.off= set; - if (avail > 1024) { - ignore_value(VIR_REALLOC_N(proxy->sockToTerminal.data, - proxy->sockToTerminal.offset + 1024= )); - proxy->sockToTerminal.length =3D proxy->sockToTerminal.offset = + 1024; - } } =20 - if (!proxy->sockToTerminal.offset) - virEventUpdateHandle(proxy->stdoutWatch, 0); - if (events & VIR_EVENT_HANDLE_ERROR) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("IO error stdout")); virRemoteSSHHelperShutdown(proxy); @@ -276,6 +284,8 @@ virRemoteSSHHelperEventOnStdout(int watch G_GNUC_UNUSED, virRemoteSSHHelperShutdown(proxy); return; } + + virRemoteSSHHelperUpdateEvents(proxy); } =20 =20 @@ -285,8 +295,21 @@ virRemoteSSHHelperRun(virNetSocketPtr sock) int ret =3D -1; virRemoteSSHHelper proxy =3D { .sock =3D sock, + .sockEvents =3D VIR_EVENT_HANDLE_READABLE, .stdinWatch =3D -1, + .stdinEvents =3D VIR_EVENT_HANDLE_READABLE, .stdoutWatch =3D -1, + .stdoutEvents =3D 0, + .sockToTerminal =3D { + .offset =3D 0, + .length =3D SSH_BUF_SIZE, + .data =3D g_new0(char, SSH_BUF_SIZE), + }, + .terminalToSock =3D { + .offset =3D 0, + .length =3D SSH_BUF_SIZE, + .data =3D g_new0(char, SSH_BUF_SIZE), + }, }; =20 virEventRegisterDefaultImpl(); --=20 2.25.4 From nobody Thu May 16 02:32:51 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 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=1606327492; cv=none; d=zohomail.com; s=zohoarc; b=K+EcJc4ypNU4RCHH/iae0pIsC22w0IwjCXHZ9tVPgpN6n5eP7WO7M7m7bg1n/viSp6ru2gHZkGBqSg4jl+wFPy1d2kiIb40LZcO8KIgvi9767kOdpI3F26K+kGa1OAu7QlFilPzfHXVONgS4t8n/kB3W77KZqBuFymCblVXnbBo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1606327492; h=Content-Type:Content-Transfer-Encoding:Cc: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=x0MBA8bOzXVWCmryJ64yGrpF4oAKLW1TqbxQIrzFJmw=; b=A2itUx5eTqZV6uh8ipvzftYEPw1hKPBp+karJWtjg9o8xT4LWc0zXr7CA+bub8RCVbhDSIJwNOR344bFtkg6P1XoubVZsuEnl+elbpUaMRM7M7RpFXKryVk5w+xL1LJK0pyoqa881DmkiEcJRILY3i6/d/0ibj1OAzoXBYC4xxM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 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-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1606327492772980.43378111289; Wed, 25 Nov 2020 10:04:52 -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-13-z5xQmfKJP5OLEGNUSsT-yw-1; Wed, 25 Nov 2020 13:04:46 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 788A884A5E6; Wed, 25 Nov 2020 18:04:40 +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 52BAC19C48; Wed, 25 Nov 2020 18:04:40 +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 1F35A4EE0B; Wed, 25 Nov 2020 18:04:40 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 0API4b2i017726 for ; Wed, 25 Nov 2020 13:04:37 -0500 Received: by smtp.corp.redhat.com (Postfix) id 1353060864; Wed, 25 Nov 2020 18:04:37 +0000 (UTC) Received: from domokun.gsslab.fab.redhat.com (gx270-2.gsslab.fab.redhat.com [10.33.8.41]) by smtp.corp.redhat.com (Postfix) with ESMTP id 53F0560854; Wed, 25 Nov 2020 18:04:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1606327490; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc: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=x0MBA8bOzXVWCmryJ64yGrpF4oAKLW1TqbxQIrzFJmw=; b=Ix2ddu1VKWLITF/MRaoMQDeEZRqJwCfSpi4ayTfHJYrFY8NRU1UO2lWHOreQLenuRR0IKR +5YvRVTG44JYlJ0J5XpupWi8L8ErJGtv7CWEA+4++/9v26MJgZ7JWD62b3nSuWNb/tbVwu ShWdVoSFxyc2iH3QQJCSt/tvxXomRiA= X-MC-Unique: z5xQmfKJP5OLEGNUSsT-yw-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH 2/2] util: avoid glib event loop workaround where possible Date: Wed, 25 Nov 2020 18:04:27 +0000 Message-Id: <20201125180427.132863-3-berrange@redhat.com> In-Reply-To: <20201125180427.132863-1-berrange@redhat.com> References: <20201125180427.132863-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Cc: Christian Ehrhardt 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.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) I previously did a workaround for a glib event loop race that causes crashes: commit 0db4743645b7a0611a3c0687f834205c9956f7fc Author: Daniel P. Berrang=C3=A9 Date: Tue Jul 28 16:52:47 2020 +0100 util: avoid crash due to race in glib event loop code it turns out that the workaround has a significant performance penalty on I/O intensive workloads. We thus need to avoid the workaround if we know we have a new enough glib to avoid the race condition. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Daniel Henrique Barboza Tested-by: Christian Ehrhardt --- src/util/vireventglib.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/util/vireventglib.c b/src/util/vireventglib.c index 6842c6e806..8c5495bfab 100644 --- a/src/util/vireventglib.c +++ b/src/util/vireventglib.c @@ -189,9 +189,21 @@ virEventGLibHandleFind(int watch) * If the last reference to a GSource is released in a non-main * thread we're exposed to a race condition that causes a * crash: - * https://gitlab.gnome.org/GNOME/glib/-/merge_requests/1358 - * Thus we're using an idle func to release our ref + * + * https://gitlab.gnome.org/GNOME/glib/-/merge_requests/1358 + * + * Thus we're using an idle func to release our ref... + * + * ...but this imposes a significant performance penalty on + * I/O intensive workloads which are sensitive to the iterations + * of the event loop, so avoid the workaround if we know we have + * new enough glib. */ +#if GLIB_CHECK_VERSION(2, 64, 0) +# define g_vir_source_unref_safe(source) g_source_unref(source) +#else +# define g_vir_source_unref_safe(source) g_idle_add(virEventGLibSourceUnre= fIdle, source); + static gboolean virEventGLibSourceUnrefIdle(gpointer data) { @@ -201,6 +213,7 @@ virEventGLibSourceUnrefIdle(gpointer data) =20 return FALSE; } +#endif =20 =20 static void @@ -231,7 +244,7 @@ virEventGLibHandleUpdate(int watch, if (data->source !=3D NULL) { VIR_DEBUG("Removed old handle source=3D%p", data->source); g_source_destroy(data->source); - g_idle_add(virEventGLibSourceUnrefIdle, data->source); + g_vir_source_unref_safe(data->source); } =20 data->source =3D virEventGLibAddSocketWatch( @@ -245,7 +258,7 @@ virEventGLibHandleUpdate(int watch, =20 VIR_DEBUG("Removed old handle source=3D%p", data->source); g_source_destroy(data->source); - g_idle_add(virEventGLibSourceUnrefIdle, data->source); + g_vir_source_unref_safe(data->source); data->source =3D NULL; data->events =3D 0; } @@ -294,7 +307,7 @@ virEventGLibHandleRemove(int watch) =20 if (data->source !=3D NULL) { g_source_destroy(data->source); - g_idle_add(virEventGLibSourceUnrefIdle, data->source); + g_vir_source_unref_safe(data->source); data->source =3D NULL; data->events =3D 0; } @@ -427,7 +440,7 @@ virEventGLibTimeoutUpdate(int timer, if (interval >=3D 0) { if (data->source !=3D NULL) { g_source_destroy(data->source); - g_idle_add(virEventGLibSourceUnrefIdle, data->source); + g_vir_source_unref_safe(data->source); } =20 data->interval =3D interval; @@ -437,7 +450,7 @@ virEventGLibTimeoutUpdate(int timer, goto cleanup; =20 g_source_destroy(data->source); - g_idle_add(virEventGLibSourceUnrefIdle, data->source); + g_vir_source_unref_safe(data->source); data->source =3D NULL; } =20 @@ -486,7 +499,7 @@ virEventGLibTimeoutRemove(int timer) =20 if (data->source !=3D NULL) { g_source_destroy(data->source); - g_idle_add(virEventGLibSourceUnrefIdle, data->source); + g_vir_source_unref_safe(data->source); data->source =3D NULL; } =20 --=20 2.25.4