{"id":1511,"date":"2020-03-02T20:50:06","date_gmt":"2020-03-03T01:50:06","guid":{"rendered":"https:\/\/enterpriseadmins.org\/blog\/?p=1511"},"modified":"2020-03-23T10:36:01","modified_gmt":"2020-03-23T14:36:01","slug":"scripts-to-max-out-cpu-and-memory","status":"publish","type":"post","link":"https:\/\/enterpriseadmins.org\/blog\/scripting\/scripts-to-max-out-cpu-and-memory\/","title":{"rendered":"Scripts to max out CPU and Memory"},"content":{"rendered":"\n<p>Most of the time we want our virtual machines to run as optimally as possible.  We don&#8217;t want to see CPU contention or high memory conditions.  However, on occasion we may want to have some stress to see what that looks like in monitoring tools like vRealize Operations.  I created two small scripts that will run in TinyCore Linux, one consuming CPU and the other memory.  For info on creating a TinyCore template, you may want to check out this post: <a href=\"https:\/\/enterpriseadmins.org\/blog\/lab-infrastructure\/lightweight-vm-for-testing-tinycore-linux\/\">https:\/\/enterpriseadmins.org\/blog\/lab-infrastructure\/lightweight-vm-for-testing-tinycore-linux\/<\/a>.  Here are the scripts for reference:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">cpubusy.sh<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>cpus=$(grep -ci processor \/proc\/cpuinfo)\necho \"System has $cpus CPUs, starting thread for each.\"\n\nfor i in $( seq 1 $cpus )\ndo\n  echo \" ..starting background process #$i to consume CPU.\"\n  sha1sum \/dev\/zero &amp;\ndone\n\necho \"You can check processes with 'top' sorting by CPU with 'P'.\"\necho \"To end all processes run 'killall sha1sum'\"<\/code><\/pre>\n\n\n\n<p>Note: this file is available at  <a href=\"https:\/\/raw.githubusercontent.com\/bwuch\/code-snips\/master\/cpubusy.sh\">https:\/\/raw.githubusercontent.com\/bwuch\/code-snips\/master\/cpubusy.sh<\/a> <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">memfill.sh<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"This script will fill memory to 90% with zeros.\"\n# disable swap, only use RAM\nsudo swapoff -a\n\n# find current system memory\nmem=$(sudo grep -i memtotal \/proc\/meminfo | awk '{print $2}')\n\n# calculate 90% of current memory, using 100% will cause instability\nfillmem=$(expr $mem \/ 100 \\* 90)\n\n# tmpfs is mounted as 50% by default, remount with our 90% number\necho \" ..remounting \/dev\/shm to use 90% of MemTotal\"\nsudo mount -o remount,size=$fillmem\"k\" \/dev\/shm\n\n# show the current size of tmpfs\ndf -h | grep tmpfs\n\n# fill that space with 1k block zeros\necho \" ..starting memory fill process.\"\ndd if=\/dev\/zero of=\/dev\/shm\/fill bs=1k<\/code><\/pre>\n\n\n\n<p>Note: this file is available at:  <a href=\"https:\/\/raw.githubusercontent.com\/bwuch\/code-snips\/master\/memfill.sh\">https:\/\/raw.githubusercontent.com\/bwuch\/code-snips\/master\/memfill.sh<\/a> <\/p>\n\n\n\n<p>I placed these files in the tc user home directory (<strong>\/home\/tc<\/strong>) and set them to executable with <strong>chmod +x filename.sh<\/strong>.  <\/p>\n\n\n\n<p>If you&#8217;d like, you can add entries to have these scripts start automatically at boot &#8212; if you want an appliance that maxes out resources all the time.  To do this, use <strong>sudo vi \/opt\/bootsync.sh<\/strong> and add entries at the end of the file for <strong>\/home\/tc\/cpubusy.sh &amp;<\/strong> and\/or <strong>\/home\/tc\/memfill.sh &amp;<\/strong>.  Note: the ending ampersand causes the script to run in the background and not wait for completion.  <\/p>\n\n\n\n<p>Typing <strong>backup <\/strong>will allow you to make these files &amp; changes persistent.<\/p>\n\n\n\n<p>Note: its much easier getting this text copied over if ssh is installed\/configured on your tinycore VM.  There is a very good write up on how to do this here:  <a href=\"https:\/\/iotbytes.wordpress.com\/configure-ssh-server-on-microcore-tiny-linux\/\">https:\/\/iotbytes.wordpress.com\/configure-ssh-server-on-microcore-tiny-linux\/<\/a>.  This post also covers how to include credentials (etc\/shadow) in the file list backed up by TinyCore, which is also very useful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most of the time we want our virtual machines to run as optimally as possible. We don&#8217;t want to see CPU contention or high memory conditions. However, on occasion we may want to have some stress to see what that &hellip; <a href=\"https:\/\/enterpriseadmins.org\/blog\/scripting\/scripts-to-max-out-cpu-and-memory\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[9,3],"tags":[],"class_list":["post-1511","post","type-post","status-publish","format-standard","hentry","category-lab-infrastructure","category-scripting"],"_links":{"self":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1511","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/comments?post=1511"}],"version-history":[{"count":6,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1511\/revisions"}],"predecessor-version":[{"id":1520,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1511\/revisions\/1520"}],"wp:attachment":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/media?parent=1511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/categories?post=1511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/tags?post=1511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}