📄 cpuload-functions.html
字号:
<!-- Copyright (C) 2003 Red Hat, Inc. --><!-- This material may be distributed only subject to the terms --><!-- and conditions set forth in the Open Publication License, v1.0 --><!-- or later (the latest version is presently available at --><!-- http://www.opencontent.org/openpub/). --><!-- Distribution of the work or derivative of the work in any --><!-- standard (paper) book form is prohibited unless prior --><!-- permission is obtained from the copyright holder. --><HTML><HEAD><TITLE>CPU Load Measurements</TITLE><meta name="MSSmartTagsPreventParsing" content="TRUE"><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="eCos Reference Manual"HREF="ecos-ref.html"><LINKREL="UP"TITLE="CPU load measurements"HREF="services-cpuload.html"><LINKREL="PREVIOUS"TITLE="CPU load measurements"HREF="services-cpuload.html"><LINKREL="NEXT"TITLE="Application profiling"HREF="services-profile-gprof.html"></HEAD><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">eCos Reference Manual</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="services-cpuload.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="services-profile-gprof.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="CPULOAD-FUNCTIONS">Chapter 51. CPU Load Measurements</H1><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="SERVICES-CPULOAD-API">CPU Load API</H1><P>The package allows the CPU load to be estimated. The measurement codemust first be calibrated to the target it is running on. Once thishas been performed the measurement process can be started. This is acontinuous process, so always providing the most up to datameasurements. The process can be stopped at any time if required. Oncethe process is active, the results can be retrieved.</P><P>Note that if the target/processor performs any power saving actions,such as reducing the clock speed, or halting until the next interruptetc, these will interfere with the CPU load measurement. Under theseconditions the measurement results are undefined. The synthetic targetis one such system. See the implementation details at the foot of thispage for further information. </P><P>SMP systems are not supported, only uniprocessor system.</P><P> The API for load measuring functions can befound in the file <TTCLASS="FILENAME">cyg/cpuload/cpuload.h</TT>.</P><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="SERVICES-CPULOAD-API-CYG-CPULOAD-CALIBRATE">cyg_cpuload_calibrate</H2><P>This function is used to calibrate the cpu load measurement code. Itmakes a measurement to determine the CPU properties while idle.</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">void cyg_cpuload_calibrate(cyg_uint32 *calibration);</PRE></TD></TR></TABLE><P>The function returns the calibration value at the location pointed toby <TTCLASS="PARAMETER"><I>calibration</I></TT>.</P><P>This function is quite unusual. For it to work correctly a fewconditions must be met. The function makes use of the two highestthread priorities. No other threads must be using these prioritieswhile the function is being used. The kernel scheduler must be startedand not disabled. The function takes 100ms to complete during whichtime no other threads will be run.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="SERVICES-CPULOAD-API-CYG-CPULOAD-CREATE">cyg_cpuload_create</H2><P>This function starts the CPU load measurments.</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">void cyg_cpuload_create(cyg_cpuload_t *cpuload, cyg_uint32 calibrate, cyg_handle_t *handle);</PRE></TD></TR></TABLE><P>The measurement process is started and a handle to it is returned in<TTCLASS="PARAMETER"><I>*handle</I></TT>. This handle is used to access theresults and the stop the measurement process. </P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="SERVICES-CPULOAD-API-CYG-CPULOAD-DELETE">cyg_cpuload_delete</H2><P>This function stops the measurement process. </P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">void cyg_cpuload_delete(cyg_handle_t handle);</PRE></TD></TR></TABLE><P><TTCLASS="PARAMETER"><I>handle</I></TT> should be the value returned by the create function.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="SERVICES-CPULOAD-API-CYG-CPULOAD-GET">cyg_cpuload_get</H2><P>This function returns the latest measurements.</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">void cyg_cpuload_get(cyg_handle_t handle, cyg_uint32 *average_point1s, cyg_uint32 *average_1s, cyg_uint32 *average_10s);</PRE></TD></TR></TABLE><P><TTCLASS="PARAMETER"><I>handle</I></TT> should be the value returned by thecreate function. The load measurements for the last 100ms, 1s and 10sare returned in<TTCLASS="PARAMETER"><I>*average_point1s</I></TT>,<TTCLASS="PARAMETER"><I>*average_1s</I></TT>and <TTCLASS="PARAMETER"><I>*average_10s</I></TT> respectively.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="SERVICES-CPULOAD-API-IMPLEMENTATION">Implementation details</H2><P>This section gives a few details of how the measurements aremade. This should help to understand what the results mean.</P><P>When there are no other threads runnable, eCos will execute the idlethread. This thread is always runnable and uses the lowest threadpriority. The idle thread does little. It is an endless loop whichincrements the variable, <TTCLASS="LITERAL">idle_thread_loops</TT> andexecutes the macro <TTCLASS="LITERAL">HAL_IDLE_THREAD_ACTION</TT>. The cpuload measurement code makes use of the variable. It periodicallyexamines the value of the variable and sees how much it haschanged. The idler the system, the more it will have incremented. Fromthis it is simple to determine the load of the system.</P><P>The function <TTCLASS="LITERAL">cyg_cpuload_calibrate</TT> executes theidle thread for 100ms to determine how much<TTCLASS="LITERAL">idle_thread_loops</TT> is incremented on a system idlefor 100ms. <TTCLASS="LITERAL">cyg_cpuload_create</TT> starts an alarm whichevery 100ms calls an alarm function. This function looks at thedifference in <TTCLASS="LITERAL">idle_thread_loops</TT> since the lastinvocation of the alarm function and so calculated how idle or busythe system has been. The structure <TTCLASS="LITERAL">cyg_cpuload</TT> isupdated during the alarm functions with the new results. The 100msresult is simply the result from the last measurement period. A simplefilter is used to average the load over a period of time, namely 1sand 10s. Due to rounding errors, the 1s and 10s value will probablynever reach 100% on a fully loaded system, but 99% is often seen.</P><P>As stated above, clever power management code will interfere withthese measurements. The basic assumption is that the idle thread willbe executed un-hindered and under the same conditions as when thecalibration function was executed. If the CPU clock rate is reduced,the idle thread counter will be incremented less and so the CPU loadmeasurements will give values too high. If the CPU is halted entirely,100% cpu load will be measured.</P></DIV></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="services-cpuload.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="ecos-ref.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="services-profile-gprof.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">CPU load measurements</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="services-cpuload.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Application profiling</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -