Posts

Post not yet marked as solved
14 Replies
And then I tested if copying a 1 KB file performs better with a 1'000 or 1'024 block size, and the first iteration of the whole test is always an outlier. Am I still doing something wrong?
Post not yet marked as solved
14 Replies
It’s important when the buffer size is less than the file size because it keeps all transfers aligned within the file, which facilitates uncached I/O. Good, that's what I was aiming at: use a block size equal to the file size up to a maximum power of 2. I don't think I was really trying to optimize the buffer allocation time; rather it didn't feel right to allocate an arbitrarily large buffer. After all, space is constrained and there is a reason why we can specify the block size, or it would automatically be set to infinity... right? it’s time to benchmark and use that to guide your optimisation Here are my results when copying a file with a given size using varying block sizes and 5 repetitions. (Explicitly writing random data to the file makes the file creation quite slow, but simply writing a buffer with uninitialized data seems to work as well.) Different file sizes seem to follow a similar trend. Using a multiple of 1000 or a multiple of the page size also doesn't seem to make a difference in the overall trend. The lowest block sizes, 1'024 and 2'048, seem to be special and cause a very fast copy. From 4'096 upwards, the time decreases... ...up to 65'536, where it suddenly increases again, but from then it definitely decreases. The bigger the file, the higher the block size needs to be to notice a difference. With a 100 MB file, increasing the block size from 1'048'576 to 2'097'152 makes the operation about twice as fast, with little improvements above that block size. With a 1 GB file, increasing the block size from 1'048'576 to 8'388'608 makes the operation about twice as fast, with little improvements above that block size. Without using F_NOCACHE, the operation gets slowly faster when increasing the block size from 1'048'576, and then gets slower again from 8'388'608 upwards. Not sure if that means anything. Here are the graphs for a 100 MB and a 1 GB file. Copying a 100 MB file: Copying a 1 GB file: Copying a 100 MB file created without F_NOCACHE: And here is the code: class AppDelegate: NSObject, NSApplicationDelegate { func applicationDidFinishLaunching(_ aNotification: Notification) { print("page size", getpagesize()) let openPanel = NSOpenPanel() openPanel.canChooseDirectories = true openPanel.runModal() test(url: openPanel.urls[0]) } func test(url: URL) { let source = url.appendingPathComponent("file source") let destination = url.appendingPathComponent("file destination") let fileSizes = [1_000, 1_000_000, 10_000_000, 100_000_000, 1_000_000_000, 10_000_000_000, Int(getpagesize()) * 10_000] let blockSizes: [Int32] = (10..<31).map({ 1 << $0 }) let repetitions = 5 var times = [[TimeInterval]](repeating: [TimeInterval](repeating: 0, count: repetitions), count: blockSizes.count) for fileSize in fileSizes { print("fileSize", fileSize) for (i, blockSize) in blockSizes.enumerated() { print("blockSize", blockSize) let date = Date() for j in 0..<repetitions { try? FileManager.default.removeItem(at: destination) var date = Date() print("create", terminator: " ") createFile(source: source, size: fileSize) print(-date.timeIntervalSinceNow) date = Date() print("copy", terminator: " ") do { try copy(source: source, destination: destination, blockSize: blockSize) } catch { preconditionFailure(error.localizedDescription) } let time = -date.timeIntervalSinceNow times[i][j] = time print(time) } let average = -date.timeIntervalSinceNow / Double(repetitions) print("average copy", average) print() } let header = blockSizes.map({ NumberFormatter.localizedString(from: $0 as NSNumber, number: .decimal) }).joined(separator: "\t") try! Data(([header] + (0..<repetitions).map { j in (["\(j)"] + (0..<blockSizes.count).map { i in return timeToString(times[i][j]) }).joined(separator: "\t") }).joined(separator: "\n").utf8).write(to: url.appendingPathComponent("results \(fileSize).tsv")) } } func timeToString(_ time: TimeInterval) -> String { return String(format: "%.6f", time) } func createFile(source: URL, size: Int) { var buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: size, alignment: Int(getpagesize())) // for i in 0..<size { // buffer[i] = UInt8.random(in: 0...255) // } let fp = fopen(source.path, "w") let success = fcntl(fileno(fp), F_NOCACHE, 1) assert(success == 0) let bytes = fwrite(buffer.baseAddress!, 1, size, fp) assert(bytes == size) fclose(fp) } func copy(source: URL, destination: URL, blockSize: Int32) throws { try source.withUnsafeFileSystemRepresentation { sourcePath in try destination.withUnsafeFileSystemRepresentation { destinationPath in let state = copyfile_state_alloc() defer { copyfile_state_free(state) } var blockSize = blockSize if copyfile_state_set(state, UInt32(COPYFILE_STATE_BSIZE), &blockSize) != 0 || copyfile_state_set(state, UInt32(COPYFILE_STATE_STATUS_CB), unsafeBitCast(copyfileCallback, to: UnsafeRawPointer.self)) != 0 || copyfile_state_set(state, UInt32(COPYFILE_STATE_STATUS_CTX), unsafeBitCast(self, to: UnsafeRawPointer.self)) != 0 || copyfile(sourcePath, destinationPath, state, copyfile_flags_t(COPYFILE_ALL | COPYFILE_NOFOLLOW | COPYFILE_EXCL)) != 0 { throw NSError(domain: NSPOSIXErrorDomain, code: Int(errno)) } } } } private let copyfileCallback: copyfile_callback_t = { what, stage, state, src, dst, ctx in if what == COPYFILE_COPY_DATA { if stage == COPYFILE_ERR { return COPYFILE_QUIT } } return COPYFILE_CONTINUE } }
Post not yet marked as solved
14 Replies
Do you imagine that it somehow reads beyond the end of the file on the disk until it reaches the block size you have specified? In case you're wondering why I don't simply use a fixed block size of 16_777_216: I thought that the larger the allocated space, the more time it would take to allocate it, and the less space would be available to the rest of the system while the copy is in progress. It may be a negligible time difference, but since I can avoid it with a very simple calculation, why not do it?
Post not yet marked as solved
14 Replies
I'd be amazed if that made any difference. Why? It sets a block size of 16 MiB for files that are larger than 16 MiB, or uses the file size itself for files that are smaller.
Post not yet marked as solved
14 Replies
Things will go better if your block size matches the allocation block size of the underlying volume Thanks again for your input. To clarify the "things will go better" part: does that mean that providing a block size that doesn't match can impact performance? Should I make sure to calculate the next multiple of two bigger than the file size to copy (with a maximum threshold of course), or can I simply do this: var bsize = Int32(16_777_216) if let size = Int32(exactly: file.size) { bsize = min(size, bsize) }
Post not yet marked as solved
6 Replies
I just had a customer reporting the same issue with getattrlistbulk and ERANGE: Result too large. According to them, it happens on macOS 10.13 with APFS but doesn't happen on macOS 10.14 anymore. I can also confirm that it only happens when the last call exactly fills the buffer. Running FileManager.contentsOfDirectory(atPath:) returns the cumulative amount of entries processed with getattrlistbulk and throws no error.
Post not yet marked as solved
14 Replies
Why do you say preferred 1KB, when in the same sentence you say the preferred block size is 1MB? Sorry, my mistake. Let me give you some actual results: when using the "preferred" 1_048_576 block size (returned by URLResourceKey.preferredIOBlockSizeKey) it takes about 6.15 seconds to copy a 7 GB file from my Mac to another folder on the same Mac. When using 16_777_216 block size, I get about 3 seconds instead. If I don't set the block size option for filecopy, I get about the same time as with the preferred block size, so I guess it's probably the same value. My question is: what makes the 1_048_576 block size "preferred", since using 16_777_216 drastically increases the filecopy performance? And can we assume that increasing the block size will always give better performance for filecopy?
Post not yet marked as solved
14 Replies
Thanks a lot for your valuable input! Things will go better if your block size matches the allocation block size of the underlying volume When you say "match", do you mean equal, or a multiple, or something else? I'm asking because the value returned by URLResourceKey.preferredIOBlockSizeKey for my Macintosh HD is 1048576, but significantly increasing the block size of filecopy from the "preferred" 1KB to 1MB or higher usually performs better.
Post not yet marked as solved
5 Replies
On the one hand, I’d like you to open a DTS tech support incident so that I can dig into this OK, I opened Case-ID: 5366675. You should file a bug to request clarification from Apple I already filed FB13397779 some weeks ago, but didn't get a response.
Post not yet marked as solved
5 Replies
Are only seeing this with mDNSResponder flows? The majority is /usr/sbin/mDNSResponder, but I also get others, such as /usr/sbin/netbiosd. (r. 115822797) Can I look this somewhere up, or is it just for your own reference? Do you think this behaviour is a bug, or does it mean that the earlier flow is invalid once the double one comes in, or what does it mean?
Post not yet marked as solved
5 Replies
It’s probably worth filing a bug about that. Please post your bug number, just for the record. I created FB13454498. First, I recommend against using Dispatch global concurrent queues, as explained in Avoid Dispatch Global Concurrent Queues. Thank you. While the second WWDC link on the linked page correctly leads to a video, the first link (WWDC 2015 Session 718 Building Responsive and Efficient Apps with GCD) leads to a list of videos where that video cannot be found. Does it still exist? Second, synchronously dispatching to the main queue is a concern because it can block the current thread for long periods of time. I know, but that thread needs access to a resource possibly occupied by the main thread before being able to continue with its work. I think you’d be much better off investing in Swift concurrency. I'm still supporting macOS 10.13, while Swift concurrency is only available on macOS 10.15.
Post not yet marked as solved
5 Replies
Shouldn't you be using self?.asd() in the closure for weak self? Not necessarily. One could use guard let self = self else { return } to avoid using self?. each time. But I don't need to use weak self in this case.
Post not yet marked as solved
6 Replies
You could test this. I manually caused an infinite recursion at the same location as the other crash report and I got a crash report with 511 frames. Frames numbered 9 to 510 are all the same, and since 510 is the last one, it looks like it's cut off. Otherwise the frames look very similar to the crash report downloaded by Xcode. Thread 4 Crashed:: Dispatch queue: com.apple.root.user-initiated-qos 0 CoreFoundation 0x1837f5084 __CFStringEncodeByteStream + 76 1 Foundation 0x184901558 -[NSString(NSStringOtherEncodings) getBytes:maxLength:usedLength:encoding:options:range:remainingRange:] + 260 2 libswiftCore.dylib 0x1931cf194 _NSStringCopyBytes(_:encoding:into:) + 124 3 libswiftCore.dylib 0x1931d67f0 _StringGuts._foreignCopyUTF8(into:) + 80 4 libswiftCore.dylib 0x1931d7bc8 _StringGuts._foreignConvertedToSmall() + 68 5 libswiftCore.dylib 0x1931d7b44 _StringGuts.append(_:) + 1232 6 libswiftCore.dylib 0x19316ba80 String.write<A>(to:) + 56 7 libswiftCore.dylib 0x19300bef0 DefaultStringInterpolation.appendInterpolation<A>(_:) + 72 8 MyApp 0x1000e4538 String.appendingPathComponent(_:) + 924 (PathAdditions.swift:75) 9 MyApp 0x100119138 Snapshot.renameFile(_:to:) + 696 (Snapshot+Modify.swift:135) 10 MyApp 0x10011915c Snapshot.renameFile(_:to:) + 732 (Snapshot+Modify.swift:135) ... 510 MyApp 0x10011915c Snapshot.renameFile(_:to:) + 732 (Snapshot+Modify.swift:135) And it's also the same exception type: Exception Type: EXC_BAD_ACCESS (SIGBUS) Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000170027fd8 Exception Codes: 0x0000000000000002, 0x0000000170027fd8 Termination Reason: Namespace SIGNAL, Code 10 Bus error: 10 Terminating Process: exc handler [66786] VM Region Info: 0x170027fd8 is in 0x170024000-0x170028000; bytes after start: 16344 bytes before end: 39 REGION TYPE START - END [ VSIZE] PRT/MAX SHRMOD REGION DETAIL Stack 16ff9c000-170024000 [ 544K] rw-/rwx SM=PRV thread 3 ---> STACK GUARD 170024000-170028000 [ 16K] ---/rwx SM=NUL ... for thread 4 Stack 170028000-1700b0000 [ 544K] rw-/rwx SM=PRV thread 4 I guess we solved the mystery? Probably the backtrace got cut off along the way between the user and my Xcode instance. Thank you very much for your help!
Post not yet marked as solved
7 Replies
I recommend that you update your bug with information about your specific use case. Thanks, updated it. It's an app that scans user-selected directories, such as Macintosh HD, and shows the contents as a graph.
Post not yet marked as solved
6 Replies
Thank you very much for your insights. How did thread 1 get started? You're right, I didn't notice that the stacktrace looks strange. The code in thread 1 is called from inside NSFileCoordinator.coordinate(readingItemAt:options:writingItemAt:options:error:byAccessor:), which in turn is called inside DispatchQueue.global(qos: .userInitiated).async, with some other of my code before and after each of these calls. What’s up with the symbolication? I'm not sure I understand why the double line 135 seems unlikely. I can confirm that in this case it's a recursive method call. Could it be that the program somehow went into infinite recursion, explaining why the thread's origin is cut off?