ROS1录包偶现一次崩溃问题定位

现象:崩到了mogo_reporter里面

堆栈:crash里面同时存在两个主线程的堆栈(两个线程并发调用main函数)

代码

#include "boost/program_options.hpp"

#include <signal.h>

#include <string>

#include <sstream>

#include <iostream>

#include <thread>

#include <ros/ros.h>

#include "std_msgs/String.h"

#include "mogo_reporter/mogo_reporter.h"

#include "master.h"

#include "slave.h"

#include "configure.h"

namespace po = boost::program_options;

std::atomic<bool> gShutdown(false);

std::atomic<bool> gShouldRestart(true);

/**

 * Handle SIGTERM to allow the recorder to cleanup by requesting a shutdown.

 * \param signal

 */

void signal_handler(int signal)

{

  (void) signal;

  ros::requestShutdown();

  gShutdown.store(true);

  gShouldRestart.store(false);

}

int main(int argc, char** argv) {

    ros::init(argc, argv, "record_cache", ros::init_options::AnonymousName);

    MOGO_MSG_INIT_CFG("record_cache");

    gShouldRestart.store(false);

    setlocale(LC_ALL, "");

     

    signal(SIGTERM, signal_handler);

    signal(SIGINT, signal_handler);

    po::options_description desc("Allowed options");

    desc.add_options()

    ("help,h""produce help message")

    ("file,f",po::value<std::string>(),"config file path")

    ("create,c""create a record task")

    ("master""run as master")

    ("slave""run as slave");

    po::positional_options_description p;

    po::variables_map vm;

    try

    {

      po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);

    }

    catch (const boost::program_options::invalid_command_line_syntax &e)

    {

      ROS_ERROR("Error reading options: %s", e.what());

      return 1;

    }

    catch (const boost::program_options::unknown_option &e)

    {

      ROS_ERROR("Unknown options:%s", e.what());

      return 1;

    }

    if (vm.count("help"))

    {

      std::cout << desc << std::endl;

      exit(0);

    }

    if (vm.count("create"))

    {

    }

    if (vm.count("master"))

    {

    }

    if (vm.count("slave"))

    {

    }

    if (vm.count("file"))

    {

      record_cache::Configure::instance()->load(vm["file"].as<std::string>());

    }

    std::thread t([](){

      while(!gShutdown.load()) {

        if(!ros::master::check() && !gShouldRestart.load()) {

          ROS_WARN_STREAM("master offline, request shutdown!");

          ros::requestShutdown();

          gShouldRestart.store(true);

          break;

        }

        std::this_thread::sleep_for(std::chrono::milliseconds(200));

      }

    });

    t.detach();

    ros::NodeHandle node_handle("~");

    std::shared_ptr<record_cache::TaskManager> managerPtr;

    std::string hostname;

    char* str = getenv("ROS_HOSTNAME");

    if(!str) {

      hostname = "unknown";

    else {

      hostname = str;

    }

       

    ROS_DEBUG_STREAM("HOST:" << ros::master::getHost());

    std::string master = ros::master::getHost();

    if(hostname == master) {

      managerPtr = std::make_shared<record_cache::Master>();

    else {

      managerPtr = std::make_shared<record_cache::Slave>();

    }

       

    double pre_allocate_count_other;

    node_handle.param("pre_allocate_count_other", pre_allocate_count_other, static_cast<double>(1.5));

    managerPtr->setPreAllocateCountOther(pre_allocate_count_other);

    double pre_allocate_count_106;

    node_handle.param("pre_allocate_count_106", pre_allocate_count_106, static_cast<double>(2.5));

    managerPtr->setPreAllocateCount106(pre_allocate_count_106);

    int pre_allocate_pice_size = 0;

    node_handle.param("pre_allocate_pice_size", pre_allocate_pice_size, 100);

    managerPtr->setPreAllocatePiceSize(pre_allocate_pice_size);

    int trigger_mast_running_tasks_num = 3;

    node_handle.param("trigger_mast_running_tasks_num", trigger_mast_running_tasks_num, 3);

    managerPtr->setTriggerMastRunningTasksNum(trigger_mast_running_tasks_num);

    int trigger_mast_bduration = 10;

    node_handle.param("trigger_mast_bduration", trigger_mast_bduration, 10);

    managerPtr->setTriggerMastBduration(trigger_mast_bduration);

    int filter_record_interval = 5;

    node_handle.param("filter_record_interval", filter_record_interval, 5);

    managerPtr->setFilterRecordInterval(filter_record_interval);

    int single_msg_mast_size = 15;

    node_handle.param("single_msg_mast_size", single_msg_mast_size, 15);

    managerPtr->setSingleMsgMastSize(single_msg_mast_size);

    int bags_max_disk_space = 150;

    node_handle.param("bags_max_disk_space", bags_max_disk_space, 150);

    managerPtr->setbagsMaxDiskSpace(bags_max_disk_space);

     

    int record_time_split_size_gnss = 3600;

    node_handle.param("record_time_split_size_gnss", record_time_split_size_gnss, 3600);

    managerPtr->setRecordTimeSplitSizeGnss(record_time_split_size_gnss);

    int record_time_split_size_test = 1800;

    node_handle.param("record_time_split_size_test", record_time_split_size_test, 1800);

    managerPtr->setRecordTimeSplitSizeTest(record_time_split_size_test);

     

    // TODO 调整spin线程数量

    ros::AsyncSpinner s(0);

    s.start();

    managerPtr->start();

    ros::waitForShutdown();

     

    sleep(1);

    if(gShouldRestart.load()) {

      return 8;

    }

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ปรัชญา แค้วคำมูล

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值