Skip to content

Instantly share code, notes, and snippets.

@tamasd
Created April 24, 2013 08:29
Show Gist options
  • Save tamasd/5450589 to your computer and use it in GitHub Desktop.
Save tamasd/5450589 to your computer and use it in GitHub Desktop.
<?php
define('KEXT_DELIMITER', " \t\n");
class KEXT {
public $index;
public $refs;
public $size;
public $wired;
public $name;
public static function formatBytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= pow(1024, $pow);
return round($bytes, $precision) . ' ' . $units[$pow];
}
public function __construct($line) {
$this->index = strtok($line, KEXT_DELIMITER);
$this->refs = strtok(KEXT_DELIMITER);
strtok(KEXT_DELIMITER); // Address
$this->size = hexdec(strtok(KEXT_DELIMITER));
$this->wired = hexdec(strtok(KEXT_DELIMITER));
$this->name = trim(strtok("\n"));
}
public function __toString() {
$size = self::formatBytes($this->size);
$wired = self::formatBytes($this->wired);
return "{$this->name}\t{$wired}\t{$size}\n";
}
}
$stat = `/usr/sbin/kextstat -l`;
$lines = explode("\n", $stat);
$kexts = array();
foreach ($lines as $line) {
$kexts[] = new KEXT($line);
}
usort($kexts, function (KEXT $a, KEXT $b) {
$prop = 'wired';
if ($a->$prop == $b->$prop) {
return 0;
}
return ($a->$prop > $b->$prop) ? -1 : 1; // ORDER BY wired DESC
});
$all_wired = 0;
$all_size = 0;
foreach ($kexts as $kext) {
$all_wired += $kext->wired;
$all_size += $kext->size;
print $kext;
}
print 'KEXT WIRED: ' . KEXT::formatBytes($all_wired) . ' SIZE: ' . KEXT::formatBytes($all_size) . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment