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

SR-1005: Replace POSIX module by Foundation #284

Closed
wants to merge 11 commits into from

Conversation

Bouke
Copy link
Contributor

@Bouke Bouke commented Apr 26, 2016

A partial implementation of SR-1005, replacing POSIX calls by Foundation calls. This PR addresses most file and directory operations by using NSFileManager and NSURL. To prevent a major overhaul of all usages of these calls, I kept all functions intact but changed the implementation. Some things to consider before merging this PR:

  • Resolved: segfaults on Linux (see comment below)
  • Cocoa Foundation API differs from SwiftFoundation. I've used #if os(Linux) for the interim until this is fixed.
  • Errors are now mostly Cocoa's default errors; so any code inspecting the error code should be adjusted (1 occurrence found and updated).
  • unlink and rmdir both use NSFileManager.removeItem(atPath:), which will remove files and directories recursively. The old implementations of unlink would only remove files, and rmdir would only remove directories non-recursive.
  • Temporary folders are now created with appending a GUID to guarantee random folder names.
  • mkdir returns relative folder name if relative name was passed. Use realpath(mkdir(...)) when old functionality is wanted; opt-in for absolute paths.
  • Unused functions (*time*, readlink, stat) were removed.
  • Dropped relativeTo argument of symlink as it was not used; all usages used an absolute path.

Not covered by this PR:

Dependencies:

@ddunbar
Copy link
Contributor

ddunbar commented Apr 26, 2016

Awesome!

For test crashes, you should be able to run the tests in a debugger to see what is actually happening.

Can you file bugs for the places where the Foundation API differs across platforms?

It would be good to identify the source of the performance regression, as well.

@Bouke Bouke changed the title Sr 1005 foundation SR-1005: Replace POSIX module by Foundation Apr 26, 2016
@Bouke
Copy link
Contributor Author

Bouke commented Apr 26, 2016

The segfault as reported by the LLDB debugger, however I'm not sure how to proceed.

* thread #1: tid = 13474, 0x00007ffff7b4b708 libswiftCore.so`swift_getErrorValue + 8, name = 'swift-build', stop reason = signal SIGSEGV: invalid address (fault address: 0x90)
    frame #0: 0x00007ffff7b4b708 libswiftCore.so`swift_getErrorValue + 8
