I have the following set up in /etc/security/limits.conf on a centOS version 6.5 kernel 3.4.102-1 custom build for x86_64
* - rtprio 99
When I run the code below for a test code named PThreadAffinity below shows the threads the process and the priority (scroll right to see the tree). Why are the priorities not showing up as specified in the code?
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(¶m,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, ¶m);
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();
}
Aucun commentaire:
Enregistrer un commentaire