Tuesday, August 13, 2013

RHEL/CentOS/Fedora: How to Disable CPU Frequency Scaling/Throttling

Attention! Be sure you REALLY want to perform this action! Do it at your own risk!

I assume that you may already know the reason before reaching my post. Run the following command as root. That's all.

echo performance | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor >& /dev/null

Have fun!!!

Thursday, July 4, 2013

Some post-installation steps to make Fedora 19 more comfortable

So, as you may now, Fedora 19 (Schrödinger’s Cat) has released for few days. And this time I decided to upgrade my PC immediately, and everything seems to be very smooth.

For Fedora, I have to make some minor changes to make it more comfortable, such as:
  • Install Liberation Fonts (version 2).
  • Fix the font's rendering problem.

Liberation Fonts

Liberation fonts are installed in Fedora by default. However, it is kept at version 1.07 with very poor quality. And to get the much better quality, I always upgrade them to version 2.

You can download compiled fonts from the Liberation Fonts page or RPM packages from Fedora's page. I choose the former method.

$ wget https://fedorahosted.org/releases/l/i/liberation-fonts/liberation-fonts-ttf-2.00.0.tar.gz
$ tar -zxvf liberation-fonts-ttf-2.00.0.tar.gz
$ sudo cp -f liberation-fonts-ttf-2.00.1/*.ttf /usr/share/fonts/liberation/
$ sudo fc-cache

Now the Liberation fonts are ready to use. We will fix the font rendering problem before change font settings for GNOME to reflect what we have done so far.

Fix Font Rendering Problem

If you've ever see an Ubuntu screen, you may see a much better font rendering. The problem here is Fedora disable RGBA sub-pixel hinting. All you have to do is to enable such feature by install package freetype-freeworld from RPM Fusion.

$ rpm -ivh ftp://mirror.switch.ch/pool/3/mirror/rpmfusion/free/fedora/releases/19/Everything/x86_64/os/freetype-freeworld-2.4.11-2.fc19.x86_64.rpm

Now, to change the default fonts and default font hinting methods for GNOME, you should install gnome-tweak-tool.

$ sudo yum install gnome-tweak-tool
$ gnome-tweak-tool &

Change the default font settings as in the below screen.


To make everything the same as Ubuntu, you should run the following command.

$ echo "Xft.lcdfilter: lcddefault" >> ~/.Xresources

You may need to log out and log in again to reflect the changes.

Saturday, August 11, 2012

SOCKS Proxy over SSH with OpenSSH (Linux) or PuTTY (Windows)

Do you want to use a proxy for surfing Internet without using untrusted proxies? If you have a SSH account to a server with an Internet connection, this article will show you how to create your own SOCKS proxy for above purpose. I will also show you how to do that on both Linux and Windows.

1. Create SOCKS Proxy

Linux

On Linux, all you need is an OpenSSH client which is normally installed by default by almost distros. Open Terminal and run the following command to create a SOCKS proxy which listens for connections on port 1080.

$ ssh -D 1080 username@hostname

A more advantage command which I normally use is as follow.

$ ssh -NCf -o "ServerAliveInterval 300" -D 1080 username@hostname

Please check the man page of ssh for options.

Windows

PuTTY might be the most popular SSH client on Windows. Fortunately, it also supports SOCKS proxy. You should use the latest version which can be downloaded from here.

Open PuTTY an declare information such as host name as normal.

The main window of PuTTY where you can declare host name, port, etc.
The important part is to open a port for listening locally as a SOCKS proxy. Change to "Tunnels" category; fill the port to "Source port" and choose "Dynamic"; click on "Add" button.


Now, you can back to the "Session" category and click on "Open" button to log in to your server.

To avoid session timeout, you should tell PuTTY to send some null packets to the server after some duration. To do that, go to "Connection" category and fill out the number of seconds between null packet sending.


That is enough for proxy. You now have a SOCKS proxy listening on port 1080. It is time to configure web browser redirect every connection via newly created proxy.

2. Web Browser Configuration

Firefox

If you use Firefox, you can set the proxy information by accessing "Options" dialog on Windows or "Preferences" dialog on Linux. Then change to "Advanced" category; choose "Network" tab and click con the "Settings..." button.


Declare the proxy information as in the following screenshot.


Click on OK and OK again. That's all! You now can access to an IP check service to confirm that your web connection routed via the proxy. You can try IP2LOCATION.

Chrome and Internet Explorer

Both Google Chrome and Internet Explorer use the system settings for the proxy. You can declare the information by opening "Internet Options" dialog from Control Panel or Internet Explorer or Chrome.


Change to "Connections" tab and click on "LAN settings" button.


On the "Local Area Network (LAN) Settings" dialog, un-check "Automatically detect settings" and check on "Use a proxy server for your LAN (These settings will not apply to dial-up or VPN connections)." Then click on "Advanced" button.


Declare information as in the above screenshot and click on OK.

On Google Chrome, you can open the system proxy settings by choosing "Settings -> Show advanced settings... -> Change proxy settings..."

Hope this article is useful to you, sometime ;).


Tuesday, July 31, 2012

Cache Issues in the I/O Performance Test

Sometime you have to benchmark IO performance such as file read/write. If it was the first time you did such kind of task, you almost probability notice that the reading speed is very high, hundreds of MB per second, with a slow hard disk, such as mechanical one with 5400rpm. That is the effects of file caching.

I will show you here some techniques to overcome or minimize the cache effects.

1. Re-mount


The easiest way is to umount and re-mount the corresponding mount point. For example,

# umount /home
# mount /home

2. Clear Cache of the OS


On Linux, you can clear or drop cache of the OS by using the following command.
# sync && echo 3 > /proc/sys/vm/drop_caches

The former part of the above command will commit buffer cache to disk, and the latter part will tell OS to drop buffer caches immediately.

There are three levels of dropping cache with corresponding numbers.
1 - Free pagecache
2 - Free dentries and inodes.
3 - Free pagecache, dentries, and inodes.

3. Use Direct I/O for POSIX


When you open a file with the flag O_DIRECT, you bypass the I/O buffers and therefore bypass the cache effects of cache at the operating system level. Almost device drivers support POSIX compatible API also support O_DIRECT flag, except of some parallel distributed file systems such as PanFS.

Benchmark tools such as IOR (-B), IOzone (-I), dd (direct) have flag for this feature. In programming, use the following example.

open(filename, O_DIRECT);

4. Clear Cache for a Specific File


If you want to clear cache for a specific file, you can use the following program clearcache.c to tell OS not to cache the file. It actually helps you clear cache of a list of files provided via program's arguments.

/* clearcache.c - mrcuongnv */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

