PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pg_numa.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * pg_numa.c
4 * Basic NUMA portability routines
5 *
6 *
7 * Copyright (c) 2025, PostgreSQL Global Development Group
8 *
9 *
10 * IDENTIFICATION
11 * src/port/pg_numa.c
12 *
13 *-------------------------------------------------------------------------
14 */
15
16#include "c.h"
17#include <unistd.h>
18
19#include "port/pg_numa.h"
20
21/*
22 * At this point we provide support only for Linux thanks to libnuma, but in
23 * future support for other platforms e.g. Win32 or FreeBSD might be possible
24 * too. For Win32 NUMA APIs see
25 * https://learn.microsoft.com/en-us/windows/win32/procthread/numa-support
26 */
27#ifdef USE_LIBNUMA
28
29#include <numa.h>
30#include <numaif.h>
31
32/* libnuma requires initialization as per numa(3) on Linux */
33int
34pg_numa_init(void)
35{
36 int r = numa_available();
37
38 return r;
39}
40
41/*
42 * We use move_pages(2) syscall here - instead of get_mempolicy(2) - as the
43 * first one allows us to batch and query about many memory pages in one single
44 * giant system call that is way faster.
45 */
46int
47pg_numa_query_pages(int pid, unsigned long count, void **pages, int *status)
48{
49 return numa_move_pages(pid, count, pages, NULL, status, 0);
50}
51
52int
54{
55 return numa_max_node();
56}
57
58#else
59
60/* Empty wrappers */
61int
63{
64 /* We state that NUMA is not available */
65 return -1;
66}
67
68int
69pg_numa_query_pages(int pid, unsigned long count, void **pages, int *status)
70{
71 return 0;
72}
73
74int
76{
77 return 0;
78}
79
80#endif
int pg_numa_query_pages(int pid, unsigned long count, void **pages, int *status)
Definition: pg_numa.c:69
int pg_numa_init(void)
Definition: pg_numa.c:62
int pg_numa_get_max_node(void)
Definition: pg_numa.c:75