<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.openvz.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Vdavydov</id>
	<title>OpenVZ Virtuozzo Containers Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.openvz.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Vdavydov"/>
	<link rel="alternate" type="text/html" href="https://wiki.openvz.org/Special:Contributions/Vdavydov"/>
	<updated>2026-04-11T09:26:19Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.31.1</generator>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Fairsched_API&amp;diff=9672</id>
		<title>Fairsched API</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Fairsched_API&amp;diff=9672"/>
		<updated>2011-01-25T08:46:31Z</updated>

		<summary type="html">&lt;p&gt;Vdavydov: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There are several parameters tuning which the OpenVZ administrator can change the CPU usage of a VE. Usually one sets these parameters with the aid of the &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt; utility. But sometimes it can be useful to modify these parameters directly, without using &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt;. For example, it can be required by developers who want to implement an in-place solution to control a particular virtualization system based on OpenVZ. That is why the Fairsched API is introduced.&lt;br /&gt;
&lt;br /&gt;
The '''Fairsched API''' is the set of system calls that can be used (and that are, in fact, used in the &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt; utility) for altering various parameters of the [[CPU Fair scheduler]], such as &amp;lt;code&amp;gt;cpuunits&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;cpulimit&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;cpus&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;cpumask&amp;lt;/code&amp;gt;. The article is targeted at describing the API.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CPU weight ==&lt;br /&gt;
&lt;br /&gt;
'''Synopsis:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int fairsched_chwt(unsigned int id, unsigned int wght);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Description:'''&lt;br /&gt;
&lt;br /&gt;
The CPU weight defines how much CPU time a VE gets. The smaller the weight is, the more CPU time the container gets. The CPU weight is a relative parameter. That means that multiplying the weight of all running VEs by the same value does not change the CPU time distribution. The minimal value of the weight is 1, and the maximal value is &amp;lt;code&amp;gt;FSCHWEIGHT_MAX&amp;lt;/code&amp;gt; (65535). The weight of a newly created VE is set to 500 by default.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;fairsched_chwt()&amp;lt;/code&amp;gt; system call sets the CPU weight of a VE. &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; is the id of the VE. &amp;lt;code&amp;gt;wght&amp;lt;/code&amp;gt; is the desired weight.&lt;br /&gt;
&lt;br /&gt;
'''Return value:'''&lt;br /&gt;
&lt;br /&gt;
On success, &amp;lt;code&amp;gt;fairsched_chwt()&amp;lt;/code&amp;gt; returns 0. On error, -1 is returned, and &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; is set appropriately.&lt;br /&gt;
&lt;br /&gt;
'''Errors:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: the value of &amp;lt;code&amp;gt;wght&amp;lt;/code&amp;gt; is out of the allowed range.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;: the VE does not exist.&lt;br /&gt;
* &amp;lt;code&amp;gt;EPERM&amp;lt;/code&amp;gt;: the caller does not have permissions to modify the CPU parameters of the VE.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
It is possible to change the CPU weight of the host system. The host system's id equals &amp;lt;code&amp;gt;INT_MAX&amp;lt;/code&amp;gt; (2147483647).&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;cpuunits&amp;lt;/code&amp;gt; option of the &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt; utility is not the same thing as the CPU weight described above, but they are closely connected by the simple equation:&lt;br /&gt;
&lt;br /&gt;
''cpuweight = 500000 / cpuunits''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CPU limit ==&lt;br /&gt;
&lt;br /&gt;
'''Synopsis:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int fairsched_rate(unsigned int id, int op, unsigned int rate);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Description:'''&lt;br /&gt;
&lt;br /&gt;
The CPU limit is the upper bound to the CPU usage for a VE. The limit is set for all CPUs the VE can execute on. That means the maximal value of the CPU limit (i.e. no limit) is the maximal limit on 1 CPU, which equals 1024, multiplied by NUM_CPUS, the number of CPUs this VM can execute on. The CPU limit is an absolute parameter in the sense that altering the limit of one VE does not affect the limits of other VEs.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;fairsched_rate()&amp;lt;/code&amp;gt; system call is used to set, get, or drop (i.e. set to the maximum) CPU limit of a VE. &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; is the id of the VE. &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; can be one of the following:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;FAIRSCHED_SET_RATE&amp;lt;/code&amp;gt; to set the limit; in this case &amp;lt;code&amp;gt;rate&amp;lt;/code&amp;gt; is the limit value to set.&lt;br /&gt;
* &amp;lt;code&amp;gt;FAIRSCHED_DROP_RATE&amp;lt;/code&amp;gt; to drop the limit; &amp;lt;code&amp;gt;rate&amp;lt;/code&amp;gt; is not used.&lt;br /&gt;
* &amp;lt;code&amp;gt;FAIRSCHED_GET_RATE&amp;lt;/code&amp;gt; to get the current value of the limit; the function returns the limit value, &amp;lt;code&amp;gt;rate&amp;lt;/code&amp;gt; is not used.&lt;br /&gt;
&lt;br /&gt;
'''Return value:'''&lt;br /&gt;
&lt;br /&gt;
On success, &amp;lt;code&amp;gt;fairsched_rate()&amp;lt;/code&amp;gt; returns the current value of the CPU limit if &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;FAIRSCHED_SET_RATE&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;FAIRSCHED_GET_RATE&amp;lt;/code&amp;gt;, or 0 if &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;FAIRSCHED_DROP_RATE&amp;lt;/code&amp;gt;. On error, -1 is returned, and &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; is set appropriately.&lt;br /&gt;
&lt;br /&gt;
'''Errors:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; is invalid.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt;=&amp;lt;code&amp;gt;FAIRSCHED_SET_RATE&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;rate&amp;lt;/code&amp;gt;&amp;lt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;: the VE does not exist.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENODATA&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt;=&amp;lt;code&amp;gt;FAIRSCHED_GET_RATE&amp;lt;/code&amp;gt;, and the limit is not set (or was dropped).&lt;br /&gt;
* &amp;lt;code&amp;gt;EPERM&amp;lt;/code&amp;gt;: the caller does not have permissions to modify the CPU parameters of the VE.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
It is possible to limit the CPU usage of the host system. The host system's id equals &amp;lt;code&amp;gt;INT_MAX&amp;lt;/code&amp;gt; (2147483647).&lt;br /&gt;
&lt;br /&gt;
CPU limits are available only in rhel5-based and rhel6-based kernels, and they behave a bit differently in them in respect of distributing CPU time among CPUs the VE can execute on.&lt;br /&gt;
&lt;br /&gt;
In the rhel5 kernel the limit has a container-wide meaning. For example, if there are 2 CPUs available for a container, and the limit is set to 1024 (of 2048), the container's CPU usage can be 100/0%, or 50/50%, or any other values whose sum does not exceed 100%.&lt;br /&gt;
&lt;br /&gt;
In the rhel6 kernel the limit is divided between the CPUs proportionally, and a busy CPU cannot borrow time from an idle one, i.e. with a 2 CPUs container and 1024 (of 2048) limit set the usage of each CPU cannot exceed 50% in any case.&lt;br /&gt;
&lt;br /&gt;
As a result, the minimal value for the limit is 1 in the rhel5 kernel and 1 * NUM_CPUS in the rhel6 kernel. If the given limit is out of the allowed range it is automatically clamped to the range.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Number of CPUs ==&lt;br /&gt;
&lt;br /&gt;
'''Synopsis:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int fairsched_vcpus(unsigned int id, unsigned int vcpus);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Description:'''&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;fairsched_vcpus()&amp;lt;/code&amp;gt; system call sets the number of virtual CPUs available in a VE. &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; is the id of the VE. &amp;lt;code&amp;gt;vcpus&amp;lt;/code&amp;gt; is the number of virtual CPUs to set for the VE. If &amp;lt;code&amp;gt;vcpus&amp;lt;/code&amp;gt;=0, the call attempts to set the number of CPUs in the VE to the number of online CPUs.&lt;br /&gt;
&lt;br /&gt;
'''Return value:'''&lt;br /&gt;
&lt;br /&gt;
On success, &amp;lt;code&amp;gt;fairsched_vcpus()&amp;lt;/code&amp;gt; returns 0. On error, -1 is returned, and &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; is set appropriately.&lt;br /&gt;
&lt;br /&gt;
'''Errors:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;: the VE does not exist.&lt;br /&gt;
* &amp;lt;code&amp;gt;EPERM&amp;lt;/code&amp;gt;: the caller does not have permissions to modify the CPU parameters of the VE.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
The parameter have not been implemented in the rhel6 kernel yet.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CPU affinity mask ==&lt;br /&gt;
&lt;br /&gt;
'''Synopsis:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int fairsched_cpumask(unsigned int id, unsigned int len, unsigned long *mask)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Description:'''&lt;br /&gt;
&lt;br /&gt;
A VE's CPU affinity mask determines the set of CPUs on which it is eligible to run. A CPU affinity mask is represented by an array of &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt;'s so that a VE is allowed to execute on the n-th CPU if and only if the (&amp;lt;code&amp;gt;n%sizeof(long)&amp;lt;/code&amp;gt;)-th bit of the (&amp;lt;code&amp;gt;n/sizeof(long)&amp;lt;/code&amp;gt;)-th element of the array is set.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;fairsched_cpumask()&amp;lt;/code&amp;gt; system call sets the CPU affinity mask for a VE. &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; is the id of the VE. &amp;lt;code&amp;gt;mask&amp;lt;/code&amp;gt; is the mask, and &amp;lt;code&amp;gt;len&amp;lt;/code&amp;gt; is the size of the mask, in bytes.&lt;br /&gt;
&lt;br /&gt;
'''Return value:'''&lt;br /&gt;
&lt;br /&gt;
On success, &amp;lt;code&amp;gt;fairsched_cpumask()&amp;lt;/code&amp;gt; returns 0. On error, -1 is returned, and &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; is set appropriately.&lt;br /&gt;
&lt;br /&gt;
'''Errors:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EBUSY&amp;lt;/code&amp;gt;: the VE has a child cpuset which is not a subset of the mask.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: the mask intersects with a cpuset, and either that cpuset or the VE's cpuset is marked exclusive.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;: the VE does not exist.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOMEM&amp;lt;/code&amp;gt;: there is no memory to copy the mask.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOSPC&amp;lt;/code&amp;gt;: the mask has no active CPUs set.&lt;br /&gt;
* &amp;lt;code&amp;gt;EPERM&amp;lt;/code&amp;gt;: the caller does not have permissions to modify the CPU parameters of the VE.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
The parameter is available since the rhel6 kernel.&lt;/div&gt;</summary>
		<author><name>Vdavydov</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Fairsched_API&amp;diff=9671</id>
		<title>Fairsched API</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Fairsched_API&amp;diff=9671"/>
		<updated>2011-01-25T08:43:43Z</updated>

		<summary type="html">&lt;p&gt;Vdavydov: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There are several parameters tuning which the OpenVZ administrator can change the CPU usage of a VE. Usually one sets these parameters with the aid of the &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt; utility. But sometimes it can be useful to modify these parameters directly, without using &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt;. For example, it can be required by developers who want to implement an in-place solution to control a particular virtualization system based on OpenVZ. That is why the Fairsched API is introduced.&lt;br /&gt;
