gpt4 book ai didi

linux - 如何计算 iostat 的效用?

转载 作者:IT王子 更新时间:2023-10-29 00:23:27 29 4
gpt4 key购买 nike

iostat -x -d 

可以显示很多i/o统计信息。对于iostat的util,解释是:

Percentage of CPU time during which I/O requests were issued to the device (band-width utilization for the device). Device saturation occurs when this value is close to 100%

我想知道 util 是如何计算出来的?

我做一个实验,(见下面代码),启动40个线程随机读取40个文件。我想磁盘利用率应该很高,但我错了,iostat如下,任何人都可以给出为什么?谢谢

Device:         rrqm/s   wrqm/s   r/s   w/s   rsec/s   wsec/s avgrq-sz avgqu-sz   await  svctm  %util
sdb1 0.01 0.44 0.24 0.57 3.44 8.14 14.34 0.00 2.28 0.66 0.05

代码:

#include <iostream>
#include <fstream>
#include <pthread.h>

using namespace std;

void* work(void* a)
{
int* id = (int*)a;
string file = "sys.partition";
char buf[100];
sprintf(buf, "%d", *id);
file.append(string(buf));
ifstream in(file.c_str());
in.seekg(0, ios_base::end);
size_t len = in.tellg();

cout << "open file : " << file << " , " << len << endl;
srand(time(NULL));

while(true)
{
size_t pos = rand() % len;
in.seekg(pos);
//cout << pos << endl;
in.read(buf, 10);
system("sync");
}
in.close();
}

int main(int argc, char** argv)
{
static const int num = 40;
pthread_t threads[num];
for (int i = 0; i < num; i++) {
pthread_create(&threads[i], NULL, work, &i);
}
for (int i = 0; i < num; i++) {
pthread_join(threads[i], NULL);
}
return 0;
}

最佳答案

%util 在 iostat 的源代码中被命名为 busy:https://code.google.com/p/tester-higkoo/source/browse/trunk/Tools/iostat/iostat.c#380

忙碌被计算为 Ticksdeltams 的百分比,限制为 100%

busy = 100.0 * blkio.ticks / deltams; /* percentage! */
if (busy > 100.0) busy = 100.0;

DeltaMS 是一段时间内系统负载的总和(用户时间 + 系统时间 + 空闲时间 + iowait)/ncpu。

double deltams = 1000.0 *
((new_cpu.user + new_cpu.system +
new_cpu.idle + new_cpu.iowait) -
(old_cpu.user + old_cpu.system +
old_cpu.idle + old_cpu.iowait)) / ncpu / HZ;

Ticks - 是该时间段内队列中请求的时间

blkio.ticks = new_blkio[p].ticks
- old_blkio[p].ticks;

在最新版本的 sysstat 中,代码有点不同: http://sources.debian.net/src/sysstat/10.2.0-1/iostat.c#L959

/*       rrq/s wrq/s   r/s   w/s  rsec  wsec  rqsz  qusz await r_await w_await svctm %util */
printf(" %8.2f %8.2f %7.2f %7.2f %8.2f %8.2f %8.2f %8.2f %7.2f %7.2f %7.2f %6.2f %6.2f\n",
...
/*
* Again: Ticks in milliseconds.
* In the case of a device group (option -g), shi->used is the number of
* devices in the group. Else shi->used equals 1.
*/
shi->used ? xds.util / 10.0 / (double) shi->used
: xds.util / 10.0); /* shi->used should never be null here */

xds 填入compute_ext_disk_stats(&sdc, &sdp, itv, &xds); http://sources.debian.net/src/sysstat/10.2.0-1/common.c?hl=679#L679

/*
* Macros used to display statistics values.
*
* HZ is 1024 on IA64 and % should be normalized to 100.
*/
#define S_VALUE(m,n,p) (((double) ((n) - (m))) / (p) * HZ)

xds->util = S_VALUE(sdp->tot_ticks, sdc->tot_ticks, itv);

还有来自iostat.c的tot_ticks的填充

  * @ioi        Current sample statistics.
* @ioj Previous sample statistics.
* @itv Interval of time.
...

sdc.tot_ticks = ioi->tot_ticks;
sdp.tot_ticks = ioj->tot_ticks;

tot_ticks 是从 read_sysfs_file_stat ( iostat.c:487 ) 中的“sysfs stat for current block device or partition”读取的,和 ioiioj 是当前和以前的统计数据。

关于linux - 如何计算 iostat 的效用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4458183/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com