niedziela, stycznia 20, 2008

glibc_malloc vs alloca => 166197 / 1536 = ~100


localhost systest_modules # ./malloc
Ustawianie maski procesorow na 1

-- SYSTEM INFO -------------------

localhost: Linux 2.6.24-rc7 #6 SMP PREEMPT Mon Jan 14 16:50:37 CET 2008

Ustawianie priorytetu dla SCHED_OTHER na 0 (zwykly proces) dla 29488
*** TIMED: Instrukcja: 'tab[++i] = malloc_sbrk(100);'
*** TIMED: 0sec 9357nsec +- 1nsec
Addr: 804c000

*** TIMED: Instrukcja: 'tab[++i] = malloc_mmap(100);'
*** TIMED: 0sec 8449nsec +- 1nsec
Addr: b7f0e000

*** TIMED: Instrukcja: 'tab[++i] = malloc_stos(100);'
*** TIMED: 0sec 1536nsec +- 1nsec
Addr: bf806550

*** TIMED: Instrukcja: 'tab[++i] = malloc_libc(100);'
*** TIMED: 0sec 166197nsec +- 1nsec
Addr: 804c070

Ustawianie priorytetu dla SHED_FIFO na 99 (zakres 1-99) dla 29488
*** TIMED: Instrukcja: 'tab[++i] = malloc_sbrk(100);'
*** TIMED: 0sec 5446nsec +- 1nsec
Addr: 806d000

*** TIMED: Instrukcja: 'tab[++i] = malloc_mmap(100);'
*** TIMED: 0sec 6495nsec +- 1nsec
Addr: b7f0d000

*** TIMED: Instrukcja: 'tab[++i] = malloc_stos(100);'
*** TIMED: 0sec 1396nsec +- 1nsec
Addr: bf806550

*** TIMED: Instrukcja: 'tab[++i] = malloc_libc(100);'
*** TIMED: 0sec 1816nsec +- 1nsec
Addr: 804c070

A kod wyglądał tak:


#include "framework.h"
#include

inline void* malloc_sbrk(int size) {
/* Poszerzenie przestrzeni adresowej programu */
void *addr = sbrk(size);
if (addr == (void*)-1)
addr = NULL;
return addr;
}

inline void* malloc_mmap(int size) {
/* Alokacja pamieci za pomoca mmap */
void *addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
if (addr == (void*)-1)
addr = NULL;
return addr;
}

inline void* malloc_stos(int size) {
/* Alokacja pamieci na stosie */
return alloca(size);
}

inline void* malloc_libc(int size) {
return malloc(size);
}

void test_malloc(int rt) {
int i = 0;
void* tab[10];

RT_ON(rt);
#define CHECK_OK() do { \
printf("Addr: %x\n\n",(unsigned int)tab[i]); \
if (tab[i] == NULL) SHOW_ERR(); \
} while(0);


TIMED(
tab[++i] = malloc_sbrk(100);
);
CHECK_OK();

TIMED(
tab[++i] = malloc_mmap(100);
);
CHECK_OK();

TIMED(
tab[++i] = malloc_stos(100);
);
CHECK_OK();

TIMED(
tab[++i] = malloc_libc(100);
);
CHECK_OK();

free(tab[i]);
}

int main(int argc, char **argv) {
one_cpu();
uname_info();
test_malloc(0);
test_malloc(1);
return 0;
}

1 komentarze:

Anonimowy pisze...

Hmm, biorąc pod uwagę jak działa alloca(), taki wynik chyba nie zaskauje jakoś szczególnie? :)