Posts

Showing posts with the label PROGRAMMING

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. ...

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;         n...

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 s...

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 setrli...

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 assem...

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...

BFS vs CFS

http://forum.cyanogenmod.com/topic/18575-cfs-vs-bfs-kernel-scheduler/

Linux boot seqence in short

This the following minimum boot sequence  1) Bootloader loads the Kernel image to Ram      The image may be compressed or uncompressed.Uboot uses a special header in added        to the kernel image and make it as uImage. Using  mkimage we can make the image zImage/vmLinux 2) Ramdisk loaded in the memory, bootloader do that. 3) Control is given to kernel ,it also provides kernel arguments/command line options.Also bootlader set ramdisk as rootfs. Initial Rootfs filesystem may be of ext2 or minix format.   initramfs is the file can be unpacked by kerne l of following formats . . gzip ,  bzip2 ,  LZMA ,  XZ  and  LZO . 3) Then "linuxrc" ran from initial ramdisk. 4) The root device is changed to that specified in the kernel parameter. 5) The init program  /etc/init  is run which will perform the user configurable boot sequence. if rdinit is used then specified binary is called. ...

Difference of Compilers arm-none-linux-gnueabi and arm-none-eabi

I was confused between  arm-none-linux-gnueabi and arm-none-eabi .I didn't know where to use what. The gcc naming convention is like as below arch-vendor-(os-)-eabi so for  arm-none-linux-gnueabi is meant for the compilation to elf which uses linux. and  arm-none-eabi is meant for the compilation of the codes which will run on bare metal arm core. The difference is that they use library accordingly to compile the source codes. 

Oracle Virtual box upgradation issue

I recently tried to run Xubuntu 12.04 on virtualbox 1.2.0.It was working fine.Then due to some bug in my head :) tried to upgrade virtualbox to VirtualBox-4.2.0_RC3 from here . Now without uninstalling the previous version I installed it.It created problem.While running any Vms after installing extension pack it gave error [ " failed to load vmmr0.r0 (verr_ldr_mismatch_native)" ]. This issue is intially I couldn't solve.Later I solved the issue as following. Remove virtualBox ,remove folders from Program Files then removing "C:\Users\USERNAME\AppData\Local\VirtualStore\Program Files\Oracle\VirtualBox" {Need administrative privilege} Now install VBox freshly and import the virtual image projects/Disks.

Small Makefile tutorial

This Makefile tutorial by Alex is small but very useful. http://www.cprogramming.com/tutorial/makefiles_continued.html

Sysfs or procfs

I became  confused about the utilization of procfs and sysfs.Which one will be used in which place? What I understand as below. Procfs is much more generic interface to provide information to user space.All the process related information and some parameters are also passed to user space through profs file system, Whereas Sysfs is much more modern implementation like procfs.But it is implemented by following Linux driver model and hierarchy.Sysfs stores driver,device information alongside of process information too.As Greg KH told " sysfs is "1 value per file"" . so can be used for full user level driver implementation. Sysfs can do all the work like procfs ,they normally not used as information interface of process.Procfs will be there holding the process in formations. Update 23/10/2013: Sysfs is extensively used by udev. Whenever any sysfs file node is created udev get event and creates device files according to majo...

Asynchronus I/O (AIO) in vxworks and Linux

In generic Asynchronus I/O (AIO) can be designed using User lavel or in Kernel level. In user level 1) Process needs async callback/notification create another thread and process which goes to sleep on blocking calls (read/write/ioctls/select) 2) One Async master handler (process/thread) keeps track.All the process needs the service submit job to them.The master handler handles the async fd in Blocking mode and reply to requesting processes (or call the callback function) using some async notificatin like signal. In Kernel Level 1) Call some system call and submit some job to kernel workQ .After work is complete kernel notify using signal to user space.   In VxWorks :  aioPxLib provide the POSIX 1003.1b standard.Which is task implementation of aio service. functions available: 1) aio_read() - initiate an asyn. read. 2) aio_write() - initiate an syn write 3) aio_listio() - Initiate a aio list upto LIO_MAX 4) aio_error() - Get an aio error status. 5) aio_return(...