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 tracing and counting of built-in operations #3713

Merged
merged 2 commits into from
Dec 18, 2019

Conversation

ChrisJefferson
Copy link
Contributor

This is some code I've had a branch of a long time, cleaned up.

Sometimes, for papers and general research it is interesting to know things like how many times + is applied to integers, or * or ^ is applied to permutations.

This can't be done by any of GAP's normal tracing functionality at present. Also, printing a message every time one of these functions is called would be far too much.

This adds TraceInternalMethods, which produces a record which contains how many times each operation is applied to each tnum (or each pair of tnums for 2-argument methods).

This doesn't try to catch when kernel functions add integers, only when these operations are performed at the GAP level.

This still needs tests, but before I put more work into polishing I wanted to see if other people found it useful.

@coveralls
Copy link

coveralls commented Oct 23, 2019

Coverage Status

Coverage remained the same at 84.691% when pulling e1d8612 on ChrisJefferson:tracking_ops into e6d5121 on gap-system:master.

@ChrisJefferson ChrisJefferson added kind: enhancement Label for issues suggesting enhancements; and for pull requests implementing enhancements release notes: to be added PRs introducing changes that should be (but have not yet been) mentioned in the release notes labels Oct 23, 2019
Copy link
Contributor

@DominikBernhardt DominikBernhardt left a comment

Choose a reason for hiding this comment

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

I think this is a helpful function, I've missed it in the past, so I would be happy for it to go into GAP. I have a few suggestions, see above. Also, it would be nice to add tests before merging this.

lib/oper.g Outdated Show resolved Hide resolved
lib/oper.g Outdated Show resolved Hide resolved
lib/oper.g Outdated Show resolved Hide resolved
src/tracing.c Outdated Show resolved Hide resolved
src/tracing.h Outdated Show resolved Hide resolved
src/tracing.h Outdated Show resolved Hide resolved
@ChrisJefferson
Copy link
Contributor Author

ChrisJefferson commented Nov 25, 2019

Thanks for the corrections. I've just put an explicit list of the "internal methods" which are traced, along with the name GAP gives them (which is the name of the internal variable, but might not be obvious so is worth explictly stating I think)

Copy link
Contributor

@DominikBernhardt DominikBernhardt left a comment

Choose a reason for hiding this comment

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

Thanks for implementing the changes, I am much happier with this now.
I've left 3 more comments. Also, I would leave it at changes requested until there are tests so this doesn't get merged accidentally?

lib/oper.g Outdated Show resolved Hide resolved
lib/oper.g Outdated Show resolved Hide resolved
lib/oper.g Outdated Show resolved Hide resolved
@ChrisJefferson ChrisJefferson force-pushed the tracking_ops branch 3 times, most recently from f28c182 to c174c7a Compare November 27, 2019 11:32
lib/oper.g Outdated
## gap> 3^(1,2,3);;
## gap> GetTracedInternalMethodsCounts();
## rec( Pow := rec( integer := rec( ("permutation (small)") := 1 ) ),
## Sum := rec( integer := rec( integer := 4 ), macfloat := rec( macfloat := 1 ) ) )
Copy link
Contributor

Choose a reason for hiding this comment

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

In the compiled documentation, the last ) is outside of the rectangular box in which Examples are printed. Thus, it looks like a overfull hbox. Maybe you could insert another line break?

Copy link
Member

Choose a reason for hiding this comment

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

If this is tested by Travis etc, then Chris doesn't have any choice about how the output should look; if he adds arbitrary line breaks, then the tests will fail when the manual examples are tested. However I'm not sure where this is ever tested by Travis.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh ok, I see. I guess I won't take the risk of a manual formatting now because it might be tested in the future and thing could break?

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 formatting was actually done by me, so I'll shorten/neaten it

@DominikBernhardt
Copy link
Contributor

Thanks @ChrisJefferson . Overall, I am happy with this now. If you could address the small points above I am happy to approve this and to get in merged.

@DominikBernhardt
Copy link
Contributor

@ChrisJefferson Tests are failing now. Maybe you could have a look at that? Also, while you're at it, could you change the documentation and change the is to are as suggested above?

@ChrisJefferson ChrisJefferson force-pushed the tracking_ops branch 2 times, most recently from 2f20af0 to 560d652 Compare December 3, 2019 08:33
lib/oper.g Outdated Show resolved Hide resolved
Copy link
Contributor

@DominikBernhardt DominikBernhardt left a comment

Choose a reason for hiding this comment

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

Thanks for all the changes @ChrisJefferson

Copy link
Member

@wilfwilson wilfwilson left a comment

Choose a reason for hiding this comment

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

Looks sensible to me 🙂

@wilfwilson wilfwilson merged commit 534dc89 into gap-system:master Dec 18, 2019
Copy link
Member

@fingolfin fingolfin left a comment

Choose a reason for hiding this comment

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

I didn't see this PR before, and just glancing at it, I wonder about several things, and thus would greatly prefer if this was not merged for now, until I have a chance to properly look at it (i.e., not while sitting in a train with a crappy internet connection).

@@ -875,6 +875,12 @@ static Obj FuncMakeImmutable(Obj self, Obj obj)
return obj;
}

