Thursday, December 18, 2008

Using procfs for debug information

In a kernel module we can dynamically create entries under /proc . Each entry (file or directory) under /proc is represented in kernel by a struct proc_dir_entry. This structure is dynamically allocated by various functions used to create directory entries. Here are some useful examples :-

To create a directory "home" under /proc

struct proc_dir_entry *home = proc_mkdir("home", NULL);

To create directory "kitchen" under /proc/home

struct proc_dir_entry *kitchen = proc_mkdir("kitchen", home);

Ok that was enough fun creating directories. Directories can only help that much. What we really want is some files which can be read to get some information from kernel. To create files, we have to specify name of the file, it's permissions, it's parent directory, and a function which will generate data to be given to user through that file. To create a file "name" under /proc/home.

struct proc_dir_entry *name = create_proc_read_entry("name", 0444, home, proc_read_name, NULL);

proc_read_name is the function which generates data

static int proc_read_name(char *page, char **start, off_t off, int count, int *eof, void *data)
{
u32 nbytes = 0;

/* Never worry about off or count, or else you will get a headache */
*start = NULL; /* We don't want kernel to use *start */

/* Here we print entire contents of file into page */
nbytes = sprintf(page, "Rama Nivas\n");

/* Signal EOF */
*eof = 1;

/* Size of file */
return nbytes;
}

These files can only handle PAGE_SIZE amount of data (actually more is possible but the effort is not worth it). If you have more than PAGE_SIZE bytes, it's better to split data across files.

Kermit scripting - 2

A kermit script consists of kermit commands that have to be executed. To
execute a kermit script use

$kermit + kscript

Arguments can be passed to script as

$kermit + kscript arg1 arg2

These arguments can be referenced within the script as \%1, \%2 etc.
Here \%1 = arg1 and \%2 = arg2

Conditionals

kermit scripts support conditional execution using "if" and looping
using "while".


if ! equal \%1 "" {
# Some piece of code to be executed when first argument is non-null
}

I have never actually used a "while" but here is one example I found
in a script.


while ! \F_eof(\m(f)) {
fread \m(f) l
out \m(l)\13
in 60 >
}

Wednesday, December 17, 2008

Command to generate emacs TAGS file for a directory tree

find . -name '*.[chSs]' -print | etags -

The find command searches for files matching the specified criteria and prints those files to standard output. The '-' argument asks etags to read list of files from standard input.

The part -name '*.[chSs]' -print asks find to print files with extension c, h, S, or s. The first argument is the root of the directory tree in which to search for files.

Kermit Scripting

In embedded systems development, kermit is commonly used to communicate to boards through serial port. Many of interactive dialog sessions between user and board can be automated using kermit scripts. If kscript is a file containing a kermit script it can be run by the following command "kermit -y kscript"

Here are some very useful kermit scripting commands

Basic interaction



To wait for a particular prompt and then issue a command
input 1000 bash$
lineout ls
Will wait for the prompt "bash$" to appear and then issue the "ls" command to board through serial port
minput <timeout> <s1> <s2> ...
Used to Wait for any of the given string
Ex:
minput 1000 bash$ >

Variables



First, define some variables
define delay 1000
define prompt bash$
define command ls
And use it
input \m(delay) \m(prompt)
lineout \m(command)

And when finally you are finished use
exit 0 "Over and out"

To add timestamped logging to kermit


set session-log timestamped-text
log session kermit-log.txt

Saturday, November 29, 2008

Memory

Memory is a funny thing, it forgets everything that I should remember and
remembers everything I want to forget.

Monday, October 27, 2008

Some feature requests to god

A search function to search for things in this world.
A Makefile with target 'clean' so that we can do a 'make clean' to clean up everything (dishes, clothes..everything).

Thursday, October 2, 2008

quote from a mathematician!!!

This is a wonderful quote from a calculus text

Logical thinking is much more important than "epsilon" and "delta".

Friendship and the power of pigenhole principle

In a group of n friends (n >= 2), atleast 2 of them will have same no: of
friends.

Reformulating as a graph theory problem
In an undirected graph G (with no self-loops) with n = |V| >= 2, atleast 2
vertices will have same degree.

Proof. We use proof by cases
(case 1). G is connected
The degree of a vertex can be any of n-1 different values 1, 2,...,n-1(A
vertex with degree 0 will imply that G is not connected). There are n
vertices in the graph. So atleast 2 of them should have same degree by
pigeonhole principle.
(case 2). G is not connected
The degree of a vertex can be any of n-1 different values 0,1,...,n-2 (A
vertex with degree n-1 will imply that G is connected). A similar argument
as case 1 applies.

