Thursday, September 22, 2022

C# - Debugging Deadlock

Basic

See https://michaelscodingspot.com/c-deadlocks-in-depth-part-1/

Tips

  1. Use dedicated lock object
  2. Keep lock object private inside a single class and do NOT pass it around outside
  3. Avoid triggering event from inside a lock

Wednesday, September 21, 2022

C# - Dispose Rule

Tips

  1. All IDisposable objects must be disposed. There are only few exceptions e.g. https://stackoverflow.com/questions/38784434/how-to-properly-dispose-the-stream-when-using-streamcontent
  2. Use "using" by default for all IDisposable classes. If "using" cannot be used, at least "try ... finally" must be used and dispose inside "finally".
  3. If a method returns an IDisposable classes, it is the responsibility of the caller to dispose it properly.
  4. Try to keep IDisposable objects lifetime as short as possible e.g. Bitmap

Tuesday, September 20, 2022

C# - Lock Rule

Tips

  1. In each class, lock every public methods and any methods triggered by event from other classes.
  2. For each methods, lock the entire code from start until the end of method by default. Reducing the lock coverage to only certain part of code in a method can be done carefully after this as an optimization.
  3. Triggering event should not be performed from inside a lock.

Setting Up Jenkins EC2 Linux Agent with Instance Storage




AWS EC2 Linux instance storage needs to be initialized first before it can be used. When setting up Jenkins EC2 Linux agent, we can do this with Init Script. The Init Script below formats and mounts instance storage if available and use it as Jenkins workspace and Docker data-root. This Init Script is failsafe. It means that it will not cause any errors if it turns out that the EC2 Linux instance does not have an instance storage.


set -x

# Initialize instance storage if available e.g. m5dn.large

mkdir -p /storage
test -z "$(blkid /dev/nvme1n1)" && (mkfs -t ext4 /dev/nvme1n1 && mount /dev/nvme1n1 /storage) || true 

# Prepare to install packages using APT

export DEBIAN_FRONTEND=noninteractive

apt-get -qq update

# Install JRE required by Jenkins agent + create a folder for Jenkins workspace

apt-get -qq -y install default-jre-headless git

mkdir -p /storage/jenkins
chmod a+w /storage/jenkins

# Install our build tools e.g. make and docker + configure docker to use instance storage if available

apt-get -qq -y install make

mkdir -p /etc/docker
mkdir -p /storage/docker
cat << EOF > /etc/docker/daemon.json
{
"data-root": "/storage/docker"
}
EOF

curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh

chgrp docker /usr/bin/docker
chmod g+s /usr/bin/docker

# Clean up

apt-get clean

Saturday, December 10, 2016

Kernel Patch - USB3 HDD Gets Mounted Again After Safely Remove


I have just found out that the Linux kernel patch for this issue has appeared on Linux Kernel Mailing List. I think it is supposed to be included in Linux kernel 4.10. Here is how I applied the patch on Debian 8 (Jessie).

Requirements

  1. Debian 8 (Jessie)
  2. Linux kernel source code 4.4.x LTS (Vanilla)
  3. Some basic knowledge on compiling Linux kernel source code

Instructions


1. Download Linux kernel source code.

$ wget "https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.4.35.tar.xz"

2. Extract kernel source code.

$ tar xvf linux-4.4.35.tar.xz

3. Enter extracted kernel source code.

$ cd linux-4.4.35

4. Download USB3 HDD safely remove patch here and apply it.

$ patch -p1 -i fix-auto-remount-of-safely-removed-or-ejected-usb-3-devices-v3.patch

5. Copy kernel config from default kernel

$ cp /boot/config-3.16.0-4-686-pae .config
$ make olddefconfig

6. Adjust kernel config. Enable CONFIG_IP_NF_NAT and its sub-configs as modules (or else you will lose iptables NAT).

$ make menuconfig

7. Compile kernel.

$ make -j $(nproc) deb-pkg KDEB_PKGVERSION=$(make kernelversion)-1~local1

8. Install kernel. See notes below for any errors encountered.

$ sudo dpkg -i linux-image-4.4.35_4.4.35-1~local1_i386.deb linux-headers-4.4.35_4.4.35-1~local1_i386.deb

Notes


1. The open-vm-tools included in Debian Jessie cannot be compiled with kernel 4.4. It can be safely purged if you don't use it.

$ sudo apt-get purge open-vm-tools-dkms

2. If you use broadcom-sta-dkms, use the one from backports. The one from stable is too old for kernel 4.4.

$ sudo apt-get -t jessie-backports install broadcom-sta-dkms

3. To suppress error about pcspkr already registered on boot, blacklist snd-pcsp.