&lt;br /&gt;
The '''Fairsched API''' is the set of system calls that can be used (and that are, in fact, used in the &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt; utility) for altering various parameters of the [[CPU Fair scheduler]], such as &amp;lt;code&amp;gt;cpuunits&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;cpulimit&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;cpus&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;cpumask&amp;lt;/code&amp;gt;. The article is targeted at describing the API.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CPU weight ==&lt;br /&gt;
&lt;br /&gt;
'''Synopsis:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int fairsched_chwt(unsigned int id, unsigned int wght);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Description:'''&lt;br /&gt;
&lt;br /&gt;
The CPU weight defines how much CPU time a VE gets. The smaller the weight is, the more CPU time the container gets. The CPU weight is a relative parameter. That means that multiplying the weight of all running VEs by the same value does not change the CPU time distribution. The minimal value of the weight is 1, and the maximal value is &amp;lt;code&amp;gt;FSCHWEIGHT_MAX&amp;lt;/code&amp;gt; (65535). The weight of a newly created VE is set to 500 by default.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;fairsched_chwt()&amp;lt;/code&amp;gt; system call sets the CPU weight of a VE. &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; is the id of the VE. &amp;lt;code&amp;gt;wght&amp;lt;/code&amp;gt; is the desired weight.&lt;br /&gt;
&lt;br /&gt;
'''Return value:'''&lt;br /&gt;
&lt;br /&gt;
On success, &amp;lt;code&amp;gt;fairsched_chwt()&amp;lt;/code&amp;gt; returns 0. On error, -1 is returned, and &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; is set appropriately.&lt;br /&gt;
&lt;br /&gt;
'''Errors:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: the value of &amp;lt;code&amp;gt;wght&amp;lt;/code&amp;gt; is out of the allowed range.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;: the VE does not exist.&lt;br /&gt;
* &amp;lt;code&amp;gt;EPERM&amp;lt;/code&amp;gt;: the caller does not have permissions to modify the CPU parameters of the VE.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
It is possible to change the CPU weight of the host system. The host system's id equals &amp;lt;code&amp;gt;INT_MAX&amp;lt;/code&amp;gt; (2147483647).&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;cpuunits&amp;lt;/code&amp;gt; option of the &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt; utility is not the same thing as the CPU weight described above, but they are closely connected by the simple equation:&lt;br /&gt;
&lt;br /&gt;
''cpuweight = 500000 / cpuunits''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CPU limit ==&lt;br /&gt;
&lt;br /&gt;
'''Synopsis:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int fairsched_rate(unsigned int id, int op, unsigned int rate);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Description:'''&lt;br /&gt;
&lt;br /&gt;
The CPU limit is the upper bound to the CPU usage for a VE. The limit is set for all CPUs the VE can execute on. That means the maximal value of the CPU limit (i.e. no limit) is the maximal limit on 1 CPU, which equals 1024, multiplied by NUM_CPUS, the number of CPUs this VM can execute on. The CPU limit is an absolute parameter in the sense that altering the limit of one VE does not affect the limits of other VEs.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;fairsched_rate()&amp;lt;/code&amp;gt; system call is used to set, get, or drop (i.e. set to the maximum) CPU limit of a VE. &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; is the id of the VE. &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; can be one of the following:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;FAIRSCHED_SET_RATE&amp;lt;/code&amp;gt; to set the limit; in this case &amp;lt;code&amp;gt;rate&amp;lt;/code&amp;gt; is the limit value to set.&lt;br /&gt;
* &amp;lt;code&amp;gt;FAIRSCHED_DROP_RATE&amp;lt;/code&amp;gt; to drop the limit; &amp;lt;code&amp;gt;rate&amp;lt;/code&amp;gt; is not used.&lt;br /&gt;
* &amp;lt;code&amp;gt;FAIRSCHED_GET_RATE&amp;lt;/code&amp;gt; to get the current value of the limit; the function returns the limit value, &amp;lt;code&amp;gt;rate&amp;lt;/code&amp;gt; is not used.&lt;br /&gt;
&lt;br /&gt;
'''Return value:'''&lt;br /&gt;
&lt;br /&gt;
On success, &amp;lt;code&amp;gt;fairsched_rate()&amp;lt;/code&amp;gt; returns the current value of the CPU limit if &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;FAIRSCHED_SET_RATE&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;FAIRSCHED_GET_RATE&amp;lt;/code&amp;gt;, or 0 if &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;FAIRSCHED_DROP_RATE&amp;lt;/code&amp;gt;. On error, -1 is returned, and &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; is set appropriately.&lt;br /&gt;
&lt;br /&gt;
'''Errors:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; is invalid.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt;=&amp;lt;code&amp;gt;FAIRSCHED_SET_RATE&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;rate&amp;lt;/code&amp;gt;&amp;lt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;: the VE does not exist.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENODATA&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt;=&amp;lt;code&amp;gt;FAIRSCHED_GET_RATE&amp;lt;/code&amp;gt;, and the limit is not set (or was dropped).&lt;br /&gt;
* &amp;lt;code&amp;gt;EPERM&amp;lt;/code&amp;gt;: the caller does not have permissions to modify the CPU parameters of the VE.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
It is possible to limit the CPU usage of the host system. The host system's id equals &amp;lt;code&amp;gt;INT_MAX&amp;lt;/code&amp;gt; (2147483647).&lt;br /&gt;
&lt;br /&gt;
CPU limits are available only in rhel5-based and rhel6-based kernels, and they behave a bit differently in them in respect of distributing the CPU time among CPUs the VE can execute on.&lt;br /&gt;
&lt;br /&gt;
In the rhel5 kernel the limit has a container-wide meaning. For example, if there are 2 CPUs available for a container, and the limit is set to 1024 (of 2048), the container's CPU usage can be 100/0%, or 50/50%, or any other values whose sum does not exceed 100%.&lt;br /&gt;
&lt;br /&gt;
In the rhel6 kernel the limit is divided between the CPUs proportionally, and a busy CPU cannot borrow time from an idle one, i.e. with a 2 CPUs container and 1024 (of 2048) limit set the usage of each CPU cannot exceed 50% in any case.&lt;br /&gt;
&lt;br /&gt;
As a result, the minimal value for the limit is 1 in the rhel5 kernel and 1 * NUM_CPUS in the rhel6 kernel. If the limit value is out of the allowed range it is automatically clamped to the range.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Number of CPUs ==&lt;br /&gt;
&lt;br /&gt;
'''Synopsis:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int fairsched_vcpus(unsigned int id, unsigned int vcpus);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Description:'''&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;fairsched_vcpus()&amp;lt;/code&amp;gt; system call sets the number of virtual CPUs available in a VE. &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; is the id of the VE. &amp;lt;code&amp;gt;vcpus&amp;lt;/code&amp;gt; is the number of virtual CPUs to set for the VE. If &amp;lt;code&amp;gt;vcpus&amp;lt;/code&amp;gt;=0, the call attempts to set the number of CPUs in the VE to the number of online CPUs.&lt;br /&gt;
&lt;br /&gt;
'''Return value:'''&lt;br /&gt;
&lt;br /&gt;
On success, &amp;lt;code&amp;gt;fairsched_vcpus()&amp;lt;/code&amp;gt; returns 0. On error, -1 is returned, and &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; is set appropriately.&lt;br /&gt;
&lt;br /&gt;
'''Errors:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;: the VE does not exist.&lt;br /&gt;
* &amp;lt;code&amp;gt;EPERM&amp;lt;/code&amp;gt;: the caller does not have permissions to modify the CPU parameters of the VE.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
The parameter have not been implemented in the rhel6 kernel yet.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CPU affinity mask ==&lt;br /&gt;
&lt;br /&gt;
'''Synopsis:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int fairsched_cpumask(unsigned int id, unsigned int len, unsigned long *mask)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Description:'''&lt;br /&gt;
&lt;br /&gt;
A VE's CPU affinity mask determines the set of CPUs on which it is eligible to run. A CPU affinity mask is represented by an array of &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt;'s so that a VE is allowed to execute on the n-th CPU if and only if the (&amp;lt;code&amp;gt;n%sizeof(long)&amp;lt;/code&amp;gt;)-th bit of the (&amp;lt;code&amp;gt;n/sizeof(long)&amp;lt;/code&amp;gt;)-th element of the array is set.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;fairsched_cpumask()&amp;lt;/code&amp;gt; system call sets the CPU affinity mask for a VE. &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; is the id of the VE. &amp;lt;code&amp;gt;mask&amp;lt;/code&amp;gt; is the mask, and &amp;lt;code&amp;gt;len&amp;lt;/code&amp;gt; is the size of the mask, in bytes.&lt;br /&gt;
&lt;br /&gt;
'''Return value:'''&lt;br /&gt;
&lt;br /&gt;
On success, &amp;lt;code&amp;gt;fairsched_cpumask()&amp;lt;/code&amp;gt; returns 0. On error, -1 is returned, and &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; is set appropriately.&lt;br /&gt;
&lt;br /&gt;
'''Errors:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EBUSY&amp;lt;/code&amp;gt;: the VE has a child cpuset which is not a subset of the mask.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: the mask intersects with a cpuset, and either that cpuset or the VE's cpuset is marked exclusive.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;: the VE does not exist.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOMEM&amp;lt;/code&amp;gt;: there is no memory to copy the mask.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOSPC&amp;lt;/code&amp;gt;: the mask has no active CPUs set.&lt;br /&gt;
* &amp;lt;code&amp;gt;EPERM&amp;lt;/code&amp;gt;: the caller does not have permissions to modify the CPU parameters of the VE.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
The parameter is available since the rhel6 kernel.&lt;/div&gt;</summary>
		<author><name>Vdavydov</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Fairsched_API&amp;diff=9670</id>
		<title>Fairsched API</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Fairsched_API&amp;diff=9670"/>
		<updated>2011-01-25T08:34:54Z</updated>

		<summary type="html">&lt;p&gt;Vdavydov: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There are several parameters tuning which the OpenVZ administrator can change the CPU usage of a VE. Usually one sets these parameters with the aid of the &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt; utility. But sometimes it can be useful to modify these parameters directly, without using &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt;. For example, it can be required by developers who want to implement an in-place solution to control a particular virtualization system based on OpenVZ. That is why the Fairsched API is introduced.&lt;br /&gt;
