使用BOOST ASIO async_read_until读取mpstat输出时,文件意外结束 [英] Unexpected end of file while reading mpstat output with BOOST ASIO async_read_until
问题描述
我尝试读取mpstat命令的输出(每秒收集cpu信息,即: mptstat -P ALL 1),以获取有关cpu和内核使用情况的信息。在多核cpu上,在读取了第一次测量后,我得到了意外的文件结束状态。
I try to read the output of mpstat command (collecting cpu information every second, ie: "mptstat -P ALL 1") in order to get information about cpu and core usage. On a multicore cpu, I get an unexpected "end of file" status just after having read the first measurements.
看来mpstat格式化输出的方式是所有核心的测量值之间用空线分隔。
It appears that mpstat formats its output in such a way that measurements for all cores are separated by an empty line.
我使用了async_read_until,且分隔符等于'\n'。
I have used async_read_until with a delimiter equal to '\n'.
请在下面找到复制者。使用此复制器,我得到以下信息:
Please find below a small reproducer. With this reproducer, I get the following:
Enter handle_read
handle_read got data: --Linux 4.13.0-46-generic (pierre) 26/08/2018 _x86_64_ (4 CPU)
--Enter handle_read
handle_read got data: --
11:39:11 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
11:39:11 all--Enter handle_read
handle_read got data: -- 5,69 0,06 2,90 0,20 0,00 0,02 0,00 0,00 0,00 91,13
11:39:11 0 5,95 0,05--Enter handle_read
handle_read got data: -- 2,80 0,13 0,00 0,01 0,00 0,00 0,00 91,06
11:39:11 1 5,24 0,01 2,50 0,14 0,00 0,02 0,00 0,00 0,00 92,09
11:39:11 2 6,30 0,17--Enter handle_read
handle_read got data: -- 2,29 0,36 0,00 0,04 0,00 0,00 0,00 90,85
11:39:11 3 5,28 0,01 4,01 0,17 0,00 0,00 0,00 0,00 0,00 90,52
--Enter handle_read
Problem while trying to read mpstat data: End of file
基本上,我能够收到第一个测量值,但是之后我立即得到文件结尾。看起来mpstat发出的空行是为什么我得到行尾指示...但我不明白为什么...
Basically, I am able to receive the first measurement but I immediatly get an "end of file" just after. Looks like the empty line issued by mpstat is why I get the "end of line" indication... but I can't understand why...
有人可以提供一些帮助?
Could somebody provide some help? Many thanks in advance.
#include <boost/asio.hpp>
#include <unistd.h>
#include <iostream>
#define PIPE_READ 0
#define PIPE_WRITE 1
#define ENDOFLINE "\n"
static boost::asio::streambuf data;
static std::shared_ptr<boost::asio::posix::stream_descriptor> cpuLoadDataStream;
static void handle_read(const boost::system::error_code& error, std::size_t bytes_transferred) {
printf("Enter handle_read\n");
if (error == boost::system::errc::success) {
if (data.size() > 0) {
std::string dataReceived((std::istreambuf_iterator<char>(&data)), std::istreambuf_iterator<char>());
std::cout << "handle_read got data: " << "--" << dataReceived << "--";
}
boost::asio::async_read_until(*cpuLoadDataStream, data, ENDOFLINE, handle_read);
} else {
std::cout << "Problem while trying to read mpstat data: " << error.message() << std::endl;
}
}
int main() {
int pipeFd[2];
boost::asio::io_service ioService;
if (pipe(pipeFd) < 0) {
std::cout << "pipe error" << std::endl;
exit(EXIT_FAILURE);
}
int pid;
if ((pid = fork()) == 0) {
// son
close(pipeFd[PIPE_READ]);
if (dup2(pipeFd[PIPE_WRITE], STDOUT_FILENO) == -1) {
std::cout << "dup2 error" << std::endl;
exit(EXIT_FAILURE);
}
close(pipeFd[PIPE_WRITE]);
if (execlp("mpstat", "1", "-P", "ALL", 0) == -1) {
std::cout << "execlp error" << std::endl;
exit(EXIT_FAILURE);
}
} else {
// parent
if (pid == -1) {
std::cout << "fork error" << std::endl;
exit(EXIT_FAILURE);
} else {
close(pipeFd[PIPE_WRITE]);
cpuLoadDataStream = std::make_shared<boost::asio::posix::stream_descriptor>(ioService, ::dup(pipeFd[PIPE_READ]));
boost::asio::async_read_until(*cpuLoadDataStream, data, ENDOFLINE, handle_read);
}
}
ioService.run();
return 1;
}
推荐答案
首先:如果您想要 1
表示 mpstat
每隔1秒重复运行一次,您必须将其设为第一个参数,而不是进程名称:
First off: if you want "1"
to mean that mpstat
runs repeatedly at 1 second interval, you must make it the first argument, not the process name:
if (execlp("mpstat", "mpstat", "1", "-P", "ALL", 0) == -1) {
请参见文档:
按照惯例,第一个参数应指向与正在执行的文件关联的文件名。
async_read_until
读取直到输入缓冲区至少包含定界符。因此,当您读取缓冲区时,必须考虑 bytes_tranferred
,它将不包括定界符。
async_read_until
reads until the input buffer at least contains the delimiter. So when you read the buffer, you must take into account the bytes_tranferred
which will be excluding the delimiter.
确保也使用定界符以避免陷入无限循环(因为已经满足停止条件):
Make sure to also consume the delimiter to avoid getting stuck in an infinite loop (because the stopping condition is already met):
void handle_read(const boost::system::error_code &error, std::size_t bytes_transferred) {
std::cout << "Enter handle_read (" << error.message() << ")\n";
if (!error) {
if (bytes_transferred > 0) {
std::copy_n(std::istreambuf_iterator<char>(&data), bytes_transferred, std::ostreambuf_iterator<char>(std::cout << "handle_read got data: '"));
std::cout << "' --\n";
data.consume(delimiter.size());
}
do_read_loop();
}
}
或更简单:
void handle_read(const boost::system::error_code &error, std::size_t bytes_transferred) {
std::cout << "Enter handle_read (" << error.message() << ")\n";
if (!error) {
std::string line;
if (getline(std::istream(&data), line))
std::cout << "handle_read got data: '" << line << "'\n";
do_read_loop();
}
}
我更喜欢第一个,因为它更多
I'd prefer the first one because it is more explicit and generally applicable.
侧面说明:
Side Notes:
-
使用全局数据的方式存在问题(只有在相应的
io_servce
已经超出范围后,该数据才会被销毁)。那就是 UB -使用asan / ubsan可以发现这些错误。
there was a problem with the way you used global data (that would only get destroyed after the corresponding
io_servce
was already out of scope). That was UB - using asan/ubsan allows you to spot these bugs.
www.boost.org/doc/libs/1_68_0/doc/html/boost_asio/reference/io_context/notify_fork.html rel = nofollow noreferrer> https://www.boost.org/doc/libs/1_68_0/doc /html/boost_asio/reference/io_context/notify_fork.html
Using io_service
across forks is not supported without support from the service implementations, see https://www.boost.org/doc/libs/1_68_0/doc/html/boost_asio/reference/io_context/notify_fork.html
固定/简化的Asio
这里有一些简化,并削减了全局变量:
Fixed/Simplified Asio
Here's with some simplifications, and cutting the globals:
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <unistd.h>
namespace ba = boost::asio;
struct Reader : std::enable_shared_from_this<Reader> {
ba::streambuf data;
ba::posix::stream_descriptor cpuLoadDataStream;
std::string const delimiter = "\n";
Reader(ba::io_service& svc, int fd)
: cpuLoadDataStream(svc, fd) {}
void do_read_loop() {
async_read_until(cpuLoadDataStream, data, delimiter, boost::bind(&Reader::handle_read, shared_from_this(), _1, _2));
}
void handle_read(const boost::system::error_code &error, std::size_t bytes_transferred) {
std::cout << "Enter handle_read (" << error.message() << ")\n";
if (!error) {
if (bytes_transferred > 0) {
std::copy_n(std::istreambuf_iterator<char>(&data), bytes_transferred, std::ostreambuf_iterator<char>(std::cout << "handle_read got data: '"));
std::cout << "' --\n";
data.consume(delimiter.size());
}
do_read_loop();
}
}
};
int main() {
int fds[2];
int& readFd = fds[0];
int& writeFd = fds[1];
if (pipe(fds) == -1) {
std::cout << "pipe error" << std::endl;
return 1;
}
if (int pid = fork()) {
ba::io_service ioService;
// parent
if (pid == -1) {
std::cerr << "fork error" << std::endl;
return 2;
} else {
close(writeFd);
std::make_shared<Reader>(ioService, ::dup(readFd))->do_read_loop();
}
ioService.run();
} else {
ba::io_service ioService;
// child
if (dup2(writeFd, STDOUT_FILENO) == -1) {
std::cerr << "dup2 error" << std::endl;
return 3;
}
close(readFd);
close(writeFd);
if (execlp("mpstat", "mpstat", "-P", "ALL", 0) == -1) {
std::cerr << "execlp error" << std::endl;
return 4;
}
ioService.run();
}
}
请注意,它不是连续的由于Coliru上的明显原因
Note it's not continuous for obvious reasons on Coliru
更简单:增强处理
为什么不
Much Simpler: Boost Process
Why not use Boost Process instead?
#include <boost/process.hpp>
#include <iostream>
#include <iomanip>
namespace bp = boost::process;
int main() {
bp::pstream output;
bp::system("mpstat -P ALL", bp::std_out > output);
for (std::string line; std::getline(output, line);) {
std::cout << "Got: " << std::quoted(line) << "\n";
}
}
或者再次使其异步:
#include <boost/asio.hpp>
#include <boost/process.hpp>
#include <iostream>
#include <iomanip>
namespace bp = boost::process;
using Args = std::vector<std::string>;
int main() {
boost::asio::io_service io;
bp::async_pipe output(io);
bp::child mpstat(bp::search_path("mpstat"),
//Args { "1", "-P", "ALL" },
Args { "1", "3" }, // limited to 3 iterations on Coliru
bp::std_out > output, io);
boost::asio::streambuf data;
std::function<void(boost::system::error_code, size_t)> handle = [&](boost::system::error_code ec, size_t /*bytes_transferred*/) {
if (ec) {
std::cout << "Good bye (" << ec.message() << ")\n";
} else {
std::string line;
std::getline(std::istream(&data), line);
std::cout << "Got: " << std::quoted(line) << "\n";
async_read_until(output, data, "\n", handle);
}
};
async_read_until(output, data, "\n", handle);
io.run();
}
打印
Got: "Linux 4.4.0-57-generic (stacked-crooked) 08/26/18 _x86_64_ (4 CPU)"
Got: ""
Got: "13:23:20 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle"
Got: "13:23:21 all 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00"
Got: "13:23:22 all 0.00 0.00 0.00 0.25 0.00 0.00 0.00 0.00 0.00 99.75"
Got: "13:23:23 all 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00"
Got: "Average: all 0.00 0.00 0.00 0.08 0.00 0.00 0.00 0.00 0.00 99.92"
Good bye (End of file)
这篇关于使用BOOST ASIO async_read_until读取mpstat输出时,文件意外结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!