Awasihba’s Log

December 23, 2008

What is load average ?

Filed under: sysadmin, unix — Tags: , — abhisawa @ 5:43 pm

Always question what exactly those three numbers indicates when you give any unix command like top, uptime. From where those numbers came in existance.

Few people said that its load on system but never told which part of system is loaded.

After reading three part series about load average triplets by Dr. Neil Gunther came to know the exact formula for load avarage calulation

Its relationship between active unix processes to process run queue length of the system.

q(t) = A q(t-1) + ( 1 – A ) n(t)

over here at time ‘t’ system took reading of process’s run queue length q(t) and number of active process on system n(t), which got arranged with previous reading of process’s run queue length q(t-1)

In above equation constant A = exp(-5/60R)

where R is 5, 10 and 15 which creates desired triplet of load average for respective minutes. Above equation is applicable for linux kernel.

Whole equation is nicely explained by Dr. Neil Gunther in above article.

 

 

December 18, 2008

My own bashrc quickhelp

Filed under: programming — Tags: — abhisawa @ 3:58 pm

Many times I write so many functions in my bashrc for quick administration task; but after a while I forget about them and over long run I stop using them because I hardly remember that I did write something like that. Here is function in bashrc which gives you quick overview of all the functions with help of comments.

$HOME/myfunctions

function myhelp {
#This prints help over here
FILE="$HOME/myfunctions"
grep -A 1 ^function $FILE | sed -e '  /^--/d ; /^function/ { s/^function//g; s/{//g;   N;   s/n#(.*)/ - 1/; } '
}

function py {
#Ping yahoo.com
  ping www.yahoo.com
}

function debugAE {
#Start debugging apple events on console
        if [ "$1" == "on" ]
        then
                export AEDebugSends=1
                export AEDebugReceives=1
        else
                export AEDebugSends=0
                export AEDebugReceives=0
        fi
}

In above code , you can modify FILE variable in myhelp() to specify your function file ( over here it is $HOME/myfunctions ). Source the same function file in ~/.bashrc or ~/.profile .

For quick help you type myhelp on prompt , you should get output as follows.



Output

myhelp – This prints help over here
py – Ping yahoo.com
debugAE – Start debugging apple events on console

November 18, 2008

Ubuntu “intrepid” makes wifi non-functional.

Filed under: Uncategorized — Tags: — abhisawa @ 5:23 pm

Today I upgraded my Ubuntu to latest release 8.10 ( aka intrepid ) and suddenly my wifi network stopped getting ip via DHCP. Google told me about the bug in network-manager which could have done evil work with my laptop. But if you are using wpa_supplicant with your own configuration file then you are in luck.
Just add following lines in /etc/network/interfaces

auto wlan0
iface wlan0 inet dhcp
pre-up wpa_supplicant -iwlan0 -c /etc/wpa_supplicant/home_wpa.conf -d -f /var/log/wpa_supplicant.log -B
down killall wpa_supplicant

don’t pass -W parameter to wpa_supplicant or it will fail to get IP from dhcp.

Post about Ubuntu network-manager bug http://journal.dedasys.com/2008/11/13/ubuntu-intrepid-regression-beware-of-wireless-and-wpa

October 29, 2008

Create Proxy function in perl

Filed under: programming — Tags: — abhisawa @ 6:38 am


Almost every time when you get handover of someone else code, before you crawl thorough all its hairy functions and figure out to which database tables they are scratching, your manager comes and ask for some enhancements in it with stringent deadlines. If your functions are working like blackbox, means for particular set of inputs always returns fixed type of outputs then here is way to extend functionality of any function in perl.

I will use this kind of functionality to maintain my own cache on top of someone else code to make code run faster. All this possible because of access to symbol tables in perl. So symbol table is hash like structure which holds all the the typeglobs in that particular namespace, and typeglob is key and value pair structure with limited set of keys like SCALAR, ARRAY, HASH, CODE,GLOB,FORMAT,IO where each key have value as reference of data for particular variable type. Its very short definition of symbol table and typeglob, to understand it better ask for help from your friend called internet.

