Friday Mar 23, 2012

Malloc performance

My co-conspirator Rick Weisner has written up a nice summary of malloc performance, including evaluating the mtmalloc changes that we got into S10 U10.

Thursday May 22, 2008

Reserving temporary memory using alloca

There are occasions where it's useful to allocate a small temporary working area for the duration of a call to a routine - for example to hold an array. One way of doing this is to use malloc and free:

void f(int a)
{
  int\* array=(int\*)malloc(sizeof(int)\*a);
  ...
  free(array);
}

Obviously the use of malloc and free does incur some overhead, and which is undesirable, particularly if this is a performance critical routine.

An alternative approach is to use alloca which allocates memory on the stack, here's the man page. Being on the stack, the memory is 'freed' when the routine exits. Well, the memory isn't allocated, so it's not really freed, but accesses to the memory once the routine exits are not valid - you'll be accessing data either beyond the stack, or in the stack frame of another routine. Neither situation is likely to be good.

The equivalent code is:

#include 

void f(int a)
{
  int\* array=(int\*)alloca(sizeof(int)\*a);
  ...
}

For routines which do require temporary storage, this can be a much faster way of allocating it.

Friday Feb 08, 2008

Interposing on malloc

Ended up wanting to look at malloc calls, how much was requested, where the memory was located, and where in the program the request was made. This was on S9, so no dtrace, so the obvious thing to do was to write an interpose library and use that. The code is pretty simple:

#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
#include <ucontext.h>

void \* malloc(size_t size)
{
  static void\* (\*func)(size_t)=0;
  void\* ret;
  if (!func) {func=(void\*(\*)(size_t))dlsym(RTLD_NEXT,"malloc");}
  ret=func(size);
  printf("size = %i address=%x\\n",size,ret);
  printstack(0);
  return ret;
}

The code uses a call to printstack to print out the stack at the point of the call.

The code is compiled and run with:

$ cc -O -G -Kpic -o libmallinter.so mallinter.c
$ LD_PRELOAD=./libmallinter.so ls
size = 17 address=25118
/home/libmallinter.so:malloc+0x5c
/usr/lib/libc.so.1:_strdup+0xc
/usr/lib/libc.so.1:0x73b54
/usr/lib/libc.so.1:0x72d44
/usr/lib/libc.so.1:0x720e4
/usr/lib/libc.so.1:setlocale+0x3c
/usr/bin/ls:main+0x14
/usr/bin/ls:_start+0x108
size = 17 address=25138
/home/libmallinter.so:malloc+0x5c
/usr/lib/libc.so.1:_strdup+0xc
/usr/lib/libc.so.1:0x73b54
/usr/lib/libc.so.1:0x72d44
/usr/lib/libc.so.1:0x720e4
/usr/lib/libc.so.1:setlocale+0x3c
/usr/bin/ls:main+0x14
/usr/bin/ls:_start+0x108
About

Darryl Gove is a senior engineer in the Solaris Studio team, working on optimising applications and benchmarks for current and future processors. He is also the author of the books:
Multicore Application Programming
Solaris Application Programming
The Developer's Edge

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
5
6
8
9
10
12
13
14
15
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks
The Developer's Edge
Solaris Application Programming
Publications
Webcasts
Presentations
OpenSPARC Book
Multicore Application Programming
Docs