$ sudo pmset -a sms 0
$ cat /Library/LaunchDaemons/noatime.plist <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>noatime</string> <key>ProgramArguments</key> <array> <string>mount</string> <string>-vuwo</string> <string>noatime</string> <string>/</string> </array> <key>RunAtLoad</key> <true/> </dict> </plist>
保存后重启, 并检查是否启用成功:
$ mount | grep " / " /dev/disk0s2 on / (hfs, local, journaled, noatime)
文件的最后访问时间属性很少被使用到, 所以关闭后不会产生影响, 这是大多数情况下硬盘性能优化的第一步.
$ sudo pmset -a hibernatemode 0 $ sudo rm /var/vm/sleepimage*
如果在电池快耗尽又不想关机的时候, 可以恢复默认休眠方式.
$ sudo pmset -a hibernatemode 3
操作完无需重启即可生效.
$ ls -l ~/.bin/cache_ramfs -rwxr-xr-x 1 d staff 433 Jul 10 01:37 /Users/d/.bin/cache_ramfs $ cat ~/.bin/cache_ramfs #!/bin/sh # 创建 1GB RAMDisk ramfs_size_mb=1024 # 挂载的卷名 volume_name="CACHE DISK" disk_size_mb2sector() { local size_mb=$1 echo $(($size_mb*1024*1024/512)) } disk_create_ramdev() { local ramfs_size=$1 echo `/usr/bin/hdid -nomount ram://$ramfs_size` } disk_create_ramdisk() { local volume_name=$1 local ramfs_size=$2 local ram_dev=`disk_create_ramdev $ramfs_size` /usr/sbin/diskutil quiet eraseVolume HFS+ "$volume_name" $ram_dev echo $ram_dev } # 计算 RAM 的扇区(Sector)大小 ramfs_size_sectors=`disk_size_mb2sector $ramfs_size_mb` # 创建 RAM 卷并挂载 disk_create_ramdisk "$volume_name" $ramfs_size_sectors # 下面的部分是创建 RAMDisk 的目录结构和需要的文件 # 因人而异可以自行增减 dir_list="Chromium Firefox Safari Safari/Meta Adium" for dir in $dir_list do /bin/mkdir -p "/Volumes/$volume_name/$dir" done
然后开始移动默认的 Cache 文件夹, 我只是创建了一些 link, 并保留原始文件, 在必要时可以 Undo.
#!/bin/sh OLD_PWD=`pwd` CACHE_DISK="/Volumes/CACHE DISK" USER_DIR="/Users/d" DO_RESTORE=0 if [ ! -z $1 ]; then case "$1" in "restore") DO_RESTORE=1 echo "In the restore mode." ;; "help") echo "Usage:\n\n ${0##*/} {move|restore|help} [path_to_cache_disk]\n" exit 1 ;; esac fi if [ ! -z $2 ] && [ -d $2 ]; then CACHE_DISK=$2 fi cache_link() { local source=$1 local target=$2 local restore=$3 local source_dir=${source%/*} local source_name=${source##*/} local create_cmd="" if [ -f "$source" ]; then create_cmd=/usr/bin/touch elif [ -d "$source" ]; then create_cmd="/bin/mkdir -p" fi if [ -d "$source_dir" ]; then cd "$source_dir" echo cd to $source_dir fi echo "Source name is '$source_name'" if [ -L "$source_name" ]; then /bin/rm "$source_name" # echo "Removed '$source_name'" fi local disk_cache=$source_name.DISK local ram_cache=$source_name.RAM if [ ! -e "$disk_cache" ]; then echo Moving the disk cache "$disk_cache" ... /bin/mv "$source_name" "$disk_cache" fi if [ ! -e "$ram_cache" ]; then echo Making the ram cache "$ram_cache" from $target ... if [ ! -e "$target" ]; then echo " $create_cmd $target" $create_cmd "$target" fi /bin/ln -s "$target" "$ram_cache" fi if [[ $restore -eq 1 ]]; then /bin/ln -s "$disk_cache" "$source_name" echo "The cache $source was restored.\n" else /bin/ln -s "$ram_cache" "$source_name" echo "The cache $source -> $target was maked.\n" fi cd $OLD_PWD } LIBDIR="$USER_DIR/Library" # Chromium cache_link "$LIBDIR/Caches/Chromium/Default/Cache" "$CACHE_DISK/Chromium" $DO_RESTORE # Adium cache_link "$LIBDIR/Caches/Adium/Default" "$CACHE_DISK/Adium" $DO_RESTORE # Firefox cache_link "$LIBDIR/Caches/Firefox/Profiles/zvo4xhu9.default/Cache" "$CACHE_DISK/Firefox" $DO_RESTORE # Safari cache_link "$LIBDIR/Caches/com.apple.Safari" "$CACHE_DISK/Safari" $DO_RESTORE # Safari Web Preview cache_link "$LIBDIR/Caches/Metadata/Safari" "$CACHE_DISK/Safari/Meta" $DO_RESTORE
用法很简单, 保存下来直接执行一次即可, 如果需要 Undo, 则加上 restore 参数. 先不忙着执行, 把 RAMDisk 挂上再说. 我是设定在自己的帐户登录后自动挂载的, 不过没有用到 Login Items, 而是放在了 launchd.
$ cat ~/Library/LaunchAgents/ramcache.plist <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>ramcache</string> <key>Program</key> <string>/PATH/TO/cache_ramfs</string> <key>RunAtLoad</key> <true/> </dict> </plist>
好, 干完上面的一堆活, 现在来注销重登录, 如果一切正常的话, 卷名为 CACHE DISK 的磁盘已经自动出现在 Finder 里了, 里面的目录结构也已创建完毕.
现在执行一次切换 Cache 文件夹的脚本, 一切就绪后开启软件进行测试, 相关的 Cache 文件都已在 RAMDisk 中了 :)
这里有必要提一下内存占用的问题, RAMDisk 虽然划分了 1GB 空间, 但并非挂上就会立即独占这些空间, 只会使用实际占用的大小. 理所当然的, 当使用到 1GB 大小时无法再写入, 即使清空 CACHE DISK 下的所有文件, 内存也不会被释放出来, 但此时可以继续写入新内容了.
$ sudo -i $ launchctl unload -w /System/Library/LaunchDaemons/com.apple.syslogd.plist $ rm /var/log/system.log* $ launchctl unload -w /System/Library/LaunchDaemons/com.apple.aslmanager.plist $ rm -r /var/log/asl
有关 aslmanager, man page 中说道:
aslmanager — Apple System Log data store file manager
aslmanager manages files in the ASL data store written by the syslogd server
…
$ defaults write com.apple.Safari DebugSnapshotsUpdatePolicy -int 2
如果需要恢复:
$ defaults delete com.apple.Safari DebugSnapshotsUpdatePolicy