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