使用funcgraph-retval和bpftrace/kprobe快速定位並解決cpu控制器無法使能的問題

来源:https://www.cnblogs.com/pengdonglin137/archive/2023/12/06/17880808.html
-Advertisement-
Play Games

版本 Linux 6.5 背景 在學習cgroupv2的時候,想給子cgroup開啟cpu控制器結果失敗了: # 查看可以開啟哪些控制器 root@ubuntu-vm:/sys/fs/cgroup# cat cgroup.controllers cpuset cpu io memory hugetl ...


版本

Linux 6.5

背景

在學習cgroupv2的時候,想給子cgroup開啟cpu控制器結果失敗了:

# 查看可以開啟哪些控制器
root@ubuntu-vm:/sys/fs/cgroup# cat cgroup.controllers
cpuset cpu io memory hugetlb pids rdma misc

# 上面看到,是支持cpu控制器的,通過下麵命令查看目前子cgroup開啟了哪些控制器
root@ubuntu-vm:/sys/fs/cgroup# cat cgroup.subtree_control
memory pids

# 通過下麵的命令給子cgroup開啟cpu控制器
root@ubuntu-vm:/sys/fs/cgroup# echo +cpu > cgroup.subtree_control
-bash: echo: write error: Invalid argument

在給子cgroup開啟cpu控制器時提示參數無效,即-EINVAL,錯誤碼是-22.

定位

之前給linux內核的function graph增加了顯示函數返回值的功能,正好可以派上用場。

  • 使用下麵的命令配置ftrace
echo 0 > /sys/kernel/debug/tracing/tracing_on
echo 14080 > /sys/kernel/debug/tracing/buffer_size_kb
echo ksys_write > /sys/kernel/debug/tracing/set_graph_function
echo $$ > /sys/kernel/debug/tracing/set_ftrace_pid
echo 1 > /sys/kernel/debug/tracing/options/funcgraph-retval
echo 1 > /sys/kernel/debug/tracing/options/funcgraph-retval-trim
echo function_graph > /sys/kernel/debug/tracing/current_tracer

目前社區版本還不支持funcgraph-retval-trim,這個是為了對返回值進行裁剪

然後使用下麵的方法抓取log:

> /sys/kernel/debug/tracing/trace;echo 1 > /sys/kernel/debug/tracing/tracing_on; echo +cpu > cgroup.subtree_control;echo 0 > /sys/kernel/debug/tracing/tracing_on

收集到trace日誌後,從上往下搜索-22錯誤碼,看到下麵的內容:

 4)               |                cgroup_migrate_execute() {
 4)               |                  cpu_cgroup_can_attach() {
 4)               |                    cgroup_taskset_first() {
 4)   0.190 us    |                      cgroup_taskset_next(); /* = 0xffff8881003b0000 */
 4)   0.551 us    |                    } /* cgroup_taskset_first = 0xffff8881003b0000 */
 4)   0.170 us    |                    sched_rt_can_attach(); /* = 0x1 */
 4)   0.180 us    |                    cgroup_taskset_next(); /* = 0xffff888100994e00 */
 4)   0.171 us    |                    sched_rt_can_attach(); /* = 0x1 */
 4)   0.180 us    |                    cgroup_taskset_next(); /* = 0xffff88810bed4e00 */
 4)   0.170 us    |                    sched_rt_can_attach(); /* = 0x1 */
 4)   0.191 us    |                    cgroup_taskset_next(); /* = 0xffff8881083d1a00 */
 4)   0.170 us    |                    sched_rt_can_attach(); /* = 0x1 */
 4)   0.170 us    |                    cgroup_taskset_next(); /* = 0xffff888108e20000 */
 4)   0.181 us    |                    sched_rt_can_attach(); /* = 0x0 */
 4)   4.248 us    |                  } /* cpu_cgroup_can_attach = -22 */

可以看到,cpu_cgroup_can_attach先返回了-22錯誤碼,具體分析源碼:

#ifdef CONFIG_RT_GROUP_SCHED
static int cpu_cgroup_can_attach(struct cgroup_taskset *tset)
{
	struct task_struct *task;
	struct cgroup_subsys_state *css;

	cgroup_taskset_for_each(task, css, tset) {
		if (!sched_rt_can_attach(css_tg(css), task))
			return -EINVAL;
	}
	return 0;
}
#endif

結合日誌和源碼,是由於sched_rt_can_attach返回了0,才會返回-EINVAL。

繼續查看sched_rt_can_attach:

int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
{
	/* Don't accept realtime tasks when there is no way for them to run */
	if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
		return 0;

	return 1;
}