&lt;br /&gt;
The '''Fairsched API''' is the set of system calls that can be used (and that are, in fact, used in the &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt; utility) for altering various parameters of the [[CPU Fair scheduler]], such as &amp;lt;code&amp;gt;cpuunits&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;cpulimit&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;cpus&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;cpumask&amp;lt;/code&amp;gt;. The article is targeted at describing the API.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CPU weight ==&lt;br /&gt;
&lt;br /&gt;
'''Synopsis:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int fairsched_chwt(unsigned int id, unsigned int wght);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Description:'''&lt;br /&gt;
&lt;br /&gt;
The CPU weight defines how much CPU time a VE gets. The smaller the weight is, the more CPU time the container gets. The CPU weight is a relative parameter. That means that multiplying the weight of all running VEs by the same value does not change the CPU time distribution. The minimal value of the weight is 1, and the maximal value is &amp;lt;code&amp;gt;FSCHWEIGHT_MAX&amp;lt;/code&amp;gt; (65535). The weight of a newly created VE is set to 500 by default.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;fairsched_chwt()&amp;lt;/code&amp;gt; system call sets the CPU weight of a VE. &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; is the id of the VE. &amp;lt;code&amp;gt;wght&amp;lt;/code&amp;gt; is the desired weight.&lt;br /&gt;
&lt;br /&gt;
'''Return value:'''&lt;br /&gt;
&lt;br /&gt;
On success, &amp;lt;code&amp;gt;fairsched_chwt()&amp;lt;/code&amp;gt; returns 0. On error, -1 is returned, and &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; is set appropriately.&lt;br /&gt;
&lt;br /&gt;
'''Errors:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: the value of &amp;lt;code&amp;gt;wght&amp;lt;/code&amp;gt; is out of the allowed range.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;: the VE does not exist.&lt;br /&gt;
* &amp;lt;code&amp;gt;EPERM&amp;lt;/code&amp;gt;: the caller does not have permissions to modify the CPU parameters of the VE.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
It is possible to change the CPU weight of the host system. The host system's id equals &amp;lt;code&amp;gt;INT_MAX&amp;lt;/code&amp;gt; (2147483647).&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;cpuunits&amp;lt;/code&amp;gt; option of the &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt; utility is not the same thing as the CPU weight described above, but they are closely connected by the simple equation:&lt;br /&gt;
&lt;br /&gt;
''cpuweight = 500000 / cpuunits''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CPU limit ==&lt;br /&gt;
&lt;br /&gt;
'''Synopsis:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int fairsched_rate(unsigned int id, int op, unsigned int rate);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Description:'''&lt;br /&gt;
&lt;br /&gt;
The CPU limit is the upper bound to the CPU usage for a VE. The limit is set for all CPUs the VE can execute on. That means the maximal value of the CPU limit (i.e. no limit) is the maximal limit on 1 CPU, which equals 1024, multiplied by the number of CPUs this VM can execute on. The minimal value is 1. The CPU limit is an absolute parameter in the sense that altering the limit of one VE does not affect the limits of other VEs.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;fairsched_rate()&amp;lt;/code&amp;gt; system call is used to set, get, or drop (i.e. set to the maximum) CPU limit of a VE. &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; is the id of the VE. &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; can be one of the following:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;FAIRSCHED_SET_RATE&amp;lt;/code&amp;gt; to set the limit; in this case &amp;lt;code&amp;gt;rate&amp;lt;/code&amp;gt; is the limit value to set.&lt;br /&gt;
* &amp;lt;code&amp;gt;FAIRSCHED_DROP_RATE&amp;lt;/code&amp;gt; to drop the limit; &amp;lt;code&amp;gt;rate&amp;lt;/code&amp;gt; is not used.&lt;br /&gt;
* &amp;lt;code&amp;gt;FAIRSCHED_GET_RATE&amp;lt;/code&amp;gt; to get the current value of the limit; the function returns the limit value, &amp;lt;code&amp;gt;rate&amp;lt;/code&amp;gt; is not used.&lt;br /&gt;
&lt;br /&gt;
'''Return value:'''&lt;br /&gt;
&lt;br /&gt;
On success, &amp;lt;code&amp;gt;fairsched_rate()&amp;lt;/code&amp;gt; returns the current value of the CPU limit if &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;FAIRSCHED_SET_RATE&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;FAIRSCHED_GET_RATE&amp;lt;/code&amp;gt;, or 0 if &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;FAIRSCHED_DROP_RATE&amp;lt;/code&amp;gt;. On error, -1 is returned, and &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; is set appropriately.&lt;br /&gt;
&lt;br /&gt;
'''Errors:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; is invalid.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt;=&amp;lt;code&amp;gt;FAIRSCHED_SET_RATE&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;rate&amp;lt;/code&amp;gt;&amp;lt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;: the VE does not exist.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENODATA&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt;=&amp;lt;code&amp;gt;FAIRSCHED_GET_RATE&amp;lt;/code&amp;gt;, and the limit is not set (or was dropped).&lt;br /&gt;
* &amp;lt;code&amp;gt;EPERM&amp;lt;/code&amp;gt;: the caller does not have permissions to modify the CPU parameters of the VE.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
It is possible to limit the CPU usage of the host system. The host system's id equals &amp;lt;code&amp;gt;INT_MAX&amp;lt;/code&amp;gt; (2147483647).&lt;br /&gt;
&lt;br /&gt;
CPU limits are available only in rhel5-based and rhel6-based kernels, and they behave a bit differently in them in respect of distributing the CPU time among CPUs the VE can execute on.&lt;br /&gt;
&lt;br /&gt;
In the rhel5 kernel the limit has a container-wide meaning. For example, if there are 2 CPUs available for a container, and the limit is set to 1024 (of 2048), the container's CPU usage can be 100/0%, or 50/50%, or any other values whose sum does not exceed 100%.&lt;br /&gt;
&lt;br /&gt;
In the rhel6 kernel the limit is divided between the CPUs proportionally, and a busy CPU cannot borrow time from an idle one, i.e. with a 2 CPUs container and 1024 (of 2048) limit set the usage of each CPU cannot exceed 50% in any case.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Number of CPUs ==&lt;br /&gt;
&lt;br /&gt;
'''Synopsis:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int fairsched_vcpus(unsigned int id, unsigned int vcpus);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Description:'''&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;fairsched_vcpus()&amp;lt;/code&amp;gt; system call sets the number of virtual CPUs available in a VE. &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; is the id of the VE. &amp;lt;code&amp;gt;vcpus&amp;lt;/code&amp;gt; is the number of virtual CPUs to set for the VE. If &amp;lt;code&amp;gt;vcpus&amp;lt;/code&amp;gt;=0, the call attempts to set the number of CPUs in the VE to the number of online CPUs.&lt;br /&gt;
&lt;br /&gt;
'''Return value:'''&lt;br /&gt;
&lt;br /&gt;
On success, &amp;lt;code&amp;gt;fairsched_vcpus()&amp;lt;/code&amp;gt; returns 0. On error, -1 is returned, and &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; is set appropriately.&lt;br /&gt;
&lt;br /&gt;
'''Errors:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;: the VE does not exist.&lt;br /&gt;
* &amp;lt;code&amp;gt;EPERM&amp;lt;/code&amp;gt;: the caller does not have permissions to modify the CPU parameters of the VE.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
The parameter have not been implemented in the rhel6 kernel yet.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CPU affinity mask ==&lt;br /&gt;
&lt;br /&gt;
'''Synopsis:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int fairsched_cpumask(unsigned int id, unsigned int len, unsigned long *mask)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Description:'''&lt;br /&gt;
&lt;br /&gt;
A VE's CPU affinity mask determines the set of CPUs on which it is eligible to run. A CPU affinity mask is represented by an array of &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt;'s so that a VE is allowed to execute on the n-th CPU if and only if the (&amp;lt;code&amp;gt;n%sizeof(long)&amp;lt;/code&amp;gt;)-th bit of the (&amp;lt;code&amp;gt;n/sizeof(long)&amp;lt;/code&amp;gt;)-th element of the array is set.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;fairsched_cpumask()&amp;lt;/code&amp;gt; system call sets the CPU affinity mask for a VE. &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; is the id of the VE. &amp;lt;code&amp;gt;mask&amp;lt;/code&amp;gt; is the mask, and &amp;lt;code&amp;gt;len&amp;lt;/code&amp;gt; is the size of the mask, in bytes.&lt;br /&gt;
&lt;br /&gt;
'''Return value:'''&lt;br /&gt;
&lt;br /&gt;
On success, &amp;lt;code&amp;gt;fairsched_cpumask()&amp;lt;/code&amp;gt; returns 0. On error, -1 is returned, and &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; is set appropriately.&lt;br /&gt;
&lt;br /&gt;
'''Errors:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EBUSY&amp;lt;/code&amp;gt;: the VE has a child cpuset which is not a subset of the mask.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: the mask intersects with a cpuset, and either that cpuset or the VE's cpuset is marked exclusive.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;: the VE does not exist.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOMEM&amp;lt;/code&amp;gt;: there is no memory to copy the mask.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOSPC&amp;lt;/code&amp;gt;: the mask has no active CPUs set.&lt;br /&gt;
* &amp;lt;code&amp;gt;EPERM&amp;lt;/code&amp;gt;: the caller does not have permissions to modify the CPU parameters of the VE.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
The parameter is available since the rhel6 kernel.&lt;/div&gt;</summary>
		<author><name>Vdavydov</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Fairsched_API&amp;diff=9668</id>
		<title>Fairsched API</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Fairsched_API&amp;diff=9668"/>
		<updated>2011-01-25T07:47:38Z</updated>

		<summary type="html">&lt;p&gt;Vdavydov: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There are several parameters tuning which the OpenVZ administrator can change the CPU usage of a VE. Usually one sets these parameters with the aid of the &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt; utility. But sometimes it can be useful to modify these parameters directly, without using &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt;. For example, it can be required by developers who want to implement an in-place solution to control a particular virtualization system based on OpenVZ. That is why the Fairsched API is introduced.&lt;br /&gt;
