paul@18 | 1 | /* |
paul@18 | 2 | * Test dataspace operations. |
paul@18 | 3 | * |
paul@18 | 4 | * Copyright (C) 2020, 2021 Paul Boddie <paul@boddie.org.uk> |
paul@18 | 5 | * |
paul@18 | 6 | * This program is free software; you can redistribute it and/or |
paul@18 | 7 | * modify it under the terms of the GNU General Public License as |
paul@18 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@18 | 9 | * the License, or (at your option) any later version. |
paul@18 | 10 | * |
paul@18 | 11 | * This program is distributed in the hope that it will be useful, |
paul@18 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@18 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@18 | 14 | * GNU General Public License for more details. |
paul@18 | 15 | * |
paul@18 | 16 | * You should have received a copy of the GNU General Public License |
paul@18 | 17 | * along with this program; if not, write to the Free Software |
paul@18 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@18 | 19 | * Boston, MA 02110-1301, USA |
paul@18 | 20 | */ |
paul@18 | 21 | |
paul@18 | 22 | #include <l4/re/env.h> |
paul@18 | 23 | #include <l4/sys/err.h> |
paul@18 | 24 | |
paul@18 | 25 | #include <stdio.h> |
paul@18 | 26 | #include <string.h> |
paul@18 | 27 | #include <stdlib.h> |
paul@18 | 28 | |
paul@18 | 29 | #include <thread> |
paul@18 | 30 | |
paul@29 | 31 | #include <ipc/thread.h> |
paul@18 | 32 | |
paul@31 | 33 | #include "file.h" |
paul@18 | 34 | #include "memory_utils.h" |
paul@18 | 35 | |
paul@18 | 36 | |
paul@18 | 37 | |
paul@21 | 38 | /* Test parameters affected by capability limits. */ |
paul@21 | 39 | |
paul@18 | 40 | const unsigned long NUMBER_OF_FILES = 10; |
paul@18 | 41 | const unsigned int START_LIMIT = 10; |
paul@21 | 42 | const unsigned int ACTIVITY_ITERATIONS = 20; |
paul@21 | 43 | |
paul@21 | 44 | /* Test parameters unaffected by any capability limits. */ |
paul@21 | 45 | |
paul@18 | 46 | const unsigned int MAP_PAGES = 20; |
paul@21 | 47 | const unsigned int REGION_ITERATIONS = 10; |
paul@18 | 48 | |
paul@18 | 49 | |
paul@18 | 50 | |
paul@21 | 51 | /* An activity opening and reading from a file. */ |
paul@18 | 52 | |
paul@35 | 53 | static long activity(file_t *context, unsigned long fileid, unsigned int start_page) |
paul@18 | 54 | { |
paul@18 | 55 | unsigned long step = page(1); |
paul@18 | 56 | unsigned long sample = page(1); |
paul@18 | 57 | |
paul@18 | 58 | /* Allocate a buffer for sampling from the file. */ |
paul@18 | 59 | |
paul@18 | 60 | char buf[sample + 1]; |
paul@18 | 61 | |
paul@18 | 62 | /* Invoke the open method to receive the file reference. */ |
paul@18 | 63 | |
paul@30 | 64 | file_t file; |
paul@18 | 65 | |
paul@35 | 66 | long err = file_open(&file, context); |
paul@18 | 67 | |
paul@18 | 68 | if (err) |
paul@18 | 69 | { |
paul@18 | 70 | printf("Could not obtain file for %ld @ page %d: %s\n", fileid, start_page, l4sys_errtostr(err)); |
paul@18 | 71 | return err; |
paul@18 | 72 | } |
paul@18 | 73 | |
paul@34 | 74 | /* A region of the file is mapped. */ |
paul@18 | 75 | |
paul@30 | 76 | err = file_mmap(&file, page(start_page), page(MAP_PAGES)); |
paul@18 | 77 | |
paul@18 | 78 | if (err) |
paul@18 | 79 | { |
paul@18 | 80 | printf("Could not map file region for %ld @ page %d: %s\n", fileid, start_page, l4sys_errtostr(err)); |
paul@30 | 81 | file_close(&file); |
paul@18 | 82 | return err; |
paul@18 | 83 | } |
paul@18 | 84 | |
paul@21 | 85 | /* Read the region a number of times. */ |
paul@18 | 86 | |
paul@21 | 87 | for (unsigned int read_counter = 0; read_counter < REGION_ITERATIONS; read_counter++) |
paul@21 | 88 | { |
paul@30 | 89 | for (unsigned long offset = 0; offset < file_span(&file); offset += step) |
paul@21 | 90 | { |
paul@30 | 91 | unsigned long remaining = file_span(&file) - offset; |
paul@21 | 92 | unsigned long sample_remaining = remaining < sample ? remaining : sample; |
paul@18 | 93 | |
paul@30 | 94 | strncpy(buf, (file.memory + offset), sample_remaining); |
paul@21 | 95 | buf[sample_remaining] = '\0'; |
paul@21 | 96 | |
paul@21 | 97 | /* Test the data obtained. */ |
paul@18 | 98 | |
paul@30 | 99 | unsigned long filepos = file.start_pos + offset; |
paul@21 | 100 | unsigned long _fileid = 0, _filepos = 0; |
paul@21 | 101 | char *sep = strchr(buf, ':'); |
paul@18 | 102 | |
paul@21 | 103 | if (sep != NULL) |
paul@21 | 104 | { |
paul@21 | 105 | *sep = '\0'; sep++; |
paul@21 | 106 | _fileid = atol(buf); _filepos = atol(sep); |
paul@21 | 107 | } |
paul@21 | 108 | |
paul@21 | 109 | if ((fileid != _fileid) || (filepos != _filepos)) |
paul@21 | 110 | printf("! %ld:%ld is not %ld:%ld\n", _fileid, _filepos, fileid, filepos); |
paul@18 | 111 | } |
paul@18 | 112 | } |
paul@18 | 113 | |
paul@30 | 114 | file_close(&file); |
paul@18 | 115 | |
paul@18 | 116 | return L4_EOK; |
paul@18 | 117 | } |
paul@18 | 118 | |
paul@18 | 119 | |
paul@18 | 120 | |
paul@35 | 121 | static long activity_iterate(file_t *context, unsigned long fileid, unsigned int start_page) |
paul@18 | 122 | { |
paul@18 | 123 | long err; |
paul@18 | 124 | |
paul@18 | 125 | /* Open the file, read pages, close the file, over and over. */ |
paul@18 | 126 | |
paul@18 | 127 | for (unsigned int iteration = 0; iteration < ACTIVITY_ITERATIONS; iteration++) |
paul@18 | 128 | { |
paul@35 | 129 | err = activity(context, fileid, start_page); |
paul@18 | 130 | if (err) |
paul@18 | 131 | break; |
paul@18 | 132 | } |
paul@18 | 133 | |
paul@18 | 134 | return err; |
paul@18 | 135 | } |
paul@18 | 136 | |
paul@18 | 137 | |
paul@18 | 138 | |
paul@30 | 139 | static long context_for_file(unsigned long fileid, file_t *file, l4_cap_idx_t server) |
paul@18 | 140 | { |
paul@33 | 141 | long err = file_context(file, server); |
paul@18 | 142 | |
paul@18 | 143 | if (err) |
paul@18 | 144 | { |
paul@18 | 145 | printf("Could not obtain context: %s\n", l4sys_errtostr(err)); |
paul@30 | 146 | file_close(file); |
paul@18 | 147 | return err; |
paul@18 | 148 | } |
paul@18 | 149 | |
paul@18 | 150 | /* Write the filename. */ |
paul@18 | 151 | |
paul@30 | 152 | sprintf(file->memory, "%ld", fileid); |
paul@18 | 153 | |
paul@18 | 154 | return L4_EOK; |
paul@18 | 155 | } |
paul@18 | 156 | |
paul@18 | 157 | |
paul@18 | 158 | |
paul@18 | 159 | int main(void) |
paul@18 | 160 | { |
paul@18 | 161 | /* Introduce concurrency control. */ |
paul@18 | 162 | |
paul@18 | 163 | ipc_cap_alloc_init(); |
paul@29 | 164 | ipc_mem_init(); |
paul@18 | 165 | |
paul@30 | 166 | /* Retain activity and context details. */ |
paul@30 | 167 | |
paul@28 | 168 | std::thread *activities[NUMBER_OF_FILES * START_LIMIT]; |
paul@30 | 169 | file_t contexts[NUMBER_OF_FILES]; |
paul@30 | 170 | |
paul@30 | 171 | /* Obtain access to the filesystem. */ |
paul@30 | 172 | |
paul@30 | 173 | l4_cap_idx_t server = l4re_env_get_cap("server"); |
paul@28 | 174 | |
paul@28 | 175 | /* Obtain opener contexts for the files. */ |
paul@18 | 176 | |
paul@18 | 177 | unsigned long fileid; |
paul@28 | 178 | |
paul@28 | 179 | for (fileid = 0; fileid < NUMBER_OF_FILES; fileid++) |
paul@28 | 180 | { |
paul@30 | 181 | long err = context_for_file(fileid, &contexts[fileid], server); |
paul@28 | 182 | |
paul@28 | 183 | if (err) |
paul@28 | 184 | { |
paul@28 | 185 | printf("Context allocation failed.\n"); |
paul@28 | 186 | return 1; |
paul@28 | 187 | } |
paul@28 | 188 | } |
paul@28 | 189 | |
paul@28 | 190 | /* Start threads accessing all the files. */ |
paul@28 | 191 | |
paul@18 | 192 | int current = 0; |
paul@18 | 193 | |
paul@18 | 194 | for (fileid = 0; fileid < NUMBER_OF_FILES; fileid++) |
paul@28 | 195 | for (unsigned int start_page = 0; start_page < START_LIMIT; start_page++) |
paul@30 | 196 | activities[current++] = new std::thread(activity_iterate, &contexts[fileid], fileid, start_page); |
paul@18 | 197 | |
paul@18 | 198 | /* Wait for the threads. */ |
paul@18 | 199 | |
paul@18 | 200 | int limit = current; |
paul@18 | 201 | |
paul@18 | 202 | printf("Waiting for %d threads...\n", limit); |
paul@18 | 203 | |
paul@18 | 204 | for (current = 0; current < limit; current++) |
paul@28 | 205 | activities[current]->join(); |
paul@28 | 206 | |
paul@28 | 207 | /* Discard the contexts. */ |
paul@28 | 208 | |
paul@28 | 209 | for (fileid = 0; fileid < NUMBER_OF_FILES; fileid++) |
paul@30 | 210 | file_close(&contexts[fileid]); |
paul@18 | 211 | |
paul@18 | 212 | printf("Activities completed.\n"); |
paul@18 | 213 | |
paul@18 | 214 | return 0; |
paul@18 | 215 | } |
paul@31 | 216 | |
paul@31 | 217 | // vim: tabstop=2 expandtab shiftwidth=2 |