LCOV - code coverage report
Current view: top level - util/alloc - fd_alloc_ctl.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 138 0.0 %
Date: 2026-03-19 18:19:27 Functions: 0 1 0.0 %

          Line data    Source code
       1             : #include "../fd_util.h"
       2             : #include "../wksp/fd_wksp_private.h"
       3             : 
       4             : #if FD_HAS_HOSTED
       5             : 
       6             : #include <stdio.h>
       7             : 
       8             : FD_IMPORT_CSTR( fd_alloc_ctl_help, "src/util/alloc/fd_alloc_ctl_help" );
       9             : 
      10             : int
      11             : fd_alloc_fprintf( fd_alloc_t * join,
      12             :                   FILE *       stream );
      13             : 
      14             : int
      15             : main( int     argc,
      16           0 :       char ** argv ) {
      17           0 :   fd_boot( &argc, &argv );
      18           0 : # define SHIFT(n) argv+=(n),argc-=(n)
      19             : 
      20           0 :   if( FD_UNLIKELY( argc<1 ) ) FD_LOG_ERR(( "no arguments" ));
      21           0 :   char const * bin = argv[0];
      22           0 :   SHIFT(1);
      23             : 
      24           0 :   ulong tag = 1UL;
      25             : 
      26           0 :   int cnt = 0;
      27           0 :   while( argc ) {
      28           0 :     char const * cmd = argv[0];
      29           0 :     SHIFT(1);
      30             : 
      31           0 :     if( !strcmp( cmd, "help" ) ) {
      32             : 
      33           0 :       fputs( fd_alloc_ctl_help, stdout );
      34             : 
      35           0 :       FD_LOG_NOTICE(( "%i: %s: success", cnt, cmd ));
      36             : 
      37           0 :     } else if( !strcmp( cmd, "tag" ) ) {
      38             : 
      39           0 :       if( FD_UNLIKELY( argc<1 ) ) FD_LOG_ERR(( "%i: %s: too few arguments\n\tDo %s help for help", cnt, cmd, bin ));
      40             : 
      41           0 :       tag = fd_cstr_to_ulong( argv[0] );
      42             : 
      43           0 :       FD_LOG_NOTICE(( "%i: %s %lu: success", cnt, cmd, tag ));
      44           0 :       SHIFT(1);
      45             : 
      46           0 :     } else if( !strcmp( cmd, "new" ) ) {
      47             : 
      48           0 :       if( FD_UNLIKELY( argc<2 ) ) FD_LOG_ERR(( "%i: %s: too few arguments\n\tDo %s help for help", cnt, cmd, bin ));
      49             : 
      50           0 :       char const * name     =                   argv[0];
      51           0 :       ulong        wksp_tag = fd_cstr_to_ulong( argv[1] );
      52             : 
      53           0 :       ulong align     = fd_alloc_align();
      54           0 :       ulong footprint = fd_alloc_footprint();
      55             : 
      56           0 :       fd_wksp_t * wksp = fd_wksp_attach( name ); /* logs details */
      57           0 :       if( FD_UNLIKELY( !wksp ) ) FD_LOG_ERR(( "%i: %s: fd_wksp_attach( \"%s\" ) failed", cnt, cmd, name ));
      58             : 
      59           0 :       ulong gaddr = fd_wksp_alloc( wksp, align, footprint, wksp_tag );
      60           0 :       if( FD_UNLIKELY( !gaddr ) )
      61           0 :         FD_LOG_ERR(( "%i: %s: fd_wksp_alloc(\"%s\",%lu,%lu,%lu) failed", cnt, cmd, name, align, footprint, wksp_tag ));
      62             : 
      63           0 :       void * shmem = fd_wksp_laddr_fast( wksp, gaddr );
      64           0 :       if( FD_UNLIKELY( !fd_alloc_new( shmem, wksp_tag ) ) ) /* logs details */
      65           0 :         FD_LOG_ERR(( "%i: %s: fd_alloc_new(\"%s\",%lu) failed", cnt, cmd, name, wksp_tag ));
      66             : 
      67           0 :       char cstr[ FD_WKSP_CSTR_MAX ];
      68           0 :       printf( "%s\n", fd_wksp_cstr( wksp, gaddr, cstr ) );
      69             : 
      70           0 :       fd_wksp_detach( wksp ); /* logs details */
      71             : 
      72           0 :       FD_LOG_NOTICE(( "%i: %s %s %lu: success", cnt, cmd, name, wksp_tag ));
      73           0 :       SHIFT(2);
      74             : 
      75           0 :     } else if( !strcmp( cmd, "delete" ) ) {
      76             : 
      77           0 :       if( FD_UNLIKELY( argc<2 ) ) FD_LOG_ERR(( "%i: %s: too few arguments\n\tDo %s help for help", cnt, cmd, bin ));
      78             : 
      79           0 :       char const * cstr            =                 argv[0];
      80           0 :       int          garbage_collect = fd_cstr_to_int( argv[1] );
      81             : 
      82           0 :       void * shalloc = fd_wksp_map( cstr );
      83           0 :       if( FD_UNLIKELY( !shalloc ) ) FD_LOG_ERR(( "%i: %s: fd_wksp_map(\"%s\") failed", cnt, cmd, cstr ));
      84             : 
      85           0 :       fd_alloc_t * alloc = fd_alloc_join( shalloc, 0UL /* d/c */ );
      86           0 :       if( FD_UNLIKELY( !alloc ) ) FD_LOG_ERR(( "%i: %s: fd_alloc_join(\"%s\",0UL) failed", cnt, cmd, cstr ));
      87             : 
      88           0 :       fd_wksp_t * wksp     = fd_alloc_wksp( shalloc );
      89           0 :       ulong       wksp_tag = fd_alloc_tag ( alloc   );
      90             : 
      91           0 :       fd_alloc_delete( fd_alloc_leave( alloc ) ); /* logs details */
      92             : 
      93           0 :       if( garbage_collect ) fd_wksp_tag_free( wksp, &wksp_tag, 1UL ); /* logs details */
      94             : 
      95           0 :       fd_wksp_unmap( shalloc ); /* logs details */
      96             : 
      97           0 :       FD_LOG_NOTICE(( "%i: %s %s %i: success", cnt, cmd, cstr, garbage_collect ));
      98           0 :       SHIFT(2);
      99             : 
     100           0 :     } else if( !strcmp( cmd, "malloc" ) ) {
     101             : 
     102           0 :       if( FD_UNLIKELY( argc<4 ) ) FD_LOG_ERR(( "%i: %s: too few arguments\n\tDo %s help for help", cnt, cmd, bin ));
     103             : 
     104           0 :       char const * cstr       =                   argv[0];
     105           0 :       ulong        cgroup_idx = fd_cstr_to_ulong( argv[1] );
     106           0 :       ulong        align      = fd_cstr_to_ulong( argv[2] );
     107           0 :       ulong        sz         = fd_cstr_to_ulong( argv[3] );
     108             : 
     109           0 :       void * shalloc = fd_wksp_map( cstr );
     110           0 :       if( FD_UNLIKELY( !shalloc ) ) FD_LOG_ERR(( "%i: %s: fd_wksp_map(\"%s\") failed", cnt, cmd, cstr ));
     111             : 
     112           0 :       fd_alloc_t * alloc = fd_alloc_join( shalloc, cgroup_idx );
     113           0 :       if( FD_UNLIKELY( !alloc ) ) FD_LOG_ERR(( "%i: %s: fd_alloc_join(\"%s\",%lu) failed", cnt, cmd, cstr, cgroup_idx ));
     114             : 
     115           0 :       fd_wksp_t * wksp  = fd_alloc_wksp( alloc );
     116           0 :       void *      laddr = fd_alloc_malloc( alloc, align, sz );
     117             : 
     118           0 :       char buf[ FD_WKSP_CSTR_MAX ];
     119           0 :       if(      FD_LIKELY( laddr ) ) printf( "%s\n", fd_wksp_cstr( wksp, fd_wksp_gaddr_fast( wksp, laddr ), buf ) );
     120           0 :       else if( FD_LIKELY( !sz   ) ) printf( "%s\n", fd_wksp_cstr( wksp, 0UL,                               buf ) );
     121           0 :       else                          FD_LOG_ERR(( "%i: %s: fd_alloc_malloc(\"%s\":%lu,%lu,%lu) failed",
     122           0 :                                                  cnt, cmd, cstr, cgroup_idx, align, sz ));
     123             : 
     124           0 :       fd_wksp_unmap( fd_alloc_leave( alloc ) ); /* logs details */
     125             : 
     126           0 :       FD_LOG_NOTICE(( "%i: %s %s %lu %lu %lu: success", cnt, cmd, cstr, cgroup_idx, align, sz ));
     127           0 :       SHIFT(4);
     128             : 
     129           0 :     } else if( !strcmp( cmd, "free" ) ) {
     130             : 
     131           0 :       if( FD_UNLIKELY( argc<3 ) ) FD_LOG_ERR(( "%i: %s: too few arguments\n\tDo %s help for help", cnt, cmd, bin ));
     132             : 
     133           0 :       char const * cstr       =                   argv[0];
     134           0 :       ulong        cgroup_idx = fd_cstr_to_ulong( argv[1] );
     135           0 :       char const * name_gaddr =                   argv[2];
     136             : 
     137           0 :       void * laddr = fd_wksp_map( name_gaddr ); /* logs details */
     138           0 :       if( FD_LIKELY( laddr ) ) {
     139           0 :         void * shalloc = fd_wksp_map( cstr ); /* logs details */
     140           0 :         if( FD_LIKELY( shalloc ) ) {
     141           0 :           fd_alloc_t * alloc = fd_alloc_join( shalloc, cgroup_idx ); /* logs details */
     142           0 :           if( FD_LIKELY( alloc ) ) {
     143           0 :             if( FD_LIKELY( fd_wksp_containing( laddr )==fd_alloc_wksp( alloc ) ) ) fd_alloc_free( alloc, laddr );
     144           0 :             else FD_LOG_WARNING(( "%i: %s: alloc %s was not used for %s", cnt, cmd, cstr, name_gaddr ));
     145           0 :             fd_alloc_leave( alloc ); /* logs details */
     146           0 :           }
     147           0 :           fd_wksp_unmap( shalloc ); /* logs details */
     148           0 :         }
     149           0 :         fd_wksp_unmap( laddr ); /* logs details */
     150           0 :       }
     151             : 
     152           0 :       FD_LOG_NOTICE(( "%i: %s %s %lu %s: success", cnt, cmd, cstr, cgroup_idx, name_gaddr ));
     153           0 :       SHIFT(3);
     154             : 
     155           0 :     } else if( !strcmp( cmd, "compact" ) ) {
     156             : 
     157           0 :       if( FD_UNLIKELY( argc<1 ) ) FD_LOG_ERR(( "%i: %s: too few arguments\n\tDo %s help for help", cnt, cmd, bin ));
     158             : 
     159           0 :       char const * cstr = argv[0];
     160             : 
     161           0 :       void * shalloc = fd_wksp_map( cstr );
     162           0 :       if( FD_UNLIKELY( !shalloc ) ) FD_LOG_ERR(( "%i: %s: fd_wksp_map(\"%s\") failed", cnt, cmd, cstr ));
     163             : 
     164           0 :       fd_alloc_t * alloc = fd_alloc_join( shalloc, 0 /*d/c*/ );
     165           0 :       if( FD_UNLIKELY( !alloc ) ) FD_LOG_ERR(( "%i: %s: fd_alloc_join(\"%s\",0) failed", cnt, cmd, cstr ));
     166             : 
     167           0 :       fd_alloc_compact( alloc ); /* logs details */
     168             : 
     169           0 :       fd_wksp_unmap( fd_alloc_leave( alloc ) ); /* logs details */
     170             : 
     171           0 :       FD_LOG_NOTICE(( "%i: %s %s: success", cnt, cmd, cstr ));
     172           0 :       SHIFT(1);
     173             : 
     174           0 :     } else if( !strcmp( cmd, "query" ) ) {
     175             : 
     176           0 :       if( FD_UNLIKELY( argc<2 ) ) FD_LOG_ERR(( "%i: %s: too few arguments\n\tDo %s help for help", cnt, cmd, bin ));
     177             : 
     178           0 :       char const * what = argv[0];
     179           0 :       char const * cstr = argv[1];
     180             : 
     181           0 :       void *       shmem = NULL;
     182           0 :       fd_alloc_t * alloc = NULL;
     183           0 :       int          err   = -1;
     184             : 
     185           0 :       shmem = fd_wksp_map( cstr );
     186           0 :       if( FD_LIKELY( shmem ) ) {
     187           0 :         alloc = fd_alloc_join( shmem, 0UL /* d/c */ );
     188           0 :         if( FD_LIKELY( alloc ) ) err = 0;
     189           0 :       }
     190             : 
     191           0 :       if(      !strcmp( what, "test" ) ) printf( "%i\n", err );
     192           0 :       else if( !strcmp( what, "tag"  ) ) printf( "%lu\n", FD_UNLIKELY( err ) ? 0UL : fd_alloc_tag( alloc ) );
     193           0 :       else if( !strcmp( what, "leak" ) ) printf( "%i\n",  FD_UNLIKELY( err ) ? -1  : !fd_alloc_is_empty( alloc ) );
     194           0 :       else if( !strcmp( what, "full" ) ) fd_alloc_fprintf( alloc, stdout );
     195           0 :       else                               FD_LOG_ERR(( "unknown query %s", what ));
     196             : 
     197           0 :       if( FD_LIKELY( alloc ) ) fd_alloc_leave( alloc );
     198           0 :       if( FD_LIKELY( shmem ) ) fd_wksp_unmap( shmem );
     199             : 
     200           0 :       FD_LOG_NOTICE(( "%i: %s %s %s: success", cnt, cmd, what, cstr ));
     201           0 :       SHIFT(2);
     202             : 
     203           0 :     } else {
     204             : 
     205           0 :       FD_LOG_ERR(( "%i: %s: unknown command\n\t"
     206           0 :                    "Do %s help for help", cnt, cmd, bin ));
     207             : 
     208           0 :     }
     209           0 :     cnt++;
     210           0 :   }
     211             : 
     212           0 :   if( FD_UNLIKELY( cnt<1 ) ) FD_LOG_NOTICE(( "processed %i commands\n\tDo %s help for help", cnt, bin ));
     213           0 :   else                       FD_LOG_NOTICE(( "processed %i commands", cnt ));
     214             : 
     215           0 : # undef SHIFT
     216           0 :   fd_halt();
     217           0 :   return 0;
     218           0 : }
     219             : 
     220             : #else
     221             : 
     222             : int
     223             : main( int     argc,
     224             :       char ** argv ) {
     225             :   fd_boot( &argc, &argv );
     226             :   if( FD_UNLIKELY( argc<1 ) ) FD_LOG_ERR(( "No arguments" ));
     227             :   if( FD_UNLIKELY( argc>1 ) ) FD_LOG_ERR(( "fd_alloc_ctl not supported on this platform" ));
     228             :   FD_LOG_NOTICE(( "processed 0 commands" ));
     229             :   fd_halt();
     230             :   return 0;
     231             : }
     232             : 
     233             : #endif
     234             : 

Generated by: LCOV version 1.14