From owner-freebsd-arch@FreeBSD.ORG Tue Jun 5 20:30:52 2012 Return-Path: Delivered-To: freebsd-arch@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 5A32A106566C; Tue, 5 Jun 2012 20:30:52 +0000 (UTC) (envelope-from gleb.kurtsou@gmail.com) Received: from mail-lb0-f182.google.com (mail-lb0-f182.google.com [209.85.217.182]) by mx1.freebsd.org (Postfix) with ESMTP id C05F08FC0A; Tue, 5 Jun 2012 20:30:50 +0000 (UTC) Received: by lbon10 with SMTP id n10so5373119lbo.13 for ; Tue, 05 Jun 2012 13:30:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:cc:subject:message-id:references:mime-version :content-type:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=u8AP51ONs2zfWJ3eMhDrdW9UcthNAbwHhoYUE3ia+vg=; b=AC3AEKqo/GksdzFuQBRfpItOG6MX8gxwE+0+fpJlDUEiF4E1aB4MeZCu2JviEODC2f AJeQ2b6zhdZjUZekw5WmZZJACP0EdavtY1hzlIyjvGgAZiiWnB6R0fGkXtDih2IQKxNV Aq9bzfaEmGh41HKsF51i8/FzY3qLpljZhIaALQg+7yxnebA1sWwhBqblF0kzvqMvt0/f qrB1o2QgN3oCtjxIOE6JneH/34fw0gCFjQm8yPfcvcGjmq0QeHcoezN+dkR9Q1oT+0M9 VMkebalIeG+1ejDI8uHtr/fK0XZcbWLvDkTUeDbk/wxlmprjOghnC8tAQDHgCkxrLlRg 9sog== Received: by 10.112.26.165 with SMTP id m5mr8686727lbg.15.1338928249549; Tue, 05 Jun 2012 13:30:49 -0700 (PDT) Received: from localhost ([78.157.92.5]) by mx.google.com with ESMTPS id hg4sm4139283lab.11.2012.06.05.13.30.47 (version=SSLv3 cipher=OTHER); Tue, 05 Jun 2012 13:30:48 -0700 (PDT) Date: Tue, 5 Jun 2012 23:30:42 +0300 From: Gleb Kurtsou To: John Baldwin Message-ID: <20120605203042.GA4081@reks> References: <201206051008.29568.jhb@freebsd.org> <86haupvk4a.fsf@ds4.des.no> <201206051222.12627.jhb@freebsd.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <201206051222.12627.jhb@freebsd.org> User-Agent: Mutt/1.5.21 (2010-09-15) Cc: Gianni , Alan Cox , Alexander Kabaev , Attilio Rao , Konstantin Belousov , freebsd-arch@freebsd.org, Konstantin Belousov , Dag-Erling =?utf-8?B?U23DuHJncmF2?= Subject: Re: Fwd: [RFC] Kernel shared variables X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 05 Jun 2012 20:30:52 -0000 On (05/06/2012 12:22), John Baldwin wrote: > On Tuesday, June 05, 2012 11:44:37 am Dag-Erling Smørgrav wrote: > > John Baldwin writes: > > > So you call getpid() on each access to a shared resource? > > > > I don't, but I've seen code that does, under the assumption that all the > > world is Linux and getpid() is free. Here's a sample from RHEL6 on a > > 3.1 GHz i5, using raise(0) as a baseline: > > > > getpid(): 10,000,000 iterations in 24,400 ms > > gettimeofday(0, 0): 10,000,000 iterations in 54,104 ms > > raise(0): 10,000,000 iterations in 1,284,593 ms > > > > The difference between the first two is due to the fact that while > > getpid() just returns a constant, gettimeofday(0, 0) performs two > > comparisons first. Passing an actual struct timeval to gettimeofday() > > slows it down by a factor of about 6. > > > > (strace confirms that no system calls occur for either getpid() or > > gettimeofday(0, 0)) > > > > Here is the same program running on FreeBSD 9.0-RELEASE in VirtualBox on > > an otherwise idle 3.4 GHz i7: > > > > getpid(): 10,000,000 iterations in 777,251 ms > > gettimeofday(0, 0): 10,000,000 iterations in 799,808 ms > > raise(0): 10,000,000 iterations in 2,142,275 ms > > Yes, we know getpid() is slow, I think the question is does it matter that > it's slow in something other than a microbenchmark. Can you name the > application that you've seen use getpid()? > arc4random* calls getpid() on every invocation (which is right thing to do, imo) to reinitialize generator after fork. As an example consider network daemon encrypting/decrypting packets that is likely to need randomness to encrypt or process considerable portion of data. Too much depends on the crypto protocols/algorithms used, but scenario is pretty much real-life. It's a good example when getpid() is actually needed, but not called often because of being cheap. Thanks, Gleb.