{"id":4726,"date":"2020-11-25T11:23:13","date_gmt":"2020-11-25T19:23:13","guid":{"rendered":"https:\/\/www.flamingspork.com\/blog\/?p=4726"},"modified":"2020-11-25T11:23:13","modified_gmt":"2020-11-25T19:23:13","slug":"why-you-should-use-nproc-and-not-grep-proc-cpuinfo","status":"publish","type":"post","link":"https:\/\/www.flamingspork.com\/blog\/2020\/11\/25\/why-you-should-use-nproc-and-not-grep-proc-cpuinfo\/","title":{"rendered":"Why you should use `nproc` and not grep \/proc\/cpuinfo"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">There&#8217;s something really quite subtle about how the <code>nproc<\/code> utility from GNU coreutils works. If you look <a href=\"https:\/\/www.gnu.org\/software\/coreutils\/manual\/html_node\/nproc-invocation.html\">at the man page<\/a>, it&#8217;s even the very first sentence:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Print the number of processing units available to the current process, which may be less than the number of online processors.<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">So, what does that actually mean? Well, just because the computer some code is running on has a certain number of CPUs (and here I mean &#8220;number of hardware threads&#8221;) doesn&#8217;t necessarily mean that you can spawn a process that uses that many. What&#8217;s a simple example? Containers! Did you know that when you invoke <code>docker<\/code> to run a container, you can easily limit how much CPU the container can use? In this case, we&#8217;re looking at the <code>--cpuset-cpus<\/code> parameter, as the <code>--cpus <\/code>one works differently.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ nproc\n8\n\n$ docker run --cpuset-cpus=0-1 --rm=true -it  amazonlinux:2\nbash-4.2# nproc\n2\nbash-4.2# exit\n\n$ docker run --cpuset-cpus=0-2 --rm=true -it  amazonlinux:2\nbash-4.2# nproc\n3\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As you can see, <code>nproc<\/code> here gets the right bit of information, so if you&#8217;re wanting to do a calculation such as &#8220;Please use up to the maximum available CPUs&#8221; as a parameter to the configuration of a piece of software (such as how many threads to run), you get the right number.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But what if you use some of the other common methods?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ \/usr\/bin\/lscpu -p | grep -c \"^&#091;0-9]\"\n8\n$ grep -c 'processor' \/proc\/cpuinfo \n8\n\n$ docker run --cpuset-cpus=0-1 --rm=true -it  amazonlinux:2\nbash-4.2# yum install -y \/usr\/bin\/lscpu\n......\nbash-4.2# \/usr\/bin\/lscpu -p | grep -c \"^&#091;0-9]\"\n8\nbash-4.2# grep -c 'processor' \/proc\/cpuinfo \n8\nbash-4.2# nproc\n2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this case, if you base your number of threads off grepping <code>lscpu<\/code> you take another dependency (on the <code>util-linux<\/code> package), which isn&#8217;t needed. You also get the wrong answer, as you do by grepping <code>\/proc\/cpuinfo<\/code>. So, what this will end up doing is <strong>just increase the number of context switches, possibly also adding a performance degradation.<\/strong> It&#8217;s not just in <code>docker<\/code> containers where this could be an issue of course, you can use the same mechanism that <code>docker<\/code> uses anywhere you want to control resources of a process.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Another subtle thing to watch out for is differences in <code>\/proc\/cpuinfo<\/code> content depending on CPU architecture. You may not think it&#8217;s an issue today, but who wants to needlessly debug something?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>tl;dr: for determining &#8220;how many processes to run&#8221;: use <code>nproc<\/code>, don&#8217;t grep <code>lscpu<\/code> or <code>\/proc\/cpuinfo<\/code><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There&#8217;s something really quite subtle about how the nproc utility from GNU coreutils works. If you look at the man page, it&#8217;s even the very first sentence: Print the number of processing units available to the current process, which may &hellip; <a href=\"https:\/\/www.flamingspork.com\/blog\/2020\/11\/25\/why-you-should-use-nproc-and-not-grep-proc-cpuinfo\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1],"tags":[760,761,141,759],"class_list":["post-4726","post","type-post","status-publish","format-standard","hentry","category-general","tag-cpuinfo","tag-docker","tag-linux","tag-nproc"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5a6n8-1ee","jetpack-related-posts":[{"id":3884,"url":"https:\/\/www.flamingspork.com\/blog\/2014\/10\/14\/mysql-5-7-5-on-power-thread-priority\/","url_meta":{"origin":4726,"position":0},"title":"MySQL 5.7.5 on POWER &#8211; thread priority","author":"Stewart Smith","date":"2014-10-14","format":false,"excerpt":"Good news everyone! MySQL 5.7.5 is out with a bunch more patches for running well on POWER in the tree. I haven't yet gone and tried it all out, but since I'm me, I look at bugs database and git\/bzr history first. On Intel CPUs, when you're spinning on a\u2026","rel":"","context":"In &quot;IBM&quot;","block_context":{"text":"IBM","link":"https:\/\/www.flamingspork.com\/blog\/category\/work-et-al\/ibm-work-et-al\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":649,"url":"https:\/\/www.flamingspork.com\/blog\/2006\/04\/17\/let-me-extend-a-fuck-you-to-skype\/","url_meta":{"origin":4726,"position":1},"title":"Let me extend a &#8220;Fuck You&#8221; to Skype","author":"Stewart Smith","date":"2006-04-17","format":false,"excerpt":"Intel\/Skype \u00e2\u20ac\u0153Deal\u00e2\u20ac\u009d Locks Out AMD CPUs For 10-Way Calling at The Musings of Chris Samuel Fuck You Skype, fuck you right in the ear. Put down the crack pipe and let your users get up from being bent over the car. Get a grip, some sane practices and then perhaps\u2026","rel":"","context":"In &quot;grumble&quot;","block_context":{"text":"grumble","link":"https:\/\/www.flamingspork.com\/blog\/category\/grumble\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3752,"url":"https:\/\/www.flamingspork.com\/blog\/2014\/06\/03\/mysql-5-6-on-power-patch-available\/","url_meta":{"origin":4726,"position":2},"title":"MySQL 5.6 on POWER (patch available)","author":"Stewart Smith","date":"2014-06-03","format":false,"excerpt":"The following sentence is brought to you by IBM Legal. The postings on this site are my own and don't necessarily represent IBM's positions, strategies or opinions. Okay, now that is out of the way.... If you're the kind of person who follows the MySQL bugs database closely or subscribes\u2026","rel":"","context":"In &quot;code&quot;","block_context":{"text":"code","link":"https:\/\/www.flamingspork.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3989,"url":"https:\/\/www.flamingspork.com\/blog\/2015\/08\/28\/doing-nothing-on-modern-cpus\/","url_meta":{"origin":4726,"position":3},"title":"doing nothing on modern CPUs","author":"Stewart Smith","date":"2015-08-28","format":false,"excerpt":"Sometimes you don't want to do anything. This is understandably human, and probably a sign you should either relax or get up and do something. For processors, you sometimes do actually want to do absolutely nothing. Often this will be while waiting for a lock. You want to do nothing\u2026","rel":"","context":"In &quot;code&quot;","block_context":{"text":"code","link":"https:\/\/www.flamingspork.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3901,"url":"https:\/\/www.flamingspork.com\/blog\/2014\/11\/12\/preliminary-mysql-cluster-benchmark-results-on-power8\/","url_meta":{"origin":4726,"position":4},"title":"Preliminary MySQL Cluster benchmark results on POWER8","author":"Stewart Smith","date":"2014-11-12","format":false,"excerpt":"Yesterday, I got the basics going for MySQL Cluster on POWER. Today, I finished up a couple more patches to improve performance and ran some benchmarks. This is on a 3.7Ghz POWER8 machine with non-balanced memory (only 2 of the 4 NUMA nodes have memory, so we have less total\u2026","rel":"","context":"In &quot;code&quot;","block_context":{"text":"code","link":"https:\/\/www.flamingspork.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4003,"url":"https:\/\/www.flamingspork.com\/blog\/2015\/10\/19\/1-million-sql-queries-per-second-ga-mariadb-10-1-on-power8\/","url_meta":{"origin":4726,"position":5},"title":"1 Million SQL Queries per second: GA MariaDB 10.1 on POWER8","author":"Stewart Smith","date":"2015-10-19","format":false,"excerpt":"A couple of days ago, MariaDB announced that MariaDB 10.1 is stable GA - around 19 months since the GA of MariaDB 10.0. With MariaDB 10.1 comes some important scalabiity improvements, especially for POWER8 systems. On POWER, we're a bit unique in that we're on the higher end of CPUs,\u2026","rel":"","context":"In &quot;IBM&quot;","block_context":{"text":"IBM","link":"https:\/\/www.flamingspork.com\/blog\/category\/work-et-al\/ibm-work-et-al\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts\/4726","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/comments?post=4726"}],"version-history":[{"count":1,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts\/4726\/revisions"}],"predecessor-version":[{"id":4727,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts\/4726\/revisions\/4727"}],"wp:attachment":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/media?parent=4726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/categories?post=4726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/tags?post=4726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}