Newer
Older
cur->cache_len += reg->len;
Mark Spencer
committed
}
}
whales_len = freed_regions_size(&whales);
minnows_len = freed_regions_size(&minnows);
ast_mutex_unlock(®lock);
Mark Spencer
committed
/* Dump the whole list */
for (cur = list; cur; cur = cur->next) {
selected_len += cur->len;
cache_len += cur->cache_len;
if (cur->cache_len) {
if (fn) {
ast_cli(a->fd, "%10u bytes (%10u cache) in %10u allocations by %20s() line %5u of %s\n",
cur->len, cur->cache_len, cur->count, cur->name, cur->lineno, fn);
ast_cli(a->fd, "%10u bytes (%10u cache) in %10u allocations in file %s\n",
cur->len, cur->cache_len, cur->count, cur->name);
ast_cli(a->fd, "%10u bytes in %10u allocations by %20s() line %5u of %s\n",
cur->len, cur->count, cur->name, cur->lineno, fn);
ast_cli(a->fd, "%10u bytes in %10u allocations in file %s\n",
cur->len, cur->count, cur->name);
Mark Spencer
committed
}
print_memory_show_common_stats(a->fd,
whales_len, minnows_len, total_len,
selected_len, cache_len, count);
Mark Spencer
committed
}
static char *handle_memory_backtrace(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
switch (cmd) {
case CLI_INIT:
e->command = "memory backtrace {on|off}";
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
e->usage =
"Usage: memory backtrace {on|off}\n"
" Enable dumping an allocation backtrace with memory diagnostics.\n"
" Note that saving the backtrace data for each allocation\n"
" can be CPU intensive.\n";
return NULL;
case CLI_GENERATE:
return NULL;
}
if (a->argc != 3) {
return CLI_SHOWUSAGE;
}
if (ast_true(a->argv[2])) {
backtrace_enabled = 1;
} else if (ast_false(a->argv[2])) {
backtrace_enabled = 0;
} else {
return CLI_SHOWUSAGE;
}
ast_cli(a->fd, "The memory backtrace is: %s\n", backtrace_enabled ? "On" : "Off");
return CLI_SUCCESS;
}
static struct ast_cli_entry cli_memory[] = {
AST_CLI_DEFINE(handle_memory_atexit_list, "Enable memory allocations not freed at exit list."),
AST_CLI_DEFINE(handle_memory_atexit_summary, "Enable memory allocations not freed at exit summary."),
AST_CLI_DEFINE(handle_memory_show_allocations, "Display outstanding memory allocations"),
Jason Parker
committed
AST_CLI_DEFINE(handle_memory_show_summary, "Summarize outstanding memory allocations"),
AST_CLI_DEFINE(handle_memory_backtrace, "Enable dumping an allocation backtrace with memory diagnostics."),
Mark Spencer
committed
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
AST_LIST_HEAD_NOLOCK(region_list, ast_region);
/*!
* \internal
* \brief Convert the allocated regions hash table to a list.
*
* \param list Fill list with the allocated regions.
*
* \details
* Take all allocated regions from the regions[] and put them
* into the list.
*
* \note reglock must be locked before calling.
*
* \note This function is destructive to the regions[] lists.
*
* \return Length of list created.
*/
static size_t mm_atexit_hash_list(struct region_list *list)
{
struct ast_region *reg;
size_t total_length;
int idx;
total_length = 0;
for (idx = 0; idx < ARRAY_LEN(regions); ++idx) {
while ((reg = regions[idx])) {
regions[idx] = AST_LIST_NEXT(reg, node);
AST_LIST_NEXT(reg, node) = NULL;
AST_LIST_INSERT_HEAD(list, reg, node);
++total_length;
}
}
return total_length;
}
/*!
* \internal
* \brief Put the regions list into the allocated regions hash table.
*
* \param list List to put into the allocated regions hash table.
*
* \note reglock must be locked before calling.
*
* \return Nothing
*/
static void mm_atexit_hash_restore(struct region_list *list)
{
struct ast_region *reg;
int hash;
while ((reg = AST_LIST_REMOVE_HEAD(list, node))) {
hash = HASH(reg->data);
AST_LIST_NEXT(reg, node) = regions[hash];
regions[hash] = reg;
}
}
/*!
* \internal
* \brief Sort regions comparision.
*
* \param left Region to compare.
* \param right Region to compare.
*
* \retval <0 if left < right
* \retval =0 if left == right
* \retval >0 if left > right
*/
static int mm_atexit_cmp(struct ast_region *left, struct ast_region *right)
{
int cmp;
ptrdiff_t cmp_ptr;
ssize_t cmp_size;
/* Sort by filename. */
cmp = strcmp(left->file, right->file);
if (cmp) {
return cmp;
}
/* Sort by line number. */
cmp = left->lineno - right->lineno;
if (cmp) {
return cmp;
}
/* Sort by allocated size. */
cmp_size = left->len - right->len;
if (cmp_size) {
if (cmp_size < 0) {
return -1;
}
return 1;
}
/* Sort by allocated pointers just because. */
cmp_ptr = left->data - right->data;
if (cmp_ptr) {
if (cmp_ptr < 0) {
return -1;
}
return 1;
}
return 0;
}
/*!
* \internal
* \brief Merge the given sorted sublists into sorted order onto the end of the list.
*
* \param list Merge sublists onto this list.
* \param sub1 First sublist to merge.
* \param sub2 Second sublist to merge.
*
* \return Nothing
*/
static void mm_atexit_list_merge(struct region_list *list, struct region_list *sub1, struct region_list *sub2)
{
struct ast_region *reg;
for (;;) {
if (AST_LIST_EMPTY(sub1)) {
/* The remaining sublist goes onto the list. */
AST_LIST_APPEND_LIST(list, sub2, node);
break;
}
if (AST_LIST_EMPTY(sub2)) {
/* The remaining sublist goes onto the list. */
AST_LIST_APPEND_LIST(list, sub1, node);
break;
}
if (mm_atexit_cmp(AST_LIST_FIRST(sub1), AST_LIST_FIRST(sub2)) <= 0) {
reg = AST_LIST_REMOVE_HEAD(sub1, node);
} else {
reg = AST_LIST_REMOVE_HEAD(sub2, node);
}
AST_LIST_INSERT_TAIL(list, reg, node);
}
}
/*!
* \internal
* \brief Take sublists off of the given list.
*
* \param list Source list to remove sublists from the beginning of list.
* \param sub Array of sublists to fill. (Lists are empty on entry.)
* \param num_lists Number of lists to remove from the source list.
* \param size Size of the sublists to remove.
* \param remaining Remaining number of elements on the source list.
*
* \return Nothing
*/
static void mm_atexit_list_split(struct region_list *list, struct region_list sub[], size_t num_lists, size_t size, size_t *remaining)
{
int idx;
for (idx = 0; idx < num_lists; ++idx) {
size_t count;
if (*remaining < size) {
/* The remaining source list goes onto the sublist. */
AST_LIST_APPEND_LIST(&sub[idx], list, node);
*remaining = 0;
break;
}
/* Take a sublist off the beginning of the source list. */
*remaining -= size;
for (count = size; count--;) {
struct ast_region *reg;
reg = AST_LIST_REMOVE_HEAD(list, node);
AST_LIST_INSERT_TAIL(&sub[idx], reg, node);
}
}
}
/*!
* \internal
* \brief Sort the regions list using mergesort.
*
* \param list Allocated regions list to sort.
* \param length Length of the list.
*
* \return Nothing
*/
static void mm_atexit_list_sort(struct region_list *list, size_t length)
{
/*! Semi-sorted merged list. */
struct region_list merged = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
/*! Sublists to merge. (Can only merge two sublists at this time.) */
struct region_list sub[2] = {
AST_LIST_HEAD_NOLOCK_INIT_VALUE,
AST_LIST_HEAD_NOLOCK_INIT_VALUE
};
/*! Sublist size. */
size_t size = 1;
/*! Remaining elements in the list. */
size_t remaining;
/*! Number of sublist merge passes to process the list. */
int passes;
for (;;) {
remaining = length;
passes = 0;
while (!AST_LIST_EMPTY(list)) {
mm_atexit_list_split(list, sub, ARRAY_LEN(sub), size, &remaining);
mm_atexit_list_merge(&merged, &sub[0], &sub[1]);
++passes;
}
AST_LIST_APPEND_LIST(list, &merged, node);
if (passes <= 1) {
/* The list is now sorted. */
break;
}
/* Double the sublist size to remove for next round. */
size <<= 1;
}
}
/*!
* \internal
* \brief List all regions currently allocated.
*
* \param alloced regions list.
*
* \return Nothing
*/
static void mm_atexit_regions_list(struct region_list *alloced)
{
struct ast_region *reg;
AST_LIST_TRAVERSE(alloced, reg, node) {
astmm_log("%s %s() line %u: %u bytes%s at %p\n",
reg->file, reg->func, reg->lineno,
(unsigned int) reg->len, reg->cache ? " (cache)" : "", reg->data);
}
}
/*!
* \internal
* \brief Summarize all regions currently allocated.
*
* \param alloced Sorted regions list.
*
* \return Nothing
*/
static void mm_atexit_regions_summary(struct region_list *alloced)
{
struct ast_region *reg;
struct ast_region *next;
struct {
unsigned int count;
unsigned int len;
unsigned int cache_len;
} by_line, by_func, by_file, total;
by_line.count = 0;
by_line.len = 0;
by_line.cache_len = 0;
by_func.count = 0;
by_func.len = 0;
by_func.cache_len = 0;
by_file.count = 0;
by_file.len = 0;
by_file.cache_len = 0;
total.count = 0;
total.len = 0;
total.cache_len = 0;
AST_LIST_TRAVERSE(alloced, reg, node) {
next = AST_LIST_NEXT(reg, node);
++by_line.count;
by_line.len += reg->len;
if (reg->cache) {
by_line.cache_len += reg->len;
}
if (next && !strcmp(reg->file, next->file) && reg->lineno == next->lineno) {
continue;
}
if (atexit_summary & SUMMARY_BY_LINE) {
if (by_line.cache_len) {
astmm_log("%10u bytes (%u in caches) in %u allocations. %s %s() line %u\n",
by_line.len, by_line.cache_len, by_line.count, reg->file, reg->func, reg->lineno);
} else {
astmm_log("%10u bytes in %5u allocations. %s %s() line %u\n",
by_line.len, by_line.count, reg->file, reg->func, reg->lineno);
}
}
by_func.count += by_line.count;
by_func.len += by_line.len;
by_func.cache_len += by_line.cache_len;
by_line.count = 0;
by_line.len = 0;
by_line.cache_len = 0;
if (next && !strcmp(reg->file, next->file) && !strcmp(reg->func, next->func)) {
continue;
}
if (atexit_summary & SUMMARY_BY_FUNC) {
if (by_func.cache_len) {
astmm_log("%10u bytes (%u in caches) in %u allocations. %s %s()\n",
by_func.len, by_func.cache_len, by_func.count, reg->file, reg->func);
} else {
astmm_log("%10u bytes in %5u allocations. %s %s()\n",
by_func.len, by_func.count, reg->file, reg->func);
}
}
by_file.count += by_func.count;
by_file.len += by_func.len;
by_file.cache_len += by_func.cache_len;
by_func.count = 0;
by_func.len = 0;
by_func.cache_len = 0;
if (next && !strcmp(reg->file, next->file)) {
continue;
}
if (atexit_summary & SUMMARY_BY_FILE) {
if (by_file.cache_len) {
astmm_log("%10u bytes (%u in caches) in %u allocations. %s\n",
by_file.len, by_file.cache_len, by_file.count, reg->file);
} else {
astmm_log("%10u bytes in %5u allocations. %s\n",
by_file.len, by_file.count, reg->file);
}
}
total.count += by_file.count;
total.len += by_file.len;
total.cache_len += by_file.cache_len;
by_file.count = 0;
by_file.len = 0;
by_file.cache_len = 0;
}
if (total.cache_len) {
astmm_log("%u bytes (%u in caches) in %u allocations.\n",
total.len, total.cache_len, total.count);
} else {
astmm_log("%u bytes in %u allocations.\n", total.len, total.count);
}
}
/*!
* \internal
* \brief Dump the memory allocations atexit.
*
* \note reglock must be locked before calling.
*
* \return Nothing
*/
static void mm_atexit_dump(void)
{
struct region_list alloced_atexit = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
size_t length;
length = mm_atexit_hash_list(&alloced_atexit);
if (!length) {
/* Wow! This is amazing! */
astmm_log("Exiting with all memory freed.\n");
return;
}
mm_atexit_list_sort(&alloced_atexit, length);
astmm_log("Exiting with the following memory not freed:\n");
if (atexit_list) {
mm_atexit_regions_list(&alloced_atexit);
}
if (atexit_summary) {
mm_atexit_regions_summary(&alloced_atexit);
}
/*
* Put the alloced list back into regions[].
*
* We have do do this because we can get called before all other
* threads have terminated.
*/
mm_atexit_hash_restore(&alloced_atexit);
}
/*!
* \internal
* \return Nothing
*/
static void mm_atexit_final(void)
Mark Spencer
committed
{
/* Only wait if we want atexit allocation dumps. */
if (atexit_list || atexit_summary) {
fprintf(stderr, "Waiting 10 seconds to let other threads die.\n");
sleep(10);
}
regions_check_all_fences();
/* Flush all delayed memory free circular arrays. */
freed_regions_flush(&whales);
freed_regions_flush(&minnows);
/* Peform atexit allocation dumps. */
if (atexit_list || atexit_summary) {
ast_mutex_lock(®lock);
mm_atexit_dump();
ast_mutex_unlock(®lock);
}
/* Close the log file. */
log = mmlog;
mmlog = NULL;
if (log) {
fclose(log);
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
}
/*!
* \brief Initialize malloc debug phase 1.
*
* \note Must be called first thing in main().
*
* \return Nothing
*/
void __ast_mm_init_phase_1(void)
{
atexit(mm_atexit_final);
}
/*!
* \internal
* \return Nothing
*/
static void mm_atexit_ast(void)
{
ast_cli_unregister_multiple(cli_memory, ARRAY_LEN(cli_memory));
}
/*!
* \brief Initialize malloc debug phase 2.
*
* \return Nothing
*/
void __ast_mm_init_phase_2(void)
{
char filename[PATH_MAX];
ast_cli_register_multiple(cli_memory, ARRAY_LEN(cli_memory));
snprintf(filename, sizeof(filename), "%s/mmlog", ast_config_AST_LOG_DIR);
ast_verb(1, "Asterisk Malloc Debugger Started (see %s))\n", filename);
mmlog = fopen(filename, "a+");
if (mmlog) {
fprintf(mmlog, "%ld - New session\n", (long) time(NULL));
} else {
ast_log(LOG_ERROR, "Could not open malloc debug log file: %s\n", filename);
ast_register_cleanup(mm_atexit_ast);
Mark Spencer
committed
}
#endif /* defined(__AST_DEBUG_MALLOC) */