[Linuxptp-users] The ptp4l application seems to have a memory leak (increase of memory allocation)
PTP IEEE 1588 stack for Linux
Brought to you by:
rcochran
From: Mario M. <Mar...@we...> - 2012-07-07 10:01:34
|
Hallo PTP4l members, I could observe a memory increasing by long term test of PTP4L application. (cat /proc/[PID of App]/status) The background of this memory leak is message pool handling in the function msg_allocate(void) and msg_put(). In the current implementation of this functions would every call of msg_allocate() make a new message allocation, because the condition if(!m) in msg_put() should always false and in the consequently is msg_pool always empty. My patch suggestion: @@ -146,12 +147,20 @@ struct ptp_message *msg_allocate(void) struct message_storage *s; struct ptp_message *m = TAILQ_FIRST(&msg_pool); if (!m) { + // pr_notice("ptp_message alloc new \n"); s = malloc(sizeof(*s)); if (s) { m = &s->msg; - m->refcnt = 1; + } else { + return NULL; } + } else { + TAILQ_REMOVE(&msg_pool, m, list); + //pr_notice("ptp_message\n"); } + + memset(m, 0, sizeof(*m)); + m->refcnt = 1; return m; } @@ -330,8 +339,11 @@ void msg_print(struct ptp_message *m, FILE *fp) void msg_put(struct ptp_message *m) { m->refcnt--; - if (!m) + if (m != TAILQ_FIRST(&msg_pool) && m->refcnt == 0 ) + { + // pr_notice("TAILQ_INSERT_HEAD %s %i",msg_type_string(msg_type(m)),m->refcnt); TAILQ_INSERT_HEAD(&msg_pool, m, list); + } } int msg_sots_missing(struct ptp_message *m) diff --git a/port.c b/port.c index bf5738f..9d3a966 100644 --- a/port.c +++ b/port.c @@ -366,7 +366,6 @@ static int port_pdelay_request(struct port *p) msg = msg_allocate(); if (!msg) return -1; - memset(msg, 0, sizeof(*msg)); pdulen = sizeof(struct pdelay_req_msg); msg->hwts.type = p->timestamping; @@ -414,7 +413,6 @@ static int port_delay_request(struct port *p) msg = msg_allocate(); if (!msg) return -1; - memset(msg, 0, sizeof(*msg)); pdulen = sizeof(struct delay_req_msg); msg->hwts.type = p->timestamping; @@ -461,7 +459,6 @@ static int port_tx_announce(struct port *p) msg = msg_allocate(); if (!msg) return -1; - memset(msg, 0, sizeof(*msg)); pdulen = sizeof(struct announce_msg); msg->hwts.type = p->timestamping; @@ -523,8 +520,6 @@ static int port_tx_sync(struct port *p) msg_put(msg); return -1; } - memset(msg, 0, sizeof(*msg)); - memset(fup, 0, sizeof(*fup)); pdulen = sizeof(struct sync_msg); msg->hwts.type = p->timestamping; @@ -767,7 +762,6 @@ static int process_delay_req(struct port *p, struct ptp_message *m) msg = msg_allocate(); if (!msg) return -1; - memset(msg, 0, sizeof(*msg)); pdulen = sizeof(struct delay_resp_msg); msg->hwts.type = p->timestamping; @@ -895,8 +889,6 @@ static int process_pdelay_req(struct port *p, struct ptp_message *m) msg_put(rsp); return -1; } - memset(rsp, 0, sizeof(*rsp)); - memset(fup, 0, sizeof(*fup)); rsp_len = sizeof(struct pdelay_resp_msg); rsp->hwts.type = p->timestamping; Attached on this email you could also find patch file I think another good way to solve this behavior is to use a circular message pool with a fix size. What do you think from this idea? Best regards, Mario |