summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
author[email protected] <[email protected]>2011-02-20 15:15:04 +0000
committer[email protected] <[email protected]>2011-02-20 15:15:04 +0000
commitceda7d874e84c4637dbef58b39b78714ad5a1178 (patch)
treeb479cc5fbc7a3673f8170075f0471a6320ae83f7 /examples
parent664a1345cf11be8897cbe4c1f9bc0a0a500e9b39 (diff)
add CPU usage example code.
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1282 bbd45198-f89e-11dd-88c7-29a3b14d5316
Diffstat (limited to 'examples')
-rw-r--r--examples/kernel/cpuusage.c71
1 files changed, 71 insertions, 0 deletions
diff --git a/examples/kernel/cpuusage.c b/examples/kernel/cpuusage.c
new file mode 100644
index 0000000000..899d7cc20d
--- /dev/null
+++ b/examples/kernel/cpuusage.c
@@ -0,0 +1,71 @@
+#include <rtthread.h>
+#include <rthw.h>
+
+#define CPU_USAGE_CALC_TICK 10
+#define CPU_USAGE_LOOP 100
+
+static rt_uint8_t cpu_usage_major = 0, cpu_usage_minor= 0;
+static rt_uint32_t total_count = 0;
+
+static void cpu_usage_idle_hook()
+{
+ rt_tick_t tick;
+ rt_uint32_t count;
+ volatile rt_uint32_t loop;
+
+ if (total_count == 0)
+ {
+ loop = 0;
+
+ /* get total count */
+ rt_enter_critical();
+ tick = rt_tick_get();
+ while(rt_tick_get() - tick < CPU_USAGE_CALC_TICK)
+ {
+ total_count ++;
+ while (loop < CPU_USAGE_LOOP) loop ++;
+ }
+ rt_exit_critical();
+ }
+
+ count = 0;
+ loop = 0;
+ /* get CPU usage */
+ tick = rt_tick_get();
+ while (rt_tick_get() - tick < CPU_USAGE_CALC_TICK)
+ {
+ count ++;
+ while (loop < CPU_USAGE_LOOP) loop ++;
+ }
+
+ /* calculate major and minor */
+ if (count < total_count)
+ {
+ count = total_count - count;
+ cpu_usage_major = (count * 100) / total_count;
+ cpu_usage_minor = ((count * 100) % total_count) * 100 / total_count;
+ }
+ else
+ {
+ total_count = count;
+
+ /* no CPU usage */
+ cpu_usage_major = 0;
+ cpu_usage_minor = 0;
+ }
+}
+
+void cpu_usage_get(rt_uint8_t *major, rt_uint8_t *minor)
+{
+ RT_ASSERT(major != RT_NULL);
+ RT_ASSERT(minor != RT_NULL);
+
+ *major = cpu_usage_major;
+ *minor = cpu_usage_minor;
+}
+
+void cpu_usage_init()
+{
+ /* set idle thread hook */
+ rt_thread_idle_sethook(cpu_usage_idle_hook);
+}