1 //==========================================================================
3 // tests/auto/memcheck.inl
5 // Automated Testing by a Host Computer
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
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.
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
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.
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.
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.
36 // -------------------------------------------
37 //####ECOSGPLCOPYRIGHTEND####
38 //####BSDCOPYRIGHTBEGIN####
40 // -------------------------------------------
42 // Portions of this software may have been derived from OpenBSD or other sources,
43 // and are covered by the appropriate copyright disclaimers included herein.
45 // -------------------------------------------
47 //####BSDCOPYRIGHTEND####
48 //==========================================================================
49 //#####DESCRIPTIONBEGIN####
54 // Purpose: Check memory usage of the eCos TCP/IP Network Stack
57 #ifndef CYG_NET_GET_MEM_STATS_MISC
58 // API is not present, so dummies:
59 void memcheck_init( void ) {}
60 void memcheck_final( void ) {}
64 extern struct mbstat mbstat;
66 extern void cyg_kmem_print_stats( void );
68 cyg_mempool_info initial[3], final[3];
72 static char *names[3] = { "Misc", "Mbufs", "Clusters" };
74 void memcheck_init( void )
78 cyg_kmem_print_stats();
80 for ( i = 0; i < 3; i++ )
81 if ( ! cyg_net_get_mem_stats( i, initial + i ) )
82 CYG_TEST_FAIL( "Getting initial mem stats" );
85 void memcheck_final( void )
88 for ( i = 0; i < 3; i++ )
89 if ( ! cyg_net_get_mem_stats( i, final + i ) )
90 CYG_TEST_FAIL( "Getting final mem stats" );
92 cyg_kmem_print_stats();
94 // NB do NOT check clusters (index 2) because clusters are managed
96 for ( i = 0; i < 3; i++ ) {
97 int size = final[i].totalmem;
98 int initial_used = size - initial[i].freemem;
99 int margin = size * RATIO / 100;
100 int final_used = size - final[i].freemem;
101 int extra_used = final_used - initial_used;
105 // ignore clusters if a faulty network is being simulated
106 // because memory gets eaten for partial packets awaiting
110 // Then it's clusters - they're managed differently
111 final_used -= (mbstat.m_clfree * final[i].blocksize);
112 extra_used = final_used - initial_used;
116 // Using up up to whatever was initially used -or- the percentage
117 // we test for; whichever is larger, is OK.
118 if ( (extra_used > initial_used) && (extra_used > margin) ) {
119 CYG_TEST_INFO( "Memory leak suspected" );
120 CYG_TEST_INFO( names[i] );
121 CYG_TEST_INFO( (final[i].blocksize > 0) ?
122 "Variable mempool" : "Fixed mempool" );
123 CYG_TEST_FAIL( "Too much memory used - storeleak suspected" );