LCOV - code coverage report
Current view: top level - util/shmem - fd_shmem_ctl.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 93 95 97.9 %
Date: 2025-07-01 05:00:49 Functions: 1 1 100.0 %

          Line data    Source code
       1             : #include "../fd_util.h"
       2             : 
       3             : #if FD_HAS_HOSTED
       4             : 
       5             : #include <stdio.h>
       6             : #include <errno.h>
       7             : #include <sys/stat.h>
       8             :  
       9             : FD_IMPORT_CSTR( fd_shmem_ctl_help, "src/util/shmem/fd_shmem_ctl_help" );
      10             : 
      11             : int
      12             : main( int     argc,
      13         141 :       char ** argv ) {
      14         141 :   fd_boot( &argc, &argv );
      15         354 : # define SHIFT(n) argv+=(n),argc-=(n)
      16             : 
      17         141 :   if( FD_UNLIKELY( argc<1 ) ) FD_LOG_ERR(( "No arguments" ));
      18         141 :   char const * bin = argv[0];
      19         141 :   SHIFT(1);
      20             :   
      21         141 :   umask( (mode_t)0 ); /* So mode setting gets respected */
      22             : 
      23         141 :   int cnt = 0;
      24         198 :   while( argc ) {
      25         168 :     char const * cmd = argv[0];
      26         168 :     SHIFT(1);
      27             : 
      28         168 :     if( !strcmp( cmd, "help" ) ) {
      29             : 
      30           3 :       fputs( fd_shmem_ctl_help, stdout );
      31             : 
      32           3 :       FD_LOG_NOTICE(( "%i: %s: success", cnt, cmd ));
      33             : 
      34         165 :     } else if( !strcmp( cmd, "cpu-cnt" ) ) {
      35             : 
      36           3 :       printf( "%lu\n", fd_shmem_cpu_cnt() );
      37             : 
      38           3 :       FD_LOG_NOTICE(( "%i: %s: success", cnt, cmd ));
      39             : 
      40         162 :     } else if( !strcmp( cmd, "numa-cnt" ) ) {
      41             : 
      42           6 :       printf( "%lu\n", fd_shmem_numa_cnt() );
      43             : 
      44           6 :       FD_LOG_NOTICE(( "%i: %s: success", cnt, cmd ));
      45             : 
      46         156 :     } else if( !strcmp( cmd, "cpu-idx" ) ) {
      47             : 
      48           6 :       if( FD_UNLIKELY( argc<1 ) ) FD_LOG_ERR(( "%i: %s: too few arguments\n\tDo %s help for help", cnt, cmd, bin ));
      49             : 
      50           3 :       ulong numa_idx = fd_cstr_to_ulong( argv[0] );
      51             : 
      52           3 :       ulong cpu_idx = fd_shmem_cpu_idx( numa_idx );
      53             : 
      54           3 :       if( FD_LIKELY( cpu_idx<ULONG_MAX ) ) printf( "%lu\n", cpu_idx );
      55           0 :       else                                 printf( "-\n" );
      56             : 
      57           3 :       FD_LOG_NOTICE(( "%i: %s %lu: success", cnt, cmd, numa_idx ));
      58           3 :       SHIFT(1);
      59             : 
      60         150 :     } else if( !strcmp( cmd, "numa-idx" ) ) {
      61             : 
      62           6 :       if( FD_UNLIKELY( argc<1 ) ) FD_LOG_ERR(( "%i: %s: too few arguments\n\tDo %s help for help", cnt, cmd, bin ));
      63             : 
      64           3 :       ulong cpu_idx = fd_cstr_to_ulong( argv[0] );
      65             : 
      66           3 :       ulong numa_idx = fd_shmem_numa_idx( cpu_idx );
      67             : 
      68           3 :       if( FD_LIKELY( numa_idx<ULONG_MAX ) ) printf( "%lu\n", numa_idx );
      69           0 :       else                                  printf( "-\n" );
      70             : 
      71           3 :       FD_LOG_NOTICE(( "%i: %s %lu: success", cnt, cmd, cpu_idx ));
      72           3 :       SHIFT(1);
      73             : 
      74         144 :     } else if( !strcmp( cmd, "create" ) ) {
      75             : 
      76          84 :       if( FD_UNLIKELY( argc<5 ) ) FD_LOG_ERR(( "%i: %s: too few arguments\n\tDo %s help for help", cnt, cmd, bin ));
      77             : 
      78          69 :       char const * name     =                           argv[0];
      79          69 :       ulong        page_cnt = fd_cstr_to_ulong        ( argv[1] );
      80          69 :       ulong        page_sz  = fd_cstr_to_shmem_page_sz( argv[2] );
      81          69 :       char const * seq      =                           argv[3];
      82          69 :       ulong        mode     = fd_cstr_to_ulong_octal  ( argv[4] );
      83             : 
      84          69 :       ulong sub_page_cnt[ 512 ];
      85          69 :       ulong sub_cpu_idx [ 512 ];
      86          69 :       ulong sub_cnt = fd_cstr_to_ulong_seq( seq, sub_cpu_idx, 512UL );
      87             : 
      88          69 :       if( FD_UNLIKELY( !sub_cnt ) )
      89          42 :         FD_LOG_ERR(( "%i: %s %s %lu %s %s 0%03lo: empty or invalid cpu sequence\n\t"
      90          69 :                      "Do %s help for help", cnt, cmd, name, page_cnt, argv[2], seq, mode, bin ));
      91             : 
      92          27 :       if( FD_UNLIKELY( sub_cnt>512UL ) )
      93           3 :         FD_LOG_ERR(( "%i: %s %s %lu %s %s 0%03lo: sequence too long, increase limit in fd_shmem_ctl.c\n\t"
      94          27 :                      "Do %s help for help", cnt, cmd, name, page_cnt, argv[2], seq, mode, bin ));
      95             : 
      96          24 :       ulong sub_page_min = page_cnt / sub_cnt;
      97          24 :       ulong sub_page_rem = page_cnt % sub_cnt;
      98          48 :       for( ulong sub_idx=0UL; sub_idx<sub_cnt; sub_idx++ ) sub_page_cnt[ sub_idx ] = sub_page_min + (ulong)(sub_idx<sub_page_rem);
      99             : 
     100          24 :       if( FD_UNLIKELY( fd_shmem_create_multi( name, page_sz, sub_cnt, sub_page_cnt, sub_cpu_idx, mode ) ) )
     101          18 :         FD_LOG_ERR(( "%i: %s %s %lu %s %s 0%03lo: FAIL\n\t"
     102          24 :                      "Do %s help for help", cnt, cmd, name, page_cnt, argv[2], seq, mode, bin ));
     103             : 
     104           6 :       FD_LOG_NOTICE(( "%i: %s %s %lu %s %s 0%03lo: success", cnt, cmd, name, page_cnt, argv[2], seq, mode ));
     105           6 :       SHIFT(5);
     106             : 
     107          60 :     } else if( !strcmp( cmd, "unlink" ) ) {
     108             : 
     109          21 :       if( FD_UNLIKELY( argc<2 ) ) FD_LOG_ERR(( "%i: %s: too few arguments\n\tDo %s help for help", cnt, cmd, bin ));
     110             : 
     111          12 :       char const * name     =                           argv[0];
     112          12 :       ulong        page_sz  = fd_cstr_to_shmem_page_sz( argv[1] );
     113             : 
     114          12 :       if( !page_sz ) {
     115           6 :         fd_shmem_info_t info[1];
     116           6 :         if( FD_UNLIKELY( fd_shmem_info( name, page_sz, info ) ) )
     117           3 :           FD_LOG_ERR(( "%i: %s %s %s: not found or bad permissions\n\tDo %s help for help", cnt, cmd, name, argv[1], bin ));
     118           3 :         page_sz = info->page_sz;
     119           3 :       }
     120             : 
     121           9 :       if( FD_UNLIKELY( fd_shmem_unlink( name, page_sz ) ) )
     122           3 :         FD_LOG_ERR(( "%i: %s %s %s: FAIL\n\tDo %s help for help", cnt, cmd, name, argv[1], bin ));
     123             : 
     124           6 :       FD_LOG_NOTICE(( "%i: %s %s %s: success", cnt, cmd, name, argv[1]));
     125           6 :       SHIFT(2);
     126             : 
     127          39 :     } else if( !strcmp( cmd, "query" ) ) {
     128             : 
     129             :       /* FIXME: MAKE THIS MORE LIKE POD QUERY WITH "WHAT" FIELDS */
     130             : 
     131          33 :       if( FD_UNLIKELY( argc<2 ) ) FD_LOG_ERR(( "%i: %s: too few arguments\n\tDo %s help for help", cnt, cmd, bin ));
     132             : 
     133          27 :       char const * name     =                           argv[0];
     134          27 :       ulong        page_sz  = fd_cstr_to_shmem_page_sz( argv[1] );
     135             : 
     136          27 :       fd_shmem_info_t info[1];
     137          27 :       int err = fd_shmem_info( name, page_sz, info );
     138          27 :       if( FD_UNLIKELY( err ) ) printf( "%i 0 0\n",    err );
     139          12 :       else                     printf( "0 %lu %lu\n", info->page_cnt, info->page_sz );
     140             : 
     141          27 :       FD_LOG_NOTICE(( "%i: %s %s %s: success", cnt, cmd, name, argv[1]));
     142          27 :       SHIFT(2);
     143             : 
     144          27 :     } else {
     145             : 
     146           6 :       FD_LOG_ERR(( "%i: %s: unknown command\n\t"
     147           6 :                    "Do %s help for help", cnt, cmd, bin ));
     148             : 
     149           6 :     }
     150          57 :     cnt++;
     151          57 :   }
     152             : 
     153          30 :   FD_LOG_NOTICE(( "processed %i commands", cnt ));
     154             : 
     155          30 : # undef SHIFT
     156          30 :   fd_halt();
     157          30 :   return 0;
     158         141 : }
     159             : 
     160             : #else
     161             : 
     162             : int
     163             : main( int     argc,
     164             :       char ** argv ) {
     165             :   fd_boot( &argc, &argv );
     166             :   if( FD_UNLIKELY( argc<1 ) ) FD_LOG_ERR(( "No arguments" ));
     167             :   if( FD_UNLIKELY( argc>1 ) ) FD_LOG_ERR(( "fd_shmem_ctl not supported on this platform" ));
     168             :   FD_LOG_NOTICE(( "processed 0 commands" ));
     169             :   fd_halt();
     170             :   return 0;
     171             : }
     172             : 
     173             : #endif

Generated by: LCOV version 1.14