Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1scm8k-00AOTB-0s for pgsql-hackers@arkaria.postgresql.org; Sat, 10 Aug 2024 13:28:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1scm8i-008lJJ-Ob for pgsql-hackers@arkaria.postgresql.org; Sat, 10 Aug 2024 13:28:12 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1scm8i-008lJ4-7w for pgsql-hackers@lists.postgresql.org; Sat, 10 Aug 2024 13:28:12 +0000 Received: from mail-io1-xd2d.google.com ([2607:f8b0:4864:20::d2d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1scm8f-0042mJ-91 for pgsql-hackers@postgresql.org; Sat, 10 Aug 2024 13:28:10 +0000 Received: by mail-io1-xd2d.google.com with SMTP id ca18e2360f4ac-816ca307407so107111039f.2 for ; Sat, 10 Aug 2024 06:28:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723296488; x=1723901288; darn=postgresql.org; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=y1jug5VUPhq9GJHZgEi60Xp58WwmkRJve7Oh4NXdaUw=; b=JqNCkfvf0F02NRBtIPQBviYKjzWD2JMi9UB/nK5cJkpCboa6wO4uFac0kY7baN2O9F Qbm97OmGfpmpczOfJ4piFjMNWZgi3wzHukSP7cBP7b7yiL0OBF2NeWvVlcY0sdoY3JTM Y0AXvZle4CGJz+XzCXgIkig7k99APdZDJ6dgD2yqyokiFonVuJ0I80ZEbx6HY5g4iYCU VXTGX4aVyV1T5jRyW8qpJZzH6JESp0534+VCxIatKpswlDNxgnG51L4B/FcCXGtKi6A3 mEnctZjJWtHalvZI2JFNATG1o/IN2i7ojOJQjTt/fanJIxh+V/y7pB9P3lFQ0BIV0To0 IT8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723296488; x=1723901288; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=y1jug5VUPhq9GJHZgEi60Xp58WwmkRJve7Oh4NXdaUw=; b=exW/GHMuNznrGsVGC1WGibDC/74VUcthVHtXquKGY8jmURFVggv9EBVhZX7sG7IxOo pDnOCnCdzihRTiWs5RmlrDBQzzwDt2KUJnG62F5NyekOgky9xrthecS4alR8jdh74gQc hbjvqbiuRGGgTlXZ+R843HCF82qmodLHHUhAmxfOKtOjHytqerkafQ/I+R3qOvIg0bVV mYTSR+VzFlCi+mYVyPtt8EY9Mxs1C9NLkdLA+x3RSSrBIgBTt7kVdg4xvN8bhm/qou78 F17oytP+kAaSXe4sChc7eoqBrWeffKHDJL3abFYhXaXW71ErAtVdoOn+NYL5gMnWVx6k UwVg== X-Forwarded-Encrypted: i=1; AJvYcCXN6sk/iLIDoyiHihriu5sXVrjAyOsG3UA2wMWhf40Jyzk/SJR7cl9xSku2rVBc9bMT2au+BrWMESDmL4NQtkXDNHO0hLcH8JVsUKuW X-Gm-Message-State: AOJu0YzYfIkcNv+z8xySSitNky390UrZHxmBGOOztPytZFZSawQMKVxv ibHcgJ2FTSaFJC/mqUDmlJGm+jF6dHnkV69UoGAKOCWK5xbBywfj+Z4tHP0I X-Google-Smtp-Source: AGHT+IFSEFqHGiaC3vbrdkpEN7aTg/hzMIbLRy2fBCJe0GWJO5ZGYDHYA5xvg+kMLRxWhgAO2UYq4Q== X-Received: by 2002:a92:b708:0:b0:39b:2133:8ed4 with SMTP id e9e14a558f8ab-39b7a40fc7fmr48812885ab.14.1723296488380; Sat, 10 Aug 2024 06:28:08 -0700 (PDT) Received: from smtpclient.apple (c-73-74-200-24.hsd1.il.comcast.net. [73.74.200.24]) by smtp.gmail.com with ESMTPSA id e9e14a558f8ab-39c30ad7dc4sm5568765ab.67.2024.08.10.06.28.07 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 10 Aug 2024 06:28:07 -0700 (PDT) From: Sami Imseih Message-Id: <526E6A51-FE30-47C4-85C8-F2D18877607F@gmail.com> Content-Type: multipart/mixed; boundary="Apple-Mail=_04946F70-4E12-4F8E-97EE-BC0186797AEE" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3774.600.62\)) Subject: Re: Restart pg_usleep when interrupted Date: Sat, 10 Aug 2024 08:27:56 -0500 In-Reply-To: <9D64CD7C-CA24-4FF4-9421-3A1A63F87DAC@gmail.com> Cc: Nathan Bossart , Tom Lane , Robert Haas , "pgsql-hackers@postgresql.org" To: Bertrand Drouvot References: <33287B8A-3CA0-494F-87D3-C9D85C6468FE@gmail.com> <19E276C9-2C2B-435A-B275-8FA22222AEB8@gmail.com> <50E1D6AD-3E51-4E45-95A7-7B20DC037DCB@gmail.com> <9D64CD7C-CA24-4FF4-9421-3A1A63F87DAC@gmail.com> X-Mailer: Apple Mail (2.3774.600.62) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_04946F70-4E12-4F8E-97EE-BC0186797AEE Content-Disposition: attachment; filename=v9-0001-vaccum_delay-with-absolute-time-nanosleep.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="v9-0001-vaccum_delay-with-absolute-time-nanosleep.patch" Content-Transfer-Encoding: quoted-printable =46rom=200fecbea4fc4c49a9f8657005095e1cab081451be=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20Sami=20Imseih=20=0ADate:=20= Fri,=209=20Aug=202024=2018:02:12=20-0500=0ASubject:=20[PATCH=20v9=201/1]=20= vaccum_delay=20with=20absolute=20time=20nanosleep=0A=0A---=0A=20= src/backend/commands/vacuum.c=20=20=20=20=20=20=20=20|=20=202=20+-=0A=20= src/backend/port/win32/signal.c=20=20=20=20=20=20|=2010=20+++++++=0A=20= src/include/port.h=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= |=20=201=20+=0A=20src/include/portability/instr_time.h=20|=2010=20= +++++++=0A=20src/port/pgsleep.c=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20|=2043=20++++++++++++++++++++++++++++=0A=205=20files=20= changed,=2065=20insertions(+),=201=20deletion(-)=0A=0Adiff=20--git=20= a/src/backend/commands/vacuum.c=20b/src/backend/commands/vacuum.c=0A= index=2048f8eab202..43333c4698=20100644=0A---=20= a/src/backend/commands/vacuum.c=0A+++=20b/src/backend/commands/vacuum.c=0A= @@=20-2384,7=20+2384,7=20@@=20vacuum_delay_point(void)=0A=20=09=09=09= msec=20=3D=20vacuum_cost_delay=20*=204;=0A=20=0A=20=09=09= pgstat_report_wait_start(WAIT_EVENT_VACUUM_DELAY);=0A-=09=09= pg_usleep(msec=20*=201000);=0A+=09=09pg_usleep_non_interruptible(msec=20= *=201000);=0A=20=09=09pgstat_report_wait_end();=0A=20=0A=20=09=09/*=0A= diff=20--git=20a/src/backend/port/win32/signal.c=20= b/src/backend/port/win32/signal.c=0Aindex=20285cb611b4..edcb181215=20= 100644=0A---=20a/src/backend/port/win32/signal.c=0A+++=20= b/src/backend/port/win32/signal.c=0A@@=20-73,6=20+73,16=20@@=20= pg_usleep(long=20microsec)=0A=20=09}=0A=20}=0A=20=0A+/*=0A+=20*=20= pg_usleep_non_interruptible=20---=20delay=20the=20specified=20number=20= of=20microseconds.=0A+=20*=0A+=20*=20Unlike=20pg_usleep,=20this=20relies=20= on=20a=20non-interruptible=20sleep.=0A+=20*/=0A+void=0A= +pg_usleep_non_interruptible(long=20microsec)=0A+{=0A+=09= SleepEx((microsec=20<=20500=20?=201=20:=20(microsec=20+=20500)=20/=20= 1000),=20FALSE);=0A+}=0A=20=0A=20/*=20Initialization=20*/=0A=20void=0A= diff=20--git=20a/src/include/port.h=20b/src/include/port.h=0Aindex=20= c740005267..c8ff23e5ee=20100644=0A---=20a/src/include/port.h=0A+++=20= b/src/include/port.h=0A@@=20-162,6=20+162,7=20@@=20extern=20int=09= pg_disable_aslr(void);=0A=20=0A=20/*=20Portable=20delay=20handling=20*/=0A= =20extern=20void=20pg_usleep(long=20microsec);=0A+extern=20void=20= pg_usleep_non_interruptible(long=20microsec);=0A=20=0A=20/*=20Portable=20= SQL-like=20case-independent=20comparisons=20and=20conversions=20*/=0A=20= extern=20int=09pg_strcasecmp(const=20char=20*s1,=20const=20char=20*s2);=0A= diff=20--git=20a/src/include/portability/instr_time.h=20= b/src/include/portability/instr_time.h=0Aindex=20e66ecf34cd..6e4b0f1b17=20= 100644=0A---=20a/src/include/portability/instr_time.h=0A+++=20= b/src/include/portability/instr_time.h=0A@@=20-36,6=20+36,10=20@@=0A=20=20= *=0A=20=20*=20INSTR_TIME_GET_NANOSEC(t)=09=09convert=20t=20to=20int64=20= (in=20nanoseconds)=0A=20=20*=0A+=20*=20INSTR_TIME_ADD_MICROSEC(x,t)=09=09= add=20t=20(in=20microseconds)=20to=20x=0A+=20*=0A+=20*=20= INSTR_TIME_IS_GREATER(x,y)=09=09is=20x=20greater=20than=20y?=0A+=20*=0A=20= =20*=20Note=20that=20INSTR_TIME_SUBTRACT=20and=20INSTR_TIME_ACCUM_DIFF=20= convert=0A=20=20*=20absolute=20times=20to=20intervals.=20=20The=20= INSTR_TIME_GET_xxx=20operations=20are=0A=20=20*=20only=20useful=20on=20= intervals.=0A@@=20-194,4=20+198,10=20@@=20GetTimerFrequency(void)=0A=20= #define=20INSTR_TIME_GET_MICROSEC(t)=20\=0A=20=09= (INSTR_TIME_GET_NANOSEC(t)=20/=20NS_PER_US)=0A=20=0A+#define=20= INSTR_TIME_ADD_MICROSEC(x,t)=20\=0A+=09((x).ticks=20+=3D=20(t)=20*=20= NS_PER_US)=0A+=0A+#define=20INSTR_TIME_IS_GREATER(x,y)=20\=0A+=09= ((x).ticks=20>=20(y).ticks)=0A+=0A=20#endif=09=09=09=09=09=09=09/*=20= INSTR_TIME_H=20*/=0Adiff=20--git=20a/src/port/pgsleep.c=20= b/src/port/pgsleep.c=0Aindex=201284458bfc..9525156abc=20100644=0A---=20= a/src/port/pgsleep.c=0A+++=20b/src/port/pgsleep.c=0A@@=20-14,6=20+14,8=20= @@=0A=20=0A=20#include=20=0A=20=0A+#include=20= "portability/instr_time.h"=0A+=0A=20/*=0A=20=20*=20In=20a=20Windows=20= backend,=20we=20don't=20use=20this=20implementation,=20but=20rather=0A=20= =20*=20the=20signal-aware=20version=20in=20= src/backend/port/win32/signal.c.=0A@@=20-54,4=20+56,45=20@@=20= pg_usleep(long=20microsec)=0A=20=09}=0A=20}=0A=20=0A+/*=0A+=20*=20= pg_usleep_non_interruptible=20---=20delay=20the=20specified=20number=20= of=20microseconds.=0A+=20*=0A+=20*=20Unlike=20pg_usleep,=20This=20= function=20continues=20the=20delay=20in=20case=20of=20an=0A+=20*=20= interrupt.=0A+=20*/=0A+void=0A+pg_usleep_non_interruptible(long=20= microsec)=0A+{=0A+=09/*=0A+=09=20*=20We=20allow=20nanosleep=20to=20= handle=20interrupts=20and=20retry=20with=20the=20remaining=0A+=09=20*=20= time.=20However,=20frequent=20interruptions=20and=20restarts=20of=20the=20= nanosleep=0A+=09=20*=20calls=20can=20substantially=20lead=20to=20drift=20= in=20the=20time=20when=20the=20sleep=0A+=09=20*=20finally=20completes.=20= To=20deal=20with=20this,=20we=20break=20out=20of=20the=20loop=20whenever=0A= +=09=20*=20the=20current=20time=20is=20past=20the=20expected=20end=20= time=20of=20the=20sleep.=0A+=09=20*/=0A+=0A+=09struct=20timespec=20= delay;=0A+=09struct=20timespec=20remain;=0A+=09instr_time=09end_time;=0A= +=0A+=09INSTR_TIME_SET_CURRENT(end_time);=0A+=09= INSTR_TIME_ADD_MICROSEC(end_time,=20microsec);=0A+=0A+=09delay.tv_sec=20= =3D=20microsec=20/=201000000L;=0A+=09delay.tv_nsec=20=3D=20(microsec=20%=20= 1000000L)=20*=201000;=0A+=0A+=09while=20(nanosleep(&delay,=20&remain)=20= =3D=3D=20-1=20&&=20errno=20=3D=3D=20EINTR)=0A+=09{=0A+=09=09instr_time=09= current_time;=0A+=0A+=09=09INSTR_TIME_SET_CURRENT(current_time);=0A+=0A+=09= =09if=20(INSTR_TIME_IS_GREATER(current_time,=20end_time))=0A+=09=09=09= break;=0A+=0A+=09=09delay=20=3D=20remain;=0A+=09}=0A+}=0A+=0A+=0A=20= #endif=09=09=09=09=09=09=09/*=20defined(FRONTEND)=20||=20!defined(WIN32)=20= */=0A--=20=0A2.39.3=20(Apple=20Git-146)=0A=0A= --Apple-Mail=_04946F70-4E12-4F8E-97EE-BC0186797AEE Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii v9 has some has some minor corrections to the comments. Regards, Sami --Apple-Mail=_04946F70-4E12-4F8E-97EE-BC0186797AEE--