Fix pbx_wilcalu from occupying 100% CPU now that it's nonblocking, and add malloc debug

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@927 65c4cc65-6c06-0410-ace0-fbb531ad65f3
1.0
Mark Spencer 22 years ago
parent 5526000ece
commit 209a6d00f3

@ -24,12 +24,13 @@ PROC=i686
ifeq (${OSARCH},Linux)
PROC=$(shell uname -m)
endif
PWD=$(shell pwd)
######### More GSM codec optimization
######### Uncomment to enable MMXTM optimizations for x86 architecture CPU's
######### which support MMX instructions. This should be newer pentiums,
######### ppro's, etc, as well as the AMD K6 and K7.
K6OPT = #-DK6OPT
K6OPT = -DK6OPT
#Tell gcc to optimize the asterisk's code
OPTIMIZE=-O6
@ -43,6 +44,13 @@ DEBUG_THREADS = #-DDO_CRASH -DDEBUG_THREADS
# Uncomment next one to enable ast_frame tracing (for debugging)
TRACE_FRAMES = #-DTRACE_FRAMES
# Uncomment next one to enable malloc debugging
# You can view malloc debugging with:
# *CLI> show memory allocations [filename]
# *CLI> show memory summary [filename]
#
MALLOC_DEBUG = #-include $(PWD)/include/asterisk/astmm.h
# Where to install asterisk after compiling
# Default -> leave empty
INSTALL_PREFIX=
@ -90,6 +98,7 @@ CFLAGS+=-DASTAGIDIR=\"$(AGI_DIR)\"
CFLAGS+= $(DEBUG_THREADS)
CFLAGS+= $(TRACE_FRAMES)
CFLAGS+= $(MALLOC_DEBUG)
CFLAGS+=# -fomit-frame-pointer
SUBDIRS=res channels pbx apps codecs formats agi cdr astman
ifeq (${OSARCH},Linux)
@ -100,7 +109,8 @@ OBJS=io.o sched.o logger.o frame.o loader.o config.o channel.o \
translate.o file.o say.o pbx.o cli.o md5.o term.o \
ulaw.o alaw.o callerid.o fskmodem.o image.o app.o \
cdr.o tdd.o acl.o rtp.o manager.o asterisk.o ast_expr.o \
dsp.o chanvars.o indications.o autoservice.o db.o privacy.o
dsp.o chanvars.o indications.o autoservice.o db.o privacy.o \
astmm.o
CC=gcc
INSTALL=install

@ -1341,6 +1341,9 @@ int main(int argc, char *argv[])
ast_verbose(term_color(tmp, "Asterisk Ready.\n", COLOR_BRWHITE, COLOR_BLACK, sizeof(tmp)));
fully_booted = 1;
pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
#ifdef __AST_DEBUG_MALLOC
__ast_mm_init();
#endif
ast_cli_register(&astshutdownnow);
ast_cli_register(&astshutdowngracefully);
ast_cli_register(&astrestartnow);

@ -1,4 +1,7 @@
#
# Don't use ast mm routines
#
CFLAGS+=-DNO_AST_MM
TARGET=$(shell if [ -f /usr/include/newt.h ]; then echo "astman"; else echo "none" ; fi)
all: $(TARGET)

