//------------------------------------------------------------------- // watchtsc.cpp // // This application continually reads and displays the contents // of the '/dev/tsc' device special file created by our 'tsc.c' // device driver module. To produce a clean and 'flicker-free' // display it employs the Linux implementation for the standard // 'curses' programming interface. (The value being displayed // is from the Pentium processor's TimeStamp Counter register.) // // compile using: $ g++ watchtsc.cpp -lncurses -o watchtsc // // programmer: ALLAN CRUSE // written on: 03 APR 2003 //------------------------------------------------------------------- #include // for printf(), perror() #include // for open() #include // for exit() #include // for read(), lseek(), close() #include // for initscr(), endwin(), etc. char filename[] = "/dev/tsc"; int main( int argc, char **argv ) { int fd = open( filename, O_RDONLY ); if ( fd < 0 ) { perror( filename ); exit(1); } printf( "\nopened \'%s\': fp=%d \n", filename, fd ); initscr(); noecho(); cbreak(); clear(); refresh(); fd_set permset; FD_ZERO( &permset ); FD_SET( fd, &permset ); FD_SET( STDIN_FILENO, &permset ); int ndevs = 1+fd; // sleep until some new data is available for(;;) { fd_set readset = permset; if ( select( ndevs, &readset, NULL, NULL, NULL ) < 0 ) break; char inch; if ( FD_ISSET( STDIN_FILENO, &readset ) ) if ( read( STDIN_FILENO, &inch, 1 ) > 0 ) { if ( inch == 0x1B ) break; if (( inch == 'q' )||( inch == 'Q' )) break; } if ( FD_ISSET( fd, &readset ) ) { unsigned long long tsc; lseek( fd, 0, SEEK_SET ); read( fd, &tsc, sizeof( tsc ) ); mvprintw( 12, 22, " TimeStamp Counter: %lld ", tsc ); move( 23, 0 ); } refresh(); } endwin(); close( fd ); }