返回0的條件:進程是實時進程,但是目的task group沒有給實時任務設置時間份額。

內核文檔中有下麵的描述:

WARNING: cgroup2 doesn't yet support control of realtime processes and the cpu controller can only be enabled when all RT processes are in the root cgroup. Be aware that system management software may already have placed RT processes into nonroot cgroups during the system boot process, and these processes may need to be moved to the root cgroup before the cpu controller can be enabled.

上面的意思是說,在開啟CPU控制器之前,需要首先將實時任務移動到根cgroup下。

那這裡是哪個實時進程導致的呢?sched_rt_can_attach函數的第二個參數就是task_struct地址,可以藉助bpftrace查看這個對應的哪個進程:

# cat trace.bt
#!/usr/bin/env bpftrace

kprobe:sched_rt_can_attach
{
        printf("task: %lx, comm: %s\n", arg1, ((struct task_struct *)arg1)->comm);
}

運行上面的腳本,然後再次執行開啟CPU控制器的操作,可以看到下麵的日誌:

root@ubuntu-vm:~# ./trace.bt
Attaching 1 probe...
task: ffff8881003b0000, comm: systemd
task: ffff888107e38000, comm: agetty
task: ffff888107f3ce00, comm: agetty
task: ffff888107e39a00, comm: systemd-journal
task: ffff88810862b400, comm: multipathd

可以看到,最後一個進程是multipathd,這個進程是否為實時進程呢?

# ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,comm | grep -E 'PID|multipathd'
    PID     TID CLS RTPRIO  NI PRI PSR %CPU STAT WCHAN          COMMAND
    153     153 RR      99   - 139   6  0.0 SLsl futex_wait_que multipathd

可以看到確實是實時進程。

下麵手動將這個進程加到根cgroup下:

root@ubuntu-vm:/sys/fs/cgroup# cat /proc/153/cgroup
0::/system.slice/multipathd.service

root@ubuntu-vm:/sys/fs/cgroup# echo 153 > cgroup.procs

root@ubuntu-vm:/sys/fs/cgroup# cat /proc/153/cgroup
0::/

然後再次開啟CPU控制器:

root@ubuntu-vm:/sys/fs/cgroup# echo +cpu > cgroup.subtree_control

root@ubuntu-vm:/sys/fs/cgroup# cat cgroup.subtree_control
cpu memory pids

到這裡,這個問題就解決了。

如果bpftrace不能用的話,可以使用kprobe_event,下麵是comm在task_struct中的偏移:

(gdb) p &((struct task_struct *)0)->comm
$1 = (char (*)[16]) 0x840

或者:

crash> *task_struct.comm -ox
struct task_struct {
   [0x840] char comm[16];
}

用下麵的命令添加kprobe_event,同時對ftrace進一步配置:

echo 'p sched_rt_can_attach $arg* +0x840($arg2):string' > dynamic_events
echo kprobe_ftrace_handler > /sys/kernel/debug/tracing/set_graph_notrace
echo 1 > events/kprobes/p_sched_rt_can_attach_0/enable

上面$arg*的用法是新版本的內核才有的,藉助BTF來獲取函數的入參,比之前方便多了,可以用來輸出函數的全部入參

這個方法跟funcgraph-retval結合起來,既實現了輸出內核函數的入參,同時也輸出了內核函數的返回值

再次按照之前的方法復現一次,可以抓到下麵的log:

2)               |                cgroup_migrate_execute() {
 2)               |                  cpu_cgroup_can_attach() {
 2)               |                    cgroup_taskset_first() {
 2)   0.190 us    |                      cgroup_taskset_next(); /* = 0xffff8881003b0000 */
 2)   0.581 us    |                    } /* cgroup_taskset_first = 0xffff8881003b0000 */
 2)               |                    sched_rt_can_attach() {
 2)               |                      /* p_sched_rt_can_attach_0: (sched_rt_can_attach+0x4/0x30) tg=0xffff88810a1b1c00 tsk=0xffff8881003b0000 arg3="systemd" */
 2)   4.529 us    |                    } /* sched_rt_can_attach = 0x1 */
 2)   0.291 us    |                    cgroup_taskset_next(); /* = 0xffff888107e38000 */
 2)               |                    sched_rt_can_attach() {
 2)               |                      /* p_sched_rt_can_attach_0: (sched_rt_can_attach+0x4/0x30) tg=0xffff88810a1b1880 tsk=0xffff888107e38000 arg3="agetty" */
 2)   1.603 us    |                    } /* sched_rt_can_attach = 0x1 */
 2)   0.251 us    |                    cgroup_taskset_next(); /* = 0xffff888107f3ce00 */
 2)               |                    sched_rt_can_attach() {
 2)               |                      /* p_sched_rt_can_attach_0: (sched_rt_can_attach+0x4/0x30) tg=0xffff88810a1b1880 tsk=0xffff888107f3ce00 arg3="agetty" */
 2)   1.413 us    |                    } /* sched_rt_can_attach = 0x1 */
 2)   0.241 us    |                    cgroup_taskset_next(); /* = 0xffff888107e39a00 */
 2)               |                    sched_rt_can_attach() {
 2)               |                      /* p_sched_rt_can_attach_0: (sched_rt_can_attach+0x4/0x30) tg=0xffff88810a1b1880 tsk=0xffff888107e39a00 arg3="systemd-journal" */
 2)   2.324 us    |                    } /* sched_rt_can_attach = 0x1 */
 2)   0.250 us    |                    cgroup_taskset_next(); /* = 0xffff88810862b400 */
 2)               |                    sched_rt_can_attach() {
 2)               |                      /* p_sched_rt_can_attach_0: (sched_rt_can_attach+0x4/0x30) tg=0xffff88810a1b1880 tsk=0xffff88810862b400 arg3="multipathd" */
 2)   2.014 us    |                    } /* sched_rt_can_attach = 0x0 */
 2) + 15.820 us   |                  } /* cpu_cgroup_can_attach = -22 */

kprobe_event的好處是,可以跟function_graph的日誌一塊結合起來看,也比較方便。上面的日誌顯示調用sched_rt_can_attach時,當進程是multipathd時,返回了0,進而導致cpu_cgroup_can_attach返回了-22.

本文來自博客園,作者:摩斯電碼,未經同意,禁止轉載


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • Options是微軟提供的選項模塊,該模塊依賴於容器使用。除了微軟的IServiceCollection,當然也可以使用其它的依賴註入容器。本文演示如何在prism中使用Options。 創建應用項目 創建一個Avalonia應用(或其它類型應用),然後使用NuGet包管理器添加Prism.DryI ...
  • 在.NET中,Microsoft.Extensions.Logging是一個廣泛使用的日誌庫,用於記錄應用程式的日誌信息。它提供了豐富的功能和靈活性,使開發人員能夠輕鬆地記錄各種類型的日誌,並將其輸出到不同的目標,包括日誌文件。本文將詳細介紹Microsoft.Extensions.Logging的 ...
  • 上篇文章講述了C#多線程知識點,本文將介紹C#處理文件的知識點。在.NET開發領域,文件系統I/O是一個至關重要的主題,尤其是在處理文件、目錄和數據存儲方面。C#作為.NET平臺的主要編程語言,提供了豐富而強大的文件系統I/O功能,為開發人員提供了靈活的工具,使其能夠高效地處理文件操作。本文將介紹C ...
  • CPF 是開源的C#跨平臺UI框架,支持使用OpenGL來渲染,可以用來硬體加速播放視頻或者顯示3D模型 實現原理其實就是Skia用OpenGL後端,Skia里綁定GLView的OpenGL紋理,將紋理作為Skia的圖像混合繪製。 在CPF里使用OpenGL,不能選擇NetCore3.0和Net4, ...
  • .net平臺使用SDK快速對接各大語言模型 1.項目介紹 最近,在做GPT項目時,發現各個平臺的介面規範和參數不同,需要根據不同平臺和模型寫多種介面,比較麻煩,不想Python那樣有豐富和方便的AI環境, 如果c#有一個SDK可以方便調用各種平臺模型就好了,這是AllInAI.Sharp.API萌芽 ...
  • WPF應用中,控制項本身也可以通過實現事件代碼實現拖動的處理,不過如果我們使用GongSolutions.WPF.DragDrop來處理,事情會變得更加簡單輕鬆,它支持很多控制項的拖動處理,如ListBox, ListView, TreeView, DataGrid等源自ItemsControl的控制項,... ...
  • 一、條件 1、windows server主機一臺,我是windows server 2019(當然windows10或者10月份5日更新的windows11也是可以行的)。 2、SQL Sever,我用的是SQL Server2019。 3、Power BI Report Server預設位置安裝 ...
  • Parallel.ForEach Parallel.ForEach 是一個用於在集合上並行執行迭代操作的強大工具。它通過有效地利用多核處理器的能力來提高性能。Parallel.ForEach 不僅能夠簡化並行編程,而且它在執行簡單迴圈時可以提供比傳統迭代更好的性能。 下麵是一個簡單的示例,演示瞭如何 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...