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