From owner-freebsd-drivers@freebsd.org Wed Sep 16 07:15:11 2020 Return-Path: Delivered-To: freebsd-drivers@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id EB3B63DAE92 for ; Wed, 16 Sep 2020 07:15:11 +0000 (UTC) (envelope-from julian.grajkowski@gmail.com) Received: from mail-io1-xd2d.google.com (mail-io1-xd2d.google.com [IPv6:2607:f8b0:4864:20::d2d]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1O1" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Brrvz1fHKz4MQS for ; Wed, 16 Sep 2020 07:15:11 +0000 (UTC) (envelope-from julian.grajkowski@gmail.com) Received: by mail-io1-xd2d.google.com with SMTP id r9so7182089ioa.2 for ; Wed, 16 Sep 2020 00:15:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=xZ8MuEfGNjvbyEwJl66cFUWcpjdFFvZyYHEcr8hi0jE=; b=C6duzDNvUIObFtqgpPmrTfmzSfugjo2apGtzgQXV7ILyURCML4XNqHld3bhW4PMkNm m88rq8M4LGje6Sep/7Z2UsKTdojVmPSVjIJ3sxpCmH5WcvU3QxBF8PdBDRPsNoF3hvDn S+ZQ+HL2LjDotvqRcfCgPpvB+L6eS7Yg4hVIwdauiq+fPJej0XX8jUCfSGVxdF/qgrmO HLnLiModgsoH+UofIU67eGKs2rlTK58GAiil7e0MpQzAiDFddUIlA+D9ZiYUAZlQi2jF FIlhdd6pQWUgcZdTQj2MtUl6QUtjNsVrJCg1TMUeNK5xf/HxJ1BRgpd06OZevjVt0Erd 8fxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=xZ8MuEfGNjvbyEwJl66cFUWcpjdFFvZyYHEcr8hi0jE=; b=F5bumGq/qQO3+gsxlEN3i+VViEPKqFNc0LcgzRG/gXWAFVtUeMychIqcgLk2J/kcw6 aFxLX0XPsCdFXSwDZ1rf8H7/uo7rgYeYPPFKQ6pIVx0UB/hUlU/XRIHvPtJMvHX/cmE4 dohhSnmQcM7I42N/sKT2tiLdp4DQONJbsHa7XlbIGbXoKWro5/WZVfxOIeKDCmotI/nh rlbVuVEoJK9XsP49njnP01d1AFO4kCprEtbZOcnvObv5NikHQhDt75v4vYRiKRFhWEFI xRK/+MugFtVvYBk+vpjaytHO3eSlRL3GcO6wnfAOZD/6BqOxiRYmf6PAJ4b4Sv2974Gd 98mw== X-Gm-Message-State: AOAM531Bfhy6D0wB0kKQGxQE/xFnRocR6VzmQtWJ1GPxxzPK+zk2EtZb d7k6n+RVhcvZS/E3dY4tv2EclygHvocz+A5e5dMDFB1RYg== X-Google-Smtp-Source: ABdhPJzNmzlpX4dhaH/Hnbz6OtCwA18EKDK+BqJIC7shNO79jfGtrrASLNkW55iE8xg19th9+6n7oPCckQzrNPrzZ8o= X-Received: by 2002:a5d:8e0a:: with SMTP id e10mr8824449iod.169.1600240509488; Wed, 16 Sep 2020 00:15:09 -0700 (PDT) MIME-Version: 1.0 From: Julian Grajkowski Date: Wed, 16 Sep 2020 09:14:58 +0200 Message-ID: Subject: getpid() performance To: freebsd-drivers@freebsd.org X-Rspamd-Queue-Id: 4Brrvz1fHKz4MQS X-Spamd-Bar: --- Authentication-Results: mx1.freebsd.org; dkim=pass header.d=gmail.com header.s=20161025 header.b=C6duzDNv; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (mx1.freebsd.org: domain of juliangrajkowski@gmail.com designates 2607:f8b0:4864:20::d2d as permitted sender) smtp.mailfrom=juliangrajkowski@gmail.com X-Spamd-Result: default: False [-3.79 / 15.00]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-0.97)[-0.967]; R_DKIM_ALLOW(-0.20)[gmail.com:s=20161025]; RCVD_TLS_ALL(0.00)[]; FROM_HAS_DN(0.00)[]; DWL_DNSWL_NONE(0.00)[gmail.com:dkim]; R_SPF_ALLOW(-0.20)[+ip6:2607:f8b0:4000::/36:c]; FREEMAIL_FROM(0.00)[gmail.com]; MIME_GOOD(-0.10)[multipart/alternative,text/plain]; PREVIOUSLY_DELIVERED(0.00)[freebsd-drivers@freebsd.org]; TO_DN_NONE(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; NEURAL_HAM_LONG(-0.98)[-0.980]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DKIM_TRACE(0.00)[gmail.com:+]; DMARC_POLICY_ALLOW(-0.50)[gmail.com,none]; RCVD_IN_DNSWL_NONE(0.00)[2607:f8b0:4864:20::d2d:from]; NEURAL_HAM_SHORT(-0.85)[-0.846]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+,1:+,2:~]; FREEMAIL_ENVFROM(0.00)[gmail.com]; ASN(0.00)[asn:15169, ipnet:2607:f8b0::/32, country:US]; TAGGED_FROM(0.00)[]; MAILMAN_DEST(0.00)[freebsd-drivers]; RCVD_COUNT_TWO(0.00)[2] Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.33 X-BeenThere: freebsd-drivers@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: Writing device drivers for FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 16 Sep 2020 07:15:12 -0000 Hi, I am working on a contiguous memory allocator which frequently calls getpid() in user space and I have noticed very poor performance of this function call. I measured this call performance using following code: inline uint64_t rdtsc_start(void) { uint32_t cycles_high; uint32_t cycles_low; asm volatile("lfence\n\t" "rdtscp\n\t" "mov %%edx, %0\n\t" "mov %%eax, %1\n\t" : "=r" (cycles_high), "=r" (cycles_low) : : "%rax", "%rdx", "%rcx"); return (((uint64_t)cycles_high << 32) | cycles_low); } inline uint64_t rdtsc_end(void) { uint32_t cycles_high; uint32_t cycles_low; asm volatile("rdtscp\n\t" "mov %%edx, %0\n\t" "mov %%eax, %1\n\t" "lfence\n\t" : "=r" (cycles_high), "=r" (cycles_low) : : "%rax", "%rdx", "%rcx"); return (((uint64_t)cycles_high << 32) | cycles_low); } This way I measured ~320 cycles used for getpid() on FreeBSD 12.1. For comparison, in Linux (CentOS 7) this call uses ~10 cycles. I am aware that this should not be compared directly. as these are different systems, but such a big difference in performance is an issue for me, as getpid() is called very often in my code. Is such a poor performance of getpid() a known problem and is it possible that this might be improved in future releases? Measurements were done on the same mahcine with following setup: CPU: Intel(R) Atom(TM) CPU C3958 @ 2.00GHz (2000.06-MHz K8-class CPU) FreeBSD/SMP: Multiprocessor System Detected: 16 CPUs 8GB RAM (2x4GB): Type: DDR4 Type Detail: Synchronous Unbuffered (Unregistered) Speed: 2400 MT/s Thank you very much in advance for any help. Kind regards, Julian From owner-freebsd-drivers@freebsd.org Wed Sep 16 07:24:48 2020 Return-Path: Delivered-To: freebsd-drivers@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 5E9323DAEE8 for ; Wed, 16 Sep 2020 07:24:48 +0000 (UTC) (envelope-from wlosh@bsdimp.com) Received: from mail-qk1-x730.google.com (mail-qk1-x730.google.com [IPv6:2607:f8b0:4864:20::730]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1O1" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Brs7327fnz4Mt1 for ; Wed, 16 Sep 2020 07:24:46 +0000 (UTC) (envelope-from wlosh@bsdimp.com) Received: by mail-qk1-x730.google.com with SMTP id q5so7232161qkc.2 for ; Wed, 16 Sep 2020 00:24:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=E4D/DDGzgqb0SPqL0l+twPwdADGffDofnaTGY88B+3w=; b=e0UdPvW/XEfvK8jFsN5B8wwO+hBB6fF3G0A+yMPuMCDgTvHKmHcBu5lFj7aYxH4aIx RFjIWmsJZomN95SKChk9pbq8wpdMhWpYHZ+YCD/T7oDZ5sePm116qbdOkUlLsdmmVtfz hMnLhqoPlCvu+eNxjOAw0sbpaLTLFuvMcUvF6c9pNCXBhV279bfFYAu0ieZ7qt+g3Ijr m65F6vvsECGqBhxP6vYD/SAFOqRywd3rmSN5bf65JrTVmS27UOUQJ2uqXBkTNRo+BVCe px2lspBa6J3HkySwLChMr1sKqoJm8RUblhUa0jSowHrF+5pp2wxFe2QB1r0TGgHYQ6va PQfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=E4D/DDGzgqb0SPqL0l+twPwdADGffDofnaTGY88B+3w=; b=U/UTjdFuceCtzsKjycp+vGG8X9uUP1hXO6/nJ0pxCdKRN5y1RI8Fx4OmTTpe/J1QvO fRuCvhfxvozbUKNcgR4BsgnxcA48FDWCuz4QA2qinoYQG0e2DzbH3GLWTxCV3ilwo6g7 ZZOYDfq/qLrR9wHQjp2zGb/jV5022dAPcsSJLGfzmYOgxvgr8uV4IDiaY5tVV0e6eyXS 4TGPO7cwB2Wbfei3ZQXTxOgh5EAbi7HX2WC1PFp1quMjNq3bGPwcaX0aJhQP5/JYR0oL B+3oiTqXF5hpStKMiNuVNv2p1P3Mhs19zJOxTRRZxrkU6eChvioTPr5L6s3AVeiyZxxh FRjA== X-Gm-Message-State: AOAM530kGygKWb5iZ9b833EU4vYFCtNJga1DonW/RmLkrFB2KHtUb7Iu hC2IGQgqzKXftOe4JUI15GxgSI1ZKQQJqCkIKljIOZqHxu1YVg== X-Google-Smtp-Source: ABdhPJxpzi/f6qUndU37/L1GK9FNiVPuG61fGanI04FzsoLCiYA8QQDOYyX7YAqXukF3Br7zLEkVk4nrMA3gdXUR9QQ= X-Received: by 2002:ae9:f70d:: with SMTP id s13mr22832741qkg.215.1600241085694; Wed, 16 Sep 2020 00:24:45 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Warner Losh Date: Wed, 16 Sep 2020 01:24:34 -0600 Message-ID: Subject: Re: getpid() performance To: Julian Grajkowski Cc: freebsd-drivers@freebsd.org X-Rspamd-Queue-Id: 4Brs7327fnz4Mt1 X-Spamd-Bar: - Authentication-Results: mx1.freebsd.org; dkim=pass header.d=bsdimp-com.20150623.gappssmtp.com header.s=20150623 header.b=e0UdPvW/; dmarc=none; spf=none (mx1.freebsd.org: domain of wlosh@bsdimp.com has no SPF policy when checking 2607:f8b0:4864:20::730) smtp.mailfrom=wlosh@bsdimp.com X-Spamd-Result: default: False [-2.00 / 15.00]; RCVD_TLS_ALL(0.00)[]; ARC_NA(0.00)[]; R_DKIM_ALLOW(-0.20)[bsdimp-com.20150623.gappssmtp.com:s=20150623]; NEURAL_HAM_MEDIUM(-0.84)[-0.843]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; NEURAL_HAM_LONG(-0.99)[-0.988]; TAGGED_RCPT(0.00)[]; MIME_GOOD(-0.10)[multipart/alternative,text/plain]; PREVIOUSLY_DELIVERED(0.00)[freebsd-drivers@freebsd.org]; DMARC_NA(0.00)[bsdimp.com]; TO_MATCH_ENVRCPT_SOME(0.00)[]; DKIM_TRACE(0.00)[bsdimp-com.20150623.gappssmtp.com:+]; RCPT_COUNT_TWO(0.00)[2]; RCVD_IN_DNSWL_NONE(0.00)[2607:f8b0:4864:20::730:from]; NEURAL_HAM_SHORT(-0.17)[-0.167]; R_SPF_NA(0.00)[no SPF record]; FREEMAIL_TO(0.00)[gmail.com]; FORGED_SENDER(0.30)[imp@bsdimp.com,wlosh@bsdimp.com]; MIME_TRACE(0.00)[0:+,1:+,2:~]; RCVD_COUNT_TWO(0.00)[2]; ASN(0.00)[asn:15169, ipnet:2607:f8b0::/32, country:US]; FROM_NEQ_ENVFROM(0.00)[imp@bsdimp.com,wlosh@bsdimp.com]; MAILMAN_DEST(0.00)[freebsd-drivers] Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.33 X-BeenThere: freebsd-drivers@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: Writing device drivers for FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 16 Sep 2020 07:24:48 -0000 On Wed, Sep 16, 2020 at 1:15 AM Julian Grajkowski < julian.grajkowski@gmail.com> wrote: > Hi, > > I am working on a contiguous memory allocator which frequently calls > getpid() in user space and I have noticed very poor performance of this > function call. I measured this call performance using following code: > > inline uint64_t rdtsc_start(void) > { > uint32_t cycles_high; > uint32_t cycles_low; > > asm volatile("lfence\n\t" > "rdtscp\n\t" > "mov %%edx, %0\n\t" > "mov %%eax, %1\n\t" > : "=r" (cycles_high), "=r" (cycles_low) > : : "%rax", "%rdx", "%rcx"); > > return (((uint64_t)cycles_high << 32) | cycles_low); > } > > > inline uint64_t rdtsc_end(void) > { > uint32_t cycles_high; > uint32_t cycles_low; > > asm volatile("rdtscp\n\t" > "mov %%edx, %0\n\t" > "mov %%eax, %1\n\t" > "lfence\n\t" > : "=r" (cycles_high), "=r" (cycles_low) > : : "%rax", "%rdx", "%rcx"); > > return (((uint64_t)cycles_high << 32) | cycles_low); > } > > This way I measured ~320 cycles used for getpid() on FreeBSD 12.1. For > comparison, in Linux (CentOS 7) this call uses ~10 cycles. I am aware that > this should not be compared directly. as these are different systems, but > such a big difference in performance is an issue for me, as getpid() is > called very often in my code. > > Is such a poor performance of getpid() a known problem and is it possible > that this might be improved in future releases? > glibc optimizes getpid() system call so it only calls it once and returns a cached value (which is in line with 10 cycles, there's no way you can save/restore state in 10 cycles, let alone do a dispatch). FreeBSD doesn't. Warner > Measurements were done on the same mahcine with following setup: > > CPU: Intel(R) Atom(TM) CPU C3958 @ 2.00GHz (2000.06-MHz K8-class CPU) > FreeBSD/SMP: Multiprocessor System Detected: 16 CPUs > > 8GB RAM (2x4GB): > Type: DDR4 > Type Detail: Synchronous Unbuffered (Unregistered) > Speed: 2400 MT/s > > Thank you very much in advance for any help. > > Kind regards, > Julian > _______________________________________________ > freebsd-drivers@freebsd.org mailing list > https://lists.freebsd.org/mailman/listinfo/freebsd-drivers > To unsubscribe, send any mail to "freebsd-drivers-unsubscribe@freebsd.org" > From owner-freebsd-drivers@freebsd.org Wed Sep 16 22:36:43 2020 Return-Path: Delivered-To: freebsd-drivers@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 0CF0D3F3864 for ; Wed, 16 Sep 2020 22:36:43 +0000 (UTC) (envelope-from jmg@gold.funkthat.com) Received: from gold.funkthat.com (gate2.funkthat.com [208.87.223.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "gate2.funkthat.com", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4BsFMG0kQRz4NX9 for ; Wed, 16 Sep 2020 22:36:41 +0000 (UTC) (envelope-from jmg@gold.funkthat.com) Received: from gold.funkthat.com (localhost [127.0.0.1]) by gold.funkthat.com (8.15.2/8.15.2) with ESMTPS id 08GMacMX063526 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 16 Sep 2020 15:36:38 -0700 (PDT) (envelope-from jmg@gold.funkthat.com) Received: (from jmg@localhost) by gold.funkthat.com (8.15.2/8.15.2/Submit) id 08GMacFC063525; Wed, 16 Sep 2020 15:36:38 -0700 (PDT) (envelope-from jmg) Date: Wed, 16 Sep 2020 15:36:38 -0700 From: John-Mark Gurney To: Warner Losh Cc: Julian Grajkowski , freebsd-drivers@freebsd.org Subject: Re: getpid() performance Message-ID: <20200916223638.GR4213@funkthat.com> Mail-Followup-To: Warner Losh , Julian Grajkowski , freebsd-drivers@freebsd.org References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Operating-System: FreeBSD 11.3-STABLE amd64 X-PGP-Fingerprint: D87A 235F FB71 1F3F 55B7 ED9B D5FF 5A51 C0AC 3D65 X-Files: The truth is out there X-URL: https://www.funkthat.com/ X-Resume: https://www.funkthat.com/~jmg/resume.html X-TipJar: bitcoin:13Qmb6AeTgQecazTWph4XasEsP7nGRbAPE X-to-the-FBI-CIA-and-NSA: HI! HOW YA DOIN? can i haz chizburger? User-Agent: Mutt/1.6.1 (2016-04-27) X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.4.3 (gold.funkthat.com [127.0.0.1]); Wed, 16 Sep 2020 15:36:38 -0700 (PDT) X-Rspamd-Queue-Id: 4BsFMG0kQRz4NX9 X-Spamd-Bar: / Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=none; spf=none (mx1.freebsd.org: domain of jmg@gold.funkthat.com has no SPF policy when checking 208.87.223.18) smtp.mailfrom=jmg@gold.funkthat.com X-Spamd-Result: default: False [-0.41 / 15.00]; RCVD_TLS_ALL(0.00)[]; ARC_NA(0.00)[]; FREEMAIL_CC(0.00)[gmail.com,freebsd.org]; FREEFALL_USER(0.00)[jmg]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_THREE(0.00)[3]; TO_DN_SOME(0.00)[]; MID_RHS_MATCH_FROM(0.00)[]; TAGGED_RCPT(0.00)[]; MIME_GOOD(-0.10)[text/plain]; DMARC_NA(0.00)[funkthat.com]; AUTH_NA(1.00)[]; NEURAL_SPAM_SHORT(0.03)[0.033]; NEURAL_HAM_LONG(-0.89)[-0.894]; TO_MATCH_ENVRCPT_SOME(0.00)[]; NEURAL_HAM_MEDIUM(-0.75)[-0.745]; R_SPF_NA(0.00)[no SPF record]; FORGED_SENDER(0.30)[jmg@funkthat.com,jmg@gold.funkthat.com]; R_DKIM_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; ASN(0.00)[asn:32354, ipnet:208.87.216.0/21, country:US]; FROM_NEQ_ENVFROM(0.00)[jmg@funkthat.com,jmg@gold.funkthat.com]; MAILMAN_DEST(0.00)[freebsd-drivers] X-BeenThere: freebsd-drivers@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: Writing device drivers for FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 16 Sep 2020 22:36:43 -0000 Warner Losh wrote this message on Wed, Sep 16, 2020 at 01:24 -0600: > On Wed, Sep 16, 2020 at 1:15 AM Julian Grajkowski < > julian.grajkowski@gmail.com> wrote: > > > Hi, > > > > I am working on a contiguous memory allocator which frequently calls > > getpid() in user space and I have noticed very poor performance of this > > function call. I measured this call performance using following code: > > > > inline uint64_t rdtsc_start(void) > > { > > uint32_t cycles_high; > > uint32_t cycles_low; > > > > asm volatile("lfence\n\t" > > "rdtscp\n\t" > > "mov %%edx, %0\n\t" > > "mov %%eax, %1\n\t" > > : "=r" (cycles_high), "=r" (cycles_low) > > : : "%rax", "%rdx", "%rcx"); > > > > return (((uint64_t)cycles_high << 32) | cycles_low); > > } > > > > > > inline uint64_t rdtsc_end(void) > > { > > uint32_t cycles_high; > > uint32_t cycles_low; > > > > asm volatile("rdtscp\n\t" > > "mov %%edx, %0\n\t" > > "mov %%eax, %1\n\t" > > "lfence\n\t" > > : "=r" (cycles_high), "=r" (cycles_low) > > : : "%rax", "%rdx", "%rcx"); > > > > return (((uint64_t)cycles_high << 32) | cycles_low); > > } > > > > This way I measured ~320 cycles used for getpid() on FreeBSD 12.1. For > > comparison, in Linux (CentOS 7) this call uses ~10 cycles. I am aware that > > this should not be compared directly. as these are different systems, but > > such a big difference in performance is an issue for me, as getpid() is > > called very often in my code. > > > > Is such a poor performance of getpid() a known problem and is it possible > > that this might be improved in future releases? > > > > glibc optimizes getpid() system call so it only calls it once and returns a > cached value (which is in line with 10 cycles, there's no way you can > save/restore state in 10 cycles, let alone do a dispatch). FreeBSD doesn't. if you really need to see if your process has forked (I assume that is why you're calling getpid so frequently), you can mmap a page, and using minherit's INHERIT_ZERO so that all the data in that page will be zero'd on fork. You can then change your getpid check to something like: pid_t *page_with_inherit_zero_set; pid_t my_getpid() { if (page_with_inherit_zero_set == NULL) allocate_page_and_set_inherit_zero(); if (*page_with_inherit_zero_set == 0) { *page_with_inherit_zero_set = getpid(); return *page_with_inherit_zero_set; } and you should see similar improvements. Though this might allow you to move this logic to a better place in your code. -- John-Mark Gurney Voice: +1 415 225 5579 "All that I will do, has been done, All that I have, has not."