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