The Linux Way of youtube to mp3
Few days ago, I found a new song in youtube and I've been listening it since then. I like to have it in my mp3 player but I couldn't find it in mp3 format. First, I thought to google it for tools or add-ons to convert from youtube video to mp3 but then I realized that it is actually quite easy with the shell. The necessary processes are already in place in my Linux.
1. youtube-dl, is a small command-line program to download videos from YouTube.com and a few more sites.
2. ffmpeg, is a complete, cross-platform solution to record, convert and stream audio and video.
3. lame, is a high quality MPEG Audio Layer III (MP3) encoder licensed under the LGPL.
Here is the procedure;
First download the video with youtube-dl
$ youtube-dl "http://www.youtube.com/watch?v=QjA5faZF1A8"
then extract the audio from the video file with ffmpeg
$ ffmpeg -i QjA5faZF1A8.flv QjA5faZF1A8.wav
last, convert the wav file to mp3 with lame
$ lame QjA5faZF1A8.wav QjA5faZF1A8.mp3
Enjoy it
ThinkPad x100e and Fedora 13
I've been running Fedora 12 with x100e since April. I had written about the Fedora 12 installation here. I decided to install Fedora 13 last week. I think Fedora 13 installation is smoother than Fedora 12 installation. In addition, I've been more satisfied with Fedora 13 performance so far.
Installation consists 3 steps,
1. Put your Fedora 13 DVD to your driver and boot up from DVD drive. Follow the installation steps and install Fedora 13. I performed the installation with external DVD driver. There is no trick in the first step, it's similar to any other simple installation.
2. During the first boot after installation, Fedora 13 freezes. You need to edit kernel parameters as following. At Grub prompt, press 'e' to edit the command before booting, then select the second line and again press 'e' to edit the selected command. Type radeon.modeset=0 at the end of the command, press 'b' to boot up. This is the way to boot up Fedora 13 until display driver installation.
3. Install the ATI display driver and wireless adapter driver.
a. ATI Display Driver link (Notebook Graphics -> Mobility Radeon Series -> Mobility Radeon HD 3xxx Series -> Linux x86).
b. You need to get wireless adapter driver from Realtek website and build it (to install compiler and other development packages as prerequisite). The RTL8192SE driver works well in my system, even wireless adapter is reported as 8172 in lspci output.
Now, you can boot up without radeon.modeset=0.
Display
Numbers of CPU Cores in Solaris 10
The psrinfo is a tool that reports the numbers of physical processors and numbers of virtual processors. Most unfortunate, it is not possible to find out numbers of cores in the Solaris 10 psrinfo output. Below output shows that my configuration has got 2 physical processors and total 16 virtual processors.
alper:~ $ psrinfo -vp The physical processor has 8 virtual processors (0-7) SPARC64-VII (portid 1024 impl 0x7 ver 0x91 clock 2400 MHz) The physical processor has 8 virtual processors (8-15) SPARC64-VII (portid 1032 impl 0x7 ver 0x91 clock 2400 MHz)
To find out the numbers of cores, you need analyze the kstat output. The below output shows that virtual CPU instance: 0 belongs to cpu core 0 (core_id line in output).
alper:~ $ kstat cpu_info
module: cpu_info instance: 0
name: cpu_info0 class: misc
brand SPARC64-VII
chip_id 1024
clock_MHz 2400
core_id 0
cpu_fru hc:///component=/MBU_A/CPUM0
cpu_type sparcv9
crtime 126.7569109
current_clock_Hz 2400000000
device_ID 279287011836295
fpu_type sparcv9
implementation SPARC64-VII (portid 1024 impl 0x7 ver 0x91 clock 2400 MHz)
snaptime 10650736.0805644
state on-line
state_begin 1269762960
supported_frequencies_Hz 2400000000
Therefore, it is possible to find out the number of cores by counting numbers of unique core_id in `kstat cpu_info` command.
alper:~ $ kstat cpu_info | grep core_id | awk '{ print $2}' | uniq | wc -l
8
Briefly, my configuration has got 2 physical processors, 8 cores (4 in each physical processor), and 16 virtual processors (2 in each core).
ThinkPad x100e and Fedora 12
I had installed the Fedora 12 via external DVD driver, I hadn't faced any problem during the installation. However, Fedora freezes on the first boot. I can make it boot by disabling ACPI in Grub.
At Grub prompt press 'e' to edit the command before booting, then select the second line and again press 'e' to edit selected command. Type acpi=off at the end of the command, press 'b' to start the boot procedure.
After I successfully boot up the Fedora, I updated the system. Unfortunately, the ACPI problem hasn't been solved by updates. I added acpi=off to the grub.conf file under /boot/grub/grub.conf to make it permanent (run grub-install after editing grub.conf file, like grub-install /dev/sda). Disabling ACPI isn't very good idea, especially in a laptop. As I found on the net, Ubuntu works fine when acpi=on. I am going to try finding better Fedora solution later. I think it is enough to get it work today.
The wireless adapter driver isn't supported on default. You need to get it from Realtek website and build it (to install compiler and other development packages as prerequisite). The RTL8192SE driver works well in my system, even wireless adapter is reported as 8172 in lspci output.
Finally, I typed the commands to getting the scroll button work as follows;
> yum install gpointing-device-settings.i686 > gpointing-device-settings
- Select TPPS/2 IBM TrackPoint, check "Use wheel emulation" and set 2 as button value (not permanent!).
Note: After I wrote the explanation above, I found another way to run Fedora on x100e. I patched and compiled the kernel. I run the Fedora without acpi=off now. I sent the patch to other two users and they reported that it works fine on their x100e as well. Please write me if you need the patch.
Peterson’s Solution on Modern Multiprocessors
I wrote about the Peterson's Solution in one of my previous posts. I gave an example of how the Peterson’s n-Process Protocol can be implemented in C. It is a classical software based solution to the critical section problem. It isn't desirable to implement on modern systems since it wastes CPU time (busy waiting).
In addition, there is one -very important- reason why it isn't implemented today; there is no guarantee that Peterson's Solution will work on modern multiprocessor. When I had first heard it, I couldn't understand the exact reason why it will not work. Yesterday afternoon, I had time to search. I was thinking I can understand the whole picture, every details in a short time (how naive).
I found the first well described information in Solaris' Multithreaded Programming Guide. Briefly, it says, Peterson's Algorithm works when the multiprocessor has strongly ordered memory. However today's multiprocessors (not all) have store buffers. When a process/thread executes a store instruction, the data is put into the store buffer. The buffered data is sent to the cache sooner or later, but not necessarily right away.

Consequently, two threads running on separate processors both could enter the critical section. Each thread sets its own interested array slot (interested[process] = TRUE) and then loads from the others slot (interested[other] == TRUE). Both threads could read the old values (FALSE) and then assume that the other party is not interested. It means both enter the critical section.
#define TRUE 1
int turn;
int interested[2];
void enterRegion(int process)
{
int other = 1 - process;
interested[process] = TRUE;
turn = process;
while (interested[other] == TRUE && turn == process)
;
}
void leaveRegion(int process)
{
interested[process] = FALSE;
}
The first explanation is pretty clear. However when you dive into the details, it becomes a complex subject (memory models, instruction reordering etc.). In addition it is hardware depended, you should read different documents for Intel, SPARC or other architectures. Anyway, I don't like to write about advanced topics which I don't have accurate knowledge yet. I found and downloaded lots of documents. I think it will take at least few months to read and to understand them. I had estimated few hours at the beginning.
After explained the problem, you might think of the question, then how can we implement multithreading? It is really easy as an application developer, just use the synchronization objects provided by operating systems (mutex locks, condition variables, read-write locks, semaphores etc.). Kernel developers consider all hardware depended issues and implement the synchronization objects in proper way.
I had been wondering if I can reproduce the buggy case on modern processors. Therefore I have implemented a test code with Peterson's Algorithm and performed some tests on three different hardware. The test code is very simple, one thread adds 1 to a variable and the other subtracts 1. Results are interesting;
- First machine has got 2 x SPARC64-VII CPU as following
bash-3.00$ psrinfo -vp The physical processor has 8 virtual processors (0-7) SPARC64-VII (portid 1024 impl 0x7 ver 0x91 clock 2400 MHz) The physical processor has 8 virtual processors (8-15) SPARC64-VII (portid 1032 impl 0x7 ver 0x91 clock 2400 MHz)
Threads run about a minute and after 32.577.239 calculations (add/sub), I have seen 17 cases where both entered critical section.
- Second machine has got 2 x UltraSPARC-T2+ CPU as following
bash-3.00$ psrinfo -vp The physical processor has 48 virtual processors (0-23 32-55) UltraSPARC-T2+ (cpuid 0 clock 1162 MHz) The physical processor has 48 virtual processors (64-111) UltraSPARC-T2+ (cpuid 64 clock 1162 MHz)
Threads run about 5 hours (please consider that first machine performed 32.577.239 calculations in 1 minute), I haven't seen any case where both enter critical section (I am not sure if it means it never happens on UltraSPARC-T2+, I am going to read the CPU specs to understand its memory model).
- Last machine has got 1 x Intel(R) Core(TM)2 Quad CPU Q8400 @ 2.66GHz CPU as following
vendor_id : GenuineIntel cpu family : 6 model : 23 model name : Intel(R) Core(TM)2 Quad CPU Q8400 @ 2.66GHz stepping : 10 cpu MHz : 1998.000 cache size : 2048 KB
Threads run about 5 minutes, I have seen 3 cases where both entered critical section.