buglwIP - A Lightweight TCP/IP stack - Bugs: bug #21077, lwip_stats.mem.used is inaccurate,...

 
 

You are not allowed to post comments on this tracker with your current authentication level.

bug #21077: lwip_stats.mem.used is inaccurate, sometimes negative

Submitter:  Mike Kleshov <kleshov>
Submitted:  Fri 14 Sep 2007 09:54:03 AM UTC
   
 
Category:  pbufs Severity:  2 - Minor
Item Group:  Faulty Behaviour Status:  Fixed
Privacy:  Public Assigned to:  kleshov
Open/Closed:  Closed Planned Release:  None
lwIP version:  None

Sat 15 Sep 2007 11:30:24 AM UTC, comment #5: 

Thanks for the feedback.
I'm checking in the fix and closing the bug.

Mike Kleshov <kleshov>
Group Member
Sat 15 Sep 2007 10:06:48 AM UTC, comment #4: 

And the suggested fix for mem.c, line 470 seems to work: the two last stats_display give:

MEM HEAP
        avail: 10240
        used: 28
        max: 84
        err: 0

MEM HEAP
        avail: 10240
        used: 0
        max: 84
        err: 0

Frédéric Bernon <fbernon>
Group Member
Sat 15 Sep 2007 10:02:26 AM UTC, comment #3: 

Simple code to show the problem (with the current CVS HEAD):

void main()
{
   void* pmem[4]={NULL};
  
   lwip_init();

   stats_display();
   pmem[0]=mem_malloc(7);
   pmem[1]=mem_malloc(23);
   pmem[2]=mem_malloc(17);
   pmem[3]=mem_malloc(11);
   stats_display();
   mem_free(pmem[1]);
   pmem[1]=mem_malloc(3);
   stats_display();
   mem_free(pmem[2]);
   mem_free(pmem[3]);
   mem_free(pmem[0]);
   stats_display();
   mem_free(pmem[1]);
   stats_display();
}

With such values in lwipopts.h :

#define MEM_ALIGNMENT           1
#define MEM_SIZE                10240

I got this output :

MEM HEAP
        avail: 10240
        used: 0
        max: 0
        err: 0

MEM HEAP
        avail: 10240
        used: 84
        max: 84
        err: 0

MEM HEAP
        avail: 10240
        used: 73
        max: 84
        err: 0

MEM HEAP
        avail: 10240
        used: 17
        max: 84
        err: 0

MEM HEAP
        avail: 10240
        used: 65525
        max: 84
        err: 0

Frédéric Bernon <fbernon>
Group Member
Fri 14 Sep 2007 08:50:07 PM UTC, comment #2: 

I've had a look through the code and your description, and your rationale and suggested fix look correct to me.

Jifl

Jonathan Larmour <jifl>
Group Member
Fri 14 Sep 2007 08:33:56 PM UTC, comment #1: 

Having spent some time analyzing the code in mem.c, I think I found where the problem comes from.

In the function mem_malloc(), lwip_stats.mem.used is updated as follows (line 470):
lwip_stats.mem.used += (size + SIZEOF_STRUCT_MEM);

But a few lines before that (see line 459), a block is reused whose size is larger than the requested size. Note the comment:

  • also can't move mem->next directly behind mem, since mem->next
  • will always be used at this point!


Taking this into account, in the mentioned special case lwip_stats.mem.used should be updated using mem->next:
lwip_stats.mem.used += mem->next - ((u8_t )mem - ram); / suggested fix */

By the way, mem->next is used to update it in mem_free():
lwip_stats.mem.used -= mem->next - ((u8_t *)mem - ram);

A quick test confirms that the fix works, lwip_stats.mem.used seems to behave normal.
Can anyone comment on this? I can commit the changes if there are no objections.

- Mike

Mike Kleshov <kleshov>
Group Member
Fri 14 Sep 2007 09:54:03 AM UTC, original submission:  

While debugging my lwip-based application with LWIP_STATS switched on, I found that lwip_stats.mem.used was assuming negative values (say, lwip_stats.mem.used == 65532 which is effectively -4.)

There are only a few places where lwip_stats.mem.used is updated. My guess is that it's mem.c, line 274:
lwip_stats.mem.used -= mem->next - ((u8_t *)mem - ram);

But I could be completely wrong. Could someone with knowledge of inner workings of lwip memory management have a look at this?

Mike Kleshov <kleshov>
Group Member

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by fbernon (Posted a comment)
  • -email is unavailable- added by jifl (Posted a comment)
  • -email is unavailable- added by kleshov (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

     

    Follow 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2007-09-15 fbernon StatusNone Fixed
        Assigned toNone kleshov
        Planned Release 1.3.0
    2007-09-15 kleshov Open/ClosedOpen Closed

    Back to the top

    Powered by Savane 3.12