usermode/library/common/hrtime.h

00001 /*
00002     Copyright (C) 2011 Computer Sciences Department, 
00003     University of Wisconsin -- Madison
00004 
00005     ----------------------------------------------------------------------
00006 
00007     This file is part of Mnemosyne: Lightweight Persistent Memory, 
00008     originally developed at the University of Wisconsin -- Madison.
00009 
00010     Mnemosyne was originally developed primarily by Haris Volos
00011     with contributions from Andres Jaan Tack.
00012 
00013     ----------------------------------------------------------------------
00014 
00015     Mnemosyne is free software; you can redistribute it and/or
00016     modify it under the terms of the GNU General Public License
00017     as published by the Free Software Foundation, version 2
00018     of the License.
00019  
00020     Mnemosyne is distributed in the hope that it will be useful,
00021     but WITHOUT ANY WARRANTY; without even the implied warranty of
00022     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023     GNU General Public License for more details.
00024 
00025     You should have received a copy of the GNU General Public License
00026     along with this program; if not, write to the Free Software
00027     Foundation, Inc., 51 Franklin Street, Fifth Floor, 
00028     Boston, MA  02110-1301, USA.
00029 
00030 ### END HEADER ###
00031 */
00032 
00033 /*
00034  * \file
00035  *
00036  * \brief Interface to x86/64's high resolution time counter.
00037  *
00038  */
00039 
00040 #ifndef _HRTIME_H_121AJ1
00041 #define _HRTIME_H_121AJ1
00042 
00043 #ifndef _HRTIME_CPUFREQ
00044 # define _HRTIME_CPUFREQ 2500 /* GHz */
00045 #endif
00046 
00047 #define HRTIME_NS2CYCLE(__ns) ((__ns) * _HRTIME_CPUFREQ / 1000)
00048 #define HRTIME_CYCLE2NS(__cycles) ((__cycles) * 1000 / _HRTIME_CPUFREQ)
00049 
00050 
00051 typedef unsigned long long hrtime_t;
00052 
00053 static inline void hrtime_barrier() {
00054         asm volatile( "cpuid" :::"rax", "rbx", "rcx", "rdx");
00055 }
00056 
00057 #if defined(__i386__)
00058 
00059 static inline unsigned long long hrtime_cycles(void)
00060 {
00061         unsigned long long int x;
00062         __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
00063         return x;
00064 }
00065 
00066 #elif defined(__x86_64__)
00067 
00068 static inline unsigned long long hrtime_cycles(void)
00069 {
00070         unsigned hi, lo;
00071         __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
00072     return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
00073 }
00074 
00075 #else
00076 #error "What architecture is this???"
00077 #endif
00078 
00079 
00080 #endif /* _HRTIME_H_121AJ1 */

Generated on Sat Apr 23 11:43:35 2011 for Mnemosyne by  doxygen 1.4.7