Posts

Showing posts from 2013

How malloc works in userspace

I was checking how malloc work as compared to kernel kmalloc in term of physical memory allocation.What I found as below.. Glibc/C libary implement the malloc implementation, For memory less than 128kb use brk()/sbrk more than that use mmap with  MAP_PRIVATE|MAP_ANONYMOUS for test ran a small program on userspace I wrote void main() {     int *a;     a =(int*)malloc(150*1024);  /* mmap invocation */    //a =(int*)malloc(10); /* brk invocation */ } and ran strace on the o/p, there I can see brk and mmap invocation.What I understood from brk call.It increases the data section (Thr Brk limit) and add the memory to malloc.In mmap anonymous private pages are allocated by kernel and added as one vma section of process address space,From there glibc allocate memory and give it to malloc. more details : Link1 Link2 Link3 bestone__  Best_link

Process Virtual to Physical Translation

Image

Allocating aligned address and freeing them

uintptr_t mask = ~(uintptr_t)(align - 1); void *mem = malloc(1024+align-1); void *ptr = (void *)(((uintptr_t)mem+align-1) & ~mask); ptr is the aligned address. Some time back I faced an interview ,There I was asked to write a custom malloc using malloc and free program for a predefined aligned address.Here is the solution.The byte before the aligned byte is always empty.We will keep the offset of actual memory allocated by malloc. But there we have to allocate total memory =(desired+alignment) instead of (desired+alignment-1) downside is that it can store upto 2pow8 offsets. mallocX(size_t X,alignment Y) { p= malloc(X+Y); ret = (p+Y) & ~(Y-1); *(ret-1) = ret - p; return ret; } similarly for free freeX(memptr mem) { free (mem - *(mem-1)); }

Tracking Linux kworker threads

How to find out which part of kernel/module has created this workqueue. How to track a kworker-thread named for example ''kworker/0:3 to its origin in kernel-space? I found  this thread on lkml  that answers your question a little. (It seems even Linus himself was puzzled as to how to find out the origin of those threads.) Basically, there are two ways of doing this: $ echo workqueue:workqueue_queue_work > /sys/kernel/debug/tracing/set_event $ cat /sys/kernel/debug/tracing/trace_pipe > out.txt (wait a few secs) For this you will need  ftrace  to be compiled in your kernel, and to enable it with: mount -t debugfs nodev /sys/kernel/debug More information on the function tracer facilities of Linux is available in the  ftrace.txt documentation . This will output what threads are all doing, and is useful for tracing multiple small jobs. cat /proc/THE_OFFENDING_KWORKER/stack This will output the stack of a single thread doing a lot of work. It may allow you

Bit reversing tips

Reversing bit pairs unsigned int i, j; // positions of bit sequences to swap unsigned int n; // number of consecutive bits in each sequence unsigned int b; // bits to swap reside in b unsigned int r; // bit-swapped result goes here unsigned int x = ((b >> i) ^ (b >> j)) & ((1U << n) - 1); // XOR temporary r = b ^ ((x << i) | (x << j)); ----------------------------------------------------------- Another standard simple method: unsigned int reverseBits(unsigned int num) {      unsigned int count = sizeof (num) * 8 - 1;      unsigned int reverse_num = num;      num >>= 1;      while (num)      {         reverse_num <<= 1;               reverse_num |= num & 1;         num >>= 1;        count--;      }      reverse_num <<= count;      return reverse_num; } int main() {      unsigned int x = 1;     printf ( "%u" , reverseBits(x));      getchar (); } --------------

Nice value and priority and relations

The  priority  of a process in linux is dynamic: The longer it runs, the lower its priority will be. A process  runs  when its actually using the CPU - most processes on a typical Linux box just wait for I/O and thus do not count as  running . The priority is taken into account when there are more processes running than CPU cores available: Highest priority wins. But as the winning process looses its proirity over time, other processes will take over the CPU at some point. nice  and  renice  will add/remove some "points" from priority. A process which has a higher  nice value will get lesser CPU time. Root can also set a negative  nice  value - the process gets more CPU time. Example: There are two processes (1 and 2) calculating the halting problem and one CPU core in the system. Default is  nice 0 , so both processes get about half of the CPU time each. Now lets renice process 1 to value 10. Result: Process 2 gets a significant higher amount of cpu time as process 1

Forked process, thread and address spaces little deeper

In Fork() The child process has a unique process ID. The child process has a different parent process ID (i.e., the process ID of the parent process). The child process has its own copy of the parent's descriptors. These descriptors reference the same underlying objects, so that, for instance, file pointers in file objects are shared between the child and the parent, so that an lseek(2) on a descriptor in the child process can affect a subsequent read(2) or write(2) by the parent. This descriptor copying is also used by the shell to establish standard input and output for newly created processes as well as to set up pipes.semaphores if opened it also inherit.  Memory mappings created in the parent are retained in the child process,(If  MAP_PRIVATE was used in parent,it will be  MAP_PRIVATE in child to,after forking if any change in memory mapped area that will be visible to corresponding process only) The child process' resource utilizations are set to 0; see setrlimit(2)

Global variable ,static always initialized but auto is not initialized

Security : leaving memory alone would leak information from other processes or the kernel. Efficiency : the values are useless until initialized to something, and it's more efficient to zero them in a block with unrolled loops. Reproducibility : leaving the values alone would make program behavior non-repeatable, making bugs really hard to find. Elegance : it's cleaner if programs can start from 0 without having to clutter the code with default initializers. One might wonder why the  auto  storage class does start as garbage. The answer is two-fold: It doesn't, in a sense. The very first stack frame does receive zero values. The "garbage", or "uninitialized" values that subsequent instances at the same stack level see are really the previous values left by the same program. There might be a runtime performance penalty associated with initializing  auto  (function locals) to anything. A function might not use any or all of a large ar

Speed of execution in term of code in some cases

Is “else if” faster than “switch() case” ? For small loops not required,For very big loop hash table is used for  switch case so faster execution. But with good compiler with optimization enabled, it's same. Why i++ is faster than i=i+1 in c older compilers used  (1)ADD      \\i+1 (2)Assignment operation          \i=x for i=i+1 for i++ (1)INR but currently good compilers with optimizations enabled creates same assembly code.

ARM Nesting of Interrupts

Image
NESTING INTERRUPTS Applies to:   RealView C Compiler Answer Information in this article applies to: RealView Compiler Version 3.0 or higher QUESTION The classic ARM architecture only provides two interrupts (IRQ and FIQ). The Vectored Interrupt Controller or Advanced Interrupt Controller provides interrupt priorities and interrupt nesting for the standard interrupt, but it requires that you set the  I  bit in the CPSR. What is the best method to allow interrupt nesting with the RealView compiler? ANSWER It should be noted that good programming technique implies that you keep interrupt functions very short. When you are using short interrupt functions, interrupt nesting becomes unimportant. When you are using an Real-Time Operating System (such as the RTX Kernel), the stack usage of user tasks becomes unpredictable when you allow interrupt nesting. However, if you still need interrupt nesting in your application, you may implement it using an assembly language w

Kashmiri style Tangra Masala

Sometime I like to cook non-everyday food by my self.Kept my wife aside and cooked the Tangra masala.Pros was that I got appreciation from her,but cons was she want me cook the tangra fish always whenever possible. :( Recipe is as follows. Ingredients  : Tangra Fish Mustard oil Onions - finely chopped Green chilies - Slit lengthwise from the middle Ginger-Garlic paste Coriander leaves Tomato - Finely chopped Cumin, Coriander, Red-chilly and Garam-Masala powder Posto seed (Poppy Seeds) OR Suji (Semolina) - whole, not paste Groundnut - paste Salt and sugar to taste Method  : Heat mustard oil in a pan, fry the fishes with turmeric powder and salt. Keep aside the fried fishes and add the finely chopped onions, tomatoes, ginger-garlic paste into the heated oil. Mix sugar (optional). Once golden-brown, add to it the green-chillies, cumin-coriander-red chilly powders also salt (save the garam-masala powder till the end) followed by the groundnut paste, coriander leaves. Fry

Airtel Digital tv remote factory reset and reprogram

Following is the way to reset Airtel Digial tv universal remote reset procedure. 1. Press red button and ok button together untill red light blinks twice. 2. Release the buttons and press 980 buttons one by one (not together)   3. Red light will blink again two times, now press ok button. Reprogramming There are 5 basic functions which your new Airtel remote needs to learn about your TV 1. TV Power code 2. TV Mute code 3. AV code 4. TV VOL + code 5. TV VOL – code Steps for Universal remote setup: Step 1:  Bring Airtel digital TV remote in learning mode. Press and hold ‘’OK + Numeric 2” key for 3 seconds. The LED blinks twice to indicate that your Airtel remote is ready to learn. Step 2 :  Hold the source remote and Airtel remote in front of each other ( 3cm-6m apart). Step 3:  Press & release the key (First on Airtel remote and then on Source remote) which needs to be programmed in Airtel remote. Example: Press and release the TV Power key on both remotes and release. The A

Priority Inheritance in Linux Kernel

According to LWN.net There are a number of approaches to priority inheritance. In effect, the kernel performs a very simple form of it by not allowing kernel code to be preempted while holding a spinlock. In some systems, each lock has a priority associated with it; whenever a process takes a lock, its priority is raised to the lock's priority. In others, a high-priority process will have its priority "inherited" by another process which holds a needed lock. Most priority inheritance schemes have shown a tendency to complicate and slow down the locking code, and they can be used to paper over poor application designs. So they are unpopular in many circles. Linus was  reasonably clear  about how he felt on the subject last December: "Friends don't let friends use priority inheritance". Just don't do it. If you really need it, your system is broken anyway. Faced with this sort of opposition, many developers would quietly shelve their priority