0% found this document useful (0 votes)
54 views1 page

How To Control Which Core A Process Runs On - by Parul Dhingra - Medium

In a single core system, the OS allows multiple processes to run concurrently by rapidly switching the CPU between processes using a scheduler. When multiple cores are available, the scheduler can run processes simultaneously on different cores to improve performance. For large single-threaded processes, frequently switching cores can reduce performance; setting an affinity to a fixed core prevents unnecessary switching. The taskset command can be used to control which cores a process runs on.

Uploaded by

Sara Vana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views1 page

How To Control Which Core A Process Runs On - by Parul Dhingra - Medium

In a single core system, the OS allows multiple processes to run concurrently by rapidly switching the CPU between processes using a scheduler. When multiple cores are available, the scheduler can run processes simultaneously on different cores to improve performance. For large single-threaded processes, frequently switching cores can reduce performance; setting an affinity to a fixed core prevents unnecessary switching. The taskset command can be used to control which cores a process runs on.

Uploaded by

Sara Vana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

X59&89"#956 =)+*&@*

!"#$%&'()*+#"
,$%&-./&0101 2 3&4)*&#5"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+@,)+',

with switching processes on a single core is overhead caused by context U;+58(&F)#%"


switching. P+'0J$N,(@+'-)$^"@@-'3/$N*-%
]W,(7"',$=*"0)3$T'"#

When a multi-core processor is available to an OS (e.g., Linux), the scheduler ')KO"4&=("#4"

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.

Ever heard of the term processor affinity?

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.

Following is its syntax:

taskset [options] mask command [argument…]


taskset [options] -p [mask] pid

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

How to assign a range of CPUs while changing affinity?

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:

taskset -cp 0,3 5941

Following is the output produced in this case:


pid 5941’s current affinity list: 0
pid 5941’s new affinity list: 0,3

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-(,'%*,/,/

,5"*&RK%%# )* R;)*8F5*C( =(#$9)H"*8"%

=")+3+%7$N0%"(+-)O$-))$-9"0% L-7$Q$"6$RSFL-7/"6T"%)+'
P""./

=&F )* S)9"C()&=;%$9);*8&F#")*9#$89 '5K;L8R$##O )* '5K;L8R$##O

='"#6)-8,$LU$+/$-#,/"@,4$!,(, T,7$L,WX./$M(+'&+.),/$%"$6"&0/
+/$#*7V +'$CFCG

!#"955J !)5##5TUK58&75H5CV )* W5*"9;*

!"#$%"$6+J$Y+DZ+$.("9),@$+' 5@.),@,'%+':$-'$2@-["'$L-/*
090'%0 U0%%"'$#"(86)"#

You might also like