# IO系统调用耗时采集场景示例

简要示范全流程使用动态源码优化工具对源码文件进行IO采集分析，以test_directory_ops_daemon.c源码文件为例，采集IO系统调用耗时数据。

#### 前提条件

以动态源码优化工具安装路径“/home/DevKit-Optimizer-CLI-x.x.x-Linux-Kunpeng”为例。


#### 操作步骤

1. 编译源码文件，生成可执行二进制文件。以源码文件存放路径“/home/test/test_directory_ops_daemon.c”为例，请根据实际情况进行修改。

```
gcc -o test_directory_ops_daemon test_directory_ops_daemon.c -O2 -g
```


  test_directory_ops_daemon.c文件内容如下：

```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#define BASE_DIR "/tmp/io_test_dir_ops"
static volatile int g_running = 1;
static double g_mkdir_total_us = 0.0, g_rmdir_total_us = 0.0;
static double g_unlink_total_us = 0.0, g_rename_total_us = 0.0;
static int g_mkdir_count = 0, g_rmdir_count = 0;
static int g_unlink_count = 0, g_rename_count = 0;
static int g_seq = 0;
static double get_time_us(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000000.0 + ts.tv_nsec / 1000.0;
}
static void handle_signal(int sig)
{
g_running = 0;
}
int main(int argc, char *argv[])
{
signal(SIGTERM, handle_signal);
signal(SIGINT, handle_signal);
mkdir(BASE_DIR, 0755);
printf("=== Directory Ops Daemon (PID=%d) ===\n", getpid());
fflush(stdout);
while (g_running) {
int i = __sync_fetch_and_add(&g_seq, 1);
char dirpath[512], dstpath[512], filepath[512];
int basefd = open(BASE_DIR, O_RDONLY | O_DIRECTORY);
/* --- mkdir + rmdir --- */
snprintf(dirpath, sizeof(dirpath), "%s/mkdir_%d_%d", BASE_DIR, getpid(), i);
double t0 = get_time_us();
mkdir(dirpath, 0755);
double t1 = get_time_us();
g_mkdir_total_us += (t1 - t0);
g_mkdir_count++;
t0 = get_time_us();
rmdir(dirpath);
t1 = get_time_us();
g_rmdir_total_us += (t1 - t0);
g_rmdir_count++;
/* --- mkdirat + rmdir --- */
if (basefd != -1) {
char dn[256];
snprintf(dn, sizeof(dn), "mkdirat_%d_%d", getpid(), i);
t0 = get_time_us();
mkdirat(basefd, dn, 0755);
t1 = get_time_us();
g_mkdir_total_us += (t1 - t0);
g_mkdir_count++;
snprintf(dirpath, sizeof(dirpath), "%s/%s", BASE_DIR, dn);
t0 = get_time_us();
rmdir(dirpath);
t1 = get_time_us();
g_rmdir_total_us += (t1 - t0);
g_rmdir_count++;
}
/* --- unlink --- */
snprintf(filepath, sizeof(filepath), "%s/unlink_%d_%d", BASE_DIR, getpid(), i);
{
int fd = open(filepath, O_CREAT | O_WRONLY, 0644);
if (fd != -1)
close(fd);
}
t0 = get_time_us();
unlink(filepath);
t1 = get_time_us();
g_unlink_total_us += (t1 - t0);
g_unlink_count++;
/* --- unlinkat --- */
if (basefd != -1) {
char fn[256];
snprintf(fn, sizeof(fn), "unlinkat_%d_%d", getpid(), i);
snprintf(filepath, sizeof(filepath), "%s/%s", BASE_DIR, fn);
{
int fd = open(filepath, O_CREAT | O_WRONLY, 0644);
if (fd != -1)
close(fd);
}
t0 = get_time_us();
unlinkat(basefd, fn, 0);
t1 = get_time_us();
g_unlink_total_us += (t1 - t0);
g_unlink_count++;
}
/* --- rename --- */
snprintf(filepath, sizeof(filepath), "%s/ren_src_%d_%d", BASE_DIR, getpid(), i);
snprintf(dstpath, sizeof(dstpath), "%s/ren_dst_%d_%d", BASE_DIR, getpid(), i);
{
int fd = open(filepath, O_CREAT | O_WRONLY, 0644);
if (fd != -1)
close(fd);
}
t0 = get_time_us();
rename(filepath, dstpath);
t1 = get_time_us();
g_rename_total_us += (t1 - t0);
g_rename_count++;
unlink(dstpath);
/* --- renameat --- */
if (basefd != -1) {
char sn[256], dnn[256];
snprintf(sn, sizeof(sn), "renat_src_%d_%d", getpid(), i);
snprintf(dnn, sizeof(dnn), "renat_dst_%d_%d", getpid(), i);
snprintf(filepath, sizeof(filepath), "%s/%s", BASE_DIR, sn);
{
int fd = open(filepath, O_CREAT | O_WRONLY, 0644);
if (fd != -1)
close(fd);
}
t0 = get_time_us();
renameat(basefd, sn, basefd, dnn);
t1 = get_time_us();
g_rename_total_us += (t1 - t0);
g_rename_count++;
snprintf(dstpath, sizeof(dstpath), "%s/%s", BASE_DIR, dnn);
unlink(dstpath);
}
if (basefd != -1)
close(basefd);
usleep(1000);
}
printf("\n=== Baseline ===\n");
if (g_mkdir_count > 0)
printf("mkdir/mkdirat avg=%.2f us count=%d\n",
g_mkdir_total_us / g_mkdir_count, g_mkdir_count);
if (g_rmdir_count > 0)
printf("rmdir avg=%.2f us count=%d\n",
g_rmdir_total_us / g_rmdir_count, g_rmdir_count);
if (g_unlink_count > 0)
printf("unlink/unlinkat avg=%.2f us count=%d\n",
g_unlink_total_us / g_unlink_count, g_unlink_count);
if (g_rename_count > 0)
printf("rename/renameat avg=%.2f us count=%d\n",
g_rename_total_us / g_rename_count, g_rename_count);
fflush(stdout);
rmdir(BASE_DIR);
return 0;
}
```


