pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Sami Imseih <samimseih@gmail.com>
To: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Cc: Nathan Bossart <nathandbossart@gmail.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Robert Haas <robertmhaas@gmail.com>
Cc: pgsql-hackers@postgresql.org <pgsql-hackers@postgresql.org>
Subject: Re: Restart pg_usleep when interrupted
Date: Sat, 10 Aug 2024 08:27:56 -0500
Message-ID: <526E6A51-FE30-47C4-85C8-F2D18877607F@gmail.com> (raw)
In-Reply-To: <9D64CD7C-CA24-4FF4-9421-3A1A63F87DAC@gmail.com>
References: <33287B8A-3CA0-494F-87D3-C9D85C6468FE@gmail.com>
	<ZqiIxptiTDqRTxDx@ip-10-97-1-34.eu-west-3.compute.internal>
	<19E276C9-2C2B-435A-B275-8FA22222AEB8@gmail.com>
	<ZrHmE3zUSNXzoHCU@ip-10-97-1-34.eu-west-3.compute.internal>
	<50E1D6AD-3E51-4E45-95A7-7B20DC037DCB@gmail.com>
	<ZrMNlUd1h09nMrNQ@ip-10-97-1-34.eu-west-3.compute.internal>
	<ZrOGi8RZTSHxTq9W@nathan>
	<D1558DDA-CEED-4CED-B3ED-C425D996FCA3@gmail.com>
	<ZrUkELsJs3CI896W@nathan>
	<ZrZoG3uZmUOgbHOv@nathan>
	<ZrZtdpTN7nLI4L8h@ip-10-97-1-34.eu-west-3.compute.internal>
	<9D64CD7C-CA24-4FF4-9421-3A1A63F87DAC@gmail.com>



v9 has some has some minor corrections to the comments.


Regards,

Sami

Attachments:

v9-0001-vaccum_delay-with-absolute-time-nanosleep.patch (application/octet-stream, 4.4K) download From 0fecbea4fc4c49a9f8657005095e1cab081451be Mon Sep 17 00:00:00 2001 From: Sami Imseih <simseih@amazon.com> Date: Fri, 9 Aug 2024 18:02:12 -0500 Subject: [PATCH v9 1/1] vaccum_delay with absolute time nanosleep --- src/backend/commands/vacuum.c | 2 +- src/backend/port/win32/signal.c | 10 +++++++ src/include/port.h | 1 + src/include/portability/instr_time.h | 10 +++++++ src/port/pgsleep.c | 43 ++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c index 48f8eab202..43333c4698 100644 --- a/src/backend/commands/vacuum.c +++ b/src/backend/commands/vacuum.c @@ -2384,7 +2384,7 @@ vacuum_delay_point(void) msec = vacuum_cost_delay * 4; pgstat_report_wait_start(WAIT_EVENT_VACUUM_DELAY); - pg_usleep(msec * 1000); + pg_usleep_non_interruptible(msec * 1000); pgstat_report_wait_end(); /* diff --git a/src/backend/port/win32/signal.c b/src/backend/port/win32/signal.c index 285cb611b4..edcb181215 100644 --- a/src/backend/port/win32/signal.c +++ b/src/backend/port/win32/signal.c @@ -73,6 +73,16 @@ pg_usleep(long microsec) } } +/* + * pg_usleep_non_interruptible --- delay the specified number of microseconds. + * + * Unlike pg_usleep, this relies on a non-interruptible sleep. + */ +void +pg_usleep_non_interruptible(long microsec) +{ + SleepEx((microsec < 500 ? 1 : (microsec + 500) / 1000), FALSE); +} /* Initialization */ void diff --git a/src/include/port.h b/src/include/port.h index c740005267..c8ff23e5ee 100644 --- a/src/include/port.h +++ b/src/include/port.h @@ -162,6 +162,7 @@ extern int pg_disable_aslr(void); /* Portable delay handling */ extern void pg_usleep(long microsec); +extern void pg_usleep_non_interruptible(long microsec); /* Portable SQL-like case-independent comparisons and conversions */ extern int pg_strcasecmp(const char *s1, const char *s2); diff --git a/src/include/portability/instr_time.h b/src/include/portability/instr_time.h index e66ecf34cd..6e4b0f1b17 100644 --- a/src/include/portability/instr_time.h +++ b/src/include/portability/instr_time.h @@ -36,6 +36,10 @@ * * INSTR_TIME_GET_NANOSEC(t) convert t to int64 (in nanoseconds) * + * INSTR_TIME_ADD_MICROSEC(x,t) add t (in microseconds) to x + * + * INSTR_TIME_IS_GREATER(x,y) is x greater than y? + * * Note that INSTR_TIME_SUBTRACT and INSTR_TIME_ACCUM_DIFF convert * absolute times to intervals. The INSTR_TIME_GET_xxx operations are * only useful on intervals. @@ -194,4 +198,10 @@ GetTimerFrequency(void) #define INSTR_TIME_GET_MICROSEC(t) \ (INSTR_TIME_GET_NANOSEC(t) / NS_PER_US) +#define INSTR_TIME_ADD_MICROSEC(x,t) \ + ((x).ticks += (t) * NS_PER_US) + +#define INSTR_TIME_IS_GREATER(x,y) \ + ((x).ticks > (y).ticks) + #endif /* INSTR_TIME_H */ diff --git a/src/port/pgsleep.c b/src/port/pgsleep.c index 1284458bfc..9525156abc 100644 --- a/src/port/pgsleep.c +++ b/src/port/pgsleep.c @@ -14,6 +14,8 @@ #include <time.h> +#include "portability/instr_time.h" + /* * In a Windows backend, we don't use this implementation, but rather * the signal-aware version in src/backend/port/win32/signal.c. @@ -54,4 +56,45 @@ pg_usleep(long microsec) } } +/* + * pg_usleep_non_interruptible --- delay the specified number of microseconds. + * + * Unlike pg_usleep, This function continues the delay in case of an + * interrupt. + */ +void +pg_usleep_non_interruptible(long microsec) +{ + /* + * We allow nanosleep to handle interrupts and retry with the remaining + * time. However, frequent interruptions and restarts of the nanosleep + * calls can substantially lead to drift in the time when the sleep + * finally completes. To deal with this, we break out of the loop whenever + * the current time is past the expected end time of the sleep. + */ + + struct timespec delay; + struct timespec remain; + instr_time end_time; + + INSTR_TIME_SET_CURRENT(end_time); + INSTR_TIME_ADD_MICROSEC(end_time, microsec); + + delay.tv_sec = microsec / 1000000L; + delay.tv_nsec = (microsec % 1000000L) * 1000; + + while (nanosleep(&delay, &remain) == -1 && errno == EINTR) + { + instr_time current_time; + + INSTR_TIME_SET_CURRENT(current_time); + + if (INSTR_TIME_IS_GREATER(current_time, end_time)) + break; + + delay = remain; + } +} + + #endif /* defined(FRONTEND) || !defined(WIN32) */ -- 2.39.3 (Apple Git-146) =

