From e77dbecee4330272964b7ea2838a4d9501d804b4 Mon Sep 17 00:00:00 2001 From: Thomas Munro Date: Tue, 21 Jan 2020 12:54:11 +1300 Subject: [PATCH 3/5] Introduce a reusable WaitEventSet for the stats collector. Avoid setup and teardown for every wait. --- src/backend/postmaster/pgstat.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c index 7169509a79..953bc9b6ae 100644 --- a/src/backend/postmaster/pgstat.c +++ b/src/backend/postmaster/pgstat.c @@ -4426,6 +4426,8 @@ PgstatCollectorMain(int argc, char *argv[]) int len; PgStat_Msg msg; int wr; + WaitEvent event; + WaitEventSet *wes; /* * Ignore all signals usually bound to some action in the postmaster, @@ -4455,6 +4457,12 @@ PgstatCollectorMain(int argc, char *argv[]) pgStatRunningInCollector = true; pgStatDBHash = pgstat_read_statsfiles(InvalidOid, true, true); + /* Prepare to wait for our latch or data in our socket. */ + wes = CreateWaitEventSet(CurrentMemoryContext, 3); + AddWaitEventToSet(wes, WL_LATCH_SET, PGINVALID_SOCKET, MyLatch, NULL); + AddWaitEventToSet(wes, WL_POSTMASTER_DEATH, PGINVALID_SOCKET, NULL, NULL); + AddWaitEventToSet(wes, WL_SOCKET_READABLE, pgStatSock, NULL, NULL); + /* * Loop to process messages until we get SIGQUIT or detect ungraceful * death of our parent postmaster. @@ -4633,10 +4641,7 @@ PgstatCollectorMain(int argc, char *argv[]) /* Sleep until there's something to do */ #ifndef WIN32 - wr = WaitLatchOrSocket(MyLatch, - WL_LATCH_SET | WL_POSTMASTER_DEATH | WL_SOCKET_READABLE, - pgStatSock, -1L, - WAIT_EVENT_PGSTAT_MAIN); + wr = WaitEventSetWait(wes, -1L, &event, 1, WAIT_EVENT_PGSTAT_MAIN); #else /* @@ -4649,18 +4654,15 @@ PgstatCollectorMain(int argc, char *argv[]) * to not provoke "using stale statistics" complaints from * backend_read_statsfile. */ - wr = WaitLatchOrSocket(MyLatch, - WL_LATCH_SET | WL_POSTMASTER_DEATH | WL_SOCKET_READABLE | WL_TIMEOUT, - pgStatSock, - 2 * 1000L /* msec */ , - WAIT_EVENT_PGSTAT_MAIN); + wr = WaitEventSetWait(wes, 2 * 1000L /* msec */, &event, 1, + WAIT_EVENT_PGSTAT_MAIN); #endif /* * Emergency bailout if postmaster has died. This is to avoid the * necessity for manual cleanup of all postmaster children. */ - if (wr & WL_POSTMASTER_DEATH) + if (wr == 1 && event.events == WL_POSTMASTER_DEATH) break; } /* end of outer loop */ @@ -4669,6 +4671,8 @@ PgstatCollectorMain(int argc, char *argv[]) */ pgstat_write_statsfiles(true, true); + FreeWaitEventSet(wes); + exit(0); } -- 2.23.0