Wednesday, December 24, 2014

Mini: escape sequence

Mini Tip: escape sequence

- Cisco:

CTRL-^ : CTRL + - in french keyboard (the key which hold the 6 ), just pressing thje CTRL key + the 6 key


- Telnet legacy

CTRL-]  : CTRL-$ in french keyboard

CMD: How to set Affinity

Intro




Hi,

today i'll write a tip about cmd and affinity because nowadays it is common to have multi-core processors, and sometimes we don't want that a process spawn on all the cores.

So, in the MS Windows os, you can achieve this by various ways. You can do it with process explorer, just select the process in the list, do a right clic, and then clic on Set Affinity. A multi check box will be presented, then you just have to choose the core you want your process run onto.

But this method is noy optimal, because you'll have to do it each time you run your process.

Another method is to write a script, or a shortcut, and then launch the process via the cmd program.

CMD have an option named Affinity, where you will provide a hexadecimal number wich serve as a mask.

I found in the web a lot of article which said that if you want to run your process on core 0, you just have to put the number 1, like:

cmd /AFFINITY 1 notepad.exe

Result:


And if you wan to run it on core 1, just put the number 2:

cmd /AFFINITY 2 notepad.exe

Result:


Ok, it will work if just have a dual core, or for the first 2 core. But i dislike those article, because a lot of people, including me, first understand that if you want to run the process on core 2, i just have to put the number 3, right ?

In fact no !

Try this in a Quad core, or more:


cmd /AFFINITY 3 notepad.exe

You will have the process running on core 0 AND 1 !

Result:


The reason is because the number you provide is a mask value !

Core Table



Imagine if you have an octo core, each core will have the value 1, and write it like this:


CORE ID   : 7 6 5 4 3 2 1 0
===========================
CORE VALUE: 1 1 1 1 1 1 1 1



AND Truth Table



To calculate the result, we have to do a logical AND. To have TRUE, you have to have TRUE AND TRUE.
If you have FALSE (0) AND TRUE (1) the result is FALSE (0).
If you have FALSE (0) AND FALSE (1) the result is FALSE (0).

As a reminder, here is the truth table for the AND:


 |1  0
-------
1|1  0
 |
0|0  0



/AFFINITY 1



Ok, lets apply this to our core table ( 1 in binary form is: 0001, or 00000001)


CORE ID   : 7 6 5 4 3 2 1 0
===========================
CORE VALUE: 1 1 1 1 1 1 1 1
VALUE 1   : 0 0 0 0 0 0 0 1
---------------------------
AND RESULT: 0 0 0 0 0 0 0 1



So if you provide 1 to the /AFFINITY, your process will run onto core 0.

/AFFINITY 2



Lets try this with value 2 ( 2 in binary form is: 0010, or 00000010):


CORE ID   : 7 6 5 4 3 2 1 0
===========================
CORE VALUE: 1 1 1 1 1 1 1 1
VALUE 2   : 0 0 0 0 0 0 1 0
---------------------------
AND RESULT: 0 0 0 0 0 0 1 0



So if you provide 2 to the /AFFINITY, your process will run onto core 1.

/AFFINITY 3



Now, lets try this with value 3 ( 3 in binary form is: 0011, or 00000011):


CORE ID   : 7 6 5 4 3 2 1 0
===========================
CORE VALUE: 1 1 1 1 1 1 1 1
VALUE 3   : 0 0 0 0 0 0 1 1
---------------------------
AND RESULT: 0 0 0 0 0 0 1 1


So if you provide 2 to the /AFFINITY, your process will run onto core 0 AND 1, and not onto core 3 ! (sic!)

We can continue.

/AFFINITY A



Some hex value have remarkable values. For example, the A ( bin form: 1010 ):



CORE ID   : 7 6 5 4 3 2 1 0
===========================
CORE VALUE: 1 1 1 1 1 1 1 1
VALUE A   : 0 0 0 0 1 0 1 0
---------------------------
AND RESULT: 0 0 0 0 1 0 1 0



So here, our process will run onto the first half odd core.

/AFFINITY AA



And if i want my process running only onto odd cores i provide the /AFFINITY AA value:


CORE ID   : 7 6 5 4 3 2 1 0
===========================
CORE VALUE: 1 1 1 1 1 1 1 1
VALUE AA  : 1 0 1 0 1 0 1 0
---------------------------
AND RESULT: 1 0 1 0 1 0 1 0


Result:



I hope this tip by the example will clarify your minds !

In fact, it clarified mine :-)