1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tests/dstest_test_client.cc Thu Apr 01 00:57:43 2021 +0200
1.3 @@ -0,0 +1,242 @@
1.4 +/*
1.5 + * Test dataspace operations.
1.6 + *
1.7 + * Copyright (C) 2020, 2021 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include <l4/re/env.h>
1.26 +#include <l4/sys/err.h>
1.27 +
1.28 +#include <stdio.h>
1.29 +#include <string.h>
1.30 +#include <stdlib.h>
1.31 +#include <time.h>
1.32 +
1.33 +#include <system_error>
1.34 +#include <thread>
1.35 +
1.36 +#include <ipc/thread.h>
1.37 +
1.38 +#include "file.h"
1.39 +#include "memory_utils.h"
1.40 +
1.41 +
1.42 +
1.43 +/* Test parameters affected by capability limits. */
1.44 +
1.45 +const unsigned long NUMBER_OF_FILES = 10;
1.46 +const unsigned int START_LIMIT = 10;
1.47 +const unsigned int ACTIVITY_ITERATIONS = 20;
1.48 +
1.49 +/* Test parameters unaffected by any capability limits. */
1.50 +
1.51 +const unsigned int MAP_PAGES = 20;
1.52 +const unsigned int REGION_ITERATIONS = 20;
1.53 +
1.54 +
1.55 +
1.56 +/* An activity opening and reading from a file. */
1.57 +
1.58 +static long activity(file_t *context, unsigned long fileid, unsigned int start_page)
1.59 +{
1.60 + unsigned long step = page(1);
1.61 + unsigned long sample = page(1);
1.62 +
1.63 + /* Allocate a buffer for sampling from the file. */
1.64 +
1.65 + char buf[sample + 1];
1.66 +
1.67 + /* Invoke the open method to receive the file reference. */
1.68 +
1.69 + file_t file;
1.70 +
1.71 + long err = file_context_open(&file, context);
1.72 +
1.73 + if (err)
1.74 + {
1.75 + printf("Could not obtain file for %ld @ page %d: %s\n", fileid, start_page, l4sys_errtostr(err));
1.76 + return err;
1.77 + }
1.78 +
1.79 + /* A region of the file is mapped. */
1.80 +
1.81 + err = file_mmap(&file, page(start_page), page(MAP_PAGES));
1.82 +
1.83 + if (err)
1.84 + {
1.85 + printf("Could not map file region for %ld @ page %d: %s\n", fileid, start_page, l4sys_errtostr(err));
1.86 + file_close(&file);
1.87 + return err;
1.88 + }
1.89 +
1.90 + /* Read the region a number of times. */
1.91 +
1.92 + for (unsigned int read_counter = 0; read_counter < REGION_ITERATIONS; read_counter++)
1.93 + {
1.94 + for (unsigned long offset = 0; offset < file_populated_span(&file); offset += step)
1.95 + {
1.96 + unsigned long remaining = file_populated_span(&file) - offset;
1.97 + unsigned long sample_remaining = remaining < sample ? remaining : sample;
1.98 +
1.99 + strncpy(buf, (file.memory + offset), sample_remaining);
1.100 + buf[sample_remaining] = '\0';
1.101 +
1.102 + /* Test the data obtained. */
1.103 +
1.104 + unsigned long filepos = file.start_pos + offset;
1.105 + unsigned long _fileid = 0, _filepos = 0;
1.106 + char *sep = strchr(buf, ':');
1.107 +
1.108 + if (sep != NULL)
1.109 + {
1.110 + *sep = '\0'; sep++;
1.111 + _fileid = atol(buf); _filepos = atol(sep);
1.112 + }
1.113 +
1.114 + if ((fileid != _fileid) || (filepos != _filepos))
1.115 + printf("! %ld:%ld is not %ld:%ld\n", _fileid, _filepos, fileid, filepos);
1.116 + }
1.117 + }
1.118 +
1.119 + file_close(&file);
1.120 +
1.121 + return L4_EOK;
1.122 +}
1.123 +
1.124 +
1.125 +
1.126 +static long activity_iterate(file_t *context, unsigned long fileid, unsigned int start_page)
1.127 +{
1.128 + long err;
1.129 +
1.130 + /* Open the file, read pages, close the file, over and over. */
1.131 +
1.132 + for (unsigned int iteration = 0; iteration < ACTIVITY_ITERATIONS; iteration++)
1.133 + {
1.134 + err = activity(context, fileid, start_page);
1.135 + if (err)
1.136 + break;
1.137 + }
1.138 +
1.139 + return err;
1.140 +}
1.141 +
1.142 +
1.143 +
1.144 +static long context_for_file(unsigned long fileid, file_t *file, l4_cap_idx_t server)
1.145 +{
1.146 + long err = file_context(file, server);
1.147 +
1.148 + if (err)
1.149 + {
1.150 + printf("Could not obtain context: %s\n", l4sys_errtostr(err));
1.151 + file_close(file);
1.152 + return err;
1.153 + }
1.154 +
1.155 + /* Write the filename. */
1.156 +
1.157 + sprintf(file->memory, "%ld", fileid);
1.158 +
1.159 + return L4_EOK;
1.160 +}
1.161 +
1.162 +
1.163 +
1.164 +int main(void)
1.165 +{
1.166 + long err;
1.167 +
1.168 + /* Introduce concurrency control. */
1.169 +
1.170 + err = ipc_thread_init();
1.171 +
1.172 + if (err)
1.173 + {
1.174 + printf("Initialisation error: %s\n", l4sys_errtostr(err));
1.175 + return 1;
1.176 + }
1.177 +
1.178 + /* Retain activity and context details. */
1.179 +
1.180 + std::thread *activities[NUMBER_OF_FILES * START_LIMIT];
1.181 + file_t contexts[NUMBER_OF_FILES];
1.182 +
1.183 + /* Obtain access to the filesystem. */
1.184 +
1.185 + l4_cap_idx_t server = l4re_env_get_cap("server");
1.186 +
1.187 + /* Obtain opener contexts for the files. */
1.188 +
1.189 + unsigned long fileid;
1.190 +
1.191 + for (fileid = 0; fileid < NUMBER_OF_FILES; fileid++)
1.192 + {
1.193 + err = context_for_file(fileid, &contexts[fileid], server);
1.194 +
1.195 + if (err)
1.196 + {
1.197 + printf("Context allocation failed.\n");
1.198 + return 1;
1.199 + }
1.200 + }
1.201 +
1.202 + /* Start threads accessing all the files. */
1.203 +
1.204 + printf("Starting threads...\n");
1.205 +
1.206 + time_t t = time(NULL);
1.207 + int current = 0;
1.208 + unsigned long errors = 0;
1.209 +
1.210 + for (fileid = 0; fileid < NUMBER_OF_FILES; fileid++)
1.211 + {
1.212 + for (unsigned int start_page = 0; start_page < START_LIMIT; start_page++)
1.213 + {
1.214 + try
1.215 + {
1.216 + activities[current++] = new std::thread(activity_iterate, &contexts[fileid], fileid, start_page);
1.217 + }
1.218 + catch (const std::system_error &exc)
1.219 + {
1.220 + errors++;
1.221 + }
1.222 + }
1.223 + }
1.224 +
1.225 + /* Wait for the threads. */
1.226 +
1.227 + int limit = current;
1.228 +
1.229 + printf("Waiting for %d threads...\n", limit);
1.230 +
1.231 + for (current = 0; current < limit; current++)
1.232 + activities[current]->join();
1.233 +
1.234 + /* Discard the contexts. */
1.235 +
1.236 + for (fileid = 0; fileid < NUMBER_OF_FILES; fileid++)
1.237 + file_close(&contexts[fileid]);
1.238 +
1.239 + t = time(NULL) - t;
1.240 + printf("Activities completed in %ld seconds (with %ld threads not started).\n", t, errors);
1.241 +
1.242 + return 0;
1.243 +}
1.244 +
1.245 +// vim: tabstop=2 expandtab shiftwidth=2