-
Notifications
You must be signed in to change notification settings - Fork 3
/
run_with_n_cpus
executable file
·70 lines (57 loc) · 1.95 KB
/
run_with_n_cpus
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
# This is a defunct strategy. There's no way it will really work.
# We can try to come up with some nice value at which to run the main
# multi-threaded process, but we run into various problems. Scheduler
# priorities are not guarantees of anything.
# When the nice is too low, the main threads sneak onto the other
# processors in spite of the dummy processes. When the nice is too
# high, the threads fail to migrate to other processers even though
# they are available.
# (Wait, I need to confirm that second part... I thought I'd seen it
# take only 100% in two-cpu mode, but now I'm having trouble
# reproducing.) Ok, yes, I'm seeing it hover at 130% when in should
# use 3 cpus (at nice 19). And it's only using 200% at 4 cpus.
# First argument is number of cpus.
N=$1
shift
TOTAL=`number_of_cpus`
DUMMIES=$((TOTAL - N))
echo "Total CPUS/Cores found: $TOTAL, disabling $DUMMIES processors"
if [ "$NOSUDO" = "" ];
then
if [ "$SUDO" = "" ]; then SUDO=sudo; fi
# First we make sure all these CPUs are on, in case somebody else left it in a bad state.
for ((i=1; i < $N; i++)) do
$SUDO sh -c "echo 1 > /sys/devices/system/cpu/cpu$i/online"
done
for ((i=$N; i < $TOTAL; i++)) do
#echo Disabling $i
$SUDO sh -c "echo 0 > /sys/devices/system/cpu/cpu$i/online"
done
time $*
for ((i=$N; i < $TOTAL; i++)) do
#echo Enabling $i
$SUDO sh -c "echo 1 > /sys/devices/system/cpu/cpu$i/online"
done
echo Reenabled extra processors.
else
# Tried 5 7 8 10 19
if [ "$NICE" = "" ]; then
NICE=8;
echo Setting nice to $NICE.
fi
echo SUDO unavailable so we attempt to run dummy processes to disable cpus.
SCHEME=chez
TMP=`mktemp`
echo '(let loop () (loop))' > $TMP
CHILDPIDS=""
for ((i=1; i <= $DUMMIES; i++)) do
$SCHEME $TMP &
CHILDPIDS+=" $! "
done
echo "Running command with nice $NICE:"
echo time nice -n $NICE $*
time nice -n $NICE $*
kill -9 $CHILDPIDS
echo Killed dummy processes.
fi