[libvirt] [PATCH v2 1/3] util: Add virProcessSetScheduler() function for scheduler settings

Eric Blake eblake at redhat.com
Wed Feb 11 17:34:40 UTC 2015


On 02/11/2015 07:04 AM, Michal Privoznik wrote:
> On 10.02.2015 16:35, Martin Kletzander wrote:
>> This function uses sched_setscheduler() function so it works with
>> processes and threads as well (even threads not created by us, which is
>> what we'll need in the future).
>>

>>
>> +VIR_ENUM_IMPL(virProcessSchedPolicy, VIR_PROC_POLICY_LAST,
>> +              "none",
>> +              "batch",
>> +              "idle",
>> +              "fifo",
>> +              "rr");
>> +
>>  /**

>> +int
>> +virProcessSetScheduler(pid_t pid, virProcessSchedPolicy policy, int priority)
>> +{
>> +    struct sched_param param = {0};
>> +    int pol = virProcessSchedTranslatePolicy(policy);
>> +
>> +    VIR_DEBUG("pid=%d, policy=%d, priority=%u", pid, policy, priority);
>> +
>> +    if (!policy)
>> +        return 0;
> 
> In order to do this, you have to make sure enum has a zero value
> element. I think compilers don't guarantee enum values <-> integer
> mapping unless told so.
> 

>> +
>> +typedef enum {
>> +    VIR_PROC_POLICY_NONE,
>> +    VIR_PROC_POLICY_BATCH,
>> +    VIR_PROC_POLICY_IDLE,
>> +    VIR_PROC_POLICY_FIFO,
>> +    VIR_PROC_POLICY_RR,
>> +
>> +    VIR_PROC_POLICY_LAST
>> +} virProcessSchedPolicy;

The C language guarantees that VIR_PROC_POLICY_NONE == 0, and that
VIR_PROC_POLICY_BATCH == (VIR_PROC_POLICY_NONE + 1).  That is, C
guarantees that an initial enum not otherwise initialized is 0, and that
all subsequent enums not otherwise initialized are one more than the
previous value (whether or not the previous value was explicitly
initialized).  So the code you questioned is safe as-is.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 604 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20150211/a413fc10/attachment-0001.sig>


More information about the libvir-list mailing list