egregius.be

Urban Exploration, PHP and others…

image

macOS Catalina move caches and logs to RAMdisk

In a previous post I wrote about how I monitor the writes to several SSD’s.
Second post in this series is about macOS Catalina, how to use a RAM disk to store caches and logs so you can avoid unnecessary disk writes. Caches are used to speed up applications. For example a browser caches images, Javascript JS files, CSS stylesheets and more. RAM Memory is the ideal option for speed. In the previous post we saw that a RAM disk is at least 3 times faster than a NVME SSD. Those files are continuously updated and written to the disk, causing huge amounts of writes.

To avoid this I wrote a shell script that removes the cache folders, creates them on a RAM disk and creates symlinks for them so the applications don’t know it’s even happening.
The first file is the shell script that does the actual work:

#!/bin/bash
while [ ! -d /Volumes ]
do
echo "waiting..."
sleep 0.5
done
if [ ! -d /Volumes/RamDisk ]; then
echo "creating ramdisk..."
sleep 0.5
diskutil partitionDisk $(hdiutil attach -nomount ram://$((2048*16000))) 1 GPTFormat APFS 'RamDisk' '100%'
mkdir -p /Volumes/RamDisk/Logs/
sudo rm -rf /private/var/log
ln -s /Volumes/RamDisk/Logs /private/var/log
sudo rm -rf /Users/guy/Library/Logs
ln -s /Volumes/RamDisk/Logs /Users/guy/Library/Logs
sudo rm -rf /Library/Logs
sudo ln -s /Volumes/RamDisk/Logs /Library/Logs
rm -rf /Users/guy/Library/Application\ Support/Adobe/ExtensibilityLibrary/Log
ln -s /Volumes/RamDisk/Logs /Users/guy/Library/Application\ Support/Adobe/ExtensibilityLibrary/Log
rm -rf /Users/guy/Library/Application\ Support/Adobe/Extension\ Manager\ CC/Log
ln -s /Volumes/RamDisk/Logs /Users/guy/Library/Application\ Support/Adobe/Extension\ Manager\ CC/Log
mkdir -p /Volumes/RamDisk/Downloads
sudo rm -rf /Users/guy/Downloads
sudo ln -s /Volumes/RamDisk/Downloads /Users/guy
sudo chown guy:staff /Users/guy/Downloads
declare -a arr=(
"Adobe Camera Raw 2"
"Adobe_CCXProcess.node"
"askpermissiond"
"BraveSoftware"
"com.adobe.acc.AdobeDesktopService"
"com.adobe.acc.ads.helper"
"com.adobe.bridge10"
"com.adobe.headlights.LogTransport2App"
"com.apple.akd"
"com.apple.AMPLibraryAgent"
"com.apple.AppleMediaServices"
"com.apple.appstore"
"com.apple.appstoreagent"
"com.apple.cache_delete"
"com.apple.commerce"
"com.apple.helpd"
"com.apple.iBooksX"
"com.apple.icloud.fmfd"
"com.apple.iCloudHelper"
"com.apple.imfoundation.IMRemoteURLConnectionAgent"
"com.apple.InstallAssistant.macOS1016"
"com.apple.installer"
"com.apple.installer.osinstallersetupd"
"com.apple.nbagent"
"com.apple.nsurlsessiond"
"com.apple.parsecd"
"com.apple.passd"
"com.apple.proactive.eventtracker"
"com.apple.remindd"
"com.apple.Spotlight"
"com.apple.touristd"
"com.apple.VideoConference"
"com.apple.XprotectFramework.AnalysisService"
"com.brave.Browser"
"com.crashlytics.data"
"com.dustinrue.ControlPlane"
"com.flixtools.flixtools"
"com.Google.GoogleDrive"
"com.google.GoogleEarthPro"
"com.google.Keystone"
"com.google.SoftwareUpdate"
"com.intel.PowerGadget"
"com.MattRajca.RetinaCapture"
"com.microsoft.autoupdate.fba"
"com.microsoft.autoupdate2"
"com.microsoft.edgemac"
"com.microsoft.teams"
"com.operasoftware.Installer.Opera"
"com.operasoftware.Opera"
"com.panic.Transmit"
"com.plausiblelabs.crashreporter.data"
"com.runningwithcrayons.Alfred"
"com.spotify.client"
"com.teamviewer.TeamViewer"
"CSXS"
"FamilyCircle"
"familycircled"
"GeoServices"
"Google"
"Google Earth"
"knowledge-agent"
"ksfetch"
"Maps"
"Microsoft"
"Microsoft Edge"
"net.freemacsoft.AppCleaner"
"net.pornel.ImageOptim"
"node"
"PassKit"
"TeamViewer"
"Transmit"
)
for i in "${arr[@]}"
do
mkdir -p "/Volumes/RamDisk/Caches/$i"
if [[ -L "/Users/guy/Library/Caches/$i" && -d "/Users/guy/Library/Caches/$i" ]]
then
echo "$i is already a symlink"
else
rm -rf "/Users/guy/Library/Caches/$i"
ln -s "/Volumes/RamDisk/Caches/$i" /Users/guy/Library/Caches
echo "$i CREATED"
fi
done
fi

That file is executed on system startup by a LaunchAgent, the file is stored as /Library/LaunchAgents/com.user.ramdisk.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.user.ramdisk</string>
<key>ProgramArguments</key>
<array>
<string>/Users/guy/OneDrive/MyApps/createramdiskatboot.command</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StartInterval</key>
<integer>300</integer>
</dict>
</plist>

Of course the LaunchAgent has to be enabled:

launchctl enable /Library/LaunchAgents/com.user.ramdisk.plist

After this, reboot your Mac and watch the RamDisk to see if everything’s working:

image
image 1

Within less than one hour it saved more than 700MB writes (The Google folder gets constantly written, the size doesn’t represent the real save).
Time to search how we can actually see how much is written to the RAM disk, there must be something we can do with iostat for that.

iostat -Id disk2
guy@Mars2020 ~ % sudo iostat -Id disk2
              disk2 
    KB/t xfrs   MB 
   24.37 153324 3648.93 

Instead of saving a meager 700MB, we saved 3648MB of disk writes.

If you have other folders that could be on a RAM disk let me know in the comments.

Geef een reactie

Deze site gebruikt Akismet om spam te verminderen. Bekijk hoe je reactie-gegevens worden verwerkt.