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