pthreads & dynamic memory in fbsd vs. the same in linux

Andriy Tkachuk ant at emict.com
Thu Feb 10 00:55:10 PST 2005


Hi folks.

I noticed the strange stick of pthreads (amount ~ 500)
when allocating dynamic memory by malloc or new
in my system:

> uname -a
FreeBSD ant.emict.com 5.3-STABLE FreeBSD 5.3-STABLE #0: Wed Feb  9 17:30:11 EET 2005     ant at ant:/lin/fbsd_obj/usr/src/sys/ANT  i386

It's interesting that the same program behaves differently
when it is compiled in linux and run on my fbsd machine in
linux_base-7.1_7 .

> ldd test2-linux
test2-linux:
        libpthread.so.0 => /lib/libpthread.so.0 (0x28065000)
        libstdc++-libc6.2-2.so.3 => /usr/lib/libstdc++-libc6.2-2.so.3 (0x2807c000)
        libm.so.6 => /lib/libm.so.6 (0x280bf000)
        libc.so.6 => /lib/libc.so.6 (0x280e1000)
        /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x2804c000)
> ldd a.out
a.out:
        libpthread.so.1 => /usr/lib/libpthread.so.1 (0x28075000)
        libstdc++.so.4 => /usr/lib/libstdc++.so.4 (0x28099000)
        libm.so.3 => /lib/libm.so.3 (0x2816b000)
        libc.so.5 => /lib/libc.so.5 (0x28184000)


Each thread allocates the amount of memory by 10 bytes in loop.
The number of iterations is specified in cmdline arg. The program
prints the time each thread is spent allocating memory in loop.
Let's look on results.

> ./a.out n 1000  # 1000 iterations of new operator. Every new allocates 10bytes.
thread 0 created
thread 1 created
thread 2 created
thread 3 created
thread 4 created
...
thread 497 created
thread 498 created
thread 499 created
0.001114
0.001022
0.001021
0.001011
0.001014
0.001010
0.001013
0.001050
0.001035
0.001011
0.001013
0.001010
0.001013
0.001010
0.001029
0.001075
0.001053
0.001011
0.001014
0.001011
0.001030
0.001010
0.001015
0.001042
0.001019
0.001011
0.001014
0.001012
0.001013
0.001010
0.001014
0.361604
3.225090
3.225458
3.225696
3.225925
3.226152
3.226380
3.226608
3.226833
3.227062
3.227290
3.227517
3.227744
3.227972
3.228202
3.228451
3.228681
3.228912
3.229140
3.229367


The same, but in linux_base-7.1_7 :

> ./test2-linux n 1000
thread 0 created
thread 1 created
...
thread 498 created
thread 499 created
0.000467
0.000403
0.000402
...
0.000391
0.000391
0.000395
...
0.000395
0.010564
0.000398
0.000394


The program source is attached.

In linux program is compiled by
$ gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
gcc version 2.96 20000731 (Red Hat Linux 7.1 2.96-98)

in fbsd:
> gcc -v
Using built-in specs.
Configured with: FreeBSD/i386 system compiler
Thread model: posix
gcc version 3.4.2 [FreeBSD] 20040728



More information about the freebsd-hackers mailing list