Replace fixed 518MB array in TaskData with dynamic shared-memory vector

DataRecord used a fixed float[129600000] consuming 5GB disk even when
collecting only a few hundred data points. Replaced with shm_vector_f
that grows on demand via push_back. Removes the need for rm -rf on
process exit — vector destructor frees memory back to the segment.
Also drops now-unnecessary task_data_size member.
This commit is contained in:
Huamonarch 2026-05-12 17:19:44 +08:00
parent 1ca922a4ef
commit e21b2af2a6
4 changed files with 22 additions and 37 deletions

View File

@ -149,7 +149,6 @@ AlarmInfo ExpBase::exec_mon() {
} }
std::vector<AlarmInfo> ExpBase::exec_task(mix_cc::time_range_t time_range) { std::vector<AlarmInfo> ExpBase::exec_task(mix_cc::time_range_t time_range) {
task_data_size = 0;
std::vector<AlarmInfo> out_alarms; std::vector<AlarmInfo> out_alarms;
/*统计类,需要单独处理数据*/ /*统计类,需要单独处理数据*/
@ -197,12 +196,12 @@ std::vector<AlarmInfo> ExpBase::exec_task(mix_cc::time_range_t time_range) {
this->sta_ptr_->reset_data(); /*参数重置*/ this->sta_ptr_->reset_data(); /*参数重置*/
logger_->Debug() << "reset_data()------2" << endl; logger_->Debug() << "reset_data()------2" << endl;
logger_->Debug() << "task_seq:" << task_seq << std::endl; logger_->Debug() << "task_seq:" << task_seq << std::endl;
logger_->Debug() << "dataSize:" << task_data_size << std::endl; auto &data_record = TaskShm::TaskRecordPtr.get()
for (size_t j = 0; j < task_data_size; j++) { ->
double dataJ = TaskShm::TaskRecordPtr.get() operator[](exp_type_ * 1000 + task_seq);
-> logger_->Debug() << "dataSize:" << data_record.size() << std::endl;
operator[](exp_type_ * 1000 + task_seq) for (size_t j = 0; j < data_record.size(); j++) {
.data_record[j]; double dataJ = data_record[j];
this->sta_ptr_->dist_add(dataJ); this->sta_ptr_->dist_add(dataJ);
} }
logger_->Debug() << "reset_data()------3" << endl; logger_->Debug() << "reset_data()------3" << endl;
@ -1364,7 +1363,7 @@ void ExpBase::task_mon_pro() {
TaskShm::TaskRecordPtr.get() TaskShm::TaskRecordPtr.get()
-> ->
operator[](exp_type_ * 1000 + task_seq) operator[](exp_type_ * 1000 + task_seq)
.data_record[task_data_size++] = res.value; .push_back(res.value);
this->sta_ptr_->running_stat_add(res.value); this->sta_ptr_->running_stat_add(res.value);
} }
} }

View File

@ -191,8 +191,6 @@ protected:
std::unique_ptr<mix_cc::matheval::Expression> exp_result_; std::unique_ptr<mix_cc::matheval::Expression> exp_result_;
size_t task_data_size = 0;
protected: protected:
const size_t const size_t
exp_type_; exp_type_;

View File

@ -94,8 +94,7 @@ EqpAlgICEI::~EqpAlgICEI() {
} }
} }
if (glob_process_type == ProcessType::kTask) { if (glob_process_type == ProcessType::kTask) {
string ss0 = "rm -rf /users/dsc/shm/TaskData_boost.mmap"; // TaskData 已改用动态 vector进程退出时自然析构无需 rm -rf
system(ss0.c_str());
} }
this->logger_->Info() << "EqpAlgICEI::~EqpAlgICEI() " this->logger_->Info() << "EqpAlgICEI::~EqpAlgICEI() "
<< "Destruct" << endl; << "Destruct" << endl;

View File

@ -1,14 +1,13 @@
#pragma once #pragma once
/** /**
* @file TaskData.h * @file TaskData.h
* @brief task进程 * @brief task进程vector
* @author your name (you@domain.com) * @author your name (you@domain.com)
* @version 0.1 * @version 0.2
* @date 2024-02-27 * @date 2024-02-27
* *
* Copyright: Baosight Co. Ltd. * Copyright: Baosight Co. Ltd.
* DO NOT COPY/USE WITHOUT PERMISSION * DO NOT COPY/USE WITHOUT PERMISSION
*
*/ */
#include <mix_cc/shm.h> #include <mix_cc/shm.h>
namespace TaskShm { namespace TaskShm {
@ -17,36 +16,26 @@ namespace bipc = mix_cc::shm::bip;
const static std::string dir_path = "/users/dsc/shm"; const static std::string dir_path = "/users/dsc/shm";
const static std::string shm_file = "TaskData"; ///<映射文件名 const static std::string shm_file = "TaskData"; ///<映射文件名
const double data_size = 500*10; ///< 数据大小 MB const double data_size = 10; ///< 数据大小 MB按需动态
const size_t MAX_LEN_DAYS = 30; ///<最多可放30天的数据
const size_t DATA_COL_FRE = 50; ///<每秒采集数据点数
static bipc::managed_mapped_file static bipc::managed_mapped_file
obj_mapped_file(bipc::open_or_create, obj_mapped_file(bipc::open_or_create,
(dir_path + "/" + shm_file + "_boost.mmap").c_str(), (dir_path + "/" + shm_file + "_boost.mmap").c_str(),
mix_cc::data_size::MB(data_size)); mix_cc::data_size::MB(data_size));
struct DataRecord { typedef bipc::managed_mapped_file::segment_manager segment_manager_t;
float data_record[MAX_LEN_DAYS * 24 * 60 * 60 * DATA_COL_FRE]; typedef bipc::node_allocator<float, segment_manager_t> vec_allocator_f;
typedef boost::container::vector<float, vec_allocator_f> shm_vector_f;
struct TaskRecord {
shm_vector_f data_record;
TaskRecord(const bipc::node_allocator<void, segment_manager_t> &alloc)
: data_record(alloc) {}
}; };
/**
* @brief ,
*/
struct TimeRecord {
int stime;
int etime;
size_t data_size; ///<数据长度
};
/*单例 map 记录task数据项*/ /*单例 map 记录task数据项*/
const static bipc::offset_ptr<SHM::Map<int, DataRecord>> TaskRecordPtr = const static bipc::offset_ptr<SHM::Map<int, TaskRecord>> TaskRecordPtr =
SHM::MapBuilder<int, DataRecord>::find_or_construct(&obj_mapped_file, SHM::MapBuilder<int, TaskRecord>::find_or_construct(&obj_mapped_file,
shm_file); shm_file);
/*单例 TimeRecord 保持 记录的起止时间*/
// const static bipc::offset_ptr<SHM::Map<int, TimeRecord>> TimeRecordPtr =
// SHM::MapBuilder<int, TimeRecord>::find_or_construct(&obj_mapped_file,
// shm_file);
/*单例 TimeRecord 保持 记录的起止时间*/
// const static bipc::offset_ptr<TimeRecord> TimeRecordPtr =
// obj_mapped_file.find_or_construct<TimeRecord>("TimeRecord")();
} // namespace TaskShm } // namespace TaskShm