【问题标题】:Is OS X have any kernel function similar to on_each_cpu() in linuxOS X 是否有任何类似于 linux 中的 on_each_cpu() 的内核函数
【发布时间】:2020-06-17 07:45:44
【问题描述】:

我想在每个处理器上执行功能。

OS X 是否有类似 linux 中的on_each_cpu() 的内核函数?

【问题讨论】:

  • 如果您只是在寻找一些常见的 Linux 和 macOS 解决方案,我可能会考虑OpenMP。但是,在 macOS 中,我们通常会从这个级别的细节中抽象出来,例如像concurrentPerform 这样的例程非常适合跨所有可用内核并行化例程,而不会冒线程爆炸的风险。也许您可以告诉我们您真正想要解决的更广泛的问题,我们或许可以为您提供更好的建议。
  • @Rob:问题不在于用户空间应用程序的功能,而在于操作系统内核。在 Linux 内核中,on_each_cpu 不用于加速计算,而是用于所有 CPU 的整合状态。例如,用于更新 per-CPU 变量。我猜提问者希望该功能用于类似目的,但在 OS X 内核中。
  • 我不知道有任何此类功能,当然不是公共 KPI(可由 3rd 方 kexts 使用)。也许您可以解释一下您要做什么,我们可以尝试找到不同的解决方案?

标签: macos kernel xnu


【解决方案1】:

你想要函数mp_cpus_call:

/*
 * mp_cpus_call() runs a given function on cpus specified in a given cpu mask.
 * Possible modes are:
 *  SYNC:   function is called serially on target cpus in logical cpu order
 *      waiting for each call to be acknowledged before proceeding
 *  ASYNC:  function call is queued to the specified cpus
 *      waiting for all calls to complete in parallel before returning
 *  NOSYNC: function calls are queued
 *      but we return before confirmation of calls completing.
 * The action function may be NULL.
 * The cpu mask may include the local cpu. Offline cpus are ignored.
 * The return value is the number of cpus on which the call was made or queued.
 */
cpu_t
mp_cpus_call(
    cpumask_t       cpus,
    mp_sync_t       mode,
    void            (*action_func)(void *),
    void            *arg)

请注意,它不会导出到 kexts(或至少非 Apple 的),因为它位于为 Apple kexts 保留的 com.apple.kpi.apple 后面。您仍然可以通过尝试resolve the symbol at runtime 或尝试使您的 kext 看起来像 Apple 来调用它(在 macOS 11 上很难)。我个人使用动态分辨率,但如果这是你想要发布的 kext,那么你大多不走运。

【讨论】:

    猜你喜欢
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    相关资源
    最近更新 更多