Transcode temporary directory on Ramdisk -- solved

I was trying to save wear on my SSD by putting the transcode temporary directory on a tmpfs drive and ran into some problems. Apparently there are some issues with the pruning process as the tmpfs disk kept eventually getting full. I setup a 4gb tmpfs file system and over the course of about an hour of watching 1080i live TV transcoded to 1080p the disk filled up with thousands of small 1-2mb files that ended in .ts. I saw in the log the pruning effort starting once the disk got to less than 100mb left, but either it quit or got stuck as the disk filled up a minute or two later. I tried this several times with the same result.

After looking at the files I decided to prune them myself. Here is the command that I used to prune the files:

find /your/transcodetempdirectory -name *.ts -cmin +2 -exec rm {} \;

What this does is search through the named directory and all sub-directories and deletes all files that end in .ts which have been modified more than 2 minutes ago. Now, it needs to be executed at regular intervals which can be done like this:

watch -c -n 30 "date; df -h -T /your/transcodetempdirectory;find /your/transcodetempdirectory -name *.ts -cmin +2 -exec rm {} \;;df -h -T /your/transcodetempdirectory"

This series of commands will cause the command to be executed every 30 seconds, along with adding some additional commands to show the disk space on the tmpfs file system (mounted at /your/transcodetempdirectory) before and after the deletion of files, plus a date command to show you when it last ran.

From my tests running a 1080i to 1080p transcode generates about 700mb of files over 3 minutes and when the delete command runs it gets down to about 400mb of files.

To automate this you need a way to invoke this at startup. To do that you must save this command as a script file (.sh extension), and mark it executable. The script only needs this one line (i.e. no bin/bash line necessary).

To invoke it you need a couple of “screen” commands in a script that runs at startup like this:

screen -dmS ramdiskclean
screen -S ramdiskclean -p 0 -X stuff "/etc/scripts/clearramdisk.sh\n"

The first command creates a virtual terminal session that runs detached (i.e. doesn’t need a logged in user to stay active), and the second command “pastes” the stuff in the quotes into the virtual terminal session called ramdiskclean and presses enter (the \n at the end). The watch command is in the script called /etc/scripts/clearramdisk.sh .

You can “attach” to this virtual session from your regular terminal session to see what’s going on by entering:

screen -r ramdiskclean

And to detach that virtual session and go back to your regular session enter into the virtual session:

ctrl-a d

Note that you don’t want to do ctrl-c or exit in the virtual session unless you want to terminate the watch command. Please read about the screen command in the linux documentation for more details.

Doing things this way has several advantages. First of course it gets around whatever the issue is with pruning. Second, you can have a decent size tmpfs filesystem for the transcoder temporary directory and not use more memory than you have to, as waiting for it to get to just 100mb short of full as the pruning does means you use up lots of memory for no good reason. Doing it this way, the only time you would use a lot of space is if lots of users are logged in transcoding at the same time.

Regarding the choice of more than 2 minutes, I just picked that out of the hat. From examining the files the last access time was never more than 2 seconds after the creation time, which leads me to believe that you could actually set that time down to 1 minute rather than two. Unfortunately there doesn’t appear to be an option on the find command to set the time by seconds, so the lowest you could go is 1 minute. Also if you want to key off of last accessed time rather than last modified time you can substitute “amin” for “cmin” in the find command.

I also tried playing a recorded TV show and the results were similar in that the transcode created a whole lot of small .ts files that it apparently only really needs for a second or two after creation if the last accessed time is to be believed. I haven’t tied playing a movie, so I don’t know if the way it creates these temporary files is any different.

The only real risk here would be that you delete .ts files that it still needed, which I think would just result in the stream using that file failing, as that’s what happened with the tempfs disk got full. If you you have enough memory you could make the tmpfs file even bigger and set “cmin” higher.

The bottom line here is that if you are going to use tmpfs for this directory, which is a really good idea if you are using a SSD given the high number of IOs being done to to it, a backstop appears necessary because the self pruning apparently has issues. Secondarily, if you use tmpfs you will save some memory by not keeping hundreds of megabyes or even gigabytes of .ts files in memory waiting for the pruning process that aren’t being used. In my test with a 4gb tmpfs file handling a single 1080i to 1080p transcode it looked like the oldest files were between half an hour and an hour old when the disk got full, before I set this up, and as it appears they are needed for only seconds after the last modify time, it sure doesn’t seem necessary to keep them around, even if the pruning issue didn’t exist.

1 Like

I’ve got some updates on this, as I started having some things I wanted to record not record. It appears that you must have 4gb of transcode temporary directory space for each half hour of a show you want to record. I think it’s doing the math actually by the number of seconds to record, but it works out to 4gb per half hour. It was requiring that for both hidef and regular def TV programs so I don’t think it takes into account the resolution of the show. This is the same amount of space it requires to be available in the location where the show itself will be stored.

Also, if you begin to record a second show while a previous one is recording, say if you have two tuners, it won’t allow the second show to record unless there is free space for the sum of both shows together. I’m not certain if is calculating the remaining number of seconds in the first show or is using the entire first show length to calculate this as my tests were done when the first show was only a minute or so in progress when I started recording the second show.

Here is what you see in the log if it decides not to record:

