AIX: pthread_rwlock_* and writer starvation


 
Thread Tools Search this Thread
Top Forums Programming AIX: pthread_rwlock_* and writer starvation
# 1  
Old 12-07-2009
AIX: pthread_rwlock_* and writer starvation

I wanted to use the pthread read/write locks in an application, unfortunately my task requires that readers perform very well, but writers are never starved. I have found that the AIX implementation, while having excellent reader performance, heavily starves writers.

To test this, I wrote an application that spawned about twenty read threads and a single writer thread. Letting it run I can see that the poor writer never gets the mutex; not even once! Worse, even if I throttle the readers back, the writer still gets the mutex in an untimely manner.

Unfortunately, I have tried two alternate strategies which, while more fair, have drastically worse reader performance than the pthreads implementation. This is why I'm here. I am hoping someone can either a) point me to something that may perform better, b) tell me the drastic performance change should be expected and move on with my life, lol, or c) give me some ideas how to tweak the pthreads implementation into submission.

If anyone is interested, the two alternate stragegies were:

1) I "rolled my own" read write lock, modelled after sources I found online. This uses a mutex, two condition variables, and counters for current readers and blocked writers/readers. After profiling, it seems the performance impediment here is high contention for the mutex, maybe because it is causing lots of context switches. All I can tell is that pthread_mutex_lock is killing me.

2) I grabbed a source which made use of the atomic operation fetch_and_add to implement the lock. Here, it seems fetch_and_add is expensive; which I suppose it would be.

Because the final application is mostly concerned with reader performance, I tested these with the driver spawning only reader threads and no writers to contend with them. Configured this way, the reader performance was as follows:

pthread_rw_lock: .02 milisecond average acquisition time over 11 million acquisitions (sum of all threads).
my home grown lock: .09 miliseconds and 2.18 million acquisitions.
fetch_and_add lock: .08 miliseconds and 2.5 million acquisitions.

Any ideas, questions, comments? Thanks!
# 2  
Old 12-08-2009
Can you share your test application with us?
# 3  
Old 12-08-2009
How much data are you keeping in memory?

If you can keep multiple copies in RAM, you can do away with the locking almost entirely.

Code:
data_t *old;
volatile data_t *current;
volatile data_t *new;
   .
   .
   .
/* data update thread */
while ( 1 )
{
    new = generateUpdatedData();
    cleanUpOldData( old );
    old = current;
    current = new;
}
   .
   .
   .
/* data reader */
data_t *myCopy = current;
extractDataTypeOne( myCopy );
extractDataTypeTwo( myCopy );

As long as the time it takes to generate a new copy of data and clean up the old copy of data is guaranteed to be longer than the time it takes for any thread to access the current copy of the data, you won't have any problems. If you can't get that guaranteed time, you can track the number of reader threads using a simple integral counter using atomic increments and decrements and not clean up the old copy until the counter goes to zero, because once a copy of the data is moved from "current" to "old", no new threads should be able to access it.

Yes, I'm assuming the compiled binary will take care of visibiity issues across multiple processors when the pointers are swapped around. I'm not sure what AIX offers for memory barriers, but Solaris and (IIRC) BSD provide membar_producer() and membar_consumer(). See "man membar_ops" or read "sys/atomic.h" on Solaris.
# 4  
Old 12-08-2009
fpmurphy: I can not share the driver right now, it is locked up in a Citrix environment and it will not allow file transfer in/out nor internet access. I'll see what I can do to get it over here.

achenle: As far as doing in-memory swaps, I would still need a mutex to control access to the reference count, or use the atomic add operations. Both seem to suffer from a performance problem.
# 5  
Old 12-08-2009
I know that the OS/400 implementation of pthread read/write locks does not favor writers.

Quote:
The OS/400 implementation of pthread_rwlock_tryrdlock(), for example, does not completely honor the Single UNIX Specification in its treatment of reader/writer contention. The standard states the following: ″The function pthread_rwlock_tryrdlock() applies a read lock as in the pthread_rwlock_rdlock() function with the exception that the function fails if any thread holds a write
lock on rwlock or there are writers blocked on rwlock.″

In the OS/400 implementation, if pthread_rwlock_tryrdlock() is used on a read/write lock that has multiple readers holding the lock and multiple waiting writers blocked on the lock, the pthread_rwlock_tryrdlock() are allowed to complete successfully.
I wonder does the same apply on AIX?
# 6  
Old 12-09-2009
fpmurphy, I will attempt to make a change to the application to use pthread_rwlock_tryrdlock and see if it helps my situation and allows the writers in more often. Thanks for the suggestion, I'll let you know how it turns out.
# 7  
Old 12-10-2009
I've used a single semaphore for reader/writer locks on occasion.

It's used somewhat like it usually would -- readers call sem_wait, do their business, then call sem_post -- but the semaphore starts with a value higher than one so multiple threads can read without contention. To get a write lock, the writer waits n times, does its business, then posts n times. This does make writing somewhat of a bigger operation than reading but if readers don't hog, writers never starve.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Emergency UNIX and Linux Support

Need an efficient XML writer for Perl

I don't care about user friendliness, but I don't wanna re-invent the wheel either. What's a good XML writer for Perl that's the most efficient? Thanks! (1 Reply)
Discussion started by: stevensw
1 Replies

2. Linux

install openoffice writer from yum

Is there a way to install openoffice writer from yum? Can I add some repository then do that? (5 Replies)
Discussion started by: cokedude
5 Replies

3. Homework & Coursework Questions

awk script that implements a report writer

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: The script should compute the sale amounts per associate for the year 2009 and print them in a sorted list ranked... (1 Reply)
Discussion started by: Jeffthrow5
1 Replies

4. Programming

Question about read writer lock

From <<Advanced Programming in the Unix>> section 11.6, it says: Although implementations vary, readerwriter locks usually block additional readers if a lock is already held in read mode and a thread is blocked trying to acquire the lock in write mode. This prevents a constant stream of readers... (5 Replies)
Discussion started by: robin.zhu
5 Replies

5. AIX

Question on CD writer & backup

Hi All, 1. I have many AIX system here but none of them has any CD writer. I want to buy external CD writer so I can move around when needed. Can some one tell me which one they have. Please let me know exact model number so it will be easy for me to order it. I tried going to IBM web site,... (4 Replies)
Discussion started by: samnyc
4 Replies

6. AIX

Probleme with DVD Writer

I can write into DVD? I have USE "MKCD" command mkcd -r directorie -d /dev/cd1 please help me it s urgent (2 Replies)
Discussion started by: mktahar
2 Replies

7. UNIX for Advanced & Expert Users

Probleme With DVD Writer

I Use mkcd for save same directories into DVD, But the commande not complete succefuly mkcd -r directorie_i_whish_save -d /dev/cd1 please it is very urgent thank you :confused: :confused: (1 Reply)
Discussion started by: mktahar
1 Replies

8. UNIX for Dummies Questions & Answers

Share CD-Writer

Hi, guys ! I have a server that runs FreeBSD 5.3 and on that server a have a CD-Writer used for backup. The question is, does anybody know how can I share this CD-Writer ? I want to be able to write CDs from another computer, without transfering all the data to the server and after that use all... (2 Replies)
Discussion started by: Sergiu-IT
2 Replies
Login or Register to Ask a Question