Tuesday, September 9, 2014

Corner cases of Ubuntu 14.04 on Lenovo ThinkPad x201

I've been using Ubuntu 14.04 for a couple months now.


1. CPU got stucked on 1.2GhZ
Recently I installed a handy utility called indicator-cpufreq, which gives a system status bar icon indicating the currently used CPU governor and the current cpu frequency.  I noticed that my CPU always got stucked on 1.2Ghz, no matter what I did. I have tried modifying it manually by echo-ing the sysfs node and using cpufreq-utils, without much luck. I then came across this page:

http://askubuntu.com/questions/340626/permanently-change-maximum-cpu-frequency
This page explains that there is a CPU bios_limit that if set wrongly, may prevent the CPU from going above the frequency threshold that's set there.

cat /sys/devices/system/cpu/cpu0/cpufreq/bios_limit

Turned out it really was the culprit!! For some weird reason it got set to 1.2GHZ!!

This is how you worked around it:

echo 1 > /sys/module/processor/parameters/ignore_ppc

To make workaround permanent, we can edit the kernel command line argument, just like what's explained on the same page:

Modify /etc/default/grub to be like this:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash processor.ignore_ppc=1"

Everything works like a charm afterwards!

I then installed cpufreqd, which is a daemon to customize CPU frequency based on the laptop's realtime battery status (e.g. power level, plug/unplugged, etc). Now my docked x201 is running buttery smooth at 2.67 GHZ :)

I want to check out how long will it take to build AOSP on this laptop now :)

2. Intermittent shutdown due to overheating
Another problem I was facing is that after a couple hours of running docked, my laptop would shut itself off! When this happened, I undocked my laptop and noticed that it's abnormally hot!
I then realized that x201's airflow is located on its bottom, which got blocked by the dock. (sigh.. guess the dock isn't poorly designed.... :( )

Don't fret out though! There's solution!
I found this handy tool to control the fan speed of the laptop's.

http://www.thinkwiki.org/wiki/How_to_control_fan_speed

Basically, x201's fan can be controlled by this node:
/proc/acpi/ibm/fan
There are several levels that can be set, which corresponds to the RPM of the fan.

With this in mind, thinkfan can be used to automate the RPM of the fan depending on the reading of the thermal sensors. This page kindly describes how to set it up. (including how to run thinkfan as a daemon, custom setup for x201, etc)

http://staff.science.uva.nl/~kholshei/thinkfan_guide/

Edit (10/06/2015):
The following steps are how I did it:
1. sudo apt-get install thinkfan
2. sudo echo "options thinkpad_acpi fan_control=1" > /etc/modprobe.d/thinkpad_acpi.conf
3. sudo vim /etc/default/thinkfan, change it to be the following (this is to load thinkfan on boot):
START=yes
4. sudo vim /etc/thinkfan.conf, change it to be the following (this is fan vs. sensor-reading):

# The way the config works is that it picks the highest value among all sysfs nodessensor /sys/devices/virtual/hwmon/hwmon0/temp1_input
sensor /sys/devices/platform/coretemp.0/hwmon/hwmon2/temp4_input
sensor /sys/devices/platform/coretemp.0/hwmon/hwmon2/temp2_input
#sensor /proc/acpi/ibm/thermal

(0, 0, 47)
(1, 46, 50)
(2, 49, 53)
(3, 52, 56)
(4, 55, 59)
(5, 58, 62)
(6, 61, 65)
(7, 64, 75)
#127 is for disengaged
(127, 74, 32767)

