Linux Doc:子系统:存储接口:块设备:Block io 优先级

介绍

io优先级特性允许用户对进程或进程组进行io优先级调整,类似于cpu调度多年来所实现的功能。io优先级的支持依赖于io调度器,目前由bfq和mq-deadline支持。

调度类

io优先级由三个通用调度类组成,它们决定了如何为进程提供io服务。

IOPRIO_CLASS_RT:这是实时io类。这个调度类的优先级比系统中任何其他调度类的优先级都高,来自这个类的进程每次都被赋予对磁盘的优先访问权。因此需要谨慎使用,一个io RT进程可能会使整个系统饥饿。在RT类中,有8个级别的类数据,它们确定了该进程在每个服务上需要磁盘的确切时间。在未来,这种方式也许会发生变化,变为传入所需的数据速率,这可能会更直接地映射到性能。

IOPRIO_CLASS_BE:这是best-effort调度类,对于没有设置特定io优先级的进程来说,这是默认的。类数据决定了进程将获得多少io带宽,它直接映射到cpu级别,只是实现得更粗糙。0是最高的BE优先级,7是最低的。cpu nice level和io nice level的对应关系确定为:io_nice = (cpu_nice + 20) / 5

IOPRIO_CLASS_IDLE:这是空闲调度类,在此级别上运行的进程只有在没有其他人需要磁盘时才获得io时间。空闲类没有类数据,因为它在这里并不真正适用。

工具

参见下面ionice工具的用法示例:

# ionice -c<class> -n<level> -p<pid>

如果没有给出pid,则假定当前进程。IO优先级设置在fork上继承,所以你可以使用ionice在给定级别启动进程:

# ionice -c2 -n0 /bin/ls

将以最高优先级的best-effort调度类运行ls。对于正在运行的进程,你可以给出pid:

# ionice -c1 -n2 -p100

将pid 100更改为在实时调度类中运行,优先级为2。

工具源码 ionice.c

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <getopt.h>
#include <unistd.h>
#include <sys/ptrace.h>
#include <asm/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
#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_CLASS_SHIFT    13

const char *to_prio[] = { "none", "realtime", "best-effort", "idle", };

int main(int argc, char *argv[])
{
      int ioprio = 4, set = 0, ioprio_class = IOPRIO_CLASS_BE;
      int c, pid = 0;

      while ((c = getopt(argc, argv, "+n:c:p:")) != EOF) {
              switch (c) {
              case 'n':
                      ioprio = strtol(optarg, NULL, 10);
                      set = 1;
                      break;
              case 'c':
                      ioprio_class = strtol(optarg, NULL, 10);
                      set = 1;
                      break;
              case 'p':
                      pid = strtol(optarg, NULL, 10);
                      break;
              }
      }

      switch (ioprio_class) {
              case IOPRIO_CLASS_NONE:
                      ioprio_class = IOPRIO_CLASS_BE;
                      break;
              case IOPRIO_CLASS_RT:
              case IOPRIO_CLASS_BE:
                      break;
              case IOPRIO_CLASS_IDLE:
                      ioprio = 7;
                      break;
              default:
                      printf("bad prio class %d\n", ioprio_class);
                      return 1;
      }

      if (!set) {
              if (!pid && argv[optind])
                      pid = strtol(argv[optind], NULL, 10);

              ioprio = ioprio_get(IOPRIO_WHO_PROCESS, pid);

              printf("pid=%d, %d\n", pid, ioprio);

              if (ioprio == -1)
                      perror("ioprio_get");
              else {
                      ioprio_class = ioprio >> IOPRIO_CLASS_SHIFT;
                      ioprio = ioprio & 0xff;
                      printf("%s: prio %d\n", to_prio[ioprio_class], ioprio);
              }
      } else {
              if (ioprio_set(IOPRIO_WHO_PROCESS, pid, ioprio | ioprio_class << IOPRIO_CLASS_SHIFT) == -1) {
                      perror("ioprio_set");
                      return 1;
              }

              if (argv[optind])
                      execvp(argv[optind], &argv[optind]);
      }

      return 0;
}

2005年3月11日,Jens Axboe jens.axboe@oracle.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值