gpt4 book ai didi

c++ - 使用 pthread_setschedparam 显示 htop 中线程的负优先级

转载 作者:太空狗 更新时间:2023-10-29 21:41:56 27 4
gpt4 key购买 nike

我在 centOS 6.5 版内核 3.4.102-1 x86_64 自定义构建的/etc/security/limits.conf 中进行了以下设置

*             -       rtprio          99 

当我为名为 PThreadAffinity 的测试代码运行下面的代码时,下面显示了线程的进程和优先级(向右滚动以查看树)。为什么优先级没有按照代码中指定的方式显示?这意味着在 main() 中分配给以下线程的优先级是 39、1、20 和 2。尽管在 htop 中它们显示为 -3、-21、-2 和 -40。

30009 bjackfly   20   0  303M  1036   864 S  0.0  0.0  0:00.00 │  │        └─ ./PThreadAffinity     
30013 bjackfly -3 0 303M 1036 864 S 0.0 0.0 0:00.00 │ │ ├─ ./PThreadAffinity
30012 bjackfly -21 0 303M 1036 864 S 0.0 0.0 0:00.00 │ │ ├─ ./PThreadAffinity
30011 bjackfly -2 0 303M 1036 864 S 0.0 0.0 0:00.00 │ │ ├─ ./PThreadAffinity
30010 bjackfly -40 0 303M 1036 864 S 0.0 0.0 0:00.00 │ │ └─ ./PThreadAffinity

来源:

       #include <thread>
#include <pthread.h>
#include <sstream>
#include <cstring>
#include <iostream>
#include <stdexcept>

std::string schedAttrAsStr(const int aPolicy, const sched_param &aParam) {
std::stringstream ss;
ss << ((aPolicy == SCHED_FIFO) ? "SCHED_FIFO" :
(aPolicy == SCHED_RR) ? "SCHED_RR" :
(aPolicy == SCHED_OTHER) ? "SCHED_OTHER" :
"???")
<< " @ " << aParam.sched_priority;
return ss.str();
}

void createManaged(std::string aName, int aCpuNum, int aPriority, int aPolicy) {
cpu_set_t cpus;
CPU_ZERO(&cpus);
CPU_SET(aCpuNum, &cpus);
int err;
if((err = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpus)) != 0) {
std::ostringstream os;
os << "ERROR: Could not set affinity for cpu " << aCpuNum << "\n";
std::cout << os.str() << std::endl;
}

// check if we have an exsiting schedule parameter
sched_param oldParam;
int oldPolicy;
if(pthread_getschedparam(pthread_self(), &oldPolicy, &oldParam) == 0) {
std::ostringstream os;
os << "Threader old param for " << aName << " is " << schedAttrAsStr(oldPolicy, oldParam) << "\n";
}


sched_param param;
memset(&param,0,sizeof(param));
param.sched_priority = aPriority;
if (aPriority > sched_get_priority_max(aPolicy) || aPriority < sched_get_priority_min(aPolicy) ) {
std::ostringstream os;
os << "Priority: " << aPriority << " is out of range for Policy: " << schedAttrAsStr(aPolicy, param) << "\n";
std::cout << os.str() << std::endl;
throw std::runtime_error(os.str().c_str());
}

int ret = pthread_setschedparam(pthread_self(), aPolicy, &param);
if(ret != 0) {
std::ostringstream os;
std::cout << " Failed to set scheduler parameters for: " << aName << "\n";
os << os.str() << std::endl;
throw std::runtime_error(os.str().c_str());
}
else
{
std::ostringstream os;
os << "Threader successfully set scheduler parameters for " << aName << " thread to " << schedAttrAsStr(aPolicy, param) <<
"\n";
std::cout << os.str() << std::endl;;
}

// Verify new
if(pthread_getschedparam(pthread_self(), &oldPolicy, &oldParam) == 0) {
std::ostringstream os;
os << "Threader new param for " << aName << " is " << schedAttrAsStr(oldPolicy, oldParam) << "\n";
std::cout << os.str() << std::endl;
}
else {
std::ostringstream os;
os << " Threader Failed to get new parameters for: " << aName << "\n";
std::cout << os.str() << std::endl;
}
};

void runData(int aThreshold, std::string aName, int aCpuNum, int aPriority, int aPolicy) {
createManaged(aName,aCpuNum,aPriority,aPolicy);
std::chrono::milliseconds timespan(aThreshold);
std::this_thread::sleep_for(timespan);

std::ostringstream os;
os << "Done Processing ThreadID: " << std::this_thread::get_id() << "\n";
std::cout << os.str() << std::endl;
}

int main() {
std::thread thr1(runData,900000,"Thread1",1,39,SCHED_FIFO);
std::thread thr2(runData,900000,"Thread2",2,1,SCHED_FIFO);
std::thread thr3(runData,900000,"Thread3",3,20,SCHED_FIFO);
std::thread thr4(runData,900000,"Thread4",4,2,SCHED_FIFO);

thr1.join();
thr2.join();
thr3.join();
thr4.join();
}

最佳答案

tl;dr - procfs 将实时优先级报告为 1-prio,但您的代码实际上工作正常。

这主要是因为 /proc 文件系统的复杂性。

首先,您将优先级向后映射(查看 htop 输出中的进程 ID)。真正的映射如下:

Thread1: Prio 39, reported -40
Thread2: Prio 1 , reported -2
Thread3: Prio 20, reported -21
Thread4: Prio 2 , reported -3

htop 从每个进程的 procfs 条目下名为 stat 的文件获取其信息,例如您的线程信息将从 /proc/30010/stat 中读取。在manpage for procfs是以下内容:

/proc/[pid]/stat Status information about the process. This is used by ps(1). It is defined in /usr/src/linux/fs/proc/array.c.

...

(18) (Explanation for Linux 2.6) For processes running a real-time scheduling policy (policy below; see sched_setscheduler(2)), this is the negated scheduling priority, minus one; that is, a number in the range -2 to -100, corresponding to real-time priorities 1 to 99. For processes running under a non-real-time scheduling policy, this is the raw nice value (setpriority(2)) as represented in the kernel. The kernel stores nice values as numbers in the range 0 (high) to 39 (low), corresponding to the user-visible nice range of -20 to 19.

关于c++ - 使用 pthread_setschedparam 显示 htop 中线程的负优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27576043/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com