non syscall gettimeofday
Subsystems (2)
PATCH 1/1 non syscall gettimeofday
The following patch implements a shared memory interface to allow implementing gettimeofday (and other clock measurement) using the TSC counter on i386. On a 1.6G Xeon this reduces gettimeofday from 1.2 us per call to .17 us per call.
The interface is through a /proc/tscinfo that is mmap'd by the library. If the kernel doesn't have working TSC and/or has NUMA TSC problems then the /proc file will not exist and the library should fall back to a syscall.
Attachments:
patch for 2.4.20-pre5 sample library routine
Documentation/magic-number.txt +1 -0
@@ -97,3 +97,4 @@ SLAB_MAGIC_DESTROYED 0xb2f23c5a kmem_slab_s mm/slab.c STLI_PORTMAGIC 0xe671c7a1 stliport include/linux/istallion.h CCB_MAGIC 0xf2691ad2 ccb drivers/scsi/ncr53c8xx.c +TSCINFO_MAGIC 0x203854e0 tscinfo include/linux/tscinfo.h
arch/i386/kernel/Makefile +1 -0
@@ -40,5 +40,6 @@ obj-$(CONFIG_X86_LOCAL_APIC) += mpparse.o apic.o nmi.o obj-$(CONFIG_X86_IO_APIC) += io_apic.o acpitable.o obj-$(CONFIG_X86_VISWS_APIC) += visws_apic.o +obj-$(CONFIG_X86_HAS_TSC) += tscinfo.o include $(TOPDIR)/Rules.make
arch/i386/kernel/time.c +42 -2
84 lines not on this page · raw message
arch/i386/kernel/tscinfo.c +113 -0
113 lines not on this page · raw message
fs/proc/root.c +3 -0
@@ -67,6 +67,9 @@#ifdef CONFIG_PPC_RTAS proc_rtas_init ();#endif +#ifdef CONFIG_X86_HAS_TSC +proc_tscinfo_init (); +#endif proc_bus =proc_mkdir ("bus" ,0 ); }
include/linux/proc_fs.h +5 -0
@@ -133,6 +133,11 @@extern void proc_rtas_init (void );/* +* i386/tscinfo.c +*/ +extern void proc_tscinfo_init (void ); + +/* * PPC64 */ extern void proc_ppc64_init (void );
include/linux/tscinfo.h +23 -0
@@ -0,0 +1,23 @@ +#ifndef _LINUX_TSCINFO_H +#define _LINUX_TSCINFO_H +/* +* Export conversion information between current time and tsc values +*/ + +struct tsc_info { +unsigned long version_magic;/* detect interface changes */ +unsigned long pre_sequence;/* detect changes while reading */ +unsigned long post_sequence; + +unsigned long cpu_khz;/* for TSC calibration */ +unsigned long offset_quotient;/* conversion from tsc to usec */ + +unsigned long last_tsc;/* lsb of TSC at last clock tick */ + +struct timeval last_tv;/* time of day at last clock tick */ +struct timezone last_tz;/* timezone */ +}; + +#define TSCINFO_MAGIC 0x203854e0 + +#endif
/* ugettime.c -- userland alternative to gettimeofday
This file is part of "ugettime", a userland alternative to gettimeofday and time system call.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/fcntl.h>
#include <sys/syscall.h>
#include <linux/tscinfo.h>
static void *mmap_base = NULL;
struct __xchg_dummy { unsigned long a[100]; };
#define __xg(x) ((struct __xchg_dummy *)(x))
static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
unsigned long new, int size)
{
unsigned long prev;
switch (size) {
case 1:
__asm__ __volatile__("lock; cmpxchgb %b1,%2"
: "=a"(prev)
: "q"(new), "m"(*__xg(ptr)), "0"(old)
: "memory");
return prev;
case 2:
__asm__ __volatile__("lock; cmpxchgw %w1,%2"
: "=a"(prev)
: "q"(new), "m"(*__xg(ptr)), "0"(old)
: "memory");
return prev;
case 4:
__asm__ __volatile__("lock; cmpxchgl %1,%2"
: "=a"(prev)
: "q"(new), "m"(*__xg(ptr)), "0"(old)
: "memory");
return prev;
}
return old;
}#define cmpxchg(ptr,o,n)\ ((__typeof__(*(ptr)))__cmpxchg((ptr),(unsigned long)(o),\ (unsigned long)(n),sizeof(*(ptr))))
/*
* Setup shared read-only map of tsc_info data structure
* if kernel does not support it then leave mmap_base set to MAP_FAILED */
static void setup_mmap(void) {
int fd;
void *base;
const long pgsz = sysconf(_SC_PAGE_SIZE);
const long mapsz
= (sizeof(struct tsc_info)+(pgsz-1)) &~ (pgsz-1);
if ((fd = open("/proc/tscinfo", O_RDONLY)) < 0)
base = MAP_FAILED;
else {
base = mmap(NULL, mapsz, PROT_READ, MAP_SHARED, fd, 0);
close(fd);
}
/* Atomicaly update mmap,
* so if two threads try to setup
* at same time only one wins.
*/
if (cmpxchg(&mmap_base, 0, base) != 0) {
/* race detected, keep other threads map */
if (base != MAP_FAILED)
munmap(base, mapsz);
}
}static inline const struct tsc_info *get_mmap(void)
{
if (!mmap_base)
setup_mmap();
if (mmap_base == MAP_FAILED ||
*(unsigned long *) mmap_base != TSCINFO_MAGIC) {
return NULL;
}
return (const struct tsc_info *) mmap_base;
}/* Read TSC register */
#define rdtsc(low,high) \
__asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high))#define mb() __asm__ __volatile__ ("lock; addl $0,0(%%esp)": : :"memory")
#define rmb() mb()
static inline unsigned long gettimeoffset(unsigned long last_tsc,
unsigned long fast_quotient)
{
register unsigned long tsc_lo, tsc_hi;
rdtsc(tsc_lo, tsc_hi);
tsc_lo -= last_tsc;
/*
* Time offset = (tsc_low delta) * fast_gettimeoffset_quotient
* = (tsc_low delta) * (usecs_per_clock)
* = (tsc_low delta) * (usecs_per_jiffy / clocks_per_jiffy)
*
* Using a mull instead of a divl saves up to 31 clock cycles
* in the critical path.
*/
__asm__("mull %2"
:"=a" (tsc_lo), "=d" (tsc_hi)
:"rm" (fast_quotient),
"0" (tsc_lo));
return tsc_hi;
}int
gettimeofday (struct timeval *tv, struct timezone *tz)
{
const struct tsc_info * tsci = get_mmap();
if (!tsci)
return syscall(SYS_gettimeofday,tv, tz);
if (tv) {
unsigned long seq, sec, usec;
/* must check to see if clock ticked */
do {
seq = tsci->pre_sequence;
usec = gettimeoffset(tsci->last_tsc,
tsci->offset_quotient);
sec = tsci->last_tv.tv_sec;
usec += tsci->last_tv.tv_usec;
rmb();
} while (tsci->post_sequence != seq);
while (usec >= 1000000) {
usec -= 1000000;
sec++;
}
tv->tv_usec = usec;
tv->tv_sec = sec;
}
if (tz) {
*tz = tsci->last_tz;
}
return 0;
}time_t
time (time_t *t)
{
const struct tsc_info * tsci = get_mmap();
time_t now;
if (!tsci)
return syscall(SYS_time,t);
now = tsci->last_tv.tv_sec;
if (t)
*t = now;
return (time_t) now;
}That seems awfully heavy handed for TSC, as well as reducing your accuracy massively.
Followup to: <1031267553.10830.71.camel@dell_ss3.pdx.osdl.net> By author: Stephen Hemminger <[email protected]> In newsgroup: linux.dev.kernel
Quoted 6 lines
The following patch implements a shared memory interface to allow implementing gettimeofday (and other clock measurement) using the TSC counter on i386. On a 1.6G Xeon this reduces gettimeofday from 1.2 us per call to .17 us per call.
This sounds like a vsyscall. Since we have discussed vsyscalls on and off without getting anywhere, I'd like to know how your implementation does it -- the #1 proposal I think was to map in a page at 0xfffff000 and have the vsyscall code there.
Note that the vsyscall needs to bounce to a regular syscall if TSC time/gettimeofday aren't available.
-hpa
Signature · 3 lines
<[email protected]> at work, <[email protected]> in private! "Unix gives you enough rope to shoot yourself in the foot." http://www.zytor.com/~hpa/puzzle.txt <[email protected]>
Quoted 5 lines
This sounds like a vsyscall. Since we have discussed vsyscalls on and off without getting anywhere, I'd like to know how your implementation does it -- the #1 proposal I think was to map in a page at
0xfffff000and have the vsyscall code there.
Id like to do a similar thing on ppc32 and ppc64. It would be good to make some of this generic before everyone implements it their own way.
Of course the lower level stuff will be arch specific, but some of it could be the same (like how do we handle ptracing into the area? Do we COW or do we deny it and fix gdb to unsderstand vsyscalls).
Anton
Hi!
Quoted 11 lines
Quoted 4 lines
This sounds like a vsyscall. Since we have discussed vsyscalls on and off without getting anywhere, I'd like to know how your implementation does it -- the #1 proposal I think was to map in a page at
0xfffff000and have the vsyscall code there.Id like to do a similar thing on ppc32 and ppc64. It would be good to make some of this generic before everyone implements it their own way.
Of course the lower level stuff will be arch specific, but some of it could be the same (like how do we handle ptracing into the area? Do we COW or do we deny it and fix gdb to unsderstand vsyscalls).
It is already implemented on x86-64.
AFAIR putting breakpoints into vsyscall area is not permitted.
Pavel
Signature · 2 lines
Philips Velo 1: 1"x4"x8", 300gram, 60, 12MB, 40bogomips, linux, mutt, details at http://atrey.karlin.mff.cuni.cz/~pavel/velo/index.html.