1.1 --- a/Makefile Mon Feb 01 01:04:30 2021 +0100
1.2 +++ b/Makefile Wed Feb 03 00:15:16 2021 +0100
1.3 @@ -46,6 +46,7 @@
1.4 opener_resource.cc opener_context_resource.cc \
1.5 page_mapper.cc page_queue.cc pager.cc pages.cc paging.cc \
1.6 region.cc resource_server.cc simple_pager.cc \
1.7 + host_file_accessor.cc host_file_opener.cc \
1.8 test_file_accessor.cc test_file_opener.cc \
1.9
1.10 # Normal definitions.
2.1 --- a/dstest_client.cc Mon Feb 01 01:04:30 2021 +0100
2.2 +++ b/dstest_client.cc Wed Feb 03 00:15:16 2021 +0100
2.3 @@ -36,8 +36,26 @@
2.4
2.5
2.6
2.7 -int main(void)
2.8 +int main(int argc, char *argv[])
2.9 {
2.10 + if (argc < 4)
2.11 + {
2.12 + printf("Need filename, step and sample size.\n");
2.13 + return 1;
2.14 + }
2.15 +
2.16 + /* Obtain filename and access parameters. */
2.17 +
2.18 + char *filename = argv[1];
2.19 + unsigned long step = atoi(argv[2]);
2.20 + unsigned long sample = atoi(argv[3]);
2.21 +
2.22 + /* Allocate a buffer for sampling from the file. */
2.23 +
2.24 + char buf[sample + 1];
2.25 +
2.26 + /* Obtain access to the filesystem. */
2.27 +
2.28 l4_cap_idx_t server = l4re_env_get_cap("server");
2.29 l4_cap_idx_t context_ref;
2.30 client_Opener opener(server);
2.31 @@ -80,7 +98,7 @@
2.32
2.33 /* Write the filename. */
2.34
2.35 - strcpy(memory, "123");
2.36 + strcpy(memory, filename);
2.37
2.38 /* Invoke the open method to receive the file reference. */
2.39
2.40 @@ -110,6 +128,9 @@
2.41 return 1;
2.42 }
2.43
2.44 + printf("Mapped region from %ld to %ld with content %ld.\n",
2.45 + start_pos, end_pos, data_end);
2.46 +
2.47 size = end_pos - start_pos;
2.48
2.49 printf("Attach region of size %ld...\n", size);
2.50 @@ -124,12 +145,17 @@
2.51
2.52 printf("Mapped memory at %p\n", memory);
2.53
2.54 - for (unsigned long offset = 0; offset < size; offset += 1024)
2.55 + if (data_end < size)
2.56 + size = data_end;
2.57 +
2.58 + for (unsigned long offset = 0; offset < size; offset += step)
2.59 {
2.60 - char buf[11];
2.61 - printf("10 bytes from %p...\n", (memory + offset));
2.62 - strncpy(buf, (memory + offset), 10);
2.63 - buf[10] = '\0';
2.64 + unsigned long remaining = size - offset;
2.65 + unsigned long sample_remaining = remaining < sample ? remaining : sample;
2.66 +
2.67 + printf("%ld bytes from %p...\n", sample_remaining, (memory + offset));
2.68 + strncpy(buf, (memory + offset), sample_remaining);
2.69 + buf[sample_remaining] = '\0';
2.70 printf("%s\n", buf);
2.71 }
2.72
3.1 --- a/dstest_server.cc Mon Feb 01 01:04:30 2021 +0100
3.2 +++ b/dstest_server.cc Wed Feb 03 00:15:16 2021 +0100
3.3 @@ -32,7 +32,8 @@
3.4 #include "pages.h"
3.5 #include "paging.h"
3.6 #include "resource_server.h"
3.7 -#include "test_file_opener.h"
3.8 +#include "host_file_opener.h"
3.9 +// #include "test_file_opener.h"
3.10
3.11
3.12
3.13 @@ -47,7 +48,8 @@
3.14 Accessing accessing;
3.15 Paging paging;
3.16 Pages pages(&mem);
3.17 - TestFileOpener opener(&accessing, &paging, &pages, page(FILE_PAGES));
3.18 + HostFileOpener opener(&accessing, &paging, &pages);
3.19 + // TestFileOpener opener(&accessing, &paging, &pages, page(FILE_PAGES));
3.20
3.21 /* Register a server associating it with the given object. */
3.22
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/host_file_accessor.cc Wed Feb 03 00:15:16 2021 +0100
4.3 @@ -0,0 +1,68 @@
4.4 +#include <stdio.h>
4.5 +#include <string.h>
4.6 +#include <sys/types.h>
4.7 +#include <sys/stat.h>
4.8 +
4.9 +#include "host_file_accessor.h"
4.10 +
4.11 +HostFileAccessor::HostFileAccessor(const char *path, fileid_t fileid)
4.12 +: Accessor(fileid), _path(path)
4.13 +{
4.14 + /* Initialise the size of the file. */
4.15 +
4.16 + struct stat buf;
4.17 +
4.18 + if (!stat(_path, &buf))
4.19 + _size = buf.st_size;
4.20 + else
4.21 + _size = 0;
4.22 +}
4.23 +
4.24 +/* Perform any closing operation on the file. */
4.25 +
4.26 +void HostFileAccessor::close()
4.27 +{
4.28 + fclose(_fp);
4.29 +}
4.30 +
4.31 +/* Perform any opening operation on the file. */
4.32 +
4.33 +void HostFileAccessor::open()
4.34 +{
4.35 + _fp = fopen(_path, "r");
4.36 +}
4.37 +
4.38 +/* Data transfer methods. */
4.39 +
4.40 +void HostFileAccessor::fill(Flexpage *flexpage)
4.41 +{
4.42 + /* Obtain the offset and seek in the file. */
4.43 +
4.44 + offset_t filepos = flexpage->base_offset;
4.45 +
4.46 + fseek(_fp, filepos, SEEK_SET);
4.47 +
4.48 + /* Tag the region with file state. */
4.49 +
4.50 + flexpage->region->fill(fileid, filepos);
4.51 +
4.52 + /* Fill the region with file content. */
4.53 +
4.54 + offset_t addr = flexpage->base_addr;
4.55 +
4.56 + size_t nread = fread((void *) addr, sizeof(char), flexpage->size, _fp);
4.57 +
4.58 + /* Pad the flexpage with zero. */
4.59 +
4.60 + memset((void *) (addr + nread), 0, flexpage->size - nread);
4.61 +}
4.62 +
4.63 +void HostFileAccessor::flush(Flexpage *flexpage)
4.64 +{
4.65 + /* NOTE: A real flush operation would store the memory content and update
4.66 + the file size, if appropriate. */
4.67 +
4.68 + flexpage->region->flush();
4.69 +}
4.70 +
4.71 +// vim: tabstop=4 expandtab shiftwidth=4
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/host_file_accessor.h Wed Feb 03 00:15:16 2021 +0100
5.3 @@ -0,0 +1,30 @@
5.4 +#pragma once
5.5 +
5.6 +#include <stdio.h>
5.7 +
5.8 +#include "accessor.h"
5.9 +
5.10 +/* A host filesystem file accessor, providing flexpages corresponding to file
5.11 + regions. */
5.12 +
5.13 +class HostFileAccessor : public Accessor
5.14 +{
5.15 +protected:
5.16 + const char *_path;
5.17 + FILE *_fp;
5.18 +
5.19 +public:
5.20 + explicit HostFileAccessor(const char *path, fileid_t fileid);
5.21 +
5.22 + virtual void close();
5.23 +
5.24 + virtual void open();
5.25 +
5.26 + /* Data transfer methods. */
5.27 +
5.28 + virtual void fill(Flexpage *flexpage);
5.29 +
5.30 + virtual void flush(Flexpage *flexpage);
5.31 +};
5.32 +
5.33 +// vim: tabstop=4 expandtab shiftwidth=4
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/host_file_opener.cc Wed Feb 03 00:15:16 2021 +0100
6.3 @@ -0,0 +1,34 @@
6.4 +#include <sys/stat.h>
6.5 +
6.6 +#include "host_file_accessor.h"
6.7 +#include "host_file_opener.h"
6.8 +
6.9 +/* Return a file identifier for the given 'path'. */
6.10 +
6.11 +fileid_t HostFileOpener::get_fileid(const char *path)
6.12 +{
6.13 + struct stat statbuf;
6.14 +
6.15 + /* Obtain the inode number.
6.16 + NOTE: This does not handle errors! */
6.17 +
6.18 + stat(path, &statbuf);
6.19 +
6.20 + _paths.insert(FilePathEntry(statbuf.st_ino, path));
6.21 +
6.22 + return statbuf.st_ino;
6.23 +}
6.24 +
6.25 +/* Return a new accessor for 'fileid'. */
6.26 +
6.27 +Accessor *HostFileOpener::make_accessor(fileid_t fileid)
6.28 +{
6.29 + FilePaths::iterator found = _paths.find(fileid);
6.30 +
6.31 + if (found != _paths.end())
6.32 + return new HostFileAccessor(found->second, fileid);
6.33 + else
6.34 + return NULL;
6.35 +}
6.36 +
6.37 +// vim: tabstop=4 expandtab shiftwidth=4
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/host_file_opener.h Wed Feb 03 00:15:16 2021 +0100
7.3 @@ -0,0 +1,36 @@
7.4 +#pragma once
7.5 +
7.6 +#include <map>
7.7 +
7.8 +#include "opener_resource.h"
7.9 +
7.10 +
7.11 +
7.12 +/* Collection types. */
7.13 +
7.14 +typedef std::map<fileid_t, const char *> FilePaths;
7.15 +typedef std::pair<fileid_t, const char *> FilePathEntry;
7.16 +
7.17 +
7.18 +
7.19 +/* Support for providing access to files. */
7.20 +
7.21 +class HostFileOpener : public OpenerResource
7.22 +{
7.23 +protected:
7.24 + FilePaths _paths;
7.25 +
7.26 + /* Configurable methods. */
7.27 +
7.28 + virtual fileid_t get_fileid(const char *path);
7.29 +
7.30 + virtual Accessor *make_accessor(fileid_t fileid);
7.31 +
7.32 +public:
7.33 + explicit HostFileOpener(Accessing *accessing, Paging *paging, Pages *pages)
7.34 + : OpenerResource(accessing, paging, pages)
7.35 + {
7.36 + }
7.37 +};
7.38 +
7.39 +// vim: tabstop=4 expandtab shiftwidth=4