我尝试读取mpstat命令的输出(每秒收集cpu信息,即:“mptstat -P ALL 1”),以获取有关cpu和内核使用情况的信息。在多核CPU上,在阅读完第一批测量后,我得到了意外的“文件结束”状态。

似乎mpstat格式化其输出,以使所有内核的测量值之间都用空行分隔。

我已将async_read_until与等于'\ n'的定界符一起使用。

请在下面找到一个小型复制器。通过此复制器,我得到以下信息:

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发出的空行是为什么我得到“行尾”指示...但是我不明白为什么...

有人可以提供帮助吗?提前谢谢了。
#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秒的间隔重复运行,则必须将其设为第一个参数,而不是进程名称:

if (execlp("mpstat", "mpstat", "1", "-P", "ALL", 0) == -1) {

参见the documentation:


async_read_until读取直到输入缓冲区至少包含定界符。因此,当您读取缓冲区时,必须考虑将bytes_tranferred排除在分隔符之外的情况。

确保还消耗定界符,以避免陷入无限循环(因为已经满足停止条件):
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();
    }
}

我更喜欢第一个,因为它更明确且普遍适用。



固定/简化的Asio

这里有一些简化,并减少了全局变量:

Live On Coliru
#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();
    }
}



更简单:提升流程

为什么不使用Boost Process呢?
#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";
    }
}

或再次使其异步:

Live On Coliru
#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)

关于c++ - 使用BOOST ASIO async_read_until读取mpstat输出时,文件意外结束,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52025021/

10-11 16:06