2012年3月29日星期四

Header file for ioprio

Linux kernel offers the ability to set the priority of the I/O requests issued by a process. The corresponding system call is ioprio_get and ioprio_set.  However, libc has yet to offer an appropriate header file for us to use this functionality in the user space.

Following is the header file I use. It simply wraps the system call and copied a few macro definitions from the kernel source file.

Name the file ioprio.h and copy it into /usr/include, and then start using ioprio :)



#include <unistd.h>

extern int sys_ioprio_set(int, int, int);
extern int sys_ioprio_get(int, int);

#if defined(__i386__)
#define __NR_ioprio_set         289
#define __NR_ioprio_get         290
#elif defined(__ppc__)
#define __NR_ioprio_set         273
#define __NR_ioprio_get         274
#elif defined(__x86_64__)
#define __NR_ioprio_set         251
#define __NR_ioprio_get         252
#elif defined(__ia64__)
#define __NR_ioprio_set         1274
#define __NR_ioprio_get         1275
#else
#error "Unsupported arch"
#endif

static inline int ioprio_set(int which, int who, int ioprio)
{
        return syscall(__NR_ioprio_set, which, who, ioprio);
}


static inline int ioprio_get(int which, int who)
{
        return syscall(__NR_ioprio_get, which, who);
}


enum { 
        IOPRIO_CLASS_NONE, 
        IOPRIO_CLASS_RT, 
        IOPRIO_CLASS_BE, 
        IOPRIO_CLASS_IDLE, 
};

enum {
        IOPRIO_WHO_PROCESS = 1,
        IOPRIO_WHO_PGRP,
        IOPRIO_WHO_USER,
};

#define IOPRIO_BITS             (16)
#define IOPRIO_CLASS_SHIFT      (13)
#define IOPRIO_PRIO_MASK        ((1UL << IOPRIO_CLASS_SHIFT) - 1)

#define IOPRIO_PRIO_CLASS(mask) ((mask) >> IOPRIO_CLASS_SHIFT)
#define IOPRIO_PRIO_DATA(mask)  ((mask) & IOPRIO_PRIO_MASK)
#define IOPRIO_PRIO_VALUE(class, data)  (((class) << IOPRIO_CLASS_SHIFT) | data)

#define ioprio_valid(mask)      (IOPRIO_PRIO_CLASS((mask)) != IOPRIO_CLASS_NONE)

没有评论:

发表评论