view thread (73+ messages)  latest in thread

Message-ID: <526E6A51-FE30-47C4-85C8-F2D18877607F@gmail.com>
Permalink:  ../526E6A51-FE30-47C4-85C8-F2D18877607F@gmail.com/
Also on:    postgresql.org/message-id/526E6A51-FE30-47C4-85C8-F2D18877607F@gmail.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: samimseih@gmail.com, bertranddrouvot.pg@gmail.com, nathandbossart@gmail.com, tgl@sss.pgh.pa.us, robertmhaas@gmail.com
  Subject: Re: Restart pg_usleep when interrupted
  In-Reply-To: <526E6A51-FE30-47C4-85C8-F2D18877607F@gmail.com>
  References: <33287B8A-3CA0-494F-87D3-C9D85C6468FE@gmail.com> <ZqiIxptiTDqRTxDx@ip-10-97-1-34.eu-west-3.compute.internal> <19E276C9-2C2B-435A-B275-8FA22222AEB8@gmail.com> <ZrHmE3zUSNXzoHCU@ip-10-97-1-34.eu-west-3.compute.internal> <50E1D6AD-3E51-4E45-95A7-7B20DC037DCB@gmail.com> <ZrMNlUd1h09nMrNQ@ip-10-97-1-34.eu-west-3.compute.internal> <ZrOGi8RZTSHxTq9W@nathan> <D1558DDA-CEED-4CED-B3ED-C425D996FCA3@gmail.com> <ZrUkELsJs3CI896W@nathan> <ZrZoG3uZmUOgbHOv@nathan> <ZrZtdpTN7nLI4L8h@ip-10-97-1-34.eu-west-3.compute.internal> <9D64CD7C-CA24-4FF4-9421-3A1A63F87DAC@gmail.com> <526E6A51-FE30-47C4-85C8-F2D18877607F@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox