内存优化

This commit is contained in:
2026-08-28 11:49:06 +08:00
parent e26f7eb52f
commit fa1e7e24fc
21 changed files with 722 additions and 237 deletions
@@ -15,14 +15,16 @@ std::shared_ptr<SSR::Msg> Data_Source_Handler::create_msg(std::string_view packe
std::cout << "first:" << mem2hex(std::string(packet)) << std::endl;
return nullptr;
}
std::string error_len = source->key + " " + LOG_POS_SIMPLE + std::string(" ") + "mode_s_error_length";
auto error_len = [&] {
return source->key + " " + LOG_POS_SIMPLE + std::string(" ") + "mode_s_error_length";
};
if (packet.size() < 2)
return nullptr;
auto mt = packet[1];
if (mt == SSR::Msg::AC) {
if (packet.size() != SSR::Msg::AC_len) {
auto packet_hex = mem2hex(std::string(packet));
SSR::mode_s_logger->c_debug(error_len, {},
SSR::mode_s_logger->c_debug(error_len(), {},
std::to_string(mt) + " size:" + std::to_string(packet.size()) +
" hex:" + packet_hex + " should:" + std::to_string(SSR::Msg::AC_len));
mode_ac_statistic.add_length_error();
@@ -34,7 +36,7 @@ std::shared_ptr<SSR::Msg> Data_Source_Handler::create_msg(std::string_view packe
if (mt == SSR::Msg::S7) {
if (packet.size() != SSR::Mode_S_Msg::S7_len) {
auto packet_hex = mem2hex(std::string(packet));
SSR::mode_s_logger->c_debug(error_len, {},
SSR::mode_s_logger->c_debug(error_len(), {},
std::to_string(mt) + " size:" + std::to_string(packet.size()) +
" hex:" + packet_hex + " should:" + std::to_string(SSR::Msg::S7_len));
mode_s_statistic.add_length_error("[length error] Msg::S7 handle_mode_s_source");
@@ -45,7 +47,7 @@ std::shared_ptr<SSR::Msg> Data_Source_Handler::create_msg(std::string_view packe
if (mt == SSR::Msg::S14) {
if (packet.size() != SSR::Msg::S14_len) {
auto packet_hex = mem2hex(std::string(packet));
SSR::mode_s_logger->c_debug(error_len, {},
SSR::mode_s_logger->c_debug(error_len(), {},
std::to_string(mt) + " size:" + std::to_string(packet.size()) +
" hex:" + packet_hex + " should:" + std::to_string(SSR::Msg::S14_len));
mode_s_statistic.add_length_error("[length error] Msg::S14 handle_mode_s_source");
@@ -57,7 +59,7 @@ std::shared_ptr<SSR::Msg> Data_Source_Handler::create_msg(std::string_view packe
return nullptr; // 不知道如何解析跳过
if (packet.size() != SSR::Msg::Radarcape_status_len) {
auto packet_hex = mem2hex(std::string(packet));
SSR::mode_s_logger->c_debug(error_len, {},
SSR::mode_s_logger->c_debug(error_len(), {},
std::to_string(mt) + " size:" + std::to_string(packet.size()) + " hex:" +
packet_hex + " should:" + std::to_string(SSR::Msg::Radarcape_status_len));
mode_s_statistic.add_length_error("[length error] Msg::Radarcape_status_len handle_mode_s_source");
@@ -70,7 +72,7 @@ std::shared_ptr<SSR::Msg> Data_Source_Handler::create_msg(std::string_view packe
auto packet_hex = mem2hex(std::string(packet));
// 找不到协议 size:23 hex:1A34195F0000001500B502FF06F423CE50000090000000
// should:5
SSR::mode_s_logger->c_debug(error_len, {},
SSR::mode_s_logger->c_debug(error_len(), {},
Psc::to_string(source->type) + " HULC size:" + std::to_string(packet.size()) +
" hex:" + packet_hex);
return nullptr;
@@ -94,14 +96,9 @@ size_t Data_Source_Handler::process_mode_acs_data(std::string_view origin_data)
std::cout << " after origin_data_transform_mode_data " << VAR_STR_2(name, mode_data.size()) << key
<< std::endl;
}
static Value_Growth_Multi_T mt;
auto t = key;
if (data_source_debug) {
std::cout << name << " 读取到:" << VAR_STR_2(key, mode_data.size()) << " " << std::endl;
}
if (mt.test(key, mode_data.size())) {
std::cout << VAR_STR_2(key, mode_data.size()) << " 数据增长过快,可能内存积压" << std::endl;
}
size_t ret = 0;
if (mode_data.empty()) {
return ret;
@@ -120,6 +117,8 @@ size_t Data_Source_Handler::process_mode_acs_data(std::string_view origin_data)
}
auto handle_packet = [this, source, &ret](std::string& packet) {
ret++;
if (source->discard_input_packet(packet))
return;
auto msg = create_msg(packet);
if (!msg)
return;
@@ -145,30 +144,14 @@ size_t Data_Source_Handler::process_mode_acs_data(std::string_view origin_data)
source->push_to_feed(msg);
}
};
auto f = [this, source, handle_packet](std::string& packet) {
const auto wait = Global::instance()->mode_acs.settings.member<&Mode_ACS_Config_Data::wait_process_msg>().read([](const auto& value) { return value; });
if (wait) {
handle_packet(packet);
}
else {
auto co = Coro::instance();
auto executor = co->process_data->get_executor();
asio::post(executor, [handle_packet, packet = std::move(packet)]() mutable { handle_packet(packet); });
}
auto f = [handle_packet](std::string& packet) {
handle_packet(packet);
};
SSR::Binary_Format_handle_buffer(source->buffer, mode_data, f);
return ret;
}
void Data_Source_Handler::handle_mode_s(std::shared_ptr<SSR::Mode_S_Msg>& mode_s_msg) {
auto source = ds(this);
std::string t =
mode_s_msg->mlat_timestamp.to_memory() + mode_s_msg->signal_level + Psc::hex2mem(mode_s_msg->msg_hex);
auto p = mode_s_msg->packet.substr(2);
if (t != p) {
auto data = std::format("编解码出问题了(组合的数据,拼接的数据) hex:[{}] [{}] bin:[{}] [{}]",
mem2hex(t, true, " "), mem2hex(mode_s_msg->packet, true, " "), t, mode_s_msg->packet);
Psc::fail_fast(data);
}
const auto mode_config = Global::instance()->mode_acs.settings.read([](const auto& value) {
return std::tuple{value.time_space_filter, value.speed_filter, value.aircraft_change_list_adsb_range_filter, value.aircraft_change_list_adsb_range_factor, value.max_speed_m_s, value.air_pos_timeout, value.surface_pos_timeout};
});
@@ -181,9 +164,11 @@ void Data_Source_Handler::handle_mode_s(std::shared_ptr<SSR::Mode_S_Msg>& mode_s
if (!base_station_pos && base_station_has_valid_position)
base_station_pos = SSR::Position_3D{lat, lon, alt};
SSR::ADS_B_T::Constraint air_constraint{max_speed_m_s, air_pos_timeout, SSR::cpr_cb, time_space_filter,
speed_filter, use_system_time, base_station_pos, alt, range_filter, range_factor};
speed_filter, use_system_time, base_station_pos, alt, range_filter, range_factor,
false};
SSR::ADS_B_T::Constraint surface_constraint{max_speed_m_s, surface_pos_timeout, SSR::cpr_cb, time_space_filter,
speed_filter, use_system_time, base_station_pos, alt, range_filter, range_factor};
speed_filter, use_system_time, base_station_pos, alt, range_filter, range_factor,
false};
SSR::parse_mode_s_bin(source.get(), mode_s_msg, base_station_pos, air_constraint, surface_constraint);
auto base = source->get_aircraft(mode_s_msg->icao);
if (base) {
@@ -271,7 +256,7 @@ void Data_Source_Handler::refresh_data_feed_key_list() {
std::vector<Cached_Source_Info> next;
for (const auto& feed : Global::instance()->mode_acs.data_feed_config.map.list()) {
if (feed->enabled() && feed->source_key == key)
next.push_back({feed->key});
next.push_back({feed->key, feed});
}
std::lock_guard<std::mutex> lock(cdf_mtx);
for (auto& item : next) {
@@ -287,14 +272,8 @@ void Data_Source_Handler::refresh_data_feed_key_list() {
}
std::optional<std::string> convert_to_send_format(Data_Source_Handler* ds, const std::shared_ptr<Data_Feed>& feed,
const std::shared_ptr<SSR::Msg>& msg) {
const std::shared_ptr<SSR::Msg>& msg) {
auto& type = msg->type;
auto fs = dynamic_cast<File_Data_Source*>(msg->source.get());
if (fs) {
if (fs->specific.member<&File_Data_Source_Data::play_mode>().read([](const auto& value) { return value; }) == Play_Mode::analysis) {
// std::cout << fs->key << " analysis i ==" << i << std::endl;
}
}
// 出口输出的条件
const auto feed_format = feed->output_format.read([](const auto& value) { return value; });
const auto output_format = feed_format.type;
@@ -409,20 +388,8 @@ void Data_Source_Handler::push_to_feed(const std::shared_ptr<SSR::Msg>& msg) {
const auto [other_size, s_size] = Global::instance()->mode_acs.settings.read([](const auto& value) {
return std::pair{value.mode_other_max_num, value.mode_s_max_num};
});
std::vector<std::string> feed_keys;
{
std::lock_guard lock(cdf_mtx);
feed_keys.reserve(cached_data_feed_key_list.size());
for (const auto& item : cached_data_feed_key_list)
feed_keys.push_back(item.key);
}
for (const auto& key : feed_keys) {
auto opt_feed = Global::instance()->mode_acs.data_feed_config.map.get(key);
if (!opt_feed.has_value()) {
std::cout << "wrong data feed: " << key << std::endl;
break;
}
std::shared_ptr<Data_Feed>& feed = opt_feed.value();
for (const auto& item : cached_data_feed_key_list) {
const auto& feed = item.feed;
if (!feed->enabled())
continue;
const bool mode_s = msg->type == SSR::Mode_Msg::T::S7 || msg->type == SSR::Mode_Msg::T::S14;