2018-08-13 19:15:09 +02:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2018-08-13 19:15:09 +02:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/common.h"
|
2018-09-10 03:08:15 +02:00
|
|
|
#include "td/utils/logging.h"
|
2019-05-22 20:17:24 +02:00
|
|
|
#include "td/utils/misc.h"
|
2020-08-07 17:50:33 +02:00
|
|
|
#include "td/utils/port/EventFd.h"
|
2018-08-13 19:15:09 +02:00
|
|
|
#include "td/utils/port/FileFd.h"
|
2019-07-23 00:50:12 +02:00
|
|
|
#include "td/utils/port/IoSlice.h"
|
2018-08-13 19:15:09 +02:00
|
|
|
#include "td/utils/port/path.h"
|
2019-07-06 13:29:15 +02:00
|
|
|
#include "td/utils/port/signals.h"
|
2020-08-07 17:50:33 +02:00
|
|
|
#include "td/utils/port/sleep.h"
|
2019-07-23 00:50:12 +02:00
|
|
|
#include "td/utils/port/thread.h"
|
|
|
|
#include "td/utils/port/thread_local.h"
|
2020-08-14 21:51:10 +02:00
|
|
|
#include "td/utils/Random.h"
|
|
|
|
#include "td/utils/ScopeGuard.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/tests.h"
|
2020-08-07 17:50:33 +02:00
|
|
|
#include "td/utils/Time.h"
|
2018-08-13 19:15:09 +02:00
|
|
|
|
2020-10-02 00:34:32 +02:00
|
|
|
#if TD_PORT_POSIX && !TD_THREAD_UNSUPPORTED
|
|
|
|
#include <algorithm>
|
2020-08-07 17:50:33 +02:00
|
|
|
#include <atomic>
|
2020-10-02 00:34:32 +02:00
|
|
|
#include <mutex>
|
2019-08-14 02:13:34 +02:00
|
|
|
|
2020-10-02 00:34:32 +02:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
2018-08-13 19:15:09 +02:00
|
|
|
|
|
|
|
TEST(Port, files) {
|
2020-10-02 00:34:32 +02:00
|
|
|
td::CSlice main_dir = "test_dir";
|
|
|
|
td::rmrf(main_dir).ignore();
|
|
|
|
ASSERT_TRUE(td::FileFd::open(main_dir, td::FileFd::Write).is_error());
|
|
|
|
ASSERT_TRUE(td::walk_path(main_dir, [](td::CSlice name, td::WalkPath::Type type) { UNREACHABLE(); }).is_error());
|
|
|
|
td::mkdir(main_dir).ensure();
|
|
|
|
td::mkdir(PSLICE() << main_dir << TD_DIR_SLASH << "A").ensure();
|
|
|
|
td::mkdir(PSLICE() << main_dir << TD_DIR_SLASH << "B").ensure();
|
|
|
|
td::mkdir(PSLICE() << main_dir << TD_DIR_SLASH << "B" << TD_DIR_SLASH << "D").ensure();
|
|
|
|
td::mkdir(PSLICE() << main_dir << TD_DIR_SLASH << "C").ensure();
|
|
|
|
ASSERT_TRUE(td::FileFd::open(main_dir, td::FileFd::Write).is_error());
|
|
|
|
td::string fd_path = PSTRING() << main_dir << TD_DIR_SLASH << "t.txt";
|
|
|
|
td::string fd2_path = PSTRING() << main_dir << TD_DIR_SLASH << "C" << TD_DIR_SLASH << "t2.txt";
|
2018-08-13 19:15:09 +02:00
|
|
|
|
2020-10-02 00:34:32 +02:00
|
|
|
auto fd = td::FileFd::open(fd_path, td::FileFd::Write | td::FileFd::CreateNew).move_as_ok();
|
|
|
|
auto fd2 = td::FileFd::open(fd2_path, td::FileFd::Write | td::FileFd::CreateNew).move_as_ok();
|
2018-09-12 02:21:23 +02:00
|
|
|
fd2.close();
|
2018-09-11 21:17:01 +02:00
|
|
|
|
|
|
|
int cnt = 0;
|
|
|
|
const int ITER_COUNT = 1000;
|
|
|
|
for (int i = 0; i < ITER_COUNT; i++) {
|
2020-10-02 00:34:32 +02:00
|
|
|
td::walk_path(main_dir, [&](td::CSlice name, td::WalkPath::Type type) {
|
|
|
|
if (type == td::WalkPath::Type::NotDir) {
|
2019-09-28 04:14:21 +02:00
|
|
|
ASSERT_TRUE(name == fd_path || name == fd2_path);
|
|
|
|
}
|
|
|
|
cnt++;
|
|
|
|
}).ensure();
|
2018-09-11 21:17:01 +02:00
|
|
|
}
|
2019-05-01 11:23:19 +02:00
|
|
|
ASSERT_EQ((5 * 2 + 2) * ITER_COUNT, cnt);
|
|
|
|
bool was_abort = false;
|
2020-10-02 00:34:32 +02:00
|
|
|
td::walk_path(main_dir, [&](td::CSlice name, td::WalkPath::Type type) {
|
2019-09-28 04:14:21 +02:00
|
|
|
CHECK(!was_abort);
|
2020-10-02 00:34:32 +02:00
|
|
|
if (type == td::WalkPath::Type::EnterDir && ends_with(name, PSLICE() << TD_DIR_SLASH << "B")) {
|
2019-09-28 04:14:21 +02:00
|
|
|
was_abort = true;
|
2020-10-02 00:34:32 +02:00
|
|
|
return td::WalkPath::Action::Abort;
|
2019-09-28 04:14:21 +02:00
|
|
|
}
|
2020-10-02 00:34:32 +02:00
|
|
|
return td::WalkPath::Action::Continue;
|
2019-09-28 04:14:21 +02:00
|
|
|
}).ensure();
|
2019-05-01 11:23:19 +02:00
|
|
|
CHECK(was_abort);
|
|
|
|
|
|
|
|
cnt = 0;
|
|
|
|
bool is_first_dir = true;
|
2020-10-02 00:34:32 +02:00
|
|
|
td::walk_path(main_dir, [&](td::CSlice name, td::WalkPath::Type type) {
|
2019-09-28 04:14:21 +02:00
|
|
|
cnt++;
|
2020-10-02 00:34:32 +02:00
|
|
|
if (type == td::WalkPath::Type::EnterDir) {
|
2019-09-28 04:14:21 +02:00
|
|
|
if (is_first_dir) {
|
|
|
|
is_first_dir = false;
|
|
|
|
} else {
|
2020-10-02 00:34:32 +02:00
|
|
|
return td::WalkPath::Action::SkipDir;
|
2019-09-28 04:14:21 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-02 00:34:32 +02:00
|
|
|
return td::WalkPath::Action::Continue;
|
2019-09-28 04:14:21 +02:00
|
|
|
}).ensure();
|
2019-05-01 11:23:19 +02:00
|
|
|
ASSERT_EQ(6, cnt);
|
2018-09-11 21:17:01 +02:00
|
|
|
|
2019-06-19 16:31:25 +02:00
|
|
|
ASSERT_EQ(0u, fd.get_size().move_as_ok());
|
2018-08-13 19:15:09 +02:00
|
|
|
ASSERT_EQ(12u, fd.write("Hello world!").move_as_ok());
|
|
|
|
ASSERT_EQ(4u, fd.pwrite("abcd", 1).move_as_ok());
|
|
|
|
char buf[100];
|
2020-10-02 00:34:32 +02:00
|
|
|
td::MutableSlice buf_slice(buf, sizeof(buf));
|
2018-08-13 19:15:09 +02:00
|
|
|
ASSERT_TRUE(fd.pread(buf_slice.substr(0, 4), 2).is_error());
|
|
|
|
fd.seek(11).ensure();
|
|
|
|
ASSERT_EQ(2u, fd.write("?!").move_as_ok());
|
|
|
|
|
2020-10-02 00:34:32 +02:00
|
|
|
ASSERT_TRUE(td::FileFd::open(main_dir, td::FileFd::Read | td::FileFd::CreateNew).is_error());
|
|
|
|
fd = td::FileFd::open(fd_path, td::FileFd::Read | td::FileFd::Create).move_as_ok();
|
2019-06-19 16:31:25 +02:00
|
|
|
ASSERT_EQ(13u, fd.get_size().move_as_ok());
|
2018-08-13 19:15:09 +02:00
|
|
|
ASSERT_EQ(4u, fd.pread(buf_slice.substr(0, 4), 1).move_as_ok());
|
|
|
|
ASSERT_STREQ("abcd", buf_slice.substr(0, 4));
|
|
|
|
|
|
|
|
fd.seek(0).ensure();
|
|
|
|
ASSERT_EQ(13u, fd.read(buf_slice.substr(0, 13)).move_as_ok());
|
|
|
|
ASSERT_STREQ("Habcd world?!", buf_slice.substr(0, 13));
|
|
|
|
}
|
2019-07-06 13:29:15 +02:00
|
|
|
|
2020-01-02 23:01:39 +01:00
|
|
|
TEST(Port, SparseFiles) {
|
2020-10-02 00:34:32 +02:00
|
|
|
td::CSlice path = "sparse.txt";
|
|
|
|
td::unlink(path).ignore();
|
|
|
|
auto fd = td::FileFd::open(path, td::FileFd::Write | td::FileFd::CreateNew).move_as_ok();
|
2020-01-02 23:01:39 +01:00
|
|
|
ASSERT_EQ(0, fd.get_size().move_as_ok());
|
2020-10-02 00:34:32 +02:00
|
|
|
td::int64 offset = 100000000;
|
2020-01-03 17:37:03 +01:00
|
|
|
fd.pwrite("a", offset).ensure();
|
2020-01-02 23:01:39 +01:00
|
|
|
ASSERT_EQ(offset + 1, fd.get_size().move_as_ok());
|
|
|
|
auto real_size = fd.get_real_size().move_as_ok();
|
2020-02-19 02:55:25 +01:00
|
|
|
if (real_size >= offset + 1) {
|
2020-01-02 23:01:39 +01:00
|
|
|
LOG(ERROR) << "File system doesn't support sparse files, rewind during streaming can be slow";
|
|
|
|
}
|
2020-10-02 00:34:32 +02:00
|
|
|
td::unlink(path).ensure();
|
2022-05-11 18:03:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Port, LargeFiles) {
|
|
|
|
td::CSlice path = "large.txt";
|
|
|
|
td::unlink(path).ignore();
|
|
|
|
auto fd = td::FileFd::open(path, td::FileFd::Write | td::FileFd::CreateNew).move_as_ok();
|
|
|
|
ASSERT_EQ(0, fd.get_size().move_as_ok());
|
|
|
|
td::int64 offset = static_cast<td::int64>(3) << 30;
|
|
|
|
if (fd.pwrite("abcd", offset).is_error()) {
|
|
|
|
LOG(ERROR) << "Writing to large files isn't supported";
|
|
|
|
td::unlink(path).ensure();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fd = td::FileFd::open(path, td::FileFd::Read).move_as_ok();
|
|
|
|
ASSERT_EQ(offset + 4, fd.get_size().move_as_ok());
|
|
|
|
td::string res(4, '\0');
|
|
|
|
if (fd.pread(res, offset).is_error()) {
|
|
|
|
LOG(ERROR) << "Reading of large files isn't supported";
|
|
|
|
td::unlink(path).ensure();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ASSERT_STREQ(res, "abcd");
|
|
|
|
fd.close();
|
|
|
|
td::unlink(path).ensure();
|
2020-01-02 23:01:39 +01:00
|
|
|
}
|
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
TEST(Port, Writev) {
|
2020-10-02 00:34:32 +02:00
|
|
|
td::vector<td::IoSlice> vec;
|
|
|
|
td::CSlice test_file_path = "test.txt";
|
|
|
|
td::unlink(test_file_path).ignore();
|
|
|
|
auto fd = td::FileFd::open(test_file_path, td::FileFd::Write | td::FileFd::CreateNew).move_as_ok();
|
|
|
|
vec.push_back(td::as_io_slice("a"));
|
|
|
|
vec.push_back(td::as_io_slice("b"));
|
|
|
|
vec.push_back(td::as_io_slice("cd"));
|
2019-07-06 13:29:15 +02:00
|
|
|
ASSERT_EQ(4u, fd.writev(vec).move_as_ok());
|
|
|
|
vec.clear();
|
2020-10-02 00:34:32 +02:00
|
|
|
vec.push_back(td::as_io_slice("efg"));
|
|
|
|
vec.push_back(td::as_io_slice(""));
|
|
|
|
vec.push_back(td::as_io_slice("hi"));
|
2019-07-06 13:29:15 +02:00
|
|
|
ASSERT_EQ(5u, fd.writev(vec).move_as_ok());
|
|
|
|
fd.close();
|
2020-10-02 00:34:32 +02:00
|
|
|
fd = td::FileFd::open(test_file_path, td::FileFd::Read).move_as_ok();
|
|
|
|
td::Slice expected_content = "abcdefghi";
|
|
|
|
ASSERT_EQ(static_cast<td::int64>(expected_content.size()), fd.get_size().ok());
|
|
|
|
td::string content(expected_content.size(), '\0');
|
2019-07-06 13:29:15 +02:00
|
|
|
ASSERT_EQ(content.size(), fd.read(content).move_as_ok());
|
|
|
|
ASSERT_EQ(expected_content, content);
|
|
|
|
}
|
|
|
|
|
2019-07-21 20:07:07 +02:00
|
|
|
#if TD_PORT_POSIX && !TD_THREAD_UNSUPPORTED
|
2019-07-06 13:29:15 +02:00
|
|
|
|
2019-07-21 20:07:07 +02:00
|
|
|
static std::mutex m;
|
2020-10-02 00:34:32 +02:00
|
|
|
static td::vector<td::string> ptrs;
|
|
|
|
static td::vector<int *> addrs;
|
2019-07-21 20:07:07 +02:00
|
|
|
static TD_THREAD_LOCAL int thread_id;
|
|
|
|
|
|
|
|
static void on_user_signal(int sig) {
|
2019-07-06 13:29:15 +02:00
|
|
|
int addr;
|
|
|
|
addrs[thread_id] = &addr;
|
|
|
|
std::unique_lock<std::mutex> guard(m);
|
2020-10-02 00:24:35 +02:00
|
|
|
ptrs.push_back(td::to_string(thread_id));
|
2019-07-06 13:29:15 +02:00
|
|
|
}
|
|
|
|
|
2020-08-07 17:50:33 +02:00
|
|
|
TEST(Port, SignalsAndThread) {
|
2020-10-02 00:34:32 +02:00
|
|
|
td::setup_signals_alt_stack().ensure();
|
|
|
|
td::set_signal_handler(td::SignalType::User, on_user_signal).ensure();
|
2020-08-07 17:50:33 +02:00
|
|
|
SCOPE_EXIT {
|
2020-10-02 00:34:32 +02:00
|
|
|
td::set_signal_handler(td::SignalType::User, nullptr).ensure();
|
2020-08-07 17:50:33 +02:00
|
|
|
};
|
2020-10-02 00:34:32 +02:00
|
|
|
td::vector<td::string> ans = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
|
2019-07-06 13:29:15 +02:00
|
|
|
{
|
2020-10-02 00:34:32 +02:00
|
|
|
td::vector<td::thread> threads;
|
2019-07-06 13:29:15 +02:00
|
|
|
int thread_n = 10;
|
2020-10-02 00:34:32 +02:00
|
|
|
td::vector<td::Stage> stages(thread_n);
|
2019-07-06 13:29:15 +02:00
|
|
|
ptrs.clear();
|
|
|
|
addrs.resize(thread_n);
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
threads.emplace_back([&, i] {
|
2020-10-02 00:34:32 +02:00
|
|
|
td::setup_signals_alt_stack().ensure();
|
2019-07-06 13:29:15 +02:00
|
|
|
if (i != 0) {
|
|
|
|
stages[i].wait(2);
|
|
|
|
}
|
|
|
|
thread_id = i;
|
|
|
|
pthread_kill(pthread_self(), SIGUSR1);
|
|
|
|
if (i + 1 < thread_n) {
|
|
|
|
stages[i + 1].wait(2);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
for (auto &t : threads) {
|
|
|
|
t.join();
|
|
|
|
}
|
|
|
|
CHECK(ptrs == ans);
|
|
|
|
|
2020-08-02 00:40:48 +02:00
|
|
|
//LOG(ERROR) << ptrs;
|
2019-07-06 13:29:15 +02:00
|
|
|
//LOG(ERROR) << addrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2020-10-02 00:34:32 +02:00
|
|
|
td::Stage stage;
|
|
|
|
td::vector<td::thread> threads;
|
2019-07-06 13:29:15 +02:00
|
|
|
int thread_n = 10;
|
|
|
|
ptrs.clear();
|
|
|
|
addrs.resize(thread_n);
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
threads.emplace_back([&, i] {
|
|
|
|
stage.wait(thread_n);
|
|
|
|
thread_id = i;
|
|
|
|
pthread_kill(pthread_self(), SIGUSR1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
for (auto &t : threads) {
|
|
|
|
t.join();
|
|
|
|
}
|
|
|
|
std::sort(ptrs.begin(), ptrs.end());
|
|
|
|
CHECK(ptrs == ans);
|
2020-12-30 16:50:57 +01:00
|
|
|
auto addrs_size = addrs.size();
|
|
|
|
td::unique(addrs);
|
|
|
|
ASSERT_EQ(addrs_size, addrs.size());
|
2019-07-06 13:29:15 +02:00
|
|
|
//LOG(ERROR) << addrs;
|
|
|
|
}
|
|
|
|
}
|
2020-08-14 21:51:10 +02:00
|
|
|
|
2021-10-25 00:46:39 +02:00
|
|
|
#if !TD_EVENTFD_UNSUPPORTED
|
2020-08-07 17:50:33 +02:00
|
|
|
TEST(Port, EventFdAndSignals) {
|
2020-10-02 00:34:32 +02:00
|
|
|
td::set_signal_handler(td::SignalType::User, [](int signal) {}).ensure();
|
2020-08-07 17:50:33 +02:00
|
|
|
SCOPE_EXIT {
|
2020-10-02 00:34:32 +02:00
|
|
|
td::set_signal_handler(td::SignalType::User, nullptr).ensure();
|
2020-08-07 17:50:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
std::atomic_flag flag;
|
|
|
|
flag.test_and_set();
|
|
|
|
auto main_thread = pthread_self();
|
|
|
|
td::thread interrupt_thread{[&flag, &main_thread] {
|
2020-10-02 00:34:32 +02:00
|
|
|
td::setup_signals_alt_stack().ensure();
|
2020-08-07 17:50:33 +02:00
|
|
|
while (flag.test_and_set()) {
|
|
|
|
pthread_kill(main_thread, SIGUSR1);
|
|
|
|
td::usleep_for(1000 * td::Random::fast(1, 10)); // 0.001s - 0.01s
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
|
|
|
for (int timeout_ms : {0, 1, 2, 10, 100, 500}) {
|
|
|
|
double min_diff = 10000000;
|
|
|
|
double max_diff = 0;
|
2020-10-02 00:34:32 +02:00
|
|
|
for (int t = 0; t < td::max(5, 1000 / td::max(timeout_ms, 1)); t++) {
|
2020-08-07 17:50:33 +02:00
|
|
|
td::EventFd event_fd;
|
|
|
|
event_fd.init();
|
|
|
|
auto start = td::Timestamp::now();
|
|
|
|
event_fd.wait(timeout_ms);
|
|
|
|
auto end = td::Timestamp::now();
|
|
|
|
auto passed = end.at() - start.at();
|
|
|
|
auto diff = passed * 1000 - timeout_ms;
|
2020-08-14 21:51:10 +02:00
|
|
|
min_diff = td::min(min_diff, diff);
|
|
|
|
max_diff = td::max(max_diff, diff);
|
2020-08-07 17:50:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LOG_CHECK(min_diff >= 0) << min_diff;
|
2020-10-02 00:24:35 +02:00
|
|
|
// LOG_CHECK(max_diff < 10) << max_diff;
|
2020-08-14 21:51:10 +02:00
|
|
|
LOG(INFO) << min_diff << " " << max_diff;
|
2020-08-07 17:50:33 +02:00
|
|
|
}
|
|
|
|
flag.clear();
|
|
|
|
}
|
2019-07-06 13:29:15 +02:00
|
|
|
#endif
|
2021-10-25 00:46:39 +02:00
|
|
|
#endif
|
2022-09-14 13:38:14 +02:00
|
|
|
|
|
|
|
#if !TD_THREAD_UNSUPPORTED
|
|
|
|
TEST(Port, ThreadAffinityMask) {
|
|
|
|
auto thread_id = td::this_thread::get_id();
|
|
|
|
auto old_mask = td::thread::get_affinity_mask(thread_id);
|
|
|
|
LOG(INFO) << "Initial thread affinity mask: " << old_mask;
|
|
|
|
for (size_t i = 0; i < 64; i++) {
|
|
|
|
auto mask = td::thread::get_affinity_mask(thread_id);
|
|
|
|
LOG(INFO) << mask;
|
|
|
|
auto result = td::thread::set_affinity_mask(thread_id, static_cast<td::uint64>(1) << i);
|
|
|
|
LOG(INFO) << i << ": " << result;
|
|
|
|
mask = td::thread::get_affinity_mask(thread_id);
|
|
|
|
LOG(INFO) << mask;
|
|
|
|
|
|
|
|
if (i <= 1) {
|
|
|
|
td::thread thread([] {
|
|
|
|
auto mask = td::thread::get_affinity_mask(td::this_thread::get_id());
|
|
|
|
LOG(INFO) << "New thread affinity mask: " << mask;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto result = td::thread::set_affinity_mask(thread_id, old_mask);
|
|
|
|
LOG(INFO) << result;
|
|
|
|
old_mask = td::thread::get_affinity_mask(thread_id);
|
|
|
|
LOG(INFO) << old_mask;
|
|
|
|
}
|
|
|
|
#endif
|