static Obj FuncGET_TNAM_FROM_TNUM(Obj self, Obj obj)
{
UInt tnum = GetBoundedInt("GET_TNAM_FROM_TNUM", obj, 0, 255);
Copy link
Member

Choose a reason for hiding this comment

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

255 should have been NUM_TYPES - 1

## <Mark>LQuo</Mark><Item>The left-quotient operator</Item>
## <Mark>Pow</Mark><Item>The operator <Ref Oper="\^"/></Item>
## <Mark>Comm</Mark><Item>The operator <Ref Oper="Comm"/></Item>
## <Mark>Mod</Mark><Item>The operator <Ref Oper="\mod"/></Item>
Copy link
Member

Choose a reason for hiding this comment

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

I find the above discussion rather confusing. It mixes up methods and operations: This text sounds as if Zero, ZeroMut, etc. are the "internal methods". But really, these are operations which have a special implementation such that before all regular methods, a special internal method is considered; and only if that is not applicable or fails or whatever, are other (regular) methods tried.

## As these methods can be called hundreds of thousands of times in simple GAP
## code, there isn't a statement printed each time one is called. Instead, the
## method <Ref Func="GetTraceInternalMethodsCounts"/> returns how many times
## each operation has been applied to each type of variable (the type of a
Copy link
Member

Choose a reason for hiding this comment

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

Variables don't have a type in GAP, so "variable" should be changed to "object" here and in the next line (I can't make change suggestions on closed PRs, I am afraid).

Comment on lines +636 to +653
## <Mark>Inv, InvMut</Mark><Item>Mutable and Immutable <Ref Attr="Inverse"/></Item>
## <Mark>Sum</Mark><Item>The operator <Ref Oper="\+"/></Item>
## <Mark>Diff</Mark><Item>The operator <C>-</C> operator</Item>
## <Mark>Prod</Mark><Item>The operator <Ref Oper="\*"/></Item>
## <Mark>Quo</Mark><Item>The operator <Ref Oper="\/"/></Item>
## <Mark>LQuo</Mark><Item>The left-quotient operator</Item>
## <Mark>Pow</Mark><Item>The operator <Ref Oper="\^"/></Item>
## <Mark>Comm</Mark><Item>The operator <Ref Oper="Comm"/></Item>
## <Mark>Mod</Mark><Item>The operator <Ref Oper="\mod"/></Item>
## </List>
## <P/>
## <Ref Func="UntraceInternalMethods"/> turns tracing off.
## As these methods can be called hundreds of thousands of times in simple GAP
## code, there isn't a statement printed each time one is called. Instead, the
## method <Ref Func="GetTraceInternalMethodsCounts"/> returns how many times
## each operation has been applied to each type of variable (the type of a
## variable can be found with the <C>TNAM_OBJ</C> method).
## The return value for two argument operators is a record of records <C>r</C>, where
Copy link
Member

Choose a reason for hiding this comment

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

"The return value of GetTraceInternalMethodsCounts for ..."

## <Func Name="TraceInternalMethods" Arg=''/>
## <Func Name="UntraceInternalMethods" Arg=''/>
## <Func Name="GetTraceInternalMethodsCounts" Arg=''/>
## <Func Name="ClearTraceInternalMethodsCounts" Arg=''/>
Copy link
Member

Choose a reason for hiding this comment

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

The documentation says nothing about ClearTraceInternalMethodsCounts

tracking_active = 0;
return 0;
}
static Obj FuncGET_TRACED_INTERNAL_METHODS_COUNTS(Obj self)
Copy link
Member

Choose a reason for hiding this comment

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

Add empty line above


// Store the list of operators which can have tracing enabled and disabled
static voidfuncs controllers[64];
static int tracking_active;
Copy link
Member

Choose a reason for hiding this comment

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

we use CamelCase and not snake_case (nor lowerCamelCase) for global variables (almost) everywhere else...

void InstallOpWrapper(voidfunc activate, voidfunc deactivate)
{
int pos = 0;
while (pos < 64 && controllers[pos].activate != 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Instead of hardcoding 64 here and elsewhere, it would be better to use ARRAYSIZE(controllers). Or use a global enum for the size. Or don't use a size at all, and instead use a PLIST to store this data.

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 started switching to PLIST, but it doesn't really work as we have to store C function pointers

void InstallOpWrapper(voidfunc, voidfunc);


// These are macros which make it simple to wrap
Copy link
Member

Choose a reason for hiding this comment

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

This comment looks as if it is incomplete? In any case, it is useless in the current form. Would be nice to have at least some comments explaining what those macros are and how to use them.


void ReportWrappedOperation1(const char *, Obj op);
void ReportWrappedOperation2(const char *, Obj op1, Obj op2);
void InstallOpWrapper(voidfunc, voidfunc);
Copy link
Member

Choose a reason for hiding this comment

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

Some comments explaining these functions and how/when to use them would be nice

@ChrisJefferson ChrisJefferson deleted the tracking_ops branch April 1, 2020 10:06
@PaulaHaehndel PaulaHaehndel self-assigned this Feb 17, 2021
@PaulaHaehndel PaulaHaehndel added release notes: added PRs introducing changes that have since been mentioned in the release notes and removed release notes: to be added PRs introducing changes that should be (but have not yet been) mentioned in the release notes labels Feb 17, 2021
@PaulaHaehndel PaulaHaehndel removed their assignment Feb 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: enhancement Label for issues suggesting enhancements; and for pull requests implementing enhancements release notes: added PRs introducing changes that have since been mentioned in the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants