Warning: Declaration of action_plugin_include::register(&$controller) should be compatible with DokuWiki_Action_Plugin::register(Doku_Event_Handler $controller) in /home/public/lib/plugins/include/action.php on line 0
Pinning VM CPUs in Proxmox [Jim Chen]

Pinning VM CPUs in Proxmox

Proxmox 4 doesn't seem to support pinning a VM's CPUs to specific host CPUs. It also doesn't support VM startup hooks, so there's no straightforward way to run taskset on the newly created VM. However, when the QEMU process is created, it writes its PID to the file /var/run/qemu-server/$id.pid, where $id is the VM ID. By watching writes to this file, e.g. through inotifywait, it's actually pretty easy to create a startup hook to run taskset or perform any other tasks.

I wrote a systemd service that watches the /var/run/qemu-server directory, and automatically calls taskset on newly created VMs processes based on configuration files.

/etc/systemd/system/autotaskset.service
[Unit]
Description = Auto taskset service
 
[Service]
Type = simple
ExecStart = /bin/bash -c " \
        conf=/etc/autotaskset; \
        dir=/var/run/qemu-server; \
        mkdir -p \"$$dir\"; \
        /usr/bin/inotifywait -mq -e modify --format %%f \"$$dir\" | \
        while read pid; do \
                [ -f \"$$conf\"/\"$$pid\" ] && \
                /usr/bin/taskset $$(< \"$$conf\"/\"$$pid\") $$(< \"$$dir\"/\"$$pid\"); \
        done"
 
[Install]
WantedBy = multi-user.target

To enable the service, install the inotify-tools package, then run systemctl enable autotaskset.

To configure CPU pinning for each VM, create a file /etc/autotaskset/$id.pid, where $id is the VM ID, that contains all arguments to taskset. For example, for a VM with ID 100, if I want to pin the VM's 4 CPUs on the host's every other CPU (to skip the second hyper-threaded CPU on each physical core for example), I would use the following conf file.

/etc/autotaskset/100.pid
-cp 0,2,4,6

The -c option enables specifying the host CPU by its ID, and the -p option is required because we're operating on an existing process given its PID.

Comments

rinseaid
· 2017/04/02 22:00 · reply
Warning: Declaration of syntax_plugin_math::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /home/public/lib/plugins/math/syntax.php on line 0

Warning: Declaration of syntax_plugin_math::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /home/public/lib/plugins/math/syntax.php on line 0

Warning: Declaration of syntax_plugin_pagelist::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /home/public/lib/plugins/pagelist/syntax.php on line 0

Warning: Declaration of syntax_plugin_pagelist::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /home/public/lib/plugins/pagelist/syntax.php on line 0

Warning: Declaration of syntax_plugin_meta::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /home/public/lib/plugins/meta/syntax.php on line 0

Warning: Declaration of syntax_plugin_meta::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /home/public/lib/plugins/meta/syntax.php on line 0

Warning: Declaration of syntax_plugin_blogtng_topic::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /home/public/lib/plugins/blogtng/syntax/topic.php on line 0

Warning: Declaration of syntax_plugin_blogtng_topic::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /home/public/lib/plugins/blogtng/syntax/topic.php on line 0

Warning: Declaration of syntax_plugin_include_footer::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /home/public/lib/plugins/include/syntax/footer.php on line 0

Warning: Declaration of syntax_plugin_include_footer::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /home/public/lib/plugins/include/syntax/footer.php on line 0

Warning: Declaration of syntax_plugin_include_wrap::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /home/public/lib/plugins/include/syntax/wrap.php on line 0

Warning: Declaration of syntax_plugin_include_wrap::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /home/public/lib/plugins/include/syntax/wrap.php on line 0

Warning: Declaration of syntax_plugin_include_editbtn::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /home/public/lib/plugins/include/syntax/editbtn.php on line 0

Warning: Declaration of syntax_plugin_include_editbtn::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /home/public/lib/plugins/include/syntax/editbtn.php on line 0

Warning: Declaration of syntax_plugin_include_locallink::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /home/public/lib/plugins/include/syntax/locallink.php on line 0

Warning: Declaration of syntax_plugin_include_locallink::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /home/public/lib/plugins/include/syntax/locallink.php on line 0

Warning: Declaration of syntax_plugin_include_include::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /home/public/lib/plugins/include/syntax/include.php on line 0

Warning: Declaration of syntax_plugin_include_include::render($format, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /home/public/lib/plugins/include/syntax/include.php on line 0

Warning: Declaration of syntax_plugin_include_readmore::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /home/public/lib/plugins/include/syntax/readmore.php on line 0

Warning: Declaration of syntax_plugin_include_readmore::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /home/public/lib/plugins/include/syntax/readmore.php on line 0

Warning: Declaration of syntax_plugin_include_header::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /home/public/lib/plugins/include/syntax/header.php on line 0

Warning: Declaration of syntax_plugin_include_header::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /home/public/lib/plugins/include/syntax/header.php on line 0

Warning: Declaration of syntax_plugin_include_closelastsecedit::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /home/public/lib/plugins/include/syntax/closelastsecedit.php on line 0

Warning: Declaration of syntax_plugin_include_closelastsecedit::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /home/public/lib/plugins/include/syntax/closelastsecedit.php on line 0

Warning: Declaration of syntax_plugin_include_sorttag::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /home/public/lib/plugins/include/syntax/sorttag.php on line 0

Warning: Declaration of syntax_plugin_include_sorttag::render($mode, &$renderer, $data) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /home/public/lib/plugins/include/syntax/sorttag.php on line 0

Warning: Declaration of renderer_plugin_blogtng_comment::plugin($name, $data) should be compatible with Doku_Renderer::plugin($name, $data, $state = '', $match = '') in /home/public/lib/plugins/blogtng/renderer/comment.php on line 0

This is awesome. Thank you very much!

Chris
· 2017/12/15 07:58 · reply

Cool!

James
· 2018/02/09 08:29 · reply

Thank you for sharing