Skip to content

Commit

Permalink
tile: fd_cpuset_t belt-sanding pass
Browse files Browse the repository at this point in the history
- Move fd_cpuset.{h,c} into fd_tile_private.h, fd_tile.c
- Remove fd_cpuset_zero (replaced by fd_cpuset_null)
- Support fd_cpuset_t non-POSIX and non-threads env
  - Use ulong instead of pid_t
- Rename fd_sched_{get,set}affinity to fd_cpuset_{get,set}affinity
  - Document pid==0 special case
- Remove FD_CPUSET_MAX, replace with FD_TILE_MAX
  • Loading branch information
riptl authored and ripatel-fd committed Jan 31, 2024
1 parent 0bb91a0 commit 37ba527
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 167 deletions.
27 changes: 13 additions & 14 deletions src/app/fdctl/run/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <linux/capability.h>
#include <linux/unistd.h>

#include "../../../util/tile/fd_cpuset.h"
#include "../../../util/tile/fd_tile_private.h"

#define NAME "run"

Expand Down Expand Up @@ -118,18 +118,17 @@ execve_tile( fd_topo_tile_t * tile,
int pipefd ) {
FD_CPUSET_DECL( cpu_set );
if( FD_LIKELY( cpu_idx<65535UL ) ) {
/* set the thread affinity before we clone the new process to ensure
kernel first touch happens on the desired thread. */
fd_cpuset_zero( cpu_set );
fd_cpuset_insert( cpu_set, cpu_idx );
if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, -19 ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
/* set the thread affinity before we clone the new process to ensure
kernel first touch happens on the desired thread. */
fd_cpuset_insert( cpu_set, cpu_idx );
if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, -19 ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
} else {
fd_memcpy( cpu_set, floating_cpu_set, fd_cpuset_footprint() );
if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, floating_priority ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
fd_memcpy( cpu_set, floating_cpu_set, fd_cpuset_footprint() );
if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, floating_priority ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
}

if( FD_UNLIKELY( fd_sched_setaffinity( 0, cpu_set ) ) ) {
FD_LOG_WARNING(( "unable to pin tile to cpu with fd_sched_setaffinity (%i-%s). "
if( FD_UNLIKELY( fd_cpuset_setaffinity( 0, cpu_set ) ) ) {
FD_LOG_WARNING(( "unable to pin tile to cpu with fd_cpuset_setaffinity (%i-%s). "
"Unable to set the thread affinity for tile %lu on cpu %hu. Attempting to "
"continue without explicitly specifying this cpu's thread affinity but it "
"is likely this thread group's performance and stability are compromised "
Expand Down Expand Up @@ -199,8 +198,8 @@ main_pid_namespace( void * _args ) {

/* Save the current affinity, it will be restored after creating any child tiles */
FD_CPUSET_DECL( floating_cpu_set );
if( FD_UNLIKELY( fd_sched_getaffinity( 0, floating_cpu_set ) ) )
FD_LOG_ERR(( "sched_getaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));
if( FD_UNLIKELY( fd_cpuset_getaffinity( 0, floating_cpu_set ) ) )
FD_LOG_ERR(( "fd_cpuset_getaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));

pid_t child_pids[ FD_TOPO_MAX_TILES+1 ];
char child_names[ FD_TOPO_MAX_TILES+1 ][ 32 ];
Expand Down Expand Up @@ -246,8 +245,8 @@ main_pid_namespace( void * _args ) {
}

if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, save_priority ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
if( FD_UNLIKELY( fd_sched_setaffinity( 0, floating_cpu_set ) ) )
FD_LOG_ERR(( "sched_setaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));
if( FD_UNLIKELY( fd_cpuset_setaffinity( 0, floating_cpu_set ) ) )
FD_LOG_ERR(( "fd_cpuset_setaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));

if( FD_UNLIKELY( close( config_memfd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
if( FD_UNLIKELY( close( config->log.lock_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
Expand Down
7 changes: 4 additions & 3 deletions src/app/fdctl/run/run1.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#define _GNU_SOURCE
#include "run.h"

#include "../../../util/tile/fd_cpuset.h"
#include "../../../util/tile/fd_tile_private.h"
#include <sched.h>
#include <sys/wait.h>

Expand Down Expand Up @@ -206,9 +206,10 @@ run1_cmd_fn( args_t * args,
if( FD_UNLIKELY( close( config->log.lock_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));

FD_CPUSET_DECL( affinity );
if( FD_UNLIKELY( -1==fd_sched_getaffinity( 0, affinity ) ) ) FD_LOG_ERR(( "sched_getaffinity() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
if( FD_UNLIKELY( -1==fd_cpuset_getaffinity( 0, affinity ) ) )
FD_LOG_ERR(( "fd_cpuset_getaffinity() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
ulong cpu_idx = fd_cpuset_first( affinity );
cpu_idx = fd_ulong_if( cpu_idx<FD_CPUSET_MAX, cpu_idx, ULONG_MAX );
cpu_idx = fd_ulong_if( cpu_idx<FD_TILE_MAX, cpu_idx, ULONG_MAX );

if( FD_UNLIKELY( cpu_idx==ULONG_MAX ) ) {
FD_LOG_WARNING(( "unable to find a CPU to run on, using CPU 0" ));
Expand Down
28 changes: 14 additions & 14 deletions src/app/fdctl/run/run_solana.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "run.h"

#include "../../../util/net/fd_ip4.h"
#include "../../../util/tile/fd_cpuset.h"
#include "../../../util/tile/fd_tile_private.h"

#include <sched.h>
#include <pthread.h>
Expand All @@ -23,8 +23,8 @@ static void
clone_labs_memory_space_tiles( config_t * const config ) {
/* Save the current affinity, it will be restored after creating any child tiles */
FD_CPUSET_DECL( floating_cpu_set );
if( FD_UNLIKELY( fd_sched_getaffinity( 0, floating_cpu_set ) ) )
FD_LOG_ERR(( "sched_getaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));
if( FD_UNLIKELY( fd_cpuset_getaffinity( 0, floating_cpu_set ) ) )
FD_LOG_ERR(( "fd_cpuset_getaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));

errno = 0;
int floating_priority = getpriority( PRIO_PROCESS, 0 );
Expand Down Expand Up @@ -67,18 +67,18 @@ clone_labs_memory_space_tiles( config_t * const config ) {

FD_CPUSET_DECL( cpu_set );
if( FD_LIKELY( cpu_idx<65535UL ) ) {
/* set the thread affinity before we clone the new process to ensure
kernel first touch happens on the desired thread. */
fd_cpuset_zero( cpu_set );
fd_cpuset_insert( cpu_set, cpu_idx );
if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, -19 ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
/* set the thread affinity before we clone the new process to ensure
kernel first touch happens on the desired thread. */
fd_cpuset_null( cpu_set );
fd_cpuset_insert( cpu_set, cpu_idx );
if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, -19 ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
} else {
fd_memcpy( cpu_set, floating_cpu_set, fd_cpuset_footprint() );
if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, floating_priority ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
fd_memcpy( cpu_set, floating_cpu_set, fd_cpuset_footprint() );
if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, floating_priority ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
}

if( FD_UNLIKELY( fd_sched_setaffinity( 0, cpu_set ) ) ) {
FD_LOG_WARNING(( "unable to pin tile to cpu with sched_setaffinity (%i-%s). "
if( FD_UNLIKELY( fd_cpuset_setaffinity( 0, cpu_set ) ) ) {
FD_LOG_WARNING(( "unable to pin tile to cpu with fd_cpuset_setaffinity (%i-%s). "
"Unable to set the thread affinity for tile %lu on cpu %hu. Attempting to "
"continue without explicitly specifying this cpu's thread affinity but it "
"is likely this thread group's performance and stability are compromised "
Expand Down Expand Up @@ -117,8 +117,8 @@ clone_labs_memory_space_tiles( config_t * const config ) {
}

/* Restore the original affinity */
if( FD_UNLIKELY( fd_sched_setaffinity( 0, floating_cpu_set ) ) )
FD_LOG_ERR(( "fd_sched_setaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));
if( FD_UNLIKELY( fd_cpuset_setaffinity( 0, floating_cpu_set ) ) )
FD_LOG_ERR(( "fd_cpuset_setaffinity failed (%i-%s)", errno, fd_io_strerror( errno ) ));
if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, floating_priority ) ) )
FD_LOG_ERR(( "fd_setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
}
Expand Down
6 changes: 3 additions & 3 deletions src/app/fddev/bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <sys/socket.h>
#include <arpa/inet.h>

#include "../../util/tile/fd_cpuset.h"
#include "../../util/tile/fd_tile_private.h"

void
bench_cmd_perm( args_t * args,
Expand Down Expand Up @@ -138,9 +138,9 @@ main_bencher( void * _args ) {
if( FD_UNLIKELY( -1==setpriority( PRIO_PROCESS, 0, -19 ) ) ) FD_LOG_ERR(( "setpriority() failed (%i-%s)", errno, fd_io_strerror( errno ) ));

FD_CPUSET_DECL( cpu_set );
fd_cpuset_zero( cpu_set );
fd_cpuset_insert( cpu_set, bench_cpu_idx( config ) );
if( FD_UNLIKELY( -1==fd_sched_setaffinity( 0, cpu_set ) ) ) FD_LOG_ERR(( "sched_setaffinity() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
if( FD_UNLIKELY( -1==fd_cpuset_setaffinity( 0, cpu_set ) ) )
FD_LOG_ERR(( "fd_cpuset_setaffinity() failed (%i-%s)", errno, fd_io_strerror( errno ) ));

int conn = socket( AF_INET, SOCK_DGRAM, 0 );
if( FD_UNLIKELY( -1==conn ) ) FD_LOG_ERR(( "socket() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
Expand Down
10 changes: 5 additions & 5 deletions src/util/log/fd_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#include <execinfo.h>
#endif

#include "../tile/fd_cpuset.h"
#include "../tile/fd_tile_private.h"

#ifdef FD_BUILD_INFO
FD_IMPORT_CSTR( fd_log_build_info, FD_BUILD_INFO );
Expand Down Expand Up @@ -174,9 +174,9 @@ fd_log_private_host_set( char const * host ) {
ulong
fd_log_private_cpu_id_default( void ) {
FD_CPUSET_DECL( cpu_set );
if( FD_UNLIKELY( fd_sched_getaffinity( (pid_t)0, cpu_set ) ) ) return ULONG_MAX;
if( FD_UNLIKELY( fd_cpuset_getaffinity( (pid_t)0, cpu_set ) ) ) return ULONG_MAX;
ulong idx = fd_cpuset_first( cpu_set );
idx = fd_ulong_if( idx<FD_CPUSET_MAX, idx, ULONG_MAX );
idx = fd_ulong_if( idx<FD_TILE_MAX, idx, ULONG_MAX );
return idx;
}

Expand Down Expand Up @@ -204,11 +204,11 @@ static void
fd_log_private_cpu_default( char * name ) { /* FD_LOG_NAME_MAX bytes */
FD_CPUSET_DECL( set );

int err = fd_sched_getaffinity( (pid_t)0, set );
int err = fd_cpuset_getaffinity( (pid_t)0, set );
if( FD_UNLIKELY( err ) ) { sprintf( name, "e%i", err ); return; }

ulong cnt = fd_cpuset_cnt( set );
if( FD_UNLIKELY( !((0UL<cnt) & (cnt<=FD_CPUSET_MAX)) ) ) { sprintf( name, "ec" ); return; }
if( FD_UNLIKELY( !((0UL<cnt) & (cnt<=FD_TILE_MAX)) ) ) { sprintf( name, "ec" ); return; }

ulong idx = fd_cpuset_first( set );
sprintf( name, (cnt>1) ? "f%lu" : "%lu", idx );
Expand Down
3 changes: 1 addition & 2 deletions src/util/tile/Local.mk
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
$(call add-hdrs,fd_tile.h)
$(call add-objs,fd_tile,fd_util)
ifdef FD_HAS_THREADS
$(call add-hdrs,fd_cpuset.h)
$(call add-objs,fd_cpuset,fd_util)
$(call make-unit-test,test_cpuset,test_cpuset,fd_util)
$(call run-unit-test,test_cpuset)
$(call add-objs,fd_tile_threads,fd_util)
Expand Down
16 changes: 0 additions & 16 deletions src/util/tile/fd_cpuset.c

This file was deleted.

77 changes: 0 additions & 77 deletions src/util/tile/fd_cpuset.h

This file was deleted.

16 changes: 16 additions & 0 deletions src/util/tile/fd_tile.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#define _GNU_SOURCE
#include <sched.h>

#include "fd_tile_private.h"

int
fd_cpuset_getaffinity( ulong pid,
fd_cpuset_t * mask ) {
return sched_getaffinity( (int)pid, fd_cpuset_word_cnt<<3, (cpu_set_t *)fd_type_pun( mask ) );
}

int
fd_cpuset_setaffinity( ulong pid,
fd_cpuset_t const * mask ) {
return sched_setaffinity( (int)pid, fd_cpuset_word_cnt<<3, (cpu_set_t const *)fd_type_pun_const( mask ) );
}
10 changes: 1 addition & 9 deletions src/util/tile/fd_tile.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ FD_FN_PURE char ** fd_tile_exec_argv( fd_tile_exec_t const * exec );

int fd_tile_exec_done( fd_tile_exec_t const * exec );

/* These functions are for fd_tile internal use only. */
/* These functions are for fd_util internal use only. */

void
fd_tile_private_boot( int * pargc,
Expand All @@ -167,14 +167,6 @@ fd_tile_private_boot( int * pargc,
void
fd_tile_private_halt( void );

void *
fd_tile_private_stack_new( int optimize,
ulong cpu_idx );

ulong
fd_tile_private_cpus_parse( char const * cstr,
ushort * tile_to_cpu );

FD_PROTOTYPES_END

#endif /* HEADER_fd_src_util_tile_fd_tile_h */
Loading

0 comments on commit 37ba527

Please sign in to comment.