linux中iowait的形成原因和核心分析

來源:互聯網
上載者:User

經常我們碰到一些問題,進程的寫檔案,寫入的速度非常慢,而且當時總的IO的數量(BI,BO)也非常低,但是vmstat顯示的iowait卻非常高,就是下圖中的wa選項。
iowait_oenhan

man vmstat手冊如下解釋:

 

wa: Time spent waiting for IO. Prior to Linux 2.5.41, included in idle.
CPU花費到IO等待上的時間,也就是說進程的io被CPU調度出去了,並沒有進行立即執行,導致整個執行時間延長,程式效能降低。

那麼wa是如何統計的呢?

vmstat是procps內實現的


oenhan@oenhan ~ $ dpkg-query -S /usr/bin/vmstat
procps: /usr/bin/vmstat
在procps中,wa中通過getstat讀取/proc/stat實現的。


void getstat(jiff *restrict cuse, jiff *restrict cice,
             jiff *restrict csys, jiff *restrict cide,
             jiff *restrict ciow, jiff *restrict cxxx,
             jiff *restrict cyyy, jiff *restrict czzz,
         unsigned long *restrict pin, unsigned long *restrict pout,
             unsigned long *restrict s_in,unsigned long *restrict sout,
         unsigned *restrict intr, unsigned *restrict ctxt,
         unsigned int *restrict running, unsigned int *restrict blocked,
         unsigned int *restrict btime, unsigned int *restrict processes)
{
  static int fd;
  unsigned long long llbuf = 0;
  int need_vmstat_file = 0;
  int need_proc_scan = 0;
  const char* b;
  buff[BUFFSIZE-1] = 0;  /* ensure null termination in buffer */
 
  if(fd){
    lseek(fd, 0L, SEEK_SET);
  }else{
    fd = open("/proc/stat", O_RDONLY, 0);
    if(fd == -1) crash("/proc/stat");
  }
  read(fd,buff,BUFFSIZE-1);
  *intr = 0;
  *ciow = 0;  /* not separated out until the 2.5.41 kernel */
  *cxxx = 0;  /* not separated out until the 2.6.0-test4 kernel */
  *cyyy = 0;  /* not separated out until the 2.6.0-test4 kernel */
  *czzz = 0;  /* not separated out until the 2.6.11 kernel */
 
  b = strstr(buff, "cpu ");
  if(b) sscanf(b,  "cpu  %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu",
          cuse, cice, csys, cide, ciow, cxxx, cyyy, czzz);
proc/stat在核心中通過show_stat實現,統計cpustat.iowait擷取最終結果:


iowait = cputime64_add(iowait, get_iowait_time(i));

static cputime64_t get_iowait_time(int cpu)
{
 u64 iowait_time = -1ULL;
 cputime64_t iowait;

 if (cpu_online(cpu))
  iowait_time = get_cpu_iowait_time_us(cpu, NULL);

 if (iowait_time == -1ULL)
  /* !NO_HZ or cpu offline so we can rely on cpustat.iowait */
  iowait = kstat_cpu(cpu).cpustat.iowait;
 else
  iowait = usecs_to_cputime64(iowait_time);

 return iowait;
}

而cpustat.iowait的值在account_system_time統計計算,通過統計rq->nr_iowait的值判斷來遞增。


void account_system_time(struct task_struct *p, int hardirq_offset,
    cputime_t cputime)
{
 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
 runqueue_t *rq = this_rq();
 cputime64_t tmp;

 p->stime = cputime_add(p->stime, cputime);

 /* Add system time to cpustat. */
 tmp = cputime_to_cputime64(cputime);
 if (hardirq_count() - hardirq_offset)
  cpustat->irq = cputime64_add(cpustat->irq, tmp);
 else if (softirq_count())
  cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
 else if (p != rq->idle)
  cpustat->system = cputime64_add(cpustat->system, tmp);
 else if (atomic_read(&rq->nr_iowait) > 0)
  cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
 else
  cpustat->idle = cputime64_add(cpustat->idle, tmp);
 /* Account for system time used */
 acct_update_integrals(p);
}

附加一句:account_system_time可以用來具體統計更詳細的CPU佔用情況。

而控制的rq->nr_iowait的函數是io_schedule和io_schedule_timeout。


void __sched io_schedule(void)
{
 struct runqueue *rq = &per_cpu(runqueues, raw_smp_processor_id());

 delayacct_blkio_start();
 atomic_inc(&rq->nr_iowait);
 schedule();
 atomic_dec(&rq->nr_iowait);
 delayacct_blkio_end();
}

long __sched io_schedule_timeout(long timeout)
{
 struct runqueue *rq = &per_cpu(runqueues, raw_smp_processor_id());
 long ret;

 delayacct_blkio_start();
 atomic_inc(&rq->nr_iowait);
 ret = schedule_timeout(timeout);
 atomic_dec(&rq->nr_iowait);
 delayacct_blkio_end();
 return ret;
}

因為沒有實際應用情境,針對調用代碼做一下分析,具體的應用情境可以參考
查看調用代碼如下:

 

先看io_schedule函數,調用它的有sync_buffer,sync_io,dio_awit_one,direct_io_worker,get_request_wait等,只要你有對磁碟的寫入操作,就會調用io_schedule,尤其以直接寫入(DIO)最明顯,每次寫入都會調用。普通寫入也只是積攢到buffer裡面,統一重新整理buffer的時候才會調用一次io_schedule。即使沒有主動的寫入IO,也有可能產生iowait,在sync_page函數中,當緩衝和硬碟資料進行同步的時候,就會被調用,比如常用的mlock鎖記憶體時就會__lock_page觸發緩衝同步,一樣會增加iowait,一些讀硬碟也會觸發。

 

雖然io_schedule調用的非常多,但真正的大殺器還是io_schedule_timeout,io_schedule由核心調用,整個調度時間還是比較短的,io_schedule_timeout則基本都是指定了HZ/10的時間段。如balance_dirty_pages中每次寫入檔案都會對髒頁進行平衡處理,wb_kupdate定時器般進行緩衝重新整理,try_to_free_pages則在釋放記憶體時刷緩衝時使用,雖然不是每次都使用io_schedule_timeout,但綜合各個的判斷條件看,當記憶體中的緩衝足夠多的時候,會極大觸發io_schedule_timeout,水線一般是dirty_background_ratio和dirty_ratio。

如上所分析的,避免iowait需要注意的有:

1.高效能要求的進程最好在記憶體中一次讀完所有資料,全部mlock住,不需要後面再讀磁碟,

2.不需要寫或者重新整理磁碟(一般搞不定,就讓單獨的進程專門幹這個活)。

3.控制整個緩衝的使用方式,但從工程上不容易處理,虛擬化估計更有效,相對與系統虛擬化(KVM),進程虛擬化(cgroup)應該更簡單有效。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.