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