&lt;br /&gt;
The '''Fairsched API''' is the set of system calls that can be used (and that are, in fact, used in the &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt; utility) for altering various parameters of the [[CPU Fair scheduler]], such as &amp;lt;code&amp;gt;cpuunits&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;cpulimit&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;cpus&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;cpumask&amp;lt;/code&amp;gt;. The article is targeted at describing the API.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CPU weight ==&lt;br /&gt;
&lt;br /&gt;
'''Synopsis:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int fairsched_chwt(unsigned int id, unsigned int wght);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Description:'''&lt;br /&gt;
&lt;br /&gt;
The CPU weight defines how much CPU time a VE gets. The smaller the weight is, the more CPU time the container gets. The CPU weight is a relative parameter. That means that multiplying the weight of all running VEs by the same value does not change the CPU time distribution. The minimal value of the weight is 1, and the maximal value is &amp;lt;code&amp;gt;FSCHWEIGHT_MAX&amp;lt;/code&amp;gt; (65535). The weight of a newly created VE is set to 500 by default.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;fairsched_chwt()&amp;lt;/code&amp;gt; system call sets the CPU weight of a VE. &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; is the id of the VE. &amp;lt;code&amp;gt;wght&amp;lt;/code&amp;gt; is the desired weight.&lt;br /&gt;
&lt;br /&gt;
'''Return value:'''&lt;br /&gt;
&lt;br /&gt;
On success, &amp;lt;code&amp;gt;fairsched_chwt()&amp;lt;/code&amp;gt; returns 0. On error, -1 is returned, and &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; is set appropriately.&lt;br /&gt;
&lt;br /&gt;
'''Errors:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: the value of &amp;lt;code&amp;gt;wght&amp;lt;/code&amp;gt; is out of the allowed range.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;: the VE does not exist.&lt;br /&gt;
* &amp;lt;code&amp;gt;EPERM&amp;lt;/code&amp;gt;: the caller does not have permissions to modify the CPU parameters of the VE.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
It is possible to change the CPU weight of the host system. The host system's id equals &amp;lt;code&amp;gt;INT_MAX&amp;lt;/code&amp;gt; (2147483647).&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;cpuunits&amp;lt;/code&amp;gt; option of the &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt; utility is not the same thing as the CPU weight described above, but they are closely connected by the simple equation:&lt;br /&gt;
&lt;br /&gt;
''cpuweight = 500000 / cpuunits''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CPU limit ==&lt;br /&gt;
&lt;br /&gt;
'''Synopsis:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int fairsched_rate(unsigned int id, int op, unsigned int rate);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Description:'''&lt;br /&gt;
&lt;br /&gt;
The CPU limit is the upper bound to the CPU usage for a VE. The limit is set for all CPUs the VE can execute on. That means the maximal value of the CPU limit (i.e. no limit) is the maximal limit on 1 CPU, which equals 1024, multiplied by the number of CPUs this VM can execute on. The minimal value is 1. The CPU limit is an absolute parameter in the sense that altering the limit of one VE does not affect the limits of other VEs.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;fairsched_rate()&amp;lt;/code&amp;gt; system call is used to set, get, or drop (i.e. set to the maximum) CPU limit of a VE. &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; is the id of the VE. &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; can be one of the following:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;FAIRSCHED_SET_RATE&amp;lt;/code&amp;gt; to set the limit; in this case &amp;lt;code&amp;gt;rate&amp;lt;/code&amp;gt; is the limit value to set.&lt;br /&gt;
* &amp;lt;code&amp;gt;FAIRSCHED_DROP_RATE&amp;lt;/code&amp;gt; to drop the limit; &amp;lt;code&amp;gt;rate&amp;lt;/code&amp;gt; is not used.&lt;br /&gt;
* &amp;lt;code&amp;gt;FAIRSCHED_GET_RATE&amp;lt;/code&amp;gt; to get the current value of the limit; the function returns the limit value, &amp;lt;code&amp;gt;rate&amp;lt;/code&amp;gt; is not used.&lt;br /&gt;
&lt;br /&gt;
'''Return value:'''&lt;br /&gt;
&lt;br /&gt;
On success, &amp;lt;code&amp;gt;fairsched_rate()&amp;lt;/code&amp;gt; returns the current value of the CPU limit if &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;FAIRSCHED_SET_RATE&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;FAIRSCHED_GET_RATE&amp;lt;/code&amp;gt;, or 0 if &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;FAIRSCHED_DROP_RATE&amp;lt;/code&amp;gt;. On error, -1 is returned, and &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; is set appropriately.&lt;br /&gt;
&lt;br /&gt;
'''Errors:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; is invalid.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt;=&amp;lt;code&amp;gt;FAIRSCHED_SET_RATE&amp;lt;/code&amp;gt;, and the value of &amp;lt;code&amp;gt;rate&amp;lt;/code&amp;gt; is out of the allowed range.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;: the VE does not exist.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENODATA&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt;=&amp;lt;code&amp;gt;FAIRSCHED_GET_RATE&amp;lt;/code&amp;gt;, and the limit is not set (or was dropped).&lt;br /&gt;
* &amp;lt;code&amp;gt;EPERM&amp;lt;/code&amp;gt;: the caller does not have permissions to modify the CPU parameters of the VE.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
It is possible to limit the CPU usage of the host system. The host system's id equals &amp;lt;code&amp;gt;INT_MAX&amp;lt;/code&amp;gt; (2147483647).&lt;br /&gt;
&lt;br /&gt;
CPU limits are available only in rhel5-based and rhel6-based kernels, and they behave a bit differently in them in respect of distributing the CPU time among CPUs the VE can execute on.&lt;br /&gt;
&lt;br /&gt;
In the rhel5 kernel the limit has a container-wide meaning. For example, if there are 2 CPUs available for a container, and the limit is set to 1024 (of 2048), the container's CPU usage can be 100/0%, or 50/50%, or any other values whose sum does not exceed 100%.&lt;br /&gt;
&lt;br /&gt;
In the rhel6 kernel the limit is divided between the CPUs proportionally, and a busy CPU cannot borrow time from an idle one, i.e. with a 2 CPUs container and 1024 (of 2048) limit set the usage of each CPU cannot exceed 50% in any case.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Number of CPUs ==&lt;br /&gt;
&lt;br /&gt;
'''Synopsis:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int fairsched_vcpus(unsigned int id, unsigned int vcpus);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Description:'''&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;fairsched_vcpus()&amp;lt;/code&amp;gt; system call sets the number of virtual CPUs available in a VE. &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; is the id of the VE. &amp;lt;code&amp;gt;vcpus&amp;lt;/code&amp;gt; is the number of virtual CPUs to set for the VE. If &amp;lt;code&amp;gt;vcpus&amp;lt;/code&amp;gt;=0, the call attempts to set the number of CPUs in the VE to the number of online CPUs.&lt;br /&gt;
&lt;br /&gt;
'''Return value:'''&lt;br /&gt;
&lt;br /&gt;
On success, &amp;lt;code&amp;gt;fairsched_vcpus()&amp;lt;/code&amp;gt; returns 0. On error, -1 is returned, and &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; is set appropriately.&lt;br /&gt;
&lt;br /&gt;
'''Errors:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;: the VE does not exist.&lt;br /&gt;
* &amp;lt;code&amp;gt;EPERM&amp;lt;/code&amp;gt;: the caller does not have permissions to modify the CPU parameters of the VE.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
The parameter have not been implemented in the rhel6 kernel yet.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CPU affinity mask ==&lt;br /&gt;
&lt;br /&gt;
'''Synopsis:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int fairsched_cpumask(unsigned int id, unsigned int len, unsigned long *mask)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Description:'''&lt;br /&gt;
&lt;br /&gt;
A VE's CPU affinity mask determines the set of CPUs on which it is eligible to run. A CPU affinity mask is represented by an array of &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt;'s so that a VE is allowed to execute on the n-th CPU if and only if the (&amp;lt;code&amp;gt;n%sizeof(long)&amp;lt;/code&amp;gt;)-th bit of the (&amp;lt;code&amp;gt;n/sizeof(long)&amp;lt;/code&amp;gt;)-th element of the array is set.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;fairsched_cpumask()&amp;lt;/code&amp;gt; system call sets the CPU affinity mask for a VE. &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; is the id of the VE. &amp;lt;code&amp;gt;mask&amp;lt;/code&amp;gt; is the mask, and &amp;lt;code&amp;gt;len&amp;lt;/code&amp;gt; is the size of the mask, in bytes.&lt;br /&gt;
&lt;br /&gt;
'''Return value:'''&lt;br /&gt;
&lt;br /&gt;
On success, &amp;lt;code&amp;gt;fairsched_cpumask()&amp;lt;/code&amp;gt; returns 0. On error, -1 is returned, and &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; is set appropriately.&lt;br /&gt;
&lt;br /&gt;
'''Errors:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EBUSY&amp;lt;/code&amp;gt;: the VE has a child cpuset which is not a subset of the mask.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: the mask intersects with a cpuset, and either that cpuset or the VE's cpuset is marked exclusive.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;: the VE does not exist.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOMEM&amp;lt;/code&amp;gt;: there is no memory to copy the mask.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOSPC&amp;lt;/code&amp;gt;: the mask has no active CPUs set.&lt;br /&gt;
* &amp;lt;code&amp;gt;EPERM&amp;lt;/code&amp;gt;: the caller does not have permissions to modify the CPU parameters of the VE.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
The parameter is available since the rhel6 kernel.&lt;/div&gt;</summary>
		<author><name>Vdavydov</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Fairsched_API&amp;diff=9667</id>
		<title>Fairsched API</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Fairsched_API&amp;diff=9667"/>
		<updated>2011-01-25T06:25:45Z</updated>

		<summary type="html">&lt;p&gt;Vdavydov: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There are several parameters tuning which the OpenVZ administrator can change the CPU usage of a VE. Usually one sets these parameters with the aid of the &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt; utility. But sometimes it can be useful to modify these parameters directly, without using &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt;. For example, it can be required by developers who want to implement an in-place solution to control a particular virtualization system based on OpenVZ. That is why the Fairsched API is introduced.&lt;br /&gt;
&lt;br /&gt;
The '''Fairsched API''' is the set of system calls that can be used (and that are, in fact, used in the &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt; utility) for altering various parameters of the [[CPU Fair scheduler]], such as &amp;lt;code&amp;gt;cpuunits&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;cpulimit&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;cpus&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;cpumask&amp;lt;/code&amp;gt;. The article is targeted at describing the API.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CPU weight ==&lt;br /&gt;
&lt;br /&gt;
'''Synopsis:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int fairsched_chwt(unsigned int id, unsigned int wght);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Description:'''&lt;br /&gt;
&lt;br /&gt;
The CPU weight defines how much CPU time a VE gets. The smaller the weight is, the more CPU time the container gets. The CPU weight is a relative parameter. That means that multiplying the weight of all running VEs by the same value does not change the CPU time distribution. The minimal value of the weight is 1, and the maximal value is &amp;lt;code&amp;gt;FSCHWEIGHT_MAX&amp;lt;/code&amp;gt; (65535). The weight of a newly created VE is set to 500 by default.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;fairsched_chwt()&amp;lt;/code&amp;gt; system call sets the CPU weight of a VE. &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; is the id of the VE. &amp;lt;code&amp;gt;wght&amp;lt;/code&amp;gt; is the desired weight.&lt;br /&gt;
&lt;br /&gt;
'''Return value:'''&lt;br /&gt;
&lt;br /&gt;
On success, &amp;lt;code&amp;gt;fairsched_chwt()&amp;lt;/code&amp;gt; returns 0. On error, -1 is returned, and &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; is set appropriately.&lt;br /&gt;
&lt;br /&gt;
'''Errors:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: the value of &amp;lt;code&amp;gt;wght&amp;lt;/code&amp;gt; is out of the allowed range.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;: the VE does not exist.&lt;br /&gt;
* &amp;lt;code&amp;gt;EPERM&amp;lt;/code&amp;gt;: the caller does not have permissions to modify the CPU parameters of the VE.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
It is possible to change the CPU weight of the host system. The host system's id equals &amp;lt;code&amp;gt;INT_MAX&amp;lt;/code&amp;gt; (2147483647).&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;cpuunits&amp;lt;/code&amp;gt; option of the &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt; utility is not the same thing as the CPU weight described above, but they are closely connected by the simple equation:&lt;br /&gt;
&lt;br /&gt;
''cpuweight = 500000 / cpuunits''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CPU limit ==&lt;br /&gt;
&lt;br /&gt;
'''Synopsis:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int fairsched_rate(unsigned int id, int op, unsigned int rate);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Description:'''&lt;br /&gt;
&lt;br /&gt;
The CPU limit is the upper bound to the CPU usage for a VE. The limit is set for all CPUs the VE can execute on. That means the maximal value of the CPU limit (i.e. no limit) is the maximal limit on 1 CPU, which equals 1024, multiplied by the number of CPUs this VM can execute on. The minimal value is 1. The CPU limit is an absolute parameter in the sense that altering the limit of one VE does not affect the limits of other VEs.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;fairsched_rate()&amp;lt;/code&amp;gt; system call is used to set, get, or drop (i.e. set to the maximum) CPU limit of a VE. &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; is the id of the VE. &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; can be one of the following:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;FAIRSCHED_SET_RATE&amp;lt;/code&amp;gt; to set the limit; in this case &amp;lt;code&amp;gt;rate&amp;lt;/code&amp;gt; is the limit value to set.&lt;br /&gt;
* &amp;lt;code&amp;gt;FAIRSCHED_DROP_RATE&amp;lt;/code&amp;gt; to drop the limit; &amp;lt;code&amp;gt;rate&amp;lt;/code&amp;gt; is not used.&lt;br /&gt;
* &amp;lt;code&amp;gt;FAIRSCHED_GET_RATE&amp;lt;/code&amp;gt; to get the current value of the limit; the function returns the limit value, &amp;lt;code&amp;gt;rate&amp;lt;/code&amp;gt; is not used.&lt;br /&gt;
&lt;br /&gt;
'''Return value:'''&lt;br /&gt;
&lt;br /&gt;
On success, &amp;lt;code&amp;gt;fairsched_rate()&amp;lt;/code&amp;gt; returns the current value of the CPU limit if &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;FAIRSCHED_SET_RATE&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;FAIRSCHED_GET_RATE&amp;lt;/code&amp;gt;, or 0 if &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;FAIRSCHED_DROP_RATE&amp;lt;/code&amp;gt;. On error, -1 is returned, and &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; is set appropriately.&lt;br /&gt;
&lt;br /&gt;
'''Errors:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; is invalid.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt;=&amp;lt;code&amp;gt;FAIRSCHED_SET_RATE&amp;lt;/code&amp;gt;, and the value of &amp;lt;code&amp;gt;rate&amp;lt;/code&amp;gt; is out of the allowed range.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;: the VE does not exist.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENODATA&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt;=&amp;lt;code&amp;gt;FAIRSCHED_SET_RATE&amp;lt;/code&amp;gt;, and the limit is not set (or was dropped).&lt;br /&gt;
* &amp;lt;code&amp;gt;EPERM&amp;lt;/code&amp;gt;: the caller does not have permissions to modify the CPU parameters of the VE.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
It is possible to limit the CPU usage of the host system. The host system's id equals &amp;lt;code&amp;gt;INT_MAX&amp;lt;/code&amp;gt; (2147483647).&lt;br /&gt;
&lt;br /&gt;
CPU limits are available only in rhel5-based and rhel6-based kernels, and they behave a bit differently in them in respect of distributing the CPU time among CPUs the VE can execute on.&lt;br /&gt;
&lt;br /&gt;
In the rhel5 kernel the limit has a container-wide meaning. For example, if there are 2 CPUs available for a container, and the limit is set to 1024 (of 2048), the container's CPU usage can be 100/0%, or 50/50%, or any other values whose sum does not exceed 100%.&lt;br /&gt;
&lt;br /&gt;
In the rhel6 kernel the limit is divided between the CPUs proportionally, and a busy CPU cannot borrow time from an idle one, i.e. with a 2 CPUs container and 1024 (of 2048) limit set the usage of each CPU cannot exceed 50% in any case.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Number of CPUs ==&lt;br /&gt;
&lt;br /&gt;
'''Synopsis:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int fairsched_vcpus(unsigned int id, unsigned int vcpus);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Description:'''&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;fairsched_vcpus()&amp;lt;/code&amp;gt; system call sets the number of virtual CPUs available in a VE. &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; is the id of the VE. &amp;lt;code&amp;gt;vcpus&amp;lt;/code&amp;gt; is the number of virtual CPUs to set for the VE. If &amp;lt;code&amp;gt;vcpus&amp;lt;/code&amp;gt;=0, the call attempts to set the number of CPUs in the VE to the number of online CPUs.&lt;br /&gt;
&lt;br /&gt;
'''Return value:'''&lt;br /&gt;
&lt;br /&gt;
On success, &amp;lt;code&amp;gt;fairsched_vcpus()&amp;lt;/code&amp;gt; returns 0. On error, -1 is returned, and &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; is set appropriately.&lt;br /&gt;
&lt;br /&gt;
'''Errors:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;: the VE does not exist.&lt;br /&gt;
* &amp;lt;code&amp;gt;EPERM&amp;lt;/code&amp;gt;: the caller does not have permissions to modify the CPU parameters of the VE.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
The parameter have not been implemented in the rhel6 kernel yet.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CPU affinity mask ==&lt;br /&gt;
&lt;br /&gt;
'''Synopsis:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int fairsched_cpumask(unsigned int id, unsigned int len, unsigned long *mask)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Description:'''&lt;br /&gt;
&lt;br /&gt;
A VE's CPU affinity mask determines the set of CPUs on which it is eligible to run. A CPU affinity mask is represented by an array of &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt;'s so that a VE is allowed to execute on the n-th CPU if and only if the (&amp;lt;code&amp;gt;n%sizeof(long)&amp;lt;/code&amp;gt;)-th bit of the (&amp;lt;code&amp;gt;n/sizeof(long)&amp;lt;/code&amp;gt;)-th element of the array is set.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;fairsched_cpumask()&amp;lt;/code&amp;gt; system call sets the CPU affinity mask for a VE. &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; is the id of the VE. &amp;lt;code&amp;gt;mask&amp;lt;/code&amp;gt; is the mask, and &amp;lt;code&amp;gt;len&amp;lt;/code&amp;gt; is the size of the mask, in bytes.&lt;br /&gt;
&lt;br /&gt;
'''Return value:'''&lt;br /&gt;
&lt;br /&gt;
On success, &amp;lt;code&amp;gt;fairsched_cpumask()&amp;lt;/code&amp;gt; returns 0. On error, -1 is returned, and &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; is set appropriately.&lt;br /&gt;
&lt;br /&gt;
'''Errors:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EBUSY&amp;lt;/code&amp;gt;: the VE has a child cpuset which is not a subset of the mask.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: the mask intersects with a cpuset, and either that cpuset or the VE's cpuset is marked exclusive.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;: the VE does not exist.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOMEM&amp;lt;/code&amp;gt;: there is no memory to copy the mask.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOSPC&amp;lt;/code&amp;gt;: the mask has no active CPUs set.&lt;br /&gt;
* &amp;lt;code&amp;gt;EPERM&amp;lt;/code&amp;gt;: the caller does not have permissions to modify the CPU parameters of the VE.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
The parameter is available since the rhel6 kernel.&lt;/div&gt;</summary>
		<author><name>Vdavydov</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Fairsched_API&amp;diff=9665</id>
		<title>Fairsched API</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Fairsched_API&amp;diff=9665"/>
		<updated>2011-01-24T16:06:01Z</updated>

		<summary type="html">&lt;p&gt;Vdavydov: Created page with 'There are several parameters tuning which the OpenVZ administrator can change the CPU usage of a VE. Usually one sets these parameters with the aid of the &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt; util…'&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There are several parameters tuning which the OpenVZ administrator can change the CPU usage of a VE. Usually one sets these parameters with the aid of the &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt; utility. But sometimes it can be useful to modify these parameters directly, without using &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt;. For example, it can be required by developers who want to implement an in-place solution to control a particular virtualization system based on OpenVZ. That is why the Fairsched API is introduced.&lt;br /&gt;
