Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
acelyc111 committed May 23, 2024
1 parent a4b82db commit 34efb46
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 69 deletions.
112 changes: 54 additions & 58 deletions src/runtime/service_api_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
#include <gperftools/malloc_extension.h>
#endif

#include "fmt/core.h"
#include "fmt/format.h"
#include "perf_counter/perf_counters.h"
#include "runtime/api_layer1.h"
#include "runtime/api_task.h"
Expand All @@ -53,8 +55,6 @@
#include "runtime/rpc/rpc_engine.h"
#include "runtime/rpc/rpc_host_port.h"
#include "runtime/rpc/rpc_message.h"
#include "security/init.h"
#include "security/negotiation_manager.h"
#include "runtime/service_app.h"
#include "runtime/service_engine.h"
#include "runtime/task/task.h"
Expand All @@ -63,6 +63,8 @@
#include "runtime/task/task_spec.h"
#include "runtime/task/task_worker.h"
#include "runtime/tool_api.h"
#include "security/init.h"
#include "security/negotiation_manager.h"
#include "utils/api_utilities.h"
#include "utils/command_manager.h"
#include "utils/config_api.h"
Expand All @@ -75,6 +77,7 @@
#include "utils/join_point.h"
#include "utils/logging_provider.h"
#include "utils/process_utils.h"
#include "utils/string_conv.h"
#include "utils/strings.h"
#include "utils/sys_exit_hook.h"
#include "utils/threadpool_spec.h"
Expand Down Expand Up @@ -342,45 +345,29 @@ inline void dsn_global_init()

static std::string dsn_log_prefixed_message_func()
{
std::string res;
res.resize(100);
char *prefixed_message = const_cast<char *>(res.c_str());

int tid = dsn::utils::get_current_tid();
auto t = dsn::task::get_current_task_id();
const int tid = dsn::utils::get_current_tid();
const auto t = dsn::task::get_current_task_id();
if (t) {
if (nullptr != dsn::task::get_current_worker2()) {
sprintf(prefixed_message,
"%6s.%7s%d.%016" PRIx64 ": ",
dsn::task::get_current_node_name(),
dsn::task::get_current_worker2()->pool_spec().name.c_str(),
dsn::task::get_current_worker2()->index(),
t);
return fmt::format("{}.{}{}.{:016}: ",
dsn::task::get_current_node_name(),
dsn::task::get_current_worker2()->pool_spec().name,
dsn::task::get_current_worker2()->index(),
t);
} else {
sprintf(prefixed_message,
"%6s.%7s.%05d.%016" PRIx64 ": ",
dsn::task::get_current_node_name(),
"io-thrd",
tid,
t);
return fmt::format(
"{}.io-thrd.{}.{:016}: ", dsn::task::get_current_node_name(), tid, t);
}
} else {
if (nullptr != dsn::task::get_current_worker2()) {
sprintf(prefixed_message,
"%6s.%7s%u: ",
dsn::task::get_current_node_name(),
dsn::task::get_current_worker2()->pool_spec().name.c_str(),
dsn::task::get_current_worker2()->index());
return fmt::format("{}.{}{}: ",
dsn::task::get_current_node_name(),
dsn::task::get_current_worker2()->pool_spec().name,
dsn::task::get_current_worker2()->index());
} else {
sprintf(prefixed_message,
"%6s.%7s.%05d: ",
dsn::task::get_current_node_name(),
"io-thrd",
tid);
return fmt::format("{}.io-thrd.{}: ", dsn::task::get_current_node_name(), tid);
}
}

return res;
}

