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

[Runtime] Unify debug variable parsing from the environment and avoid getenv when possible. #32137

Merged
merged 1 commit into from
Jun 4, 2020

Conversation

mikeash
Copy link
Contributor

@mikeash mikeash commented Jun 2, 2020

There are a few environment variables used to enable debugging options in the
runtime, and we'll likely add more over time. These are implemented with
scattered getenv() calls at the point of use. This is inefficient, as most/all
OSes have to do a linear scan of the environment for each call. It's also not
discoverable, since the only way to find these variables is to inspect the
source.

This commit places all of these variables in a central location.
stdlib/public/runtime/EnvironmentVariables.def defines all of the debug
variables including their name, type, default value, and a help string. On OSes
which make an environ array available, the entire array is scanned in a single
pass the first time any debug variable is requested. By quickly rejecting
variables that do not start with SWIFT_, we optimize for the common case where
no debug variables are set. We also have a fallback to repeated getenv() calls
when a full scan is not possible.

Setting SWIFT_HELP=YES will print out all available debug variables along with
a brief description of what they do.

@mikeash mikeash requested review from compnerd and tbkka June 2, 2020 15:28
Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

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

This definitely is going to improve working on the runtime! Thanks for doing this.


// Define backing variables.
#define VARIABLE(name, type, defaultValue, help) \
type Environment::name ## _variable;
Copy link
Member

Choose a reason for hiding this comment

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

Should this not be static type ...?

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, this and initializeToken get exposed to other files through the inline getter functions in the header, so they need to be non-static.

}

// Initialization code.
OnceToken_t Environment::initializeToken;
Copy link
Member

Choose a reason for hiding this comment

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

Should this not be static?

@mikeash mikeash force-pushed the debug-environment-variables branch from da26553 to a8edaca Compare June 3, 2020 16:01
@mikeash
Copy link
Contributor Author

mikeash commented Jun 3, 2020

@compnerd Thanks for the feedback! I just pushed changes to address the comments I didn't reply to. Let me know if you see anything else.

@mikeash
Copy link
Contributor Author

mikeash commented Jun 3, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Jun 3, 2020

Build failed
Swift Test Linux Platform
Git Sha - a8edacaee4555bc82d4f07a6699194ac173ea223

@swift-ci
Copy link
Contributor

swift-ci commented Jun 3, 2020

Build failed
Swift Test OS X Platform
Git Sha - a8edacaee4555bc82d4f07a6699194ac173ea223

@mikeash mikeash force-pushed the debug-environment-variables branch from a8edaca to fbcb335 Compare June 3, 2020 17:37
@mikeash
Copy link
Contributor Author

mikeash commented Jun 3, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Jun 3, 2020

Build failed
Swift Test Linux Platform
Git Sha - a8edacaee4555bc82d4f07a6699194ac173ea223

@swift-ci
Copy link
Contributor

swift-ci commented Jun 3, 2020

Build failed
Swift Test OS X Platform
Git Sha - a8edacaee4555bc82d4f07a6699194ac173ea223

@mikeash mikeash force-pushed the debug-environment-variables branch from fbcb335 to 5ea8bc3 Compare June 3, 2020 20:46
@mikeash
Copy link
Contributor Author

mikeash commented Jun 3, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Jun 3, 2020

Build failed
Swift Test Linux Platform
Git Sha - fbcb335703f06dd763ec18628ad170ba79f00e8a

@swift-ci
Copy link
Contributor

swift-ci commented Jun 3, 2020

Build failed
Swift Test OS X Platform
Git Sha - fbcb335703f06dd763ec18628ad170ba79f00e8a

@@ -0,0 +1,41 @@
//===--- CompatibiltyOverride.h - Debug variables. --------------*- C++ -*-===//
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
//===--- CompatibiltyOverride.h - Debug variables. --------------*- C++ -*-===//
//===--- EnvironmentVariables.h - Debug variables. --------------*- C++ -*-===//

Copy link
Contributor Author

Choose a reason for hiding this comment

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

D'oh, good catch. Thank you!

@@ -0,0 +1,182 @@
//===--- CompatibiltyOverride.h - Debug variables. --------------*- C++ -*-===//
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
//===--- CompatibiltyOverride.h - Debug variables. --------------*- C++ -*-===//
//===--- EnvironmentVariables.cpp - Debug variables. ------------*- C++ -*-===//

… getenv when possible.

There are a few environment variables used to enable debugging options in the
runtime, and we'll likely add more over time. These are implemented with
scattered getenv() calls at the point of use. This is inefficient, as most/all
OSes have to do a linear scan of the environment for each call. It's also not
discoverable, since the only way to find these variables is to inspect the
source.

This commit places all of these variables in a central location.
stdlib/public/runtime/EnvironmentVariables.def defines all of the debug
variables including their name, type, default value, and a help string. On OSes
which make an `environ` array available, the entire array is scanned in a single
pass the first time any debug variable is requested. By quickly rejecting
variables that do not start with `SWIFT_`, we optimize for the common case where
no debug variables are set. We also have a fallback to repeated `getenv()` calls
when a full scan is not possible.

Setting `SWIFT_HELP=YES` will print out all available debug variables along with
a brief description of what they do.
@mikeash mikeash force-pushed the debug-environment-variables branch from 5ea8bc3 to f2fb539 Compare June 4, 2020 14:00
@mikeash
Copy link
Contributor Author

mikeash commented Jun 4, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Jun 4, 2020

Build failed
Swift Test Linux Platform
Git Sha - 5ea8bc36eff0766b3e06fe5269ee1c2ed0918bfd

@swift-ci
Copy link
Contributor

swift-ci commented Jun 4, 2020

Build failed
Swift Test OS X Platform
Git Sha - 5ea8bc36eff0766b3e06fe5269ee1c2ed0918bfd

@mikeash
Copy link
Contributor Author

mikeash commented Jun 4, 2020

@swift-ci please test

Copy link
Contributor

@tbkka tbkka left a comment

Choose a reason for hiding this comment

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

Nice! I like how you've organized this. Should be pretty easy to build on in the future.

@@ -5655,10 +5656,8 @@ void swift::verifyMangledNameRoundtrip(const Metadata *metadata) {
// variable lets us easily turn on verification to find and fix these
// bugs. Remove this and leave it permanently on once everything works
// with it enabled.
bool verificationEnabled =
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the last sentence of the comment above, since we no longer plan to remove this verification code.

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 idea. I'll do that in a followup, since I just got the thing through PR testing and I'd rather not continue that particular struggle....

@mikeash mikeash merged commit b25dab2 into swiftlang:master Jun 4, 2020
3405691582 added a commit to 3405691582/swift that referenced this pull request Jun 5, 2020
In swiftlang#32137 direct environment variable parsing was introduced, the
availability of which is branched by a preprocessor symbol (`ENVIRON`)
if the environment is available directly on the platform, or if getenv
must be used.

The message expectation in the unit test in the however diverged on the
non-`ENVIRON` branch, causing a unit test failure. This PR fixes the
expectation, but also, while we're here, OpenBSD supports the `ENVIRON`
branch anyway.
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.

5 participants