Sponsored Content
Top Forums Programming Performance inquiry...guestimate better approach Post 302102965 by DreamWarrior on Monday 15th of January 2007 03:37:41 PM
Old 01-15-2007
Performance inquiry...guestimate better approach

So, I've been told and heard a million times that malloc and free are expensive calls to be avoided. So many times I avoid malloc by reusing already allocated memory blocks. Store them on free lists somewhere myself and don't call free when I know malloc will be called very shortly thereafter.

I am writing an in-memory communication queue API for my multi-threaded application. Originally there was only to be one set of threads in the application who would be using this queue. Now, there may be more sets of threads and therefore more queues. Because they all use the same size payload to communicate, they could also all share their allocations. However, if that were to happen then the free list would have to be mutex protected across thread groups.

So...here's the rub. At this point the free list is my bottleneck. Before it wasn't such a big deal because I only had one set of threads which needed a mutex anyway. So I crafted the API to protect the free list and the queue together. Now the free list would either be wasteful (unshared) or shared (and slow). The other option, of course, at this point is to rebuke my "malloc is slow" mentality and alloc and free the payloads as needed.

Anyone want to jump in and comment. What do you think will be faster and why?

Method 1:

(producer)
- Malloc payload
- grab queue mutex
- place on queue
- alert threads (condition var)
- unlock mutex

(consumer)
- lock queue mutex
- block condition var
- acquire work off queue
- unlock mutex
- free payload

Method 2:

(producer)
- grab free list mutex
- get item off free list (or malloc if list is empty)
- unlock free list mutex
- place on queue
- alert threads (condition var)
- unlock mutex

(consumer)
- lock queue mutex
- block condition var
- acquire work off queue
- unlock mutex
- grab free list mutex
- place item on free list
- unlock free list mutex

My guess is that at this point, the overhead of malloc and free is probably less than that of a mutex lock for the free items list. That's my hunch, but I know malloc and free have to be protecting their heaps for multi-thread access, but they could be using a lock-free algorithm too. Also, by using malloc and free a lot I have the potential to fragment memory if interleaving application calls allocate memory too. Given this is an API I'm writing, I have to take that into consideration a little too. I'd love to get back to the simple life and not manage a free list, but I also don't want to kill performance to do it. Of course, if the free list is the performance hog it may be, then I obviously don't want to use it and just let malloc and free do their jobs.

Oh...what to do...what to do.... Comments, questions, I'd love to hear some input on this one. I'd prototype it, but I don't have a whole lot of time, and I just got done prototyping a posix in-memory queue vs. AIX message queue application and I'm prototyped out, lol. Given the API I'm writing, I'm guessing you can pick the winner of that shoot-out.
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

uniX iNQUIry from a newbie

hi, i would like to study unix but i don't have the software for me to test the scripts that i read from the book and from the internet. I would like to ask anyones help to please tell me link wer i can download for free the unix system. I would be glad to receive your replies. thanks, (2 Replies)
Discussion started by: youdexter
2 Replies

2. Web Development

Suggested tool / approach for performance testing

What is a good approach for a performance testing tool suite for web applications? I am specifically interested in tools that execute a certain set of tasks well as opposed to tuning high traffic sites. In other words, a profiler would be a good idea to have, although I understand these tools are... (4 Replies)
Discussion started by: figaro
4 Replies

3. Infrastructure Monitoring

Firewall / Network isolation inquiry

Good morning folks, A good friend of mine has a network where every host has two paths to the file servers (two NICs & two networks for all hosts). Normally speaking, one network will be used for regular application traffic - license servers, itunes library, collaboration tools - while the... (4 Replies)
Discussion started by: avronius
4 Replies

4. UNIX for Dummies Questions & Answers

Offline Agents Inquiry.

Hello, I currently use Solaris, and typically I use the svcs -a | grep PROCESS to see if it's online or Offline. My questions is SVCS is in solaris but if I want to find out if a daemon or process is offline what other methods can I use? ps -ef | grep PROCESS "what do I look for" or... (1 Reply)
Discussion started by: NelsonC
1 Replies

