Thread: priority changing : CPU time?

  1. #1
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251

    priority changing : CPU time?

    Could you suggest some relevant tests for CPU time necessary for temporarily changing thread priority?? (links or figures)
    Thank you

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It's a system call, so it is not just a couple of instructions. The actual code to change the priority of is a simple pointer-to-structure update, with some checking around it. I'd say that on a reasonably modern processor, less than a microsecond for sure.

    However, I'd question the validity of (often) switching thread priority - perhaps it should always be the higher priority then?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by matsp View Post
    It's a system call, so it is not just a couple of instructions. The actual code to change the priority of is a simple pointer-to-structure update, with some checking around it. I'd say that on a reasonably modern processor, less than a microsecond for sure.

    However, I'd question the validity of (often) switching thread priority - perhaps it should always be the higher priority then?

    --
    Mats
    it is related to the other thread we discussed about (priority inversion due to lock/unlock)
    so this dynamical change of priority would happen not so often (well not even rarely..)

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, what you are saying is that you are not actually changing the priority very often, but you want to change it when you take the mutex? And how often does that ACTUALLY happen? Once an hour, once every five seconds, or some other number?

    --
    Mas
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by matsp View Post
    So, what you are saying is that you are not actually changing the priority very often, but you want to change it when you take the mutex? And how often does that ACTUALLY happen? Once an hour, once every five seconds, or some other number?

    --
    Mas
    say once every 50 msec (more or less)

  6. #6
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    the Linux I am using is embedded with LinuxThreads instead of NPTL (unfortunately!!)

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mynickmynick View Post
    the Linux I am using is embedded with LinuxThreads instead of NPTL (unfortunately!!)
    So process == thread aside from memory mapping.

    Every 50 msec is pretty often, when you consider that the scheduling quanta is 10ms. I don't think this thread should be low priority at all.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    i am talking of SCHED_FF or SCHED_RR threads

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mynickmynick View Post
    i am talking of SCHED_FF or SCHED_RR threads
    Do you mean SCHED_FIFO? SCHED_RR is definitely going to (potentially at least) run threads in a different order than strict priority - I think SCHED_FIFO does too, but in a different non-strict order.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by matsp View Post
    Do you mean SCHED_FIFO? --
    Mats
    yes!

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right. So I'm still 90% sure that having different priorities is the WRONG solution.

    I bet one of your threads is really CPU intensive, another is IO-intensive, and the third one is just doing some other random things. This sort of scheme generally fails when the scheduler decides to schedule the tasks in a different way than you expect them to be scheduled. The way around that is to do your own scheduling enforcement [it still doesn't prevent some random process from getting in and doing stuff, but it allows you to control which of YOUR processes runs next]. The other option is to entirely disconnect the threads from each other, such as using pipes or message passing mechanisms that are (for all intents and purposes) lock-free.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. priority inversion due to pthread_mutex_lock(): how to avoid?
    By mynickmynick in forum C Programming
    Replies: 11
    Last Post: 04-07-2009, 10:34 AM
  3. Portable method of accessing CPU time for given process
    By saeculum in forum Linux Programming
    Replies: 3
    Last Post: 03-13-2009, 03:44 PM
  4. need help in time zone
    By Gong in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2007, 04:44 AM
  5. CPU time / thread tool
    By Carlos in forum Windows Programming
    Replies: 2
    Last Post: 01-31-2003, 09:11 AM