WARNING - OLD ARCHIVES

This is an archived copy of the Xen.org mailing list, which we have preserved to ensure that existing links to archives are not broken. The live archive, which contains the latest emails, can be found at http://lists.xen.org/
   
 
 
Xen 
 
Home Products Support Community News
 
   
 

xen-devel

[Xen-devel] RE: [RFC] Correct/fast timestamping in apps under Xen [0 of

To: "Xen-Devel (E-mail)" <xen-devel@xxxxxxxxxxxxxxxxxxx>
Subject: [Xen-devel] RE: [RFC] Correct/fast timestamping in apps under Xen [0 of 4]
From: Dan Magenheimer <dan.magenheimer@xxxxxxxxxx>
Date: Mon, 5 Oct 2009 08:56:59 -0700 (PDT)
Cc: kurt.hackel@xxxxxxxxxx, Ian Pratt <Ian.Pratt@xxxxxxxxxxxxx>, Keir Fraser <keir.fraser@xxxxxxxxxxxxx>
Delivery-date: Mon, 05 Oct 2009 08:58:11 -0700
Envelope-to: www-data@xxxxxxxxxxxxxxxxxxx
In-reply-to: <18b5f9d0-1806-4ea5-8d69-579f33214cfc@default>
List-help: <mailto:xen-devel-request@lists.xensource.com?subject=help>
List-id: Xen developer discussion <xen-devel.lists.xensource.com>
List-post: <mailto:xen-devel@lists.xensource.com>
List-subscribe: <http://lists.xensource.com/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=subscribe>
List-unsubscribe: <http://lists.xensource.com/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=unsubscribe>
Sender: xen-devel-bounces@xxxxxxxxxxxxxxxxxxx
OK, here'e some pseudo-code to describe the
decision tree an app would go through -- once
at startup -- to probe the environment and
determine which timestamp mechanism is the
best, meaning all of correct AND fastest
AND highest resolution.  Also following
that is the code an app would use whenever
a timestamp is needed (could be a static
inline function to avoid function call
overhead).

Remember that this is ONLY needed by apps
that must obtain a timestamp at a
high frequency, say >2K/core/sec.  At lower
frequencies, TSC emulation or calling an
OS intrinsic should be sufficient.

In some environments (e.g. if/when pvclock+
vsyscall is implemented), an OS intrinsic
may be faster than emulation.  However,
there is no way to probe this so, if necessary,
we determine which is fastest by measuring both.

The pvrdtscp mechanism, which must be proactively
configured for a guest, uses the rdtscp
instruction to return TSC and the guest
incarnation number (which changes infrequently).
If the app is currently running on hardware
which either does not support rdtscp or
doesn't have a reliable TSC, zero is
returned in place of the incarnation number
and Xen system time (in nsec) is returned
instead of TSC.

Legend: XEN_* indicates information obtained
by the app directly from Xen (using a TBD
mechanism).  All other functions are in-app
calls.

Dan

====================

/* run once at app startup */
if (running_on_xen())
    timestamp_mech = TS_NATIVE;

if (XEN_guest_is_landlocked() && XEN_tsc_is_reliable())
    timestamp_mech = TS_RDTSC_SCALE;
    timestamp_scale = XEN_tsc_scale();
}
else if (XEN_guest_has_pvrdtscp())
    timestamp_mech = TS_PVRDTSCP;
else if (XEN_guest_tsc_emulated())  {
    os_intr_cycles = measure_os_intrinsic();
    tsc_emul_cycles = measure_tsc_emulation();
    if (tsc_emul_cycles < os_intr_cycles) {
        timestamp_mech = TS_RDTSC_NSEC;
    else
        timestamp_mech = TS_OS_INTRINSIC;
}

/* returns monotonically increasing timestamp (nsec) */
u64 get_nsec_timestamp(void)
{
    static u64 last = 0;
    u64 now;

    if (timestamp_mech == TS_NATIVE)
        now = native_timestamp();
    else if (timestamp_mech == TS_PVRDTSCP) {
        static u32 last_aux = 0;
        u32 this_aux;
        now = rdtscp(&this_aux);
        if (this_aux != 0) {
            while (this_aux != last_aux) {
                /* occurs only very rarely */
                last_aux = this_aux;
                timestamp_scale = XEN_tsc_scale();        
                now = rdtscp(&this_aux);
            }
            now = ts_scale(timestamp_scale,now);
        }
        /* this_aux == 0 means was emulated and
           already have nsec */
    }
    else if (timestamp_mech == TS_RDTSC_NSEC)
        now = rdtsc();
    else if (timestamp_mech == TS_RDTSC_SCALE)
        now = ts_scale(timestamp_scale,rdtsc());
    else /* TS_OS_INTRINSIC */
        now = os_intrinsic_get_nsec();
    /* ensure monotonically increasing */
    if ((s64)(now - last) > 0)
        last = now;
    else
        now = ++last;
    return now;
}

_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel

<Prev in Thread] Current Thread [Next in Thread>