- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 Linux 中做一些时间计算测试。我的内核是 Preempt-RT(但是 vanilla 内核在我的测试中给出了类似的结果......)
我有两个 pthread,在同一个处理器中同时运行(给定亲和性)。它们是实时线程(prio 99)。
我有一个受自旋锁保护的关键部分,其中两个线程竞争锁。在关键部分内,我有一个增量操作,我尝试计算该操作的运行时间。
使用 __rdtsc 的代码示例:
pthread_spin_lock(&lock);
start_time = __rdtsc();
++cnt; //shared ram variable, type is unsigned long long
stop_time = __rdtsc();
pthread_spin_unlock(&lock);
计时码表示例:
pthread_spin_lock(&lock);
auto _start_time = std::chrono::high_resolution_clock::now();
++cnt; //shared ram variable, type is unsigned long long
auto _stop_time = std::chrono::high_resolution_clock::now();
pthread_spin_unlock(&lock);
线程循环运行几百万次然后终止。解锁自旋锁后,我记录了平均耗用时间和最大耗用时间。
现在,事情变得有趣了(至少对我而言):
测试 1:线程的调度策略为 SCHED_RR:
线程号:0,最大时间:34124,平均时间:28.114271,运行 Cnt:10000000
线程号:1,最长时间:339256976,平均时间:74.781960,运行次数:10000000
测试 2:线程的调度策略为 SCHED_FIFO:
线程号:0,最大时间:33114,平均时间:48.414173,运行 Cnt:10000000
线程号:1,最大时间:38637,平均时间:24.327742,运行 Cnt:10000000
测试3:只有单线程,调度策略为SCHED_RR:
线程号:0,最大时间:34584,平均时间:54.165470,运行 Cnt:10000000
注意:主线程是非 rt 线程,它在单独的处理器中具有亲和性。这里不重要。
注 1:所有测试给出大约。每次我运行它们时都会得到类似的结果。
注2:给出的结果是rdtsc的输出。然而,计时计时器的结果与这些几乎相似。
所以我想我可能对调度器有一个误解,所以我需要问这些问题:
我所有的测试代码是:
#include <stdio.h>
#include <stdlib.h>
#include "stdint.h"
#include <float.h>
#include <pthread.h>
#include <cxxabi.h>
#include <limits.h>
#include <sched.h>
#include <sys/mman.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <malloc.h>
#include <chrono>
/********* TEST CONFIG ************/
#define TEST_PTHREAD_RUN_CNT 10000000 //1000000000
#define NUM_OF_TEST_PTHREADS 2
#define MAIN_THREAD_CORE_INDEX 0
#define TEST_PTHREAD_PRIO 99
#define TEST_PTHREAD_POLICY SCHED_RR
#define TIME_RDTSC 1
#define TIME_CHRONO 0
/**********************************/
/**********************************/
struct param_list_s
{
unsigned int thread_no;
};
/**********************************/
/********* PROCESS RAM ************/
pthread_t threads[NUM_OF_TEST_PTHREADS];
struct param_list_s param_list[NUM_OF_TEST_PTHREADS];
unsigned long long max_time[NUM_OF_TEST_PTHREADS];
unsigned long long _max_time[NUM_OF_TEST_PTHREADS];
unsigned long long tot_time[NUM_OF_TEST_PTHREADS];
unsigned long long _tot_time[NUM_OF_TEST_PTHREADS];
unsigned long long run_cnt[NUM_OF_TEST_PTHREADS];
unsigned long long cnt;
pthread_spinlock_t lock;
/**********************************/
/*Proto*/
static void configureMemoryBehavior(void);
void create_rt_pthread(unsigned int thread_no);
/*
* Date............:
* Function........: main
* Description.....:
*/
int main(void)
{
cpu_set_t mask;
int i;
for (i = 0; i < NUM_OF_TEST_PTHREADS; ++i)
{
max_time[i] = 0;
tot_time[i] = 0;
run_cnt[i] = 0;
_max_time[i] = 0;
_tot_time[i] = 0;
}
cnt = 0;
printf("\nSetting scheduler affinity for the process...");
CPU_ZERO(&mask);
CPU_SET(MAIN_THREAD_CORE_INDEX, &mask);
sched_setaffinity(0, sizeof(mask), &mask);
printf("done.\n");
configureMemoryBehavior();
pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE);
for (i = 0; i < NUM_OF_TEST_PTHREADS; ++i)
{
create_rt_pthread(i);
}
printf("Waiting threads to join\n...\n");
for (i = 0; i < NUM_OF_TEST_PTHREADS; i++)
{
pthread_join(threads[i], NULL);
#if(TIME_RDTSC == 1)
printf("Thread no: %d, Max Time: %llu, Avg Time: %f, Run Cnt: %llu\n", i, max_time[i], (float)((float)tot_time[i] / run_cnt[i]), run_cnt[i]);
#endif
#if(TIME_CHRONO == 1)
printf("Thread no: %d, Max Time: %lu, Avg Time: %f, Run Cnt: %lu\n", i, _max_time[i], (float)((float)_tot_time[i] / run_cnt[i]), run_cnt[i]);
#endif
}
printf("All threads joined\n");
printf("Shared Cnt: %llu\n", cnt);
return 0;
}
/*
* Date............:
* Function........: thread_func
* Description.....:
*/
void *thread_func(void *argv)
{
unsigned long long i, start_time, stop_time, latency = 0;
unsigned int thread_no;
thread_no = ((struct param_list_s *)argv)->thread_no;
i = 0;
while (1)
{
#if(TIME_RDTSC == 1)
pthread_spin_lock(&lock);
start_time = __rdtsc();
++cnt;
stop_time = __rdtsc();
pthread_spin_unlock(&lock);
if (stop_time > start_time)
{
latency = stop_time - start_time;
++run_cnt[thread_no];
tot_time[thread_no] += latency;
if (latency > max_time[thread_no])
max_time[thread_no] = latency;
}
#endif
#if(TIME_CHRONO == 1)
pthread_spin_lock(&lock);
auto _start_time = std::chrono::high_resolution_clock::now();
++cnt;
auto _stop_time = std::chrono::high_resolution_clock::now();
pthread_spin_unlock(&lock);
auto __start_time = std::chrono::duration_cast<std::chrono::nanoseconds>(_start_time.time_since_epoch()).count();
auto __stop_time = std::chrono::duration_cast<std::chrono::nanoseconds>(_stop_time.time_since_epoch()).count();
auto __latency = __stop_time - __start_time;
if (__stop_time > __start_time)
{
_tot_time[thread_no] += __latency;
++run_cnt[thread_no];
if (__latency > _max_time[thread_no])
{
_max_time[thread_no] = __latency;
}
}
#endif
if (++i >= TEST_PTHREAD_RUN_CNT)
break;
}
return 0;
}
/*
* Date............:
* Function........: create_rt_pthread
* Description.....:
*/
void create_rt_pthread(unsigned int thread_no)
{
struct sched_param param;
pthread_attr_t attr;
printf("Creating a new real-time thread\n");
/* Initialize pthread attributes (default values) */
pthread_attr_init(&attr);
/* Set a specific stack size */
pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
/* Set scheduler policy and priority of pthread */
pthread_attr_setschedpolicy(&attr, TEST_PTHREAD_POLICY);
param.sched_priority = TEST_PTHREAD_PRIO;
pthread_attr_setschedparam(&attr, ¶m);
/* Set the processor affinity*/
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(1, &cpuset);
pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpuset);
/* Use scheduling parameters of attr */
pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
param_list[thread_no].thread_no = thread_no;
if(pthread_create(&threads[thread_no], &attr, thread_func, (void *)¶m_list[thread_no]) != 0)
{
printf("Thread could not be created.\n");
exit(-1);
}
}
/*
* Date............:
* Function........: configureMemoryBehavior
* Description.....:
*/
static void configureMemoryBehavior(void)
{
printf("\nLocking memory...");
/* Now lock all current and future pages
from preventing of being paged */
if (mlockall(MCL_CURRENT | MCL_FUTURE))
perror("mlockall failed:");
/* Turn off malloc trimming.*/
mallopt(M_TRIM_THRESHOLD, -1);
/* Turn off mmap usage. */
mallopt(M_MMAP_MAX, 0);
printf("done.\n");
}
最佳答案
当您使用 SCHED_FIFO
运行时,您的线程之一开始运行。然后它会一直运行直到完成——因为这就是 SCHED_FIFO
的工作方式——没有任何东西会抢占它。因此,它在自旋锁中花费的时间是相对一致的。然后,在第一个线程完成后,第二个线程在没有争用锁的情况下运行到完成。所以它也有一个更一致的时间。由于中断等原因,两者仍然存在一些抖动,但这两者之间相当一致。
当您使用 SCHED_RR
运行时,您的一个线程会运行一段时间。在一个时间片结束时,它会被抢占,另一个将开始运行——因为这就是 SCHED_RR
的工作方式。现在,它很有可能在持有自旋锁时被抢占。所以,现在另一个线程正在运行,它立即尝试获取自旋锁,但失败了——因为另一个线程持有锁。但它只是一直尝试直到时间片结束(因为自旋锁就是这样工作的——它永远不会阻塞等待获取锁)。当然,它在此期间什么也做不了。最终,时间片结束,持有锁的线程再次运行。但是归因于该单个增量操作的时间现在包括等待另一个线程在其整个时间片中自旋的所有时间。
我认为如果您增加最大计数 (TEST_PTHREAD_RUN_CNT
),您会看到 SCHED_RR
行为随着您的两者而变得均匀线程最终会受到这种影响。现在,我猜一个线程很有可能在一两个时间片内完成。
如果您想锁定在同一处理器上以同等优先级运行的另一个线程,您可能应该使用 pthread_mutex_t
。在成功获取的情况下,这与自旋锁的作用几乎相同,但在无法获取锁时会阻塞。
但请注意:这样做的结果很可能会将 SCHED_RR
行为转变为 SCHED_FIFO
行为:大多数情况下,抢占会在一个线程发生时发生持有锁,因此另一个将运行几条指令,直到它尝试获取锁,然后它将阻塞,第一个将再次运行一个完整的时间片。
总的来说,尝试在一个处理器上运行两个 RT 优先级线程确实很冒险,因为它们都需要运行很长时间。 RT 优先级在您将每个线程锁定到其自己的核心,或者 RT 线程需要立即得到调度但在再次阻塞之前只会运行很短时间的情况下效果最好。
关于c++ - 当调度策略为 SCHED_RR 时,pthread 临界区中运行时间峰值的原因可能是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55336272/
考虑以下示例代码: void main() { List array = []; for (int i = 0; i newList = array.where( (value) => v
我在java中实现了textrank,但它看起来很慢。有谁知道它的预期性能吗? 如果预计速度不会很慢,则可能是以下任一问题: 1) 似乎没有办法在 JGraphT 时间内创建一条边并同时为其添加权重,
我正在尝试提交解决方案(使用一些具有编译时间限制的在线编译器)来对数组进行排序 - 这是我的代码片段 - class TSORT { public static void main(Strin
只是一个困惑......部分C++示例代码如下 我只是重新编辑了整个帖子。抱歉造成任何混淆 int i, j; i = 0; // c1 j = 0; // c2 while (i
我正在使用 OpenMP 运行一些并行工作,但发现加速无法很好地扩展。然后我发现随着线程数量的增加,完成相同工作量的时间也会增加。这是示例: void fillingVec(vector& vec)
我目前正在分析 maximum subarray problem 用于强力算法和分而治之算法(递归)。 使用蛮力算法,最坏情况下的运行时间为 O(n^2)。使用递归算法,最坏情况下的运行时间为 O(n
如果我有两个数组都有 10000 个项目,现在我想将它们合并到一个数组中,所以我用 concat 来完成: array1=array1.concat(array2); 但是有人知道运行时间是多少吗?
{ clock_t t1, t2; double time1 = 0; t1 = clock(); bubbleSort(data, n); t2 = cloc
我在某处读到有人可以在运行时访问配置值,但不能在设计时访问。在这种情况下,运行时和设计时有什么区别? 最佳答案 设计时间是有人用愉快的“看起来不错!”在我们的 Word 文档和 UML 图表上签字的时
我正在比较我们一个项目的两个分支的性能,一个比另一个慢得多。我注意到其中一个的 GC 运行计数更高(见下图)。 更有趣的是,运行时间要长很多倍,远远超过额外运行所能解释的时间。什么可以解释运行次数增加
我想以可读的方式格式化 log4j 经过的运行时间,%r 参数: [00:36:25.844] 和 [01 13:35:25.844] [时:分:ss.SSS] ... [dd 时:分:ss.SSS]
如何计算此代码的大 O 运行时效率?我的直觉告诉我它是 O(n^3),但我不确定,因为我不确定循环是独立的还是相关的。 for (i=1; i<=n; i++) for (j=1; j<=n;
确定这些不同循环的大 O 运行时间? for i = 1 to n { ... for j = 1 to 2*i { ... k = j; while (k>=0)
运行时间、复杂性、编译时间和执行时间有什么区别? 运行时间与时间复杂度有冲突,执行时间和执行时间有什么区别? 最佳答案 您真正需要的是如何将大O时间复杂度转换为运行时。这不像一开始看起来那么容易。 因
斯内普的《Unfriendly Algorithms for Wizards》教科书声称合并的运行时间排序是 O(n^4)。这种说法是否正确? 解决方案:是的。这个说法在技术上是正确的,因为 O(n^
我有一系列问题需要反馈和答案。我会评论我的想法,这不是家庭作业而是准备为了我的考试。 我的主要问题是确定不同情况下循环的迭代。试图弄清楚这一点会如何? 评估运行时间。 Q2。 for(int i =
我试图找到一个合理的机会来测量我的 java 程序/程序部分的速度,例如测试两种方法中哪一种执行得更快。注释掉这两种方法中的一种,确定运行时间,最后比较时间。我不想在我的程序中使用任何代码,例如: p
好吧,这些都是非常简单的方法,而且有几个,所以当它们都是同一件事时,我不想只创建多个问题。 BigO 是我的弱点。我只是想不通他们是如何得出这些答案的。无论如何,您是否可以让我深入了解您对分析其中一些
如何计算此给定代码的 Theta 运行时间: void f(int n) { for (int i=3; i
使用 leiningen 创建 uberjar 并使用 java -jar foo-uberjar.jar 运行该 jar 后程序运行良好,最后一行代码执行得相当快,但程序在关闭前挂了大约一分钟。这是
我是一名优秀的程序员,十分优秀!