Archive for November 12th, 2008

PART 3 IS INACCURATE, THE BELOW SCRIPT IS FOR REFERENCE ONLY, IT HAS BEEN REPLACED IN PART 4

In part 3, I am going to cover a bash function that will allow you to profile the memory usage of any application by name.

By adding the function below into your script you can execute a command such as: sysadmin appmem apache


function appmem {
if [ -z "$1" ]; then
echo “Usage: sysadmin appmem app_name i.e. (sysadmin appmem apache)”;
else
if [ -x '/usr/bin/pmap' ]; then
APID=(`ps aux | grep “$1″ | grep -v ‘grep’ | grep -v “$0″ | awk ‘{print $2}’`);
COUNT=0;
AMEM=0
for PID in ${APID[@]}
do
TMP=$((`pmap -x $PID | grep “total” | awk ‘{print $3}’`));
AMEM=$(($AMEM+$TMP));
COUNT=$(($COUNT+1));
done
AMEM=$(($AMEM/1024));
echo -e “$YELLOW —– MEMORY USAGE REPORT FOR ‘$1′ —– $CLEAR”;
echo “PID Count: $COUNT”;
echo “Mem usage: $AMEM MB”;
echo “Mem/PID: $(($AMEM/$COUNT)) MB”;
echo -e “$RED”
echo -e “For more information run: pmap -x $PID $CLEAR”;
else
echo ‘Could not execute /usr/bin/pmap … aborting’;
exit;
fi
fi
}

Sample output:


—– MEMORY USAGE REPORT FOR ‘apache’ —–
PID Count: 6
Mem usage: 1134 MB
Mem/PID: 189 MB

For more information run: pmap -x 123456

You can of course replace ‘apache’ with the application or daemon name you want to profile the memory usage of.

This script does require that pmap is installed, if the script can not find it, it will abort.

As always any problems, post a comment.

UPDATE: Apparently I need to point out that if you haven’t read PART 2! then the colored output will not work … That’s why this entry is titled part 3, it does assume a degree of competence on your part in realizing part’s 1 and 2 may just be required reading …

NOTE: The above provides a complete memory footprint of the indvidual PID, the same as VIRT in top.

VIRT — Virtual Image (kb)
* The total amount of virtual memory used by the task. It includes all code, data and shared libraries plus pages that have been swapped out.
* VIRT = SWAP + RES

Tags: , ,

Comments 2 Comments »

The problem that most people face when setting up a UTF-8 database in mySQL is that without calling ‘SET NAMES’ in the mySQL client prior to issuing any queries (PHP, C++ etc …) that the client connection will actually in most cases default to  latin-1.

However as of mySQL 5.x or higher you can issue a statement in the my.cnf file calling init_connect.

This will trigger a series of defined commands / queries every time a non super user connects (So if you are using root to connect to your mySQL database, stop reading now and slap yourself HARD).

i.e.

[mysqld]
init_connect=’SET collation_connection = utf8_general_ci’
init_connect=’SET NAMES utf8′
default-character-set=utf8
character-set-server=utf8
collation-server=utf8_general_ci

Restart mySQL and check the mysqld.log has not returned any errors (Or your event viewer if you are using windows).

Every client connection will now default to utf-8 encoding and not latin-1, removing the need to add a SET NAMES call on every connection.

This will work for PHP, C++, ruby etc… as the client encoding is now handeled server side, rather that waiting on the client to issue a SET NAMES command.

Tags: , , , , ,

Comments 1 Comment »