C ++套接字服务器-无法饱和CPU [英] C++ Socket Server - Unable to saturate CPU

查看:76
本文介绍了C ++套接字服务器-无法饱和CPU的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用boost :: asio在C ++中开发了一个微型HTTP服务器,现在我正在使用多个客户端对其进行负载测试,但我无法接近使CPU饱和的程度.我正在Amazon EC2实例上进行测试,一个CPU的使用率约为50%,另一个CPU的使用率为20%,其余两个则处于闲置状态(根据htop).

I've developed a mini HTTP server in C++, using boost::asio, and now I'm load testing it with multiple clients and I've been unable to get close to saturating the CPU. I'm testing on a Amazon EC2 instance, and getting about 50% usage of one cpu, 20% of another, and the remaining two are idle (according to htop).

详细信息:

  • 服务器每个核心启动一个线程
  • 接收,解析,处理请求并写出响应
  • 请求是针对从内存中读取的数据(此测试为只读)
  • 我正在使用两台计算机加载"服务器,每台计算机运行一个Java应用程序,运行25个线程,发送请求
  • 我看到大约230个请求/秒的吞吐量(这是 application 请求,由许多HTTP请求组成)
  • The server fires up one thread per core
  • Requests are received, parsed, processed, and responses are written out
  • The requests are for data, which is read out of memory (read-only for this test)
  • I'm 'loading' the server using two machines, each running a java application, running 25 threads, sending requests
  • I'm seeing about 230 requests/sec throughput (this is application requests, which are composed of many HTTP requests)

那么,我应该怎么看才能改善这个结果?鉴于CPU大部分处于空闲状态,我想利用这些额外的容量来获得更高的吞吐量,例如800个请求/秒或其他.

So, what should I look at to improve this result? Given the CPU is mostly idle, I'd like to leverage that additional capacity to get a higher throughput, say 800 requests/sec or whatever.

我的想法:

  • 请求非常小,通常会在几毫秒内完成,我可以修改客户端以发送/编写更大的请求(也许使用批处理)
  • 我可以修改HTTP服务器以使用选择"设计模式,这是否合适?
  • 我可以进行一些剖析以尝试了解瓶颈所在/是

推荐答案

boost :: asio不像您希望的那样对线程友好-在boost/asio/detail/epoll_reactor中的epoll代码有很大的锁定. hpp,这意味着一次只能有一个线程可以调用内核的epoll syscall.对于非常小的请求,这将带来所有不同(意味着您只会看到大约单线程性能).

boost::asio is not as thread-friendly as you would hope - there is a big lock around the epoll code in boost/asio/detail/epoll_reactor.hpp which means that only one thread can call into the kernel's epoll syscall at a time. And for very small requests this makes all the difference (meaning you will only see roughly single-threaded performance).

请注意,这是boost :: asio使用Linux内核工具(不一定是Linux内核本身)的方式的限制.使用边缘触发的事件时,epoll syscall确实支持多个线程,但是正确(没有过多锁定)可能会非常棘手.

Note that this is a limitation of how boost::asio uses the Linux kernel facilities, not necessarily the Linux kernel itself. The epoll syscall does support multiple threads when using edge-triggered events, but getting it right (without excessive locking) can be quite tricky.

顺便说一句,我一直在这方面做一些工作(将完全多线程的边缘触发epoll事件循环与用户预定的线程/光纤相结合),并在 nginetd 项目.

BTW, I have been doing some work in this area (combining a fully-multithreaded edge-triggered epoll event loop with user-scheduled threads/fibers) and made some code available under the nginetd project.

这篇关于C ++套接字服务器-无法饱和CPU的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