All subroutines in program are global that means you can always find their reference in symbol table that means you can not define subroutine with ‘my’ ( actually you can attach ‘my’ variable to anonymous subroutine reference , but we are not going to consider that over here ). The trick over here is about pulling out reference of original function from symbol table and wrap it under your customized function. Then update symbol table with reference of your wrapper function which internally calls original function with its magical overheads.

Take a look at following example code, in this exercise we are going to steal reference of subroutine named my_function() which prints one statement with all input arguments. On line 6 we are stealing and replacing my_function() with help of subroutine called Proxy() which is imported from package named PROXY.pm.



example.pl

#!/usr/bin/perl
#
use strict;
use PROXY;

Proxy 'my_function';

sub my_function {
  my @input = @_;
  print "This is original my_function and inputs are ";
  print "@_\n";
}

&my_function ("one two three");

So subroutine Proxy() takes subroutine name as an input argument, it generates full name of that subroutine with namespace part attached on line 11 in following code. Once it gets to that it steals reference of that particular subroutine from symbol table on line 14 . On line 16 with help of selective aliasing it replace that subroutine reference with another reference.

Now new reference is pointing to anonymous subroutine which returns a closure having subroutine named Stub() getting feed with original function’s reference ( i.e. my_function() reference ) and its input arguments. Stub() should be designed to do whatever you want to do prior the execution of original subroutine. On line 24 it prints extra line and then calls my_function() along with its arguments.



PROXY.pm

package PROXY;
use strict;
use Exporter;
our @ISA    = qw(Exporter);
our @EXPORT = qw( Proxy );

sub Proxy {
  my $function_to_steal = shift;

  my $who_called = caller;
  my $function_name =
    $who_called . '::' . $function_to_steal;
  no strict;
  my $function_referance = *{$function_name}{CODE};

  *{$function_name} =
    sub { &Stub($function_referance,@_); };
}

sub Stub {
  my $original_function = shift;
  my @input = @_; 

  print "This is originating from stub function\n";
  $original_function->( @input );
}

In the output when you called &my_function(“one two three”) in example.pl, it will following two lines instead of only one line.


Output

This is originating from stub function
This is original function and inputs are one two three

You can enhance Stub() for your own devious purpose like I mentioned to maintain local cache of some expensive database queries.

reference
Above code is very basic version of Memoize.pm by Mark-Jason Dominus

October 13, 2008

UDP on freebsd

Filed under: freebsd, udp — Tags: , — abhisawa @ 1:32 pm

My recent encounter with UDP packet drop on freebsd boxes gave birth to this post. Here what I learn from that skirmish.

Why UDP needs tune up as compare to TCP ?
Operating system automatically assign TCP buffer space on based on different factors, and TCP sending kernel can monitor buffer flow adjust sending size of data with help of window scaling. These kind of facility is not availble for UDP.

Buffer space for any UDP connection on freebsd is affected by following parameters

net.inet.udp.recvspace – When you open any UDP socket this parameters decides default receiving buffer space for userland data for that socket . You can override that size with help of setsockopt in your code.

kern.ipc.maxsockbuf – The buffer space for whole socket is determine by this parameter. So if you try to open socket with large send and receive buffer and you get error like ‘no buffer space availble’ then you should consider tweaking kern.ipc.maxsockbuf. Sometime you see frequent UDP drops while dealing with large number of tiny UDP packets. Even if your recvspace buffer is not filled up completely , still you will drop the packets. After digging around for a while we figured out that it was happening because we were hitting another hard limit of sockbuf->sb_mbmax, it specifies maximum number of mbufs allocated for each socket. You can increase that limit by increasing kern.ipc.maxsockbuf . You need to restart related services to get advantage of increased

kern.ipc.nmbcluster – this parameter governs ultimate amount memory you have to allocate all sockets opened on that system. This number tells how many number of mbuf cluster should be allocated. Usually each cluster is of 2k size. For example , if you are planning to open 1000 sockets with each having 8k sending and 8k size receving buffer . In this case each socket will need 16k of memory and in total you will need 16M (16k x 1000 ) of memory to handle all 1000 connections.

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.