]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/services/cpuload/v2_0/tests/cpuload.c
Initial revision
[karo-tx-redboot.git] / packages / services / cpuload / v2_0 / tests / cpuload.c
1 //=================================================================
2 //
3 //        cpuload.c
4 //
5 //        test case for the cpuload code.
6 //
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 2002 Andrew Lunn
12 //
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
16 //
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 //
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
32 //
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
35 //
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
42 //
43 // Author(s):     asl
44 // Contributors:  asl
45 // Date:          2002-08-06
46 // Description:   Tests the calculation code
47 //####DESCRIPTIONEND####
48
49 #include <cyg/hal/hal_arch.h>
50 #include <cyg/kernel/kapi.h>
51 #include <cyg/infra/testcase.h>
52 #include <cyg/infra/diag.h>
53 #include <cyg/cpuload/cpuload.h>
54
55 void
56 do_test(cyg_addrword_t data) {
57   cyg_uint32 calibration;
58   cyg_cpuload_t cpuload;
59   cyg_handle_t handle;
60   cyg_uint32 average_point1s;
61   cyg_uint32 average_1s;
62   cyg_uint32 average_10s;
63   cyg_uint32 i,j;
64   
65   CYG_TEST_INFO("About to calibrate cpuload");
66   
67   cyg_cpuload_calibrate(&calibration);
68   
69   CYG_TEST_INFO("Performing 100% load test");
70   
71   cyg_cpuload_create(&cpuload,calibration,&handle);
72   
73   /* Busy loop doing useless work. This will cause the cpu load to be
74      100%. After all these iterations, check the measured load really
75      is 100%. */
76
77   for (i=250; i--; ) {
78     for (j=calibration*10; j-- ; ) 
79       ;
80     cyg_cpuload_get(handle,&average_point1s,&average_1s,&average_10s);
81     /* diag_printf("%d %d %d\n",average_point1s,average_1s,average_10s);*/
82   }
83
84   /* Rounding errors mean it will probably never reach 100% */
85   if ((average_point1s == 100) &&
86       (average_1s >= 99) && (average_1s <=100) &&
87       (average_10s >= 98) && (average_10s <= 100))
88     CYG_TEST_PASS("100% load o.k.");
89   else {
90     CYG_TEST_FAIL("100% load");
91   }
92   
93   /* Sleeping doing nothing. This will cause the cpuload to be
94      0%. After all these iterations, check the measured load really is
95      0%. */
96   CYG_TEST_INFO("Performing 0% load test");
97   for (i=350; i-- ; ) {
98     cyg_thread_delay(10);
99     cyg_cpuload_get(handle,&average_point1s,&average_1s,&average_10s);
100     /* diag_printf("%d %d %d\n",average_point1s,average_1s,average_10s);*/
101   }
102   
103   if ((average_point1s == 0) &&
104       (average_1s == 0) &&
105       (average_10s >= 0) && (average_10s <= 2))
106     CYG_TEST_PASS_FINISH("0% load o.k.");
107   else {
108     CYG_TEST_FAIL_FINISH("0% load");
109   }
110 }
111
112
113 externC void
114 cyg_start(void) {
115
116 #ifdef CYGPKG_HAL_SYNTH
117
118   CYG_TEST_NA("Not applicable to Synth target");
119 #else
120   static char stack[CYGNUM_HAL_STACK_SIZE_MINIMUM];
121   static cyg_handle_t handle;
122   static cyg_thread thread;
123   
124   CYG_TEST_INIT();
125   
126   cyg_thread_create(4,do_test,0,"cpuload",
127                     stack,sizeof(stack),&handle,&thread);
128   cyg_thread_resume(handle);
129   
130   cyg_scheduler_start();
131 #endif
132 }