Skip to content

Commit fffc0c8

Browse files
topointon-jumpmmcgee-jump
authored andcommitted
funk, topo, util: make Funk workspace use unpinned pages
1 parent fb8f219 commit fffc0c8

File tree

22 files changed

+293
-33
lines changed

22 files changed

+293
-33
lines changed

.github/workflows/backtest.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,8 @@ jobs:
4343
sudo prlimit --pid=$$ --nofile=1048576
4444
sudo prlimit --pid=$$ --memlock=unlimited
4545
DUMP_DIR=../dump make run-runtime-backtest
46+
47+
- name: fini
48+
if: always()
49+
run: |
50+
sudo $OBJDIR/bin/firedancer-dev configure fini all

contrib/offline-replay/offline_replay.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@
4040
[paths]
4141
identity_key = "{identity_key_path}"
4242
vote_account = "{vote_account_path}"
43+
[hugetlbfs]
44+
mount_path = "/data/firedancer/mnt"

src/app/firedancer-dev/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,13 @@ extern configure_stage_t fd_cfg_stage_kill;
5454
extern configure_stage_t fd_cfg_stage_netns;
5555
extern configure_stage_t fd_cfg_stage_genesis;
5656
extern configure_stage_t fd_cfg_stage_keys;
57+
extern configure_stage_t fd_cfg_stage_normalpage;
5758

