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@29 | 33 | #include <ipc/thread.h> |
paul@18 | 34 | |
paul@18 | 35 | #include "dataspace_client.h" |
paul@18 | 36 | #include "opener_client.h" |
paul@18 | 37 | #include "opener_context_client.h" |
paul@18 | 38 | #include "mapped_file_client.h" |
paul@18 | 39 | #include "memory_utils.h" |
paul@18 | 40 | |
paul@18 | 41 | |
paul@18 | 42 | |
paul@21 | 43 | /* Test parameters affected by capability limits. */ |
paul@21 | 44 | |
paul@18 | 45 | const unsigned long NUMBER_OF_FILES = 10; |
paul@18 | 46 | const unsigned int START_LIMIT = 10; |
paul@21 | 47 | const unsigned int ACTIVITY_ITERATIONS = 20; |
paul@21 | 48 | |
paul@21 | 49 | /* Test parameters unaffected by any capability limits. */ |
paul@21 | 50 | |
paul@18 | 51 | const unsigned int MAP_PAGES = 20; |
paul@21 | 52 | const unsigned int REGION_ITERATIONS = 10; |
paul@18 | 53 | |
paul@18 | 54 | |
paul@18 | 55 | |
paul@21 | 56 | /* An activity opening and reading from a file. */ |
paul@18 | 57 | |
paul@18 | 58 | static long activity(l4_cap_idx_t context_ref, unsigned long fileid, unsigned int start_page) |
paul@18 | 59 | { |
paul@18 | 60 | client_OpenerContext context(context_ref); |
paul@18 | 61 | unsigned long step = page(1); |
paul@18 | 62 | unsigned long sample = page(1); |
paul@18 | 63 | |
paul@18 | 64 | /* Allocate a buffer for sampling from the file. */ |
paul@18 | 65 | |
paul@18 | 66 | char buf[sample + 1]; |
paul@18 | 67 | |
paul@18 | 68 | /* Invoke the open method to receive the file reference. */ |
paul@18 | 69 | |
paul@18 | 70 | unsigned long size; |
paul@18 | 71 | l4_cap_idx_t file_ref; |
paul@18 | 72 | |
paul@18 | 73 | long err = context.open(L4_FPAGE_RW, &size, &file_ref); |
paul@18 | 74 | |
paul@18 | 75 | if (err) |
paul@18 | 76 | { |
paul@18 | 77 | printf("Could not obtain file for %ld @ page %d: %s\n", fileid, start_page, l4sys_errtostr(err)); |
paul@18 | 78 | return err; |
paul@18 | 79 | } |
paul@18 | 80 | |
paul@18 | 81 | client_MappedFile file(file_ref); |
paul@18 | 82 | |
paul@18 | 83 | /* Some memory to be mapped. */ |
paul@18 | 84 | |
paul@18 | 85 | size_t start_pos, end_pos, data_end; |
paul@18 | 86 | |
paul@18 | 87 | err = file.mmap(page(start_page), page(MAP_PAGES), &start_pos, &end_pos, &data_end); |
paul@18 | 88 | |
paul@18 | 89 | if (err) |
paul@18 | 90 | { |
paul@18 | 91 | printf("Could not map file region for %ld @ page %d: %s\n", fileid, start_page, l4sys_errtostr(err)); |
paul@18 | 92 | ipc_cap_free_um(file_ref); |
paul@18 | 93 | return err; |
paul@18 | 94 | } |
paul@18 | 95 | |
paul@18 | 96 | size = end_pos - start_pos; |
paul@18 | 97 | |
paul@18 | 98 | char *memory; |
paul@18 | 99 | |
paul@21 | 100 | err = ipc_attach_dataspace(file_ref, size, (void **) &memory); |
paul@18 | 101 | |
paul@18 | 102 | if (err) |
paul@18 | 103 | { |
paul@18 | 104 | printf("Could not map memory for %ld @ page %d: %s\n", fileid, start_page, l4sys_errtostr(err)); |
paul@18 | 105 | ipc_cap_free_um(file_ref); |
paul@18 | 106 | return err; |
paul@18 | 107 | } |
paul@18 | 108 | |
paul@18 | 109 | if (data_end < size) |
paul@18 | 110 | size = data_end; |
paul@18 | 111 | |
paul@21 | 112 | /* Read the region a number of times. */ |
paul@18 | 113 | |
paul@21 | 114 | for (unsigned int read_counter = 0; read_counter < REGION_ITERATIONS; read_counter++) |
paul@21 | 115 | { |
paul@21 | 116 | for (unsigned long offset = 0; offset < size; offset += step) |
paul@21 | 117 | { |
paul@21 | 118 | unsigned long remaining = size - offset; |
paul@21 | 119 | unsigned long sample_remaining = remaining < sample ? remaining : sample; |
paul@18 | 120 | |
paul@21 | 121 | strncpy(buf, (memory + offset), sample_remaining); |
paul@21 | 122 | buf[sample_remaining] = '\0'; |
paul@21 | 123 | |
paul@21 | 124 | /* Test the data obtained. */ |
paul@18 | 125 | |
paul@21 | 126 | unsigned long filepos = start_pos + offset; |
paul@21 | 127 | unsigned long _fileid = 0, _filepos = 0; |
paul@21 | 128 | char *sep = strchr(buf, ':'); |
paul@18 | 129 | |
paul@21 | 130 | if (sep != NULL) |
paul@21 | 131 | { |
paul@21 | 132 | *sep = '\0'; sep++; |
paul@21 | 133 | _fileid = atol(buf); _filepos = atol(sep); |
paul@21 | 134 | } |
paul@21 | 135 | |
paul@21 | 136 | if ((fileid != _fileid) || (filepos != _filepos)) |
paul@21 | 137 | printf("! %ld:%ld is not %ld:%ld\n", _fileid, _filepos, fileid, filepos); |
paul@18 | 138 | } |
paul@18 | 139 | } |
paul@18 | 140 | |
paul@27 | 141 | ipc_cap_free_um(file_ref); |
paul@21 | 142 | ipc_detach_dataspace(memory); |
paul@18 | 143 | |
paul@18 | 144 | return L4_EOK; |
paul@18 | 145 | } |
paul@18 | 146 | |
paul@18 | 147 | |
paul@18 | 148 | |
paul@18 | 149 | static long activity_iterate(l4_cap_idx_t context_ref, unsigned long fileid, unsigned int start_page) |
paul@18 | 150 | { |
paul@18 | 151 | long err; |
paul@18 | 152 | |
paul@18 | 153 | /* Open the file, read pages, close the file, over and over. */ |
paul@18 | 154 | |
paul@18 | 155 | for (unsigned int iteration = 0; iteration < ACTIVITY_ITERATIONS; iteration++) |
paul@18 | 156 | { |
paul@18 | 157 | err = activity(context_ref, fileid, start_page); |
paul@18 | 158 | if (err) |
paul@18 | 159 | break; |
paul@18 | 160 | } |
paul@18 | 161 | |
paul@18 | 162 | return err; |
paul@18 | 163 | } |
paul@18 | 164 | |
paul@18 | 165 | |
paul@18 | 166 | |
paul@18 | 167 | static long context_for_file(unsigned long fileid, l4_cap_idx_t *context_ref, char **filename) |
paul@18 | 168 | { |
paul@18 | 169 | /* Obtain access to the filesystem. */ |
paul@18 | 170 | |
paul@18 | 171 | l4_cap_idx_t server = l4re_env_get_cap("server"); |
paul@18 | 172 | client_Opener opener(server); |
paul@18 | 173 | |
paul@18 | 174 | long err = opener.context(context_ref); |
paul@18 | 175 | |
paul@18 | 176 | if (err) |
paul@18 | 177 | { |
paul@18 | 178 | printf("Could not obtain context: %s\n", l4sys_errtostr(err)); |
paul@18 | 179 | return err; |
paul@18 | 180 | } |
paul@18 | 181 | |
paul@18 | 182 | client_Dataspace context_ds(*context_ref); |
paul@18 | 183 | unsigned long size, flags; |
paul@18 | 184 | |
paul@18 | 185 | err = context_ds.info(&size, &flags); |
paul@18 | 186 | |
paul@18 | 187 | if (err) |
paul@18 | 188 | { |
paul@18 | 189 | printf("Could not obtain context info: %s\n", l4sys_errtostr(err)); |
paul@18 | 190 | ipc_cap_free_um(*context_ref); |
paul@18 | 191 | return err; |
paul@18 | 192 | } |
paul@18 | 193 | |
paul@18 | 194 | /* Map context memory to write the filename. */ |
paul@18 | 195 | |
paul@21 | 196 | err = ipc_attach_dataspace(*context_ref, size, (void **) filename); |
paul@18 | 197 | |
paul@18 | 198 | if (err) |
paul@18 | 199 | { |
paul@18 | 200 | printf("Could not map memory: %s\n", l4sys_errtostr(err)); |
paul@18 | 201 | ipc_cap_free_um(*context_ref); |
paul@18 | 202 | return err; |
paul@18 | 203 | } |
paul@18 | 204 | |
paul@18 | 205 | /* Write the filename. */ |
paul@18 | 206 | |
paul@18 | 207 | sprintf(*filename, "%ld", fileid); |
paul@18 | 208 | |
paul@18 | 209 | return L4_EOK; |
paul@18 | 210 | } |
paul@18 | 211 | |
paul@18 | 212 | |
paul@18 | 213 | |
paul@18 | 214 | int main(void) |
paul@18 | 215 | { |
paul@18 | 216 | /* Introduce concurrency control. */ |
paul@18 | 217 | |
paul@18 | 218 | ipc_cap_alloc_init(); |
paul@29 | 219 | ipc_mem_init(); |
paul@18 | 220 | |
paul@28 | 221 | std::thread *activities[NUMBER_OF_FILES * START_LIMIT]; |
paul@28 | 222 | l4_cap_idx_t context_refs[NUMBER_OF_FILES]; |
paul@28 | 223 | char *filenames[NUMBER_OF_FILES]; |
paul@28 | 224 | |
paul@28 | 225 | /* Obtain opener contexts for the files. */ |
paul@18 | 226 | |
paul@18 | 227 | unsigned long fileid; |
paul@28 | 228 | |
paul@28 | 229 | for (fileid = 0; fileid < NUMBER_OF_FILES; fileid++) |
paul@28 | 230 | { |
paul@28 | 231 | long err = context_for_file(fileid, &context_refs[fileid], &filenames[fileid]); |
paul@28 | 232 | |
paul@28 | 233 | if (err) |
paul@28 | 234 | { |
paul@28 | 235 | printf("Context allocation failed.\n"); |
paul@28 | 236 | return 1; |
paul@28 | 237 | } |
paul@28 | 238 | } |
paul@28 | 239 | |
paul@28 | 240 | /* Start threads accessing all the files. */ |
paul@28 | 241 | |
paul@18 | 242 | int current = 0; |
paul@18 | 243 | |
paul@18 | 244 | for (fileid = 0; fileid < NUMBER_OF_FILES; fileid++) |
paul@28 | 245 | for (unsigned int start_page = 0; start_page < START_LIMIT; start_page++) |
paul@28 | 246 | activities[current++] = new std::thread(activity_iterate, context_refs[fileid], fileid, start_page); |
paul@18 | 247 | |
paul@18 | 248 | /* Wait for the threads. */ |
paul@18 | 249 | |
paul@18 | 250 | int limit = current; |
paul@18 | 251 | |
paul@18 | 252 | printf("Waiting for %d threads...\n", limit); |
paul@18 | 253 | |
paul@18 | 254 | for (current = 0; current < limit; current++) |
paul@28 | 255 | activities[current]->join(); |
paul@28 | 256 | |
paul@28 | 257 | /* Discard the contexts. */ |
paul@28 | 258 | |
paul@28 | 259 | for (fileid = 0; fileid < NUMBER_OF_FILES; fileid++) |
paul@18 | 260 | { |
paul@28 | 261 | ipc_cap_free_um(context_refs[fileid]); |
paul@28 | 262 | ipc_detach_dataspace(filenames[fileid]); |
paul@18 | 263 | } |
paul@18 | 264 | |
paul@18 | 265 | printf("Activities completed.\n"); |
paul@18 | 266 | |
paul@18 | 267 | return 0; |
paul@18 | 268 | } |