@ -0,0 +1,319 @@
/*
* Asterisk -- A telephony toolkit for Linux.
*
* Channel Variables
*
* Copyright (C) 2002, Mark Spencer
*
* Mark Spencer <markster@linux-support.net>
*
* This program is free software, distributed under the terms of
* the GNU General Public License
*/
#ifdef __AST_DEBUG_MALLOC
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <asterisk/cli.h>
#define SOME_PRIME 563
#define FUNC_CALLOC 1
#define FUNC_MALLOC 2
#define FUNC_REALLOC 3
#define FUNC_STRDUP 4
#define FUNC_STRNDUP 5
/* Undefine all our macros */
#undef malloc
#undef calloc
#undef realloc
#undef strdup
#undef strndup
#undef free
static struct ast_region {
struct ast_region *next;
char file[40];
char func[40];
int lineno;
int which;
size_t len;
unsigned char data[0];
} *regions[SOME_PRIME];
#define HASH(a) \
(((unsigned long)(a)) % SOME_PRIME)
static pthread_mutex_t reglock = PTHREAD_MUTEX_INITIALIZER;
static inline void *__ast_alloc_region(size_t size, int which, const char *file, int lineno, const char *func)
{
struct ast_region *reg;
void *ptr=NULL;
int hash;
reg = malloc(size + sizeof(struct ast_region));
pthread_mutex_lock(&reglock);
if (reg) {
strncpy(reg->file, file, sizeof(reg->file) - 1);
reg->file[sizeof(reg->file) - 1] = '\0';
strncpy(reg->func, func, sizeof(reg->func) - 1);
reg->func[sizeof(reg->func) - 1] = '\0';
reg->lineno = lineno;
reg->len = size;
reg->which = which;
ptr = reg->data;
hash = HASH(ptr);
reg->next = regions[hash];
regions[hash] = reg;
}
pthread_mutex_unlock(&reglock);
if (!reg) {
fprintf(stderr, "Out of memory :(\n");
}
return ptr;
}
static inline size_t __ast_sizeof_region(void *ptr)
{
int hash = HASH(ptr);
struct ast_region *reg;
size_t len = 0;
pthread_mutex_lock(&reglock);
reg = regions[hash];
while(reg) {
if (reg->data == ptr) {
len = reg->len;
break;
}
reg = reg->next;
}
pthread_mutex_unlock(&reglock);
return len;
}
static void __ast_free_region(void *ptr, const char *file, int lineno, const char *func)
{
int hash = HASH(ptr);
struct ast_region *reg, *prev = NULL;
pthread_mutex_lock(&reglock);
reg = regions[hash];
while(reg) {
if (reg->data == ptr) {
if (prev)
prev->next = reg->next;
else
regions[hash] = reg->next;
break;
}
prev = reg;
reg = reg->next;
}
pthread_mutex_unlock(&reglock);
if (reg) {
free(reg);
} else
fprintf(stderr, "WARNING: Freeing unused memory at %p, in %s of %s, line %d\n",
ptr, func, file, lineno);
}
void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func)
{
void *ptr;
ptr = __ast_alloc_region(size * nmemb, FUNC_CALLOC, file, lineno, func);
if (ptr)
memset(ptr, 0, size * nmemb);
return ptr;
}
void *__ast_malloc(size_t size, const char *file, int lineno, const char *func)
{
return __ast_alloc_region(size, FUNC_MALLOC, file, lineno, func);
}
void __ast_free(void *ptr, const char *file, int lineno, const char *func)
{
__ast_free_region(ptr, file, lineno, func);
}
void *__ast_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func)
{
void *tmp;
size_t len=0;
if (ptr) {
len = __ast_sizeof_region(ptr);
if (!len) {
fprintf(stderr, "WARNING: Realloc of unalloced memory at %p, in %s of %s, line %d\n",
ptr, func, file, lineno);
return NULL;
}
}
tmp = __ast_alloc_region(size, FUNC_REALLOC, file, lineno, func);
if (tmp) {
if (len > size)
len = size;
if (ptr) {
memcpy(tmp, ptr, len);
__ast_free_region(ptr, file, lineno, func);
}
}
return tmp;
}
char *__ast_strdup(const char *s, const char *file, int lineno, const char *func)
{
size_t len;
void *ptr;
if (!s)
return NULL;
len = strlen(s) + 1;
ptr = __ast_alloc_region(len, FUNC_STRDUP, file, lineno, func);
if (ptr)
strcpy(ptr, s);
return ptr;
}
char *__ast_strndup(const char *s, size_t n, const char *file, int lineno, const char *func)
{
size_t len;
void *ptr;
if (!s)
return NULL;
len = strlen(s) + 1;
if (len > n)
len = n;
ptr = __ast_alloc_region(len, FUNC_STRDUP, file, lineno, func);
if (ptr)
strcpy(ptr, s);
return ptr;
}
static int handle_show_memory(int fd, int argc, char *argv[])
{
char *fn = NULL;
int x;
struct ast_region *reg;
unsigned int len=0;
int count = 0;
if (argc >3)
fn = argv[3];
/* try to lock applications list ... */
pthread_mutex_lock(&reglock);
for (x=0;x<SOME_PRIME;x++) {
reg = regions[x];
while(reg) {
if (!fn || !strcasecmp(fn, reg->file)) {
ast_cli(fd, "%10d bytes allocated in %20s at line %5d of %s\n", reg->len, reg->func, reg->lineno, reg->file);
len += reg->len;
count++;
}
reg = reg->next;
}
}
ast_cli(fd, "%d bytes allocated %d units total\n", len, count);
pthread_mutex_unlock(&reglock);
return RESULT_SUCCESS;
}
struct file_summary {
char fn[80];
int len;
int count;
struct file_summary *next;
};
static int handle_show_memory_summary(int fd, int argc, char *argv[])
{
char *fn = NULL;
int x;
struct ast_region *reg;
unsigned int len=0;
int count = 0;
struct file_summary *list = NULL, *cur;
if (argc >3)
fn = argv[3];
/* try to lock applications list ... */
pthread_mutex_lock(&reglock);
for (x=0;x<SOME_PRIME;x++) {
reg = regions[x];
while(reg) {
if (!fn || !strcasecmp(fn, reg->file)) {
cur = list;
while(cur) {
if ((!fn && !strcmp(cur->fn, reg->file)) || (fn && !strcmp(cur->fn, reg->func)))
break;
cur = cur->next;
}
if (!cur) {
cur = alloca(sizeof(struct file_summary));
memset(cur, 0, sizeof(struct file_summary));
strncpy(cur->fn, fn ? reg->func : reg->file, sizeof(cur->fn) - 1);
cur->next = list;
list = cur;
}
cur->len += reg->len;
cur->count++;
}
reg = reg->next;
}
}
pthread_mutex_unlock(&reglock);
/* Dump the whole list */
while(list) {
cur = list;
len += list->len;
count += list->count;
if (fn)
ast_cli(fd, "%10d bytes in %5d allocations in function '%s' of '%s'\n", list->len, list->count, list->fn, fn);
else
ast_cli(fd, "%10d bytes in %5d allocations in file '%s'\n", list->len, list->count, list->fn);
list = list->next;
#if 0
free(cur);
#endif
}
ast_cli(fd, "%d bytes allocated %d units total\n", len, count);
return RESULT_SUCCESS;
}
static char show_memory_help[] =
"Usage: show memory allocations [<file>]\n"
" Dumps a list of all segments of allocated memory, optionally\n"
"limited to those from a specific file\n";
static char show_memory_summary_help[] =
"Usage: show memory summary [<file>]\n"
" Summarizes heap memory allocations by file, or optionally\n"
"by function, if a file is specified\n";
static struct ast_cli_entry show_memory_allocations_cli =
{ { "show", "memory", "allocations", NULL },
handle_show_memory, "Display outstanding memory allocations",
show_memory_help };
static struct ast_cli_entry show_memory_summary_cli =
{ { "show", "memory", "summary", NULL },
handle_show_memory_summary, "Summarize outstanding memory allocations",
show_memory_summary_help };
void __ast_mm_init(void)
{
ast_cli_register(&show_memory_allocations_cli);
ast_cli_register(&show_memory_summary_cli);
}
#endif