libswiftCore.so`swift_getErrorValue:
->  0x7ffff7b4b708 <+8>:  movq   0x90(%rcx), %rcx
    0x7ffff7b4b70f <+15>: movzwl %cx, %esi
    0x7ffff7b4b712 <+18>: leaq   0x20(%rdi,%rsi), %rsi

API differences reported in SR-1328.

I suspect that the performance regressions are expected as the old POSIX calls are very lean. The new calls using Foundation probably contain extra error checking; more layers of indirection.

@ddunbar
Copy link
Contributor

ddunbar commented Apr 26, 2016

I suspect that the performance regressions are expected as the old POSIX calls are very lean. The new calls using Foundation probably contain extra error checking; more layers of indirection.

This probably isn't the case. While it is true there is a little more indirection, that is unlikely to be able to add up to 6% given the kind of work we are doing... my guess is something else is going on here. Can you try to identify a particular individual test which has regressed?

@Bouke
Copy link
Contributor Author

Bouke commented Apr 27, 2016

This probably isn't the case. While it is true there is a little more indirection, that is unlikely to be able to add up to 6% given the kind of work we are doing... my guess is something else is going on here. Can you try to identify a particular individual test which has regressed?

That was indeed the case. Some NSFileManager functions performed even slightly better in a quick performance test. The full test suite is now run as fast as before.

@mxcl
Copy link
Contributor

mxcl commented Apr 29, 2016

Great stuff. Let's deconflict and merge.

@Bouke
Copy link
Contributor Author

Bouke commented Apr 30, 2016

Thanks! Indeed it does conflict, it was written against the latest development toolchain snapshot. The Foundation API is being imported now with new rules, so I'm unable to compile until I have an updated toolchain. I've yet to learn to build my own toolchain, maybe I'll just wait for a new toolchain snapshot.

@Bouke
Copy link
Contributor Author

Bouke commented May 2, 2016

I've located the segfaults on Linux to be related to NSFileManager.rename(atPath:toPath:), for which I've filed a bug report and a PR swiftlang/swift-corelibs-foundation#350. I've updated this PR to have smaller atomic commits, which allowed for git bisect to find the segfault. The PR is rebased on master against the updated Foundation API.

@Bouke Bouke force-pushed the sr-1005-foundation branch 2 times, most recently from ee28942 to ca6fd84 Compare May 6, 2016 21:34
@Bouke
Copy link
Contributor Author

Bouke commented May 6, 2016

I've rebased the PR and verified against OSX and Ubuntu 15.10.

@Bouke
Copy link
Contributor Author

Bouke commented May 7, 2016

I've also moved rename over to NSFileManager, making this PR dependent on the merger of swiftlang/swift-corelibs-foundation#350.

@ddunbar
Copy link
Contributor

ddunbar commented May 9, 2016

Can you rebase please?

@Bouke Bouke force-pushed the sr-1005-foundation branch 4 times, most recently from c10ef55 to 9eed36c Compare May 9, 2016 08:05
@ddunbar
Copy link
Contributor

ddunbar commented May 9, 2016

@swift-ci please test and merge


import func libc.unlink
import var libc.errno
import Foundation

public func unlink(_ path: String) throws {
Copy link
Contributor

Choose a reason for hiding this comment

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

This function should be renamed, using unlink as a synonym for removing a tree is confusing, as that is not the POSIX behavior.

@ddunbar
Copy link
Contributor

ddunbar commented May 9, 2016

@swift-ci please test

@Bouke
Copy link
Contributor Author

Bouke commented May 9, 2016

Any ideas on why the build failed on OSX? The logs include the following test case, but I'm not sure how that's related?

ERROR: test_lldbmi_break_insert_function_pending (TestMiBreak.MiBreakTestCase)
   Test that 'lldb-mi --interpreter' works for pending function breakpoints.

@ddunbar
Copy link
Contributor

ddunbar commented May 9, 2016

That is unrelated, it sounds like from mail on swift-dev.

@Bouke
Copy link
Contributor Author

Bouke commented May 10, 2016

Can you retest?

@ddunbar
Copy link
Contributor

ddunbar commented May 10, 2016

@swift-ci please test

@aciidgh
Copy link
Contributor

aciidgh commented May 11, 2016

@Bouke conflicts

@Bouke
Copy link
Contributor Author

Bouke commented May 11, 2016

@ddunbar @aciidb0mb3r rebased and tested against OSX and Ubuntu 15.10.

@aciidgh
Copy link
Contributor

aciidgh commented May 11, 2016

@swift-ci please test

@ddunbar
Copy link
Contributor

ddunbar commented May 19, 2016

I am going to start pulling over individual parts of this piecemeal for things that make more sense to be implemented on top of Foundation than POSIX wrappers...

@ddunbar ddunbar mentioned this pull request May 19, 2016
@ddunbar
Copy link
Contributor

ddunbar commented May 19, 2016

I posted a new PR #355 which cherry picks (and reworks) a couple of commits from this PR.

The ones I took:

  • 07f50bf [Foundation] Implemented mkdir
  • f5728c6 [Foundation] Implemented unlink / rmdir / rmtree

The ones I abandoned, and why:

  • 3c673df [Foundation] Implemented POSIX.realpath

    Foundation implementation is more complicated than POSIX.

  • 24f2878 [Foundation] Implemented chdir

    Foundation implementation isn't better than POSIX one, and we need to change interface slightly from what it is in either case:

  • e1e7852 [Foundation] Implemented getcwd

    Foundation implementation doesn't handle all cases we do (missing cwd).

  • b5a4a85 [Foundation] Implemented symlink, c8bea50 [Foundation] Implemented rename

    We may eventually have a need for using the ...at POSIX facilities directly (which Foundation doesn't expose), so I'd rather keep the implementation which does that in our own code.

  • c56dca2 [Foundation] Implemented mkdtemp

    We need to change the current implementation, but I prefer to continue to use mkdtemp at least versus reimplementing it, so I will rework this part later.

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.

4 participants