aboutsummaryrefslogtreecommitdiffstats
path: root/vbench/src/vbench/core/mapped_file_input.cpp
blob: c4edc619e5d076eaf721f7d42b28342322b4a58e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/fastos/fastos.h>
#include "mapped_file_input.h"

namespace vbench {

MappedFileInput::MappedFileInput(const string &name)
    : _file(open(name.c_str(), O_RDONLY)),
      _data(static_cast<char*>(MAP_FAILED)),
      _size(0),
      _taint(strfmt("could not open file: %s", name.c_str())),
      _pos(0)
{
    struct stat info;
    if (_file >= 0 && fstat(_file, &info) == 0) {
        _data = static_cast<char*>(mmap(0, info.st_size,
                                        PROT_READ, MAP_SHARED, _file, 0));
        if (_data != MAP_FAILED) {
            _size = info.st_size;
            madvise(_data, _size, MADV_SEQUENTIAL);
            _taint.reset();
        }
    }
}

Memory
MappedFileInput::obtain(size_t bytes, size_t)
{
    return Memory(_data + _pos, std::min(bytes, (_size - _pos)));
}

Input &
MappedFileInput::evict(size_t bytes)
{
    assert(bytes <= (_size - _pos));
    _pos += bytes;
    return *this;
}

} // namespace vbench