int
clear_file_cache(filename)
     char *filename;
{
  int fd, rs;

  printf("%s", filename);
  if ((fd = open(filename, O_RDONLY)) != -1) {
    if ((rs = posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED)) == 0) {
      printf(" --> Cleared\n");
      return 0;
    }
  }
  printf(" --> %s (%d)\n", strerror(errno), errno);
  return 1;
}

int
main(argc, argv)
     int argc;
     char *argv[];
{
  if (argc < 2) {
    fprintf(stderr, "Syntax: %s FILES\n", argv[0]);
    return 1;
  }

  int i, rs = 0;
  for (i = 1; i < argc; i++) {
    rs += clear_file_cache(argv[i]);
  }

  return rs;
}

The most important part in the above code is posix_fadvise(), which tells OS that the specified data will not be accessed in the near future.

Thursday, July 19, 2012

Git: Ignore All Contents of a Directory but the Directory Itself

You sometime want to ignore all contents of a directory but the directory itself in a commitment, such as cache/ directory for example. It can both reduce the amount of data and temporary files to be stored in the repository.

In case of Git, all you need is to create a .gitignore file inside the cache/ directory with  the content as follows.

*
!.gitignore

The first line tells Git to ignore all contents, but the second line tells Git to keep the file .gitignore, and therefore, keeps it to be a non-empty directory.