Skip to content
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

mm/mm_heap : realtime support for malloc/free functions performing wi… #3398

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

davidfather
Copy link
Contributor

…thin a constant time

Do memory allocation and releasing within a constant time
if the requested size is less than or equal to (16 * MM_REALTIME_SUPPORT_NUMOF_SIZES),
where MM_REALTIME_SUPPORT_NUMOF_SIZES is the number of sizes for realtime support,
starting from 16 bytes with 16 bytes step.
For example, if MM_REALTIME_SUPPORT_NUMOF_SIZES is 3,
16, 32, 48 bytes memory can be allocated within a constant time.
This number should be positive integer.

MM_REALTIME_SUPPORT_NUMS is the list of numbers each of which means the number of
a certain-sized free nodes which can be used for memory allocation within a constant time.
For example, by default, MM_REALTIME_SUPPORT_NUMS = "100 100 100" means
a hundred free nodes for each size, i.e, 16, 32, 48 bytes,
are reserved for realtime support. The total amount of memory sizes
for realtime support is calculated by (16 * N1 + 32 * N2 + ... + 16 * n * Nn) bytes,
where n is the number of sizes (i.e., MM_REALTIME_SUPPORT_NUMOF_SIZES)
and Nn is the number of free nodes for the size of 16 * n bytes.
These numbers should not be negative integers.
If n is larger than the number of these numbers, non-defined numbers of free nodes
become, by default, zero.

@seinfra
Copy link

seinfra commented Jun 15, 2019

Target : [006d616eb3bff6dc9380778460179f076c815d60] - Code Rule Check (C++) OK.

@seinfra
Copy link

seinfra commented Jun 15, 2019

Target : [006d616eb3bff6dc9380778460179f076c815d60] - Code Rule Check Result:
os/mm/mm_heap/mm_initialize.c:158: ERROR: [SPC_M_KWD] space required before the open parenthesis '('
os/mm/mm_heap/mm_initialize.c:160: ERROR: [SPC_M_KWD] space required before the open parenthesis '('
os/mm/mm_heap/mm_size2ndx.c:88: ERROR: [SPC_M_KWD] space required before the open parenthesis '('
os/mm/mm_heap/mm_size2ndx.c:130: ERROR: [SPC_M_KWD] space required before the open parenthesis '('

@seinfra
Copy link

seinfra commented Jun 15, 2019

Target : [eac8f6315338d20ea3a0357bcf528661c22c7163] - Code Rule Check (C++) OK.

@seinfra
Copy link

seinfra commented Jun 15, 2019

Target : [eac8f6315338d20ea3a0357bcf528661c22c7163] - Code Rule Check Result:
os/mm/mm_heap/mm_initialize.c:158: ERROR: [SPC_M_KWD] space required before the open parenthesis '('
os/mm/mm_heap/mm_initialize.c:160: ERROR: [SPC_M_KWD] space required before the open parenthesis '('
os/mm/mm_heap/mm_size2ndx.c:88: ERROR: [SPC_M_KWD] space required before the open parenthesis '('
os/mm/mm_heap/mm_size2ndx.c:130: ERROR: [SPC_M_KWD] space required before the open parenthesis '('

@seinfra
Copy link

seinfra commented Jun 15, 2019

Target : [b21610aa00c5f1d4b7ec8271c304d868f6af2145] - Code Rule Check (C++) OK.

@seinfra
Copy link

seinfra commented Jun 15, 2019

Target : [b21610aa00c5f1d4b7ec8271c304d868f6af2145] - Code Rule Check Result:
os/mm/mm_heap/mm_initialize.c:158: ERROR: [SPC_M_KWD] space required before the open parenthesis '('
os/mm/mm_heap/mm_initialize.c:160: ERROR: [SPC_M_KWD] space required before the open parenthesis '('

@seinfra
Copy link

seinfra commented Jun 15, 2019

Target : [2fe08620cc5f5a0c653eb63d991f16cb62858b77] - Code Rule Check (C++) OK.

@seinfra
Copy link

seinfra commented Jun 15, 2019

Target : [2fe08620cc5f5a0c653eb63d991f16cb62858b77] - Code Rule Check Result:
os/mm/mm_heap/mm_initialize.c:158: ERROR: [SPC_M_KWD] space required before the open parenthesis '('
os/mm/mm_heap/mm_initialize.c:160: ERROR: [SPC_M_KWD] space required before the open parenthesis '('

@seinfra
Copy link

seinfra commented Jun 15, 2019