$ echo "blacklist snd-pcsp" > "/etc/modprobe.d/snd-pcsp-blacklist.conf"

4. If you are on Intel graphics and Xorg occasionally crash e.g. when you are playing video using VAAPI, add "i915.semaphores=1" to GRUB_CMDLINE_LINUX_DEFAULT in "/etc/default/grub" and run "sudo update-grub"

GRUB_CMDLINE_LINUX_DEFAULT="quiet i915.semaphores=1"

References

Sunday, March 13, 2016

FFmpeg - Transcoding Videos for Pioneer Head Unit AVH-P4450BT


Here are the basic steps to transcode videos for Pioneer Head Unit AVH-P4450BT using FFmpeg. The resulting videos should be playable on similar models such as AVH-P3450DVD, AVH-2450BT, and AVH-1450DVD. 

Pioneer Head Unit AVH-P4450BT


I am using Debian 8.3 and FFmpeg 7:2.8.3-1~bpo8+1 from jessie-backports. To get maximum video quality for the given bitrate, we will use two-pass encoding.

1. Since we have to use the same options for both encoding passes, we declare it first.

$ FFMPEG_OPTS="-c:v libxvid -b:v 1800k \
-vf scale='-2:min(ih,240)',setsar='1/1' -vtag DX50 \
-mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -g 300 -bf 2 \
-c:a libmp3lame -q:a 2 -ac 2 \
-threads $(nproc)"

Explanation:

  • -c:v libxvid -b:v 1800k XVID 1800k.
  • -vf scale='-2:min(ih,240)',setsar='1/1' Scale the video size.
  • -vtag DX50 The resulting video won't be detected by the head unit without this.
  • -mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -g 300 -bf 2 Some mumbo-jumbo to maximize video quality.
  • -c:a libmp3lame -q:a 2 -ac 2  LAME MP3 preset standard (good for music videos).
  • -threads $(nproc) Use all available CPU cores.

2. Run 1st pass.

$ ffmpeg -i "input.mp4" $FFMPEG_OPTS -pass 1 -passlogfile "ffmpeg2pass" -f null "/dev/null"

3. Run 2nd pass.

$ ffmpeg -i "input.mp4" $FFMPEG_OPTS -pass 2 -passlogfile "ffmpeg2pass" -f avi "output.avi"

That's it!

Wednesday, April 29, 2015

Updated Steps for Enabling BCM43142 Bluetooth on Debian 8 "Jessie" Stable

UPDATE 2015-05-15: Add a script to reload btusb after suspend to "install-btusb-bcm43142a0". 

Debian 8 "Jessie" has been released recently. A lot of things have changed since a year ago when it was still in Testing, especially on the kernel side. The good news is the support for loading firmware for Broadcom bluetooth has been committed to the mainline kernel. The bad news is for the vendor ID and the device ID for BCM43142 bluetooth (105b:e065) is still not there. Therefore, we still need to patch and recompile btusb in order to get it to work. The difference is the patch is much smaller now.

Preparation

1. Make sure you have the correct device. We are talking about 105b:e065 here.

$ lsusb | grep 105b:e065
Bus 003 Device 003: ID 105b:e065

2. Make sure you already have deb-src lines on your "/etc/apt/sources.list".

deb-src http://kartolo.sby.datautama.net.id/debian/ jessie main contrib non-free
deb-src http://kartolo.sby.datautama.net.id/debian/ jessie-updates main contrib non-free
deb-src http://security.debian.org/ jessie/updates main contrib non-free

3. Install "build-essential".

# apt-get install build-essential

4. Download and install "hex2hcd" from source.

$ wget "https://github.com/jessesung/hex2hcd/archive/master.zip"
$ unzip master.zip
$ cd hex2hcd-master
$ make
# cp hex2hcd /usr/local/bin

5. Get hex firmware for BC43412 from a Windows installation under "C:\Windows\System32\Drivers". The file name should be like "BCM43142A0_*.hex".

Recompiling

1. Download the bash script that I have written here and extract it.

$ tar xvf btusb-bcm43142a0-20150515.tar.gz

2. Put your hex firmware together with the script and rename it to "BCM43142A0.hex".

$ ls
BCM43142A0.hex  
btusb-bcm43142A0.patch  
install-btusb-bcm43142a0

3. Run the script. It will download the kernel source for the running kernel, patch btusb module, and install the firmware.

# ./install-btusb-bcm43142a0

IMPORTANT: Please verify the content of the script yourself first before running. I have only tested it on my installation of Debian 8 "Jessie" i386. I won't be responsible for any damage that it caused. Use at your own risk!

4. Your bluetooth should be running on the next boot. If not, try disabling and enabling it. See whether it runs.

If you have any questions, write in the comments below.