c - 用C有效迁移一个linux进程

标签 c linux kernel process-migration

我需要估算将 linux 进程迁移到同一台计算机的另一个内核上的成本。为了迁移我正在使用 sched_setaffinity 系统调用的进程,但我注意到迁移并不总是立即发生,这是我的要求。

更深入地说,我正在创建一个 C 程序,每次进行两次大量简单计算,第一次没有迁移,第二次有迁移。计算两个时间戳之间的差异应该可以让我粗略估计迁移开销。但是,我需要弄清楚如何迁移当前进程并等待迁移发生

#define _GNU_SOURCE
#define _POSIX_C_SOURCE 199309L

#include <assert.h>
#include <sched.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <stdint.h>

//Migrates the process
int migrate(pid_t pid) {
    const int totCPU = 8;
    const int nextCPU = (sched_getcpu() +1) % totCPU;

    cpu_set_t target;
    CPU_SET(nextCPU, &target);
    if(sched_setaffinity(pid, sizeof(target), &target) < 0)
        perror("Setaffinity");

    return nextCPU;
}

int main(void) {
    long i =0;
    const long iterations = 4;
    uint64_t total_sequential_delays = 0;
    uint64_t total_migration_delays = 0;
    uint64_t delta_us;

    for(int i=0; i < iterations; i++) {
        struct timespec start, end;

        //Migration benchmark only happens in odd iterations
        bool do_migration = i % 2 == 1;
        //Start timestamp
        clock_gettime(CLOCK_MONOTONIC_RAW, &start);
        //Target CPU to migrate
        int target;
        if(do_migration) {
            target = migrate(0);
            //if current CPU is not the target CPU
            if(target != sched_getcpu()) {
                do {
                    clock_gettime(CLOCK_MONOTONIC_RAW, &end);
                }
                while(target != sched_getcpu());
            }
        }

        //Simple computation 
        double k = 5;
        for(int j = 1; j <= 9999; j++) {
            k *= j / (k-3);
        }

        //End timestamp
        clock_gettime(CLOCK_MONOTONIC_RAW, &end);

        //Elapsed time
        delta_us = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000;
        if(do_migration) total_migration_delays += delta_us;
        else total_sequential_delays += delta_us;
    }

    //Compute the averages
    double avg_migration = total_migration_delays / iterations;
    double avg_sequential = total_sequential_delays / iterations;

    //Print them
    printf("\navg_migration=%f, avg_sequential=%f",avg_migration,avg_sequential);

    return EXIT_SUCCESS;
}

这里的问题是 do-while 循环(第 46-49 行)有时会永远运行。

最佳答案

I need to estimate how much it costs to migrate a linux process on another core of the same computer.

OK,成本可以估算为:

  • 设置新 CPU 亲和性并执行“yield”或“sleep(0)”以强制任务切换/重新安排所花费的时间(包括任务切换开销等) .

  • 每次“缓存在旧 CPU 上但尚未缓存在新 CPU 上”内存访问的缓存未命中成本

  • 每次 future “虚拟到物理转换已缓存在旧 CPU 中但尚未缓存在新 CPU 中”内存访问的 TLB 未命中成本

  • NUMA 惩罚

  • 负载平衡问题(例如,从“轻载”CPU 或核心迁移到“被其他进程重载”的 CPU 或核心可能会导致严重的性能问题,包括内核决定将其他进程迁移到不同的成本用于修复负载平衡的 CPU,其中其他进程支付的成本/开销可能应该包含在迁移进程造成的总成本中)。

注意:

a) 有多个级别的缓存(跟踪缓存、指令缓存、L1 数据缓存、L2 数据缓存,..)并且一些缓存在一些 CPU 之间共享(例如,L1 可能在同一核心内的逻辑 CPU 之间共享, L2 可能由 2 个内核共享,L3 可能由 8 个内核共享)。

b) TLB 未命中成本取决于很多因素(例如,如果内核在没有 PCID 功能的情况下使用 Meltdown 缓解措施并且无论如何都会在每次系统调用时清除 TLB 信息)。

c) NUMA 惩罚是延迟成本 - 每次访问分配在前一个 CPU 上的 RAM(例如缓存未命中)(对于前一个 NUMA 节点)将比访问分配在新/当前 CPU 上的 RAM 具有更高的延迟CPU(正确的 NUMA 节点)。

d) 所有缓存未命中成本、TLB 未命中成本和 NUMA 惩罚都取决于内存访问模式。没有内存访问的基准会产生误导。

e) 缓存未命中成本、TLB 未命中成本和 NUMA 惩罚高度依赖于所涉及的硬件 - 例如一台“具有快速 RAM 且无 NUMA 的慢速 CPU”计算机的基准测试与另一台“具有慢速 RAM 和许多 NUMA 域的快速 CPU”计算机完全无关。同样,它高度依赖于哪些 CPU(例如,从 CPU #0 迁移到 CPU #1 可能花费很少,而从 CPU #0 迁移到 CPU #15 可能非常昂贵)。

To migrate the process I'm using the sched_setaffinity system call, but i've noticed that migration does not always happens instantaneously, which is my requirement.

在“sched_setaffinity();”之后放置一个“sleep(0);”。

关于c - 用C有效迁移一个linux进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58613008/

相关文章:

c - 在通过 execv 执行的子进程中使用 Scanf() 不起作用

regex - 使用 bash 计算子树中特定目录的数量

c - 与缓存行对齐并了解缓存行大小

matlab - 针对大数据优化 matlab for 循环

c - 运行时间或新想法

c - STM32 的 DMA 到闪存

C 函数指针错误

java - Bash 脚本运行包含外部文件的 jar 不起作用

c - fork.c :764: request for member `list' in something not a structure or union

console - 在没有控制台的情况下使用现有的 ipython 内核运行 python 脚本