2. 在“/home/test”目录执行二进制文件。

```
./test_directory_ops_daemon
```


  返回以下信息：

```
=== Directory Ops Daemon (PID=239466) ===
```


3. 保持程序运行，进入源码优化工具目录，指定test_directory_ops_daemon进程的PID，采集IO系统调用耗时数据，生成数据文件。

```
cd
/home/DevKit-Optimizer-CLI-x.x.x-Linux-Kunpeng
./devopt.sh record -p 239466 -d 5 -o /home/test
```


  进程PID为239466，采集时间指定为5s，在“/home/test”下生成数据文件。

  返回信息如下：

```
Saved the record data to /home/test/devopt_239466_20260817113411.rawdata
```


4. 启用IO精细化采集模式，基于数据文件进行IO系统调用耗时分析。

```
./devopt.sh record -p 239466 -d 5 --io -i /home/test/devopt_239466_20260817113411.rawdata
```


5. 以使用report子命令查看分析结果为例，执行以下命令查看分析结果。

```
./devopt.sh report -i /home/test/devopt_239466_20260817113411.rawdata
```


  返回信息如下：

  鼠标点击“IO”数据标识或者按数字键“2”切换到IO面板，IO面板展示信息如下图所示。

  考虑到性能开销的影响当前默认调用栈解析方案是参照Perf的FP（Frame Pointer，帧指针）方式进行栈解析，因此当点击某行IO数据查看调用栈详情时，栈帧从libc的syscall wrapper直接跳到“__libc_start_call_main”，中间缺失“main”函数。这是因为调用栈采集使用FP链回溯，glibc中部分syscall wrapper（如“mkdir”、“close”、“rename”）被编译为无帧指针函数，导致FP链在该处断裂，“main”的栈帧无法被正确识别。

**表1 工具总览界面IO采集区域说明**

| 区域 | 名称 | 说明 |
| --- | --- | --- |
| 1 | 表格区域 | 展示源码文件中的函数信息，其字段介绍请参见表2。支持通过表格区域中的搜索框快速查询函数信息。 |
| 2 | 调用栈区域 | 展示函数的调用信息，支持通过快捷键“e”进行折叠与展开。 |
| 3 | 提示区域 | 展示IO系统调用耗时采集数据信息。 |
| 4 | 快捷键区域 | 展示快捷键信息，其介绍请参见表3。 |


**表2 表格区域字段介绍**

| 字段 | 字段说明 |
| --- | --- |
| func | 触发IO系统调用者的函数名称。 |
| posix | POSIX系统调用名称。 |
| file | 调用者函数所在源文件的名称。 |
| module | 调用者函数所属模块。 |
| percentage(%) | 函数耗时占总耗时的百分比。 |
| avg | IO系统调用函数的平均耗时。 |
| min | IO系统调用函数的最小耗时。 |
| max | IO系统调用函数的最大耗时。 |
| P99 | 99%分位耗时。表示99%的函数耗时不超过此值。 |


**表3 快捷键介绍**

| 快捷键 | 功能说明 |
| --- | --- |
| Ctrl+C | 退出当前界面，返回命令行界面。 |
| Tab | 切换工具界面内的选中区域。 |
| Enter | 进入当前光标选中数据的明细页面。 |
| F1 | 在选中区域内部进行切换。 |
| / | 选中搜索框。 |
| 空格 | 显示数据页大小切换框。 |
| h | 显示帮助信息。 |
| e | 在调用栈区域折叠或展开函数的调用信息。 |
| ↑/↓ | 纵向滚动条向上/向下滚动。 |
| ←/→ | 横向滚动条向左/向右滚动。 |
| PageDown/PageUp | 纵向滚动条向下/向上快速滚动。 |
| q/Esc | 从工具的明细界面返回总览界面。 |


6. 根据工具总览界面中提示区域的内容，选中对应的IO数据条目，使用“Enter”键可查看IO系统调用耗时采集的详细数据内容，具体内容如下图所示。
