High processor performance in boost :: asio :: io_service :: run

I had a strange problem with boost :: asio :: io_service :: run. Sometimes this feature runseems to consume the entire processor (100%), and sometimes not. I am not very clear about the template.

corresponding code:

class Asio {
 public:
  Asio() :
      io_service_(new boost::asio::io_service),
      run_() {}

  void Start() {
    if (!asio_thread_.joinable()) {
      run_ = true;
      asio_thread_ = std::thread([=] {
        Run();
      });
    }
  }

  boost::asio::io_service* io_service() { return io_service_.get(); }

 protected:
  void Run() {
    for (;;) {
      boost::system::error_code ec;
      io_service()->run(ec);

      if (run_) {
        io_service()->reset();
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
      } else {
        break;
      }
    }
  }

 protected:
  std::unique_ptr<boost::asio::io_service>    io_service_;
  std::thread                                 asio_thread_;
  std::atomic<bool>       run_;
};

When the function runworks fine, below is the column

#0  0x00000035f74e9163 in epoll_wait () from /lib64/libc.so.6
#1  0x0000000000b3f6ef in boost::asio::detail::epoll_reactor::run(bool, boost::asio::detail::op_queue<boost::asio::detail::task_io_service_operation>&) ()                                       
#2  0x0000000000b40111 in boost::asio::detail::task_io_service::do_run_one(boost::asio::detail::scoped_lock<boost::asio::detail::posix_mutex>&, boost::asio::detail::task_io_service_thread_info&, boost::system::error_code const&) ()
#3  0x0000000000b3feaf in boost::asio::detail::task_io_service::run(boost::system::error_code&) ()
#4  0x0000000000b403fd in boost::asio::io_service::run(boost::system::error_code&) ()
#5  0x0000000000b3ddc1 in Asio::Run() ()

When the function runbehaves abnormally, below is the column:

#0  0x00000031bbee53c9 in syscall () from /lib64/libc.so.6
#1  0x00007f831d1d3d68 in std::chrono::_V2::steady_clock::now() () from /usr/local/gcc48/lib64/libstdc++.so.6
#2  0x0000000000b45b6d in boost::asio::detail::chrono_time_traits<std::chrono::_V2::steady_clock, boost::asio::wait_traits<std::chrono::_V2::steady_clock> >::now() ()
#3  0x0000000000b45608 in boost::asio::detail::timer_queue<boost::asio::detail::chrono_time_traits<std::chrono::_V2::steady_clock, boost::asio::wait_traits<std::chrono::_V2::steady_clock> > >::get_ready_timers(boost::asio::detail::op_queue<boost::asio::detail::task_io_service_operation>&) ()
#4  0x0000000000b3f5d7 in boost::asio::detail::timer_queue_set::get_ready_timers(boost::asio::detail::op_queue<boost::asio::detail::task_io_service_operation>&) ()
#5  0x0000000000b3f815 in boost::asio::detail::epoll_reactor::run(bool, boost::asio::detail::op_queue<boost::asio::detail::task_io_service_operation>&) ()                                       
#6  0x0000000000b40111 in boost::asio::detail::task_io_service::do_run_one(boost::asio::detail::scoped_lock<boost::asio::detail::posix_mutex>&, boost::asio::detail::task_io_service_thread_info&, boost::system::error_code const&) ()
#7  0x0000000000b3feaf in boost::asio::detail::task_io_service::run(boost::system::error_code&) ()
#8  0x0000000000b403fd in boost::asio::io_service::run(boost::system::error_code&) ()
#9  0x0000000000b3ddc1 in Asio::Run() ()

In both cases, io_service has some pending handlers, so io_service::runit should not return and should wait for events.

Any advice is appreciated.

I still checked it seems due to using boost :: asio :: stable_timer. Using stable_timer involves using the following template:

boost::asio::steady_timer timer;
timer.expires_at(some_expiry, error_code)
timer.async_wait((=)(boost::system::error_code ec) {
  // some operation
  timer.expires_at(new_expiry, error_code);
  timer.asyn_wait(...);
});

, lamda.

+5
1

glusterfs , , epoll_wait 50% -100% :

(gdb) bt
#0  0x00007f556393e923 in epoll_wait () from /lib64/libc.so.6
#1  0x00007f556526fa42 in event_dispatch_epoll_worker () from /lib64/libglusterfs.so.0
#2  0x00007f5564071e25 in start_thread () from /lib64/libpthread.so.0
#3  0x00007f556393e34d in clone () from /lib64/libc.so.6

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                                             
19660 root      20   0 1468800 917156   4124 S 54.3  0.3 141:55.10 glusterepoll0                                                                                       
19301 root      20   0 1468800 917156   4124 S  1.3  0.3   0:34.83 glustertimer                                                                                        
19300 root      20   0 1468800 917156   4124 S  0.0  0.3   0:03.78 glusterd                                                                                            
19302 root      20   0 1468800 917156   4124 S  0.0  0.3   0:00.00 glustersigwait                                                                                      
19303 root      20   0 1468800 917156   4124 S  0.0  0.3   0:00.51 glustermemsweep                                                                                     
19304 root      20   0 1468800 917156   4124 S  0.0  0.3   0:00.04 glustersproc0                                                                                       
19305 root      20   0 1468800 917156   4124 S  0.0  0.3   0:22.10 glustersproc1                                                                                       
19658 root      20   0 1468800 917156   4124 S  0.0  0.3   0:00.00 glustergdhooks

.

0

Source: https://habr.com/ru/post/1548020/


All Articles