Tuning Ubuntu Studio for HTPC

credit: https://wiki.linuxaudio.org/wiki/system_configuration#installing_a_real-time_kernel

  • Checking /etc/fstab mounting drive should using noatime. 
  • Ubuntu Studio already set vm.wappiness = 10. But if you have more than 4 GB ram you could run HTPC linux without swap.
  • add user to audio group. Use $USER return current user or just type username.

sudo useradd -G audio $USER

  • limits.conf/audio.conf. It is advisable to set up your /etc/security/limits.conf file properly, for example:

@audio – rtprio 90 # maximum realtime priority
@audio – memlock unlimited # maximum locked-in-memory address space (KB)

  •  Using realtimeConfigQuickScan script for checking system

You need to install git first

sudo apt install git

Install script and run script

git clone git://github.com/raboof/realtimeconfigquickscan.git
cd realtimeconfigquickscan
perl ./realTimeConfigQuickScan.pl

  • If script warn about CPU governer and said CPU are not run in performance mode. As some might found that cpufreq don’t have driver for their CPU. Just disable CPU energy saving and Turbo mode in Bios would be more easy solution. you could install cpufrequtils and run cpufreq-info to check if driver is available or not. You could check CPU speed by using command

more /proc/cpuinfo | grep MHz

After setting change, every cores in CPU should run at same speed whether load are high or low.

  • In Ubuntu there’re no out of the box realtime kernel, we have to built it ourselves. If you just using Linux for HTPC and didn’t record video or audio, you don’t need realtime kernel. To install necessary packages run

sudo apt-get install kernel-package fakeroot build-essential libssl-dev

mkdir -p ~/tmp/linux-rt
cd ~/tmp/linux-rt
wget -c https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.11.8.tar.xz
wget -c https://www.kernel.org/pub/linux/kernel/projects/rt/4.11/patch-4.11.8-rt4.patch.xz

Extract the kernel sources and patch them with the RT patchset:

tar xJvf linux-4.11.8.tar.xz
cd linux-4.11.8
xzcat ../patch-4.11.8-rt4.patch.xz | patch -p1

 

cp /boot/config-`uname -r` .config

This will copy the config of the kernel you’re currently using. You might want to consider using a config of a kernel already optimized for audio work, like the Ubuntu -lowlatency kernel config. The next step is to create a new config with full preemption enabled from the copied config:

make oldconfig

make -j `nproc` LOCALVERSION= deb-pkg

P.S.

I don’t recommend booting neither realtime or lowlatency kernel for audio playback. When playback with ALSA mode in deadbeef and audacious, I found that its sound a little widen, and less dynamic compare to generic kernel. And I cannot hear differences between realtime and lowlatency kernel. Unless, you could hear differences between generic and lowlatency, there are no need for compiling realtime kernel, imo.

Details of my system are Ubuntu Studio 16.04 LTS, kernel version 4.11.8, and ESI Juli@ + External DAC

 

TEARING WHEN PLAYBACK VIDEO OR SCROLLING WEBPAGE (Intel Graphics)

Create file
/usr/share/X11/xorg.conf.d/20-intel.conf

or

/etc/X11/xorg.conf.d/20-intel.conf

Check folder first whether which one existed for *buntu mostly /usr/share/X11/xorg.conf.d/

Section "Device"
	Identifier "Intel Graphics"
	Driver "Intel"
		Option "AccelMethod" "sna"
		Option "TearFree" "true"
		Option "DRI" "3"
		Option "TripleBuffer" "true"
		Option "MigrationHeuristic" "greedy"
		Option "Tiling" "true"
EndSection

or

Section "Device"
	Identifier "Intel Graphics"
	Driver "Intel"
		Option "AccelMethod" "uxa"
		Option "DRI" "2"
		Option "TripleBuffer" "true"
		Option "MigrationHeuristic" "greedy"
		Option "Tiling" "true"
EndSection

for Skylake

Section "Device"
	Identifier "Intel Graphics"
	Driver "intel"
		Option "DRI" "false"
EndSection

 

Note

  1. SNA acceleration is default, change to UXA only you encounter pixelated graphics, corrupt text, etc.
  2. “TearFree” doesn’t work with UXA acceleration
  3. by default DRI is 3 change to 2 if you’re using virtual desktops and switching between desktops cause artifacts


References

https://wiki.archlinux.org/index.php/intel_graphics

https://www.x.org/archive/X11R7.5/doc/man/man4/intel.4.html

 

Force KODI to use ALSA

Force load PulseAudio and ALSA at the same time. Buggy, if you set Kodi Audio output to same device as default desktop audio output. In my case, work like charm as my default desktop audio route to HDMI while Kodi using another device.

create file /usr/share/applications/kodi-alsa.desktop and paste this into the file

 

[Desktop Entry]
Version=1.0
Name=Kodi-ALSA
GenericName=Media Center
Comment=Forced Kodi to run on ALSA instead of PulseAudio
Exec=/usr/share/applications/kodi-alsa.sh
TryExec=/usr/share/applications/kodi-alsa.sh
Icon=kodi
Terminal=false
Type=Application
Categories=AudioVideo;Video;Player;TV;

Actions=Fullscreen;Standalone;

Path=
StartupNotify=false

Create file /usr/share/applications/kodi-alsa.sh and paste this into file

#! /bin/bash

# this makes kodi start with ALSA instead of FUCKING pulse audio

# disable pulse audio
pasuspender

# start kodi with ALSA
AE_SINK=ALSA kodi

Set permission so kodi-alsa.sh could be executed.

credit : https://gist.github.com/bmaeser/1918514429e4855d5188

Config PulseAudio

To disable PulseAudio (affect every user)

in /etc/pulse/client.conf

change this line from

; autospawn = yes

to

autospawn = no

To set sample format, check sample spec first by

pacmd list-sinks

Screenshot at 2017-03-02 22:39:29.png

in /etc/pulse/daemon.conf

resample-method = speex-float-5

default-sample-format = float32ne
default-sample-rate = 88200
alternate-sample-rate = 96000
default-sample-channels = 2
default-channel-map = front-left,front-right

Credit:http://kodi.wiki/view/PulseAudio

SSD & Ram Drive Tweak

As my PC has 16GB ram and SSD. I decided to tweak it like this.

In /etc/fstab, for SSD drive I replace “errors=remount-ro” with “errors=remount-ro,noatime,commit=60”

and add these lines to move system tmp and log to Ramdrive

#Create Ram Disk for cache#Create Ram Disk for cache
tmpfs /var/log tmpfs defaults,noatime,size=10M 0 0
tmpfs /tmp tmpfs defaults,noatime,mode=1777,size=20% 0 0
tmpfs /home/<<your username>>/.cache tmpfs defaults,noatime,mode=1777,size=20% 0 0
tmpfs /home/<<your username>>/.thumbnails tmpfs defaults,noatime,mode=1777,size=10% 0 0

TCP Tweak

In file /etc/sysctl.conf

# tweak - start
net.core.wmem_max=33554432
net.core.rmem_max=33554432
net.ipv4.tcp_rmem= 10240 87380 33554432
net.ipv4.tcp_wmem= 10240 87380 33554432
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_sack = 1
net.ipv4.tcp_no_metrics_save = 1
net.core.netdev_max_backlog = 5000
# tweak - end

credit: https://www.cyberciti.biz/faq/linux-tcp-tuning/

In file /etc/rc.local

ip link set mtu 7000 dev enp4s0

Set MTU size by add this line

ip link set mtu [supported MTU] dev [NIC]

Check MTU size that your NIC supported first.

sudo ifconfig [NIC] mtu [MTU]

Fix slow network file transfer

If you’re using Linux with Windows file server on Gigabit Network, Samba max bandwidth is about 20-30MB/s. You have to install NFS and using it instead of Samba. I couldn’t find the way to make Samba able to read file from Windows share at 100+MB/s. If you could, your guide would be appreciated.

/etc/samba/smb.conf

Increase send and receive buffers size by add this line

[global]