@ -29,8 +29,10 @@ struct ast_category {
char name[80];
struct ast_variable *root;
struct ast_category *next;
#ifdef PRESERVE_COMMENTS
struct ast_comment *precomments;
struct ast_comment *sameline;
#endif
};
struct ast_config {
@ -38,14 +40,18 @@ struct ast_config {
for now */
struct ast_category *root;
struct ast_category *prev;
#ifdef PRESERVE_COMMENTS
struct ast_comment *trailingcomments;
#endif
};
#ifdef PRESERVE_COMMENTS
struct ast_comment_struct
{
struct ast_comment *root;
struct ast_comment *prev;
};
#endif
static char *strip(char *buf)
{
@ -60,6 +66,7 @@ static char *strip(char *buf)
return start;
}
#ifdef PRESERVE_COMMENTS
static void free_comments(struct ast_comment *com)
{
struct ast_comment *l;
@ -69,6 +76,7 @@ static void free_comments(struct ast_comment *com)
free(l);
}
}
#endif
void ast_destroy(struct ast_config *ast)
{
@ -85,18 +93,24 @@ void ast_destroy(struct ast_config *ast)
vn = v;
free(v->name);
free(v->value);
#ifdef PRESERVE_COMMENTS
free_comments(v->precomments);
free_comments(v->sameline);
#endif
v = v->next;
free(vn);
}
catn = cat;
#ifdef PRESERVE_COMMENTS
free_comments(cat->precomments);
free_comments(cat->sameline);
#endif
cat = cat->next;
free(catn);
}
#ifdef PRESERVE_COMMENTS
free_comments(ast->trailingcomments);
#endif
free(ast);
}
@ -164,6 +178,7 @@ char *ast_variable_retrieve(struct ast_config *config, char *category, char *val
return NULL;
}
#ifdef PRESERVE_COMMENTS
int ast_variable_delete(struct ast_config *cfg, char *category, char *variable, char *value)
{
struct ast_variable *v, *pv, *bv, *bpv;
@ -377,6 +392,7 @@ struct ast_variable *ast_variable_append_modify(struct ast_config *config, char
}
return v;
}
#endif
int ast_category_exist(struct ast_config *config, char *category_name)
{
@ -393,6 +409,7 @@ int ast_category_exist(struct ast_config *config, char *category_name)
return 0;
}
#ifdef PRESERVE_COMMENTS
static struct ast_comment *build_comment(char *cmt)
{
struct ast_comment *c;
@ -406,22 +423,36 @@ static struct ast_comment *build_comment(char *cmt)
}
return c;
}
#endif
static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel
#ifdef PRESERVE_COMMENTS
, struct ast_comment_struct *acs
#endif
);
static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel, struct ast_comment_struct *acs);
static int cfg_process(struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, char *buf, int lineno, char *configfile, int includelevel, struct ast_comment_struct *acs)
static int cfg_process(struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, char *buf, int lineno, char *configfile, int includelevel
#ifdef PRESERVE_COMMENTS
,struct ast_comment_struct *acs
#endif
)
{
char *c;
char *cur;
struct ast_variable *v;
#ifdef PRESERVE_COMMENTS
struct ast_comment *com = NULL;
#endif
int object;
/* Strip off lines using ; as comment */
c = strchr(buf, ';');
if (c) {
*c = '\0';
#ifdef PRESERVE_COMMENTS
c++;
if (*c != '!')
com = build_comment(c);
#endif
}
cur = strip(buf);
if (strlen(cur)) {
@ -441,16 +472,20 @@ static int cfg_process(struct ast_config *tmp, struct ast_category **_tmpc, stru
memset(*_tmpc, 0, sizeof(struct ast_category));
strncpy((*_tmpc)->name, cur+1, sizeof((*_tmpc)->name) - 1);
(*_tmpc)->root = NULL;
#ifdef PRESERVE_COMMENTS
(*_tmpc)->precomments = acs->root;
(*_tmpc)->sameline = com;
#endif
if (!tmp->prev)
tmp->root = *_tmpc;
else
tmp->prev->next = *_tmpc;
tmp->prev = *_tmpc;
#ifdef PRESERVE_COMMENTS
acs->root = NULL;
acs->prev = NULL;
#endif
*_last = NULL;
} else {
ast_log(LOG_WARNING,
@ -484,7 +519,11 @@ static int cfg_process(struct ast_config *tmp, struct ast_category **_tmpc, stru
break;
}
if (includelevel < MAX_INCLUDE_LEVEL) {
__ast_load(cur, tmp, _tmpc, _last, includelevel + 1, acs);
__ast_load(cur, tmp, _tmpc, _last, includelevel + 1
#ifdef PRESERVE_COMMENTS
,acs
#endif
);
} else
ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", includelevel);
} else
@ -519,11 +558,13 @@ static int cfg_process(struct ast_config *tmp, struct ast_category **_tmpc, stru
v->lineno = lineno;
v->object = object;
/* Put and reset comments */
#ifdef PRESERVE_COMMENTS
v->precomments = acs->root;
v->blanklines = 0;
v->sameline = com;
acs->prev = NULL;
acs->root = NULL;
v->sameline = com;
#endif
v->blanklines = 0;
if (*_last)
(*_last)->next = v;
else
@ -541,6 +582,7 @@ static int cfg_process(struct ast_config *tmp, struct ast_category **_tmpc, stru
}
} else {
/* store any comments if there are any */
#ifdef PRESERVE_COMMENTS
if (com) {
if (acs->prev)
acs->prev->next = com;
@ -552,10 +594,12 @@ static int cfg_process(struct ast_config *tmp, struct ast_category **_tmpc, stru
(*_last)->blanklines++;
}
#endif
}
return 0;
}
#ifdef PRESERVE_COMMENTS
static void dump_comments(FILE *f, struct ast_comment *comment)
{
while (comment) {
@ -563,6 +607,7 @@ static void dump_comments(FILE *f, struct ast_comment *comment)
comment = comment->next;
}
}
#endif
int ast_save(char *configfile, struct ast_config *cfg, char *generator)
{
@ -591,16 +636,22 @@ int ast_save(char *configfile, struct ast_config *cfg, char *generator)
fprintf(f, ";!\n");
cat = cfg->root;
while(cat) {
#ifdef PRESERVE_COMMENTS
/* Dump any precomments */
dump_comments(f, cat->precomments);
#endif
/* Dump section with any appropriate comment */
#ifdef PRESERVE_COMMENTS
if (cat->sameline)
fprintf(f, "[%s] ; %s\n", cat->name, cat->sameline->cmt);
else
#endif
fprintf(f, "[%s]\n", cat->name);
var = cat->root;
while(var) {
#ifdef PRESERVE_COMMENTS
dump_comments(f, var->precomments);
#endif
if (var->sameline)
fprintf(f, "%s %s %s ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
else
@ -621,7 +672,9 @@ int ast_save(char *configfile, struct ast_config *cfg, char *generator)
#endif
cat = cat->next;
}
#ifdef PRESERVE_COMMENTS
dump_comments(f, cfg->trailingcomments);
#endif
} else {
if (option_debug)
printf("Unable to open for writing: %s\n", fn);
@ -633,7 +686,11 @@ int ast_save(char *configfile, struct ast_config *cfg, char *generator)
return 0;
}
static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel, struct ast_comment_struct *acs)
static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel
#ifdef PRESERVE_COMMENTS
, struct ast_comment_struct *acs
#endif
)
{
char fn[256];
char buf[256];
@ -671,7 +728,11 @@ static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, s
fgets(buf, sizeof(buf), f);
lineno++;
if (!feof(f)) {
if (cfg_process(tmp, _tmpc, _last, buf, lineno, configfile, includelevel, acs)) {
if (cfg_process(tmp, _tmpc, _last, buf, lineno, configfile, includelevel
#ifdef PRESERVE_COMMENTS
, acs
#endif
)) {
fclose(f);
return NULL;
}
@ -684,12 +745,14 @@ static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, s
else if (option_verbose > 1)
ast_verbose( "Not found (%s)", strerror(errno));
}
#ifdef PRESERVE_COMMENTS
if (master) {
/* Keep trailing comments */
tmp->trailingcomments = acs->root;
acs->root = NULL;
acs->prev = NULL;
}
#endif
return tmp;
}
@ -697,8 +760,14 @@ struct ast_config *ast_load(char *configfile)
{
struct ast_category *tmpc=NULL;
struct ast_variable *last = NULL;
#ifdef PRESERVE_COMMENTS
struct ast_comment_struct acs = { NULL, NULL };
return __ast_load(configfile, NULL, &tmpc, &last, 0, &acs);
#endif
return __ast_load(configfile, NULL, &tmpc, &last, 0
#ifdef PRESERVE_COMMENTS
,&acs
#endif
);
}
char *ast_category_browse(struct ast_config *config, char *prev)

@ -0,0 +1,64 @@
/*
* Asterisk -- A telephony toolkit for Linux.
*
* Asterisk debugging
*
* Copyright (C) 2002, Mark Spencer
*
* Mark Spencer <markster@linux-support.net>
*
* This program is free software, distributed under the terms of
* the GNU General Public License
*/
#ifndef NO_AST_MM
#ifndef _ASTMM_H
#define _ASTMM_H
#define __AST_DEBUG_MALLOC
/* Include these now to prevent them from being needed later */
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
/* Undefine any macros */
#undef malloc
#undef calloc
#undef realloc
#undef strdup
#undef strndup
void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func);
void *__ast_malloc(size_t size, const char *file, int lineno, const char *func);
void __ast_free(void *ptr, const char *file, int lineno, const char *func);
void *__ast_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func);
char *__ast_strdup(const char *s, const char *file, int lineno, const char *func);
char *__ast_strndup(const char *s, size_t n, const char *file, int lineno, const char *func);
void __ast_mm_init(void);
/* Provide our own definitions */
#define calloc(a,b) \
__ast_calloc(a,b,__FILE__, __LINE__, __PRETTY_FUNCTION__)
#define malloc(a) \
__ast_malloc(a,__FILE__, __LINE__, __PRETTY_FUNCTION__)
#define free(a) \
__ast_free(a,__FILE__, __LINE__, __PRETTY_FUNCTION__)
#define realloc(a,b) \
__ast_realloc(a,b,__FILE__, __LINE__, __PRETTY_FUNCTION__)
#define strdup(a) \
__ast_strdup(a,__FILE__, __LINE__, __PRETTY_FUNCTION__)
#define strndup(a,b) \
__ast_strndup(a,b,__FILE__, __LINE__, __PRETTY_FUNCTION__)
#else
#error "NEVER INCLUDE astmm.h DIRECTLY!!"
#endif
#endif

@ -60,6 +60,7 @@ static void *autodial(void *ignore)
char * sendbufptr=sendbuf;
int fd=open(dialfile,O_RDONLY|O_NONBLOCK);
int flags = fcntl(fd, F_GETFL);
fd_set fds;
fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
printf("Entered Wil-Calu fd=%d\n",fd);
if(fd<0) {
@ -74,7 +75,11 @@ static void *autodial(void *ignore)
void *pass;
memset(buf,0,257);
FD_ZERO(&fds);
FD_SET(fd, &fds);
ast_select(fd + 1, &fds, NULL, NULL, NULL);
bytes=read(fd,buf,256);
printf("Bytes: %d\n", bytes);
buf[(int)bytes]=0;
if(bytes>0){

Loading…
Cancel
Save