On 05.11.2007 [18:28:21 -0600], Andrew Hastings wrote:
> Allow hugetlbfs_morecore to shrink the heap.

Thanks, much easier to follow now.

> Signed-off-by: Andrew Hastings <[EMAIL PROTECTED]> on behalf of Cray Inc.
> ---
> I've run the test suite with this on x86_64 and PPC64.
> 
>  morecore.c         |   63 
> ++++++++++++++++++++++++++++++++++++++---------------
>  tests/Makefile     |    2 -
>  tests/heapshrink.c |   48 ++++++++++++++++++++++++++++++++++++++++
>  tests/run_tests.sh |    2 +
>  4 files changed, 97 insertions(+), 18 deletions(-)
> 
> diff -ruNp libhugetlbfs-1.2-rename-newsize/morecore.c 
> libhugetlbfs-1.2-shrink/morecore.c
> --- libhugetlbfs-1.2-rename-newsize/morecore.c        2007-11-05 
> 18:20:06.831195000 -0600
> +++ libhugetlbfs-1.2-shrink/morecore.c        2007-11-02 17:32:04.000000000 
> -0500
> @@ -70,7 +70,7 @@ static void *hugetlbfs_morecore(ptrdiff_
>  {
>       int ret;
>       void *p;
> -     long delta = 0;
> +     long delta;
> 
>       DEBUG("hugetlbfs_morecore(%ld) = ...\n", (long)increment);
> 
> @@ -83,13 +83,11 @@ static void *hugetlbfs_morecore(ptrdiff_
>       DEBUG("heapbase = %p, heaptop = %p, mapsize = %lx, delta=%ld\n",
>             heapbase, heaptop, mapsize, delta);
> 
> -     /* growing the heap */
> +     /* align to multiple of hugepagesize. */
> +     delta = ALIGN(delta, blocksize);
> +
>       if (delta > 0) {
> -             /*
> -              * convert our request to a multiple of hugepages
> -              * we will have more space allocated then used, basically
> -              */
> -             delta = ALIGN(delta, blocksize);
> +             /* growing the heap */
> 
>               DEBUG("Attempting to map %ld bytes\n", delta);
> 
> @@ -134,6 +132,46 @@ static void *hugetlbfs_morecore(ptrdiff_
> 
>               /* we now have mmap'd further */
>               mapsize += delta;
> +     } else if (delta < 0) {
> +             /* shrinking the heap */
> +
> +             if (!mapsize) {
> +                     WARNING("Can't shrink empty heap!");
> +                     return NULL;
> +             }
> +
> +             /*
> +              * If we are forced to change the heapaddr from the
> +              * original brk() value we have violated brk semantics
> +              * (which we are not supposed to do).  This shouldn't
> +              * pose a problem until glibc tries to trim the heap to an
> +              * address lower than what we aligned heapaddr to.  At that
> +              * point the alignment "gap" causes heap corruption.
> +              * So we don't allow the heap to shrink below heapbase.
> +              */
> +             if (mapsize + delta < 0) {

Since this delta is known to be negative, this is really asking

        if (mapsize < abs(delta))

right? That is, we can't unmap more than we have mapped. Just trying to
make sure I follow.

> +                     WARNING("Unable to shrink heap below %p\n", heapbase);
> +                     delta = -mapsize;

So we just unmap what we can instead.

> +                     increment = heapbase - heaptop;

a.k.a. the whole heap.

> +             }
> +             DEBUG("Attempting to unmap %ld bytes @ %p\n", -delta,
> +                     heapbase + mapsize + delta);
> +             ret = munmap(heapbase + mapsize + delta, -delta);
> +             if (ret) {
> +                     WARNING("Unmapping failed in hugetlbfs_morecore()\n");

Maybe a strerror(errno) in there, so we might be able to see why?

> +             } else {
> +
> +                     /*
> +                      * Now shrink the hugetlbfs file.
> +                      */
> +                     mapsize += delta;
> +                     ret = ftruncate(heap_fd, mapsize);
> +                     if (ret) {
> +                             WARNING("Couldn't toss pages in "
> +                                     "hugetlbfs_morecore()\n");

I would just say "Couldn't truncate hugetlbfs file in
hugetlbfs_morecore()". Maybe a strerror(errno) here too.

> diff -ruNp libhugetlbfs-1.2-rename-newsize/tests/heapshrink.c 
> libhugetlbfs-1.2-shrink/tests/heapshrink.c
> --- libhugetlbfs-1.2-rename-newsize/tests/heapshrink.c        1969-12-31 
> 18:00:00.000000000 -0600
> +++ libhugetlbfs-1.2-shrink/tests/heapshrink.c        2007-11-02 
> 14:56:11.000000000 -0500

Testcase seems sane.

Does the HOWTO need updating at all?

Thanks,
Nish

-- 
Nishanth Aravamudan <[EMAIL PROTECTED]>
IBM Linux Technology Center

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Libhugetlbfs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/libhugetlbfs-devel

Reply via email to