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

Submitted by:  Mike Kleshov <kleshov>
Submitted on:  Fri 14 Sep 2007 09:54:03 AM UTC  
 
Category: pbufsSeverity: 2 - Minor
Item Group: Faulty BehaviourStatus: Fixed
Privacy: PublicAssigned to: Mike Kleshov <kleshov>
Open/Closed: ClosedPlanned 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>
Project MemberIn charge of this item.
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>
Project 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>
Project 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>
Project 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>
Project MemberIn charge of this item.
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>
Project MemberIn charge of this item.

 

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

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

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 0 encouragements so far.

    Only logged-in users can vote.

     

    Please enter the title of George Orwell's famous dystopian book (it's a date):

     

     

    Follow 4 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Sat 15 Sep 2007 12:02:22 PM UTCfbernonStatusNone=>Fixed
      Assigned toNone=>kleshov
      Planned Release=>1.3.0
    Sat 15 Sep 2007 11:30:24 AM UTCkleshovOpen/ClosedOpen=>Closed

    Back to the top


    Powered by Savane 3.1-cleanup1