Target : [c57311982eb4d58d28cc9123404f7f8b389b2702] - Code Rule Check OK.

@seinfra
Copy link

seinfra commented Jun 15, 2019

Target : [c57311982eb4d58d28cc9123404f7f8b389b2702] - Code Rule Check (C++) OK.

#define MM_NNODES (MM_MAX_SHIFT - MM_MIN_SHIFT + 1)
#endif // CONFIG_MM_REALTIME_SUPPORT

#define MM_GRAN_MASK (MM_MIN_CHUNK-1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide space before and after '-'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is not modified. Let's just keep it as it is.


else
install:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this extra line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is like a template. Let's just keep it as it is.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK


else
context:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is like a template. Let's just keep it as it is.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

{
char *addr;
int ndx;
uint32_t miss[MM_REALTIME_SUPPORT_NUMOF_SIZES] = {0, };

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide space after open brace.
like: { 0, }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@seinfra
Copy link

seinfra commented Jun 18, 2019

Target : [39bc5e7384e2285ac42d7bd51d0b91e29d8f8c10] - Code Rule Check (C++) OK.

@seinfra
Copy link

seinfra commented Jun 18, 2019

Target : [39bc5e7384e2285ac42d7bd51d0b91e29d8f8c10] - Code Rule Check OK.


addr = (char *)malloc(sizeof(char));

if (argc > 2) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 comments here.

  1. null check after malloc needed but I think you can use unsigned with out mem alloc.
  2. You'd better modify it to switch-case if you consider expand it.
  3. argc > 2 is intended code? actually you already mentioned usage in else state, it means you restrict usage.
    So Error case also should be there if argc is greater than 2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, my feedback.

  1. Null check is necessary, but this mem alloc is intended to get heap stat.
  2. I'll do when I need to extend this function further.
  3. if argc <= 2, 'heapstat' just goes well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean if user type heapstat reset blah blah blah it also works. is it intended? of course some os accepts it but some other doesn't accept it. I was just curious because you added usaage below..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it was intentionally allowed to keep the code simple. Thanks for your comment, though.

@@ -149,6 +149,9 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem)
/* Check if the following node is free and, if so, merge it */

next = (FAR struct mm_freenode_s *)((char *)node + node->size);
#ifdef CONFIG_MM_REALTIME_SUPPORT
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Below is our style.

#ifdef CONFIG_MM_REALTIME_SUPPORT
        if (A)
#endif
        {
               do_something();
        }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Thanks.

struct mm_heap_s *heap;
int ndx;

heap = mm_get_heap(addr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you'd better change type from void, and return error if mm_get_heap returns null

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Thanks.

mm_givesemaphore(heap);
}

void mm_heapstat_reset(char *addr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Thanks.

heap->mm_realtime_miss[ndx] = 0;
}
mm_givesemaphore(heap);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we separate binary, application call these 2 apis? It's not registered syscall.
I Think you'd better expand proc if you want to display info of heap stat in application..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are included in user library.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it.