How many digits does 2 "raise to" 1000 have?

We have to find decimal (base 10) digits. So convert to base 10.

21000 = 10(log 2)1000

Now 101, 102, and 103 has 2, 3, and 4 digits respectively.

So 21000 should have approximately (log 2)1000 + 1 digits.

Tuesday, September 23, 2008

Rambo mode of Linux kernel

Ok this is interesting......

Kernel enters *Rambo* mode in out_of_memory() :mm/oom_kill.c
and what does it stand for..well, kernel starts to "shoot down" processes hoping to increase amount of free memory in the system.

Friday, September 19, 2008

What does 'I' stand for?

No, I am not going to answer that question in this post.

The book I am a strange loop is a search for the answer. I just read 50 pages of it and came up with this thought...

In that book 'being alive' is described as having a set of complex reflexes. Then one of those relexes (one of the strongest) should be a reflex that generates the answer 'I am alive' whenever I ask myself 'Am I alive?'.

"I think, therefore I am"

Church numerals (numbers and arithmetic using lambda calculus)

Here's how one would define zero and increment function using lambda calculus
 
(define zero (lambda (f) (lambda (x) x)))

(define (add-1 n)
  (lambda (f) (lambda (x) (f ((n f) x)))))
- From SICP
 
Here zero is a function that takes a function (f) and returns a function which takes an argument (x) and applies f to x, zero times (or do not apply f to x).
 
The definition of add-1 can be easily understood by looking at the body of inner lambda expression. The body applies f to x (n+1) times, where n is the parameter.
 
So one would be
 
(define one (lambda (f) (lambda (x) (f x)))) ; We apply f one time to x
I'll leave the definition of one as an exercise.:-)
 

Tuesday, September 16, 2008

Bug fixing

Two ways to fix a bug

1. Change the implementation to match the spec.
2. Change the spec to match the implementation.

Monday, September 15, 2008

Some thoughts on mass

F = (Gm1m2)/r2, is the force of attraction between two bodies with masses m1 and m2 and r being the distance between them.

  1. So when two bodies are brought sufficiently close together (such that r becomes very small), the force of attraction will be very large. This force will pull them closer. So masses does have a tendency to combine.
  2. Two masses always attract each other. The universe is full of masses, but ever expanding why??
Law of gravitation

First drive after driving test

I drove all the way from mannuthi to palakkad (1 1/2 hour drive). The drive was OK, but I've got to get lot more practice.

Created a blogger template!!!

I created a blogger template with 2-column layout. You can watch a demo here.

Saturday, September 13, 2008

Implementing conditionals using lambda calculus

Lambda calculus is a way of modelling programming constructs by using only function definition and function application.
This shows you how to model if-then-else using lambda calculus
>>> true = lambda x, y: x
>>> false = lambda x, y: y
>>> ifelse = lambda x, y, z: x(y,z)
>>> ifelse(false, 1, 2)
2
>>> ifelse(true, 1, 2)
1
>>>
Well that was simple enough...Now for some explanation..We'll look at it top-down.
The chracters in this play are ifelse function, true function and false function [All characters are functions :-)]. The ifelse function takes three arguments and let's it's condition argument chose between 2nd (then-expression) and 3rd (else-expression) arguments. The true function always chooses true's first argument and false always chooses false's 2nd argument.
Note: - There is one catch though. Since python uses applicative order evaluation, this if else doesn't work exactly like the built-in ifelse construct. The following code will cause an out-of-memory exception.
>>> def p(): p()
...
>>> ifelse(false, p(), 1)

Factorials as summation

The factorial function is defined as
n! = 1, if n = 0
= n(n-1)!, if n > 0
It is recursively defined using products.
It can also be expressed as a recursive summation
Let's see what 3! looks like
3! is simply 3 groups of 2 groups of 1
(1 + 1) +
(1 + 1) +
(1 + 1)
similarly 4! is 4 groups of 3 groups of 2 groups of 1 (I.e. four copies of the above)
((1 + 1) +
(1 + 1) +
(1 + 1) ) +
((1 + 1) +
(1 + 1) +
(1 + 1) ) +
((1 + 1) +
(1 + 1) +
(1 + 1) ) +
((1 + 1) +
(1 + 1) +
(1 + 1) )
Now we can visualize why factorials "grow" like crazy..(10! may take up an entire book)
"Why bats, Master Wayne?"
"Bats frighten me. It's time my enemies shared my dread."
-From Batman Begins

