1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** Thread-Metric Component                                               */
16 /**                                                                       */
17 /**   Memory Allocation Test                                              */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 /**************************************************************************/
23 /*                                                                        */
24 /*  FUNCTION                                               RELEASE        */
25 /*                                                                        */
26 /*    tm_memory_allocation_test                           PORTABLE C      */
27 /*                                                           6.1.7        */
28 /*  AUTHOR                                                                */
29 /*                                                                        */
30 /*    William E. Lamie, Microsoft Corporation                             */
31 /*                                                                        */
32 /*  DESCRIPTION                                                           */
33 /*                                                                        */
34 /*    This file defines the Message exchange processing test.             */
35 /*                                                                        */
36 /*  RELEASE HISTORY                                                       */
37 /*                                                                        */
38 /*    DATE              NAME                      DESCRIPTION             */
39 /*                                                                        */
40 /*  10-15-2021     William E. Lamie         Initial Version 6.1.7         */
41 /*                                                                        */
42 /**************************************************************************/
43 #include "tm_api.h"
44 
45 
46 /* Define the counters used in the demo application...  */
47 
48 unsigned long   tm_memory_allocation_counter;
49 
50 
51 /* Define the test thread prototypes.  */
52 
53 void            tm_memory_allocation_thread_0_entry(void);
54 
55 
56 /* Define the reporting thread prototype.  */
57 
58 void            tm_memory_allocation_thread_report(void);
59 
60 
61 /* Define the initialization prototype.  */
62 
63 void            tm_memory_allocation_initialize(void);
64 
65 
66 /* Define main entry point.  */
67 
tm_main()68 void tm_main()
69 {
70 
71     /* Initialize the test.  */
72     tm_initialize(tm_memory_allocation_initialize);
73 }
74 
75 
76 /* Define the memory allocation processing test initialization.  */
77 
tm_memory_allocation_initialize(void)78 void  tm_memory_allocation_initialize(void)
79 {
80 
81     /* Create thread 0 at priority 10.  */
82     tm_thread_create(0, 10, tm_memory_allocation_thread_0_entry);
83 
84     /* Resume thread 0.  */
85     tm_thread_resume(0);
86 
87     /* Create a memory pool.  */
88     tm_memory_pool_create(0);
89 
90     /* Create the reporting thread. It will preempt the other
91        threads and print out the test results.  */
92     tm_thread_create(5, 2, tm_memory_allocation_thread_report);
93     tm_thread_resume(5);
94 }
95 
96 
97 /* Define the memory allocation processing thread.  */
tm_memory_allocation_thread_0_entry(void)98 void  tm_memory_allocation_thread_0_entry(void)
99 {
100 
101 int             status;
102 unsigned char   *memory_ptr;
103 
104 
105     while(1)
106     {
107 
108         /* Allocate memory from pool.  */
109         tm_memory_pool_allocate(0, &memory_ptr);
110 
111         /* Release the memory back to the pool.  */
112         status =  tm_memory_pool_deallocate(0, memory_ptr);
113 
114         /* Check for invalid memory allocation/deallocation.  */
115         if (status != TM_SUCCESS)
116             break;
117 
118         /* Increment the number of memory allocations sent and received.  */
119         tm_memory_allocation_counter++;
120     }
121 }
122 
123 
124 /* Define the memory allocation test reporting thread.  */
tm_memory_allocation_thread_report(void)125 void  tm_memory_allocation_thread_report(void)
126 {
127 
128 unsigned long   last_counter;
129 unsigned long   relative_time;
130 
131 
132     /* Initialize the last counter.  */
133     last_counter =  0;
134 
135     /* Initialize the relative time.  */
136     relative_time =  0;
137 
138     while(1)
139     {
140 
141         /* Sleep to allow the test to run.  */
142         tm_thread_sleep(TM_TEST_DURATION);
143 
144         /* Increment the relative time.  */
145         relative_time =  relative_time + TM_TEST_DURATION;
146 
147         /* Print results to the stdio window.  */
148         printf("**** Thread-Metric Memory Allocation Test **** Relative Time: %lu\n", relative_time);
149 
150         /* See if there are any errors.  */
151         if (tm_memory_allocation_counter == last_counter)
152         {
153 
154             printf("ERROR: Invalid counter value(s). Error allocating/deallocating memory!\n");
155         }
156 
157         /* Show the time period total.  */
158         printf("Time Period Total:  %lu\n\n", tm_memory_allocation_counter - last_counter);
159 
160         /* Save the last counter.  */
161         last_counter =  tm_memory_allocation_counter;
162     }
163 }
164 
165