#!/bin/sh
# Copyright (c) 2012-2013 National Instruments.
# All rights reserved.
[ "${VERBOSE}" != "no" ] && echo -n "Starting nisetupkernelconfig:"

#This disables RT scheduler's CPU throttling for National Instruments LabVIEW Real-Time
echo -1 > /proc/sys/kernel/sched_rt_runtime_us

#This forces CPU affinity of IRQs to Core-0. Some reserved IRQs and specific IRQs
#such as timer-watch-dog (twd) are per core and their affinity cannot be changed
#and thus we ignore the errors on those by redirecting error to /dev/null.
#Note: smp_affinity file accepts CPU mask
echo 1 > /proc/irq/default_smp_affinity
for x in /proc/irq/*/smp_affinity;
do
  echo 1 > $x
done 2> /dev/null

# Set affinity for running IRQ threads to Core-0 immediately.
# Setting /proc/irq/default_smp_affinity and /proc/irq/*/smp_affinity as done above will
# eventually cause the kernel to change the affinity for these threads automatically, but
# not until the next time those threads are scheduled. We want to remove the possibility
# of that happening during jitter-sensitive code in a user application.
PIDS=`grep -l irq/ /proc/*/comm 2>/dev/null | cut -d/ -f3`
if [ -z "$PIDS" ] ; then
  echo $0: Error: Setting affinity for running IRQ threads.
  exit 1
fi
echo $PIDS | xargs -n1 taskset -p 1 >/dev/null 2>&1

[ "${VERBOSE}" != "no" ] && echo "done"