Oct 28, 2019 21:03:12.727 [0x7f0ac2fed700] DEBUG - DVR:Grabber: checking for 15510 MB of available disk space in grab path and transcode cache path
Oct 28, 2019 21:03:12.730 [0x7f0ac2fed700] ERROR - DVR:Grabber: Not enough free space in "/mnt/plextemp/Transcode/Sessions"
Oct 28, 2019 21:03:12.730 [0x7f0ac2fed700] DEBUG - DVR:Grabber: Operation for 'Retro Report on PBS - E7 - Episode Seven' on channel 48.1 completed with status error (There was not enough free disk space to start the recording)
Oct 28, 2019 21:03:12.732 [0x7f0ac2fed700] ERROR - DVR:Grabber: Starting media grab failed.

In the above log the 15510 MB is what it showed for the second show, but it’s really almost the full sum for two hour long shows as the first show was already recording. It’s a bit short because either the first show was already recording or because I started recording the second show a few minutes into it, or both.

While this logic may be correct for the final location of the show itself, it’s completely incorrect for the transcode temporary directory, as those small transcode files are only needed for a very short period of time before the can be deleted. I tested this by creating a tmpfs file system large enough to allow it to record a single hour long show (8gb). I used the script from my first post to delete the .vs files after a few minutes and the whole hour show recorded correctly, meaning that when the show is finished it doesn’t need the full hour’s worth of files in the transcode temporary directory to do any processing on the 8gb recorded file in the .grab directory before moving it to its final location in the same directory path that the permanent shows are stored.

They should really fix all this. In the mean time, you can still put your transcode directory on a tmpfs, but you are going to need to make it large enough to hold basically the full size of all the shows you intend to record at once, even though you not need to use even a tiny fraction of that space if you are deleting the .vs files with my script.

Obviously you don’t want to dedicate that much memory to this effort, even if you have it available, and actually you don’t have to. You need to figure out how much you are going to need by running my script above while watching live TV at the highest resolution with the cmin or amin you have set on the delete command. I’d take that number and double it, and then make sure you have that much free RAM, plus perhaps another gb or two in your Plex server system. For the rest you can create a large swap file to cover the rest of the size you need to define the tmpfs filesystem as.

So for example lets say that your plex system is using 1gb of memory and you have 4gb of memory available on the computer, and you need a 17gb tmpfs file system to support recording two one hour shows at the same time. You should then create a 21gb swap file (17 for the tmpfs and 4 to back the rest of the memory), and then you should run your test with live TV to see how much ends up on the tmpfs file system when you delete the files at various time limits. On my system setting amin (I use amin instead of cmin) to +5 (5 minutes) lets the tmpfs file system get to about 500mb in size before the oldest files are deleted. This means that even though the tmpfs file system is 17gb in size, it won’t get much bigger than 500mb and it won’t use more memory than that, and it won’t use swap space. The swap space is only necessary in case something bad happens and the pruning stops, at which point the large swap file would prevent your system from locking up even if the tmpfs got full.

It’s unfortunate that it has to get this complicated, but Plex really needs to fix their code not to require the temporary transcode directory to be equal in size to the sum of all the records in progress. Based on how it’s working in practice, that’s completely unnecessary, and with SSDs becoming the common form of storage, they shouldn’t be doing things that cause excessive IOs to those SSDs if they don’t absolutely have to. There really is no reason I can see for the transcode temporary directory to need to be any larger then a gigabyte or two, which would be well within the ramdisk capabilities of most systems, and wouldn’t require a huge swap file to fake out the software.

1 Like

Good work but in general ramdisks < 32GB aren’t for TV use, especially if you have 4 tuners going at once.

I’m guessing you don’t time shift Live TV very much or this is on a higher end machine. The reason Plex is storing all the segments is for the those that want to jump back to the beginning or pause 15 -20 minutes and skip through commercials. Also, have you tested what happens if you decide to click the record button on something you have been live streaming for a time.

1 Like

I’ll do some more tests, but even before I did this Plex appeared to have problems when you pause too long or try to go backward on a live show (sometimes it goes just to the beginning and sometimes it won’t go back more than a few minutes and if you pause sometime when you resume it jumps to current and won’t let you go back). I did test playing a recorded show with this in effect and jumping around, which worked fine.

I’m running Plex as a VM under VMWare ESXi on a 14 core server with 128gb of memory, so I can make this VM as large as I need to in order to get this to work. I’m more concerned with the wear and tear on the storage as it’s a bunch of SSD drives configured as RAID 6., My only other choice would be to add a traditional USB hard disk to the configuration for the exclusive use of Plex’s transcode temporary directory.

you should definitely have more ram if you want to use ramdisk/tmpfs/dev/shm

32g would be the minimum, 4 gig is way too small.

on linux you do not even need to create a tmpfs, you can use /dev/shm which uses 50% of install ram.

I have 64g ram and a 32g /dev/shm has been trouble free.

transcoder temp folder is set directly to /dev/shm in plex settings.

also, there are a several existing threads about this topic which might be informative.

https://forums.plex.tv/search?q=transcoder%20temp%20folder%20ram%20order%3Aviews

Did you have any issues with it getting full? Before I created the script my transcode buffer got full as if the default pruning (at 100mb remaining) didn’t work.

nope. plex does prune old transcode files automatically, it just has to have enough space to do whatever it needs to, especially in regards to live tv, and 4 gig is simply not enough.

dvr recordings do not use the transcoder temp folder, they use a hidden .grab folder in the library where the recording is set to.

other transcodes for remote/local go to the transcoder temp folder, personally I have had at least 4 transcodes going with no known problems, with 32gig of /dev/shm.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.