Tuesday, January 2, 2018

Natural Word size of Processor


natural word-size of a processor running a modern Linux, one can issue the following commands:

• getconf WORD_BIT
• getconf LONG_BIT

In the case of a modern x86_64 computer, WORD_BIT would return 32 and LONG_BIT would return 64. In the case of a x86 computer without a 64-bit extension, it would be 32 in both cases.

Compiler Flag to Display Padding if it adds to Structure

root@ALOPRASA-EAN6Q:/mnt/c/Users/aloprasa# cat a.c
#include
main()
{

        struct Foo {
                char x; // 1 byte
                char z; // 1 bytes
                int  y; // 4 bytes
        };
}

root@ALOPRASA-EAN6Q:/mnt/c/Users/aloprasa# gcc a.c -Wpadded
a.c: In function ‘main’:
a.c:8:8: warning: padding struct to align ‘y’ [-Wpadded]
   int  y; // 4 bytes
        ^


To avoid Padding by Compiler

  1. #pragma pack (1) before structure
  2.  __attribute__((packed)) after structure 
  3. Using compiler flag -fpack-struct


Performance of Aligned vs Non Aligned

gcc -DRUNS=400000000 -DCLOCK=CLOCK_MONOTONIC -std=gnu99 -O0 

1. Aligned Program
struct Foo {
char x;
short y;
int z;
};
struct Foo foo;
clock_gettime(CLOCK, &start);
for (unsigned long i = 0; i < RUNS; ++i) {
foo.z = 1;
foo.z += 1;
}
clock_gettime(CLOCK, &end);


2.Mis Aligned Program ( Packed structure)

struct Bar {
char x;
short y;
int z;
} __attribute__((packed));
struct Bar bar;
clock_gettime(CLOCK, &start);
for (unsigned long i = 0; i < RUNS; ++i) {
bar.z = 1;
bar.z += 1;
}
clock_gettime(CLOCK, &end);


Intel Core i7-2670QM CPU on Linux 3.12.5.
Results: aligned runtime: 9.504220399 s
unaligned runtime: 9.491816620 s
( Not much on X86 Arachitecture)

On Rasperry Pi
Results: aligned runtime: 12.174631568 s
unaligned runtime: 26.453561832 s



Source :https://wr.informatik.uni-hamburg.de/_media/teaching/wintersemester_2013_2014/epc-14-haase-svenhendrik-alignmentinc-paper.pdf







No comments:

Post a Comment

Featured Post

XDP - Getting Started with XDP (Linux)