5859
configure_stage_t * STAGES[] = {
5960
&fd_cfg_stage_kill,
6061
&fd_cfg_stage_netns,
6162
&fd_cfg_stage_hugetlbfs,
63+
&fd_cfg_stage_normalpage,
6264
&fd_cfg_stage_sysctl,
6365
&fd_cfg_stage_hyperthreads,
6466
&fd_cfg_stage_ethtool_channels,

src/app/firedancer/topology.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,10 @@ setup_topo_funk( fd_topo_t * topo,
112112
ulong wksp_idx = fd_topo_find_wksp( topo, wksp_name );
113113
FD_TEST( wksp_idx!=ULONG_MAX );
114114
fd_topo_wksp_t * wksp = &topo->workspaces[ wksp_idx ];
115-
ulong part_max = fd_wksp_part_max_est( funk_footprint, 1U<<14U );
115+
ulong part_max = fd_wksp_part_max_est( funk_footprint+(heap_size_gib*(1UL<<30)), 1U<<14U );
116116
if( FD_UNLIKELY( !part_max ) ) FD_LOG_ERR(( "fd_wksp_part_max_est(%lu,16KiB) failed", funk_footprint ));
117117
wksp->part_max += part_max;
118+
wksp->is_locked = 0;
118119

119120
return obj;
120121
}

src/app/shared/commands/run/run.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,9 @@ workspace_path( config_t const * config,
472472
case FD_SHMEM_GIGANTIC_PAGE_SZ:
473473
mount_path = config->hugetlbfs.gigantic_page_mount_path;
474474
break;
475+
case FD_SHMEM_NORMAL_PAGE_SZ:
476+
mount_path = config->hugetlbfs.normal_page_mount_path;
477+
break;
475478
default:
476479
FD_LOG_ERR(( "invalid page size %lu", wksp->page_sz ));
477480
}
@@ -511,6 +514,7 @@ warn_unknown_files( config_t const * config,
511514
int known_file = 0;
512515
for( ulong i=0UL; i<config->topo.wksp_cnt; i++ ) {
513516
fd_topo_wksp_t const * wksp = &config->topo.workspaces[ i ];
517+
if( !wksp->is_locked ) continue;
514518

515519
char expected_path[ PATH_MAX ];
516520
workspace_path( config, wksp, expected_path );

src/app/shared/fd_config.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,11 @@ fd_config_fill( fd_config_t * config,
292292
NULL,
293293
"%s/.huge",
294294
config->hugetlbfs.mount_path ) );
295+
FD_TEST( fd_cstr_printf_check( config->hugetlbfs.normal_page_mount_path,
296+
sizeof(config->hugetlbfs.normal_page_mount_path),
297+
NULL,
298+
"%s/.normal",
299+
config->hugetlbfs.mount_path ) );
295300

296301
ulong max_page_sz = fd_cstr_to_shmem_page_sz( config->hugetlbfs.max_page_size );
297302
if( FD_UNLIKELY( max_page_sz!=FD_SHMEM_HUGE_PAGE_SZ && max_page_sz!=FD_SHMEM_GIGANTIC_PAGE_SZ ) ) FD_LOG_ERR(( "[hugetlbfs.max_page_size] must be \"huge\" or \"gigantic\"" ));

src/app/shared/fd_config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ struct fd_config {
253253
struct {
254254
char gigantic_page_mount_path[ PATH_MAX ];
255255
char huge_page_mount_path[ PATH_MAX ];
256+
char normal_page_mount_path[ PATH_MAX ];
256257
char mount_path[ PATH_MAX ];
257258
char max_page_size[ 16 ];
258259
ulong gigantic_page_threshold_mib;

src/app/shared_dev/Local.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ $(call add-objs,commands/configure/kill,fddev_shared)
3030
ifdef FD_HAS_INT128
3131
$(call add-objs,commands/configure/genesis,fddev_shared)
3232
endif
33+
$(call add-objs,commands/configure/normalpage,fddev_shared)
3334

3435
endif
3536
endif
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/* This stage configures the normal pages directory, which is where the
2+
files backing memory-mapped unlocked workspaces should be stored.
3+
4+
The files backing these workspaces are stored in the normal pages
5+
directory configured by this command, and follow the normal workspace
6+
shmem file naming convention. */
7+
8+
#include "../../../shared/commands/configure/configure.h"
9+
10+
#include "../../../platform/fd_file_util.h"
11+
12+
#include <unistd.h>
13+
#include <errno.h>
14+
#include <stdio.h>
15+
#include <stdlib.h> /* strtoul */
16+
#include <dirent.h>
17+
#include <sys/stat.h>
18+
#include <sys/mount.h>
19+
#include <linux/capability.h>
20+
21+
static void
22+
init( config_t const * config ) {
23+
char const * path = config->hugetlbfs.normal_page_mount_path;
24+
25+
FD_LOG_NOTICE(( "RUN: `mkdir -p %s`", path ));
26+
if( FD_UNLIKELY( -1==fd_file_util_mkdir_all( path, config->uid, config->gid ) ) ) {
27+
FD_LOG_ERR(( "could not create normal page directory `%s` (%i-%s)", path, errno, fd_io_strerror( errno ) ));
28+
}
29+
if( FD_UNLIKELY( chown( path, config->uid, config->gid ) ) )
30+
FD_LOG_ERR(( "chown of normal page directory `%s` failed (%i-%s)", path, errno, fd_io_strerror( errno ) ));
31+
if( FD_UNLIKELY( chmod( config->hugetlbfs.normal_page_mount_path, S_IRUSR | S_IWUSR | S_IXUSR ) ) )
32+
FD_LOG_ERR(( "chmod of normal page directory `%s` failed (%i-%s)", config->hugetlbfs.normal_page_mount_path, errno, fd_io_strerror( errno ) ));
33+
}
34+
35+
static int
36+
is_mountpoint( char const * directory ) {
37+
struct stat st;
38+
int result = stat( directory, &st );
39+
if( FD_UNLIKELY( -1==result && errno==ENOENT ) ) return 0;
40+
if( FD_UNLIKELY( -1==result ) ) FD_LOG_ERR(( "failed to stat `%s` (%i-%s)", directory, errno, fd_io_strerror( errno ) ));
41+
42+
char parent_path[ PATH_MAX+4UL ];
43+
FD_TEST( fd_cstr_printf_check( parent_path, sizeof(parent_path), NULL, "%s/..", directory ) );
44+
45+
struct stat st_parent;
46+
result = stat( parent_path, &st_parent );
47+
if( FD_UNLIKELY( -1==result && errno==ENOENT ) ) return 0;
48+
if( FD_UNLIKELY( -1==result ) ) FD_LOG_ERR(( "failed to stat `%s` (%i-%s)", parent_path, errno, fd_io_strerror( errno ) ));
49+
50+
return st_parent.st_dev!=st.st_dev;
51+
}
52+
53+
static void
54+
fini( config_t const * config,
55+
int pre_init ) {
56+
(void)pre_init;
57+
char const * path = config->hugetlbfs.normal_page_mount_path;
58+
59+
/* fd_shmem_cfg mounts a tmpfs filesystem onto the .normal directory
60+
sometimes, which is the expected way to manage normal pages, but
61+
not what is done by firedancer-dev to support a special temporary
62+
funk use case where normal pages are backed by disk. To prevent
63+
fighting with the other script, we unmount the normal pages if they
64+
have been mounted. */
65+
66+
if( FD_UNLIKELY( is_mountpoint( path ) ) ) {
67+
FD_LOG_NOTICE(( "RUN: `umount %s`", path ));
68+
if( FD_UNLIKELY( -1==umount( path ) && errno!=EINVAL && errno!=ENOENT ) )
69+
FD_LOG_ERR(( "error unmounting normal pages directory at `%s` (%i-%s)", path, errno, fd_io_strerror( errno ) ));
70+
}
71+
72+
FD_LOG_NOTICE(( "RUN: `rm -rf %s`", path ));
73+
if( FD_UNLIKELY( -1==fd_file_util_rmtree( path, 1 ) ) ) FD_LOG_ERR(( "error removing normal pages directory at `%s` (%i-%s)", path, errno, fd_io_strerror( errno ) ));
74+
}
75+
76+
77+
static configure_result_t
78+
check( config_t const * config ) {
79+
char const * path = config->hugetlbfs.normal_page_mount_path;
80+
81+
struct stat st;
82+
int result = stat( path, &st );
83+
if( FD_UNLIKELY( result && errno!=ENOENT ) )
84+
PARTIALLY_CONFIGURED( "failed to stat `%s` (%i-%s)", path, errno, fd_io_strerror( errno ) );
85+
86+
if( FD_UNLIKELY( is_mountpoint( path ) ) )
87+
PARTIALLY_CONFIGURED( "normal pages directory `%s` is a mountpoint", path );
88+
89+
if( FD_UNLIKELY( result ) )
90+
NOT_CONFIGURED( "normal pages directory `%s` does not exist", path );
91+
92+
CHECK( check_dir( path, config->uid, config->gid, S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR ) );
93+
94+
CONFIGURE_OK();
95+
}
96+
97+
configure_stage_t fd_cfg_stage_normalpage = {
98+
.name = "normalpage",
99+
.always_recreate = 0,
100+
.init = init,
101+
.fini = fini,
102+
.check = check,
103+
};

src/disco/topo/fd_topo.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fd_topo_join_workspace( fd_topo_t * topo,
1717
char name[ PATH_MAX ];
1818
FD_TEST( fd_cstr_printf_check( name, PATH_MAX, NULL, "%s_%s.wksp", topo->app_name, wksp->name ) );
1919

20-
wksp->wksp = fd_wksp_join( fd_shmem_join( name, mode, NULL, NULL, NULL ) );
20+
wksp->wksp = fd_wksp_join( fd_shmem_join( name, mode, NULL, NULL, NULL, wksp->is_locked ) );
2121
if( FD_UNLIKELY( !wksp->wksp ) ) FD_LOG_ERR(( "fd_wksp_join failed" ));
2222
}
2323

@@ -82,15 +82,17 @@ fd_topo_create_workspace( fd_topo_t * topo,
8282
ulong sub_cpu_idx [ 1 ] = { fd_shmem_cpu_idx( wksp->numa_idx ) };
8383

8484
int err;
85-
if( FD_UNLIKELY( update_existing ) ) {
85+
if( FD_UNLIKELY( !wksp->is_locked ) ) {
86+
err = fd_shmem_create_multi_unlocked( name, wksp->page_sz, wksp->page_cnt, S_IRUSR | S_IWUSR ); /* logs details */
87+
} else if( FD_UNLIKELY( update_existing ) ) {
8688
err = fd_shmem_update_multi( name, wksp->page_sz, 1, sub_page_cnt, sub_cpu_idx, S_IRUSR | S_IWUSR ); /* logs details */
8789
} else {
8890
err = fd_shmem_create_multi( name, wksp->page_sz, 1, sub_page_cnt, sub_cpu_idx, S_IRUSR | S_IWUSR ); /* logs details */
8991
}
9092
if( FD_UNLIKELY( err && errno==ENOMEM ) ) return -1;
9193
else if( FD_UNLIKELY( err ) ) FD_LOG_ERR(( "fd_shmem_create_multi failed" ));
9294

93-
void * shmem = fd_shmem_join( name, FD_SHMEM_JOIN_MODE_READ_WRITE, NULL, NULL, NULL ); /* logs details */
95+
void * shmem = fd_shmem_join( name, FD_SHMEM_JOIN_MODE_READ_WRITE, NULL, NULL, NULL, wksp->is_locked ); /* logs details */
9496

9597
void * wkspmem = fd_wksp_new( shmem, name, 0U, wksp->part_max, wksp->total_footprint ); /* logs details */
9698
if( FD_UNLIKELY( !wkspmem ) ) FD_LOG_ERR(( "fd_wksp_new failed" ));
@@ -223,6 +225,8 @@ fd_topo_mlock_max_tile1( fd_topo_t const * topo,
223225
ulong tile_mem = 0UL;
224226

225227
for( ulong i=0UL; i<topo->wksp_cnt; i++ ) {
228+
if( FD_UNLIKELY( !topo->workspaces[ i ].is_locked ) ) continue;
229+
226230
if( FD_UNLIKELY( -1!=tile_needs_wksp( topo, tile, i ) ) )
227231
tile_mem += topo->workspaces[ i ].page_cnt * topo->workspaces[ i ].page_sz;
228232
}
@@ -296,6 +300,7 @@ FD_FN_PURE ulong
296300
fd_topo_mlock( fd_topo_t const * topo ) {
297301
ulong result = 0UL;
298302
for( ulong i=0UL; i<topo->wksp_cnt; i++ ) {
303+
if( FD_UNLIKELY( !topo->workspaces[ i ].is_locked ) ) continue;
299304
result += topo->workspaces[ i ].page_cnt * topo->workspaces[ i ].page_sz;
300305
}
301306
return result;
@@ -385,7 +390,7 @@ fd_topo_print_log( int stdout,
385390

386391
char size[ 24 ];
387392
fd_topo_mem_sz_string( wksp->page_sz * wksp->page_cnt, size );
388-
PRINT( " %2lu (%7s): %12s page_cnt=%3lu page_sz=%-8s numa_idx=%-2lu footprint=%10lu loose=%lu\n", i, size, wksp->name, wksp->page_cnt, fd_shmem_page_sz_to_cstr( wksp->page_sz ), wksp->numa_idx, wksp->known_footprint, wksp->total_footprint - wksp->known_footprint );
393+
PRINT( " %2lu (%7s): %12s page_cnt=%3lu page_sz=%-8s numa_idx=%-2lu footprint=%10lu loose=%10lu is_locked=%d\n", i, size, wksp->name, wksp->page_cnt, fd_shmem_page_sz_to_cstr( wksp->page_sz ), wksp->numa_idx, wksp->known_footprint, wksp->total_footprint - wksp->known_footprint, wksp->is_locked );
389394
}
390395

391396
PRINT( "\nOBJECTS\n" );

0 commit comments

Comments
 (0)