{"id":1927,"date":"2011-12-02T11:03:28","date_gmt":"2011-12-02T17:03:28","guid":{"rendered":"http:\/\/icesquare.com\/wordpress\/?p=1927"},"modified":"2011-12-28T15:39:07","modified_gmt":"2011-12-28T21:39:07","slug":"phphow-to-get-the-number-of-cpu-cores-in-fedora-ubuntu-and-freebsd","status":"publish","type":"post","link":"https:\/\/icesquare.com\/wordpress\/phphow-to-get-the-number-of-cpu-cores-in-fedora-ubuntu-and-freebsd\/","title":{"rendered":"[PHP]How to get the number of CPU cores in Fedora, Ubuntu, and FreeBSD"},"content":{"rendered":"<p><img decoding=\"async\" data-attachment-id=\"1932\" data-permalink=\"https:\/\/icesquare.com\/wordpress\/phphow-to-get-the-number-of-cpu-cores-in-fedora-ubuntu-and-freebsd\/cpu\/#main\" data-orig-file=\"https:\/\/icesquare.com\/wordpress\/wp-content\/uploads\/2011\/12\/cpu.jpg\" data-orig-size=\"400,355\" data-comments-opened=\"1\" data-image-title=\"CPU\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/icesquare.com\/wordpress\/wp-content\/uploads\/2011\/12\/cpu.jpg\" src=\"http:\/\/icesquare.com\/wordpress\/wp-content\/uploads\/2011\/12\/cpu-300x266.jpg\" alt=\"\" title=\"CPU\" width=\"250\" class=\"alignleft size-medium wp-image-1932\" srcset=\"https:\/\/icesquare.com\/wordpress\/wp-content\/uploads\/2011\/12\/cpu-300x266.jpg 300w, https:\/\/icesquare.com\/wordpress\/wp-content\/uploads\/2011\/12\/cpu.jpg 400w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>I am working on a PHP application which needs to be deploy to multiple places. One of the variables is the number of CPU core. Since each server has different number of CPU cores, I need to specify the number of CPU core in my application for each server. I think there should be a smarter way to do it.<\/p>\n<p>\nPHP is a scripting language, it has limit support on accessing the hardware level information. In short, there is no library or function to do it. Fortunately, we can do it via shell command. In the other words, the following methods are not limited to PHP, it will work in any languages, as long as your language supports running the UNIX command and catch the return.\n<\/p>\n<div style=\"margin-top:50px;\"><\/div>\n<h2>Getting the number of CPU Cores &#8211; Linux<\/h2>\n<p>(Tested on Fedora, Ubuntu Linux, should work on other Linuxs because they all use the same Linux kernel.)<\/p>\n<pre>\r\ncat \/proc\/cpuinfo | grep processor | wc -l\r\n<\/pre>\n<p>This will return something like this:<\/p>\n<pre>\r\n8\r\n<\/pre>\n<h2>Getting the number of CPU Cores &#8211; FreeBSD<\/h2>\n<pre>\r\nsysctl -a | grep 'hw.ncpu' | cut -d ':' -f2\r\n<\/pre>\n<p>which will return something like this (notice the extra space before the number):<\/p>\n<pre>\r\n8\r\n<\/pre>\n<p><\/p>\n<p>Now, let&#8217;s put everything together. Run the command inside your application (Here I am using PHP for example):<\/p>\n<pre>\r\n\/\/Linux\r\n$cmd = \"cat \/proc\/cpuinfo | grep processor | wc -l\";\r\n\r\n\/\/FreeBSD\r\n$cmd = \"sysctl -a | grep 'hw.ncpu' | cut -d ':' -f2\";\r\n\r\n$cpuCoreNo = intval(trim(shell_exec($cmd)));\r\n<\/pre>\n<p>Of course, you can make the application to detect the system automatically:<\/p>\n<pre>\r\n$cmd = \"uname\";\r\n$OS = strtolower(trim(shell_exec($cmd)));\r\n\r\nswitch($OS){\r\n   case('linux'):\r\n      $cmd = \"cat \/proc\/cpuinfo | grep processor | wc -l\";\r\n      break;\r\n\r\n   case('freebsd'):\r\n      $cmd = \"sysctl -a | grep 'hw.ncpu' | cut -d ':' -f2\";\r\n      break;\r\n\r\n   default:\r\n      unset($cmd);\r\n}\r\n\r\nif ($cmd != ''){\r\n   $cpuCoreNo = intval(trim(shell_exec($cmd)));\r\n}\r\n<\/pre>\n<p>That&#8217;s it! Happy PHPing.<\/p>\n<p>&#8211;Derrick<\/p>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am working on a PHP application which needs to be deploy to multiple places. One of the variables is the number of CPU core. Since each server has different <a class=\"more-link\" href=\"https:\/\/icesquare.com\/wordpress\/phphow-to-get-the-number-of-cpu-cores-in-fedora-ubuntu-and-freebsd\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"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_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_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":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[94,7,10,141,6,69],"tags":[137,171,48,172],"class_list":["post-1927","post","type-post","status-publish","format-standard","hentry","category-computer-problems-computer-help-pc-support","category-freebsd","category-php","category-programming-problems","category-technology","category-troubleshooting-technology","tag-cpu","tag-freebsd","tag-linux","tag-php"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p1sqzr-v5","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":3925,"url":"https:\/\/icesquare.com\/wordpress\/freebsdupgrade-php-5-5-to-5-6\/","url_meta":{"origin":1927,"position":0},"title":"[FreeBSD]Upgrade PHP 5.5 to 5.6","author":"Derrick","date":"February 20, 2015","format":false,"excerpt":"It is not easy to upgrade PHP 5.5 to 5.6 in FreeBSD. Without proper preparation, the upgrade process may drive you nut. Before you decide to get your hands wet, here are what I recommend you to do: Back up your files Test your website in a PHP 5.6 environment\u2026","rel":"","context":"In &quot;Computer Problems&quot;","block_context":{"text":"Computer Problems","link":"https:\/\/icesquare.com\/wordpress\/category\/technology\/computer-problems-computer-help-pc-support\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":462,"url":"https:\/\/icesquare.com\/wordpress\/tornado-web-very-fast\/","url_meta":{"origin":1927,"position":1},"title":"Tornado Web Server &#8211; Very Fast!","author":"Derrick","date":"November 29, 2009","format":false,"excerpt":"I just did a benchmark comparison on Tornado Web Server and Apache + PHP server. The result is pretty amazing. What to test: A simple Hello World application. How: ab -n 1000 -c 300 http:\/\/myserver.ip Test Environment OS: FreeBSD 7.1 CPU: Pentium III 933 MHz Ram: 256 MB PHP: 5.2.9\u2026","rel":"","context":"In &quot;Computer Problems&quot;","block_context":{"text":"Computer Problems","link":"https:\/\/icesquare.com\/wordpress\/category\/technology\/computer-problems-computer-help-pc-support\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2792,"url":"https:\/\/icesquare.com\/wordpress\/phpfreebsd-the-databasesphp5-sqlite-port-has-been-deleted-removed-from-core-php-aborting-update\/","url_meta":{"origin":1927,"position":2},"title":"[PHP\/FreeBSD]===>>> The databases\/php5-sqlite port has been deleted: Removed from core php ===>>> Aborting update","author":"Derrick","date":"May 17, 2012","format":false,"excerpt":"[ad#adbrite] When PHP 5.4 was available few months ago, I was hesitated to upgrade my FreeBSD box to upgrade to 5.4, because it comes with lots of big changes, including removing LOTS of old stuffs that were available in 5.3 or earlier versions. Today PHP 5.4.3 becomes available in FreeBSD\u2026","rel":"","context":"In &quot;Computer Problems&quot;","block_context":{"text":"Computer Problems","link":"https:\/\/icesquare.com\/wordpress\/category\/technology\/computer-problems-computer-help-pc-support\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1380,"url":"https:\/\/icesquare.com\/wordpress\/how-to-improve-zfs-performance\/","url_meta":{"origin":1927,"position":3},"title":"How to Improve ZFS performance","author":"Derrick","date":"August 24, 2014","format":false,"excerpt":"First Edition: March 14, 2010 Last Edited: January 10, 2026 \u00a0 In this tutorial, I will show you how to improve the performance of your ZFS using the affordable consumer-grade hardware (e.g., Gigabit network card, standard SATA non-SSD hard drives, consumer-grade motherboard etc.). Many people found a problem on their\u2026","rel":"","context":"In &quot;Computer Problems&quot;","block_context":{"text":"Computer Problems","link":"https:\/\/icesquare.com\/wordpress\/category\/technology\/computer-problems-computer-help-pc-support\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/icesquare.com\/wordpress\/wp-content\/uploads\/2011\/01\/zfs_logo.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":31,"url":"https:\/\/icesquare.com\/wordpress\/installing-apache-php-mysql-and-memcached-on-freebsd\/","url_meta":{"origin":1927,"position":4},"title":"Installing Apache, PHP, MySQL and MemCached on FreeBSD","author":"Derrick","date":"May 19, 2009","format":false,"excerpt":"This is my note on how to to install Apache, PHP, MySQL and MemCached on a FreeBSD 7 Box. 1. MySQL Go to the mysql52-server port directory by typing the command: cd \/usr\/ports\/databases\/mysql51-server Build the port by typing: make BUILD_OPTIMIZED=yes BUILD_STATIC=yes Using these two options will speed up the execution\u2026","rel":"","context":"In &quot;FreeBSD&quot;","block_context":{"text":"FreeBSD","link":"https:\/\/icesquare.com\/wordpress\/category\/sever-problem\/freebsd\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":603,"url":"https:\/\/icesquare.com\/wordpress\/weird-php-error-after-freebsd-port-upgrade\/","url_meta":{"origin":1927,"position":5},"title":"Weird PHP Error after FreeBSD port upgrade (PHP Warning:  PHP Startup: Unable to load dynamic library)","author":"Derrick","date":"April 6, 2010","format":false,"excerpt":"Today I upgraded the port on my FreeBSD box using portmaster tools: portmaster -Dai After that, I found that the PHP gave the following weird error: php -v PHP Warning: PHP Startup: Unable to load dynamic library '\/usr\/local\/lib\/php\/20060613\/memcached.so' - \/usr\/local\/lib\/php\/20060613\/memcached.so: Undefined symbol \" php_session_create_id\" in Unknown on line 0 PHP\u2026","rel":"","context":"In &quot;Computer Problems&quot;","block_context":{"text":"Computer Problems","link":"https:\/\/icesquare.com\/wordpress\/category\/technology\/computer-problems-computer-help-pc-support\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/posts\/1927","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1927"}],"version-history":[{"count":25,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/posts\/1927\/revisions"}],"predecessor-version":[{"id":2417,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/posts\/1927\/revisions\/2417"}],"wp:attachment":[{"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/icesquare.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}