Test na redundancję kodu

19 May 2011 10:25

Czas na nieco żartobliwy eksperyment…

Wpierw liczymy ile jest niepustych i nietrywialnych linii kodu, np.:

find katalog1 katalog2 -name '*.php' -exec cat '{}' ';' | sed 's/^\s\+//' | sed 's/\s\+$//' | grep -v '^$' | grep -v '^function\|^public\|^protected\|^private\|^class\|static\|^}\|^{\|^?>\|^<?php\|^//\|^\*\|^/\*' | wc

Potem liczymy ile jest unikalnych linii w tym co znaleźliśmy:

find katalog1 katalog2 -name '*.php' -exec cat '{}' ';' | sed 's/^\s\+//' | grep -v '^$' | grep -v '^function\|^public\|^protected\|^private\|^class\|static\|^}\|^{\|^?>\|^<?php\|^//\|^\*\|^/\*' | sort | uniq | wc

Dzielimy jedną liczbę przez drugą i uzyskujemy współczynnik redundancji kodu.

Należy dostosować kod do danego języka, aby lepiej matchować trywialne linie, które są potrzebne, ale przecież nie świadczą o redundancji kodu.

Comments: 1

Ubuntu 11.04 (Natty) on Sony Vaio SZ640

10 May 2011 16:49

As always, when a new Ubuntu release is done, I'm installing in onto my Sony Vaio SZ series laptop and experience some problems, that I later work on and finally fix.

To let others use this knowledge every half a year a note like this is written onto my blog.

Installation

You can read about installation LiveCD issues in a separate blog post. Let's concentrate on things specific to Sony Vaio (mostly the dual-graphics system we have from Sony).

Let's just say it's much better to install Ubuntu using the Intel graphics (the graphics-card switch in STAMINA position) card, since it's supported much much better by Linux.

Installing nVidia drivers

This should be easy, but is not due to a few glitches. Let's get through them step by step:

  1. Have the Ubuntu installed using Intel graphics card (STAMINA mode)
  2. Stop the laptop
  3. Put the graphics-mode switch to SPEED mode
  4. Start the laptop again
  5. Wait till you can log in
  6. Log in. You won't be able to use the Unity desktop, so classic Ubuntu desktop should be launched (if not, choose "Classic Ubuntu (no effects)" from drop-down list when logging in)
  7. Click System » Administration » Additional drivers
  8. Choose recommended driver and click enable. This will download and install the nVidia drivers for you.
  9. Don't reboot yet. You need to blacklist the nouveau driver (it's the open-source 3D nVidia driver attempt). Do this:
    echo blacklist nouveau | sudo tee /etc/modprobe.d/blacklist-nv.conf
  10. OK. Now you can reboot. The nvidia driver should kick in and you should have working Unity desktop

One more thing. Intel 3D drivers are now broken, so you won't have the Unity desktop working in there. Let's fix it!

Make 3D graphics (and Unity desktop) work in both Intel and nVidia

