如何读取 /proc/interrupts 验证 IRQ 平衡关闭?

解读

在国内 Linux 服务器或嵌入式岗位面试中,“关中断亲和”是排查高延迟、网络抖动、实时性问题的必考题。
/proc/interrupts 是内核以
文本格式
导出的每个 IRQ 在各 CPU 上的累计中断次数。
若IRQ 平衡关闭,期望看到指定 IRQ 只在某一个 CPU 列上有持续增长,其余 CPU 列保持 0 或不再变化;反之若开启 irqbalance,则各 CPU 列增长趋于均匀。
面试官通常追问三点:

  1. 如何原子采样两次文件内容并计算差值;
  2. 如何程序化解析避免 shell 文本处理带来的竞态;
  3. 如何验证 affinity mask 与观察值一致。
    用纯 C 实现可体现指针运算、零拷贝读取、字符串流处理等底层功力,是系统级岗位的高频考点。

知识点

  1. /proc 伪文件系统:无阻塞顺序读取,打开后需一次性读完,否则需重新 open 保证快照一致性。
  2. 中断行格式:
    CPU0 CPU1 ... CPUn
    IRQ: count0 count1 ... countn type device_name
    列数随 CPU 核数动态变化,解析须先读标题行确定列数。
  3. IRQ 亲和设置接口:/proc/irq/IRQ/smp_affinity 为十六进制位掩码,最低位对应 CPU0。
  4. 验证逻辑:
    • 关闭 irqbalance 服务:systemctl stop irqbalance
    • 手动绑核:echo 02 > /proc/irq/24/smp_affinity # 仅 CPU1 可接收
    • 两次采样 /proc/interrupts,差值非零的 CPU 必须只有 mask 中为 1 的那一位。
  5. 编码细节:
    • 使用read + mmap 不可取,/proc 不支持 mmap;
    • 采用静态大缓冲区(如 8 KiB)一次性 read;
    • 用strtoul解析每列,避免 sscanf 性能陷阱;
    • 采样间隔**≥1 ms即可,但需用clock_gettime(CLOCK_MONOTONIC)** 记录精确时差,体现严谨性。

答案

下面给出可直接在面试白板手写的最小化 C 实现,突出指针游走、零拷贝、差值判断:

#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#define BUF_SZ 8192
#define MAX_CPU 128

static unsigned long long old_cnt[MAX_CPU];
static unsigned long long new_cnt[MAX_CPU];
static int cpu_nr;

/* 一次性读取整个文件,返回字节数 */
static int read_whole(int fd, char *buf, int size)
{
    int total = 0, n;
    while ((n = read(fd, buf + total, size - total)) > 0) total += n;
    return total;
}

/* 解析标题行,确定 CPU 列数 */
static void parse_header(const char *p)
{
    cpu_nr = 0;
    while (*p == ' ') p++;
    while (*p && *p != '\n') {
        if (strncmp(p, "CPU", 3) == 0) cpu_nr++;
        while (*p && *p != ' ' && *p != '\n') p++;
        while (*p == ' ') p++;
    }
}

/* 解析指定 IRQ 行,填充 cnt 数组 */
static int parse_irq_line(const char *p, int target_irq, unsigned long long *cnt)
{
    int irq;
    if (sscanf(p, "%d:", &irq) != 1 || irq != target_irq) return -1;
    p = strchr(p, ':') + 1;
    for (int i = 0; i < cpu_nr; i++) {
        while (*p == ' ') p++;
        cnt[i] = strtoull(p, (char **)&p, 10);
    }
    return 0;
}

int main(int argc, char **argv)
{
    if (argc != 2) { dprintf(2, "usage: %s <irq>\n", argv[0]); return 1; }
    int target = atoi(argv[1]);
    char buf[BUF_SZ];

    /* 第一次采样 */
    int fd = open("/proc/interrupts", O_RDONLY | O_CLOEXEC);
    if (fd < 0) { perror("open"); return 1; }
    int len = read_whole(fd, buf, sizeof(buf));
    close(fd);
    buf[len] = '\0';
    char *p = strchr(buf, '\n');          /* 跳过标题 */
    parse_header(buf);
    while ((p = strchr(p + 1, '\n'))) {
        if (parse_irq_line(p + 1, target, old_cnt) == 0) break;
    }

    /* 休眠 100 ms 产生差值 */
    struct timespec ts = {.tv_sec = 0, .tv_nsec = 100000000};
    nanosleep(&ts, NULL);

    /* 第二次采样 */
    fd = open("/proc/interrupts", O_RDONLY | O_CLOEXEC);
    len = read_whole(fd, buf, sizeof(buf));
    close(fd);
    buf[len] = '\0';
    p = strchr(buf, '\n');
    while ((p = strchr(p + 1, '\n'))) {
        if (parse_irq_line(p + 1, target, new_cnt) == 0) break;
    }

    /* 打印差值并判断平衡是否关闭 */
    int active_cpu = -1, ok = 1;
    for (int i = 0; i < cpu_nr; i++) {
        unsigned long long d = new_cnt[i] - old_cnt[i];
        printf("CPU%d: %llu\n", i, d);
        if (d > 0) {
            if (active_cpu == -1) active_cpu = i;
            else ok = 0;               /* 多于一个 CPU 有增长 */
        }
    }
    if (ok && active_cpu >= 0)
        printf("**IRQ%d 平衡已关闭,仅 CPU%d 处理中断**\n", target, active_cpu);
    else
        printf("**IRQ%d 仍被多 CPU 处理,平衡未关闭或亲和设置失败**\n", target);
    return 0;
}

编译:gcc irq_check.c -o irq_check
运行:sudo ./irq_check 24
输出示例:

CPU0: 0
CPU1: 327
**IRQ24 平衡已关闭,仅 CPU1 处理中断**

若看到多 CPU 非零,即可判定亲和设置未生效或 irqbalance 仍在运行。

拓展思考

  1. 生产环境常把网卡中断绑到最后几个核,避免抢业务线程;如何自动选择 NUMA 本地核?
    提示:结合 /sys/class/net/eth0/device/numa_node 与 /proc/cpuinfo 的 physical id,用CPU_SET 构造 cpuset,再 write 到 smp_affinity_list。
  2. 若中断号大于 256(MSI-X),/proc/interrupts 行数可能过万,O(n) 逐行扫描性能下降;如何优化?
    可预分配哈希表按 IRQ 号索引,或改用get_msi_irq_info 等内核接口,但需 root 且带 CAP_SYS_ADMIN。
  3. 容器场景下 /proc/interrupts 显示的是宿主机全局视图,而容器 cpuset 仅限制进程调度域;如何向面试官解释**“容器内看到的 IRQ 列增长”并不违背隔离?
    关键点:/proc 未命名空间化,中断计数仍全局可见,但容器线程
    无法迁移到未授权 CPU**,故亲和设置对容器无效,需宿主机侧统一调优。