Skip to content

Instantly share code, notes, and snippets.

@pavel-odintsov
Created June 25, 2015 12:25
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save pavel-odintsov/9b065f96900da40c5301 to your computer and use it in GitHub Desktop.
Save pavel-odintsov/9b065f96900da40c5301 to your computer and use it in GitHub Desktop.
irq_balance_habrahabr.sh
#!/bin/bash
# from http://habrahabr.ru/post/108240/
ncpus=`grep -ciw ^processor /proc/cpuinfo`
test "$ncpus" -gt 1 || exit 1
n=0
for irq in `cat /proc/interrupts | grep eth | awk '{print $1}' | sed s/\://g`
do
f="/proc/irq/$irq/smp_affinity"
test -r "$f" || continue
cpu=$[$ncpus - ($n % $ncpus) - 1]
if [ $cpu -ge 0 ]
then
mask=`printf %x $[2 ** $cpu]`
echo "Assign SMP affinity: eth queue $n, irq $irq, cpu $cpu, mask 0x$mask"
echo "$mask" > "$f"
let n+=1
fi
done
@hit0ri
Copy link

hit0ri commented Jun 25, 2015

You can replace line 4 with this:
ncpus=$(nproc)

@pavel-odintsov
Copy link
Author

Thanks for feedback! But it's very draft tool and you could fix it as you want :)

@SaveTheRbtz
Copy link

Intel provides set_irq_affinity.sh with their drivers, that can do that + XPS and NUMA:
https://gist.github.com/SaveTheRbtz/8875474

@jwbensley
Copy link

jwbensley commented Sep 9, 2017

@hit0ri - The problem with nrpoc is that if you are using isolcpu/nohz_full/rcu_nocbs it won't count those CPUs. Example case, we set nohz_full=1-15 isolcpus=1-15 rcu_nocbs=1-15 and use core 0 for OS, nproc returns 1 instead of 16.

ncpus=`grep -ciw ^processor /proc/cpuinfo` works fine.

@Jordynhollander
Copy link

(dont ask me why, hehe) On php

	$numCpus = 1;
	$cpuinfo = file_get_contents('/proc/cpuinfo');
	preg_match_all('/^processor/m', $cpuinfo, $matches);
	$numCpus = count($matches[0]);
	$ethint = array();
	$baseethint = array();
	foreach(explode(PHP_EOL, file_get_contents("/proc/interrupts")) as $interrupt){
		foreach($interfaces as $eth){
			if (strpos($interrupt, $eth) !== false) {
				$name = @end(explode(" ", $interrupt));
				$ethint[$name] = (int)explode(":", $interrupt)[0];
				$basename = explode("-", $name)[0];
				if ($basename != $name) {
					$baseethint[$basename] = 1;
				}
			}
		}
	}
	$counter = 0;
	foreach($ethint as $name => $interrupt){
		$core = $counter % $numCpus;
		$basename = explode("-", $name)[0];
		if ($basename != $name || !array_key_exists ($basename, $baseethint)) {
			echo "\t".$name." - IRQ-".$interrupt." => ".$core."\n";
			file_put_contents("/proc/irq/".$interrupt."/smp_affinity", dechex(pow(2,$core)));
			$counter++;
		}
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment