How To Control Which Core A Process Runs On - by Parul Dhingra - Medium
How To Control Which Core A Process Runs On - by Parul Dhingra - Medium
!"#$%&'()*+#"
,$%&-./&0101 2 3&4)*"6 2 7)895*
=5"#C(
!"#$%"$&"'%(")$#*+&*$&"(,$-$.("&,//$(0'/$"'1
The Key goals for any power requirement efforts on a mobile device are to ensure
overall battery usage is optimal. The objective of the tuning efforts is to
maximize performance without impacting power utilization as much as possible.
()#*+%,-./0#)
YZ&:;%%;<5#8
=;>9<"#5&?*+)*55#&@@&A&B)C#;8;>9&D&E5C(&F%;++5#&D&G
H#")*&"4H)65I9#;$8&+55J
:;%%;<
!"#$%&#"'%!$3.*'
G%5I"*65#&!"L"+5;#+);$
N(+&8+':$!"@,9(,#$%"$-W"+3
+'/%-))+':$:&&$-/$3,.,'3,'&7$6"(
In a single core system, the OS allows multiple processes to run by sharing CPU N,((-6"(@
time with the multiple processes. This is called concurrency, which gives the
[GP@\]
illusion of multiple processes executing at once, but is in fact just using a !+/%"(7$"6$\>#-(,O$]W")0%+"'
scheduler to give each process dedicated time on the CPU. The time associated N+@,)+',
will do its best to allow processes to run simultaneously (by placing processes on P,-('$%*,$9-/+&/$"6$X.,(-%+':
/7/%,@/$6"($L,W"./
different cores) in addition to running concurrently (different processes on the
same core).
In case of single threaded heavy application, you will see that the main thread S5%L =9"9$8 ^#)95#8 F%;+ R"#55#8 !#)K"CO E5#48 GH;$9
P*;<"H%5
migrates between different CPUs frequently and it runs for some duration on
each CPU. This frequent context switching for such big thread may cause power
hit/performance hit, so we need to affine that particular thread to a fixed CPU
or a range of CPUs so that it won’t migrate and save the power/perf.
To control which core a process runs on, we can tell the scheduler to give a
process a certain affinity towards a given set of CPUs.
It’s a feature that allows you to bind or unbind processes to a particular central
processing unit, or a range of CPUs.
Yes, you can tell the system which CPU core(s) should be used to run a particular
process. It’s the task of the JVM’s optimizer to ensure, that objects affine to one
thread are placed close to each other in memory to fit into one CPU’s cache but
place objects affine to different threads not too close to each other to avoid that
they share a cache line as otherwise two CPUs/cores have to synchronize them
too often.
The taskset command allows you to set or retrieve a process given its pid, or to
launch a new command with a given CPU affinity.
So hexadecimal value ‘f’ here means the process can run on any of the 4
processor cores: 0,1,2,3.
If you want the output to be in terms of CPU range, you can add the -c command
line option.
1bengal:/#taskset-cp5941
pid5941'scurrentaffinitylist:0-3
And then you can again check the new affinity using the following command:
taskset -p 5941
This is no big deal. All you have to do is to add the -c command-line option to the
command we’ve used in the previous section along with the CPU core range as
input.
Here is an example:
When taskset returns, it is guaranteed that the given program has been
scheduled to a legal CPU.
2'3$%*+/$#-/$-))4
Do share your feedback and suggestions in the comments section below.
56$7"0$)+8,3$%*+/$9)":;$*+%$%*,$ $<$=%-7$%0',3<
-.3
-.3
!"#$%&#"'%()#*+%,-./0#) :;%%;<
=;>9<"#5&?*+)*55#&@@&A&B)C#;8;>9&D&E5C(&F%;++5#&D&G&H#")*&"4H)65I9#;$8&+55J
7;K5&L;6C"898&;#&"$6);H;;J8M&75"#*&;*&9(5&+;&<)9(&;$#&*5<&"LLN E#O&P*;<"H%5
1$2"''$/3$3%&#"'%!$3.*'
Q"6);&R"C" S"*I);*+
>+),/%"',/$-'3$?"-3@-./$ABCD GIGE<$>-J+@0@$K,/%+':$L,.%*
BE$CFCG;$CFCCH "6$%*,$M-(,'%*,/,/
=")+3+%7$N0%"(+-)O$-))$-9"0% L-7$Q$"6$RSFL-7/"6T"%)+'
P""./
='"#6)-8,$LU$+/$-#,/"@,4$!,(, T,7$L,WX./$M(+'&+.),/$%"$6"&0/
+/$#*7V +'$CFCG
!"#$%"$6+J$Y+DZ+$.("9),@$+' 5@.),@,'%+':$-'$2@-["'$L-/*
090'%0 U0%%"'$#"(86)"#