for (i = 0; i < MM_REALTIME_SUPPORT_NUMOF_SIZES; ++i) {
if (heap->mm_realtime_num[i] < 1) {
heap->mm_realtime_num[i] = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about use unsigned int?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so because it's not a size but a signed number.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean it allowed negative value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, but I checked whether the input is negative at line 170.
However, if this var is unsigned, it might be greater than the maximum number.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mentioned unsigned type because it is stable for kind of size of buffer, address, length, and so on.
We usually use size_t or ssize_t for it, That's why I commented it, choice is yours.


/* First, add one big chunk of free node for non-realtime. */
node = (FAR struct mm_freenode_s *)(heapbase + SIZEOF_MM_ALLOCNODE);
node->size = heapsize - 2 * SIZEOF_MM_ALLOCNODE - mem_pool_size;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You had better add bracket for multiplication.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's necessary, but fixed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes minor one, just for readability, thanks.

mm_heapstat_reset(addr);
printf("\nReset the stat of heap for realtime support.");
} else {
printf("Usage: %s [reset]\n", argv[1]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you need free(addr) here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, dear.
Fixed. Thanks.

size_t miss[MM_REALTIME_SUPPORT_NUMOF_SIZES] = { 0, };
int num[MM_REALTIME_SUPPORT_NUMOF_SIZES] = { 0, };

addr = (char *)malloc(sizeof(char));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so that add free(addr)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Thanks.

@seinfra
Copy link

seinfra commented Jun 20, 2019

Target : [51e7c0d1d16daa63a5cd18e9e5917c32d9e62c11] - Code Rule Check (C++) OK.

@seinfra
Copy link

seinfra commented Jun 20, 2019

Target : [51e7c0d1d16daa63a5cd18e9e5917c32d9e62c11] - Code Rule Check OK.

@@ -671,6 +695,9 @@ void mm_addfreechunk(FAR struct mm_heap_s *heap, FAR struct mm_freenode_s *node)
/* Functions contained in mm_size2ndx.c.c ***********************************/

int mm_size2ndx(size_t size);
#ifdef CONFIG_MM_REALTIME_SUPPORT
Copy link
Contributor

@YiHyunJin YiHyunJin Jun 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the below?

#ifdef CONFIG_MM_REALTIME_SUPPORT
int mm_size2ndx_realtime(FAR struct mm_heap_s *heap, size_t size);
#else
int mm_size2ndx(size_t size);
#endif

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment, but it's an additional function in case of realtime support.

#endif

size_t tsize = (MM_REALTIME_SUPPORT_NUMOF_SIZES + 2) * MM_REALTIME_SUPPORT_STEP_SIZE;
heap->mm_ndx_offset = (tsize >> MM_MIN_SHIFT) - mm_size2ndx(tsize) - 2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't mm_size2ndx_realtime() instead of mm_size2ndx()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment, but in order to compute the offset the original function, mm_size2ndx(), is required.

…thin a constant time

Do memory allocation and releasing within a constant time
if the requested size is less than or equal to (16 * MM_REALTIME_SUPPORT_NUMOF_SIZES),
where MM_REALTIME_SUPPORT_NUMOF_SIZES is the number of sizes for realtime support,
starting from 16 bytes with 16 bytes step.
For example, if MM_REALTIME_SUPPORT_NUMOF_SIZES is 3,
16, 32, 48 bytes memory can be allocated within a constant time.
This number should be positive integer.

MM_REALTIME_SUPPORT_NUMS is the list of numbers each of which means the number of
a certain-sized free nodes which can be used for memory allocation within a constant time.
For example, by default, MM_REALTIME_SUPPORT_NUMS = "100 100 100" means
a hundred free nodes for each size, i.e, 16, 32, 48 bytes,
are reserved for realtime support. The total amount of memory sizes
for realtime support is calculated by (16 * N1 + 32 * N2 + ... + 16 * n * Nn) bytes,
where n is the number of sizes (i.e., MM_REALTIME_SUPPORT_NUMOF_SIZES)
and Nn is the number of free nodes for the size of 16 * n bytes.
These numbers should not be negative integers.
If n is larger than the number of these numbers, non-defined numbers of free nodes
become, by default, zero.
…stat of heap for realtime support

'heapstat' is an utility app which shows the number of memory requests not allocated within a constant time.
When MM_REALTIME_SUPPORT_NUMOF_SIZES = 8 and MM_REALTIME_SUPPORT_NUMS = "200 200 0 150 0 0 0 150",
'heapstat' might print out the current status as shown below:
Size 16 bytes : 10 times, given 200 reserved nodes.
Size 32 bytes : 20 times, given 200 reserved nodes.
Size 48 bytes : 30 times, given 0 reserved nodes.
Size 64 bytes :  0 times, given 150 reserved nodes.
Size 80 bytes :  5 times, given 0 reserved nodes.
Size 96 bytes :  0 times, given 0 reserved nodes.
Size 112 bytes : 0 times, given 0 reserved nodes.
Size 128 bytes : 8 times, given 150 reserved nodes.
@seinfra
Copy link

seinfra commented Jun 22, 2019

Target : [c5107ec] - Code Rule Check OK.

@seinfra
Copy link

seinfra commented Jun 22, 2019

Target : [c5107ec] - Code Rule Check (C++) OK.

@@ -0,0 +1,3 @@
config ENTRY_HEAP_STAT_REALTIME
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

Please add below Kconfig header as we follow in TizenRT.

#
# For a description of the syntax of this configuration file,
# see kconfig-language at https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt
#

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't see the same header in other Kconfig_ENTRY files.
I will add this header in all Kconfig_ENTRY files if necessary.

@tizen-build
Copy link

Target : [c5107ec] - Code Rule Check (C++) OK.

@tizen-build
Copy link

Target : [c5107ec] - Code Rule Check OK.

1 similar comment
@tizen-build
Copy link

Target : [c5107ec] - Code Rule Check OK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants