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