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.
It appears that mpstat formats its output in such a way that measurements for all cores are separated by an empty line.
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
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;
}