- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我搜索了有关 CPU 亲和性的问题/答案并阅读了结果,但我仍然无法让我的线程锁定单个 CPU。
我正在开发一个将在专用 Linux 机器上运行的应用程序,因此我不关心其他进程,只关心我自己的进程。该应用程序当前生成一个 pthread,然后主线程进入 while 循环以使用 POSIX 消息队列处理控制消息。这个 while 循环阻塞等待控制消息进入然后处理它。所以主线程非常简单并且不重要。我的代码工作得很好,因为我可以发送这个应用程序消息,它会很好地处理它们。所有控制消息的大小都非常小,仅用于控制应用程序的功能,即仅发送/接收少量控制消息。
在进入此 while 循环之前,我使用 sched_getaffinity() 来记录所有可用的 CPU。然后我使用 sched_setaffinity() 将此进程设置为单个 CPU。然后我再次调用 sched_getaffinity() 来检查它是否设置为仅在一个 CPU 上运行,并且确实是正确的。
生成的单个 pthread 执行类似的操作。我在新创建的 pthread 中做的第一件事是调用 pthread_getaffinity_np() 并检查可用的 CPU,然后调用 pthread_setaffinity_np() 将其设置为不同的 CPU,然后调用 pthread_getaffinity_np() 检查它是否按需要设置并且确实正确。
这就是令人困惑的地方。当我运行应用程序并在系统监视器中查看 CPU 历史记录时,我发现与在没有所有这些设置关联性内容的情况下运行应用程序时没有任何区别。调度程序仍然在该四核机器上的 4 个 CPU 中运行几秒钟。因此看来调度程序忽略了我的关联设置。
我期望看到主线程和 pthread 实际上在它们自己的单个 CPU 中运行的证据,这是错误的吗?或者我是否忘记做更多的事情来让它按我的预期工作?
谢谢
-安德烈斯
最佳答案
你没有答案,我会尽我所能:一些部分帮助
假设您检查了 pthread_setaffinity_np 的返回值:
如何分配 cpuset 非常重要,在主线程中创建它。为了你想要的。它将传播到连续的线程。您检查返回码了吗?
您实际获得的 cpuset 将是硬件可用 cpu 与您定义的 cpuset 的交集。下面代码中的 min.h
是通用构建包含文件。您必须定义_GNU_SOURCE
- 请注意代码最后一行的注释。
CPUSET
和CPUSETSIZE
是宏。我想我在其他地方定义了它们,我不记得了。它们可能位于标准 header 中。
#define _GNU_SOURCE
#include "min.h"
#include <pthread.h>
int
main(int argc, char **argv)
{
int s, j;
cpu_set_t cpuset;
pthread_t tid=pthread_self();
// Set affinity mask to include CPUs 0 & 1
CPU_ZERO(&cpuset);
for (j = 0; j < 2; j++)
CPU_SET(j, &cpuset);
s = pthread_setaffinity_np(tid, sizeof(cpu_set_t), &cpuset);
if (s != 0)
{
fprintf(stderr, "%d ", s);
perror(" pthread_setaffinity_np");
exit(1);
}
// lets see what we really have in the actual affinity mask assigned our thread
s = pthread_getaffinity_np(tid, sizeof(cpu_set_t), &cpuset);
if (s != 0)
{
fprintf(stderr, "%d ", s);
perror(" pthread_setaffinity_np");
exit(1);
}
printf("my cpuset has:\n");
for (j = 0; j < CPU_SETSIZE; j++)
if (CPU_ISSET(j, &cpuset))
printf(" CPU %d\n", j);
// @Andres note: any pthread_create call from here on creates a thread with the identical
// cpuset - you do not have to call it in every thread.
return 0;
}
关于linux - 为什么系统监视器不显示正确的 CPU 关联性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20535789/
这个问题已经有答案了: Why does the expression a = a + b - ( b = a ) give a sequence point warning in c++? (4 个
有人可以解释以下代码片段的输出行为吗? function Vertex(x, y, z) { this.x = parseInt(x); this.y = parseInt(y);
理论上的输入/输出流运算符关联性: 从左到右 (例如,根据这个:Sait Mary's University website 输入/输出流运算符关联性实践: #include int func0()
根据 Hoogle,>= (bar <=< baz) 无积分,它给了我 bar <=< baz =<< foo 考虑到固定性,这看起来不太正确。 最佳答案 Frege 就像 Haskell,但 Fre
我正在使用任务集工具为我的一个程序设置 CPU 关联性。如何仅在单个 CPU 上设置亲和性 - 因为我对此不确定,所以我这样做: taskset -c 2-2 tests/prog 1 2 3 ...
我搜索了有关 CPU 亲和性的问题/答案并阅读了结果,但我仍然无法让我的线程锁定单个 CPU。 我正在开发一个将在专用 Linux 机器上运行的应用程序,因此我不关心其他进程,只关心我自己的进程。该应
我想知道linux进程是否可以使用默认的亲和性。默认值是 ~0(截断为可用 CPU 的数量),但我希望能够为系统的所有进程设置它。在启动时执行此操作也很好,这样我就可以有效地防止任何进程使用某些 CP
在 C99 标准中,表达式允许优先级和关联性。 优先级被很好地记录下来,因为文档中运算符出现的顺序是降低优先级的,所以函数调用在乘法运算符之前,而乘法运算符又在加法运算符之前。 但是,我找不到关于结合
我想将应用程序设置“ARR Affinity”配置为在通过我们的 ARM 模板配置新的应用程序服务时关闭。我该怎么做? 我找不到任何关于此的信息,这表明目前尚不支持它。 最佳答案 您正在 Micros
我想指定特定 pthread 的 CPU 亲和性。到目前为止我找到的所有引用资料都涉及设置进程(pid_t)而不是线程(pthread_t)的CPU亲和性。我尝试了一些传递 pthread_t 的实验
我在具有 2 个实例的云服务中拥有一个 Web 角色。 我有一个函数在一个实例中创建文件并切换到另一个中间函数,导致 404 错误,因为它找不到文件。 在 Azure Web Apps 中,Azure
F# pipe-forward 可以表示为: let (|>) x f = f x 例如: let SimpleFunction (a : typeA) (b : typeB) (c : typeC)
我有一个 HTTPS 负载均衡器,配置了一个后端服务和 3 个实例组: 端点协议(protocol):HTTPS 命名端口:https 超时:600 秒 运行状况检查:ui-health2 sessi
这个问题已经有答案了: Java thread affinity (5 个回答) 已关闭 6 年前。 有一些第三方库出于许可目的检查 CPU 数量,但没有明显的方式告诉他们“使用 1 个核心”。 为了
我们正在 Heroku 中基于 NodeJS 和 Websockets (MeteorJS) 运行生产操作几个月,使用配置为 session affinity 的 3 个实例。 。现在,我们想要从 H
我是一名优秀的程序员,十分优秀!