changeset 103e2f92c965 in /z/repo/gem5
details: http://repo.gem5.org/gem5?cmd=changeset;node=103e2f92c965
description:
        syscall_emul: [patch 10/22] refactor fdentry and add fdarray class

        Several large changes happen in this patch.

        The FDEntry class is rewritten so that file descriptors now correspond 
to
        types: 'File' which is normal file-backed file with the file open on the
        host machine, 'Pipe' which is a pipe that has been opened on the host 
machine,
        and 'Device' which does not have an open file on the host yet acts as a 
pseudo
        device with which to issue ioctls. Other types which might be added in 
the
        future are directory entries and sockets (off the top of my head).

        The FDArray class was create to hold most of the file descriptor 
handling
        that was stuffed into the Process class. It uses shared pointers and
        the std::array type to hold the FDEntries mentioned above.

        The changes to these two classes needed to be propagated out to the rest
        of the code so there were quite a few changes for that. Also, comments 
were
        added where I thought they were needed to help others and extend our
        DOxygen coverage.

diffstat:

 src/gpu-compute/cl_driver.cc |   11 +-
 src/sim/SConscript           |    1 +
 src/sim/fd_array.cc          |  344 +++++++++++++++++++++++++++++++++++++++++++
 src/sim/fd_array.hh          |  148 ++++++++++++++++++
 src/sim/fd_entry.cc          |  139 +++++++++--------
 src/sim/fd_entry.hh          |  242 ++++++++++++++++++++++-------
 src/sim/process.cc           |  276 +++-------------------------------
 src/sim/process.hh           |   49 +-----
 src/sim/syscall_emul.cc      |  184 ++++++++++++----------
 src/sim/syscall_emul.hh      |  154 +++++++++---------
 10 files changed, 965 insertions(+), 583 deletions(-)

diffs (truncated from 2070 to 300 lines):

diff -r c706f4ab5dd7 -r 103e2f92c965 src/gpu-compute/cl_driver.cc
--- a/src/gpu-compute/cl_driver.cc      Wed Nov 09 14:27:42 2016 -0600
+++ b/src/gpu-compute/cl_driver.cc      Wed Nov 09 14:27:42 2016 -0600
@@ -35,6 +35,8 @@
 
 #include "gpu-compute/cl_driver.hh"
 
+#include <memory>
+
 #include "base/intmath.hh"
 #include "cpu/thread_context.hh"
 #include "gpu-compute/dispatcher.hh"
@@ -93,11 +95,10 @@
 int
 ClDriver::open(Process *p, ThreadContext *tc, int mode, int flags)
 {
-    int fd = p->allocFD(-1, filename, 0, 0, false);
-    FDEntry *fde = p->getFDEntry(fd);
-    fde->driver = this;
-
-    return fd;
+    std::shared_ptr<DeviceFDEntry> fdp;
+    fdp = std::make_shared<DeviceFDEntry>(this, filename);
+    int tgt_fd = p->fds->allocFD(fdp);
+    return tgt_fd;
 }
 
 int
diff -r c706f4ab5dd7 -r 103e2f92c965 src/sim/SConscript
--- a/src/sim/SConscript        Wed Nov 09 14:27:42 2016 -0600
+++ b/src/sim/SConscript        Wed Nov 09 14:27:42 2016 -0600
@@ -78,6 +78,7 @@
     Source('aux_vector.cc')
     Source('faults.cc')
     Source('process.cc')
+    Source('fd_array.cc')
     Source('fd_entry.cc')
     Source('pseudo_inst.cc')
     Source('syscall_emul.cc')
