#!/bin/bash # # Get an estimate of how CPU-hoggy to be. # # Usage: cpus2use.sh # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, you can access it online at # http://www.gnu.org/licenses/gpl-2.0.html. # # Copyright (C) IBM Corporation, 2013 # # Authors: Paul E. McKenney global ncpus := $[grep '^processor' /proc/cpuinfo | wc -l] global idlecpus := $[mpstat | tail -1 | \ awk -v ncpus=$ncpus '{ print ncpus * ($7 + $NF) / 100 }] awk -v ncpus=$ncpus -v idlecpus=$idlecpus < /dev/null ' BEGIN { cpus2use = idlecpus; if (cpus2use < 1) cpus2use = 1; if (cpus2use < ncpus / 10) cpus2use = ncpus / 10; if (cpus2use == int(cpus2use)) cpus2use = int(cpus2use) else cpus2use = int(cpus2use) + 1 print cpus2use; }< /dev/null ' BEGIN { cpus2use = idlecpus; if (cpus2use < 1) cpus2use = 1; if (cpus2use < ncpus / 10) cpus2use = ncpus / 10; if (cpus2use == int(cpus2use)) cpus2use = int(cpus2use) else cpus2use = int(cpus2use) + 1 print cpus2use; }' (CommandList children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:ncpus) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (C {(grep)} {(SQ <"^processor">)} {(/proc/cpuinfo)}) (C {(wc)} {(-l)}) ] negated: False ) ] ) left_token: spids: [71 85] ) } spids: [70] ) ] spids: [70] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:idlecpus) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (C {(mpstat)}) (C {(tail)} {(-1)}) (C {(awk)} {(-v)} {(Lit_VarLike "ncpus=") ($ VSub_Name "$ncpus")} {(SQ <"{ print ncpus * ($7 + $NF) / 100 }">)} ) ] negated: False ) ] ) left_token: spids: [88 111] ) } spids: [87] ) ] spids: [87] ) (SimpleCommand words: [ {(awk)} {(-v)} {(Lit_VarLike "ncpus=") ($ VSub_Name "$ncpus")} {(-v)} {(Lit_VarLike "idlecpus=") ($ VSub_Name "$idlecpus")} { (SQ <"\n"> <"BEGIN {\n"> <"\tcpus2use = idlecpus;\n"> <"\tif (cpus2use < 1)\n"> <"\t\tcpus2use = 1;\n"> <"\tif (cpus2use < ncpus / 10)\n"> <"\t\tcpus2use = ncpus / 10;\n"> <"\tif (cpus2use == int(cpus2use))\n"> <"\t\tcpus2use = int(cpus2use)\n"> <"\telse\n"> <"\t\tcpus2use = int(cpus2use) + 1\n"> <"\tprint cpus2use;\n"> <"}"> ) } ] redirects: [(Redir op_id:Redir_Less fd:-1 arg_word:{(/dev/null)} spids:[125])] ) ] )