This is basically the same as explained in the previous post, but using separate script (so it's more elegant).

Create executable file /usr/local/bin/detect-gl :

sudo touch /usr/local/bin/detect-gl
sudo chmod +x /usr/local/bin/detect-gl

Edit it:

sudo gedit /usr/local/bin/detect-gl

Put the following into it:

#!/bin/bash

if ls -l /etc/alternatives/gl_conf | grep nvidia; then
    link=nvidia
else
    link=mesa
fi

if lspci | grep 'VGA compatible controller: nVidia Corporation'; then
    hw=nvidia
else
    hw=intel
fi

if [ "$link" = "nvidia" -a "$hw" = "intel" ]; then
    update-alternatives --set gl_conf /usr/lib/mesa/ld.so.conf
    ldconfig
    killall Xorg
fi

if [ "$link" = "mesa" -a "$hw" = "nvidia" ]; then
    update-alternatives --set gl_conf /usr/lib/nvidia-current/ld.so.conf
    ldconfig
    killall Xorg
fi

Call that script from /etc/rc.local (commands from this script are launched on each boot). Open file /etc/rc.local:

sudo gedit /etc/rc.local

before exit 0 line, put the following line:

/usr/local/bin/detect-gl

Your whole file should look more or less like this:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

/usr/local/bin/detect-gl

exit 0

This script will make sure the proper GL library is used for both graphics cards (Mesa lib for Intel, nVidia lib for nVidia card).

Fixing nVidia backlight issues

This is the most elegant solution I could think of. As the brightness settings almost work (when you press the Fn-F5, Fn-F6 keys, the notification appears and the bar shows you the backlight changes) we'll use D-Bus to listen for backlight change signal to set the backlight using nvclock command.

Install nvclock:

sudo apt-get install nvclock

Now create executable file:

sudo touch /usr/local/bin/nvidia-brightness-helper.py
sudo chmod +x /usr/local/bin/nvidia-brightness-helper.py

Edit it:

sudo gedit /usr/local/bin/nvidia-brightness-helper.py

Put the following into it:

#!/usr/bin/env python
 
import dbus, gobject
from subprocess import call
from dbus.mainloop.glib import DBusGMainLoop
 
def apply_brightness(new_brightness):
    if new_brightness < 15:
        new_brightness = 15
    call(['nvclock', '-S', str(new_brightness)])
 
# only launch when nVidia is present
if call(['bash',  '-c', 'lspci | grep "VGA compatible controller: nVidia Corporation" 1>/dev/null']) == 0:
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
    bus = dbus.SessionBus()
    bus.add_signal_receiver(apply_brightness,
                            dbus_interface="org.gnome.PowerManager.Backlight",
                            signal_name="BrightnessChanged")
 
    loop = gobject.MainLoop()
    loop.run()

Now open your Startup programs configuration dialog and add the script to things started when you log in.

Fixing suspend

I must confess I haven't check the suspend without doing this but suspecting it IS broken like in was in Maverick, I've blacklisted the TPM modules by putting:

blacklist tpm
blacklist tpm_bios
blacklist tpm_tis
blacklist tpm_infineon

into /etc/modprobe.d/blacklist-tpm.conf file.

Poor 3D performance

When running Unity all the 3D games have poor performance. I found the easiest workaround is to log in into Classic Ubuntu (with no effects) desktop, which gives you standard GNOME with no compiz and nice 3D performance for launched apps.

Dual-screen issues

I'm using a dual-screen setup (laptop's LCD + external monitor connected via VGA port). The current version of Ubuntu has multiple issues with this, especially when aligning the screens in vertical (laptop's screen being the bottom one, big LCD the top one). The "dock" or "launcher" if you wish appears on the wrong screen (the small laptop instead of on the big screen). To fix this do this:

xrandr --output VGA1 --primary

Log out and in again after doing this. Launcher is on top screen now :-).

I found another irritating bug: when you plug in a big external monitor into the VGA port, the desktop automatically expands to both monitor (not a bug yet), but on one or both of them, some area is black, so you can't see the menus and windows. Logging out and in is also a workaround for this.

When you log in with an external monitor connected, the desktop is strangely stretched and has some artifacts. Interestingly taping Super button (to show the Ubuntu Dash) and Escape (to hide it) refreshes the screen and it looks OK then.

It's hard to put some windows on the bottom screen (your mouse pointer stops on the bottom of the top screen while dragging). You can move the windows even further down using Alt and dragging by window content (and not the title bar).

Once a window is on the bottom screen, trying to resize it crashes the application and it disappears… The workaround is to resize by holding Alt key and drag with middle mouse button.

Other issues

