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

Add enable/disable cb function in mem_trace #7948

Merged
merged 1 commit into from
Oct 18, 2018

Conversation

kegilbert
Copy link
Contributor

Description

This came up in: #7888

This adds a slightly cleaner method to enabling/disabling the memory tracing callbacks as outlined the above issue.

Tested with:

#include "mbed.h"                                                
#include "mbed_mem_trace.h"                                      
                                                                 
int main() {                                                     
    printf("RUNNING....\r\n");                                   
                                                                 
    mbed_mem_trace_set_callback(mbed_mem_trace_default_callback);
    uint16_t count = 0;                                          
                                                                 
    while(true) {                                                
        void *p = malloc(50);                                    
        wait(0.5f);                                              
        free(p);                                                 
        count += 1;                                              
                                                                 
        if ((count % 5) == 0) {                                  
            if (count % 10 != 0) {                               
                printf("Disabling cb in main...\r\n");           
                mbed_mem_trace_disable();                        
            } else {                                             
                printf("Setting cb to default...\r\n");          
                mbed_mem_trace_enable();                         
            }                                                    
        }                                                        
    }                                                            
}                                                                

@deepikabhavnani

Pull request type

[X] Fix
[ ] Refactor
[ ] Target update
[ ] Functionality change
[ ] Breaking change

@@ -45,6 +46,18 @@ void mbed_mem_trace_set_callback(mbed_mem_trace_cb_t cb) {
mem_trace_cb = cb;
}

void mbed_mem_trace_disable()
{
mem_trace_cb_reserve = mem_trace_cb;

Choose a reason for hiding this comment

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

Will it be interesting to check it in multi threaded system, when some thread trying to activate it and other disable it. I am not sure if that is a valid use case or not?

Copy link
Contributor Author

@kegilbert kegilbert Aug 31, 2018

Choose a reason for hiding this comment

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

@deepikabhavnani Good point! Updated it by adding the mutex locks used around malloc calls in the enable/disable functions. Do you think that'd be sufficient? Writing up a quick test in the meantime. Will squash the commits later if it works out fine.

@kegilbert
Copy link
Contributor Author

kegilbert commented Aug 31, 2018

There still could be the concern with someone using different callbacks in different threads while enabling/disabling logging output as two subsequent disables would wipe out the first cb state. Added a change to check for the current cb pointer to see if it was already null and not to disable twice. This would cause the second disable to lose its state however. Wanted to allow an internal storage mechanism for the original cb to prevent needing to have the user maintain the pointer and pass it around between threads potentially but this does add some complexity to corner cases like this.

@kegilbert
Copy link
Contributor Author

I'll also add a check on the currently set cb to ensure it's null before overwriting it with the reserve to stop the following:

Set_cb(...) 
Enable() 

@0xc0170 0xc0170 requested a review from a team September 3, 2018 08:20
Copy link

@deepikabhavnani deepikabhavnani left a comment

Choose a reason for hiding this comment

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

Looks good to me 👍

@NirSonnenschein
Copy link
Contributor

/morph build

@mbed-ci
Copy link

mbed-ci commented Oct 3, 2018

Build : SUCCESS

Build number : 3220
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/7948/

Triggering tests

/morph test
/morph mbed2-build

@mbed-ci
Copy link

mbed-ci commented Oct 3, 2018

@NirSonnenschein
Copy link
Contributor

/morph test

1 similar comment
@studavekar
Copy link
Contributor

/morph test

@mbed-ci
Copy link

mbed-ci commented Oct 7, 2018

@NirSonnenschein
Copy link
Contributor

/morph export-build

@mbed-ci
Copy link

mbed-ci commented Oct 7, 2018

@NirSonnenschein
Copy link
Contributor

passed CI , but still needs maintainer review. @0xc0170 @cmonr @adbridge anyone have time to review?

void mbed_mem_trace_enable()
{
mbed_mem_trace_lock();
if (mem_trace_cb == 0 && mem_trace_cb_reserve) {
Copy link
Contributor

Choose a reason for hiding this comment

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

'If (mem_trace_cb == 0)' this is inconsistent with the previous function where 'if (mem_trace_cb)' is used. One is checking a value directly and one is treating the variable as a logical True/False (which I assume is how it should be used). If the latter is indeed true then the checking against 0 should actually be replaced by:
if (!mem_trace_cb).

@adbridge
Copy link
Contributor

adbridge commented Oct 8, 2018

@kegilbert please address comment.

@kegilbert
Copy link
Contributor Author

@adbridge Updated

@cmonr
Copy link
Contributor

cmonr commented Oct 12, 2018

Note: This could probably come into 10.2.

@0xc0170
Copy link
Contributor

0xc0170 commented Oct 17, 2018

Note: This could probably come into 10.2.

enhancement - new functionality thus 5.11 is good here.

@cmonr
Copy link
Contributor

cmonr commented Oct 17, 2018

/morph build

@mbed-ci
Copy link

mbed-ci commented Oct 17, 2018

Build : SUCCESS

Build number : 3393
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/7948/

Triggering tests

/morph test
/morph export-build
/morph mbed2-build

@mbed-ci
Copy link

mbed-ci commented Oct 18, 2018

@mbed-ci
Copy link

mbed-ci commented Oct 18, 2018

@cmonr cmonr merged commit ad6ada0 into ARMmbed:master Oct 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants