forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmman.h
More file actions
140 lines (121 loc) · 3.31 KB
/
Copy pathmman.h
File metadata and controls
140 lines (121 loc) · 3.31 KB
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
135
136
137
138
139
140
/*
* 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.
*/
// This file ensures that mman.h compatible functions are defined in the global
// namespace for windows and posix environments.
#pragma once
#include <executorch/runtime/platform/compiler.h>
#include <sys/stat.h>
#include <cstdint>
#ifndef _WIN32
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
ET_INLINE size_t get_os_page_size() {
return sysconf(_SC_PAGESIZE);
}
/**
* Platform-specific file stat function.
*/
ET_INLINE int get_file_stat(int fd, size_t* out_size) {
struct stat st;
int err = ::fstat(fd, &st);
if (err >= 0) {
*out_size = static_cast<size_t>(st.st_size);
}
return err;
}
/**
* Platform-specific mmap offset type conversion.
*/
ET_INLINE off_t get_mmap_offset(size_t offset) {
return static_cast<off_t>(offset);
}
/**
* Hint the kernel to prefetch pages eagerly and to optimize for sequential
* reads. Intended to reduce page-fault stutter during model initialization
* when the caller does not want to mlock the pages into RAM.
*
* MADV_WILLNEED / MADV_SEQUENTIAL are absent on some POSIX libcs (e.g. the
* Hexagon DSP toolchain).
*/
ET_INLINE void madvise_pages_willneed_sequential(void* addr, size_t len) {
#ifdef MADV_WILLNEED
::madvise(addr, len, MADV_WILLNEED);
#endif
#ifdef MADV_SEQUENTIAL
::madvise(addr, len, MADV_SEQUENTIAL);
#endif
}
/**
* On Apple platforms, schedule kernel read-ahead on the file descriptor itself
* via fcntl(F_RDADVISE). This is more aggressive than madvise for cold starts:
* it brings pages into the unified buffer cache so first-touch faults are
* serviced from RAM instead of storage. No-op on non-Apple POSIX platforms.
*/
ET_INLINE void fcntl_rdadvise_apple(int fd, size_t file_size) {
#if defined(__APPLE__)
struct radvisory advice;
advice.ra_offset = 0;
advice.ra_count = static_cast<int>(file_size);
::fcntl(fd, F_RDADVISE, &advice);
#else
(void)fd;
(void)file_size;
#endif
}
#else
#ifndef NOMINMAX
#define NOMINMAX
#include <windows.h>
#undef NOMINMAX
#else
#include <windows.h>
#endif
#include <io.h>
#include <executorch/extension/data_loader/mman_windows.h>
ET_INLINE long get_os_page_size() {
SYSTEM_INFO si;
GetSystemInfo(&si);
long pagesize = si.dwAllocationGranularity > si.dwPageSize
? si.dwAllocationGranularity
: si.dwPageSize;
return pagesize;
}
/**
* Platform-specific file stat function.
*/
ET_INLINE int get_file_stat(int fd, size_t* out_size) {
struct _stat64 st;
int err = ::_fstat64(fd, &st);
if (err >= 0) {
*out_size = static_cast<size_t>(st.st_size);
}
return err;
}
/**
* Platform-specific mmap offset type conversion.
*/
ET_INLINE uint64_t get_mmap_offset(size_t offset) {
return static_cast<uint64_t>(offset);
}
/**
* No-op on Windows: there is no direct equivalent to madvise(MADV_WILLNEED |
* MADV_SEQUENTIAL) and the existing mman_windows shim does not implement one.
*/
ET_INLINE void madvise_pages_willneed_sequential(void* addr, size_t len) {
(void)addr;
(void)len;
}
/**
* No-op on Windows: F_RDADVISE is an Apple-specific fcntl command.
*/
ET_INLINE void fcntl_rdadvise_apple(int fd, size_t file_size) {
(void)fd;
(void)file_size;
}
#endif