Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect SSD in Mac OS X?

Is there a reliable, quick, deterministic way (i.e. not a benchmark) to check whether the system drive Mac OS X is on is a Solid State Drive?

Is there any other indicator how well disk handles parallel access? I'm trying to adjust number of threads that my program is going to use for disk-bound operations.

I'm not interested in raw speed or seek time, only which type of access – serial or parallel – is faster for the drive. I don't expect users of my program to use iSCSI or RAID. SSD is my focus, anything else is nice-to-have.

Device Characteristics of IOAHCIBlockStorageDevice contains this information. How can I read it programmatically?


So far I've figured out it goes like this: (following is pseudocode)

match = IOBSDNameMatching(kIOMasterPortDefault,0,"disk0s2");
IOServiceGetMatchingServices(kIOMasterPortDefault, match, &iterator);
while(entry = IOIteratorNext(iterator)) {
   do {
     entry = IORegistryEntryGetParentEntry(nextMedia, kIOServicePlane, &entry);
     dict = IORegistryEntryCreateCFProperty(nextMedia, 
            CFSTR(kIOPropertyDeviceCharacteristicsKey), kCFAllocatorDefault, 0);
     [dict objectForKey:CFSTR(kIOPropertyMediumTypeKey)];
   } 
   while(!dict && entry); 
}

Edit: Here's complete source code. I've verified it works with Intel SSD and OCZ Vertex.

like image 923
Kornel Avatar asked Jan 17 '10 17:01

Kornel


People also ask

Why is my Mac not recognizing my SSD?

The most common reason for SSD not being detected by Mac is due to lose or damaged connecting wires. It may also happen when the SATA to the USB 3.0 adapter is incompatible with the hard drive's SATA interface.

How do I find my hard drive on Mac OS X?

In the Finder on your Mac, choose Finder > Preferences. Click General, then select the items you want to see on the desktop. For example, if you select “Hard disks,” icons for your hard disks appear on your desktop. Click Sidebar, then select the items you want to see in the Finder sidebar.


3 Answers

Actually, I think you should go the benchmarking route, because it more accurately answers your question - you don't really care that the disk happens to be an SSD, you just care that the disk is really fast. What if the user is using a fast RAID setup, or a Fiber Channel array, or is using iSCSI?

Just read a bunch of random sectors from the underlying /dev/diskX and if it meets your requirements you can treat it as a "Fast" drive

like image 196
Ana Betts Avatar answered Sep 23 '22 07:09

Ana Betts


If you're trying to get that kind of information, you're best guess is IOKit.

You can try some of it's functionality using the ioreg command line tool or the IORegistryExplorer.


Here's some code that might help you. It fetches all hard drives that aren't a RAID and aren't partitions. This isn't what you want, but it might get you started.

#import "TWDevice.h"

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <paths.h>
#include <sys/param.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/IOBSD.h>
#include <IOKit/storage/IOMedia.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/Kext/KextManager.h>


@implementation TWDevice

@synthesize name, devicePath, size, blockSize, writable, icon;

+ (NSArray *)allDevices {
    // create matching dictionary
    CFMutableDictionaryRef classesToMatch;
    classesToMatch = IOServiceMatching(kIOMediaClass);
    if (classesToMatch == NULL) {
        [NSException raise:@"TWError" format:@"Classes to match could not be created"];
    }

    // get iterator of matching services
    io_iterator_t mediaIterator;
    kern_return_t kernResult;
    kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault,
                                                                      classesToMatch,
                                                                      &mediaIterator);

    if (kernResult != KERN_SUCCESS) {
        [NSException raise:@"TWError" format:@"Matching services did not succed."];
    }

    // iterate over all found medias
    io_object_t nextMedia;
    NSMutableArray *detectedDevices = [NSMutableArray array];
    while (nextMedia = IOIteratorNext(mediaIterator)) {
        NSMutableDictionary *properties;
        kernResult = IORegistryEntryCreateCFProperties(nextMedia,
                                                                                  (CFMutableDictionaryRef *)&properties,
                                                                                  kCFAllocatorDefault, 0);

        if (kernResult != KERN_SUCCESS) {
            [NSException raise:@"TWError" format:@"Getting properties threw error."];
        }

        // is it a whole device or just a partition?
        if ([[properties valueForKey:@"Whole"] boolValue] &&
            ![[properties valueForKey:@"RAID"] boolValue]) {
            TWDevice *device = [[[TWDevice alloc] init] autorelease];

            device.devicePath = [NSString stringWithFormat:@"%sr%@", _PATH_DEV, [properties valueForKey:@"BSD Name"]];
            device.blockSize = [[properties valueForKey:@"Preferred Block Size"] unsignedLongLongValue];
            device.writable = [[properties valueForKey:@"Writable"] boolValue];
            device.size = [[properties valueForKey:@"Size"] unsignedLongLongValue];

            io_name_t name;
            IORegistryEntryGetName(nextMedia, name);
            device.name = [NSString stringWithCString:name encoding:NSASCIIStringEncoding];

            …

            [detectedDevices addObject:device];
        }

        // tidy up
        IOObjectRelease(nextMedia);
        CFRelease(properties);
    }
    IOObjectRelease(mediaIterator);

    return detectedDevices;
}

@end
like image 32
Georg Schölly Avatar answered Sep 23 '22 07:09

Georg Schölly


This only seems possible for internal drives. I could not find a way using IOKit to query a Samsung T5 external USB 3 SSD for its SSD-ness (nor a Toshiba Canvio USB HDDD). I did manage for the internal drive in my MBP, though.

Disk utility also doesn’t think the Samsung is an SSD, so that makes me think there may not be a way, other than to measure actual performance.

like image 45
fzwo Avatar answered Sep 24 '22 07:09

fzwo