&lt;br /&gt;
The '''Fairsched API''' is the set of system calls that can be used (and that are, in fact, used in the &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt; utility) for altering various parameters of the [[CPU Fair scheduler]], such as &amp;lt;code&amp;gt;cpuunits&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;cpulimit&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;cpus&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;cpumask&amp;lt;/code&amp;gt;. The article is targeted to describe the API.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CPU weight ==&lt;br /&gt;
&lt;br /&gt;
'''Synopsis:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int fairsched_chwt(unsigned int id, unsigned int wght);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Description:'''&lt;br /&gt;
&lt;br /&gt;
The CPU weight defines how much CPU time a VE gets. The smaller the weight is, the more CPU time the container gets. The CPU weight is a relative parameter. That means that multiplying the weight of all running VEs by the same value does not change the CPU time distribution. The minimal value of the weight is 1, and the maximal value is &amp;lt;code&amp;gt;FSCHWEIGHT_MAX&amp;lt;/code&amp;gt; (65535). The weight of a newly created VE is set to 500 by default.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;fairsched_chwt()&amp;lt;/code&amp;gt; system call sets the CPU weight of a VE. &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; is the id of the VE. &amp;lt;code&amp;gt;wght&amp;lt;/code&amp;gt; is the desired weight.&lt;br /&gt;
&lt;br /&gt;
'''Return value:'''&lt;br /&gt;
&lt;br /&gt;
On success, &amp;lt;code&amp;gt;fairsched_chwt()&amp;lt;/code&amp;gt; returns 0. On error, -1 is returned, and &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; is set appropriately.&lt;br /&gt;
&lt;br /&gt;
'''Errors:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: the value of &amp;lt;code&amp;gt;wght&amp;lt;/code&amp;gt; is out of the allowed range.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;: the VE does not exist.&lt;br /&gt;
* &amp;lt;code&amp;gt;EPERM&amp;lt;/code&amp;gt;: the caller does not have permissions to modify the CPU parameters of the VE.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
It is possible to change the CPU weight of the host system. The host system's id equals &amp;lt;code&amp;gt;INT_MAX&amp;lt;/code&amp;gt; (2147483647).&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;cpuunits&amp;lt;/code&amp;gt; option of the &amp;lt;code&amp;gt;vzctl&amp;lt;/code&amp;gt; utility is not the same thing as the CPU weight described above, but they are closely connected by the simple equation:&lt;br /&gt;
&lt;br /&gt;
''cpuweight = 500000 / cpuunits''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CPU limit ==&lt;br /&gt;
&lt;br /&gt;
'''Synopsis:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int fairsched_rate(unsigned int id, int op, unsigned int rate);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Description:'''&lt;br /&gt;
&lt;br /&gt;
The CPU limit is the upper bound to the CPU usage for a VE. The limit is set for all CPUs the VE can execute on. That means the maximal value of the CPU limit (i.e. no limit) is the maximal limit on 1 CPU, which equals 1024, multiplied by the number of CPUs this VM can execute on. The minimal value is 1. The CPU limit is an absolute parameter in the sense that altering the limit of one VE does not affect the limits of other VEs.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;fairsched_rate()&amp;lt;/code&amp;gt; system call is used to set, get, or drop (i.e. set to the maximum) CPU limit of a VE. &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; is the id of the VE. &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; can be one of the following:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;FAIRSCHED_SET_RATE&amp;lt;/code&amp;gt; to set the limit; in this case &amp;lt;code&amp;gt;rate&amp;lt;/code&amp;gt; is the limit value to set.&lt;br /&gt;
* &amp;lt;code&amp;gt;FAIRSCHED_DROP_RATE&amp;lt;/code&amp;gt; to drop the limit; &amp;lt;code&amp;gt;rate&amp;lt;/code&amp;gt; is not used.&lt;br /&gt;
* &amp;lt;code&amp;gt;FAIRSCHED_GET_RATE&amp;lt;/code&amp;gt; to get the current value of the limit; the function returns the limit value, &amp;lt;code&amp;gt;rate&amp;lt;/code&amp;gt; is not used.&lt;br /&gt;
&lt;br /&gt;
'''Return value:'''&lt;br /&gt;
&lt;br /&gt;
On success, &amp;lt;code&amp;gt;fairsched_rate()&amp;lt;/code&amp;gt; returns the current value of the CPU limit if &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;FAIRSCHED_SET_RATE&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;FAIRSCHED_GET_RATE&amp;lt;/code&amp;gt;, or 0 if &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;FAIRSCHED_DROP_RATE&amp;lt;/code&amp;gt;. On error, -1 is returned, and &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; is set appropriately.&lt;br /&gt;
&lt;br /&gt;
'''Errors:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt; is invalid.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt;=&amp;lt;code&amp;gt;FAIRSCHED_SET_RATE&amp;lt;/code&amp;gt;, and the value of &amp;lt;code&amp;gt;rate&amp;lt;/code&amp;gt; is out of the allowed range.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;: the VE does not exist.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENODATA&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;op&amp;lt;/code&amp;gt;=&amp;lt;code&amp;gt;FAIRSCHED_SET_RATE&amp;lt;/code&amp;gt;, and the limit is not set (or was dropped).&lt;br /&gt;
* &amp;lt;code&amp;gt;EPERM&amp;lt;/code&amp;gt;: the caller does not have permissions to modify the CPU parameters of the VE.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
It is possible to limit the CPU usage of the host system. The host system's id equals &amp;lt;code&amp;gt;INT_MAX&amp;lt;/code&amp;gt; (2147483647).&lt;br /&gt;
&lt;br /&gt;
CPU limits are available only in rhel5-based and rhel6-based kernels, and they behave a bit differently in them in respect of distributing the CPU time among CPUs the VE can execute on.&lt;br /&gt;
&lt;br /&gt;
In the rhel5 kernel the limit has a container-wide meaning. For example, if there are 2 CPUs available for a container, and the limit is set to 1024 (of 2048), the container's CPU usage can be 100/0%, or 50/50%, or any other values whose sum does not exceed 100%.&lt;br /&gt;
&lt;br /&gt;
In the rhel6 kernel the limit is divided between the CPUs proportionally, and a busy CPU cannot borrow time from an idle one, i.e. with a 2 CPUs container and 1024 (of 2048) limit set the usage of each CPU cannot exceed 50% in any case.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Number of CPUs ==&lt;br /&gt;
&lt;br /&gt;
'''Synopsis:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int fairsched_vcpus(unsigned int id, unsigned int vcpus);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Description:'''&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;fairsched_vcpus()&amp;lt;/code&amp;gt; system call sets the number of virtual CPUs available in a VE. &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; is the id of the VE. &amp;lt;code&amp;gt;vcpus&amp;lt;/code&amp;gt; is the number of virtual CPUs to set for the VE. If &amp;lt;code&amp;gt;vcpus&amp;lt;/code&amp;gt;=0, the call attempts to set the number of CPUs in the VE to the number of online CPUs.&lt;br /&gt;
&lt;br /&gt;
'''Return value:'''&lt;br /&gt;
&lt;br /&gt;
On success, &amp;lt;code&amp;gt;fairsched_vcpus()&amp;lt;/code&amp;gt; returns 0. On error, -1 is returned, and &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; is set appropriately.&lt;br /&gt;
&lt;br /&gt;
'''Errors:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;: the VE does not exist.&lt;br /&gt;
* &amp;lt;code&amp;gt;EPERM&amp;lt;/code&amp;gt;: the caller does not have permissions to modify the CPU parameters of the VE.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
The parameter have not been implemented in the rhel6 kernel yet.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== CPU affinity mask ==&lt;br /&gt;
&lt;br /&gt;
'''Synopsis:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int fairsched_cpumask(unsigned int id, unsigned int len, unsigned long *mask)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Description:'''&lt;br /&gt;
&lt;br /&gt;
A VE's CPU affinity mask determines the set of CPUs on which it is eligible to run. A CPU affinity mask is represented by an array of &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt;'s so that a VE is allowed to execute on the n-th CPU if and only if the (&amp;lt;code&amp;gt;n%sizeof(long)&amp;lt;/code&amp;gt;)-th bit of the (&amp;lt;code&amp;gt;n/sizeof(long)&amp;lt;/code&amp;gt;)-th element of the array is set.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;fairsched_cpumask()&amp;lt;/code&amp;gt; system call sets the CPU affinity mask for a VE. &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt; is the id of the VE. &amp;lt;code&amp;gt;mask&amp;lt;/code&amp;gt; is the mask, and &amp;lt;code&amp;gt;len&amp;lt;/code&amp;gt; is the size of the mask, in bytes.&lt;br /&gt;
&lt;br /&gt;
'''Return value:'''&lt;br /&gt;
&lt;br /&gt;
On success, &amp;lt;code&amp;gt;fairsched_cpumask()&amp;lt;/code&amp;gt; returns 0. On error, -1 is returned, and &amp;lt;code&amp;gt;errno&amp;lt;/code&amp;gt; is set appropriately.&lt;br /&gt;
&lt;br /&gt;
'''Errors:'''&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;EBUSY&amp;lt;/code&amp;gt;: the VE has a child cpuset which is not a subset of the mask.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;id&amp;lt;/code&amp;gt;=0.&lt;br /&gt;
* &amp;lt;code&amp;gt;EINVAL&amp;lt;/code&amp;gt;: the mask intersects with a cpuset, and either that cpuset or the VE's cpuset is marked exclusive.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOENT&amp;lt;/code&amp;gt;: the VE does not exist.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOMEM&amp;lt;/code&amp;gt;: there is no memory to copy the mask.&lt;br /&gt;
* &amp;lt;code&amp;gt;ENOSPC&amp;lt;/code&amp;gt;: the mask has no active CPUs set.&lt;br /&gt;
* &amp;lt;code&amp;gt;EPERM&amp;lt;/code&amp;gt;: the caller does not have permissions to modify the CPU parameters of the VE.&lt;br /&gt;
&lt;br /&gt;
'''Notes:'''&lt;br /&gt;
&lt;br /&gt;
The parameter is available since the rhel6 kernel.&lt;/div&gt;</summary>
		<author><name>Vdavydov</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Ubstat_system_call&amp;diff=9647</id>
		<title>Ubstat system call</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Ubstat_system_call&amp;diff=9647"/>
		<updated>2011-01-20T14:06:41Z</updated>

		<summary type="html">&lt;p&gt;Vdavydov: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This article describes an interesting system call which was designed to pick beancounters statistics.&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
The system call appeared in the very first version of the OpenVZ. Its API is rather sloppy, but this is something we have to live with due to backward compatibility reasons.&lt;br /&gt;
&lt;br /&gt;
The main intention of this system call is to allow a user space process get the beancounters statistics ''periodically''. This statistics includes the fields observed in the &amp;lt;code&amp;gt;/proc/user_beancounters&amp;lt;/code&amp;gt; file and one more field - the so called &amp;lt;code&amp;gt;minheld&amp;lt;/code&amp;gt; value which is opposite to the &amp;lt;code&amp;gt;maxheld&amp;lt;/code&amp;gt; one. As long as exporting the statistics the system call also notifies the task about the desired period has elapsed. The notification is performed by sending a signal to a process and this notification is one-shot.&lt;br /&gt;
&lt;br /&gt;
== How it works ==&lt;br /&gt;
The typical usage of this call is in performing the following steps.&lt;br /&gt;
# Request the amount of resources&lt;br /&gt;
# Get the IDs of all the living beancounters&lt;br /&gt;
# Setup a handler for some signal (e.g. USR1)&lt;br /&gt;
# Perform a system call to setup the notification&lt;br /&gt;
# Go do something (or sleep for ever)&lt;br /&gt;
&lt;br /&gt;
In a signal handler one should just perform a respective system call to get the stats and schedule the next notification (yes, they are performed in one go; see below for more details).&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
The system call description is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
long ubstat(int func, unsigned long luid, void *notif, void *buf, int size);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macros and data typed used are declared in &amp;lt;code&amp;gt;include/ub/ub_stat.h&amp;lt;/code&amp;gt; file.&lt;br /&gt;
&lt;br /&gt;
=== Arguments description ===&lt;br /&gt;
&lt;br /&gt;
'''&amp;lt;code&amp;gt;func&amp;lt;/code&amp;gt;''' is like &amp;lt;code&amp;gt;cmd&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;ioctl&amp;lt;/code&amp;gt; system call. It can be one of&lt;br /&gt;
* &amp;lt;code&amp;gt;UBSTAT_READ_ONE&amp;lt;/code&amp;gt; to read basic stats for one resource. The desired resource itself should be &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;-ed with the &amp;lt;code&amp;gt;func&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;UBSTAT_READ_ALL&amp;lt;/code&amp;gt; to read basic stats about all the resources&lt;br /&gt;
* &amp;lt;code&amp;gt;UBSTAT_READ_FULL&amp;lt;/code&amp;gt; to read extended stats about all the resources&lt;br /&gt;
* &amp;lt;code&amp;gt;UBSTAT_UBLIST&amp;lt;/code&amp;gt; to get the ids of the beancounters&lt;br /&gt;
* &amp;lt;code&amp;gt;UBSTAT_UBPARMNUM&amp;lt;/code&amp;gt; to get the number of resources used by the kernel&lt;br /&gt;
* &amp;lt;code&amp;gt;UBSTAT_GETTIME&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
See below for what ''basic'' and ''extended'' stats mean.&lt;br /&gt;
&lt;br /&gt;
'''&amp;lt;code&amp;gt;luid&amp;lt;/code&amp;gt;''' is the desired beancounter ID. Only one beancounter can be checked at one call.&lt;br /&gt;
&lt;br /&gt;
'''&amp;lt;code&amp;gt;notif&amp;lt;/code&amp;gt;''' is the pointer to a &amp;lt;code&amp;gt;ubnotifrq_t&amp;lt;/code&amp;gt; structure which describes the notification details (see below).&lt;br /&gt;
&lt;br /&gt;
'''&amp;lt;code&amp;gt;buf&amp;lt;/code&amp;gt;''' is the pointer to a chunk of memory, which will contain the data requested.&lt;br /&gt;
&lt;br /&gt;
'''&amp;lt;code&amp;gt;size&amp;lt;/code&amp;gt;''' is the &amp;lt;code&amp;gt;buf&amp;lt;/code&amp;gt; memory size.&lt;br /&gt;
&lt;br /&gt;
=== The statistics format ===&lt;br /&gt;
The format of data returned into the buffer depends on the function requested.&lt;br /&gt;
&lt;br /&gt;
'''1. &amp;lt;code&amp;gt;UBSTAT_READ_ONE&amp;lt;/code&amp;gt;''' format is&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
        typedef unsigned long ubstattime_t;&lt;br /&gt;
&lt;br /&gt;
        typedef struct {&lt;br /&gt;
                unsigned long   maxheld;&lt;br /&gt;
                unsigned long   failcnt;&lt;br /&gt;
        } ubstatparm_t;&lt;br /&gt;
&lt;br /&gt;
        struct {&lt;br /&gt;
                ubstattime_t    start_time;&lt;br /&gt;
                ubstattime_t    end_time;&lt;br /&gt;
                ubstatparm_t    param[1];&lt;br /&gt;
        }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It contains the time period for which the stats are returned and the &amp;lt;code&amp;gt;maxheld&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;failcnt&amp;lt;/code&amp;gt; for the resource.&lt;br /&gt;
&lt;br /&gt;
'''2. &amp;lt;code&amp;gt;UBSTAT_READ_ALL&amp;lt;/code&amp;gt;''' format is&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
        typedef unsigned long ubstattime_t;&lt;br /&gt;
&lt;br /&gt;
        typedef struct {&lt;br /&gt;
                unsigned long   maxheld;&lt;br /&gt;
                unsigned long   failcnt;&lt;br /&gt;
        } ubstatparm_t;&lt;br /&gt;
&lt;br /&gt;
        struct {&lt;br /&gt;
                ubstattime_t    start_time;&lt;br /&gt;
                ubstattime_t    end_time;&lt;br /&gt;
                ubstatparm_t    param[UB_RESOURCES];&lt;br /&gt;
        }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It contains the same info as the &amp;lt;code&amp;gt;UBSTAT_READ_ONE&amp;lt;/code&amp;gt; does, but for all the resources.&lt;br /&gt;
&lt;br /&gt;
'''3. &amp;lt;code&amp;gt;UBSTAT_READ_FULL&amp;lt;/code&amp;gt;''' format is&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
        typedef unsigned long ubstattime_t;&lt;br /&gt;
&lt;br /&gt;
        typedef struct {&lt;br /&gt;
                unsigned long   barrier;&lt;br /&gt;
                unsigned long   limit;&lt;br /&gt;
                unsigned long   held;&lt;br /&gt;
                unsigned long   maxheld;&lt;br /&gt;
                unsigned long   minheld;&lt;br /&gt;
                unsigned long   failcnt;&lt;br /&gt;
                unsigned long __unused1;&lt;br /&gt;
                unsigned long __unused2;&lt;br /&gt;
       } ubstatparmf_t;&lt;br /&gt;
&lt;br /&gt;
        struct {&lt;br /&gt;
                ubstattime_t    start_time;&lt;br /&gt;
                ubstattime_t    end_time;&lt;br /&gt;
                ubstatparmf_t    param[UB_RESOURCES];&lt;br /&gt;
        }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It contains the extended info for all the resources.&lt;br /&gt;
&lt;br /&gt;
'''4. UBSTAT_UBLIST''' treats the &amp;lt;code&amp;gt;buf&amp;lt;/code&amp;gt; to point to the &amp;lt;code&amp;gt;unsigned long&amp;lt;/code&amp;gt; array.&lt;br /&gt;
&lt;br /&gt;
'''5. UBSTAT_UBPARMNUM''' ignores the &amp;lt;code&amp;gt;buf&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''6. UBSTAT_GETTIME''' format is&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
        typedef unsigned long ubstattime_t;&lt;br /&gt;
&lt;br /&gt;
        struct {&lt;br /&gt;
                ubstattime_t    start_time;&lt;br /&gt;
                ubstattime_t    end_time;&lt;br /&gt;
                ubstattime_t    cur_time;&lt;br /&gt;
        };&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It returns the time interval within which the stats are collected and the current time.&lt;br /&gt;
&lt;br /&gt;
All the times used are in seconds.&lt;br /&gt;
&lt;br /&gt;
=== Notification ===&lt;br /&gt;
The notification info is passed via the &amp;lt;code&amp;gt;notif&amp;lt;/code&amp;gt; argument and is being set up for all the functions except the &amp;lt;code&amp;gt;UBLIST&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;UBPARNUM&amp;lt;/code&amp;gt;. The notification is one-shot, but note that once you requested the statistics the next shot is scheduled at the same time.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;notif&amp;lt;/code&amp;gt; should point to&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
        typedef struct {&lt;br /&gt;
                long            maxinterval;&lt;br /&gt;
                int             signum;&lt;br /&gt;
        } ubnotifrq_t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;maxinterval&amp;lt;/code&amp;gt; is the time after which the notification will be delivered. It should be more than 1 (second).&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;signum&amp;lt;/code&amp;gt; is the signal that will be sent to notify.&lt;br /&gt;
