Helper for reading data from kernel more efficiently.

This commit is contained in:
Bartosz Taudul 2019-09-26 22:55:02 +02:00
parent c09f3c0676
commit 6f5dd44f1f

View File

@ -0,0 +1,36 @@
#include <fcntl.h>
#include <poll.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
enum { BufSize = 64*1024 };
int main()
{
char buf[BufSize];
int kernelFd = open( "/sys/kernel/debug/tracing/trace_pipe", O_RDONLY );
if( kernelFd == -1 ) return -1;
struct pollfd pfd;
pfd.fd = kernelFd;
pfd.events = POLLIN | POLLERR;
struct timespec sleepTime;
sleepTime.tv_sec = 0;
sleepTime.tv_nsec = 1000 * 1000 * 10;
for(;;)
{
while( poll( &pfd, 1, 0 ) <= 0 ) nanosleep( &sleepTime, NULL );
const int rd = read( kernelFd, buf, BufSize );
if( rd <= 0 ) break;
write( STDOUT_FILENO, buf, rd );
}
close( kernelFd );
return 0;
}