none
Clock Interrupt

    Question

  • Hi,

    A thread is allowed to run a certain number of quantum in Windows. This check is performed by the Kernel whenever the clock interrupt happens. The question I have is, will the logic to perform this check happens within the clock interval itself? If so, will the thread get only a part of its each clock interval to do its work?

    Thanks,
    Suresh.
    Sunday, November 1, 2009 1:04 AM

Answers

  • Well, the tick count indeed counts the number of clock interrupts, but clock in this case really refers to a time source like the PIC, the APIC or in todays architectures one of the HPET timers. These programmable timers have their own clock cycle that is independent from that of the CPU. So, if Mark refers to the system clock, he means one of those time sources. 

    There is a good reason for using a different time source. Let's do some theoretic example. Imagine the clock interrupt really occurs after every clock cycle of the CPU. That would mean that the CPU, after executing one instruction of the application, would have to execute the clock ISR which consists of 50 or more instructions. And this in turn means that the application has virtually no progress because the CPU is all the time busy with handling the clock interrupt. So it is better to have the timer interrupt occur every x clock cycles, where x depends on the frequency of the CPU. Also, for measuring time, the clock cycle may not be a good choice to track progress in time as the frequency of the clock cycle can change if the CPU goes into a power saving mode.

    Best regards,
    Alex

    http://www.dcl.hpi.uni-potsdam.de/research/WRK
    Wednesday, November 4, 2009 10:00 AM