Thursday, September 11, 2008

Stack permutation

I was trying to solve a stack permutation problem in taocp. The question asks us to find no: of permutations of n numbers from 1...n that can be generated using a stack. The no:s 1...n can be pushed into the stack in that order only. When an element is popped of the stack, it is appended to an output queue. Once all elements are in output queue, the order of elements in the queue is a permutation obtained using stack.


For ex:- To obtain 2,3,1 from 1,2,3 we follow the sequence of operations
push 1,push 2,pop 2,push 3,pop 3,pop 1
Not all permutations can be obtained using the stack (for ex: - 3,1,2).


Let the no: of permutations of 1...n be a(n) with a(0) = 1 (only empty permutation possible with no numbers).


We have to find a(n+1)


Suppose that nl no:s appear to left of "1" and nr no:s to right of "1" in a permutation. The process of obtaining a permutation for those nl numbers to the left of "1" can be considered as permuting 2...(nl+1) using a stack (The "1" at the bottom of the stack can be ignored). This no: will be the same as no: of permutations possible for 1...nl using a stack(Only the number of numbers matter).



Then no: of permutations possible for nl numbers to the left of "1" = a(nl).
Similarly no: of permutations possible for nr numbers to the right of "1" = a(nr).



For each of a(nl) permutations of numbers to the left, a(nr) permutations are possible for numbers to the right.
So a(nl)a(nr) permutations are possible for a particular position of "1" in the permutation.
Now nr = n - nl
So a(nl)a(nr) = a(nl)a(n-nl)



No: of elements to left of "1" can vary from 0...n.
So total number of permutations a(n+1) = sum(nl, 0, n) {a(nl)a(n-nl)} = a(0)a(n) + a(1)a(n-1) + .... + a(n)a(0)}


The term a(n) is simply the nth catalan number.


"Never believe in history, because history is created by those who win."

Driver's license

I got my driver's license today.....now I've got to learn to drive(properly)..

"I can only ride in 8's and drive in H's.."
- balu

PS: - This post should be dated 3-Sep-2008

Saturday, August 30, 2008

Life update

I should start practicing for my driving license test...(breakevide... :-P)

A question to 8-year olds!!:-o

Find the smallest number that when divided by n yields the remainder (n-1) for 2 <= n <= 10 (ie. dividing n by 10 yields 9, by 9 yields 8...).

Believe it or not, this was one of the questions asked for a 3rd standard annual exam (state syllabus).

Design documents

Code and design are maintained as separate files in almost all software projects. A major task faced by all IT firms is ensuring the consistency between the source code and the corresponding design document. Code maintainers confused by inconsistent design documents are commonplace. Literate programming might be the first step towards a solution.

Exposing module internals using seq_file interface

Learn all about seq_file interface here
http://www.xenotime.net/linux/doc/seq_file_howto.txt

The seq_file mechanism provides a more straightforward, non-iterator style, interface. A driver writer may simply define show() with operations that output data to proc file.

First of all, we have to create a file in /proc. For this, we have to call create_proc_entry in our module initialization function.

struct proc_dir_entry *foo = create_proc_entry("foo", 0, NULL); /* This will create "/proc/foo" */

Then initialize the proc file operations structure
foo->proc_fops = &foo_proc_operations;

where foo_proc_operations is defined as,

static struct file_operations foo_proc_operations = {
.open = foo_proc_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
seq_read, seq_lseek, and single_release are functions defined by Linux seq_file core.

Within foo_proc_open we have to call single_open() and pass it "show()", the function performing actual display.

return single_open(file, foo_proc_show, NULL);

Within foo_proc_show, we can use seq_{putc|puts|printf} to output data to "/proc/foo". These functions work like normal putc|puts|printf.

static int foo_proc_show(struct seq_file *m, void *v)
{
seq_puts(m, "Hello from foo\n"); /* write to our proc file */
return 0;
}

And finally don't forget to remove the proc file by calling,

remove_proc_entry("foo", NULL);

Sunday, February 10, 2008

back blogging....Feels like heaven

I'm back. Somehow got my system up and running after a long time.

Some very interesting LJ articles for your reading pleasure...
inside linux packet filter I
inside linux packet filter II
these articles describe the path of a network packet up the Linux kernel protocol stack.(And also describes how packet sockets are implemented).

Oh..and my new years resolution was - "Atleast one blog/month" ;-)