Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1owU5n-00057T-B2 for pgsql-hackers@arkaria.postgresql.org; Sat, 19 Nov 2022 20:05:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1owU5l-0004R0-Uq for pgsql-hackers@arkaria.postgresql.org; Sat, 19 Nov 2022 20:05:33 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1owU5l-0004Qq-J6 for pgsql-hackers@lists.postgresql.org; Sat, 19 Nov 2022 20:05:33 +0000 Received: from mail-ed1-x52a.google.com ([2a00:1450:4864:20::52a]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1owU5j-0006sE-2C for pgsql-hackers@postgresql.org; Sat, 19 Nov 2022 20:05:33 +0000 Received: by mail-ed1-x52a.google.com with SMTP id y24so6397508edi.10 for ; Sat, 19 Nov 2022 12:05:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=D2MogPbWnpuUNbxYSnQ14pPdDwuQIGsF0g8gUZEMgSw=; b=pW1ePo6a3BIGbwAiSf6R/FKG8ZK14yQmApmz0dFYOHgRbCeckyEmaarMYWbbFkwhiW wyEu846sxqmeV2I+AxArx0Vr7R6MiJEVyequQaXqLIYiZ87dPLypFoXBON1p43SpRv6U NaAC3aOKTGyV4H3lizOu0HPr1nLh3SiV+XhIF91cTsPvvyiZlmwkEydTpeQ7GFlj0Ve1 96pT9ryHPGwgl3HhwYQQX6gkmNDbJw13sGrhKNX4L+EdyiwQRBL/RxC/gLQV1xEQYqU7 UW4nEilKWyU6DROFvD+qfSyOWJN9gW5o6qa9G8sWcZ3R9UuNVFn2STrNzJjo41tNVPUi mxhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=D2MogPbWnpuUNbxYSnQ14pPdDwuQIGsF0g8gUZEMgSw=; b=KVrqgBNRAXbsqVQd4w/qT1czHWT22o4FwBP/xTpWH1ZMQAMFzRA3lF+8icgyvW8FpW LvidLUSzZzBTOIV8xo9P1wBe7HMNo0yEYhnnzFgedX4p5whwFX/xbFQKAsTAroR9Ijto yAnkOp2GRNk4Jsp1E7fQ+SKGv9rg/pWxPp35Of2XGWDxiuJ30tJlYrKTNBIyEYGWSJGO tZIvCU2Pcc6Hv03FYEQfYnEgNzqZYmUnZfiui4n3Woy2wm3jYuqFAVJorVLEMEy7OLys NzzdB4y10Vs0ABFvi39AFIyDCQGB4AFqNSZer1HwI3NtaFRvWEVgNzvDC8+jSD4XnME2 Q3fg== X-Gm-Message-State: ANoB5pnIgL9Uas2JGeilkybA5N2mpr04lJP+JHmliZV2tECXH0kJSGZC rYoIRNkwgUv1wM++HASw7ds= X-Google-Smtp-Source: AA0mqf5tVspIpbxQZ8RPHm5pJMiZjBuN++LmB3/wZfELPYMIFWU0xJevRHuORVvyAZypJPX/9q4FdA== X-Received: by 2002:a05:6402:4311:b0:458:c66a:3664 with SMTP id m17-20020a056402431100b00458c66a3664mr2402033edc.79.1668888329809; Sat, 19 Nov 2022 12:05:29 -0800 (PST) Received: from [172.16.209.129] ([147.161.235.14]) by smtp.gmail.com with ESMTPSA id ek19-20020a056402371300b00458824aee80sm3246120edb.38.2022.11.19.12.05.29 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 19 Nov 2022 12:05:29 -0800 (PST) Message-ID: Date: Sat, 19 Nov 2022 21:05:28 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: Re: Reduce timing overhead of EXPLAIN ANALYZE using rdtsc? Content-Language: en-US To: Michael Paquier , Ibrar Ahmed Cc: Maciek Sakrejda , Andres Freund , Lukas Fittl , pgsql-hackers References: <20200612232810.f46nbqkdhbutzqdg@alap3.anarazel.de> <20220701172639.ty4iu5almspueriu@alap3.anarazel.de> From: David Geier In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk I think it would be great to get this patch committed. Beyond the reasons already mentioned, the significant overhead also tends to skew the reported runtimes in ways that makes it difficult to compare them. For example, if two nodes are executed equally often but one needs twice the time to process the rows: in such a case EXPLAIN ANALYZE should report timings that are 2x apart. However, currently, the high overhead of clock_gettime() tends to skew the relative runtimes. On 10/12/22 10:33, Michael Paquier wrote: > No rebased version has been sent since this update, so this patch has > been marked as RwF. I've rebased the patch set on latest master and fixed a few compiler warnings. Beyond that some findings and thoughts: You're only using RDTSC if the clock source is 'tsc'. Great idea to not bother caring about a lot of hairy TSC details. Looking at the kernel code this seems to imply that the TSC is frequency invariant. I don't think though that this implies that Linux is not running under a hypervisor; which is good because I assume PostgreSQL is used a lot in VMs. However, when running under a hypervisor (at least with VMWare) CPUID leaf 0x16 is not available. In my tests __get_cpuid() indicated success but the returned values were garbage. Instead of using leaf 0x16, we should then use the hypervisor interface to obtain the TSC frequency. Checking if a hypervisor is active can be done via: bool IsHypervisorActive() {     uint32 cpuinfo[4] = {0};     int res = __get_cpuid(0x1, &cpuinfo[0], &cpuinfo[1], &cpuinfo[2], &cpuinfo[3]);     return res > 0 && (cpuinfo[2] & (1 << 30)); } Obtaining the TSC frequency via the hypervisor interface can be done with the following code. See https://lwn.net/Articles/301888/ for more details. // Under hypervisors (tested with VMWare) leaf 0x16 is not available, even though __get_cpuid() succeeds. // Hence, if running under a hypervisor, use the hypervisor interface to obtain TSC frequency. uint32 cpuinfo[4] = {0}; if (IsHypervisorActive() && __get_cpuid(0x40000001, &cpuinfo[0], &cpuinfo[1], &cpuinfo[2], &cpuinfo[3]) > 0)     cycles_to_sec = 1.0 / ((double)cpuinfo[0] * 1000 * 1000); Given that we anyways switch between RDTSC and clock_gettime() with a global variable, what about exposing the clock source as GUC? That way the user can switch back to a working clock source in case we miss a detail around activating or reading the TSC. I'm happy to update the patches accordingly. -- David Geier (ServiceNow)