When using Intel graphics card and Unity, Adobe Flash movies (YouTube for example) shows some artifacts. From time to time they also crash the whole X session (I'm not sure why).

OK, this would be it. Hope this helps someone.

Comments: 9

Ubuntu 11.04 final

05 May 2011 17:45

A few weeks after testing Ubuntu 11.04 beta 1 which brought me a bit of disappointment, I decided to test the recently released final version of this most popular Linux distribution.

As I had no more CDs or DVDs, I decided to try booting the distribution from USB drive that I assembled from microSDHC card reader and a microSD 4GB card that I got with my Android phone using standard Ubuntu tool called "Boot disk assistant", which burns CD ISO file onto a USB drive in a way that it becomes bootable equivalent of the CD with additional option to save the modifications you make to the Live system on the disk itself, so that it becomes more or less persistent.

LiveUSB creation

First I decided to use that option and let the program use about 3 GB for the overlay file. This made the program busy for about a half an hour, the card reader (actually writer) got warm, and the result was the USB was not that bootable.

A day after that attempt I decided to give it a try without the persistence and it worked this time. The problem came when I tried to "Try Ubuntu" (in opposite to "Install Ubuntu"). The graphic mode quited and I got nothing in return. I tried multiple things to do in text mode, rebooted a few times and after that it appeared that the graphic mode re-run after about a minute. The CPU was completely idle in this time as was the disk, so this wasn't reasonable thing.

Anyways it appeared the release was much more stable than the beta version I tested and it kind of even worked. I found a few concepts useful and cool, so I decided to put the system on my disk to give it a serious try.

Having it on disk

Automatically detecting an external monitor was the biggest positive surprise for me. This never worked in Ubuntu till now. The downside is the graphics got crazy and displayed desktop in not really usable chunks, but I moved some windows around and it got fixed.

nVidia troubles

Then I tried booting with nVidia graphics card enabled (I have a dual-graphics Sony Vaio laptop) to see if the drivers would automatically install. Ubuntu first booted into classic Ubuntu mode (no fancy Unity desktop shell) but it was usable. I navigated to System » Administration » Additional Drivers and chose the recommended proprietary nVidia drivers to install. They got installed and I was asked to reboot the computer so I did.

After the reboot graphic mode was not working and so didn't the text mode. I rebooted with Intel graphics card to discover that installing the nVidia drivers broke Intel drivers which made me without Unity desktop shell even on previously working Intel card. Since I knew what to do to "fix" nVidia I did this — I blacklisted the nouveau module and also a bunch of tpm modules that prevented from proper resume after suspend in previous version of Ubuntu. And rebooted with nVidia card chosen.

This time Xorg went up, the Unity showed up and everything was more or less OK. After removing nVidia drivers (apt-get remove nvidia-current) Intel got working Unity back. But I wanted to have Unity working on both graphics card without constant installing and uninstalling nVidia drivers and I figured out how to do it.

Automating Intel/nVidia boots

In /etc/rc.local before exit 0 I entered something like this:

if ls -l /etc/alternatives/gl_conf | grep nvidia; then
    link=nvidia
else
    link=mesa
fi

if lspci | grep 'VGA compatible controller: nVidia Corporation'; then
    hw=nvidia
else
    hw=intel
fi

if [ "$link" = "nvidia" -a "$hw" = "intel" ]; then
    update-alternatives --set gl_conf /usr/lib/mesa/ld.so.conf
    ldconfig
    killall Xorg
fi

if [ "$link" = "mesa" -a "$hw" = "nvidia" ]; then
    update-alternatives --set gl_conf /usr/lib/nvidia-current/ld.so.conf
    ldconfig
    killall Xorg
fi

This code checks whether current GL implementation is nVidia (valid for nVidia card) or Mesa (valid for Intel card), then checks current card by examining the output of lspci command and uses certain update-alternatives lines followed by ldconfig to update the GL implementations. killall Xorg kills the X server which is brought back by login manager with GL library updated.

This is a dirty hack but it works just well for me and made both graphic cards working with no additional work.

Glitches/Summary

Ubuntu seems very stable now, but X server crashed a few times (especially loading Flash videos from Vimeo in Firefox). There are some problems in multi-monitor configurations, like no way to move the window to bottom monitor (if they are aligned vertically) by dragging its top bar (moving with Alt works). The windows on bottom monitor are not maximized properly (they are maximized on the top monitor instead) and they can't be resized by dragging the resize handles (but resizing with Alt+middle mouse button works).

There were some other small random issues, either not really relevant or easily fixable, but overall it seems a very good beta release. But wait. This is no beta, but final release. In this case it's not good. It's OK for a Linux user like me, but it definitely should not be released for regular users. Shame on you Canonical. They promised they release in April, but they should have release another beta release, do a serious testing (what I did was anything but unusual), fix the remaining bugs and then release the final version.

Comments: 0

WRTU54G-TM running X-Wrt

20 Apr 2011 20:59

OK, so some time ago I bought a Linksys WRTU54G-TM. It's powered by Infineon ADM8668 processor (at 200 MHz) and has 64 MB RAM, which makes is pretty powerful router. Also it's really cheap. It looks like this:

2612942957_3197bdd443.jpg

The problem with this router is that there was no cool open source software for it. Linksys opened its sources, but I meant something like DD-WRT or OpenWRT. The problem is that the CPU architecture is different from for example the widely known and used WRT54G series.

Nonetheless there was some heavy work done to run a custom Linux firmware on it. After some successes, the author decided to port OpenWRT distribution to this router. And this is how it's done :-).

The OpenWRT is a command-line distribution of Linux for a bunch of routers. There is a web interface called X-Wrt which is installable as a opkg package to the OpenWRT systems, but there is no real information of how to install it on specifically WRTU54G-TM.

The good news is it can be done, the bad news is you have to hack it a little (but only very little). So let's go.

First, you need to "update" the software of the router to the OpenWRT version. The reference here is: http://wiki.scottn.us/wrtu54g-tm. Just log in to the router WWW interface, select Administration » Firmware Upgrade and choose this file wrtu54g-tm-openwrt-devel-b5.bin (download it from http://wrt.scottn.us/wrtu54g-tm-openwrt-devel-b5.bin).

This takes a while. Router then reboots. Give it a few minutes to load and install. After that the power LED keeps blinking which is probably a bug (in the official firmware, the LED blinks while the system is loading, but once it's up it lights still).

Connect to one of 4 Ethernet ports and you should be given an address IP in the 192.168.1.* network. If not, assign yourself one (for example 192.168.1.10). Then you can confirm the router is up, by pinging it:

ping 192.168.1.1

Trying to ssh to it will reveal you can't really do it, since you don't know the root password. This is tricky, but you need telnet to the router:

telnet 192.168.1.1

You'll be given the root shell. Set the password:

passwd

You'll need to enter the password twice for confirmation. Don't forget it :-).

Now you can ssh to the router:

ssh root@192.168.1.1

Deal with the RSA "host not known" warnings etc, and supply the password you just set.

If you connected the router to the network (by Ethernet cable to the "Internet" port) and your ISP has DHCP server, you should already have the Internet connectiong:

ping google.com

If you need to supply the static IP address, edit the following file:

/etc/config/network

You can use vim (busybox version) editor:

vim /etc/config/network

wan network corresponds to the Internet connection, while lan to the 4-port switch.

In this scenario it's possible you also have to supply the DNS servers, so instead of checking the Internet connectivity by pinging google.com, ping some IP address, like:

ping 8.8.8.8

Once you have the Internet connection, you can update the packages repositories, so you can install the X-Wrt interface:

mv /etc/opkg.conf /etc/original.opkg.conf
cat > /etc/opkg.conf

Enter the following:

src/gz adm8668 http://wrt.scottn.us/packages
src/gz snapshots http://downloads.openwrt.org/snapshots/trunk/brcm47xx/packages
dest root /
dest ram /tmp
lists_dir ext /var/opkg-lists
option overlay_root /overlay
arch all 5
arch brcm47xx 10
arch adm8668 20

… and press Ctrl-D. Your /etc/opkg.conf file should now contain the lines above. Update the repositories:

opkg update

Install the X-Wrt, which is packed in package named webif:

opkg install webif

Now if you point your web browser to http://192.168.1.1/ you should have a poor 404 page. It seems the permissions of the /www directory are wrong. I just did that to fix the problem:

chmod -R a+rX /www
/etc/init.d/uhttpd restart

Now you can log to the Web interface in with your login (root) and password.

You can now play with the router settings using the browser :-). Much simpler.

References:

http://wiki.scottn.us/wrtu54g-tm
http://wiki.openwrt.org/doc/start

Comments: 3

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License