Gprof2dot

2009/12/06

Profiling

I want to share with you a nice trick which, even if not fundamental, is still very useful.
You can also use this to impress your girlfriend.

Sometimes profiling is necessary to understand which part of our code are taking so long.
Python offer different alternatives, I advice you to read the official python documentation to get an idea.

Gprof2Dot

Another very nice thing is gprof2dot .
This little program takes into input profiling data in many different formats and converts it to a dot file, keeping all the valuable informations.

As you can see for example here .
You also need to have graphviz to be able to actually generate the final graph.

Scripting and emacs integration

Given that I want to automate it and I want to use it also from emacs I wrote a simple bash script:

#!/bin/bash
FNAME=$1
if [ ! -f $FNAME ]
then
    echo "file $FNAME not found, executing from $PWD"
    exit 1
fi
# discard filename
shift

# change those variables as you prefer
STATS="output.pstats"
TYPE=png
OUT=output.$TYPE
OPEN="open"

# $@ collect all the arguments for the python script
# it doens't contain the file name after the shift
python -m cProfile -o $STATS $FNAME $@
gprof2dot.py -f pstats $STATS | dot -T$TYPE -o $OUT
rm $STATS
$OPEN $OUT

You can find this script also in my emacs configuration .

Now we’re almost done, just put this somewhere in the emacs exec-path and you’re ready to use it!
Just press Meta-! on a python buffer and launch “gprofile.sh python_file.py [extra args..]“

To automate even more this little elisp functions does that for you:

(defun gprof ()
  (interactive)
  (shell-command (concat "gprofile.sh " buffer-file-name)))

But at the moment it doesn’t take arguments for the script (will fix it soon).


elisp fun

2009/12/02

Problem

As we all know programmers are lazy, repetition is the evil, we want to type as fast as possible.
Using C-like languages you always need to put the comma in the end.

Normally after the comma you also want to go to newline and indent, so why not put together those two things and automate it?

If you also like yasnippet then you’ll find this feature even nicer.
I took from this feature textmate , a wonderful text editor which comes with this setting by default.
As we’ll see we don’t need many lines of elisp code to end up with a much more powerful and flexible version of this nice feature.

Solution

newline-force

We first define a first function newline-force
Here it is:

(defun newline-force()
  "Goes to newline leaving untouched the rest of the line"
  (interactive)
  (progn
    (end-of-line)
    (newline-and-indent)))

This function goes to end of line and calls the predefined function newline-and-indent, which goes to newline and indent according to mode
In more detail:

  • interactive: function is available for user
  • progn: executes a sequence of disjointed operations

Read the rest of this entry »


Github behind a proxy

2009/04/10

Suppose you have an account to github and you want to use it even when you are behind a very restrictive proxy.
The problem arises “only” when you have to push new patches, given that you can clone/pull even using http out of the box.

Luckily the github guys give us the access to another server (ssh.github.com) which basically listen on the port 443 and forwards

The informations to set up your environment can be found here:

github over proxy

And here you find:
corkscrew

This for example is my configuration (on Macosx but on linux is the same):

SSH_CONFIG

Host gitproxy
  User git
  HostName ssh.github.com
  Port 443
  ProxyCommand corkscrew <proxy> <port> %h %p
  IdentityFile /Users/andrea/.ssh/github

Now in your git repository you can simply add the new remote

git remote add git@gitproxy:<repository>

Another nice way to bypass the proxy (thanks Christian) is to use the GIT_PROXY_COMMAND variable, in this way

export GIT_PROXY_COMMAND=/usr/local/bin/proxy-cmd.sh

#!/bin/bash
(echo "CONNECT $1:$2 HTTP/1.0"; echo; cat ) | socket proxy.unitn.it 3128 | (read a; read a; cat )

And now git for every push/pull will execute that script and bypass the proxy.


Auto ssh over a set of machines

2009/03/19

I just found out that every pc in the lab have sshd running and waiting at port 22.
Now I immediately thought some nice and nasty things like:

  • running the same process everywhere
  • do some funny jokes to the users connected
  • etc etc

    Or simply get a shell with all the laboratory software you need automatically.

    If you also have on those machines a listening xorg then you could also use X11 programs over the ssh, which works like a charm. (ssh -Yc “hostname” “command” for example)

    The only two “problems” are:

  • the need to insert the password every time
  • the need to accept the fingerprint for each new pc
  • Let’s see how to solve them.

    First we generate a new key:

    ssh-keygen -t dsa -f $HOME/.ssh/unilab

    Then we upload the public key to one of the pc’s, as long as the home directory is mounted over the network (of course) it doesn’t matter which one.

     scp $HOME/.ssh/unilab.pub utente@host:.ssh/ 

    Last thing you need to add to the authorized keys your public key (on whatever lab pc you’re logged in).

     cat $HOME/.ssh/unilab.pub &gt;&gt; $HOME/.ssh/authorized_keys 

    Now we can set a really nice configuration in our $HOME/.ssh/config.

    Here it is:

    Host a10?pc*
      User <youruser>
      IdentityFile <your private key>
      Port 22
      StrictHostKeyChecking no
    

    And that’s it, if for example you now do

     ssh a105pc08 

    ssh will find the matching on the regular expression and use those settings, letting you login automatically.

    But we’re not over, how do we know where sshd is up and running?

    Read the rest of this entry »


    Find that pc

    2009/01/27

    Assume that you are in a big network where the ip addresses are given via dhcp.
    Assume also that you want to be able to communicate with the same pc which can change the ip address every time it reboots.

    One easy and “legal” way to always find is using netbios, a protocol that makes a computer shout over the network it’s pairing Name -> IP address.

    For windows and macosx this works out of the box, for linux/bsd machines you can just set a minimal smb.conf like this.

    [global]
    	workgroup = WORKGROUP
    	server string = My Samba Server
    	netbios name = koala
    

    It’s useful to have the correct pairing right in /etc/hosts for every kind of command line program that doesn’t speak netbios.
    So we’ll use nmblookup and sed for the task.
    Just substitute the netbios name that you need here:

    export koala=$(nmblookup koala | sed -n 2p | cut -d ' ' -f 1)
    # Now I can substitute the right ip in /etc/hosts
    sed "/koala/ s/.*/$koala\tkoala" < /etc/hosts > /tmp/hosts
    sudo mv /etc/hosts /etc/hosts.bak
    sudo mv /tmp/hosts /etc/hosts
    

    Maybe you also want to modify sudoers to use it without inserting a password each time.


    Automating the interaction

    2009/01/22

    A pretty nice feature of shell scripting is “<<" operator.
    It allows basically to launch interactive commands within a bash script.

    For example I wanted to add a small but very useful function to textmate, I want to be able to know all the possible informations about a whatever function or data type.

    The default info function in the haskell bundle unfortunately is only able to retrieve informations about prelude functions or functions used with the full path.
    (eg Data.List.lookup and not simply lookup ).

    Using ghci I should load the file (which automatically loads also the needed libraries) and type :info.
    This is really easily done using “<<".

    I added a command “info about” with the following code in my haskell bundle and now if the file is correct (it compiles) you are sure that you’ll get the informations about the function you’re looking for.

    GHCI << EOF
    :load $TM_FILEPATH
    :info ${TM_SELECTED_TEXT:-$TM_CURRENT_WORD}
    EOF
    

    I also think that there should be a better way to communicate with ghci (and using System.Enviroment to get the arguments) but I didn’t find any, this solution even if not really elegant works pretty well.

    This “approach” can be used also with many interpreters, sometimes I find it really useful.


    Life behing a proxy

    2009/01/19

    Staying behind a proxy can be very annoying, with macosx it’s nice and easy to set a global proxy.
    Unfortunately command line programs don’t care about those settings, so we have to manually set them every time.

    With the script you’ll find below we set globally the proxy for ftp, http, rsync and subversion:

    Read the rest of this entry »


    Follow

    Get every new post delivered to your Inbox.