Skip to content

Conversation

@powercode
Copy link
Collaborator

Fixes #2578
When group policies are in effect for execution policy, a check is made to determine if the current process has been launched by gpscript.exe, and in that case, ignore the execution policy.
This is expensive to determine, is done over and over again, and has resulted in very long load times for scripts and modules.

One of the things that is checked (in an expensive way) is the parent id of the current process. That never changes, and is now cached.

The other change is to check once if we are launched by gpscript.exe or not and save that value.
This results in speedups in the order of a magnitude for module loads.

@msftclas
Copy link

msftclas commented Nov 2, 2016

Hi @powercode, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!
You've already signed the contribution license agreement. Thanks!

The agreement was validated by Microsoft and real humans are currently evaluating your PR.

TTYL, MSBOT;

@powercode powercode changed the title Performance improvements when group policies are in effect for ExecutionPolicy Performance improvements to security checks when group policies are in effect for ExecutionPolicy Nov 2, 2016
Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems this should be in #if !UNIX

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, you are correct. Fixing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed

Copy link
Collaborator

Choose a reason for hiding this comment

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

PG do not accept formatting without code changes. It would be good to revert this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Unneeded empty line.

@powercode powercode force-pushed the master branch 2 times, most recently from 17cfe31 to 8ad2783 Compare November 2, 2016 12:54
Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be good to mention here about main - use caching dramatically accelerated GetParentProcess.

@lzybkr
Copy link
Contributor

lzybkr commented Nov 2, 2016

@powercode - it looks like these changes cause the test failures.

@LeeHolmes - can you review these changes?

@powercode
Copy link
Collaborator Author

It is my bad. Fixing now.

Copy link
Collaborator

Choose a reason for hiding this comment

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

s_currentProcessId never changes. It seems unnecessary.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point. Refactoring...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It is there to avoid creating a new process object every time the id of the current process is needed

Copy link
Collaborator

Choose a reason for hiding this comment

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

Agree!

Copy link
Collaborator

Choose a reason for hiding this comment

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

I maybe stupid here but why you check s_currentProcessId instead of s_currentParentProcessId.HasValue?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Clear - "current" != "current" 😊

Copy link
Contributor

Choose a reason for hiding this comment

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

This conditional is the opposite of what I think you intended.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, fixed in the updated pull request

Copy link
Contributor

Choose a reason for hiding this comment

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

Our coding style puts a newline above comment blocks, as it represents a paragraph of thought.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed

@powercode
Copy link
Collaborator Author

Fixing

Copy link
Contributor

@LeeHolmes LeeHolmes Nov 14, 2016

Choose a reason for hiding this comment

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

This would be an error condition (taking a snapshot with no processes), so returning 'parentPid' is misleading. Instead, we should return 0.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point. Fixed.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is very close to being a duplication of GetParentProcess(). I think it'd be OK to add the caching logic to GetParentProcessIdNative. Also, it doesn't have the start time validation that GetParentProcess has, so we should make sure we don't expose anything that looks like a useful helper function without this validation.

@powercode powercode force-pushed the master branch 2 times, most recently from fce53f2 to 16f0e24 Compare November 15, 2016 08:00
@powercode
Copy link
Collaborator Author

Pushed new version with
private static int GetParentProcessIdCandidate
instead of
internal static int GetParentProcessId

@powercode powercode force-pushed the master branch 2 times, most recently from d7baf80 to 636a14c Compare November 20, 2016 20:05
@PowerShellTeam PowerShellTeam added the Review - Needed The PR is being reviewed label Nov 22, 2016
@lzybkr lzybkr assigned mirichmo and unassigned LeeHolmes Mar 29, 2017
@mirichmo
Copy link
Member

@powercode Can you please rebase this PR to the latest from master and push an update? It looks like it is ready to merge and just needs final code review sign off.

@LeeHolmes - Please take another look at this PR and provide feedback as appropriate.
@PaulHigin - I am adding you as Lee's backup if he is too busy or if you are curious

@mirichmo mirichmo requested a review from PaulHigin March 30, 2017 01:07
Copy link
Contributor

@PaulHigin PaulHigin left a comment

Choose a reason for hiding this comment

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

I looked through the code changes and comments. I agree with Lee's comments but I notice that two have not yet been addressed:

  1. GetParentProcessIdNative() helper method is not needed and can be merged with GetParentProcessId() method.
  2. The GetParentProcessId() method should include the process start time check and return '0' if start time.
  3. Actually I don't see a need for GetParentProcessId() at all, since it is always used within GetParentProcess() and nowhere else. We can just add the s_currentParentProcessId caching in GetParentProcess().

Staffan Gustafsson added 3 commits April 19, 2017 08:06
In certain scenarios, like a domain joined machine with an executionpolicy set via GPO, a lot of checks are done for the ExecutionPolicy, and this is in the hot path.
It is expensive to determine if a script was run as a result of applying a group policy. This does not change during the lifetime of a process.
This only affect the scenario when an execution policy has been set by a group policy.
@powercode
Copy link
Collaborator Author

@PaulHigin We now have an implementation of GetParentPid available. Using it instead, and inlining the caching as suggested.

Copy link
Contributor

@PaulHigin PaulHigin left a comment

Choose a reason for hiding this comment

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

LGTM

@mirichmo mirichmo merged commit f0eda03 into PowerShell:master Apr 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review - Needed The PR is being reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants