{"id":71,"date":"2014-02-13T08:26:08","date_gmt":"2014-02-13T08:26:08","guid":{"rendered":"https:\/\/avalon.land\/blog\/?p=71"},"modified":"2017-10-26T11:03:06","modified_gmt":"2017-10-26T11:03:06","slug":"swap-calculate","status":"publish","type":"post","link":"https:\/\/avalon.land\/blog\/it\/swap-calculate\/","title":{"rendered":"\u0427\u0442\u043e \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 swap?"},"content":{"rendered":"<p>\u041d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0432, \u0447\u0442\u043e\u0431 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u043a\u0430\u043a\u0438\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 swap.<br \/>\n<!--more--><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#!\/bin\/bash \r\n# Get current swap usage for all running processes\r\n# Erik Ljungstrom 27\/05\/2011\r\n# Modified by Mikko Rantalainen 2012-08-09\r\n# Pipe the output to &quot;sort -nk3&quot; to get sorted output\r\nSUM=0\r\nOVERALL=0\r\nfor DIR in `find \/proc\/ -maxdepth 1 -type d -regex &quot;^\/proc\/&#x5B;0-9]+&quot;`\r\ndo\r\n    PID=`echo $DIR | cut -d \/ -f 3`\r\n    PROGNAME=`ps -p $PID -o comm --no-headers`\r\n    for SWAP in `grep Swap $DIR\/smaps 2&gt;\/dev\/null | awk &#039;{ print $2 }&#039;`\r\n    do\r\n        let SUM=$SUM+$SWAP\r\n    done\r\n    if (( $SUM &gt; 0 )); then\r\n        echo &quot;PID=$PID swapped $SUM KB ($PROGNAME)&quot;\r\n    fi\r\n    let OVERALL=$OVERALL+$SUM\r\n    SUM=0\r\ndone\r\necho &quot;Overall swap used: $OVERALL KB&quot;\r\n<\/pre>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#!\/bin\/bash\r\n\r\n    # find-out-what-is-using-your-swap.sh\r\n    # -- Get current swap usage for all running processes\r\n    # --\r\n    # -- rev.0.3, 2012-09-03, Jan Smid          - alignment and intendation, sorting\r\n    # -- rev.0.2, 2012-08-09, Mikko Rantalainen - pipe the output to &quot;sort -nk3&quot; to get sorted output\r\n    # -- rev.0.1, 2011-05-27, Erik Ljungstrom   - initial version\r\n\r\n\r\nSCRIPT_NAME=`basename $0`;\r\nSORT=&quot;kb&quot;;                 # {pid|kB|name} as first parameter, &#x5B;default: kb]\r\n&#x5B; &quot;$1&quot; != &quot;&quot; ] &amp;&amp; { SORT=&quot;$1&quot;; }\r\n\r\n&#x5B; ! -x `which mktemp` ] &amp;&amp; { echo &quot;ERROR: mktemp is not available!&quot;; exit; }\r\nMKTEMP=`which mktemp`;\r\nTMP=`${MKTEMP} -d`;\r\n&#x5B; ! -d &quot;${TMP}&quot; ] &amp;&amp; { echo &quot;ERROR: unable to create temp dir!&quot;; exit; }\r\n\r\n&gt;${TMP}\/${SCRIPT_NAME}.pid;\r\n&gt;${TMP}\/${SCRIPT_NAME}.kb;\r\n&gt;${TMP}\/${SCRIPT_NAME}.name;\r\n\r\nSUM=0;\r\nOVERALL=0;\r\n    echo &quot;${OVERALL}&quot; &gt; ${TMP}\/${SCRIPT_NAME}.overal;\r\n\r\nfor DIR in `find \/proc\/ -maxdepth 1 -type d -regex &quot;^\/proc\/&#x5B;0-9]+&quot;`;\r\ndo\r\n    PID=`echo $DIR | cut -d \/ -f 3`\r\n    PROGNAME=`ps -p $PID -o comm --no-headers`\r\n\r\n    for SWAP in `grep Swap $DIR\/smaps 2&gt;\/dev\/null| awk &#039;{ print $2 }&#039;`\r\n    do\r\n        let SUM=$SUM+$SWAP\r\n    done\r\n\r\n    if (( $SUM &gt; 0 ));\r\n    then\r\n        echo -n &quot;.&quot;;\r\n        echo -e &quot;${PID}\\t${SUM}\\t${PROGNAME}&quot; &gt;&gt; ${TMP}\/${SCRIPT_NAME}.pid;\r\n        echo -e &quot;${SUM}\\t${PID}\\t${PROGNAME}&quot; &gt;&gt; ${TMP}\/${SCRIPT_NAME}.kb;\r\n        echo -e &quot;${PROGNAME}\\t${SUM}\\t${PID}&quot; &gt;&gt; ${TMP}\/${SCRIPT_NAME}.name;\r\n    fi\r\n    let OVERALL=$OVERALL+$SUM\r\n    SUM=0\r\ndone\r\necho &quot;${OVERALL}&quot; &gt; ${TMP}\/${SCRIPT_NAME}.overal;\r\necho;\r\necho &quot;Overall swap used: ${OVERALL} kB&quot;;\r\necho &quot;========================================&quot;;\r\ncase &quot;${SORT}&quot; in\r\n    name )\r\n        echo -e &quot;name\\tkB\\tpid&quot;;\r\n        echo &quot;========================================&quot;;\r\n        cat ${TMP}\/${SCRIPT_NAME}.name|sort -r;\r\n        ;;\r\n\r\n    kb )\r\n        echo -e &quot;kB\\tpid\\tname&quot;;\r\n        echo &quot;========================================&quot;;\r\n        cat ${TMP}\/${SCRIPT_NAME}.kb|sort -rh;\r\n        ;;\r\n\r\n    pid | * )\r\n        echo -e &quot;pid\\tkB\\tname&quot;;\r\n        echo &quot;========================================&quot;;\r\n        cat ${TMP}\/${SCRIPT_NAME}.pid|sort -rh;\r\n        ;;\r\nesac\r\nrm -fR &quot;${TMP}\/&quot;;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u041d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0432, \u0447\u0442\u043e\u0431 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u043a\u0430\u043a\u0438\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 swap.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-71","post","type-post","status-publish","format-standard","hentry","category-it","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/avalon.land\/blog\/wp-json\/wp\/v2\/posts\/71","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/avalon.land\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/avalon.land\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/avalon.land\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/avalon.land\/blog\/wp-json\/wp\/v2\/comments?post=71"}],"version-history":[{"count":9,"href":"https:\/\/avalon.land\/blog\/wp-json\/wp\/v2\/posts\/71\/revisions"}],"predecessor-version":[{"id":80,"href":"https:\/\/avalon.land\/blog\/wp-json\/wp\/v2\/posts\/71\/revisions\/80"}],"wp:attachment":[{"href":"https:\/\/avalon.land\/blog\/wp-json\/wp\/v2\/media?parent=71"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/avalon.land\/blog\/wp-json\/wp\/v2\/categories?post=71"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/avalon.land\/blog\/wp-json\/wp\/v2\/tags?post=71"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}