&lt;br /&gt;
=== Return value ===&lt;br /&gt;
The system call returns -1 in case error has occurred. In case of &amp;lt;code&amp;gt;UBSTAT_UBPARMNUM&amp;lt;/code&amp;gt; it returns &amp;lt;code&amp;gt;UB_RESOURCES&amp;lt;/code&amp;gt; and in all other cases it returns the amount of bytes written to the &amp;lt;code&amp;gt;buf&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Demo ==&lt;br /&gt;
The following program demonstrates how you can (but not should) use the described API. This example is deliberately made very stupid and simple to demonstrate the main idea and will only work on x86_64.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;ub_stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;signal.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define UBSTAT_BUFSIZE	4096&lt;br /&gt;
#define UBSTAT_NOTIFSIG	SIGUSR1&lt;br /&gt;
#define UB_RESOURCES	24&lt;br /&gt;
&lt;br /&gt;
static char ubstat_buf[UBSTAT_BUFSIZE];&lt;br /&gt;
static int luid, func;&lt;br /&gt;
static ubnotifrq_t notif;&lt;br /&gt;
static void (*print_stat)(void *buf);&lt;br /&gt;
&lt;br /&gt;
#define sys_ubstat(args...)	syscall(503, ## args)&lt;br /&gt;
&lt;br /&gt;
static void usage(void)&lt;br /&gt;
{&lt;br /&gt;
	printf(&amp;quot;ubstat &amp;lt;id&amp;gt; &amp;lt;period&amp;gt; &amp;lt;resource&amp;gt;\n&amp;quot;);&lt;br /&gt;
	printf(&amp;quot;resource can be either num or:\n&amp;quot;);&lt;br /&gt;
	printf(&amp;quot;-1 for all maxheld and failcnt\n&amp;quot;);&lt;br /&gt;
	printf(&amp;quot;-2 for all at all\n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void print_stat_one(void *buf)&lt;br /&gt;
{&lt;br /&gt;
	struct {&lt;br /&gt;
		ubstattime_t    start_time;&lt;br /&gt;
		ubstattime_t    end_time;&lt;br /&gt;
		ubstatparm_t    param[1];&lt;br /&gt;
	} *data;&lt;br /&gt;
&lt;br /&gt;
	data = buf;&lt;br /&gt;
&lt;br /&gt;
	printf(&amp;quot;maxheld: %lu\n&amp;quot;, data-&amp;gt;param[0].maxheld);&lt;br /&gt;
	printf(&amp;quot;failcnt: %lu\n&amp;quot;, data-&amp;gt;param[0].failcnt);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void print_stat_all(void *buf)&lt;br /&gt;
{&lt;br /&gt;
	struct {&lt;br /&gt;
		ubstattime_t    start_time;&lt;br /&gt;
		ubstattime_t    end_time;&lt;br /&gt;
		ubstatparm_t    param[UB_RESOURCES];&lt;br /&gt;
	} *data;&lt;br /&gt;
	int res;&lt;br /&gt;
&lt;br /&gt;
	data = buf;&lt;br /&gt;
	for (res = 0; res &amp;lt; UB_RESOURCES; res++) {&lt;br /&gt;
		printf(&amp;quot;res %d\n&amp;quot;, res);&lt;br /&gt;
		printf(&amp;quot;\tmaxheld: %lu\n&amp;quot;, data-&amp;gt;param[res].maxheld);&lt;br /&gt;
		printf(&amp;quot;\tfailcnt: %lu\n&amp;quot;, data-&amp;gt;param[res].failcnt);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void print_stat_full(void *buf)&lt;br /&gt;
{&lt;br /&gt;
	struct {&lt;br /&gt;
		ubstattime_t    start_time;&lt;br /&gt;
		ubstattime_t    end_time;&lt;br /&gt;
		ubstatparmf_t    param[UB_RESOURCES];&lt;br /&gt;
	} *data;&lt;br /&gt;
	int res;&lt;br /&gt;
&lt;br /&gt;
	data = buf;&lt;br /&gt;
	for (res = 0; res &amp;lt; UB_RESOURCES; res++) {&lt;br /&gt;
		printf(&amp;quot;res %d\n&amp;quot;, res);&lt;br /&gt;
		printf(&amp;quot;minheld: %lu\n&amp;quot;, data-&amp;gt;param[res].minheld);&lt;br /&gt;
		printf(&amp;quot;maxheld: %lu\n&amp;quot;, data-&amp;gt;param[res].maxheld);&lt;br /&gt;
		printf(&amp;quot;failcnt: %lu\n&amp;quot;, data-&amp;gt;param[res].failcnt);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static inline int res2func(int resource)&lt;br /&gt;
{&lt;br /&gt;
	if (resource &amp;gt;= 0) {&lt;br /&gt;
		print_stat = print_stat_one;&lt;br /&gt;
		return UBSTAT_READ_ONE | resource;&lt;br /&gt;
	}&lt;br /&gt;
	if (resource == -1) {&lt;br /&gt;
		print_stat = print_stat_all;&lt;br /&gt;
		return UBSTAT_READ_ALL;&lt;br /&gt;
	}&lt;br /&gt;
	if (resource == -2) {&lt;br /&gt;
		print_stat = print_stat_full;&lt;br /&gt;
		return UBSTAT_READ_FULL;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	printf(&amp;quot;Bad resource %d\n&amp;quot;, resource);&lt;br /&gt;
	exit(1);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void do_notify(int x)&lt;br /&gt;
{&lt;br /&gt;
	int err;&lt;br /&gt;
&lt;br /&gt;
	err = sys_ubstat(func, luid, (unsigned long)&amp;amp;notif,&lt;br /&gt;
			ubstat_buf, UBSTAT_BUFSIZE);&lt;br /&gt;
	if (err &amp;lt; 0) {&lt;br /&gt;
		perror(&amp;quot;Can't set stat&amp;quot;);&lt;br /&gt;
		exit(0);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	print_stat(ubstat_buf);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int do_ubstat(int id, int period, int resource)&lt;br /&gt;
{&lt;br /&gt;
	luid = id;&lt;br /&gt;
	func = res2func(resource);&lt;br /&gt;
	notif.maxinterval = period;&lt;br /&gt;
	notif.signum = UBSTAT_NOTIFSIG;&lt;br /&gt;
	signal(UBSTAT_NOTIFSIG, do_notify);&lt;br /&gt;
	do_notify(0);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv)&lt;br /&gt;
{&lt;br /&gt;
	int id, period, res;&lt;br /&gt;
&lt;br /&gt;
	if (argc == 1) {&lt;br /&gt;
		usage();&lt;br /&gt;
		return 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	id = atoi(argv[1]);&lt;br /&gt;
	period = atoi(argv[2]);&lt;br /&gt;
	res = atoi(argv[3]);&lt;br /&gt;
&lt;br /&gt;
	do_ubstat(id, period, res);&lt;br /&gt;
&lt;br /&gt;
	while (1)&lt;br /&gt;
		sleep(10);&lt;br /&gt;
&lt;br /&gt;
	return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implementation constraints ==&lt;br /&gt;
Unfortunately the API is not architecture independent and thus 32-bit application will simply not work on x86_64.&lt;br /&gt;
&lt;br /&gt;
[[Category:UBC]]&lt;/div&gt;</summary>
		<author><name>Vdavydov</name></author>
		
	</entry>
	<entry>
		<id>https://wiki.openvz.org/index.php?title=Ubstat_system_call&amp;diff=9646</id>
		<title>Ubstat system call</title>
		<link rel="alternate" type="text/html" href="https://wiki.openvz.org/index.php?title=Ubstat_system_call&amp;diff=9646"/>
		<updated>2011-01-20T14:06:31Z</updated>

		<summary type="html">&lt;p&gt;Vdavydov: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This article describes an interesting system call which was designed to pick beancounters statistics&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
The system call appeared in the very first version of the OpenVZ. Its API is rather sloppy, but this is something we have to live with due to backward compatibility reasons.&lt;br /&gt;
&lt;br /&gt;
The main intention of this system call is to allow a user space process get the beancounters statistics ''periodically''. This statistics includes the fields observed in the &amp;lt;code&amp;gt;/proc/user_beancounters&amp;lt;/code&amp;gt; file and one more field - the so called &amp;lt;code&amp;gt;minheld&amp;lt;/code&amp;gt; value which is opposite to the &amp;lt;code&amp;gt;maxheld&amp;lt;/code&amp;gt; one. As long as exporting the statistics the system call also notifies the task about the desired period has elapsed. The notification is performed by sending a signal to a process and this notification is one-shot.&lt;br /&gt;
&lt;br /&gt;
== How it works ==&lt;br /&gt;
The typical usage of this call is in performing the following steps.&lt;br /&gt;
# Request the amount of resources&lt;br /&gt;
# Get the IDs of all the living beancounters&lt;br /&gt;
# Setup a handler for some signal (e.g. USR1)&lt;br /&gt;
# Perform a system call to setup the notification&lt;br /&gt;
# Go do something (or sleep for ever)&lt;br /&gt;
&lt;br /&gt;
In a signal handler one should just perform a respective system call to get the stats and schedule the next notification (yes, they are performed in one go; see below for more details).&lt;br /&gt;
&lt;br /&gt;
== API ==&lt;br /&gt;
The system call description is&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
long ubstat(int func, unsigned long luid, void *notif, void *buf, int size);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The macros and data typed used are declared in &amp;lt;code&amp;gt;include/ub/ub_stat.h&amp;lt;/code&amp;gt; file.&lt;br /&gt;
&lt;br /&gt;
=== Arguments description ===&lt;br /&gt;
&lt;br /&gt;
'''&amp;lt;code&amp;gt;func&amp;lt;/code&amp;gt;''' is like &amp;lt;code&amp;gt;cmd&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;ioctl&amp;lt;/code&amp;gt; system call. It can be one of&lt;br /&gt;
* &amp;lt;code&amp;gt;UBSTAT_READ_ONE&amp;lt;/code&amp;gt; to read basic stats for one resource. The desired resource itself should be &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;-ed with the &amp;lt;code&amp;gt;func&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;UBSTAT_READ_ALL&amp;lt;/code&amp;gt; to read basic stats about all the resources&lt;br /&gt;
* &amp;lt;code&amp;gt;UBSTAT_READ_FULL&amp;lt;/code&amp;gt; to read extended stats about all the resources&lt;br /&gt;
* &amp;lt;code&amp;gt;UBSTAT_UBLIST&amp;lt;/code&amp;gt; to get the ids of the beancounters&lt;br /&gt;
* &amp;lt;code&amp;gt;UBSTAT_UBPARMNUM&amp;lt;/code&amp;gt; to get the number of resources used by the kernel&lt;br /&gt;
* &amp;lt;code&amp;gt;UBSTAT_GETTIME&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
See below for what ''basic'' and ''extended'' stats mean.&lt;br /&gt;
&lt;br /&gt;
'''&amp;lt;code&amp;gt;luid&amp;lt;/code&amp;gt;''' is the desired beancounter ID. Only one beancounter can be checked at one call.&lt;br /&gt;
&lt;br /&gt;
'''&amp;lt;code&amp;gt;notif&amp;lt;/code&amp;gt;''' is the pointer to a &amp;lt;code&amp;gt;ubnotifrq_t&amp;lt;/code&amp;gt; structure which describes the notification details (see below).&lt;br /&gt;
&lt;br /&gt;
'''&amp;lt;code&amp;gt;buf&amp;lt;/code&amp;gt;''' is the pointer to a chunk of memory, which will contain the data requested.&lt;br /&gt;
&lt;br /&gt;
'''&amp;lt;code&amp;gt;size&amp;lt;/code&amp;gt;''' is the &amp;lt;code&amp;gt;buf&amp;lt;/code&amp;gt; memory size.&lt;br /&gt;
&lt;br /&gt;
=== The statistics format ===&lt;br /&gt;
The format of data returned into the buffer depends on the function requested.&lt;br /&gt;
&lt;br /&gt;
'''1. &amp;lt;code&amp;gt;UBSTAT_READ_ONE&amp;lt;/code&amp;gt;''' format is&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
        typedef unsigned long ubstattime_t;&lt;br /&gt;
&lt;br /&gt;
        typedef struct {&lt;br /&gt;
                unsigned long   maxheld;&lt;br /&gt;
                unsigned long   failcnt;&lt;br /&gt;
        } ubstatparm_t;&lt;br /&gt;
&lt;br /&gt;
        struct {&lt;br /&gt;
                ubstattime_t    start_time;&lt;br /&gt;
                ubstattime_t    end_time;&lt;br /&gt;
                ubstatparm_t    param[1];&lt;br /&gt;
        }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It contains the time period for which the stats are returned and the &amp;lt;code&amp;gt;maxheld&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;failcnt&amp;lt;/code&amp;gt; for the resource.&lt;br /&gt;
&lt;br /&gt;
'''2. &amp;lt;code&amp;gt;UBSTAT_READ_ALL&amp;lt;/code&amp;gt;''' format is&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
        typedef unsigned long ubstattime_t;&lt;br /&gt;
&lt;br /&gt;
        typedef struct {&lt;br /&gt;
                unsigned long   maxheld;&lt;br /&gt;
                unsigned long   failcnt;&lt;br /&gt;
        } ubstatparm_t;&lt;br /&gt;
&lt;br /&gt;
        struct {&lt;br /&gt;
                ubstattime_t    start_time;&lt;br /&gt;
                ubstattime_t    end_time;&lt;br /&gt;
                ubstatparm_t    param[UB_RESOURCES];&lt;br /&gt;
        }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It contains the same info as the &amp;lt;code&amp;gt;UBSTAT_READ_ONE&amp;lt;/code&amp;gt; does, but for all the resources.&lt;br /&gt;
&lt;br /&gt;
'''3. &amp;lt;code&amp;gt;UBSTAT_READ_FULL&amp;lt;/code&amp;gt;''' format is&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
        typedef unsigned long ubstattime_t;&lt;br /&gt;
&lt;br /&gt;
        typedef struct {&lt;br /&gt;
                unsigned long   barrier;&lt;br /&gt;
                unsigned long   limit;&lt;br /&gt;
                unsigned long   held;&lt;br /&gt;
                unsigned long   maxheld;&lt;br /&gt;
                unsigned long   minheld;&lt;br /&gt;
                unsigned long   failcnt;&lt;br /&gt;
                unsigned long __unused1;&lt;br /&gt;
                unsigned long __unused2;&lt;br /&gt;
       } ubstatparmf_t;&lt;br /&gt;
&lt;br /&gt;
        struct {&lt;br /&gt;
                ubstattime_t    start_time;&lt;br /&gt;
                ubstattime_t    end_time;&lt;br /&gt;
                ubstatparmf_t    param[UB_RESOURCES];&lt;br /&gt;
        }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It contains the extended info for all the resources.&lt;br /&gt;
&lt;br /&gt;
'''4. UBSTAT_UBLIST''' treats the &amp;lt;code&amp;gt;buf&amp;lt;/code&amp;gt; to point to the &amp;lt;code&amp;gt;unsigned long&amp;lt;/code&amp;gt; array.&lt;br /&gt;
&lt;br /&gt;
'''5. UBSTAT_UBPARMNUM''' ignores the &amp;lt;code&amp;gt;buf&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
'''6. UBSTAT_GETTIME''' format is&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
        typedef unsigned long ubstattime_t;&lt;br /&gt;
&lt;br /&gt;
        struct {&lt;br /&gt;
                ubstattime_t    start_time;&lt;br /&gt;
                ubstattime_t    end_time;&lt;br /&gt;
                ubstattime_t    cur_time;&lt;br /&gt;
        };&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It returns the time interval within which the stats are collected and the current time.&lt;br /&gt;
&lt;br /&gt;
All the times used are in seconds.&lt;br /&gt;
&lt;br /&gt;
=== Notification ===&lt;br /&gt;
The notification info is passed via the &amp;lt;code&amp;gt;notif&amp;lt;/code&amp;gt; argument and is being set up for all the functions except the &amp;lt;code&amp;gt;UBLIST&amp;lt;/code&amp;gt; and the &amp;lt;code&amp;gt;UBPARNUM&amp;lt;/code&amp;gt;. The notification is one-shot, but note that once you requested the statistics the next shot is scheduled at the same time.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;notif&amp;lt;/code&amp;gt; should point to&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
        typedef struct {&lt;br /&gt;
                long            maxinterval;&lt;br /&gt;
                int             signum;&lt;br /&gt;
        } ubnotifrq_t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;maxinterval&amp;lt;/code&amp;gt; is the time after which the notification will be delivered. It should be more than 1 (second).&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;signum&amp;lt;/code&amp;gt; is the signal that will be sent to notify.&lt;br /&gt;
&lt;br /&gt;
=== Return value ===&lt;br /&gt;
The system call returns -1 in case error has occurred. In case of &amp;lt;code&amp;gt;UBSTAT_UBPARMNUM&amp;lt;/code&amp;gt; it returns &amp;lt;code&amp;gt;UB_RESOURCES&amp;lt;/code&amp;gt; and in all other cases it returns the amount of bytes written to the &amp;lt;code&amp;gt;buf&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Demo ==&lt;br /&gt;
The following program demonstrates how you can (but not should) use the described API. This example is deliberately made very stupid and simple to demonstrate the main idea and will only work on x86_64.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;ub_stat.h&amp;gt;&lt;br /&gt;
#include &amp;lt;signal.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define UBSTAT_BUFSIZE	4096&lt;br /&gt;
#define UBSTAT_NOTIFSIG	SIGUSR1&lt;br /&gt;
#define UB_RESOURCES	24&lt;br /&gt;
&lt;br /&gt;
static char ubstat_buf[UBSTAT_BUFSIZE];&lt;br /&gt;
static int luid, func;&lt;br /&gt;
static ubnotifrq_t notif;&lt;br /&gt;
static void (*print_stat)(void *buf);&lt;br /&gt;
&lt;br /&gt;
#define sys_ubstat(args...)	syscall(503, ## args)&lt;br /&gt;
&lt;br /&gt;
static void usage(void)&lt;br /&gt;
{&lt;br /&gt;
	printf(&amp;quot;ubstat &amp;lt;id&amp;gt; &amp;lt;period&amp;gt; &amp;lt;resource&amp;gt;\n&amp;quot;);&lt;br /&gt;
	printf(&amp;quot;resource can be either num or:\n&amp;quot;);&lt;br /&gt;
	printf(&amp;quot;-1 for all maxheld and failcnt\n&amp;quot;);&lt;br /&gt;
	printf(&amp;quot;-2 for all at all\n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void print_stat_one(void *buf)&lt;br /&gt;
{&lt;br /&gt;
	struct {&lt;br /&gt;
		ubstattime_t    start_time;&lt;br /&gt;
		ubstattime_t    end_time;&lt;br /&gt;
		ubstatparm_t    param[1];&lt;br /&gt;
	} *data;&lt;br /&gt;
&lt;br /&gt;
	data = buf;&lt;br /&gt;
&lt;br /&gt;
	printf(&amp;quot;maxheld: %lu\n&amp;quot;, data-&amp;gt;param[0].maxheld);&lt;br /&gt;
	printf(&amp;quot;failcnt: %lu\n&amp;quot;, data-&amp;gt;param[0].failcnt);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void print_stat_all(void *buf)&lt;br /&gt;
{&lt;br /&gt;
	struct {&lt;br /&gt;
		ubstattime_t    start_time;&lt;br /&gt;
		ubstattime_t    end_time;&lt;br /&gt;
		ubstatparm_t    param[UB_RESOURCES];&lt;br /&gt;
	} *data;&lt;br /&gt;
	int res;&lt;br /&gt;
&lt;br /&gt;
	data = buf;&lt;br /&gt;
	for (res = 0; res &amp;lt; UB_RESOURCES; res++) {&lt;br /&gt;
		printf(&amp;quot;res %d\n&amp;quot;, res);&lt;br /&gt;
		printf(&amp;quot;\tmaxheld: %lu\n&amp;quot;, data-&amp;gt;param[res].maxheld);&lt;br /&gt;
		printf(&amp;quot;\tfailcnt: %lu\n&amp;quot;, data-&amp;gt;param[res].failcnt);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void print_stat_full(void *buf)&lt;br /&gt;
{&lt;br /&gt;
	struct {&lt;br /&gt;
		ubstattime_t    start_time;&lt;br /&gt;
		ubstattime_t    end_time;&lt;br /&gt;
		ubstatparmf_t    param[UB_RESOURCES];&lt;br /&gt;
	} *data;&lt;br /&gt;
	int res;&lt;br /&gt;
&lt;br /&gt;
	data = buf;&lt;br /&gt;
	for (res = 0; res &amp;lt; UB_RESOURCES; res++) {&lt;br /&gt;
		printf(&amp;quot;res %d\n&amp;quot;, res);&lt;br /&gt;
		printf(&amp;quot;minheld: %lu\n&amp;quot;, data-&amp;gt;param[res].minheld);&lt;br /&gt;
		printf(&amp;quot;maxheld: %lu\n&amp;quot;, data-&amp;gt;param[res].maxheld);&lt;br /&gt;
		printf(&amp;quot;failcnt: %lu\n&amp;quot;, data-&amp;gt;param[res].failcnt);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static inline int res2func(int resource)&lt;br /&gt;
{&lt;br /&gt;
	if (resource &amp;gt;= 0) {&lt;br /&gt;
		print_stat = print_stat_one;&lt;br /&gt;
		return UBSTAT_READ_ONE | resource;&lt;br /&gt;
	}&lt;br /&gt;
	if (resource == -1) {&lt;br /&gt;
		print_stat = print_stat_all;&lt;br /&gt;
		return UBSTAT_READ_ALL;&lt;br /&gt;
	}&lt;br /&gt;
	if (resource == -2) {&lt;br /&gt;
		print_stat = print_stat_full;&lt;br /&gt;
		return UBSTAT_READ_FULL;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	printf(&amp;quot;Bad resource %d\n&amp;quot;, resource);&lt;br /&gt;
	exit(1);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void do_notify(int x)&lt;br /&gt;
{&lt;br /&gt;
	int err;&lt;br /&gt;
&lt;br /&gt;
	err = sys_ubstat(func, luid, (unsigned long)&amp;amp;notif,&lt;br /&gt;
			ubstat_buf, UBSTAT_BUFSIZE);&lt;br /&gt;
	if (err &amp;lt; 0) {&lt;br /&gt;
		perror(&amp;quot;Can't set stat&amp;quot;);&lt;br /&gt;
		exit(0);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	print_stat(ubstat_buf);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int do_ubstat(int id, int period, int resource)&lt;br /&gt;
{&lt;br /&gt;
	luid = id;&lt;br /&gt;
	func = res2func(resource);&lt;br /&gt;
	notif.maxinterval = period;&lt;br /&gt;
	notif.signum = UBSTAT_NOTIFSIG;&lt;br /&gt;
	signal(UBSTAT_NOTIFSIG, do_notify);&lt;br /&gt;
	do_notify(0);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv)&lt;br /&gt;
{&lt;br /&gt;
	int id, period, res;&lt;br /&gt;
&lt;br /&gt;
	if (argc == 1) {&lt;br /&gt;
		usage();&lt;br /&gt;
		return 0;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	id = atoi(argv[1]);&lt;br /&gt;
	period = atoi(argv[2]);&lt;br /&gt;
	res = atoi(argv[3]);&lt;br /&gt;
&lt;br /&gt;
	do_ubstat(id, period, res);&lt;br /&gt;
&lt;br /&gt;
	while (1)&lt;br /&gt;
		sleep(10);&lt;br /&gt;
&lt;br /&gt;
	return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Implementation constraints ==&lt;br /&gt;
Unfortunately the API is not architecture independent and thus 32-bit application will simply not work on x86_64.&lt;br /&gt;
&lt;br /&gt;
[[Category:UBC]]&lt;/div&gt;</summary>
		<author><name>Vdavydov</name></author>
		
	</entry>
</feed>