Skip to content

Commit 3887d0c

Browse files
committed
Cleanup of pg_numa.c
This moves/renames some of the functions defined in pg_numa.c: * pg_numa_get_pagesize() is renamed to pg_get_shmem_pagesize(), and moved to src/backend/storage/ipc/shmem.c. The new name better reflects that the page size is not related to NUMA, and it's specifically about the page size used for the main shared memory segment. * move pg_numa_available() to src/backend/storage/ipc/shmem.c, i.e. into the backend (which more appropriate for functions callable from SQL). While at it, improve the comment to explain what page size it returns. * remove unnecessary includes from src/port/pg_numa.c, adding unnecessary dependencies (src/port should be suitable for frontent). These were either leftovers or unnecessary thanks to the other changes in this commit. This eliminates unnecessary dependencies on backend symbols, which we don't want in src/port. Reported-by: Kirill Reshke <reshkekirill@gmail.com> Reviewed-by: Andres Freund <andres@anarazel.de> https://postgr.es/m/CALdSSPi5fj0a7UG7Fmw2cUD1uWuckU_e8dJ+6x-bJEokcSXzqA@mail.gmail.com
1 parent e2665ef commit 3887d0c

File tree

5 files changed

+43
-46
lines changed

5 files changed

+43
-46
lines changed

contrib/pg_buffercache/pg_buffercache_pages.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ pg_buffercache_numa_pages(PG_FUNCTION_ARGS)
343343
* This information is needed before calling move_pages() for NUMA
344344
* node id inquiry.
345345
*/
346-
os_page_size = pg_numa_get_pagesize();
346+
os_page_size = pg_get_shmem_pagesize();
347347

348348
/*
349349
* The pages and block size is expected to be 2^k, so one divides the

src/backend/storage/ipc/shmem.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ static HTAB *ShmemIndex = NULL; /* primary index hashtable for shmem */
9393
/* To get reliable results for NUMA inquiry we need to "touch pages" once */
9494
static bool firstNumaTouch = true;
9595

96+
Datum pg_numa_available(PG_FUNCTION_ARGS);
97+
9698
/*
9799
* InitShmemAccess() --- set up basic pointers to shared memory.
98100
*/
@@ -615,7 +617,7 @@ pg_get_shmem_allocations_numa(PG_FUNCTION_ARGS)
615617
* This information is needed before calling move_pages() for NUMA memory
616618
* node inquiry.
617619
*/
618-
os_page_size = pg_numa_get_pagesize();
620+
os_page_size = pg_get_shmem_pagesize();
619621

620622
/*
621623
* Allocate memory for page pointers and status based on total shared
@@ -727,3 +729,39 @@ pg_get_shmem_allocations_numa(PG_FUNCTION_ARGS)
727729

728730
return (Datum) 0;
729731
}
732+
733+
/*
734+
* Determine the memory page size used for the shared memory segment.
735+
*
736+
* If the shared segment was allocated using huge pages, returns the size of
737+
* a huge page. Otherwise returns the size of regular memory page.
738+
*
739+
* This should be used only after the server is started.
740+
*/
741+
Size
742+
pg_get_shmem_pagesize(void)
743+
{
744+
Size os_page_size;
745+
#ifdef WIN32
746+
SYSTEM_INFO sysinfo;
747+
748+
GetSystemInfo(&sysinfo);
749+
os_page_size = sysinfo.dwPageSize;
750+
#else
751+
os_page_size = sysconf(_SC_PAGESIZE);
752+
#endif
753+
754+
Assert(IsUnderPostmaster);
755+
Assert(huge_pages_status != HUGE_PAGES_UNKNOWN);
756+
757+
if (huge_pages_status == HUGE_PAGES_ON)
758+
GetHugePageSize(&os_page_size, NULL);
759+
760+
return os_page_size;
761+
}
762+
763+
Datum
764+
pg_numa_available(PG_FUNCTION_ARGS)
765+
{
766+
PG_RETURN_BOOL(pg_numa_init() != -1);
767+
}

src/include/port/pg_numa.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@
1414
#ifndef PG_NUMA_H
1515
#define PG_NUMA_H
1616

17-
#include "fmgr.h"
18-
1917
extern PGDLLIMPORT int pg_numa_init(void);
2018
extern PGDLLIMPORT int pg_numa_query_pages(int pid, unsigned long count, void **pages, int *status);
2119
extern PGDLLIMPORT int pg_numa_get_max_node(void);
22-
extern PGDLLIMPORT Size pg_numa_get_pagesize(void);
2320

2421
#ifdef USE_LIBNUMA
2522

src/include/storage/shmem.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ extern void *ShmemInitStruct(const char *name, Size size, bool *foundPtr);
4141
extern Size add_size(Size s1, Size s2);
4242
extern Size mul_size(Size s1, Size s2);
4343

44+
extern PGDLLIMPORT Size pg_get_shmem_pagesize(void);
45+
4446
/* ipci.c */
4547
extern void RequestAddinShmemSpace(Size size);
4648

src/port/pg_numa.c

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,10 @@
1313
*-------------------------------------------------------------------------
1414
*/
1515

16-
#include "postgres.h"
16+
#include "c.h"
1717
#include <unistd.h>
1818

19-
#ifdef WIN32
20-
#include <windows.h>
21-
#endif
22-
23-
#include "fmgr.h"
24-
#include "miscadmin.h"
2519
#include "port/pg_numa.h"
26-
#include "storage/pg_shmem.h"
2720

2821
/*
2922
* At this point we provide support only for Linux thanks to libnuma, but in
@@ -36,8 +29,6 @@
3629
#include <numa.h>
3730
#include <numaif.h>
3831

39-
Datum pg_numa_available(PG_FUNCTION_ARGS);
40-
4132
/* libnuma requires initialization as per numa(3) on Linux */
4233
int
4334
pg_numa_init(void)
@@ -66,8 +57,6 @@ pg_numa_get_max_node(void)
6657

6758
#else
6859

69-
Datum pg_numa_available(PG_FUNCTION_ARGS);
70-
7160
/* Empty wrappers */
7261
int
7362
pg_numa_init(void)
@@ -89,32 +78,3 @@ pg_numa_get_max_node(void)
8978
}
9079

9180
#endif
92-
93-
Datum
94-
pg_numa_available(PG_FUNCTION_ARGS)
95-
{
96-
PG_RETURN_BOOL(pg_numa_init() != -1);
97-
}
98-
99-
/* This should be used only after the server is started */
100-
Size
101-
pg_numa_get_pagesize(void)
102-
{
103-
Size os_page_size;
104-
#ifdef WIN32
105-
SYSTEM_INFO sysinfo;
106-
107-
GetSystemInfo(&sysinfo);
108-
os_page_size = sysinfo.dwPageSize;
109-
#else
110-
os_page_size = sysconf(_SC_PAGESIZE);
111-
#endif
112-
113-
Assert(IsUnderPostmaster);
114-
Assert(huge_pages_status != HUGE_PAGES_UNKNOWN);
115-
116-
if (huge_pages_status == HUGE_PAGES_ON)
117-
GetHugePageSize(&os_page_size, NULL);
118-
119-
return os_page_size;
120-
}

0 commit comments

Comments
 (0)