ia64/xen-unstable

changeset 3705:e1632e7fbc88

bitkeeper revision 1.1159.212.118 (420808adQbNXWTSEhY2Rqu5ALqDF5Q)

- Detect the CPU speed automatically, rather than having the user
specify a ips in bochsrc. The user can still specify it to override
the auto-detected value.

- Care should be taken to make sure that tsc_per_bx_tick is not too
small or too big. Otherwise, numerical precision issues may result
in the wrong calculation of elapsed time

Signed-off-by: Edwin Zhai <edwin.zhai@intel.com>
Signed-off-by: Arun Sharma <arun.sharma@intel.com>
Signed-off-by: ian@xensource.com
author iap10@labyrinth.cl.cam.ac.uk
date Tue Feb 08 00:32:45 2005 +0000 (2005-02-08)
parents 9bf9541713e7
children 396d0cbdc29b
files tools/examples/bochsrc tools/ioemu/include/pc_system.h tools/ioemu/iodev/cpu.cc tools/ioemu/iodev/pc_system.cc
line diff
     1.1 --- a/tools/examples/bochsrc	Tue Feb 08 00:30:21 2005 +0000
     1.2 +++ b/tools/examples/bochsrc	Tue Feb 08 00:32:45 2005 +0000
     1.3 @@ -16,4 +16,3 @@ error: action=report
     1.4  panic: action=ask
     1.5  
     1.6  mouse: enabled=0
     1.7 -ips: 1500000
     2.1 --- a/tools/ioemu/include/pc_system.h	Tue Feb 08 00:30:21 2005 +0000
     2.2 +++ b/tools/ioemu/include/pc_system.h	Tue Feb 08 00:32:45 2005 +0000
     2.3 @@ -45,6 +45,13 @@ BOCHSAPI extern class bx_pc_system_c bx_
     2.4  extern double m_ips;
     2.5  #endif
     2.6  
     2.7 +#ifdef BX_USE_VMX
     2.8 +extern unsigned int tsc_per_bx_tick;
     2.9 +
    2.10 +#define rdtscll(val) \
    2.11 +     __asm__ __volatile__("rdtsc" : "=A" (val))
    2.12 +#endif
    2.13 +
    2.14  class BOCHSAPI bx_pc_system_c : private logfunctions {
    2.15  private:
    2.16  
    2.17 @@ -87,6 +94,26 @@ private:
    2.18    double     m_ips; // Millions of Instructions Per Second
    2.19  #endif
    2.20  
    2.21 +#ifdef BX_USE_VMX
    2.22 +  static Bit64s get_clock(void) {
    2.23 +    struct timeval tv;
    2.24 +    gettimeofday(&tv, NULL);
    2.25 +    return tv.tv_sec * 1000000LL + tv.tv_usec;
    2.26 +    }
    2.27 +
    2.28 +  static Bit64u cpu_calibrate_ticks(void) {
    2.29 +    Bit64s usec, t1, t2;
    2.30 +
    2.31 +    usec = get_clock();
    2.32 +    rdtscll(t1);
    2.33 +
    2.34 +    usleep(50 * 1000);
    2.35 +    usec = get_clock() - usec;
    2.36 +    rdtscll(t2);
    2.37 +
    2.38 +    return (((t2 - t1) * 1000000LL + (usec >> 1)) / usec);
    2.39 +    }
    2.40 +#endif
    2.41    // This handler is called when the function which decrements the clock
    2.42    // ticks finds that an event has occurred.
    2.43    void   countdownEvent(void);
     3.1 --- a/tools/ioemu/iodev/cpu.cc	Tue Feb 08 00:30:21 2005 +0000
     3.2 +++ b/tools/ioemu/iodev/cpu.cc	Tue Feb 08 00:32:45 2005 +0000
     3.3 @@ -167,9 +167,6 @@ bx_cpu_c::timer_handler(void)
     3.4  #define rdtscl(low) \
     3.5       __asm__ __volatile__("rdtsc" : "=a" (low) : : "edx")
     3.6  
     3.7 -#define rdtscll(val) \
     3.8 -     __asm__ __volatile__("rdtsc" : "=A" (val))
     3.9 -
    3.10  void
    3.11  bx_cpu_c::cpu_loop(int max_instr_count)
    3.12  {
    3.13 @@ -211,9 +208,9 @@ bx_cpu_c::cpu_loop(int max_instr_count)
    3.14  #endif
    3.15  
    3.16  		if (t2 <= t1)
    3.17 -			BX_TICKN((t2 + ULONGLONG_MAX - t1));
    3.18 +			BX_TICKN((t2 + ULONGLONG_MAX - t1) / tsc_per_bx_tick);
    3.19  		else
    3.20 -			BX_TICKN((t2 - t1));
    3.21 +			BX_TICKN((t2 - t1) / tsc_per_bx_tick);
    3.22  		t1 = t2;
    3.23  
    3.24  		timer_handler();
     4.1 --- a/tools/ioemu/iodev/pc_system.cc	Tue Feb 08 00:30:21 2005 +0000
     4.2 +++ b/tools/ioemu/iodev/pc_system.cc	Tue Feb 08 00:32:45 2005 +0000
     4.3 @@ -44,6 +44,10 @@ unsigned long ips_count=0;
     4.4  double     m_ips; // Millions of Instructions Per Second
     4.5  #endif
     4.6  
     4.7 +#ifdef BX_USE_VMX
     4.8 +unsigned int tsc_per_bx_tick;
     4.9 +#endif
    4.10 +
    4.11  // Option for turning off BX_TIMER_DEBUG?
    4.12  // Check out m_ips and ips
    4.13  
    4.14 @@ -98,6 +102,16 @@ bx_pc_system_c::init_ips(Bit32u ips)
    4.15    a20_mask   = 0xffffffff;
    4.16  #endif
    4.17  
    4.18 +#ifdef BX_USE_VMX
    4.19 +  Bit64u phy_cpu_freq = cpu_calibrate_ticks();
    4.20 + 
    4.21 +  if (ips == 500000) {  //default ips: we use fixed scaling factor to calulate ips
    4.22 +    tsc_per_bx_tick = 2000;
    4.23 +    ips = phy_cpu_freq / tsc_per_bx_tick;
    4.24 +  } else  //use uesr defined ips to calulate factor
    4.25 +    tsc_per_bx_tick = ((phy_cpu_freq + (ips>>1)) / ips);
    4.26 +#endif
    4.27 +
    4.28    // parameter 'ips' is the processor speed in Instructions-Per-Second
    4.29    m_ips = double(ips) / 1000000.0L;
    4.30