Thursday, May 30, 2013

Tweaking tcp port - Improving performance of network intensive applications over high latency network

Building an application over a low latency network could give you very good performance, but sometimes over a high latency network, read() or write() system calls could be a cause of bottleneck for the applications performance.

Consider a network intensive application running on server A. This server continuously generates log and sends it over the network to another server B . Now, server B may be slow (low computation power) or the network may have high error/ latency, due to which the write buffer of A gets saturated. As a result server A stalls (write buffer of A is filled, but B is not ready to read the data yet).

By default in the linux kernel, each tcp port is configured with around 64-80KB read/write buffer.

This can be seen using the command :

$sudo su
#sysctl -a | grep net | grep mem

...
net.ipv4.tcp_mem = 93132        124179  186264
net.ipv4.tcp_wmem = 4096        16384   3973728
net.ipv4.tcp_rmem = 4096        87380   3973728
...

The numbers represent the minumum, default and maximum memory allocated to every tcp port  that is created on a machine.

For solving our current problem, all we need to do is increase the default port size to a larger value. Increasing the memory associated with a port can be done as follows:

#sysctl -w net.ipv4.tcp_rmem='4096        3000000   3973728'
#sysctl -w net.ipv4.tcp_rmem='4096        3000000   3973728'

NOTE: This change gets reflected on every port that is created on the server. Hence, one should be careful not to increase the default port size to a very large value, as each new connection would result in higher memory usage.





 

Thursday, May 2, 2013

Templates in C [Macros!]

Today I faced this problem of writing a generalized function in C that would accept a "configurable" type of argument. i.e. the function should be able to accept any kind of argument that I pass to it, and it should be able to do something with it (say print the value of the argument passed).

What I wanted could easily be done in C++ using TEMPLATES; create a function with a signature

void foo(class Template <T> x){
     cout << "x" << "\n";
     return ;
}

This cannot be done in C, as it is a middle level language (yes I said it! if you do not agree, go learn C++ STL :P ).

Now, there is NO (absolutely NO) way you can do function/operator overloading in C, hence we resort to finding workarounds... say what? macros biaatch!


MACROS - a coders nemesis -

Why:
1. Its difficult to write  a macro.
2. Its even more difficult to read a program with macros, you practically end up expanding the whole macros in your notebook / as comments in your code.
3. CSCOPE /CTAGS dont work on MACROS. you end up using grep instead to find out the exact place where the macro gets expanded.


Then Why do we still use it:
1. Makes ur code compact.
2. 1st reason is enough.

Without further ado, i present to you using macros to compactify (i just made that word up) your code.

consider the program :

int main(){
     callPrint_int(1);
     callPrint_float(4.2);
     callPrint_char('b');
     return 0;
} 

1. Program without a macro:

callPrint_int(int x){
     cout << x;
}


callPrint_float(float x){
     cout << x;
} 

callPrint_char(char x){
    cout << x;
}

2. Program with a macro: 

#define callPRINT(T) \
    void callPRINT_##T(T x) { cout << x ; }

callPRINT(int);
callPRINT(float);  
callPRINT(char);
  
each of the above three lines give you basically functions in part 1. but look at how compact your code becomes. isn't she a beauty :P.










  


Saturday, January 5, 2013

Ubuntu Cropping Videos using VLC [Notes]

go to command prompt.

Start vlc.

Check the box  View -> Advanced Controls.

A new pannel appears below the vlc video.

Click on the red dot. Record starts.

Click on the Red dot again. Record stops.

Where is the video saved?

dont know. But you can specify the path using the command line option

vlc --input-record-path=/home/shehbaz/videos/ sample_video.mp4


Friday, December 7, 2012

Shell scripting - sleep for milliseconds

Ever tried to make your shell script sleep for milliseconds? I tried, and failed miserably in doing so. I didnt know that decimal numbers are not allowed after the sleep command.

I found the following command that could be used for "virtually simulating" the millisecond sleep command:

typeset -i count=0;
while [[ ${count} -le 5700 ]];
do
      count=${count}+1
done

decrease the value 5700 to a value which would suit your needs. 5700 gives you ~1 sec time.


Tuesday, November 20, 2012

Setting up LAMP on Ubuntu 12.04

LAMP - Linux Apache MySQL PhP.

Most of the stuff mentioned here is inspired from :: https://help.ubuntu.com/community/ApacheMySQLPHP

first to install apache use the following:

1> sudo apt-get install apache2

restart/start apache2 services using the following command:

2>  sudo /etc/init.d/apache2 restart

This will restart apache services.

Check whether the apache2 services have started again by visiting http://localhost. You should get the message "It works!" on your browser. If it actually works, move to point 3. else:

reasons:

a. check /var/log/apache2/error.log.** where ** is the most recent(largest) value. you will get some kind of error message. Though it is self explanatory (eg. could not access some file) which means that there is a mode problem (you have to make /var/www/index.html 744).

b. error log is empty - problem with browser caching - try removing recent content (if you have tried logging into localhost many times, browser caches your page). To remove this clear your browser cache.

3> "It works!" page is located at /var/www/index.html. This is the default root hosting directory for linux. for multiple sites or your own website, you do not want to clutter your /var/www directory. Therefore it is suggested you change your default browsing directory.

How do I do that you say?

for every website , apache creates a config file in the directory
/etc/apache2/sites-available/

with the name of the website. Opening the configuration file, you will notice a xml file like this:



Saturday, September 8, 2012

Ubuntu 12.04LTS Bug - Close, minimize and Maximize button becomes invisible in GNOME

Recently, I faced this issue of application window not showing the top most bar - containing close, minimize and maximize button.

I was using Ubuntu 12.04 LTS with GNOME Desktop Theme. As usual the problem was with ccsm (CompizConfig Setup Manager).

All I had to do was to go to Application -> Settings ->Compizconfig Setup manager (just type ccsm on the shell.)

once that is done, unmark (if already marked) and then mark again the Effects -> Window manager tab.



Try opening a shell. I hope this time you see the close minimize and maximize buttons.

==Don==

Wednesday, September 5, 2012

DPRINTF Debug Macro in C

Extremely useful for Debugging projects in C.


#define DEBUG   // comment if you do not want the debug statments to appear.

#ifdef DEBUG
#define DPRINTF(fmt, ...) \
    do { printf("my_file: " fmt, ## __VA_ARGS__); } while (0)
#else
#define DPRINTF(fmt, ...) \
    do { } while (0)
#endif




Usage -> DPRINTF("This is a debug statement");

if you want the debug statements to appear, then define DEBUG
#define DPRINTF
at start of the code.

if, on the other hand, you do not wish the debug statements to appear, comment it.

you can add additional information to your debug statement, for example -

DPRINTF("%p:%d Debug statement\n",__func__,__LINE__);

this gives both the function name and the line number of the DPRINTF statement along with the message "Debug statement".