paul@18 | 1 | /* |
paul@18 | 2 | * Test dataspace operations. |
paul@18 | 3 | * |
paul@507 | 4 | * Copyright (C) 2020, 2021, 2022, 2023 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@507 | 25 | #include <systypes/env.h> |
paul@85 | 26 | #include <systypes/fcntl.h> |
paul@455 | 27 | #include <systypes/format.h> |
paul@455 | 28 | |
paul@455 | 29 | #define FMToffsetd "%" pFMToffset "d" |
paul@85 | 30 | |
paul@18 | 31 | #include <stdio.h> |
paul@18 | 32 | #include <string.h> |
paul@18 | 33 | #include <stdlib.h> |
paul@39 | 34 | #include <time.h> |
paul@18 | 35 | |
paul@41 | 36 | #include <system_error> |
paul@18 | 37 | #include <thread> |
paul@18 | 38 | |
paul@29 | 39 | #include <ipc/thread.h> |
paul@18 | 40 | |
paul@94 | 41 | #include <fsclient/file.h> |
paul@94 | 42 | #include <mem/memory_utils.h> |
paul@18 | 43 | |
paul@18 | 44 | |
paul@18 | 45 | |
paul@21 | 46 | /* Test parameters affected by capability limits. */ |
paul@21 | 47 | |
paul@18 | 48 | const unsigned long NUMBER_OF_FILES = 10; |
paul@18 | 49 | const unsigned int START_LIMIT = 10; |
paul@21 | 50 | const unsigned int ACTIVITY_ITERATIONS = 20; |
paul@21 | 51 | |
paul@21 | 52 | /* Test parameters unaffected by any capability limits. */ |
paul@21 | 53 | |
paul@18 | 54 | const unsigned int MAP_PAGES = 20; |
paul@41 | 55 | const unsigned int REGION_ITERATIONS = 20; |
paul@18 | 56 | |
paul@18 | 57 | |
paul@18 | 58 | |
paul@21 | 59 | /* An activity opening and reading from a file. */ |
paul@18 | 60 | |
paul@35 | 61 | static long activity(file_t *context, unsigned long fileid, unsigned int start_page) |
paul@18 | 62 | { |
paul@18 | 63 | unsigned long step = page(1); |
paul@18 | 64 | unsigned long sample = page(1); |
paul@18 | 65 | |
paul@18 | 66 | /* Allocate a buffer for sampling from the file. */ |
paul@18 | 67 | |
paul@18 | 68 | char buf[sample + 1]; |
paul@18 | 69 | |
paul@18 | 70 | /* Invoke the open method to receive the file reference. */ |
paul@18 | 71 | |
paul@30 | 72 | file_t file; |
paul@18 | 73 | |
paul@85 | 74 | long err = file_context_open(&file, O_RDONLY, context); |
paul@18 | 75 | |
paul@18 | 76 | if (err) |
paul@18 | 77 | { |
paul@18 | 78 | printf("Could not obtain file for %ld @ page %d: %s\n", fileid, start_page, l4sys_errtostr(err)); |
paul@18 | 79 | return err; |
paul@18 | 80 | } |
paul@18 | 81 | |
paul@34 | 82 | /* A region of the file is mapped. */ |
paul@18 | 83 | |
paul@339 | 84 | err = file_mmap(&file, page(start_page), page(MAP_PAGES), 0, 0, file_region_flags(file.flags)); |
paul@18 | 85 | |
paul@18 | 86 | if (err) |
paul@18 | 87 | { |
paul@18 | 88 | printf("Could not map file region for %ld @ page %d: %s\n", fileid, start_page, l4sys_errtostr(err)); |
paul@30 | 89 | file_close(&file); |
paul@18 | 90 | return err; |
paul@18 | 91 | } |
paul@18 | 92 | |
paul@21 | 93 | /* Read the region a number of times. */ |
paul@18 | 94 | |
paul@21 | 95 | for (unsigned int read_counter = 0; read_counter < REGION_ITERATIONS; read_counter++) |
paul@21 | 96 | { |
paul@45 | 97 | for (unsigned long offset = 0; offset < file_populated_span(&file); offset += step) |
paul@21 | 98 | { |
paul@45 | 99 | unsigned long remaining = file_populated_span(&file) - offset; |
paul@21 | 100 | unsigned long sample_remaining = remaining < sample ? remaining : sample; |
paul@18 | 101 | |
paul@30 | 102 | strncpy(buf, (file.memory + offset), sample_remaining); |
paul@21 | 103 | buf[sample_remaining] = '\0'; |
paul@21 | 104 | |
paul@21 | 105 | /* Test the data obtained. */ |
paul@18 | 106 | |
paul@422 | 107 | offset_t filepos = file.start_pos + offset; |
paul@422 | 108 | unsigned long _fileid = 0; |
paul@422 | 109 | offset_t _filepos = 0; |
paul@21 | 110 | char *sep = strchr(buf, ':'); |
paul@18 | 111 | |
paul@21 | 112 | if (sep != NULL) |
paul@21 | 113 | { |
paul@21 | 114 | *sep = '\0'; sep++; |
paul@422 | 115 | _fileid = atol(buf); _filepos = atoll(sep); |
paul@21 | 116 | } |
paul@21 | 117 | |
paul@21 | 118 | if ((fileid != _fileid) || (filepos != _filepos)) |
paul@455 | 119 | printf("! %ld:" FMToffsetd " is not %ld:" FMToffsetd "\n", _fileid, _filepos, fileid, filepos); |
paul@18 | 120 | } |
paul@18 | 121 | } |
paul@18 | 122 | |
paul@30 | 123 | file_close(&file); |
paul@18 | 124 | |
paul@18 | 125 | return L4_EOK; |
paul@18 | 126 | } |
paul@18 | 127 | |
paul@18 | 128 | |
paul@18 | 129 | |
paul@35 | 130 | static long activity_iterate(file_t *context, unsigned long fileid, unsigned int start_page) |
paul@18 | 131 | { |
paul@18 | 132 | long err; |
paul@18 | 133 | |
paul@18 | 134 | /* Open the file, read pages, close the file, over and over. */ |
paul@18 | 135 | |
paul@18 | 136 | for (unsigned int iteration = 0; iteration < ACTIVITY_ITERATIONS; iteration++) |
paul@18 | 137 | { |
paul@35 | 138 | err = activity(context, fileid, start_page); |
paul@18 | 139 | if (err) |
paul@18 | 140 | break; |
paul@18 | 141 | } |
paul@18 | 142 | |
paul@18 | 143 | return err; |
paul@18 | 144 | } |
paul@18 | 145 | |
paul@18 | 146 | |
paul@18 | 147 | |
paul@30 | 148 | static long context_for_file(unsigned long fileid, file_t *file, l4_cap_idx_t server) |
paul@18 | 149 | { |
paul@33 | 150 | long err = file_context(file, server); |
paul@18 | 151 | |
paul@18 | 152 | if (err) |
paul@18 | 153 | { |
paul@18 | 154 | printf("Could not obtain context: %s\n", l4sys_errtostr(err)); |
paul@30 | 155 | file_close(file); |
paul@18 | 156 | return err; |
paul@18 | 157 | } |
paul@18 | 158 | |
paul@18 | 159 | /* Write the filename. */ |
paul@18 | 160 | |
paul@30 | 161 | sprintf(file->memory, "%ld", fileid); |
paul@18 | 162 | |
paul@18 | 163 | return L4_EOK; |
paul@18 | 164 | } |
paul@18 | 165 | |
paul@18 | 166 | |
paul@18 | 167 | |
paul@18 | 168 | int main(void) |
paul@18 | 169 | { |
paul@44 | 170 | long err; |
paul@44 | 171 | |
paul@18 | 172 | /* Introduce concurrency control. */ |
paul@18 | 173 | |
paul@44 | 174 | err = ipc_thread_init(); |
paul@44 | 175 | |
paul@44 | 176 | if (err) |
paul@44 | 177 | { |
paul@44 | 178 | printf("Initialisation error: %s\n", l4sys_errtostr(err)); |
paul@44 | 179 | return 1; |
paul@44 | 180 | } |
paul@18 | 181 | |
paul@30 | 182 | /* Retain activity and context details. */ |
paul@30 | 183 | |
paul@28 | 184 | std::thread *activities[NUMBER_OF_FILES * START_LIMIT]; |
paul@30 | 185 | file_t contexts[NUMBER_OF_FILES]; |
paul@30 | 186 | |
paul@30 | 187 | /* Obtain access to the filesystem. */ |
paul@30 | 188 | |
paul@507 | 189 | l4_cap_idx_t server = l4re_env_get_cap(ENV_FILESYSTEM_SERVER_NAME); |
paul@28 | 190 | |
paul@28 | 191 | /* Obtain opener contexts for the files. */ |
paul@18 | 192 | |
paul@18 | 193 | unsigned long fileid; |
paul@28 | 194 | |
paul@28 | 195 | for (fileid = 0; fileid < NUMBER_OF_FILES; fileid++) |
paul@28 | 196 | { |
paul@44 | 197 | err = context_for_file(fileid, &contexts[fileid], server); |
paul@28 | 198 | |
paul@28 | 199 | if (err) |
paul@28 | 200 | { |
paul@28 | 201 | printf("Context allocation failed.\n"); |
paul@28 | 202 | return 1; |
paul@28 | 203 | } |
paul@28 | 204 | } |
paul@28 | 205 | |
paul@28 | 206 | /* Start threads accessing all the files. */ |
paul@28 | 207 | |
paul@41 | 208 | printf("Starting threads...\n"); |
paul@41 | 209 | |
paul@39 | 210 | time_t t = time(NULL); |
paul@18 | 211 | int current = 0; |
paul@41 | 212 | unsigned long errors = 0; |
paul@18 | 213 | |
paul@18 | 214 | for (fileid = 0; fileid < NUMBER_OF_FILES; fileid++) |
paul@41 | 215 | { |
paul@28 | 216 | for (unsigned int start_page = 0; start_page < START_LIMIT; start_page++) |
paul@41 | 217 | { |
paul@41 | 218 | try |
paul@41 | 219 | { |
paul@41 | 220 | activities[current++] = new std::thread(activity_iterate, &contexts[fileid], fileid, start_page); |
paul@41 | 221 | } |
paul@41 | 222 | catch (const std::system_error &exc) |
paul@41 | 223 | { |
paul@41 | 224 | errors++; |
paul@41 | 225 | } |
paul@41 | 226 | } |
paul@41 | 227 | } |
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...\n", limit); |
paul@18 | 234 | |
paul@18 | 235 | for (current = 0; current < limit; current++) |
paul@28 | 236 | activities[current]->join(); |
paul@28 | 237 | |
paul@28 | 238 | /* Discard the contexts. */ |
paul@28 | 239 | |
paul@28 | 240 | for (fileid = 0; fileid < NUMBER_OF_FILES; fileid++) |
paul@30 | 241 | file_close(&contexts[fileid]); |
paul@18 | 242 | |
paul@39 | 243 | t = time(NULL) - t; |
paul@41 | 244 | printf("Activities completed in %ld seconds (with %ld threads not started).\n", t, errors); |
paul@402 | 245 | printf("All threads started: %s\n", errors ? "False" : "True"); |
paul@402 | 246 | printf("End of test.\n"); |
paul@18 | 247 | |
paul@18 | 248 | return 0; |
paul@18 | 249 | } |
paul@31 | 250 | |
paul@31 | 251 | // vim: tabstop=2 expandtab shiftwidth=2 |