00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include <vpr/vprConfig.h>
00043
00044 #if defined(VPR_OS_IRIX)
00045
00046 #include <sys/utsname.h>
00047 #include <sys/types.h>
00048 #include <sys/stat.h>
00049 #include <fcntl.h>
00050 #include <sys/mman.h>
00051 #include <unistd.h>
00052 #include <sys/syssgi.h>
00053 #endif
00054
00055 #if defined(VPR_USE_NSPR)
00056 # include <prinrval.h>
00057 #endif
00058
00059 #ifdef VPR_SIMULATOR
00060 # include <vpr/md/SIM/Controller.h>
00061 #endif
00062
00063 #include <vpr/Util/Interval.h>
00064 #include <vpr/System.h>
00065
00066
00067 const vpr::Interval vpr::Interval::NoWait(0,vpr::Interval::Base);
00068 const vpr::Interval vpr::Interval::NoTimeout(0xffffffffUL, vpr::Interval::Base);
00069 const vpr::Interval vpr::Interval::HalfPeriod((0xffffffffUL/2), vpr::Interval::Base);
00070
00071 namespace vpr
00072 {
00073
00074
00075 #ifdef VPR_SIMULATOR
00076 void Interval::setNow()
00077 {
00078 mMicroSeconds = vpr::sim::Controller::instance()->getClock().getCurrentTime().getBaseVal();
00079 }
00080 #else
00081 void Interval::setNow()
00082 {
00083 setNowReal();
00084 }
00085 #endif
00086
00087
00088
00089
00090 void Interval::setNowReal()
00091 {
00092 #if defined(VPR_OS_Windows)
00093 LARGE_INTEGER count, counts_per_sec;
00094
00095 QueryPerformanceFrequency(&counts_per_sec);
00096
00097
00098 vpr::Uint64 counts_per_sec64;
00099 vpr::Uint64 counts_per_sec_high64;
00100 counts_per_sec_high64 = counts_per_sec.HighPart;
00101 counts_per_sec64 = counts_per_sec.LowPart;
00102 counts_per_sec64 += (counts_per_sec_high64 << 32);
00103 double usecs_per_count =
00104 (1.0f / (double) ((vpr::Int64) counts_per_sec64)) * 1000000.0f;
00105
00106 if (QueryPerformanceCounter(&count))
00107 {
00108 vpr::Uint64 low = count.LowPart;
00109 vpr::Uint64 high = count.HighPart;
00110 mMicroSeconds = low + (high << 32);
00111 mMicroSeconds =
00112 (vpr::Uint64) (((double) (vpr::Int64) mMicroSeconds) * usecs_per_count);
00113 }
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126 #elif defined(VPR_OS_IRIX) // SGI Cycle counter version
00127 if (mMmem_fd != -1)
00128 {
00129 if (64 == mClockWidth)
00130 {
00131 Uint64 temp = *(Uint64*)mTimerAddr;
00132 mTicks = temp;
00133 mMicroSeconds = temp*mTicksToMicroseconds;
00134 }
00135 else
00136 {
00137
00138 Uint64 ticks = mTicks;
00139 unsigned int now = *(unsigned int*)mTimerAddr, temp;
00140 unsigned int residual = mResidual;
00141 unsigned int previous = mPrevious;
00142
00143
00144
00145
00146 temp = now - previous + residual;
00147 residual = temp & mClockMask;
00148 ticks += temp;
00149
00150 mPrevious = now;
00151 mResidual = residual;
00152 mTicks = ticks;
00153 mMicroSeconds = ticks * mTicksToMicroseconds;
00154 }
00155 }
00156 else
00157 {
00158
00159
00160 timeval cur_time;
00161 gettimeofday(&cur_time);
00162 vpr::Uint64 storage = vpr::Uint64(1000000u) * vpr::Uint64(cur_time.tv_sec);
00163 mMicroSeconds = vpr::Uint64(cur_time.tv_usec) + storage;
00164 }
00165 #else // Default to POSIX time setting
00166 vpr::TimeVal cur_time;
00167 vpr::System::gettimeofday(&cur_time);
00168 vpr::Uint64 storage = vpr::Uint64(1000000u) * vpr::Uint64(cur_time.tv_sec);
00169 mMicroSeconds = vpr::Uint64(cur_time.tv_usec) + storage;
00170 #endif
00171 }
00172
00173
00174 #if defined(VPR_OS_IRIX) // SGI Cycle counter version support code
00175
00176
00177 bool Interval::initializeCycleCounter()
00178 {
00179
00180
00181
00182
00183
00184
00185 mMmem_fd = -1;
00186 mClockWidth = 0;
00187 mTimerAddr = NULL;
00188 mClockMask = 0;
00189 mPrevious = 0;
00190 mResidual = 0;
00191
00192
00193 struct utsname utsinfo;
00194 uname(&utsinfo);
00195 if ( (strncmp("IP12", utsinfo.machine, 4) != 0) &&
00196 ((mMmem_fd = open("/dev/mmem", O_RDONLY)) != -1) )
00197 {
00198 int poffmask = getpagesize() - 1;
00199 __psunsigned_t phys_addr, raddr;
00200 vpr::Uint32 cycleval;
00201
00202 phys_addr = syssgi(SGI_QUERY_CYCLECNTR, &cycleval);
00203 raddr = phys_addr & ~poffmask;
00204 mTimerAddr = mmap(0, poffmask, PROT_READ, MAP_PRIVATE, mMmem_fd,
00205 (__psint_t)raddr);
00206
00207 mClockWidth = syssgi(SGI_CYCLECNTR_SIZE);
00208 if (mClockWidth < 0)
00209 {
00210
00211
00212
00213
00214
00215
00216
00217 if (!strncmp(utsinfo.machine, "IP19", 4) ||
00218 !strncmp(utsinfo.machine, "IP21", 4))
00219 {
00220 mClockWidth = 64;
00221 }
00222 else
00223 {
00224 mClockWidth = 32;
00225 }
00226 }
00227
00228
00229
00230 mTicksToMicroseconds = cycleval / 1000000.0L;
00231 mClockMask = (1 << mClockWidth) -1;
00232 mTimerAddr = (void*)
00233 ((__psunsigned_t)mTimerAddr + (phys_addr & poffmask));
00234 }
00235
00236 return true;
00237 }
00238
00239 int Interval::mMmem_fd;
00240 int Interval::mClockWidth;
00241 void *Interval::mTimerAddr;
00242 vpr::Uint32 Interval::mClockMask;
00243 vpr::Uint32 Interval::mPrevious;
00244 vpr::Uint32 Interval::mResidual;
00245 double Interval::mTicksToMicroseconds;
00246
00247 const bool Interval::mInitialized = Interval::initializeCycleCounter();
00248
00249 #endif // VPR_OS_IRIX cycle counter version
00250
00251 }
00252