-1

我正在使用这样配置的多线程 websocketpp 服务器:

Server::Server(int ep) {
    using websocketpp::lib::placeholders::_1;
    using websocketpp::lib::placeholders::_2;
    using websocketpp::lib::bind;

    Server::wspp_server.clear_access_channels(websocketpp::log::alevel::all);

    Server::wspp_server.init_asio();

    Server::wspp_server.set_open_handler(bind(&Server::on_open, this, _1));;
    Server::wspp_server.set_close_handler(bind(&Server::on_close, this, _1));
    Server::wspp_server.set_message_handler(bind(&Server::on_message, this, _1, _2));

    try {
        Server::wspp_server.listen(ep);
    } catch (const websocketpp::exception &e){
        std::cout << "Error in Server::Server(int): " << e.what() << std::endl;
    }
    Server::wspp_server.start_accept();
}

void Server::run(int threadCount) {
    boost::thread_group tg;

    for (int i = 0; i < threadCount; i++) {
        tg.add_thread(new boost::thread(
            &websocketpp::server<websocketpp::config::asio>::run,
            &Server::wspp_server));
        std::cout << "Spawning thread " << (i + 1) << std::endl;
    }

    tg.join_all();
}

void Server::updateClients() {
    /*
       run updates
    */
    for (websocketpp::connection_hdl hdl : Server::conns) {
        try {
            std::string message = "personalized message for this client from the ran update above";
            wspp_server.send(hdl, message, websocketpp::frame::opcode::text);
        } catch (const websocketpp::exception &e) {
            std::cout << "Error in Server::updateClients(): " << e.what() << std::endl;
        }
    }
}

void Server::on_open(websocketpp::connection_hdl hdl) {
    boost::lock_guard<boost::shared_mutex> lock(Server::conns_mutex);
    Server::conns.insert(hdl);

    //do stuff


    //when the first client connects, start the update routine
    if (conns.size() == 1) {
        Server::run = true;
        bool *run = &(Server::run);
        std::thread([run] () {
            while (*run) {
                auto nextTime = std::chrono::steady_clock::now() + std::chrono::milliseconds(15);
                Server::updateClients();
                std::this_thread::sleep_until(nextTime);
            }
        }).detach();
    }
}

void Server::on_close(websocketpp::connection_hdl hdl) {
    boost::lock_guard<boost::shared_mutex> lock(Server::conns_mutex);
    Server::conns.erase(hdl);

    //do stuff

    //stop the update loop when all clients are gone
    if (conns.size() < 1)
        Server::run = false;
}

void Server::on_message(
        websocketpp::connection_hdl hdl,
        websocketpp::server<websocketpp::config::asio>::message_ptr msg) {
    boost::lock_guard<boost::shared_mutex> lock(Server::conns_mutex);

    //do stuff
}

我启动服务器:

int port = 9000;
Server server(port);
server.run(/* number of threads */);

添加连接时唯一的实质性区别在于消息发射 [ wssp.send(...)]。越来越多的客户端并没有真正为内部计算增加任何东西。只有要发出的消息量才会增加。

我的问题是,无论我使用 1 个或多个线程,CPU 使用率似乎都没有太大差异。

server.run(1)我用or启动服务器并不重要server.run(4)(都在 4 核 CPU 专用服务器上)。对于类似的负载,CPU 使用率图表显示的百分比大致相同。我期望使用 4 个并行运行的线程来降低使用率。我想错了吗?

在某些时候,我感觉到并行性确实比发射更适用于聆听部分。因此,我尝试将其封闭send在一个新线程(我分离)中,因此它独立于需要它的序列,但它并没有改变图表上的任何内容。

我不应该看到 CPU 产生的工作有什么不同吗?否则,我做错了什么?为了强制从不同的线程发出消息,我是否还缺少另一个步骤?

4

1 回答 1

1

“我的问题是,无论我使用 1 个线程还是多个线程,CPU 使用率似乎都没有太大差异。”

这不是问题。这是事实。这只是意味着整个事情不受CPU限制。这应该很明显,因为它是网络 IO。事实上,出于这个原因,高性能服务器通常只将 1 个线程专用于所有 IO 任务。

“我原以为并行运行 4 个线程时使用率会更低。我是不是想错了?”

是的,似乎是。如果您以 4 种方式分摊账单,您也不希望支付更少的费用。

事实上,就像在餐厅一样,由于分担负载(成本/任务)的开销,您通常最终会支付更多费用。除非您需要比单个线程提供的更多 CPU 容量/更低的反应时间,否则单个 IO 线程(显然)更有效,因为没有调度开销和/或上下文切换损失。

另一个心理锻炼:

  • 如果您运行 100 个线程,处理器将在您的可用内核上调度它们,在最佳情况下
  • 同样,如果您的系统上正在运行其他进程(显然,总是存在),那么处理器可能会将您的 4 个线程全部安排在同一个逻辑核心上。您希望 CPU 负载更低吗?为什么?(提示:当然不是)。

背景:并发、并行和异步方法有什么区别?

于 2017-12-18T07:50:40.647 回答