-
Notifications
You must be signed in to change notification settings - Fork 524
/
Copy pathmmap_data_loader.h
134 lines (118 loc) · 4.26 KB
/
mmap_data_loader.h
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <executorch/runtime/core/data_loader.h>
#include <executorch/runtime/core/result.h>
#include <executorch/runtime/platform/compiler.h>
namespace executorch {
namespace extension {
/**
* A DataLoader that loads segments from a file, allocating the memory
* with `malloc()`.
*
* Note that this will keep the file open for the duration of its lifetime, to
* avoid the overhead of opening it again for every load() call.
*/
class MmapDataLoader final : public executorch::runtime::DataLoader {
public:
/**
* Describes how and whether to lock loaded pages with `mlock()`.
*
* Using `mlock()` typically loads all of the pages immediately, and will
* typically ensure that they are not swapped out. The actual behavior
* will depend on the host system.
*/
enum class MlockConfig {
/// Do not call `mlock()` on loaded pages.
NoMlock,
/// Call `mlock()` on loaded pages, failing if it fails.
UseMlock,
/// Call `mlock()` on loaded pages, ignoring errors if it fails.
UseMlockIgnoreErrors,
};
/**
* Creates a new MmapDataLoader that wraps the named file. Fails if
* the file can't be opened for reading or if its size can't be found.
*
* @param[in] file_name The path to the file to load from. The file will be
* kept open until the MmapDataLoader is destroyed, to avoid the
* overhead of opening it again for every load() call.
* @param[in] mlock_config How and whether to lock loaded pages with
* `mlock()`.
*/
static executorch::runtime::Result<MmapDataLoader> from(
const char* file_name,
MlockConfig mlock_config = MlockConfig::UseMlock);
/// DEPRECATED: Use the lowercase `from()` instead.
ET_DEPRECATED static executorch::runtime::Result<MmapDataLoader> From(
const char* file_name,
MlockConfig mlock_config = MlockConfig::UseMlock) {
return from(file_name, mlock_config);
}
/// DEPRECATED: Use the version of `from()` that takes an MlockConfig.
ET_DEPRECATED
static executorch::runtime::Result<MmapDataLoader> From(
const char* file_name,
bool use_mlock) {
MlockConfig mlock_config =
use_mlock ? MlockConfig::UseMlock : MlockConfig::NoMlock;
return from(file_name, mlock_config);
}
// Movable to be compatible with Result.
MmapDataLoader(MmapDataLoader&& rhs) noexcept
: file_name_(rhs.file_name_),
file_size_(rhs.file_size_),
page_size_(rhs.page_size_),
fd_(rhs.fd_),
mlock_config_(rhs.mlock_config_) {
const_cast<const char*&>(rhs.file_name_) = nullptr;
const_cast<size_t&>(rhs.file_size_) = 0;
const_cast<size_t&>(rhs.page_size_) = 0;
const_cast<int&>(rhs.fd_) = -1;
const_cast<MlockConfig&>(rhs.mlock_config_) = MlockConfig::NoMlock;
}
~MmapDataLoader() override;
ET_NODISCARD
executorch::runtime::Result<executorch::runtime::FreeableBuffer> load(
size_t offset,
size_t size,
const DataLoader::SegmentInfo& segment_info) const override;
ET_NODISCARD executorch::runtime::Result<size_t> size() const override;
private:
MmapDataLoader(
int fd,
size_t file_size,
const char* file_name,
size_t page_size,
MlockConfig mlock_config)
: file_name_(file_name),
file_size_(file_size),
page_size_(page_size),
fd_(fd),
mlock_config_(mlock_config) {}
// Not safely copyable.
MmapDataLoader(const MmapDataLoader&) = delete;
MmapDataLoader& operator=(const MmapDataLoader&) = delete;
MmapDataLoader& operator=(MmapDataLoader&&) = delete;
const char* const file_name_; // String data is owned by the instance.
const size_t file_size_;
const size_t page_size_;
const int fd_; // Owned by the instance.
const MlockConfig mlock_config_;
};
} // namespace extension
} // namespace executorch
namespace torch {
namespace executor {
namespace util {
// TODO(T197294990): Remove these deprecated aliases once all users have moved
// to the new `::executorch` namespaces.
using ::executorch::extension::MmapDataLoader;
} // namespace util
} // namespace executor
} // namespace torch