bool run(const char *config_file,
Expand Down Expand Up @@ -447,14 +434,14 @@ bool run(const char *config_file,
spec.dir_log = ::dsn::utils::filesystem::path_combine(cdir, "log");
dsn::utils::filesystem::create_directory(spec.dir_log);

// init tools
// Initialize tools.
dsn_all.tool.reset(::dsn::utils::factory_store<::dsn::tools::tool_app>::create(
spec.tool.c_str(), ::dsn::PROVIDER_TYPE_MAIN, spec.tool.c_str()));
dsn_all.tool->install(spec);

// init app specs
// Initialize app specs.
if (!spec.init_app_specs()) {
printf("error in config file %s, exit ...\n", config_file);
fmt::print(stderr, "error in config file {}, exit ...\n", config_file);
return false;
}

Expand All @@ -465,18 +452,18 @@ bool run(const char *config_file,
// init logging
dsn_log_init(spec.logging_factory_name, spec.dir_log, dsn_log_prefixed_message_func);

// prepare minimum necessary
// Prepare the minimum necessary.
::dsn::service_engine::instance().init_before_toollets(spec);

LOG_INFO("process({}) start: {}, date: {}",
getpid(),
dsn::utils::process_start_millis(),
dsn::utils::process_start_date_time_mills());

// init toollets
for (auto it = spec.toollets.begin(); it != spec.toollets.end(); ++it) {
auto tlet =
dsn::tools::internal_use_only::get_toollet(it->c_str(), ::dsn::PROVIDER_TYPE_MAIN);
// Initialize toollets.
for (const auto &toollet_name : spec.toollets) {
auto tlet = dsn::tools::internal_use_only::get_toollet(toollet_name.c_str(),
::dsn::PROVIDER_TYPE_MAIN);
CHECK_NOTNULL(tlet, "toolet not found");
tlet->install(spec);
}
Expand Down Expand Up @@ -507,29 +494,37 @@ bool run(const char *config_file,
}
}

// split app_name and app_index
std::list<std::string> applistkvs;
::dsn::utils::split_args(app_list.c_str(), applistkvs, ';');
// init apps
for (auto &sp : spec.app_specs) {
if (!sp.run)
if (!sp.run) {
continue;
}

bool create_it = false;
// create all apps
if (app_list == "") {
if (app_list.empty()) {
create_it = true;
} else {
for (auto &kv : applistkvs) {
std::list<std::string> argskvs;
::dsn::utils::split_args(kv.c_str(), argskvs, '@');
if (std::string("apps.") + argskvs.front() == sp.config_section) {
if (argskvs.size() < 2)
// Extract app_names.
std::list<std::string> app_names_and_indexes;
::dsn::utils::split_args(app_list.c_str(), app_names_and_indexes, ';');
for (const auto &app_name_and_index : app_names_and_indexes) {
std::vector<std::string> name_and_index;
::dsn::utils::split_args(app_name_and_index.c_str(), name_and_index, '@');
CHECK(!name_and_index.empty(),
"app_name should be specified in '{}'",
app_name_and_index);
if (std::string("apps.") + name_and_index.front() == sp.config_section) {
if (name_and_index.size() < 2) {
create_it = true;
else
create_it = (std::stoi(argskvs.back()) == sp.index);
} else {
int32_t index = 0;
const auto index_str = name_and_index.back();
CHECK(dsn::buf2int32(index_str, index),
"'{}' is not a valid index",
index_str);
create_it = (index == sp.index);
}
break;
}
}
Expand All @@ -540,10 +535,11 @@ bool run(const char *config_file,
}
}
if (dsn::service_engine::instance().get_all_nodes().size() == 0) {
printf("no app are created, usually because \n"
"app_name is not specified correctly, should be 'xxx' in [apps.xxx]\n"
"or app_index (1-based) is greater than specified count in config file\n");
if (dsn::service_engine::instance().get_all_nodes().empty()) {
fmt::print(stderr,
"no app are created, usually because \n"
"app_name is not specified correctly, should be 'xxx' in [apps.xxx]\n"
"or app_index (1-based) is greater than specified count in config file\n");
exit(1);
}
Expand Down
8 changes: 4 additions & 4 deletions src/utils/simple_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,21 +318,21 @@ void simple_logger::flush()

void simple_logger::print_header(log_level_t log_level)
{
add_bytes_if_valid(::dsn::tools::print_header(_log, _stderr_start_level, log_level));
::dsn::tools::print_header(_log, _stderr_start_level, log_level);
}

void simple_logger::print_long_header(const char *file,
const char *function,
const int line,
log_level_t log_level)
{
add_bytes_if_valid(::dsn::tools::print_long_header(
_log, file, function, line, FLAGS_short_header, _stderr_start_level, log_level));
::dsn::tools::print_long_header(
_log, file, function, line, FLAGS_short_header, _stderr_start_level, log_level);
}

void simple_logger::print_body(const char *body, log_level_t log_level)
{
add_bytes_if_valid(::dsn::tools::print_body(_log, body, _stderr_start_level, log_level));
::dsn::tools::print_body(_log, body, _stderr_start_level, log_level);
}

void simple_logger::log(
Expand Down
7 changes: 0 additions & 7 deletions src/utils/simple_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,6 @@ class simple_logger : public logging_provider
log_level_t log_level);
void print_body(const char *body, log_level_t log_level);

inline void add_bytes_if_valid(int bytes)
{
if (dsn_likely(bytes > 0)) {
_file_bytes += static_cast<uint64_t>(bytes);
}
}

void create_log_file();

private:
Expand Down

0 comments on commit 34efb46

Please sign in to comment.