paul@18 | 1 | /* |
paul@18 | 2 | * Test dataspace operations. |
paul@18 | 3 | * |
paul@18 | 4 | * Copyright (C) 2020, 2021 Paul Boddie <paul@boddie.org.uk> |
paul@18 | 5 | * |
paul@18 | 6 | * This program is free software; you can redistribute it and/or |
paul@18 | 7 | * modify it under the terms of the GNU General Public License as |
paul@18 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@18 | 9 | * the License, or (at your option) any later version. |
paul@18 | 10 | * |
paul@18 | 11 | * This program is distributed in the hope that it will be useful, |
paul@18 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@18 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@18 | 14 | * GNU General Public License for more details. |
paul@18 | 15 | * |
paul@18 | 16 | * You should have received a copy of the GNU General Public License |
paul@18 | 17 | * along with this program; if not, write to the Free Software |
paul@18 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@18 | 19 | * Boston, MA 02110-1301, USA |
paul@18 | 20 | */ |
paul@18 | 21 | |
paul@18 | 22 | #include <l4/re/env.h> |
paul@18 | 23 | #include <l4/sys/err.h> |
paul@18 | 24 | |
paul@18 | 25 | #include <stdio.h> |
paul@18 | 26 | #include <string.h> |
paul@18 | 27 | #include <stdlib.h> |
paul@18 | 28 | |
paul@18 | 29 | #include <thread> |
paul@18 | 30 | |
paul@18 | 31 | #include <ipc/cap_alloc.h> |
paul@18 | 32 | #include <ipc/mem_ipc.h> |
paul@18 | 33 | |
paul@18 | 34 | #include "dataspace_client.h" |
paul@18 | 35 | #include "opener_client.h" |
paul@18 | 36 | #include "opener_context_client.h" |
paul@18 | 37 | #include "mapped_file_client.h" |
paul@18 | 38 | #include "memory_utils.h" |
paul@18 | 39 | |
paul@18 | 40 | |
paul@18 | 41 | |
paul@21 | 42 | /* Test parameters affected by capability limits. */ |
paul@21 | 43 | |
paul@18 | 44 | const unsigned long NUMBER_OF_FILES = 10; |
paul@18 | 45 | const unsigned int START_LIMIT = 10; |
paul@21 | 46 | const unsigned int ACTIVITY_ITERATIONS = 20; |
paul@21 | 47 | |
paul@21 | 48 | /* Test parameters unaffected by any capability limits. */ |
paul@21 | 49 | |
paul@18 | 50 | const unsigned int MAP_PAGES = 20; |
paul@21 | 51 | const unsigned int REGION_ITERATIONS = 10; |
paul@18 | 52 | |
paul@18 | 53 | |
paul@18 | 54 | |
paul@21 | 55 | /* An activity opening and reading from a file. */ |
paul@18 | 56 | |
paul@18 | 57 | static long activity(l4_cap_idx_t context_ref, unsigned long fileid, unsigned int start_page) |
paul@18 | 58 | { |
paul@18 | 59 | client_OpenerContext context(context_ref); |
paul@18 | 60 | unsigned long step = page(1); |
paul@18 | 61 | unsigned long sample = page(1); |
paul@18 | 62 | |
paul@18 | 63 | /* Allocate a buffer for sampling from the file. */ |
paul@18 | 64 | |
paul@18 | 65 | char buf[sample + 1]; |
paul@18 | 66 | |
paul@18 | 67 | /* Invoke the open method to receive the file reference. */ |
paul@18 | 68 | |
paul@18 | 69 | unsigned long size; |
paul@18 | 70 | l4_cap_idx_t file_ref; |
paul@18 | 71 | |
paul@18 | 72 | long err = context.open(L4_FPAGE_RW, &size, &file_ref); |
paul@18 | 73 | |
paul@18 | 74 | if (err) |
paul@18 | 75 | { |
paul@18 | 76 | printf("Could not obtain file for %ld @ page %d: %s\n", fileid, start_page, l4sys_errtostr(err)); |
paul@18 | 77 | return err; |
paul@18 | 78 | } |
paul@18 | 79 | |
paul@18 | 80 | client_MappedFile file(file_ref); |
paul@18 | 81 | |
paul@18 | 82 | /* Some memory to be mapped. */ |
paul@18 | 83 | |
paul@18 | 84 | size_t start_pos, end_pos, data_end; |
paul@18 | 85 | |
paul@18 | 86 | err = file.mmap(page(start_page), page(MAP_PAGES), &start_pos, &end_pos, &data_end); |
paul@18 | 87 | |
paul@18 | 88 | if (err) |
paul@18 | 89 | { |
paul@18 | 90 | printf("Could not map file region for %ld @ page %d: %s\n", fileid, start_page, l4sys_errtostr(err)); |
paul@18 | 91 | ipc_cap_free_um(file_ref); |
paul@18 | 92 | return err; |
paul@18 | 93 | } |
paul@18 | 94 | |
paul@18 | 95 | size = end_pos - start_pos; |
paul@18 | 96 | |
paul@18 | 97 | char *memory; |
paul@18 | 98 | |
paul@21 | 99 | err = ipc_attach_dataspace(file_ref, size, (void **) &memory); |
paul@18 | 100 | |
paul@18 | 101 | if (err) |
paul@18 | 102 | { |
paul@18 | 103 | printf("Could not map memory for %ld @ page %d: %s\n", fileid, start_page, l4sys_errtostr(err)); |
paul@18 | 104 | ipc_cap_free_um(file_ref); |
paul@18 | 105 | return err; |
paul@18 | 106 | } |
paul@18 | 107 | |
paul@18 | 108 | if (data_end < size) |
paul@18 | 109 | size = data_end; |
paul@18 | 110 | |
paul@21 | 111 | /* Read the region a number of times. */ |
paul@18 | 112 | |
paul@21 | 113 | for (unsigned int read_counter = 0; read_counter < REGION_ITERATIONS; read_counter++) |
paul@21 | 114 | { |
paul@21 | 115 | for (unsigned long offset = 0; offset < size; offset += step) |
paul@21 | 116 | { |
paul@21 | 117 | unsigned long remaining = size - offset; |
paul@21 | 118 | unsigned long sample_remaining = remaining < sample ? remaining : sample; |
paul@18 | 119 | |
paul@21 | 120 | strncpy(buf, (memory + offset), sample_remaining); |
paul@21 | 121 | buf[sample_remaining] = '\0'; |
paul@21 | 122 | |
paul@21 | 123 | /* Test the data obtained. */ |
paul@18 | 124 | |
paul@21 | 125 | unsigned long filepos = start_pos + offset; |
paul@21 | 126 | unsigned long _fileid = 0, _filepos = 0; |
paul@21 | 127 | char *sep = strchr(buf, ':'); |
paul@18 | 128 | |
paul@21 | 129 | if (sep != NULL) |
paul@21 | 130 | { |
paul@21 | 131 | *sep = '\0'; sep++; |
paul@21 | 132 | _fileid = atol(buf); _filepos = atol(sep); |
paul@21 | 133 | } |
paul@21 | 134 | |
paul@21 | 135 | if ((fileid != _fileid) || (filepos != _filepos)) |
paul@21 | 136 | printf("! %ld:%ld is not %ld:%ld\n", _fileid, _filepos, fileid, filepos); |
paul@18 | 137 | } |
paul@18 | 138 | } |
paul@18 | 139 | |
paul@27 | 140 | ipc_cap_free_um(file_ref); |
paul@21 | 141 | ipc_detach_dataspace(memory); |
paul@18 | 142 | |
paul@18 | 143 | return L4_EOK; |
paul@18 | 144 | } |
paul@18 | 145 | |
paul@18 | 146 | |
paul@18 | 147 | |
paul@18 | 148 | static long activity_iterate(l4_cap_idx_t context_ref, unsigned long fileid, unsigned int start_page) |
paul@18 | 149 | { |
paul@18 | 150 | long err; |
paul@18 | 151 | |
paul@18 | 152 | /* Open the file, read pages, close the file, over and over. */ |
paul@18 | 153 | |
paul@18 | 154 | for (unsigned int iteration = 0; iteration < ACTIVITY_ITERATIONS; iteration++) |
paul@18 | 155 | { |
paul@18 | 156 | err = activity(context_ref, fileid, start_page); |
paul@18 | 157 | if (err) |
paul@18 | 158 | break; |
paul@18 | 159 | } |
paul@18 | 160 | |
paul@18 | 161 | return err; |
paul@18 | 162 | } |
paul@18 | 163 | |
paul@18 | 164 | |
paul@18 | 165 | |
paul@18 | 166 | static long context_for_file(unsigned long fileid, l4_cap_idx_t *context_ref, char **filename) |
paul@18 | 167 | { |
paul@18 | 168 | /* Obtain access to the filesystem. */ |
paul@18 | 169 | |
paul@18 | 170 | l4_cap_idx_t server = l4re_env_get_cap("server"); |
paul@18 | 171 | client_Opener opener(server); |
paul@18 | 172 | |
paul@18 | 173 | long err = opener.context(context_ref); |
paul@18 | 174 | |
paul@18 | 175 | if (err) |
paul@18 | 176 | { |
paul@18 | 177 | printf("Could not obtain context: %s\n", l4sys_errtostr(err)); |
paul@18 | 178 | return err; |
paul@18 | 179 | } |
paul@18 | 180 | |
paul@18 | 181 | client_Dataspace context_ds(*context_ref); |
paul@18 | 182 | unsigned long size, flags; |
paul@18 | 183 | |
paul@18 | 184 | err = context_ds.info(&size, &flags); |
paul@18 | 185 | |
paul@18 | 186 | if (err) |
paul@18 | 187 | { |
paul@18 | 188 | printf("Could not obtain context info: %s\n", l4sys_errtostr(err)); |
paul@18 | 189 | ipc_cap_free_um(*context_ref); |
paul@18 | 190 | return err; |
paul@18 | 191 | } |
paul@18 | 192 | |
paul@18 | 193 | /* Map context memory to write the filename. */ |
paul@18 | 194 | |
paul@21 | 195 | err = ipc_attach_dataspace(*context_ref, size, (void **) filename); |
paul@18 | 196 | |
paul@18 | 197 | if (err) |
paul@18 | 198 | { |
paul@18 | 199 | printf("Could not map memory: %s\n", l4sys_errtostr(err)); |
paul@18 | 200 | ipc_cap_free_um(*context_ref); |
paul@18 | 201 | return err; |
paul@18 | 202 | } |
paul@18 | 203 | |
paul@18 | 204 | /* Write the filename. */ |
paul@18 | 205 | |
paul@18 | 206 | sprintf(*filename, "%ld", fileid); |
paul@18 | 207 | |
paul@18 | 208 | return L4_EOK; |
paul@18 | 209 | } |
paul@18 | 210 | |
paul@18 | 211 | |
paul@18 | 212 | |
paul@18 | 213 | static long activity_for_file(unsigned long fileid) |
paul@18 | 214 | { |
paul@18 | 215 | std::thread *activities[START_LIMIT]; |
paul@18 | 216 | |
paul@18 | 217 | l4_cap_idx_t context_ref; |
paul@18 | 218 | char *filename; |
paul@18 | 219 | int current = 0; |
paul@18 | 220 | |
paul@18 | 221 | long err = context_for_file(fileid, &context_ref, &filename); |
paul@18 | 222 | |
paul@18 | 223 | if (err) |
paul@18 | 224 | return err; |
paul@18 | 225 | |
paul@18 | 226 | for (unsigned int start_page = 0; start_page < START_LIMIT; start_page++) |
paul@18 | 227 | activities[current++] = new std::thread(activity_iterate, context_ref, fileid, start_page); |
paul@18 | 228 | |
paul@18 | 229 | /* Wait for the threads. */ |
paul@18 | 230 | |
paul@18 | 231 | int limit = current; |
paul@18 | 232 | |
paul@18 | 233 | printf("Waiting for %d threads for %ld...\n", limit, fileid); |
paul@18 | 234 | |
paul@18 | 235 | for (current = 0; current < limit; current++) |
paul@18 | 236 | { |
paul@18 | 237 | activities[current]->join(); |
paul@18 | 238 | printf("End: %ld @ %d\n", fileid, current); |
paul@18 | 239 | } |
paul@18 | 240 | |
paul@18 | 241 | /* Discard the context. */ |
paul@18 | 242 | |
paul@27 | 243 | ipc_cap_free_um(context_ref); |
paul@21 | 244 | ipc_detach_dataspace(filename); |
paul@18 | 245 | |
paul@18 | 246 | printf("End: %ld\n", fileid); |
paul@18 | 247 | return L4_EOK; |
paul@18 | 248 | } |
paul@18 | 249 | |
paul@18 | 250 | |
paul@18 | 251 | |
paul@18 | 252 | int main(void) |
paul@18 | 253 | { |
paul@18 | 254 | /* Introduce concurrency control. */ |
paul@18 | 255 | |
paul@18 | 256 | ipc_cap_alloc_init(); |
paul@18 | 257 | |
paul@18 | 258 | std::thread *activities[NUMBER_OF_FILES]; |
paul@18 | 259 | |
paul@18 | 260 | unsigned long fileid; |
paul@18 | 261 | int current = 0; |
paul@18 | 262 | |
paul@18 | 263 | for (fileid = 0; fileid < NUMBER_OF_FILES; fileid++) |
paul@18 | 264 | activities[current++] = new std::thread(activity_for_file, fileid); |
paul@18 | 265 | |
paul@18 | 266 | /* Wait for the threads. */ |
paul@18 | 267 | |
paul@18 | 268 | int limit = current; |
paul@18 | 269 | |
paul@18 | 270 | printf("Waiting for %d threads...\n", limit); |
paul@18 | 271 | |
paul@18 | 272 | for (current = 0; current < limit; current++) |
paul@18 | 273 | { |
paul@18 | 274 | activities[current]->join(); |
paul@18 | 275 | printf("Ended: %d\n", current); |
paul@18 | 276 | } |
paul@18 | 277 | |
paul@18 | 278 | printf("Activities completed.\n"); |
paul@18 | 279 | |
paul@18 | 280 | return 0; |
paul@18 | 281 | } |