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