From owner-freebsd-hackers@FreeBSD.ORG Sun Mar 27 01:22:56 2011 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 2E226106564A for ; Sun, 27 Mar 2011 01:22:56 +0000 (UTC) (envelope-from julian@freebsd.org) Received: from vps1.elischer.org (vps1.elischer.org [204.109.63.16]) by mx1.freebsd.org (Postfix) with ESMTP id DAB228FC12 for ; Sun, 27 Mar 2011 01:22:55 +0000 (UTC) Received: from julian-mac.elischer.org (home-nat.elischer.org [67.100.89.137]) (authenticated bits=0) by vps1.elischer.org (8.14.4/8.14.4) with ESMTP id p2R1Movc006390 (version=TLSv1/SSLv3 cipher=DHE-RSA-CAMELLIA256-SHA bits=256 verify=NO); Sat, 26 Mar 2011 18:22:51 -0700 (PDT) (envelope-from julian@freebsd.org) Message-ID: <4D8E917F.3010701@freebsd.org> Date: Sat, 26 Mar 2011 18:23:11 -0700 From: Julian Elischer User-Agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10.4; en-US; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 MIME-Version: 1.0 To: Peter Jeremy References: <20110325082427.GA56170@server.vk2pj.dyndns.org> In-Reply-To: <20110325082427.GA56170@server.vk2pj.dyndns.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: freebsd-hackers@freebsd.org, Jing Huang Subject: Re: [GSoc] Timeconter Performance Improvements X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 27 Mar 2011 01:22:56 -0000 On 3/25/11 1:24 AM, Peter Jeremy wrote: > On 2011-Mar-24 17:00:02 +0800, Jing Huang wrote: >> In this scenario, I plan to use both tsc and shared memory to >> calculate precise time in user mode. The shared memory includes >> system_time, tsc_system_time and factor_tsc-system_time. > This sounds like a reasonable approach to me. Note that once we > implement a shared page, there is probably a variety of other > information we could usefully place on that page. > > SunOS 4.x included a page of shared memory per CPU. This was mapped > as an array (indexed by CPU number) at one address and the page > reflecting the current CPU was additionally mapped at another fixed > address. This allowed a process to both refer to data on its CPU > as well any CPU on the system. > >> We also consider the CPU frequency, because tsc counter is >> related to it. When kernel changes CPU frequency, the shared memory >> should be update subsequently. > Two issues with this, particularly on x86 without invariant TSC: > - looking up the current CPU frequency may not be a cheap operation > - the reported CPU frequency appears to be just an approximate value, > rather than the actual TSC frequency. > > On 2011-Mar-24 21:34:35 +0800, Jing Huang wrote: >> As I know, tsc counter is CPU specific. If the process running on >> a multi-core platform, we must consider switching problem. The one >> way, we can let the kernel to take of this. When switching to another >> CPU, the kernel will reset the shared memory according to the new CPU. > I'm not sure what the cost of managing this page mapping will be. > >> The second way, we can use CPUID instruction to get the info of >> current CPU, which can be executed in user mode ether. At the same >> time, the kernel maintains shared memory for each CPU. When invoke >> gettimeofday, the function will calculate precise time with current >> CPU's shared memory. > This approach suffers from a race condition between the CPUID > instruction and accessing the appropriate shared page - there is the > potential for an interrupt causing the process to be switched to a > different CPU, resulting in an incorrect page being accessed. > The shared page(s) can be in the form of an elf module that is linked with the process at load time. that way you can put cpu-specific code snippets there as well. when using a shared page to modify the TSC value read, one also needs to tempirarily lock the cpu you are on between the time you read the calibration value and the time you read the TSC.. A user process has only limited ability to do that.