# FORCE THE DISK SYSTEM TO ALLOCATE REAL STORAGE BLOCKS WHEN
# A FILE IS CREATED OR EXTENDED TO BE A GIVEN SIZE.
# THIS IS ONLY A GOOD OPTION FOR FILE SYSTEMS THAT SUPPORT
# UNWRITTEN EXTENTS LIKE XFS, EXT4, BTRFS, OCS2.
# IF YOU USE A FILE SYSTEM THAT DOES NOT SUPPORT UNWRITTEN
# EXTENTS, SET "strict allocate = no".
# NOTE: MAY WASTE DRIVE SPACE EVEN ON SUPPORTED FILE SYSTEMS
# SEE: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=798532

   strict allocate = Yes

# THIS IS TO COUNTERACT SPACE WASTAGE THAT CAN BE 
# CAUSED BY THE PREVIOUS OPTION 
# SEE: https://lists.samba.org/archive/samba-technical/2014-July/101304.html

   allocation roundup size = 4096

# ALLOW READS OF 65535 BYTES IN ONE PACKET.
# THIS TYPICALLY PROVIDES A MAJOR PERFORMANCE BENEFIT.

   read raw = Yes

# SUPPORT RAW WRITE SMBs WHEN TRANSFERRING DATA FROM CLIENTS.

   write raw = Yes

# WHEN "strict locking = no", THE SERVER PERFORMS FILE LOCK
# CHECKS ONLY WHEN THE CLIENT EXPLICITLY ASKS FOR THEM.
# WELL-BEHAVED CLIENTS ALWAYS ASK FOR LOCK CHECKS WHEN IT IS
# IMPORTANT, SO IN THE VAST MAJORITY OF CASES,
# "strict locking = auto" OR "strict locking = no" IS ACCEPTABLE.

   strict locking = No

# TCP_NODELAY:
#    SEND AS MANY PACKETS AS NECESSARY TO KEEP DELAY LOW
# IPTOS_LOWDELAY:
#    [Linux IPv4 Tweak] MINIMIZE DELAYS FOR INTERACTIVE TRAFFIC
# SO_RCVBUF:
#    ENLARGE SYSTEM SOCKET RECEIVE BUFFER
# SO_SNDBUF:
#    ENLARGE SYSTEM SOCKET SEND BUFFER

   socket options = TCP_NODELAY IPTOS_LOWDELAY SO_RCVBUF=131072 SO_SNDBUF=131072

# SMBWriteX CALLS GREATER THAN "min receivefile size" WILL BE
# PASSED DIRECTLY TO KERNEL recvfile/splice SYSTEM CALL.
# TO ENABLE POSIX LARGE WRITE SUPPORT (SMB/CIFS WRITES UP TO 16MB),
# THIS OPTION MUST BE NONZERO.
# THIS OPTION WILL HAVE NO EFFECT IF SET ON A SMB SIGNED CONNECTION.
# MAX VALUE = 128k

   min receivefile size = 16384

# USE THE MORE EFFICIENT sendfile() SYSTEM CALL FOR EXCLUSIVELY
# OPLOCKED FILES.
# NOTE: ONLY FOR CLIENTS HIGHER THAN WINDOWS 98/Me

   use sendfile = Yes

# READ FROM FILE ASYNCHRONOUSLY WHEN SIZE OF REQUEST IS BIGGER
# THAN THIS VALUE.
# NOTE: SAMBA MUST BE BUILT WITH ASYNCHRONOUS I/O SUPPORT

   aio read size = 16384

# WRITE TO FILE ASYNCHRONOUSLY WHEN SIZE OF REQUEST IS BIGGER
# THAN THIS VALUE
# NOTE: SAMBA MUST BE BUILT WITH ASYNCHRONOUS I/O SUPPORT

   aio write size = 16384

credit: https://eggplant.pro/blog/faster-samba-smb-cifs-share-performance/

However, in this line

socket options = TCP_NODELAY IPTOS_LOWDELAY SO_RCVBUF=131072 SO_SNDBUF=131072

Should change to

socket options = TCP_NODELAY IPTOS_LOWDELAY

In order to let Samba handle buffer size automatically. Should do TCP cache Tweak instead.