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" >