次の方法で共有


Optimize Source Code to Improve Your OS Design (Compact 2013)

3/26/2014

This topic discusses three optimization strategies that you can perform on your design or your source code: avoiding priority inversion, reducing thread switching, and optimizing code design. These optimizations are not meant to replace good software design practices, but to provide some tips for improving your OS design. As you design and build your OS image code, consider the following optimization and performance conditions so that you can reduce latency and performance issues in your system.

Priority Inversion

Sometimes a resource used by a lower-priority thread delays the running of a higher-priority thread that is waiting for the same resource. To free the higher-priority thread, the system triggers a process called priority inversion. The system inverts the priority level of the two threads, allowing the thread holding the resource to run at a higher priority until it releases its use of the resource.

For example, if you have two threads at different priorities as follows:

  • The NDIS IST thread runs at priority 116
  • The TCP send thread normally runs at priority 251

When the lower-priority TCP send thread has locked a resource that the higher-priority NDIS IST thread needs, the NDIS IST execution will switch the priority of the TCP send thread with the priority of the NDIS IST thread until the required resource is released. To estimate the impact of the priority inversion, manually switch the TCP send thread to priority 116 and lower the NDIS IST thread to priority 251 so no priority inversion occurs. In this example, priority inversion reduces performance by almost 50 percent.

Windows Embedded Compact 2013 uses priority inheritance to avoid priority inversion. You can also avoid priority inversion by implementing one or more of these solutions, depending on the needs of your system design:

  • Adjusting thread priorities.
  • Using TryEnterCriticalSection to synchronize resources.
  • Serializing your send/receive operations.

Thread Switching

Excessive thread switching slows system performance even when priority inversion is reduced or eliminated. Kernel Tracker indicates between 1 ms and 4 ms lag per interrupt.

You can use Kernel Tracker to view and analyze the activity that could be causing high CPU loads. A high number of hits in KCNextThread, DoWaitForObjects, WaitForSingleObject, or WaitForMultipleObjects, all located in kernel.dll, might indicate that many short instances of thread switching are occurring. When you see a high number of hits in these functions, use CeLog and Kernel Tracker to view the thread behavior to determine why threads are running for such short periods of time. You can then alter your code to make a thread switch run for a longer period of time.

You can also reduce thread switching by batching jobs and processing as much as possible before reenabling interrupts.

Design Code to Reduce CPU Consumption

To optimize your code design during development, minimize the number of times your system polls the power management component to reduce CPU consumption. Also, to ensure that your system does not repeatedly read the same data, consider caching bus traffic, CPU consumption, and speed data.

See Also

Concepts

Optimize Runtime Performance
Optimization