-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Heap fragmentation problem in malloc #23
Comments
Using multiple bins might mitigate fragmentations. In the current malloc implementation, there's only one bin (or aka free list) Line 8 in 93d3f4d
Multiple bins, as its name suggests, malloc has multiple In multiple bins approach, malloc does not split the chunk except ones in the bin for remaining big ones. That is, once a chunk splitted into 16 bytes long, it will be in the bin for 16-bytes chunks forever. I believe this approach work well. |
Thank you for the wonderful explanation. I have a quick question. Pardon me if this is naive. Do we decide the number of chunks of sizes 8 bytes, 16 bytes etc. beforehand? Also, are all malloc requests multiple of 8 bytes in resea as described in the page you linked? If not, we still have some internal fragmentation there right? |
Yes. We don't need to change the size of chunk in each bin dynamically. I recommend using power of 2 for chunk sizes and since existing Resea applications don't need large chunks, I suppose 16 is a good number of bins (i.e. the first bin have
Good point. Internal fragmentation is still inevitable in this multiple bins approach. To eliminate internal fragmentation, I think we need another solution such as the slab allocator. |
Are all the chunks free? If so, why do we have the below condition in the loop - if (chunk->magic != MALLOC_FREE) {
continue;
} Also, I am assuming that initially
|
Seems you're right. All chunks in the
That's correct. Initially, we have a single chunk which points to the whole heap.
I think this way is better. As you say we might need further improvements around the splitting, but I suppose it's way better than the current first-fit approach. |
Okay. So here's what I think we can do:
static struct malloc_chunk *chunks[NUM_CHUNKS]
Please let me know your views and suggestions. |
Sounds good! Let's try it. |
I have some comments regarding malloc tests.
|
Is there a way I can use size_t get_chunk_number_from_size(size_t size) {
// If requested size is less or equal to the size of second largest chunk
// (the last fixed chunk)
if (size <= 1 << (NUM_CHUNKS - 2)) {
return ceil(log2(size));
}
// Return index of the last, dynamic-sized chunk
return NUM_CHUNKS - 1;
} |
About the tests, noted! Let me try to figure out how to write proper tests. (I have written unittests for Django REST APIs in the past) |
log2 has been deleted because it was no longer used. It might be good idea to re-add it to libs/resea. Since size is an integer, I believe there're clever algorithm using bitwise operators. That said, as the first step, how about a naive implementation using a size_t get_chunk_number_from_size(size_t size) {
for (int i = 0; i < NUM_CHUNKS; i++) {
if (size <= 1 << i) {
return i;
}
}
return NUM_CHUNKS - 1;
} |
Okay yes. This sounds fair for a start. Once we get everything to work, we'll probably consider optimizing this. Thank you! |
I added the function definition in test.h and added I have implemented a |
Can you share your code on GitHub so I can take a look into the problem?
On Tue, Oct 20, 2020 at 22:27 Yashraj Kakkad ***@***.***> wrote:
I added the function definition in test.h and added malloc_test.o in the
associated build.mk file. Is that enough? I think my test is not running
as even ASSERT(1==2) is not creating a warning.
I have implemented a malloc but as it crashed the OS I renamed it to
mymalloc to test it properly first.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#23 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABGR2EVG7AKTKBSNMHXRJITSLWF4NANCNFSM4RXHRHCQ>
.
--
Seiya
|
Yes, here's the branch we are working on - https://github.com/yashrajkakkad/resea/tree/malloc/libs/resea
|
Got the problem @nuta! I had forgotten to modify main.c. Meanwhile, how much memory does a typical resea application ask for as of now? |
I have got the preliminary tests to work. You can find it in the same branch and the same files as I mentioned before. I found one or two small bugs when I gave my implementation a second thought. I am working on that. Meanwhile, the |
Meanwhile, how much memory does a typical resea application ask for as of now?
Do you mean the typical chunk size (i.e. x in malloc(x)) or the total
memory consumption? I've never measured both metrics.
Meanwhile, the MALLOC_FRAME_LEN in my system is 64. I suppose that's a constant value. Is there any point therefore to have chunks smaller than that? The system crashes right now because ASSERT(len > MALLOC_FRAME_LEN) fails.
Currently the chunk *header* must be larger than 64 bytes. The chunk
capacity can be less than 64 bytes.
…On Wed, Oct 21, 2020 at 8:05 PM Yashraj Kakkad ***@***.***> wrote:
I have got the preliminary tests to work. You can find it in the same
branch and the same files as I mentioned before. I found one or two small
bugs when I gave my implementation a second thought. I am working on that.
Meanwhile, the MALLOC_FRAME_LEN in my system is 64. I suppose that's a
constant value. Is there any point therefore to have chunks smaller than
that? The system crashes right now because ASSERT(len > MALLOC_FRAME_LEN)
fails.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#23 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABGR2EW4NH4FET6BV5WXOE3SL26A3ANCNFSM4RXHRHCQ>
.
|
Apologies for the delayed response. I actually had misunderstood some part of the code. Nevertheless, I have fixed the impending issues, integrated my new malloc to our branch and the tests have passed too. I have used |
Two integrated tests are apparently failing in GitHub actions - https://github.com/yashrajkakkad/resea/runs/1315001384?check_suite_focus=true |
Looks like your failing tests are running too long than expected so just
ignore them.
I've added some improvements on integrated tests. Please pull and try the
latest master branch.
…On Tue, Oct 27, 2020 at 10:57 PM Yashraj Kakkad ***@***.***> wrote:
Two integrated tests are apparently failing in GitHub actions -
https://github.com/yashrajkakkad/resea/runs/1315001384?check_suite_focus=true
One is related to DHCP and other is related to shell. I am not sure what I
ended up breaking. I am trying to investigate into that as well.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#23 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABGR2EUQV3OOWESHEHTCMY3SM3GUFANCNFSM4RXHRHCQ>
.
|
Specifically, the timeout for each test is specified through the
`--timeout` option:
https://github.com/nuta/resea/blob/master/.github/workflows/integrated_tests.yml#L82
…On Wed, Oct 28, 2020 at 9:19 PM Seiya Nuta ***@***.***> wrote:
Looks like your failing tests are running too long than expected so just
ignore them.
I've added some improvements on integrated tests. Please pull and try the
latest master branch.
On Tue, Oct 27, 2020 at 10:57 PM Yashraj Kakkad ***@***.***>
wrote:
> Two integrated tests are apparently failing in GitHub actions -
> https://github.com/yashrajkakkad/resea/runs/1315001384?check_suite_focus=true
> One is related to DHCP and other is related to shell. I am not sure what
> I ended up breaking. I am trying to investigate into that as well.
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <#23 (comment)>, or
> unsubscribe
> <https://github.com/notifications/unsubscribe-auth/ABGR2EUQV3OOWESHEHTCMY3SM3GUFANCNFSM4RXHRHCQ>
> .
>
|
They still fail with a timeout - https://github.com/yashrajkakkad/resea/runs/1320690388?check_suite_focus=true Also, minlin application shows an error - 'Already exists' when I run it in my system. Rest looks fine. I did some debugging but nothing seems suspicious from my changes. |
Please ignore this. This shows up in resea's master branch as well. |
If you think your implementation is ready for the review, please feel free to send a pull request even if some CI tests are failing. They could be newly discovered bugs thanks to your new malloc implementation. |
Just did that! I request you to check! |
Closed by #27 |
The current
malloc()
implementation uses a naive algorithm and it easily creates heap fragmentation. Obviously we need to fix this.resea/libs/resea/malloc.c
Lines 66 to 88 in 93d3f4d
The text was updated successfully, but these errors were encountered: