Назначить отдельное ядро ​​процессу

1601
Leonid

То, что я пытаюсь реализовать, - это запустить скрипт Python на собственном ядре на Raspberry Pi 2 с Debian. Можно использовать, tasksetчтобы связать процесс с конкретным ядром. Но мне нужно, чтобы это ядро ​​было свободным от загрузки ОС и других процессов одновременно. Как мне это сделать?

4
Может быть, также установить приоритет в реальном времени? grawity 7 лет назад 0
@Leonidas Я расширил свой ответ некоторыми тестовыми результатами. Проверьте это. Kamil Maciorowski 7 лет назад 0

2 ответа на вопрос

5
Kamil Maciorowski

This whole thread should be useful.

I would look for a way to limit the 'init' process to running on cpu 1.

In general case the last words should be "running on all CPUs but 1". I guess one can use some kernel parameters; irqaffinity, isolcpus look promising.


EDIT:

About isolcpus in kernel parameters documentation:

This option is the preferred way to isolate CPUs. The alternative -- manually setting the CPU mask of all tasks in the system -- can cause problems and suboptimal load balancer performance.

I did some tests. Results:

  • Kernel parameter isolcpus is the one you may use to limit the init process and isolate CPU(s).

  • It seemed to me that kernel parameter irqaffinity did not affect isolated CPU(s). You probably don't need this parameter.

  • When I isolated CPU0 it didn't go completely idle. Is that system or hardware requirement to do some computation with it despite isolation? – I don't know.

  • When I isolated CPU1 or CPU7 it was solid as idle as a rock.

To isolate CPU1 add isolcpus=1 in your grub.cfg, like this:
linux /vmlinuz-(…all your current parameters here…)isolcpus=1


ORIGINAL ANSWER CONTINUES:

Alternatively, after the system has started, you can re-assign the affinity of all currently running processes, but that seems less elegant.

I agree it seems less elegant, yet it may work good enough (EDIT: it can cause problems and suboptimal load balancer performance, as stated above). There is quick and dirty one-liner that works in my Debian:

sudo bash -c 'cd /proc ; for i in [1-9]* ; do taskset -a -p fffffffe $i ; done' 

I got errors for some pids, nevertheless htop shows that one core is virtually idle after that. Of course you have to run your script later, exclude it somehow or simply reassign:

taskset -a -p 1 PID 

I expect you won't need to repeat said one-liner to cover new processes since CPU affinity is inherited.

3
L. Levrel

With package Cpuset:

# cset shield -c 42 -k on 

to reserve CPU #42 for your task (you can reserve a group of CPUs), then

# cset shield -e your_script 

to start your_script on your reserved CPUs. (See also options --user and --group.)

When you're done, release CPUs ("reset the shield") with:

# cset shield -r 

There is much, much more to this tool. E.g., attaching a previously running process to the reserved CPU:

# cset shield -s -p <PID> --threads 

Command-line help:

# cset help 

Online help/tutorial

Выглядит многообещающе. Можете ли вы расширить свой ответ и отправить команду, чтобы отменить бронирование и вернуться в состояние по умолчанию? Kamil Maciorowski 7 лет назад 0
@KamilMaciorowski: есть ли у вас какие-либо отзывы о командах, которые я добавил по вашему запросу? L. Levrel 7 лет назад 0

Похожие вопросы