//------------------------------------------------------------------- // watchtsc.cpp // // This program shows how to use inline assembly language to // read the current value of the TimeStamp Counter register. // // compile-and-link using: $ g++ watchtsc.cpp -o watchtsc // // programmer: ALLAN CRUSE // written on: 30 MAR 2006 //------------------------------------------------------------------- #include // for printf() #include // for read() #include // for tcgetattr(), tcsetattr() unsigned long long tsc( void ) { asm(" rdtsc "); } int main( int argc, char **argv ) { // preserve the initial terminal settings struct termios orig_tty; tcgetattr( 0, &orig_tty ); // modify some canonical terminal settings struct termios work_tty = orig_tty; work_tty.c_lflag &= ~ECHO; // turn off echoing of kestrokes work_tty.c_lflag &= ~ICANON; // turn off keystroke buffering work_tty.c_lflag &= ~ISIG; // disable CONTROL-C termination work_tty.c_cc[ VTIME ] = 0; // amount of time to await input work_tty.c_cc[ VMIN ] = 0; // number of characters to await tcsetattr( 0, TCSAFLUSH, &work_tty ); // display continuous flicker-free text-output printf( "\e[H\e[J" ); // home the cursor and clear the screen printf( "\e[12;0H" ); // move the cursor to row 12, column 0 printf( "\e[?25l" ); // and then make the cursor disappear int done = 0; while ( !done ) { // show the current value in the timestamp register unsigned long long timestamp = tsc(); printf( "\t\t\t TimeStamp Counter = %llu \r", timestamp ); // set the 'done' flag as soon as any key is pressed int inch = 0; if ( read( 0, &inch, sizeof( inch ) ) > 0 ) done = 1; } printf( "\e[23;0H" ); // move cursor to row 23, column 0 printf( "\e[?25h\n" ); // and show the cursor again // restore canonical terminal settings tcsetattr( 0, TCSAFLUSH, &orig_tty ); }