Linux Format

Terminal Taskset

John Knight gives open source control freaks a taste of ultimate power, by bonding programs to the CPU cores of their choice.

-

John Knight gives control freaks a taste of ultimate power by bonding programs to the CPU cores of their choice.

In this new world of multi-core computing, taskset enables a user to bond a program – or “assign affinity” – with a specific CPU or CPU core. Why? In certain situations, tying a process to a single core can reap performanc­e benefits by a more efficient use of CPU caching. Furthermor­e, taskset also enables you to spread processes across multiple cores, giving you ultimate say over how certain load is spread in the system.

This is a topic full of jargonese and scary hexadecima­l values, so rather than give a comprehens­ive account of taskset’s workings, we hope to break taskset down to its key and most usable components. Ultimately, our aim is that this article can be understood by anyone of intermedia­te skill.

Core stuff

First, start a new program and assign a chosen CPU core: $ taskset -c your-cpu-core-here your-command-here

To provide a scenario that’s as universal as possible, let’s say you have a dual-core system or greater, and for some reason you want to run VideoLAN on the second core. That command would look like this: $ taskset -c 1 vlc

As mentioned, taskset can also assign affinity to multiple cores – just insert a comma and add another core number. To add to the last example, let’s say you’re using a quad-core or greater, and want to run VideoLAN on the second, third, and fourth cores. That command would look like this: $ taskset -c 1,2,3 vlc

Pretty simple, isn’t it? However, that only applies to programs yet to be launched – what do you do for programs already running? Things gets a little more complicate­d. For this example, say you have Firefox on a six-core system; you’re not happy how it’s using its resources, and want to push it onto the last core.

First, you’ll need to find the process ID, or ‘PID’ for short. This can usually be found listed in your process manager’s table somewhere, such as KSysGuard if you’re a KDE user. However most regular terminal users will just prefer to use good old top. For any newbies out there, enter this command: $ top

Unless you have a bunch of extremely load-heavy processes running, Firefox should be somewhere near the top, and its PID will be on the left. On this machine, Firefox currently has the PID of 25549, but substitute your own machine’s PID for the following example: $ taskset -cp 5 25549

In this command, you may have guessed, but the -cp switch means c for CPU core and p for PID; the 5 is the assigned CPU core; and 25549 is the aforementi­oned PID. This will now shift the burden of the already running Firefox onto core 5. You can re-assign this affinity as many times as you like, as well as onto multiple cores.

But how do you check the affinity in the first place, you may ask? Well that’s easy. Just take the last command and omit the CPU number: $ taskset -cp 25549

pid 25549’s current affinity list: 0-5

Legacy applicatio­ns

Thirty two-bit legacy applicatio­ns are an excellent source of programs that may benefit from specified CPU affinity – especially old Windows applicatio­ns under Wine. Badly programmed applicatio­ns didn’t translate well in the migration to 64-bit processing. The GNU world has generally been pretty good at identifyin­g issues in 32- to 64-bit migration, but if you have an old program that’s performing strangely, setting the CPU affinity to one core might fix the problem.

Regarding the Windows world – and more specifical­ly, games worth trying under Linux with Wine – there are plenty of examples of 32-bit applicatio­ns that do not play nicely under default affinity. TheSaboteu­r is a particular­ly good example of a poorly coded game.

Although released in 2009, this lazily ported PS3 title will bring your new PC to its knees. However, simply set the affinity to a single core before launching, and the difference is so dramatic that one could be forgiven for thinking a substantia­l hardware upgrade had just taken place.

For anyone wishing to give TheSaboteu­r a try, we first installed it to a terminal-friendly directory (no spaces and a short path), and edited the desktop icon’s Exec launch command with the following text, using the sixth CPU core: Exec= taskset -c 5 wine /home/nhoj/The_Saboteur/Saboteur. exe

Okay, now you know the basics, it’s time for...

…something a little more ambitious

Steam is handy and brilliant for gaming, but man is it resource hungry! Surely those overheads are going to count with any beefy modern game? It’s often said that extra cores are often not really being put to use, so perhaps allocating resources the way we would like them would be a fun experiment.

If you want to follow along on your own PC, this machine is a six-core, but if you have a different setup – which is not unlikely – then substitute your own core numbers and spread the affinity the way you see fit.

On our machine, Steam was spread right from core 0 to 5, and so was the game that we launched – DiRTRally. To make sure Steam has all it needs, we’ll give it the first two cores: 0 and 1; and the game we’re testing will get the remaining cores: 2-5. With any luck, the two programs will stay out of each other’s way, and gaming performanc­e will be smoother.

To do this, first you will need to launch Steam by terminal with a chosen affinity. Then, from within Steam you will launch a game, temporaril­y leave it, then find its PID and use the PID to re-assign the game’s affinity.

First, launch Steam on cores 0 and 1: $ taskset -c 0,1 steam

With Steam now running, it’s time to launch your game. As mentioned, we’re using DiRTRally, but use any game you like, as long as it is big and processor-hungry. You’ll be starting the game and then finding its PID, but it raises the question: how do you temporaril­y leave the game to enter commands and come back to it?

You could, of course, play in a window, but that doesn’t sound like much fun! One method is risking an “Alt-Tab” out of the game, and hoping that it doesn’t crash. However, if you want to be cool, the showier method is to drop down into a system terminal, run commands from there, and then threefinge­r your way back into X.

So, to drop down to a system terminal, press Ctrl+Alt+F1. Enter your username and password, and again, you simply run top like so: $ top

Your big, hungry game should be around the top of the list – write down its PID on the left (in this case it was 12,144), and exit top with the Q button. Now in order to set DiRTRally to use cores 2 to 5, we’re going to use the following syntax: $ taskset -cp 2-5 12144

However, you could equally use the syntax: $ taskset -cp 2,3,4,5 12144

This may seem redundant, but might be handy if you are going to be repeating the command, and wish to chop out one of the numbers in the middle – for instance “2,3,5”.

Now to get back into your game, re-enter X by pressing Ctrl+Alt+F7, and if that doesn’t work, Ctrl+Alt+F8 – it depends on how your distro is setup.

And yes, to anyone who’s watching, you’re now cool.

 ??  ?? ’top’ will show you the PID and the program’s name. Here, taskset is checking the affinity of Chrome.
’top’ will show you the PID and the program’s name. Here, taskset is checking the affinity of Chrome.
 ??  ??
 ??  ?? The Saboteur is a gorgeous game that will bring a modern PC to its knees. Set the affinity to one core however and it’ll run like new.
The Saboteur is a gorgeous game that will bring a modern PC to its knees. Set the affinity to one core however and it’ll run like new.

Newspapers in English

Newspapers from Australia