Skip to content

Commit b513e93

Browse files
author
lishuo121
committed
fix: format code and fix compile error on mac
1 parent db40940 commit b513e93

File tree

2 files changed

+78
-44
lines changed

2 files changed

+78
-44
lines changed

include/ylt/coro_io/coro_file.hpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ inline bool open_native_async_file(File &file, Executor &executor,
135135

136136
if (use_direct_io) {
137137
#if defined(ASIO_WINDOWS)
138-
#else
138+
#elif defined(__linux__)
139139
asio_flags = static_cast<asio::file_base::flags>(
140140
static_cast<int>(asio_flags) | O_DIRECT);
141141
#endif
@@ -149,6 +149,18 @@ inline bool open_native_async_file(File &file, Executor &executor,
149149
file = std::make_shared<asio::random_access_file>(
150150
executor.get_asio_executor(), std::string(filepath), asio_flags);
151151
}
152+
153+
// On macOS, use F_NOCACHE as an alternative to O_DIRECT
154+
if (use_direct_io && file && file->is_open()) {
155+
#if defined(__APPLE__) || defined(__MACH__)
156+
int fd = file->native_handle();
157+
if (fd >= 0 && fcntl(fd, F_NOCACHE, 1) != 0) {
158+
std::error_code ec;
159+
file->close(ec);
160+
return false;
161+
}
162+
#endif
163+
}
152164
} catch (std::exception &ex) {
153165
ELOG_INFO << "line " << __LINE__ << " coro_file open failed" << ex.what()
154166
<< "\n";
@@ -533,7 +545,7 @@ class basic_random_coro_file {
533545

534546
if (use_direct_io) {
535547
#if defined(ASIO_WINDOWS)
536-
#else
548+
#elif defined(__linux__)
537549
open_flags |= O_DIRECT;
538550
#endif
539551
}
@@ -547,6 +559,16 @@ class basic_random_coro_file {
547559
return false;
548560
}
549561

562+
// On macOS, use F_NOCACHE as an alternative to O_DIRECT
563+
if (use_direct_io) {
564+
#if defined(__APPLE__) || defined(__MACH__)
565+
if (fcntl(fd, F_NOCACHE, 1) != 0) {
566+
::close(fd);
567+
return false;
568+
}
569+
#endif
570+
}
571+
550572
prw_random_file_ = std::shared_ptr<int>(new int(fd), [](int *ptr) {
551573
#if defined(ASIO_WINDOWS)
552574
_close(*ptr);

src/coro_io/tests/test_corofile.cpp

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#if !defined(ASIO_WINDOWS)
1818
#include <fcntl.h>
1919
#include <unistd.h>
20+
2021
#include <cstdlib>
2122
#include <cstring>
2223
#endif
@@ -934,107 +935,118 @@ TEST_CASE("large_file_write_with_pool_test") {
934935
#if !defined(ASIO_WINDOWS)
935936
constexpr size_t DIRECT_IO_ALIGNMENT = 512;
936937

937-
inline void* aligned_alloc_direct_io(size_t size) {
938-
void* ptr = nullptr;
938+
inline void *aligned_alloc_direct_io(size_t size) {
939+
void *ptr = nullptr;
939940
if (posix_memalign(&ptr, DIRECT_IO_ALIGNMENT, size) != 0) {
940941
return nullptr;
941942
}
942943
return ptr;
943944
}
944945

945-
inline void aligned_free_direct_io(void* ptr) {
946-
free(ptr);
947-
}
946+
inline void aligned_free_direct_io(void *ptr) { free(ptr); }
948947

949948
inline size_t align_offset_for_direct_io(size_t offset) {
950949
return (offset / DIRECT_IO_ALIGNMENT) * DIRECT_IO_ALIGNMENT;
951950
}
952951

953952
inline size_t align_size_for_direct_io(size_t size) {
954-
return ((size + DIRECT_IO_ALIGNMENT - 1) / DIRECT_IO_ALIGNMENT) * DIRECT_IO_ALIGNMENT;
953+
return ((size + DIRECT_IO_ALIGNMENT - 1) / DIRECT_IO_ALIGNMENT) *
954+
DIRECT_IO_ALIGNMENT;
955955
}
956956

957957
template <coro_io::execution_type execute_type>
958958
void test_direct_io_read_write(std::string_view filename) {
959959
size_t file_size = 8 * KB;
960960
create_files({std::string(filename)}, file_size);
961-
961+
962962
auto executor = coro_io::get_global_block_executor();
963963
coro_io::basic_random_coro_file<execute_type> file(executor);
964-
964+
965965
bool opened = file.open(filename, std::ios::in | std::ios::out, true);
966966
if (!opened) {
967967
WARN("Direct I/O not supported on this filesystem, skipping test");
968968
return;
969969
}
970-
970+
971971
CHECK(file.is_open());
972-
972+
973973
size_t aligned_size = align_size_for_direct_io(block_size);
974-
void* aligned_buf = aligned_alloc_direct_io(aligned_size);
974+
void *aligned_buf = aligned_alloc_direct_io(aligned_size);
975975
REQUIRE(aligned_buf != nullptr);
976-
976+
977977
struct BufferGuard {
978-
void* ptr;
978+
void *ptr;
979979
~BufferGuard() {
980980
if (ptr) {
981981
aligned_free_direct_io(ptr);
982982
}
983983
}
984984
} guard{aligned_buf};
985-
986-
char* buf = static_cast<char*>(aligned_buf);
987-
985+
986+
char *buf = static_cast<char *>(aligned_buf);
987+
988988
size_t aligned_offset = align_offset_for_direct_io(0);
989989
size_t aligned_read_size = align_size_for_direct_io(block_size);
990-
991-
auto [ec, bytes_read] = async_simple::coro::syncAwait(
990+
991+
std::error_code ec;
992+
size_t bytes_read;
993+
std::tie(ec, bytes_read) = async_simple::coro::syncAwait(
992994
file.async_read_at(aligned_offset, buf, aligned_read_size));
993-
995+
994996
CHECK(!ec);
995997
CHECK(bytes_read == aligned_read_size);
996998
CHECK(!file.eof());
997-
999+
9981000
std::memset(buf, 'X', aligned_read_size);
999-
1000-
auto [write_ec, bytes_written] = async_simple::coro::syncAwait(
1001+
1002+
std::error_code write_ec;
1003+
size_t bytes_written;
1004+
std::tie(write_ec, bytes_written) = async_simple::coro::syncAwait(
10011005
file.async_write_at(aligned_offset, std::string_view(buf, aligned_read_size)));
1002-
1006+
10031007
CHECK(!write_ec);
10041008
CHECK(bytes_written == aligned_read_size);
1005-
1009+
10061010
std::memset(buf, 0, aligned_read_size);
1007-
auto [read_ec, read_bytes] = async_simple::coro::syncAwait(
1011+
std::error_code read_ec;
1012+
size_t read_bytes;
1013+
std::tie(read_ec, read_bytes) = async_simple::coro::syncAwait(
10081014
file.async_read_at(aligned_offset, buf, aligned_read_size));
1009-
1015+
10101016
CHECK(!read_ec);
10111017
CHECK(read_bytes == aligned_read_size);
1012-
1018+
10131019
for (size_t i = 0; i < aligned_read_size; ++i) {
10141020
CHECK(buf[i] == 'X');
10151021
}
1016-
1017-
for (size_t offset = 0; offset < file_size - aligned_read_size; offset += aligned_read_size) {
1022+
1023+
for (size_t offset = 0; offset < file_size - aligned_read_size;
1024+
offset += aligned_read_size) {
10181025
size_t aligned_off = align_offset_for_direct_io(offset);
1019-
1020-
std::memset(buf, 'A' + (offset / aligned_read_size) % 26, aligned_read_size);
1021-
auto [w_ec, w_bytes] = async_simple::coro::syncAwait(
1022-
file.async_write_at(aligned_off, std::string_view(buf, aligned_read_size)));
1026+
1027+
std::memset(buf, 'A' + (offset / aligned_read_size) % 26,
1028+
aligned_read_size);
1029+
std::error_code w_ec;
1030+
size_t w_bytes;
1031+
std::tie(w_ec, w_bytes) = async_simple::coro::syncAwait(file.async_write_at(
1032+
aligned_off, std::string_view(buf, aligned_read_size)));
10231033
CHECK(!w_ec);
10241034
CHECK(w_bytes == aligned_read_size);
1025-
1035+
10261036
std::memset(buf, 0, aligned_read_size);
1027-
auto [r_ec, r_bytes] = async_simple::coro::syncAwait(
1037+
std::error_code r_ec;
1038+
size_t r_bytes;
1039+
std::tie(r_ec, r_bytes) = async_simple::coro::syncAwait(
10281040
file.async_read_at(aligned_off, buf, aligned_read_size));
10291041
CHECK(!r_ec);
10301042
CHECK(r_bytes == aligned_read_size);
1031-
1043+
10321044
char expected = 'A' + (offset / aligned_read_size) % 26;
10331045
for (size_t i = 0; i < aligned_read_size; ++i) {
10341046
CHECK(buf[i] == expected);
10351047
}
10361048
}
1037-
1049+
10381050
file.close();
10391051
}
10401052
#endif
@@ -1062,19 +1074,19 @@ TEST_CASE("direct io alignment test") {
10621074
CHECK(align_offset_for_direct_io(512) == 512);
10631075
CHECK(align_offset_for_direct_io(1000) == 512);
10641076
CHECK(align_offset_for_direct_io(1024) == 1024);
1065-
1077+
10661078
CHECK(align_size_for_direct_io(0) == 0);
10671079
CHECK(align_size_for_direct_io(100) == 512);
10681080
CHECK(align_size_for_direct_io(512) == 512);
10691081
CHECK(align_size_for_direct_io(1000) == 1024);
10701082
CHECK(align_size_for_direct_io(1024) == 1024);
1071-
1072-
void* ptr1 = aligned_alloc_direct_io(1024);
1083+
1084+
void *ptr1 = aligned_alloc_direct_io(1024);
10731085
REQUIRE(ptr1 != nullptr);
10741086
CHECK(reinterpret_cast<uintptr_t>(ptr1) % DIRECT_IO_ALIGNMENT == 0);
10751087
aligned_free_direct_io(ptr1);
1076-
1077-
void* ptr2 = aligned_alloc_direct_io(4096);
1088+
1089+
void *ptr2 = aligned_alloc_direct_io(4096);
10781090
REQUIRE(ptr2 != nullptr);
10791091
CHECK(reinterpret_cast<uintptr_t>(ptr2) % DIRECT_IO_ALIGNMENT == 0);
10801092
aligned_free_direct_io(ptr2);

0 commit comments

Comments
 (0)