And for debugging:
1. To see how a new config file works:
sudo thinkfan -n
2. To check current temperature:
cat /sys/devices/virtual/hwmon/hwmon0/temp1_input && cat /sys/devices/platform/coretemp.0/hwmon/hwmon2/temp4_input && cat /sys/devices/platform/coretemp.0/hwmon/hwmon2/temp2_input
'or'
cat /proc/acpi/ibm/thermal (in ubuntu 14.04, only shows one temperature)
3. To check fan level:
cat /proc/acpi/ibm/fan
(level shouldn't be 'auto' if thinkfan is really running)
4. ps aux | grep thinkfan <- you should see the process running


Wednesday, January 29, 2014

Android Binder, AIDL, Service, and API

Android Binder is an IPC mechanism used in Android. Binder can be accessed through the native code (C++) and also through Java code.

Here are some places where Binder are used:
1. When you use system service through mContext.getSystemService(...)
2. When you start another application through Intent.
3. When you're implementing a public service that uses AIDL file (this is pretty obvious)
4. When you're implementing interacting through different services using Messenger class

To me, what's the most interesting is that Android abstracts it so well such that I didn't realize I've been doing IPC calls so many times! In fact, I began my first Android programming without having any idea what an IPC was.

For the work I've been doing, I need to implement a piece of code that talks to the driver layer. Since I don't want to re-write the tedious JNI code multiple times (in case of it's needed somewhere else), I decided to create a public Service using AIDL convention. This service is the one that interacts with the native layer, and will then be exposed as an API in the SDK.
After that, it will essentially be wrapped one more time, such that user wouldn't have to actually interact with the service directly. Sounds like I'm over-complicating it, huh? Not really.. This is exactly how InputManager is working under the hood. When you do  mContext.getSystemService(INPUT_MANAGER), under the hood, InputManager class is interacting with InputManagerService. On the whole system, there's only a single instance of InputManagerService. This service is interacting with some native piece of codes that are tightly coupled to a lower layer. This way, as an application developer, we can all simply use the handy interface exposed through InputManager :)

Friday, May 3, 2013

Running MatLab on command-line mode. What goods does it have?

For those who are wondering about how to run MatLab on command-line mode, here it is:
./matlab -nojvm -nodisplay -nosplash -r "command"

I am new user to MatLab. Basically I was following some instruction for running MatLab code. I ran into a situation where I had to run a compile.m script that invokes gcc, mex, or any other MatLab's C compiler. When I ran the script through GUI version of MatLab, I kept getting errors that wasn't very helpful. Turned out that the same script ran without any problem on the MatLab ran on command-line mode. So yeah, give it a shot!

Oh yeah,
To ease thing out I also created alias on my .bash_rc as follow:
alias matlab_cmd="matlab -nojvm -nodisplay -nosplash -r "

I then can run the script as follow:
matlab_cmd "command"

Thursday, May 2, 2013

Using windows-shared printers on Linux

With Ubuntu 13.04 (possibly also with any of the lower versions I guess), having Linux to use printers shared for Windows is never easier. Just go to your Printers preferences, add a new printer with SMB sharing protocol. Finally type in your printer's IP address there. (eg. for my school's one it's like smb://crux.cs.washington.edu/ps022) and Voila! It lists you all the printer drivers available. Pick one (or pick Generic, if it is not listed) and things should work right away.

One caveat is that if you're printer is of a specified DOMAIN, put it as part of the user name. For example, my school's undergraduate Windows domain (or I guess it's called Workgroup?) is CSEPCLAB, so I'd put CSEPCLAB\username as my username when connecting to the printer through SMB protocol. It's a bit hacky to do the same thing with Mac. But I believe I have a post for it somewhere in this blog.

OSX, Windows, Ubuntu?

I had been a long-time user of OSX for quite sometimes. (+- 4 years) I found its UNIX environment and super-friendly user interfaces to be very useful for especially programming. Macbook's trackpad and keyboard shortcuts have always been my personal favorite as well.
I've always thought that MacBook's so called over-price is actually worth every penny. It has SMS, light sensor, very decent LCD, etc etc.
I still have lots and lots of positive things I'd want to write about MacBook. Software like SizeUp, TextMate, SMC Fan Control, iTerm 2, etc have been easing my life as a programmer so much. Maybe I'll write some articles about how much I loved OSX sometimes.

However, as I am becoming more more into UNIX environment, I've found OSX to not be able to catch up with my needs. First and foremost is because many software applications simply aren't written to work well with OSX! It's inevitable that the main market consumer is still Windows and so many software industry simply writes crappy port of their software for OSX. What I mean by crappy here is lots of memory leaks, not optimized to work efficiently and so on. Sad to say this, but I have to admit that I found my MacBook 5,1 with 8GB of RAM and 2.1 core-2-duo processor to be considerably sluggish compared to a laptop with a similar spec with Windows on it. A very obvious example would be Flash player. Try running a Youtube video on a 5,1 MacBook. Notice how hot your MacBook is? I can't help but to also notice how sluggish it becomes. It's just very annoying sometimes..

On the other hand, since some Linux are open source, the software support is so amazing. For almost every single application that you're searching for, you'd almost always able to find a decent open-source version that substitutes it. It's just amazing to see how much Linux have been growing. Also the driver supports that let things work out-of-the-box is just so sweet... I guess I'll have to write another post dedicated for Linux at some point.

:D
Feel free to start a discussion within this thread. I'm no big fan of one single OS or biased towards any. I have so much positive things to say about Linux, Windows, or OSX. They're just great and bad in their own ways.

Conveniences from cloud-based services

It's 2013 and I just realized how much conveniences I've been taking for granted from cloud-based services. With ever growing power of Internet, we no longer need to worry about forgetting to bring a USB stick drive or an external hard-drive anymore!

Long story short here are the list of apps that has been very useful to ease my life:
1. Google Chrome: Allows bookmarks, passwords, extensions, and tab synchronizations among different devices (PC, Mac, Linux, or even Android!)
2. Gmail: As an Android user, I found it to be very useful that the phone application is very well synced with the web-based interface. All the emails you send, starred, or labeled can be accessed easily from either web or phone interface.
3. Evernote: Note-taking application that works across platforms.
4. Dropbox: Share folders among computers, tablets, etc. It also creates snapshots for updates that are done to the files. One time Dropbox's backup saved me from losing my school programming work.
Dropbox for Android is also very as it allows automatic synchronization of the pictures you take from your phones. Never again scare to lose your pictures.
5. Google Calendar: Peeking at other's schedule, finding free timeslots for meeting among different people, inviting people to a meeting, or simply a reminder that has a pop-up/email notifications through web or Android device. What else can we ask? It's just getting better and better!
6. Google Drive/Microsoft Skydrive: Microsoft Word as a must for each of your computers? Nah! Why bother with hassles of having to carry USB stick around. Just edit, real-time collaborate, and backup your word docs and spreadsheet online! I recently starting to use Microsoft Skydrive because it mimics Microsoft Word even closer than Google Drive.
7. Google Voice: Ever needing to text someone/group of people, looking/adding/editing your phone contacts through computer, do an international calling for a cheap price, or even a local calling/texting for free? Better try voice.google.com

Life is just so much easier with all these services.

Wednesday, December 14, 2011

Network printing in Ubuntu 11.10

When you try to add network printers via System Tools -> Setting -> Printers, there's an error mentioning something about FirewallD being not activated. To workaround this, open terminal and type 'system-config-printer' this would open an alternative printer configurator that works!

Enjoy printing with your Ubuntu!