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