All replies

  • Hi Suresh,

    you're right, the time for executing the logic is "charged" to the currently active thread. If you have access to the WRK, you can see that behavior by looking at the KeUpdateSystemTime() function in base\ntos\ke\architecture \clockint.asm. Right at the end of the assembler code another function is called: KeUpdateSystemTime which is responsible for decreasing the quantum count. If the quantum value of thread get less or equal to zero a software interrupt is triggered to activate the dispatching code. This, however, is executed not as part of the clock interrupt but rather at the DISPATCH IRQL.

    Personally, I believe, although I never measured it, that the quantum decrement logic does not steal any significant computing time of the running thread, as a timer interrupt only occurs every 15 ms and the quantum check is only a couple of processor instructions. If you do some measurements on your own to analyze the ratio, please keep me posted.

    Best regards,
    Alex

    http://www.dcl.hpi.uni-potsdam.de/research/WRK
    Monday, November 2, 2009 7:57 PM
  • Hi Alex,

    Thanks for your reply and can you please let me know how can I get access to WRK?

    Some more related questions.

    1. Lets say that the Kernel detects the Quantum for the current thread is expired and decides to perform context switch. The IRQL is at DPC/Dispatch now and the Kernel code must execute to find a suitable thread and perform the switch. Now, the question is how can the Kernel code execute? All the processor registers are still filled with the previous thread's data (the thread for which the quatum expires). Will it be moved to a temporary location so that the registers can be used by the kernel code to do dispatching? 

    2. And a question related to the above point. When re-storing the state of the context switched thread, how can the kernel set the values of PC, IR etc., as it is filled with kernel's data to perform the switch.

    3. I'm surprised to know that clock interrupt occurs every 15 ms. My understanding is, a clock interrupt will occur on each cycle of the system clock. Say, if the System Clock speed is 2 MHZ, then there are 2,000,000 cycles/second and a clock interrupt occurs during each cycle (so 2,000,000 times). Is this wrong? If it occurs every 15 ms, then there are only 67 (aprox) clock interrupts per second. Please clarify.


    Thanks,
    Suresh.
    Tuesday, November 3, 2009 12:25 AM
  • Suresh,

    the WRK contains the source code of the Windows Server 2003 operating system kernel (ntoskrnl.exe). The WRK is available to universisties only, which means you can get it if:

    (1) You are a faculty member of a university: In that case you can download the WRK from the Faculty Connection Web site .
    (2) You are currently a student: In that case you can get access only through a professor of your university.
    (3) You don't fall in any of the above categories: You, unfortunately, cannot get access to the WRK.

    As your questions are concerned:

    (1) I am not 100% sure of the details, but if I remember correctly, Windows installs a generic interrupt handler that saves the state of the CPU by creating a trap frame on the stack and then calls the respective interrupt service routine (ISR). The ISR can then be executed without corrupting the current thread.

    (2) Basically, when dispatching another thread, alle the kernel does is switching the stack from one thread to another. As I pointed out above, the top of the stack contains the trap frame and therefore the current state of the thread. If you switch to another stack, the stack contains the trap frame of the new thread. When the dispatch routine returns the generic interrupt handler will update all processor registers as defined in the trap frame.

    (3) Yes, it is wrong. Imagine that the clock interrupt occurs at every cycle of the processor. In that case your processor would most of the time be busy with updating the system time, quantum counting, etc. No actual work would be done. This is why the clock interrupt is triggered from a seperate, programmable timer that fires an interrupt in certain intervals, e.g. 15 ms. Sometimes, the clock interrupt fires every millisecond. The OS is free to program it in way it considers appropriate.

    Best regards,
    Alex
    http://www.dcl.hpi.uni-potsdam.de/research/WRK
    Tuesday, November 3, 2009 8:03 PM
  • Hi Alex,

    Thanks again for your reply. I'm not going to have access to WRK as I work as a C# developer, but that's fine.

    I'm still surprised with the clock interrupt. I'm reading the Windows Internals (4th edition) book and here is what it says

    'The kernel most frequently uses a DPC to handle quantum expiration. At every tick of the system clock, an interrupt occurs at clock IRQL. The clock interrupt handler (running at clock IRQL) updates the system time and then decrements a counter that tracks how long the current thread has run. '

    Isn't every tick refers here a clock cycle??

    Thanks again for your time.

    Thanks,
    Suresh.
    Tuesday, November 3, 2009 10:09 PM
  • Alex,

    If you think this question is unrelated to this forum, can you please direct me to some Windows OS forums?

    Thanks,
    Suresh.
    Tuesday, November 3, 2009 10:14 PM
  • Well, the tick count indeed counts the number of clock interrupts, but clock in this case really refers to a time source like the PIC, the APIC or in todays architectures one of the HPET timers. These programmable timers have their own clock cycle that is independent from that of the CPU. So, if Mark refers to the system clock, he means one of those time sources. 

    There is a good reason for using a different time source. Let's do some theoretic example. Imagine the clock interrupt really occurs after every clock cycle of the CPU. That would mean that the CPU, after executing one instruction of the application, would have to execute the clock ISR which consists of 50 or more instructions. And this in turn means that the application has virtually no progress because the CPU is all the time busy with handling the clock interrupt. So it is better to have the timer interrupt occur every x clock cycles, where x depends on the frequency of the CPU. Also, for measuring time, the clock cycle may not be a good choice to track progress in time as the frequency of the clock cycle can change if the CPU goes into a power saving mode.

    Best regards,
    Alex

    http://www.dcl.hpi.uni-potsdam.de/research/WRK
    Wednesday, November 4, 2009 10:00 AM
  • Thanks Alex. I understand it better now.

    And, Can I continue posting my questions on Windows kernel in this forum?

    Thanks,
    Suresh.
    Wednesday, November 4, 2009 6:16 PM
  • Hi Suresh,

    I am glad I could be of some help.

    Although this forum is supposed for WRK related questions and its usage for teaching and research, I think it is a perfect place as well for all questions related to the Windows kernel. So, if you have any further Windows kernel questions, feel free to post them here.

    Alex
    http://www.dcl.hpi.uni-potsdam.de/research/WRK
    Wednesday, November 4, 2009 7:23 PM
  • Thanks Alex.
    Wednesday, November 4, 2009 8:59 PM