Is there any way to tell if a file is on SSD volume?

I have a need to optimize reading strategy, based on if the file is on hard disk or SSD. Does macOS provide any low-level API so that I can query such information?

I don't know of any API to get this kind of information directly. You might be better off using performance measurements to change your strategy on-the-fly. What differs between SSD and rotating rust that influences your strategy?

You can query a volume to find out its BSD name, using statfs. The f_mntfromname field contains the BSD name, (e.g. /dev/disk1s2). You can search the IORegistry to find nodes with BSD names matching e.g. "disk1s2". Then look up the IORegistry to see if the controller is an NVMe controller or something else. The methods for iterating through the IORegistry are in IOKitLib.h, there's a tool called IORegistryExplorer which lets you inspect the registry.

I have a need to optimize reading strategy, based on if the file is on hard disk or SSD.

This goal doesn’t make sense. You’re assuming that performance is tied to a specific type of hardware, which is not the case. It’s quite feasible for a sufficiently complex RAID system to have spinning rust that’s faster than a consumer SSD. And then there’s hybrid situations, like a Fusion Drive, where there isn’t a single correct answer.

As ssmith_c explained, you can map a specific volume to an I/O registry node and then poke around there, but that’s unlikely to end well. If you want to know how fast something is, measure it.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Is there any way to tell if a file is on SSD volume?
 
 
Q