Skip to content

Rewriting/simplifying DynamicLibrary in C. - #5221

Merged
benvanik merged 1 commit into
mainfrom
benvanik-dylib
Mar 25, 2021
Merged

benvanik merged 1 commit into
mainfrom
benvanik-dylib

Conversation

@benvanik

Copy link
Copy Markdown
Collaborator

This allowed for a lot of file IO code to go away - there was needless
abstraction here as there was only a single user of a lot of these things
that was already platform-specialized.

Progress on #4369 and #3848.
Fixes #4642.
Unblocks #3845, which can now be added cleanly.

@benvanik benvanik added the runtime Relating to the IREE runtime library label Mar 24, 2021
@benvanik
benvanik requested a review from ScottTodd March 24, 2021 18:26
@google-cla google-cla Bot added the cla: yes label Mar 24, 2021
@benvanik
benvanik force-pushed the benvanik-dylib branch 4 times, most recently from c5b070e to c45c361 Compare March 24, 2021 19:43
@benvanik
benvanik marked this pull request as ready for review March 24, 2021 19:57
@benvanik

Copy link
Copy Markdown
Collaborator Author

(this may break windows pdb symbol attachment to executables - but I'm likely going to be deprecating that path soon anyway so I'm fine with that - everyone but me is using linux anyway)

Comment on lines +53 to +47
// Query the 'TMPDIR' environment variable to allow users to override the
// path.
const char* tmpdir = getenv("TMPDIR");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we use TEST_TMPDIR also?

We had this before:
https://github.com/google/iree/blob/603b208f623e65eb48d8594c4ffc4218f36f41f5/iree/base/internal/file_io_posix.cc#L118-L127

Or, more generally... @GMNGeoffrey what would you like to see here, relating to #5147 ?

The specific concern is:

When I sshed into phones in our lab, I see a huge amount of files with dylib_executableNNNNNN directly under /data/local/tmp. They should be generated into TEST_TMPDIR so it can be deleted after running the tests?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Nope - something like TEST_TMPDIR does not belong in core code - that's for test outputs, where these are not test outputs. If the test env wants to override the temp path it can do so via one of the system temp env vars.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Figured that would be your answer :P
@GMNGeoffrey want to handle setting one of the supported env vars through the test runners as part of #5147?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, using TEST_TMPDIR to set the system temp var seems like the right approach. Core code shouldn't have to know about being executed by a test. However, I don't want to do that right this instant and I suspect this change will mean that we have a bunch of temp files getting written all over the place and not getting cleaned up. Can we make sure that doesn't happen before submitting this? I believe when I initially added this because without it tests failed if you ran them twice (#731). Not sure if this will also cause a regression there (probably depends on whether those tests still exist).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"all over the place" will still be the system temp directory though? That is easy enough to clean (by the OS or us manually)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah but filling up the temp directory on everyone's machine isn't very nice, not respecting the TEST_TMPDIR breaks the contract of tests run on Bazel (would cause issues?), and I'm concerned that it will cause non-hermetic behavior of tests.

@benvanik benvanik Mar 25, 2021 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I did some shuffling and was able to match the FILE_FLAG_DELETE_ON_CLOSE behavior I have on Windows by just unlinking the temp file after dlopening it on linux. This means that the only time a temp file will remain in the temp path is if the test dies (and that means segfault - not gracefully fails) while loading the library - which is just about never. I'm fine with a few files per month at most accumulating on the CI - and we should be running these things in docker images with transient tmp dirs anyway so I don't think that would even happen.

I think the metapoint here is that the world is not defined by the bazel test runner. If bazel wants to have non-test temp files written to a certain path it should set the platform environment variables for the temp path. Go already does this so maybe porting that to the cc rules would be useful if one really cared (edit: looks like that got reverted, with --sandbox_tmpfs_path=/tmp being recommended instead). TEST_TMPDIR also causes issues for usability - prior to this change one couldn't run the file_io or dynamic_library tests unless they were run under the bazel test runner or through our crazy 4-layer cmake shims. All test binaries should be runnable without needing any environment setup.

I prefer having the system temp path used for this particular case (again, non-test temp files) as having the usage be hermetic is reducing coverage of a critical use case: the code as anyone will be using it goes to the system temp path. If two concurrently running tests can have issues by doing that then it means two concurrently running real applications would as well - and we should be catching that. For example #731 linked above is terrifying: if a test can't run with existing non-test temp files, then a real app can't either. I don't want the first report of issues to be real users having trouble with their temp dirs. In other words, having the system temp path used should never flake and if it does we need to fix that vs. working around it in a way that is not representative of real usage.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree with almost everything you're saying here. We shouldn't need to have prod code execute a different path if it's on a test. But if some part of our project is doing something bad that we don't like, we should fix that first, not take a scorched earth approach of "this is the way things should be so I'll act like this is the way things are". #5147 should hopefully address these concerns, but I'm prioritizing it against many other things and I haven't gotten to it yet.

With regards to #731, the particular issue is that the test is (was?) asserting that the file it's trying to create isn't already there so that when it checks the contents of that file it's actually testing what it thinks it's testing. This would not be an issue with real applications. The particular approach of having a wrapper for our tests under ctest was one of several approaches I considered and ultimately one you approved. It's totally fine that we both now think that wasn't the best approach, but again let's not introduce instability into our project because of that.

Having the tmp files get automatically cleaned up seems great, thank you (and I agree that there's not much we can do if the process just dies. Polluting the tmp dir is about the best case for that).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Totally makes sense - thanks for pushing back a bit as it led me down the path of fixing it in a much nicer way :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for doing that :-)

Comment thread iree/base/internal/file_io_test.cc Resolved
This allowed for a lot of file IO code to go away - there was needless
abstraction here as there was only a single user of a lot of these things
that was already platform-specialized.

Progress on #4369 and #3848.
Fixes #4642.
Unblocks #3845, which can now be added cleanly.

# Conflicts:
#	iree/hal/local/loaders/legacy_library_loader.c
@benvanik
benvanik merged commit 46b31b0 into main Mar 25, 2021
@benvanik
benvanik deleted the benvanik-dylib branch March 25, 2021 19:38
@KoolJBlack KoolJBlack mentioned this pull request Mar 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

runtime Relating to the IREE runtime library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rewrite iree/base/dynamic_library.h in C.

3 participants