Update TDLib to 1.8.6 and add options for changing CPU affinity.

This commit is contained in:
levlam 2022-09-18 00:20:41 +03:00
parent ffecd115fe
commit c35bbf1bd2
3 changed files with 55 additions and 26 deletions

2
td

@ -1 +1 @@
Subproject commit 96fc91e239f25a123df2e0405f58e24bc963cab7 Subproject commit cb70993b90ce84c16dea07c347835addbff46a2c

View File

@ -4472,27 +4472,23 @@ void Client::on_update_authorization_state() {
make_object<td_api::optionValueBoolean>(true)), make_object<td_api::optionValueBoolean>(true)),
td::make_unique<TdOnOkCallback>()); td::make_unique<TdOnOkCallback>());
auto parameters = make_object<td_api::tdlibParameters>(); auto request = make_object<td_api::setTdlibParameters>();
request->use_test_dc_ = is_test_dc_;
request->database_directory_ = dir_;
//request->use_file_database_ = false;
//request->use_chat_info_database_ = false;
//request->use_secret_chats_ = false;
request->use_message_database_ = USE_MESSAGE_DATABASE;
request->api_id_ = parameters_->api_id_;
request->api_hash_ = parameters_->api_hash_;
request->system_language_code_ = "en";
request->device_model_ = "server";
request->application_version_ = parameters_->version_;
request->enable_storage_optimizer_ = true;
request->ignore_file_names_ = true;
parameters->use_test_dc_ = is_test_dc_; return send_request(std::move(request), td::make_unique<TdOnInitCallback>(this));
parameters->database_directory_ = dir_;
//parameters->use_file_database_ = false;
//parameters->use_chat_info_database_ = false;
//parameters->use_secret_chats_ = false;
parameters->use_message_database_ = USE_MESSAGE_DATABASE;
parameters->api_id_ = parameters_->api_id_;
parameters->api_hash_ = parameters_->api_hash_;
parameters->system_language_code_ = "en";
parameters->device_model_ = "server";
parameters->application_version_ = parameters_->version_;
parameters->enable_storage_optimizer_ = true;
parameters->ignore_file_names_ = true;
return send_request(make_object<td_api::setTdlibParameters>(std::move(parameters)),
td::make_unique<TdOnInitCallback>(this));
} }
case td_api::authorizationStateWaitEncryptionKey::ID:
return send_request(make_object<td_api::checkDatabaseEncryptionKey>(), td::make_unique<TdOnInitCallback>(this));
case td_api::authorizationStateWaitPhoneNumber::ID: case td_api::authorizationStateWaitPhoneNumber::ID:
send_request(make_object<td_api::setOption>("online", make_object<td_api::optionValueBoolean>(true)), send_request(make_object<td_api::setOption>("online", make_object<td_api::optionValueBoolean>(true)),
td::make_unique<TdOnOkCallback>()); td::make_unique<TdOnOkCallback>());
@ -5618,7 +5614,7 @@ td::Result<td_api::object_ptr<td_api::InputMessageContent>> Client::get_input_me
td_api::object_ptr<td_api::messageSendOptions> Client::get_message_send_options(bool disable_notification, td_api::object_ptr<td_api::messageSendOptions> Client::get_message_send_options(bool disable_notification,
bool protect_content) { bool protect_content) {
return make_object<td_api::messageSendOptions>(disable_notification, false, protect_content, nullptr); return make_object<td_api::messageSendOptions>(disable_notification, false, protect_content, false, nullptr);
} }
td::Result<td::vector<td_api::object_ptr<td_api::InputInlineQueryResult>>> Client::get_inline_query_results( td::Result<td::vector<td_api::object_ptr<td_api::InputInlineQueryResult>>> Client::get_inline_query_results(

View File

@ -44,6 +44,7 @@
#include "td/utils/port/signals.h" #include "td/utils/port/signals.h"
#include "td/utils/port/stacktrace.h" #include "td/utils/port/stacktrace.h"
#include "td/utils/port/Stat.h" #include "td/utils/port/Stat.h"
#include "td/utils/port/thread.h"
#include "td/utils/port/user.h" #include "td/utils/port/user.h"
#include "td/utils/Promise.h" #include "td/utils/Promise.h"
#include "td/utils/Slice.h" #include "td/utils/Slice.h"
@ -223,6 +224,8 @@ int main(int argc, char *argv[]) {
td::string username; td::string username;
td::string groupname; td::string groupname;
td::uint64 max_connections = 0; td::uint64 max_connections = 0;
td::uint64 cpu_affinity = 0;
td::uint64 main_thread_affinity = 0;
ClientManager::TokenRange token_range{0, 1}; ClientManager::TokenRange token_range{0, 1};
parameters->api_id_ = [](auto x) -> td::int32 { parameters->api_id_ = [](auto x) -> td::int32 {
@ -310,6 +313,17 @@ int main(int argc, char *argv[]) {
options.add_option('g', "groupname", "effective group name to switch to", td::OptionParser::parse_string(groupname)); options.add_option('g', "groupname", "effective group name to switch to", td::OptionParser::parse_string(groupname));
options.add_checked_option('c', "max-connections", "maximum number of open file descriptors", options.add_checked_option('c', "max-connections", "maximum number of open file descriptors",
td::OptionParser::parse_integer(max_connections)); td::OptionParser::parse_integer(max_connections));
#if TD_HAVE_THREAD_AFFINITY
options.add_checked_option('\0', "cpu-affinity", "CPU affinity as 64-bit mask (defaults to all available CPUs)",
td::OptionParser::parse_integer(cpu_affinity));
options.add_checked_option(
'\0', "main-thread-affinity",
"CPU affinity of the main thread as 64-bit mask (defaults to the value of the option --cpu-affinity)",
td::OptionParser::parse_integer(main_thread_affinity));
#else
(void)cpu_affinity;
(void)main_thread_affinity;
#endif
options.add_checked_option('\0', "proxy", options.add_checked_option('\0', "proxy",
"HTTP proxy server for outgoing webhook requests in the format http://host:port", "HTTP proxy server for outgoing webhook requests in the format http://host:port",
@ -363,6 +377,26 @@ int main(int argc, char *argv[]) {
td::TsLog ts_log(&file_log); td::TsLog ts_log(&file_log);
auto init_status = [&] { auto init_status = [&] {
#if TD_HAVE_THREAD_AFFINITY
if (main_thread_affinity == 0) {
main_thread_affinity = cpu_affinity;
}
if (main_thread_affinity != 0) {
auto initial_mask = td::thread::get_affinity_mask(td::this_thread::get_id());
if (initial_mask == 0) {
return td::Status::Error("Failed to get current thread affinity");
}
if (cpu_affinity != 0) {
TRY_STATUS_PREFIX(td::thread::set_affinity_mask(td::this_thread::get_id(), cpu_affinity),
"Can't set CPU affinity mask: ");
} else {
cpu_affinity = initial_mask;
}
TRY_STATUS_PREFIX(td::thread::set_affinity_mask(td::this_thread::get_id(), main_thread_affinity),
"Can't set main thread CPU affinity mask: ");
}
#endif
if (max_connections != 0) { if (max_connections != 0) {
TRY_STATUS_PREFIX(td::set_resource_limit(td::ResourceLimitType::NoFile, max_connections), TRY_STATUS_PREFIX(td::set_resource_limit(td::ResourceLimitType::NoFile, max_connections),
"Can't set file descriptor limit: "); "Can't set file descriptor limit: ");
@ -463,12 +497,11 @@ int main(int argc, char *argv[]) {
// << (td::GitInfo::is_dirty() ? "(dirty)" : "") << " started"; // << (td::GitInfo::is_dirty() ? "(dirty)" : "") << " started";
LOG(WARNING) << "Bot API " << parameters->version_ << " server started"; LOG(WARNING) << "Bot API " << parameters->version_ << " server started";
const int threads_n = 6; // +3 for Td, one for watchdog, one for slow HTTP connections, one for DNS resolving const int thread_count = 6; // +3 for Td, one for watchdog, one for slow HTTP connections, one for DNS resolving
td::ConcurrentScheduler sched; td::ConcurrentScheduler sched(thread_count, cpu_affinity);
sched.init(threads_n);
td::GetHostByNameActor::Options get_host_by_name_options; td::GetHostByNameActor::Options get_host_by_name_options;
get_host_by_name_options.scheduler_id = threads_n; get_host_by_name_options.scheduler_id = thread_count;
parameters->get_host_by_name_actor_id_ = parameters->get_host_by_name_actor_id_ =
sched.create_actor_unsafe<td::GetHostByNameActor>(0, "GetHostByName", std::move(get_host_by_name_options)) sched.create_actor_unsafe<td::GetHostByNameActor>(0, "GetHostByName", std::move(get_host_by_name_options))
.release(); .release();
@ -498,7 +531,7 @@ int main(int argc, char *argv[]) {
constexpr double WATCHDOG_TIMEOUT = 0.5; constexpr double WATCHDOG_TIMEOUT = 0.5;
auto watchdog_id = auto watchdog_id =
sched.create_actor_unsafe<Watchdog>(threads_n - 2, "Watchdog", td::this_thread::get_id(), WATCHDOG_TIMEOUT); sched.create_actor_unsafe<Watchdog>(thread_count - 2, "Watchdog", td::this_thread::get_id(), WATCHDOG_TIMEOUT);
sched.start(); sched.start();