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 18 : char ** argv ) {
14 18 : fd_boot( &argc, &argv );
15 39 : # define SHIFT(n) argv+=(n),argc-=(n)
16 :
17 18 : if( FD_UNLIKELY( argc<1 ) ) FD_LOG_ERR(( "No arguments" ));
18 18 : char const * bin = argv[0];
19 18 : SHIFT(1);
20 :
21 18 : umask( (mode_t)0 ); /* So mode setting gets respected */
22 :
23 18 : int cnt = 0;
24 33 : while( argc ) {
25 15 : char const * cmd = argv[0];
26 15 : SHIFT(1);
27 :
28 15 : 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 12 : } 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 9 : } else if( !strcmp( cmd, "numa-cnt" ) ) {
41 :
42 3 : printf( "%lu\n", fd_shmem_numa_cnt() );
43 :
44 3 : FD_LOG_NOTICE(( "%i: %s: success", cnt, cmd ));
45 :
46 6 : } else if( !strcmp( cmd, "cpu-idx" ) ) {
47 :
48 3 : 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 3 : } else if( !strcmp( cmd, "numa-idx" ) ) {
61 :
62 3 : 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 3 : } else if( !strcmp( cmd, "create" ) ) {
75 :
76 0 : if( FD_UNLIKELY( argc<5 ) ) FD_LOG_ERR(( "%i: %s: too few arguments\n\tDo %s help for help", cnt, cmd, bin ));
77 :
78 0 : char const * name = argv[0];
79 0 : ulong page_cnt = fd_cstr_to_ulong ( argv[1] );
80 0 : ulong page_sz = fd_cstr_to_shmem_page_sz( argv[2] );
81 0 : char const * seq = argv[3];
82 0 : ulong mode = fd_cstr_to_ulong_octal ( argv[4] );
83 :
84 0 : ulong sub_page_cnt[ 512 ];
85 0 : ulong sub_cpu_idx [ 512 ];
86 0 : ulong sub_cnt = fd_cstr_to_ulong_seq( seq, sub_cpu_idx, 512UL );
87 :
88 0 : if( FD_UNLIKELY( !sub_cnt ) )
89 0 : FD_LOG_ERR(( "%i: %s %s %lu %s %s 0%03lo: empty or invalid cpu sequence\n\t"
90 0 : "Do %s help for help", cnt, cmd, name, page_cnt, argv[2], seq, mode, bin ));
91 :
92 0 : if( FD_UNLIKELY( sub_cnt>512UL ) )
93 0 : FD_LOG_ERR(( "%i: %s %s %lu %s %s 0%03lo: sequence too long, increase limit in fd_shmem_ctl.c\n\t"
94 0 : "Do %s help for help", cnt, cmd, name, page_cnt, argv[2], seq, mode, bin ));
95 :
96 0 : ulong sub_page_min = page_cnt / sub_cnt;
97 0 : ulong sub_page_rem = page_cnt % sub_cnt;
98 0 : 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 0 : if( FD_UNLIKELY( fd_shmem_create_multi( name, page_sz, sub_cnt, sub_page_cnt, sub_cpu_idx, mode ) ) )
101 0 : FD_LOG_ERR(( "%i: %s %s %lu %s %s 0%03lo: FAIL\n\t"
102 0 : "Do %s help for help", cnt, cmd, name, page_cnt, argv[2], seq, mode, bin ));
103 :
104 0 : FD_LOG_NOTICE(( "%i: %s %s %lu %s %s 0%03lo: success", cnt, cmd, name, page_cnt, argv[2], seq, mode ));
105 0 : SHIFT(5);
106 :
107 0 : } else if( !strcmp( cmd, "unlink" ) ) {
108 :
109 0 : if( FD_UNLIKELY( argc<2 ) ) FD_LOG_ERR(( "%i: %s: too few arguments\n\tDo %s help for help", cnt, cmd, bin ));
110 :
111 0 : char const * name = argv[0];
112 0 : ulong page_sz = fd_cstr_to_shmem_page_sz( argv[1] );
113 :
114 0 : if( !page_sz ) {
115 0 : fd_shmem_info_t info[1];
116 0 : if( FD_UNLIKELY( fd_shmem_info( name, page_sz, info ) ) )
117 0 : 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 0 : page_sz = info->page_sz;
119 0 : }
120 :
121 0 : if( FD_UNLIKELY( fd_shmem_unlink( name, page_sz ) ) )
122 0 : FD_LOG_ERR(( "%i: %s %s %s: FAIL\n\tDo %s help for help", cnt, cmd, name, argv[1], bin ));
123 :
124 0 : FD_LOG_NOTICE(( "%i: %s %s %s: success", cnt, cmd, name, argv[1]));
125 0 : SHIFT(2);
126 :
127 0 : } else if( !strcmp( cmd, "query" ) ) {
128 :
129 : /* FIXME: MAKE THIS MORE LIKE POD QUERY WITH "WHAT" FIELDS */
130 :
131 0 : if( FD_UNLIKELY( argc<2 ) ) FD_LOG_ERR(( "%i: %s: too few arguments\n\tDo %s help for help", cnt, cmd, bin ));
132 :
133 0 : char const * name = argv[0];
134 0 : ulong page_sz = fd_cstr_to_shmem_page_sz( argv[1] );
135 :
136 0 : fd_shmem_info_t info[1];
137 0 : int err = fd_shmem_info( name, page_sz, info );
138 0 : if( FD_UNLIKELY( err ) ) printf( "%i 0 0\n", err );
139 0 : else printf( "0 %lu %lu\n", info->page_cnt, info->page_sz );
140 :
141 0 : FD_LOG_NOTICE(( "%i: %s %s %s: success", cnt, cmd, name, argv[1]));
142 0 : SHIFT(2);
143 :
144 0 : } else {
145 :
146 0 : FD_LOG_ERR(( "%i: %s: unknown command\n\t"
147 0 : "Do %s help for help", cnt, cmd, bin ));
148 :
149 0 : }
150 15 : cnt++;
151 15 : }
152 :
153 18 : FD_LOG_NOTICE(( "processed %i commands", cnt ));
154 :
155 18 : # undef SHIFT
156 18 : fd_halt();
157 18 : return 0;
158 18 : }
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
|