mem_measure_tool.c (8743B)
1 /* 2 * 3 * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file. 4 * 5 * Copyright 2007-2019 Broadcom Inc. All rights reserved. 6 * 7 * This file contains the memory measurement tool implementation. 8 */ 9 10 #include <shared/mem_measure_tool.h> 11 12 #include <shared/error.h> 13 14 int memory_measurement_tool_initialized = 0; 15 memory_measurement_tool_t memory_measurement_tool; 16 17 /* 18 * See .h file 19 */ 20 memory_measurement_tool_dnx_t memory_measurement_tool_dnx; 21 22 int memory_consumption_start_measurement(char *str) 23 { 24 int fnd = 0; 25 int idx; 26 for(idx = 0;idx < memory_measurement_tool.count;idx++) { 27 if(sal_strcmp(memory_measurement_tool.elements[idx].id, str) == 0) { 28 if(memory_measurement_tool.elements[idx].is_active) { 29 return _SHR_E_EXISTS; 30 } 31 memory_measurement_tool.elements[idx].is_active = 1; 32 fnd = 1; 33 break; 34 } 35 } 36 if(!fnd) { 37 if(memory_measurement_tool.count + 1 > MEMORY_MEASUREMENT_ELEMENTS_MAX_NUM) { 38 return _SHR_E_MEMORY; 39 } 40 memory_measurement_tool.elements[memory_measurement_tool.count].is_active = 1; 41 memory_measurement_tool.elements[memory_measurement_tool.count].sal_size = 0; 42 memory_measurement_tool.elements[memory_measurement_tool.count].sw_state_size = 0; 43 sal_strncpy(memory_measurement_tool.elements[memory_measurement_tool.count].id, str, MEMORY_MEASUREMENT_ID_MAX_LENGTH - 1); 44 memory_measurement_tool.elements[memory_measurement_tool.count].thread_id = sal_thread_self(); 45 memory_measurement_tool.count++; 46 } 47 return _SHR_E_NONE; 48 } 49 50 int memory_consumption_pause_measurement(char *str) 51 { 52 int idx; 53 int fnd = 0; 54 for(idx = 0;idx < memory_measurement_tool.count;idx++) { 55 if((sal_strcmp(memory_measurement_tool.elements[idx].id, str) == 0) && (memory_measurement_tool.elements[idx].thread_id == sal_thread_self())) { 56 fnd = 1; 57 memory_measurement_tool.elements[idx].is_active = 0; 58 break; 59 } 60 } 61 if (fnd != 1) { 62 return _SHR_E_NOT_FOUND; 63 } 64 return _SHR_E_NONE; 65 } 66 67 int memory_consumption_end_measurement(char *str) 68 { 69 int idx; 70 int fnd = 0; 71 for(idx = 0;idx < memory_measurement_tool.count;idx++) { 72 if((sal_strcmp(memory_measurement_tool.elements[idx].id, str) == 0) && (memory_measurement_tool.elements[idx].thread_id == sal_thread_self())) { 73 fnd = 1; 74 memory_measurement_tool.elements[idx].is_active = 0; 75 if(idx != (memory_measurement_tool.count - 1)) { 76 memory_measurement_tool.elements[idx] = memory_measurement_tool.elements[memory_measurement_tool.count - 1]; 77 memory_measurement_tool.count--; 78 } 79 break; 80 } 81 } 82 if (fnd != 1) { 83 return _SHR_E_NOT_FOUND; 84 } 85 return _SHR_E_NONE; 86 } 87 88 uint32 memory_consumption_measurement_get_sal_size(char *str) 89 { 90 int idx; 91 for(idx = 0;idx < memory_measurement_tool.count;idx++) { 92 if((sal_strcmp(memory_measurement_tool.elements[idx].id, str) == 0) 93 && (memory_measurement_tool.elements[idx].thread_id == sal_thread_self())) { 94 return memory_measurement_tool.elements[idx].sal_size; 95 } 96 } 97 return 0; 98 } 99 100 101 void memory_measurement_tool_init(void) 102 { 103 memory_measurement_tool.count = 0; 104 } 105 106 int memory_measurement_print(char *str) 107 { 108 #ifdef MEMORY_MEASUREMENT_DIAGNOSTICS_PRINT 109 int fnd = 0; 110 int idx; 111 for(idx = 0;idx < memory_measurement_tool.count;idx++) { 112 if(sal_strcmp(memory_measurement_tool.elements[idx].id, str) == 0) { 113 fnd = 1; 114 cli_out(" Str = %s\n is_active = %d\n sal_size = %u sw_state_size = %u\n\n", 115 memory_measurement_tool.elements[idx].id, memory_measurement_tool.elements[idx].is_active, 116 memory_measurement_tool.elements[idx].sal_size, memory_measurement_tool.elements[idx].sw_state_size); 117 } 118 } 119 if (fnd != 1) { 120 return _SHR_E_NOT_FOUND; 121 } 122 return _SHR_E_NONE; 123 #else 124 return _SHR_E_DISABLED; 125 #endif 126 } 127 128 /** SW state memory measurment for DNX devices */ 129 130 /* 131 * See .h file 132 */ 133 int memory_consumption_start_measurement_dnx(char *str) 134 { 135 int fnd = 0; 136 int idx; 137 for(idx = 0;idx < memory_measurement_tool_dnx.count;idx++) { 138 if(sal_strcmp(memory_measurement_tool_dnx.elements[idx].id, str) == 0) { 139 if(memory_measurement_tool_dnx.elements[idx].is_active) { 140 return _SHR_E_EXISTS; 141 } 142 memory_measurement_tool_dnx.elements[idx].is_active = 1; 143 fnd = 1; 144 break; 145 } 146 } 147 if(!fnd) { 148 if(memory_measurement_tool_dnx.count + 1 > MEMORY_MEASUREMENT_ELEMENTS_MAX_NUM) { 149 return _SHR_E_MEMORY; 150 } 151 memory_measurement_tool_dnx.elements[memory_measurement_tool_dnx.count].is_active = 1; 152 memory_measurement_tool_dnx.elements[memory_measurement_tool_dnx.count].dnx_sw_state_size_alloc = 0; 153 memory_measurement_tool_dnx.elements[memory_measurement_tool_dnx.count].dnx_sw_state_size_free = 0; 154 sal_strncpy(memory_measurement_tool_dnx.elements[memory_measurement_tool_dnx.count].id, str, MEMORY_MEASUREMENT_ID_MAX_LENGTH - 1); 155 memory_measurement_tool_dnx.elements[memory_measurement_tool_dnx.count].thread_id = sal_thread_self(); 156 memory_measurement_tool_dnx.count++; 157 } 158 return _SHR_E_NONE; 159 } 160 161 /* 162 * See .h file 163 */ 164 int memory_consumption_end_measurement_dnx(char *str) 165 { 166 int idx; 167 int fnd = 0; 168 for(idx = 0;idx < memory_measurement_tool_dnx.count;idx++) { 169 if((sal_strcmp(memory_measurement_tool_dnx.elements[idx].id, str) == 0) && (memory_measurement_tool_dnx.elements[idx].thread_id == sal_thread_self())) { 170 fnd = 1; 171 memory_measurement_tool_dnx.elements[idx].is_active = 0; 172 if(idx != (memory_measurement_tool_dnx.count - 1)) { 173 memory_measurement_tool_dnx.elements[idx] = memory_measurement_tool_dnx.elements[memory_measurement_tool_dnx.count - 1]; 174 memory_measurement_tool_dnx.count--; 175 } 176 break; 177 } 178 } 179 if (fnd != 1) { 180 return _SHR_E_NOT_FOUND; 181 } 182 return _SHR_E_NONE; 183 } 184 185 186 /* 187 * See .h file 188 */ 189 int memory_consumption_clear_measurement_dnx(char *str) 190 { 191 int idx; 192 int fnd = 0; 193 for(idx = 0;idx < memory_measurement_tool_dnx.count;idx++) { 194 if((sal_strcmp(memory_measurement_tool_dnx.elements[idx].id, str) == 0) && (memory_measurement_tool_dnx.elements[idx].thread_id == sal_thread_self())) { 195 fnd = 1; 196 memory_measurement_tool_dnx.elements[idx].is_active = 0; 197 memory_measurement_tool_dnx.elements[memory_measurement_tool_dnx.count].dnx_sw_state_size_alloc = 0; 198 memory_measurement_tool_dnx.elements[memory_measurement_tool_dnx.count].dnx_sw_state_size_free = 0; 199 sal_strncpy(memory_measurement_tool_dnx.elements[memory_measurement_tool_dnx.count].id, "", MEMORY_MEASUREMENT_ID_MAX_LENGTH - 1); 200 memory_measurement_tool_dnx.elements[idx] = memory_measurement_tool_dnx.elements[memory_measurement_tool_dnx.count - 1]; 201 memory_measurement_tool_dnx.count--; 202 break; 203 } 204 } 205 if (fnd != 1) { 206 return _SHR_E_NOT_FOUND; 207 } 208 return _SHR_E_NONE; 209 } 210 211 /* 212 * See .h file 213 */ 214 int memory_measurement_dnx_sw_state_get(char *str, uint32 *dnx_sw_state_size, int is_alloc) 215 { 216 int fnd = 0; 217 int idx; 218 for(idx = 0;idx < memory_measurement_tool_dnx.count;idx++) { 219 if(sal_strcmp(memory_measurement_tool_dnx.elements[idx].id, str) == 0) { 220 fnd = 1; 221 if(is_alloc){ 222 *dnx_sw_state_size=memory_measurement_tool_dnx.elements[idx].dnx_sw_state_size_alloc; 223 } else { 224 *dnx_sw_state_size=memory_measurement_tool_dnx.elements[idx].dnx_sw_state_size_free; 225 } 226 } 227 } 228 if (fnd != 1) { 229 return _SHR_E_NOT_FOUND; 230 } 231 return _SHR_E_NONE; 232 } 233 234 /* 235 * See .h file 236 */ 237 void memory_measurement_dnx_sw_state_sample(uint32 size, int is_alloc) 238 { 239 int idx; 240 for(idx = 0;idx < memory_measurement_tool_dnx.count;idx++) { 241 if(memory_measurement_tool_dnx.elements[idx].is_active && (memory_measurement_tool_dnx.elements[idx].thread_id == sal_thread_self())) { 242 if(is_alloc){ 243 memory_measurement_tool_dnx.elements[idx].dnx_sw_state_size_alloc += size; 244 } else { 245 memory_measurement_tool_dnx.elements[idx].dnx_sw_state_size_free += size; 246 } 247 } 248 } 249 }