Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #include <svm/svm_fifo_segment.h> |
| 17 | |
| 18 | svm_fifo_segment_main_t svm_fifo_segment_main; |
| 19 | |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 20 | static void |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 21 | allocate_new_fifo_chunk (svm_fifo_segment_header_t * fsh, |
| 22 | u32 data_size_in_bytes, int chunk_size) |
| 23 | { |
| 24 | int freelist_index; |
| 25 | u32 size; |
| 26 | u8 *fifo_space; |
| 27 | u32 rounded_data_size; |
| 28 | svm_fifo_t *f; |
| 29 | int i; |
| 30 | |
| 31 | rounded_data_size = (1 << (max_log2 (data_size_in_bytes))); |
| 32 | freelist_index = max_log2 (rounded_data_size) |
| 33 | - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE); |
| 34 | |
| 35 | /* Calculate space requirement $$$ round-up data_size_in_bytes */ |
| 36 | size = (sizeof (*f) + rounded_data_size) * chunk_size; |
| 37 | |
| 38 | /* Allocate fifo space. May fail. */ |
| 39 | fifo_space = clib_mem_alloc_aligned_at_offset |
| 40 | (size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ , |
| 41 | 0 /* os_out_of_memory */ ); |
| 42 | |
| 43 | /* Out of space.. */ |
| 44 | if (fifo_space == 0) |
| 45 | return; |
| 46 | |
| 47 | /* Carve fifo space */ |
| 48 | f = (svm_fifo_t *) fifo_space; |
| 49 | for (i = 0; i < chunk_size; i++) |
| 50 | { |
| 51 | f->freelist_index = freelist_index; |
| 52 | f->next = fsh->free_fifos[freelist_index]; |
| 53 | fsh->free_fifos[freelist_index] = f; |
| 54 | fifo_space += sizeof (*f) + rounded_data_size; |
| 55 | f = (svm_fifo_t *) fifo_space; |
| 56 | } |
| 57 | } |
| 58 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 59 | /** |
| 60 | * Pre-allocates fifo pairs in fifo segment |
| 61 | * |
| 62 | * The number of fifos pre-allocated is the minimum of the requested number |
| 63 | * of pairs and the maximum number that fit within the segment. If the maximum |
| 64 | * is hit, the number of fifo pairs requested is updated by subtracting the |
| 65 | * number of fifos that have been successfully allocated. |
| 66 | */ |
| 67 | void |
| 68 | svm_fifo_segment_preallocate_fifo_pairs (svm_fifo_segment_private_t * s, |
| 69 | u32 rx_fifo_size, u32 tx_fifo_size, |
| 70 | u32 * n_fifo_pairs) |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 71 | { |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 72 | u32 rx_rounded_data_size, tx_rounded_data_size, pair_size; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 73 | u32 rx_fifos_size, tx_fifos_size, pairs_to_allocate; |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 74 | int rx_freelist_index, tx_freelist_index; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 75 | ssvm_shared_header_t *sh = s->ssvm.sh; |
| 76 | svm_fifo_segment_header_t *fsh = s->h; |
| 77 | u8 *rx_fifo_space, *tx_fifo_space; |
| 78 | uword space_available; |
| 79 | void *oldheap; |
| 80 | svm_fifo_t *f; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 81 | int i; |
| 82 | |
| 83 | /* Parameter check */ |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 84 | if (rx_fifo_size == 0 || tx_fifo_size == 0 || *n_fifo_pairs == 0) |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 85 | return; |
| 86 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 87 | if (rx_fifo_size < FIFO_SEGMENT_MIN_FIFO_SIZE || |
| 88 | rx_fifo_size > FIFO_SEGMENT_MAX_FIFO_SIZE) |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 89 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 90 | clib_warning ("rx fifo_size out of range %d", rx_fifo_size); |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 91 | return; |
| 92 | } |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 93 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 94 | if (tx_fifo_size < FIFO_SEGMENT_MIN_FIFO_SIZE || |
| 95 | tx_fifo_size > FIFO_SEGMENT_MAX_FIFO_SIZE) |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 96 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 97 | clib_warning ("tx fifo_size out of range %d", rx_fifo_size); |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 101 | rx_rounded_data_size = (1 << (max_log2 (rx_fifo_size))); |
| 102 | rx_freelist_index = max_log2 (rx_fifo_size) |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 103 | - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 104 | tx_rounded_data_size = (1 << (max_log2 (tx_fifo_size))); |
| 105 | tx_freelist_index = max_log2 (tx_fifo_size) |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 106 | - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE); |
| 107 | |
| 108 | /* Calculate space requirements */ |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 109 | pair_size = 2 * sizeof (*f) + rx_rounded_data_size + tx_rounded_data_size; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 110 | #if USE_DLMALLOC == 0 |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 111 | space_available = s->ssvm.ssvm_size - mheap_bytes (sh->heap); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 112 | #else |
| 113 | space_available = s->ssvm.ssvm_size - mspace_usable_size (sh->heap); |
| 114 | #endif |
| 115 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 116 | pairs_to_allocate = clib_min (space_available / pair_size, *n_fifo_pairs); |
| 117 | rx_fifos_size = (sizeof (*f) + rx_rounded_data_size) * pairs_to_allocate; |
| 118 | tx_fifos_size = (sizeof (*f) + tx_rounded_data_size) * pairs_to_allocate; |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 119 | |
| 120 | vec_validate_init_empty (fsh->free_fifos, |
| 121 | clib_max (rx_freelist_index, tx_freelist_index), |
| 122 | 0); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 123 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 124 | oldheap = ssvm_push_heap (sh); |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 125 | /* Allocate rx fifo space. May fail. */ |
| 126 | rx_fifo_space = clib_mem_alloc_aligned_at_offset |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 127 | (rx_fifos_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ , |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 128 | 0 /* os_out_of_memory */ ); |
| 129 | |
| 130 | /* Same for TX */ |
| 131 | tx_fifo_space = clib_mem_alloc_aligned_at_offset |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 132 | (tx_fifos_size, CLIB_CACHE_LINE_BYTES, 0 /* align_offset */ , |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 133 | 0 /* os_out_of_memory */ ); |
| 134 | |
| 135 | /* Make sure it worked. Clean up if it didn't... */ |
| 136 | if (rx_fifo_space == 0 || tx_fifo_space == 0) |
| 137 | { |
| 138 | if (rx_fifo_space) |
| 139 | clib_mem_free (rx_fifo_space); |
| 140 | else |
| 141 | clib_warning ("rx fifo preallocation failure: size %d npairs %d", |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 142 | rx_fifo_size, *n_fifo_pairs); |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 143 | |
| 144 | if (tx_fifo_space) |
| 145 | clib_mem_free (tx_fifo_space); |
| 146 | else |
| 147 | clib_warning ("tx fifo preallocation failure: size %d nfifos %d", |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 148 | tx_fifo_size, *n_fifo_pairs); |
| 149 | ssvm_pop_heap (oldheap); |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 150 | return; |
| 151 | } |
| 152 | |
| 153 | /* Carve rx fifo space */ |
| 154 | f = (svm_fifo_t *) rx_fifo_space; |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 155 | for (i = 0; i < pairs_to_allocate; i++) |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 156 | { |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 157 | f->freelist_index = rx_freelist_index; |
| 158 | f->next = fsh->free_fifos[rx_freelist_index]; |
| 159 | fsh->free_fifos[rx_freelist_index] = f; |
| 160 | rx_fifo_space += sizeof (*f) + rx_rounded_data_size; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 161 | f = (svm_fifo_t *) rx_fifo_space; |
| 162 | } |
| 163 | /* Carve tx fifo space */ |
| 164 | f = (svm_fifo_t *) tx_fifo_space; |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 165 | for (i = 0; i < pairs_to_allocate; i++) |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 166 | { |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 167 | f->freelist_index = tx_freelist_index; |
| 168 | f->next = fsh->free_fifos[tx_freelist_index]; |
| 169 | fsh->free_fifos[tx_freelist_index] = f; |
| 170 | tx_fifo_space += sizeof (*f) + tx_rounded_data_size; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 171 | f = (svm_fifo_t *) tx_fifo_space; |
| 172 | } |
Florin Coras | 9d06304 | 2017-09-14 03:08:00 -0400 | [diff] [blame] | 173 | |
| 174 | /* Account for the pairs allocated */ |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 175 | *n_fifo_pairs -= pairs_to_allocate; |
| 176 | ssvm_pop_heap (oldheap); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Initialize svm fifo segment shared header |
| 181 | */ |
| 182 | int |
| 183 | svm_fifo_segment_init (svm_fifo_segment_private_t * s) |
| 184 | { |
| 185 | svm_fifo_segment_header_t *fsh; |
| 186 | ssvm_shared_header_t *sh; |
| 187 | void *oldheap; |
| 188 | |
| 189 | sh = s->ssvm.sh; |
| 190 | oldheap = ssvm_push_heap (sh); |
| 191 | |
| 192 | fsh = clib_mem_alloc (sizeof (*fsh)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 193 | clib_memset (fsh, 0, sizeof (*fsh)); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 194 | s->h = sh->opaque[0] = fsh; |
| 195 | |
| 196 | ssvm_pop_heap (oldheap); |
| 197 | |
| 198 | sh->ready = 1; |
| 199 | return (0); |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 200 | } |
| 201 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 202 | /** |
| 203 | * Create an svm fifo segment and initialize as master |
| 204 | */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 205 | int |
| 206 | svm_fifo_segment_create (svm_fifo_segment_create_args_t * a) |
| 207 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 208 | svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 209 | svm_fifo_segment_private_t *s; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 210 | int rv; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 211 | |
| 212 | /* Allocate a fresh segment */ |
| 213 | pool_get (sm->segments, s); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 214 | clib_memset (s, 0, sizeof (*s)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 215 | |
| 216 | s->ssvm.ssvm_size = a->segment_size; |
| 217 | s->ssvm.i_am_master = 1; |
| 218 | s->ssvm.my_pid = getpid (); |
Dave Wallace | b885664 | 2017-07-31 13:33:11 -0400 | [diff] [blame] | 219 | s->ssvm.name = format (0, "%s%c", a->segment_name, 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 220 | s->ssvm.requested_va = sm->next_baseva; |
| 221 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 222 | if ((rv = ssvm_master_init (&s->ssvm, a->segment_type))) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 223 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 224 | pool_put (sm->segments, s); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 225 | return (rv); |
| 226 | } |
| 227 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 228 | /* Note: requested_va updated due to seg base addr randomization */ |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 229 | sm->next_baseva = s->ssvm.sh->ssvm_va + a->segment_size; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 230 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 231 | svm_fifo_segment_init (s); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 232 | vec_add1 (a->new_segment_indices, s - sm->segments); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 233 | return (0); |
| 234 | } |
| 235 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 236 | /** |
| 237 | * Create an svm fifo segment in process-private memory |
| 238 | */ |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 239 | int |
| 240 | svm_fifo_segment_create_process_private (svm_fifo_segment_create_args_t * a) |
| 241 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 242 | svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 243 | svm_fifo_segment_private_t *s; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 244 | ssvm_shared_header_t *sh; |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 245 | u32 rnd_size = 0; |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 246 | u8 *heap; |
| 247 | u32 pagesize = clib_mem_get_page_size (); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 248 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 249 | pool_get (sm->segments, s); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 250 | clib_memset (s, 0, sizeof (*s)); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 251 | |
| 252 | rnd_size = (a->segment_size + (pagesize - 1)) & ~pagesize; |
| 253 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 254 | #if USE_DLMALLOC == 0 |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 255 | heap = mheap_alloc (0, rnd_size); |
| 256 | if (heap == 0) |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 257 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 258 | clib_unix_warning ("mheap alloc"); |
| 259 | pool_put (sm->segments, s); |
| 260 | return -1; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 261 | } |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 262 | { |
| 263 | mheap_t *heap_header; |
| 264 | heap_header = mheap_header (heap); |
| 265 | heap_header->flags |= MHEAP_FLAG_THREAD_SAFE; |
| 266 | } |
| 267 | #else |
| 268 | heap = create_mspace (rnd_size, 1 /* locked */ ); |
| 269 | #endif |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 270 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 271 | s->ssvm.ssvm_size = rnd_size; |
| 272 | s->ssvm.i_am_master = 1; |
| 273 | s->ssvm.my_pid = getpid (); |
| 274 | s->ssvm.name = format (0, "%s%c", a->segment_name, 0); |
| 275 | s->ssvm.requested_va = ~0; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 276 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 277 | /* Allocate a [sic] shared memory header, in process memory... */ |
| 278 | sh = clib_mem_alloc_aligned (sizeof (*sh), CLIB_CACHE_LINE_BYTES); |
| 279 | s->ssvm.sh = sh; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 280 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 281 | clib_memset (sh, 0, sizeof (*sh)); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 282 | sh->heap = heap; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 283 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 284 | svm_fifo_segment_init (s); |
| 285 | vec_add1 (a->new_segment_indices, s - sm->segments); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 286 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 287 | return (0); |
| 288 | } |
| 289 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 290 | /** |
| 291 | * Attach as slave to an svm fifo segment |
| 292 | */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 293 | int |
| 294 | svm_fifo_segment_attach (svm_fifo_segment_create_args_t * a) |
| 295 | { |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 296 | svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 297 | svm_fifo_segment_private_t *s; |
| 298 | int rv; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 299 | |
| 300 | /* Allocate a fresh segment */ |
| 301 | pool_get (sm->segments, s); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 302 | clib_memset (s, 0, sizeof (*s)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 303 | |
| 304 | s->ssvm.ssvm_size = a->segment_size; |
| 305 | s->ssvm.my_pid = getpid (); |
Dave Wallace | b885664 | 2017-07-31 13:33:11 -0400 | [diff] [blame] | 306 | s->ssvm.name = format (0, "%s%c", a->segment_name, 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 307 | s->ssvm.requested_va = sm->next_baseva; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 308 | if (a->segment_type == SSVM_SEGMENT_MEMFD) |
| 309 | s->ssvm.fd = a->memfd_fd; |
| 310 | else |
| 311 | s->ssvm.attach_timeout = sm->timeout_in_seconds; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 312 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 313 | if ((rv = ssvm_slave_init (&s->ssvm, a->segment_type))) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 314 | { |
| 315 | _vec_len (s) = vec_len (s) - 1; |
| 316 | return (rv); |
| 317 | } |
| 318 | |
| 319 | /* Fish the segment header */ |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 320 | s->h = s->ssvm.sh->opaque[0]; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 321 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 322 | vec_add1 (a->new_segment_indices, s - sm->segments); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 323 | return (0); |
| 324 | } |
| 325 | |
| 326 | void |
| 327 | svm_fifo_segment_delete (svm_fifo_segment_private_t * s) |
| 328 | { |
| 329 | svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 330 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 331 | ssvm_delete (&s->ssvm); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 332 | clib_memset (s, 0xfe, sizeof (*s)); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 333 | pool_put (sm->segments, s); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 334 | } |
| 335 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 336 | /** |
| 337 | * Allocate fifo in svm segment |
| 338 | */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 339 | svm_fifo_t * |
| 340 | svm_fifo_segment_alloc_fifo (svm_fifo_segment_private_t * s, |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 341 | u32 data_size_in_bytes, |
| 342 | svm_fifo_segment_freelist_t list_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 343 | { |
| 344 | ssvm_shared_header_t *sh; |
| 345 | svm_fifo_segment_header_t *fsh; |
Florin Coras | b082324 | 2018-04-27 09:46:46 -0700 | [diff] [blame] | 346 | svm_fifo_t *f = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 347 | void *oldheap; |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 348 | int freelist_index; |
| 349 | |
| 350 | /* |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 351 | * 4K minimum. It's not likely that anything good will happen |
| 352 | * with a smaller FIFO. |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 353 | */ |
| 354 | if (data_size_in_bytes < FIFO_SEGMENT_MIN_FIFO_SIZE || |
| 355 | data_size_in_bytes > FIFO_SEGMENT_MAX_FIFO_SIZE) |
| 356 | { |
| 357 | clib_warning ("fifo size out of range %d", data_size_in_bytes); |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | freelist_index = max_log2 (data_size_in_bytes) |
| 362 | - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 363 | |
| 364 | sh = s->ssvm.sh; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 365 | ssvm_lock_non_recursive (sh, 1); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 366 | fsh = (svm_fifo_segment_header_t *) sh->opaque[0]; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 367 | |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 368 | switch (list_index) |
| 369 | { |
| 370 | case FIFO_SEGMENT_RX_FREELIST: |
| 371 | case FIFO_SEGMENT_TX_FREELIST: |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 372 | vec_validate_init_empty (fsh->free_fifos, freelist_index, 0); |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 373 | f = fsh->free_fifos[freelist_index]; |
Florin Coras | b082324 | 2018-04-27 09:46:46 -0700 | [diff] [blame] | 374 | if (PREDICT_FALSE (!f)) |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 375 | { |
Florin Coras | b082324 | 2018-04-27 09:46:46 -0700 | [diff] [blame] | 376 | /* Preallocated and no fifo left. Don't even try */ |
| 377 | if (fsh->flags & FIFO_SEGMENT_F_IS_PREALLOCATED) |
| 378 | goto done; |
| 379 | |
| 380 | oldheap = ssvm_push_heap (sh); |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 381 | allocate_new_fifo_chunk (fsh, data_size_in_bytes, |
| 382 | FIFO_SEGMENT_ALLOC_CHUNK_SIZE); |
Florin Coras | b082324 | 2018-04-27 09:46:46 -0700 | [diff] [blame] | 383 | ssvm_pop_heap (oldheap); |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 384 | f = fsh->free_fifos[freelist_index]; |
| 385 | } |
| 386 | if (PREDICT_TRUE (f != 0)) |
| 387 | { |
| 388 | fsh->free_fifos[freelist_index] = f->next; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 389 | /* (re)initialize the fifo, as in svm_fifo_create */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 390 | clib_memset (f, 0, sizeof (*f)); |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 391 | f->nitems = data_size_in_bytes; |
| 392 | f->ooos_list_head = OOO_SEGMENT_INVALID_INDEX; |
Florin Coras | 326b81e | 2018-10-04 19:03:05 -0700 | [diff] [blame] | 393 | f->ct_session_index = SVM_FIFO_INVALID_SESSION_INDEX; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 394 | f->refcnt = 1; |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 395 | f->freelist_index = freelist_index; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 396 | goto found; |
| 397 | } |
Florin Coras | b082324 | 2018-04-27 09:46:46 -0700 | [diff] [blame] | 398 | break; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 399 | case FIFO_SEGMENT_FREELIST_NONE: |
| 400 | break; |
| 401 | |
| 402 | default: |
| 403 | clib_warning ("ignore bogus freelist %d", list_index); |
| 404 | break; |
| 405 | } |
| 406 | |
Florin Coras | b082324 | 2018-04-27 09:46:46 -0700 | [diff] [blame] | 407 | /* Catch all that allocates just one fifo. Note: this can fail, |
| 408 | * in which case: create another segment */ |
| 409 | oldheap = ssvm_push_heap (sh); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 410 | f = svm_fifo_create (data_size_in_bytes); |
Florin Coras | b082324 | 2018-04-27 09:46:46 -0700 | [diff] [blame] | 411 | ssvm_pop_heap (oldheap); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 412 | if (PREDICT_FALSE (f == 0)) |
Florin Coras | b082324 | 2018-04-27 09:46:46 -0700 | [diff] [blame] | 413 | goto done; |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 414 | f->freelist_index = freelist_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 415 | |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 416 | found: |
| 417 | /* If rx_freelist add to active fifos list. When cleaning up segment, |
| 418 | * we need a list of active sessions that should be disconnected. Since |
| 419 | * both rx and tx fifos keep pointers to the session, it's enough to track |
| 420 | * only one. */ |
| 421 | if (list_index == FIFO_SEGMENT_RX_FREELIST) |
| 422 | { |
| 423 | if (fsh->fifos) |
| 424 | { |
| 425 | fsh->fifos->prev = f; |
| 426 | f->next = fsh->fifos; |
| 427 | } |
| 428 | fsh->fifos = f; |
| 429 | } |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 430 | fsh->n_active_fifos++; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 431 | |
Florin Coras | b082324 | 2018-04-27 09:46:46 -0700 | [diff] [blame] | 432 | done: |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 433 | ssvm_unlock_non_recursive (sh); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 434 | return (f); |
| 435 | } |
| 436 | |
| 437 | void |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 438 | svm_fifo_segment_free_fifo (svm_fifo_segment_private_t * s, svm_fifo_t * f, |
| 439 | svm_fifo_segment_freelist_t list_index) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 440 | { |
| 441 | ssvm_shared_header_t *sh; |
| 442 | svm_fifo_segment_header_t *fsh; |
| 443 | void *oldheap; |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 444 | int freelist_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 445 | |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 446 | ASSERT (f->refcnt > 0); |
| 447 | |
| 448 | if (--f->refcnt > 0) |
| 449 | return; |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 450 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 451 | sh = s->ssvm.sh; |
| 452 | fsh = (svm_fifo_segment_header_t *) sh->opaque[0]; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 453 | |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 454 | freelist_index = f->freelist_index; |
| 455 | |
Dave Barach | b7f1faa | 2017-08-29 11:43:37 -0400 | [diff] [blame] | 456 | ASSERT (freelist_index < vec_len (fsh->free_fifos)); |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 457 | |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 458 | ssvm_lock_non_recursive (sh, 2); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 459 | oldheap = ssvm_push_heap (sh); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 460 | |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 461 | switch (list_index) |
| 462 | { |
| 463 | case FIFO_SEGMENT_RX_FREELIST: |
| 464 | /* Remove from active list */ |
| 465 | if (f->prev) |
| 466 | f->prev->next = f->next; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 467 | else |
| 468 | fsh->fifos = f->next; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 469 | if (f->next) |
| 470 | f->next->prev = f->prev; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 471 | /* Fall through: we add only rx fifos to active pool */ |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 472 | case FIFO_SEGMENT_TX_FREELIST: |
| 473 | /* Add to free list */ |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 474 | f->next = fsh->free_fifos[freelist_index]; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 475 | f->prev = 0; |
Dave Barach | 818eb54 | 2017-08-02 13:56:13 -0400 | [diff] [blame] | 476 | fsh->free_fifos[freelist_index] = f; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 477 | break; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 478 | case FIFO_SEGMENT_FREELIST_NONE: |
| 479 | break; |
| 480 | |
| 481 | default: |
| 482 | clib_warning ("ignore bogus freelist %d", list_index); |
| 483 | break; |
| 484 | } |
| 485 | |
Florin Coras | 6881062 | 2017-07-24 17:40:28 -0700 | [diff] [blame] | 486 | if (CLIB_DEBUG) |
| 487 | { |
| 488 | f->master_session_index = ~0; |
| 489 | f->master_thread_index = ~0; |
| 490 | } |
| 491 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 492 | fsh->n_active_fifos--; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 493 | ssvm_pop_heap (oldheap); |
Dave Barach | 2c25a62 | 2017-06-26 11:35:07 -0400 | [diff] [blame] | 494 | ssvm_unlock_non_recursive (sh); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | void |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 498 | svm_fifo_segment_main_init (u64 baseva, u32 timeout_in_seconds) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 499 | { |
| 500 | svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; |
| 501 | |
| 502 | sm->next_baseva = baseva; |
| 503 | sm->timeout_in_seconds = timeout_in_seconds; |
| 504 | } |
| 505 | |
| 506 | u32 |
| 507 | svm_fifo_segment_index (svm_fifo_segment_private_t * s) |
| 508 | { |
| 509 | return s - svm_fifo_segment_main.segments; |
| 510 | } |
| 511 | |
Florin Coras | c87c91d | 2017-08-16 19:55:49 -0700 | [diff] [blame] | 512 | /** |
| 513 | * Retrieve svm segments pool. Used only for debug purposes. |
| 514 | */ |
| 515 | svm_fifo_segment_private_t * |
| 516 | svm_fifo_segment_segments_pool (void) |
| 517 | { |
| 518 | svm_fifo_segment_main_t *sm = &svm_fifo_segment_main; |
| 519 | return sm->segments; |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Get number of active fifos |
| 524 | */ |
| 525 | u32 |
| 526 | svm_fifo_segment_num_fifos (svm_fifo_segment_private_t * fifo_segment) |
| 527 | { |
| 528 | return fifo_segment->h->n_active_fifos; |
| 529 | } |
| 530 | |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 531 | u32 |
| 532 | svm_fifo_segment_num_free_fifos (svm_fifo_segment_private_t * fifo_segment, |
| 533 | u32 fifo_size_in_bytes) |
| 534 | { |
| 535 | ssvm_shared_header_t *sh; |
| 536 | svm_fifo_segment_header_t *fsh; |
| 537 | svm_fifo_t *f; |
| 538 | int i; |
| 539 | u32 count = 0, rounded_data_size, freelist_index; |
| 540 | |
| 541 | sh = fifo_segment->ssvm.sh; |
| 542 | fsh = (svm_fifo_segment_header_t *) sh->opaque[0]; |
| 543 | |
| 544 | /* Count all free fifos? */ |
| 545 | if (fifo_size_in_bytes == ~0) |
| 546 | { |
| 547 | for (i = 0; i < vec_len (fsh->free_fifos); i++) |
| 548 | { |
| 549 | f = fsh->free_fifos[i]; |
| 550 | if (f == 0) |
| 551 | continue; |
| 552 | |
| 553 | while (f) |
| 554 | { |
| 555 | f = f->next; |
| 556 | count++; |
| 557 | } |
| 558 | } |
| 559 | return count; |
| 560 | } |
| 561 | |
| 562 | rounded_data_size = (1 << (max_log2 (fifo_size_in_bytes))); |
| 563 | freelist_index = max_log2 (rounded_data_size) |
| 564 | - max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE); |
| 565 | |
Eyal Bari | cd30774 | 2018-07-22 12:45:15 +0300 | [diff] [blame] | 566 | if (freelist_index >= vec_len (fsh->free_fifos)) |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 567 | return 0; |
| 568 | |
| 569 | f = fsh->free_fifos[freelist_index]; |
| 570 | if (f == 0) |
| 571 | return 0; |
| 572 | |
| 573 | while (f) |
| 574 | { |
| 575 | f = f->next; |
| 576 | count++; |
| 577 | } |
| 578 | return count; |
| 579 | } |
| 580 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 581 | void |
| 582 | svm_fifo_segment_info (svm_fifo_segment_private_t * seg, uword * address, |
| 583 | u64 * size) |
| 584 | { |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 585 | if (ssvm_type (&seg->ssvm) == SSVM_SEGMENT_PRIVATE) |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 586 | { |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 587 | #if USE_DLMALLOC == 0 |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 588 | mheap_t *heap_header; |
| 589 | |
| 590 | *address = pointer_to_uword (seg->ssvm.sh->heap); |
| 591 | heap_header = mheap_header (seg->ssvm.sh->heap); |
| 592 | *size = heap_header->max_size; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 593 | #else |
| 594 | mspace_get_address_and_size (seg->ssvm.sh->heap, |
| 595 | (unsigned long long *) address, |
| 596 | (unsigned long long *) size); |
| 597 | #endif |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 598 | } |
| 599 | else |
| 600 | { |
| 601 | *address = seg->ssvm.sh->ssvm_va; |
| 602 | *size = seg->ssvm.ssvm_size; |
| 603 | } |
| 604 | } |
| 605 | |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 606 | void * |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 607 | svm_fifo_segment_heap (svm_fifo_segment_private_t * seg) |
| 608 | { |
| 609 | return seg->ssvm.sh->heap; |
| 610 | } |
| 611 | |
| 612 | u8 * |
| 613 | format_svm_fifo_segment_type (u8 * s, va_list * args) |
| 614 | { |
| 615 | svm_fifo_segment_private_t *sp; |
| 616 | sp = va_arg (*args, svm_fifo_segment_private_t *); |
| 617 | ssvm_segment_type_t st = ssvm_type (&sp->ssvm); |
| 618 | |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame] | 619 | if (st == SSVM_SEGMENT_PRIVATE) |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 620 | s = format (s, "%s", "private-heap"); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 621 | else if (st == SSVM_SEGMENT_MEMFD) |
| 622 | s = format (s, "%s", "memfd"); |
| 623 | else if (st == SSVM_SEGMENT_SHM) |
| 624 | s = format (s, "%s", "shm"); |
| 625 | else |
| 626 | s = format (s, "%s", "unknown"); |
| 627 | return s; |
| 628 | } |
| 629 | |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 630 | /** |
| 631 | * Segment format function |
| 632 | */ |
| 633 | u8 * |
| 634 | format_svm_fifo_segment (u8 * s, va_list * args) |
| 635 | { |
| 636 | svm_fifo_segment_private_t *sp |
| 637 | = va_arg (*args, svm_fifo_segment_private_t *); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 638 | int verbose __attribute__ ((unused)) = va_arg (*args, int); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 639 | svm_fifo_segment_header_t *fsh = sp->h; |
| 640 | u32 count, indent; |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 641 | svm_fifo_t *f; |
| 642 | int i; |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 643 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 644 | indent = format_get_indent (s) + 2; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 645 | #if USE_DLMALLOC == 0 |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 646 | s = format (s, "%U segment heap: %U\n", format_white_space, indent, |
| 647 | format_mheap, svm_fifo_segment_heap (sp), verbose); |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 648 | s = format (s, "%U segment has %u active fifos\n", |
| 649 | format_white_space, indent, svm_fifo_segment_num_fifos (sp)); |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 650 | #endif |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 651 | |
| 652 | for (i = 0; i < vec_len (fsh->free_fifos); i++) |
| 653 | { |
| 654 | f = fsh->free_fifos[i]; |
| 655 | if (f == 0) |
| 656 | continue; |
| 657 | count = 0; |
| 658 | while (f) |
| 659 | { |
| 660 | f = f->next; |
| 661 | count++; |
| 662 | } |
| 663 | |
| 664 | s = format (s, "%U%-5u Kb: %u free", |
| 665 | format_white_space, indent + 2, |
| 666 | 1 << (i + max_log2 (FIFO_SEGMENT_MIN_FIFO_SIZE) - 10), |
| 667 | count); |
| 668 | } |
| 669 | return s; |
| 670 | } |
| 671 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 672 | /* |
| 673 | * fd.io coding-style-patch-verification: ON |
| 674 | * |
| 675 | * Local Variables: |
| 676 | * eval: (c-set-style "gnu") |
| 677 | * End: |
| 678 | */ |