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