Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ MT-multithreading and servers » Set thread priority for linux
Set thread priority for linux [message #36495] Sat, 02 June 2012 18:50 Go to next message
tojocky is currently offline  tojocky
Messages: 607
Registered: April 2008
Location: UK
Contributor

Hello,

Having some experience with thread priority on linux I propose to implent Thread::Priority(int);

Ok the implementation for linux is:
....
#ifdef PLATFORM_POSIX
	int policy, res;
	struct sched_param param;
	
	if ((res = pthread_getschedparam(handle, &policy, &param)) != 0){
		return;
		//TODO: should returns the error
	}
	if(percent <= 25){
		policy = SCHED_IDLE;
		param.sched_priority = (sched_get_priority_max(policy) - sched_get_priority_min(policy))*percent/25;
	}else if(percent <= 75){
		policy = SCHED_BATCH;
		param.sched_priority = (sched_get_priority_max(policy) - sched_get_priority_min(policy))*(percent-25)/50;
	}else if(percent <= 125){
		policy = SCHED_OTHER;
		param.sched_priority = (sched_get_priority_max(policy) - sched_get_priority_min(policy))*(percent-75)/50;
	}else if(percent <= 175){// should be a root
		policy = SCHED_FIFO;
		param.sched_priority = (sched_get_priority_max(policy) - sched_get_priority_min(policy))*(percent-125)/50;
	}else{
		policy = SCHED_RR;
		param.sched_priority = (sched_get_priority_max(policy) - sched_get_priority_min(policy))*(minmax(percent, 175, 225)-175)/25;
	}
	
	if ((res = pthread_setschedparam(handle, policy, &param)) != 0){
		return;
		//TODO: should returns the error
	}
#endif
....


and the hole method:
void Thread::Priority(int percent)
{
	ASSERT(IsOpen());
#ifdef PLATFORM_WIN32
	int prior;
	if(percent <= 25)
		prior = THREAD_PRIORITY_LOWEST;
	else if(percent <= 75)
		prior = THREAD_PRIORITY_BELOW_NORMAL;
	else if(percent <= 125)
		prior = THREAD_PRIORITY_NORMAL;
	else if(percent <= 175)
		prior = THREAD_PRIORITY_ABOVE_NORMAL;
	else
		prior = THREAD_PRIORITY_HIGHEST;
	SetThreadPriority(handle, prior);
#endif
#ifdef PLATFORM_POSIX
	int policy, res;
	struct sched_param param;
	
	if ((res = pthread_getschedparam(handle, &policy, &param)) != 0){
		return;
		//TODO: should returns the error
	}
	if(percent <= 25){
		policy = SCHED_IDLE;
		param.sched_priority = (sched_get_priority_max(policy) - sched_get_priority_min(policy))*percent/25;
	}else if(percent <= 75){
		policy = SCHED_BATCH;
		param.sched_priority = (sched_get_priority_max(policy) - sched_get_priority_min(policy))*(percent-25)/50;
	}else if(percent <= 125){
		policy = SCHED_OTHER;
		param.sched_priority = (sched_get_priority_max(policy) - sched_get_priority_min(policy))*(percent-75)/50;
	}else if(percent <= 175){// should be a root
		policy = SCHED_FIFO;
		param.sched_priority = (sched_get_priority_max(policy) - sched_get_priority_min(policy))*(percent-125)/50;
	}else{
		policy = SCHED_RR;
		param.sched_priority = (sched_get_priority_max(policy) - sched_get_priority_min(policy))*(minmax(percent, 175, 225)-175)/25;
	}
	
	if ((res = pthread_setschedparam(handle, policy, &param)) != 0){
		return;
		//TODO: should returns the error
	}
#endif
}


Any comments are welcome!

Best regards,
Ion.

[Updated on: Sat, 02 June 2012 18:58]

Report message to a moderator

Re: Set thread priority for linux [message #36504 is a reply to message #36495] Sun, 03 June 2012 13:15 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
I would like to add this patch, but (Ubuntu 10.04):

/home/cxl/upp.src/uppsrc/Core/Mt.cpp: In member function ‘void Upp::Thread::Priority(int)’:
/home/cxl/upp.src/uppsrc/Core/Mt.cpp:255:12: error: ‘SCHED_IDLE’ was not declared in this scope

...I guess more work is needed to resolve all compatiblity issues.

It has to work with Debian since 5, FreeBSD, MacOSX.

Mirek
Re: Set thread priority for linux [message #36507 is a reply to message #36504] Sun, 03 June 2012 15:17 Go to previous messageGo to next message
tojocky is currently offline  tojocky
Messages: 607
Registered: April 2008
Location: UK
Contributor

mirek wrote on Sun, 03 June 2012 14:15

I would like to add this patch, but (Ubuntu 10.04):

/home/cxl/upp.src/uppsrc/Core/Mt.cpp: In member function ‘void Upp::Thread::Priority(int)’:
/home/cxl/upp.src/uppsrc/Core/Mt.cpp:255:12: error: ‘SCHED_IDLE’ was not declared in this scope

...I guess more work is needed to resolve all compatiblity issues.

It has to work with Debian since 5, FreeBSD, MacOSX.

Mirek


You are right,
Sorry, I don't use old versions.

Please try this one:
void Thread::Priority(int percent)
{
	ASSERT(IsOpen());
#ifdef PLATFORM_WIN32
	int prior;
	if(percent <= 25)
		prior = THREAD_PRIORITY_LOWEST;
	else if(percent <= 75)
		prior = THREAD_PRIORITY_BELOW_NORMAL;
	else if(percent <= 125)
		prior = THREAD_PRIORITY_NORMAL;
	else if(percent <= 175)
		prior = THREAD_PRIORITY_ABOVE_NORMAL;
	else
		prior = THREAD_PRIORITY_HIGHEST;
	SetThreadPriority(handle, prior);
#endif
#ifdef PLATFORM_POSIX
	int policy, res;
	struct sched_param param;
	
	if ((res = pthread_getschedparam(handle, &policy, &param)) != 0){
		return;
		//TODO: should returns the error
	}
	int percen_min, percen_max;
	if(percent <= 25){
		#if defined(SCHED_IDLE)
			policy = SCHED_IDLE;
			percen_min = 0;
			percen_max = 25;
		#elif defined(SCHED_BATCH)
			policy = SCHED_BATCH;
			percen_min = 0;
			percen_max = 75;
		#else
			policy = SCHED_OTHER;
			percen_min = 0;
			percen_max = 125;
		#endif
	}else if(percent <= 75){
		#if defined(SCHED_IDLE)
			policy = SCHED_BATCH;
			percen_min = 25;
			percen_max = 75;
		#elif defined(SCHED_BATCH)
			policy = SCHED_BATCH;
			percen_min = 0;
			percen_max = 75;
		#else
			policy = SCHED_OTHER;
			percen_min = 0;
			percen_max = 125;
		#endif
	}else if(percent <= 125){
		policy = SCHED_OTHER;
		#if defined(SCHED_IDLE)
			percen_min = 75;
			percen_max = 125;
		#elif defined(SCHED_BATCH)
			percen_min = 25;
			percen_max = 125;
		#else
			percen_min = 0;
			percen_max = 125;
		#endif
	}else if(percent <= 175){// should be a root
		policy = SCHED_FIFO;
		percen_min = 125;
		percen_max = 175;
	}else{
		policy = SCHED_RR;
	}
	param.sched_priority = (sched_get_priority_max(policy) - sched_get_priority_min(policy))*(minmax(percent, percen_min, percen_max)-percen_min)/(percen_max - percen_min);
	
	if ((res = pthread_setschedparam(handle, policy, &param)) != 0){
		// don't have privileges? I'm trying maxim possible! I do not use EPERM because not all os support this one
		policy = SCHED_OTHER;
		percen_max = 125;
		percen_min = minmax(percen_min, 0, percen_max);
		param.sched_priority = (sched_get_priority_max(policy) - sched_get_priority_min(policy))*(minmax(percent, percen_min, percen_max)-percen_min)/(percen_max - percen_min);
		if ((res = pthread_setschedparam(handle, policy, &param)) != 0){
			return;
			//TODO: should returns the error
		}
	}
#endif
}


I have checked this functionality for MacOS, iOS, Linux and freebsd if it exists. My code I compared with QT.

Thank you Mirek!

Ion.
Re: Set thread priority for linux [message #36555 is a reply to message #36507] Fri, 08 June 2012 19:29 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Applied. Thanks.
Re: Set thread priority for linux [message #36558 is a reply to message #36555] Fri, 08 June 2012 20:26 Go to previous messageGo to next message
tojocky is currently offline  tojocky
Messages: 607
Registered: April 2008
Location: UK
Contributor

mirek wrote on Fri, 08 June 2012 20:29

Applied. Thanks.

Thank you Mirek!
Re: Set thread priority for linux [message #36559 is a reply to message #36555] Fri, 08 June 2012 22:12 Go to previous message
tojocky is currently offline  tojocky
Messages: 607
Registered: April 2008
Location: UK
Contributor

mirek wrote on Fri, 08 June 2012 20:29

Applied. Thanks.


Mirek,

It is my mistake, please change from "percen_" into "percent_".

Thank you in advance.
Previous Topic: Doubt with timeout in HttpRequest
Next Topic: Http file download question
Goto Forum:
  


Current Time: Thu Mar 28 19:23:27 CET 2024

Total time taken to generate the page: 0.01851 seconds