diff -r c706f4ab5dd7 -r 103e2f92c965 src/sim/fd_array.cc
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/src/sim/fd_array.cc       Wed Nov 09 14:27:42 2016 -0600
@@ -0,0 +1,344 @@
+/*
+ * Copyright (c) 2016 Advanced Micro Devices, Inc.
+ * All rights reserved.
+ *
+ * For use for simulation and test purposes only
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of the copyright holder nor the names of its
+ * contributors may be used to endorse or promote products derived from this
+ * software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Author: Brandon Potter
+ */
+
+#include "sim/fd_array.hh"
+
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <array>
+#include <memory>
+#include <string>
+
+#include "base/misc.hh"
+#include "params/Process.hh"
+#include "sim/fd_entry.hh"
+
+FDArray::FDArray(std::string const& input, std::string const& output,
+                 std::string const& errout)
+    : _input(input), _output(output), _errout(errout), _fdArray(),
+      imap {{"",       -1},
+            {"cin",    STDIN_FILENO},
+            {"stdin",  STDIN_FILENO}},
+      oemap{{"",       -1},
+            {"cout",   STDOUT_FILENO},
+            {"stdout", STDOUT_FILENO},
+            {"cerr",   STDERR_FILENO},
+            {"stderr", STDERR_FILENO}}
+{
+    int sim_fd;
+    std::map<std::string, int>::iterator it;
+
+    /**
+     * Search through the input options and setup the default fd if match is
+     * found; otherwise, open an input file and seek to location.
+     */
+    if ((it = imap.find(input)) != imap.end())
+        sim_fd = it->second;
+    else
+        sim_fd = openInputFile(input);
+
+    auto ffd = std::make_shared<FileFDEntry>(sim_fd, O_RDONLY, input, false);
+    _fdArray[STDIN_FILENO] = ffd;
+
+    /**
+     * Search through the output/error options and setup the default fd if
+     * match is found; otherwise, open an output file and seek to location.
+     */
+    if ((it = oemap.find(output)) != oemap.end())
+        sim_fd = it->second;
+    else
+        sim_fd = openOutputFile(output);
+
+    ffd = std::make_shared<FileFDEntry>(sim_fd, O_WRONLY | O_CREAT | O_TRUNC,
+                                        output, false);
+    _fdArray[STDOUT_FILENO] = ffd;
+
+    if (output == errout)
+        ; /* Reuse the same file descriptor if these match. */
+    else if ((it = oemap.find(errout)) != oemap.end())
+        sim_fd = it->second;
+    else
+        sim_fd = openOutputFile(errout);
+
+    ffd = std::make_shared<FileFDEntry>(sim_fd, O_WRONLY | O_CREAT | O_TRUNC,
+                                        errout, false);
+    _fdArray[STDERR_FILENO] = ffd;
+}
+
+void
+FDArray::updateFileOffsets()
+{
+    for (auto& fdp : _fdArray) {
+        /**
+         * It only makes sense to check the offsets if the file descriptor
+         * type is 'File' (which indicates that this file is backed by a
+         * file on the host). If the type is File, then record the offset.
+         */
+        auto ffd = std::dynamic_pointer_cast<FileFDEntry>(fdp);
+
+        if (!ffd)
+            continue;
+
+        /**
+         * Use lseek with SEEK_CUR with offset 0 to figure out where the
+         * offset currently resides and pass that back to our setter.
+         */
+        int sim_fd = ffd->getSimFD();
+        ffd->setFileOffset(lseek(sim_fd, 0, SEEK_CUR));
+    }
+}
+
+void
+FDArray::restoreFileOffsets()
+{
+    /**
+     * Use this lambda to highlight what we mean to do with the seek.
+     * Notice that this either seeks correctly (sets the file location on the
+     * host) or it fails with a fatal. The error is fatal because it's not
+     * possible to guarantee that the simulation will proceed as it should
+     * have in the same way that it would have proceeded sans checkpoints.
+     */
+    void (*seek)(std::shared_ptr<FileFDEntry>)
+        = [] (std::shared_ptr<FileFDEntry> ffd)
+    {
+        if (lseek(ffd->getSimFD(), ffd->getFileOffset(), SEEK_SET) < 0)
+            fatal("Unable to seek to location in %s", ffd->getFileName());
+    };
+
+    std::map<std::string, int>::iterator it;
+
+    /**
+     * Search through the input options and set fd if match is found;
+     * otherwise, open an input file and seek to location.
+     * Check if user has specified a different input file, and if so, use it
+     * instead of the file specified in the checkpoint. This also resets the
+     * file offset from the checkpointed value
+     */
+    std::shared_ptr<FDEntry> stdin_fde = _fdArray[STDIN_FILENO];
+    auto stdin_ffd = std::dynamic_pointer_cast<FileFDEntry>(stdin_fde);
+
+    if (_input != stdin_ffd->getFileName()) {
+        warn("Using new input file (%s) rather than checkpointed (%s)\n",
+             _input, stdin_ffd->getFileName());
+        stdin_ffd->setFileName(_input);
+        stdin_ffd->setFileOffset(0);
+    }
+
+    if ((it = imap.find(stdin_ffd->getFileName())) != imap.end()) {
+        stdin_ffd->setSimFD(it->second);
+    } else {
+        stdin_ffd->setSimFD(openInputFile(stdin_ffd->getFileName()));
+        seek(stdin_ffd);
+    }
+
+    /**
+     * Search through the output options and set fd if match is found;
+     * otherwise, open an output file and seek to location.
+     * Check if user has specified a different output file, and if so, use it
+     * instead of the file specified in the checkpoint. This also resets the
+     * file offset from the checkpointed value
+     */
+    std::shared_ptr<FDEntry> stdout_fde = _fdArray[STDOUT_FILENO];
+    auto stdout_ffd = std::dynamic_pointer_cast<FileFDEntry>(stdout_fde);
+
+    if (_output != stdout_ffd->getFileName()) {
+        warn("Using new output file (%s) rather than checkpointed (%s)\n",
+             _output, stdout_ffd->getFileName());
+        stdout_ffd->setFileName(_output);
+        stdout_ffd->setFileOffset(0);
+    }
+
+    if ((it = oemap.find(stdout_ffd->getFileName())) != oemap.end()) {
+        stdout_ffd->setSimFD(it->second);
+    } else {
+        stdout_ffd->setSimFD(openOutputFile(stdout_ffd->getFileName()));
+        seek(stdout_ffd);
+    }
+
+    /**
+     * Search through the error options and set fd if match is found;
+     * otherwise, open an error file and seek to location.
+     * Check if user has specified a different error file, and if so, use it
+     * instead of the file specified in the checkpoint. This also resets the
+     * file offset from the checkpointed value
+     */
+    std::shared_ptr<FDEntry> stderr_fde = _fdArray[STDERR_FILENO];
+    auto stderr_ffd = std::dynamic_pointer_cast<FileFDEntry>(stderr_fde);
+
+    if (_errout != stderr_ffd->getFileName()) {
+        warn("Using new error file (%s) rather than checkpointed (%s)\n",
+             _errout, stderr_ffd->getFileName());
+        stderr_ffd->setFileName(_errout);
+        stderr_ffd->setFileOffset(0);
+    }
+
+    if (stdout_ffd->getFileName() == stderr_ffd->getFileName()) {
+        /* Reuse the same sim_fd file descriptor if these match. */
+        stderr_ffd->setSimFD(stdout_ffd->getSimFD());
+    } else if ((it = oemap.find(stderr_ffd->getFileName())) != oemap.end()) {
+        stderr_ffd->setSimFD(it->second);
+    } else {
+        stderr_ffd->setSimFD(openOutputFile(stderr_ffd->getFileName()));
+        seek(stderr_ffd);
+    }
+
+    for (int tgt_fd = 3; tgt_fd < _fdArray.size(); tgt_fd++) {
+        std::shared_ptr<FDEntry> fdp = _fdArray[tgt_fd];
+        if (!fdp)
+            continue;
+
+        /* Need to reconnect pipe ends. */
+        if (auto pfd = std::dynamic_pointer_cast<PipeFDEntry>(fdp)) {
+            /**
+             * Check which end of the pipe we are looking at; we only want
+             * to setup the pipe once so we arbitrarily choose the read
+             * end to be the end that sets up the pipe.
+             */
+            if (pfd->getEndType() == PipeFDEntry::EndType::write)
+                continue;
+
+            /* Setup the pipe or fatal out of the simulation. */
+            int fd_pair[2];
+            if (pipe(fd_pair) < 0)
+                fatal("Unable to create new pipe");
+
+            /**
+             * Reconstruct the ends of the pipe by reassigning the pipe
+             * that we created on the host. This one is the read end.
+             */
+            pfd->setSimFD(fd_pair[0]);
+
+            /**
+             * Grab the write end by referencing the read ends source and
+             * using that tgt_fd to index the array.
+             */
+            int prs = pfd->getPipeReadSource();
+            std::shared_ptr<FDEntry> write_fdp = _fdArray[prs];
+
+            /* Now cast it and make sure that we are still sane. */
+            auto write_pfd = std::dynamic_pointer_cast<PipeFDEntry>(write_fdp);
+
+            /* Hook up the write end back to the right side of the pipe. */
+            write_pfd->setSimFD(fd_pair[1]);
+        }
+
_______________________________________________
gem5-dev mailing list
gem5-dev@gem5.org
http://m5sim.org/mailman/listinfo/gem5-dev

Reply via email to