5. Shell Programming and Scripting

Inquiry on Grabbing info from file.

Here is another script I am trying to customize currently, this script is used to send me disk space information, but at the moment I have to enter all the servers in manually SERVER= "xxx bbb ccc" ect.. how can I script it so that the servers are called off a txt file versus me entering all... (1 Reply)
Discussion started by: NelsonC
1 Replies

6. UNIX for Dummies Questions & Answers

UNIX Inquiry

Hi Everyone! I'm new with UNIX,so, sorry if this question seems really dumb.:( Anyway, I'd just like if it's possible to automatically inform someone (via mail or pop-up box or something) that a file has been recently uploaded/received to the UNIX box? If it is, any advice on how to get that... (0 Replies)
Discussion started by: jam04
0 Replies

7. UNIX for Dummies Questions & Answers

UNIX Inquiry for Automatic Sending

Hi Everyone! I'm new with UNIX,so, sorry if this question seems really dumb.:( Anyway, I'd just like if it's possible to automatically inform someone (via mail or pop-up box or something) that a file has been recently uploaded/received to the UNIX box? If it is, any advice on how to get that... (1 Reply)
Discussion started by: jam04
1 Replies

8. UNIX for Dummies Questions & Answers

UNIX inquiry for 'awk'

Hello Everyone, May I ask for your help regarding one of the UNIX command “awk”. So I executed a script and the output looks like this (see below): output.txt CONTRACTNAME ... (3 Replies)
Discussion started by: steven_huskie
3 Replies

9. Solaris

Sun Server and Solaris 7 Inquiry

Greetings! Will be firing up the good ole pizza box, soon. Does anyone know if Solaris 7 is still okay to use? Last time I attempted was 2006. Thank you in advance, ControlTomato (6 Replies)
Discussion started by: ControlTomato
6 Replies
pthread_mutex_trylock(3)				     Library Functions Manual					  pthread_mutex_trylock(3)

NAME
pthread_mutex_trylock - Attempts to lock the specified mutex, without waiting if already locked. LIBRARY
DECthreads POSIX 1003.1c Library (libpthread.so) SYNOPSIS
#include <pthread.h> int pthread_mutex_trylock( pthread_mutex_t *mutex); STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: IEEE Std 1003.1c-1995, POSIX System Application Program Interface PARAMETERS
Mutex to be locked. DESCRIPTION
This routine attempts to lock the mutex specified in the mutex argument. When a thread calls this routine, an attempt is made to immedi- ately lock the mutex. If the mutex is successfully locked, this routine returns zero (0) and the calling thread becomes the mutex's current owner. If the specified mutex is locked when a thread calls this routine, the calling thread does not wait for the mutex to become avail- able. The behavior of this routine is as follows: For a normal, default, or errorcheck mutex: if the mutex is locked by any thread (including the calling thread) when this routine is called, this routine returns [EBUSY] and the calling thread does not wait to acquire the lock. For a normal or errorcheck mutex: if the mutex is not owned, this routine returns zero (0) and the mutex becomes locked by the calling thread. For a recursive mutex: if the mutex is owned by the calling thread, this routine returns zero (0) and the mutex lock count is incremented. (To unlock a recursive mutex, each call to pthread_mutex_trylock(3) must be matched by a call to pthread_mutex_unlock(3).) Use the pthread_mutexattr_settype(3) routine to set the mutex type attribute (normal, default, recursive, or errorcheck). RETURN VALUES
If an error condition occurs, this routine returns an integer value indicating the type of error. Possible return values are as follows: Successful completion. The mutex is already locked; therefore, it was not acquired. The value specified by mutex is not a valid mutex. ERRORS
None RELATED INFORMATION
Functions: pthread_mutexattr_settype(3), pthread_mutex_destroy(3), pthread_mutex_init(3), pthread_mutex_lock(3), pthread_mutex_unlock(3) Manuals: Guide to DECthreads and Programmer's Guide delim off pthread_mutex_trylock(3)
All times are GMT -4. The time now is 01:48 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy