Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 20.9 Performance 781


The normal use ofdb_rewindanddb_nextrecis in a loop of the form
db_rewind(db);
while ((ptr = db_nextrec(db, key)) != NULL) {
/* process record */
}
As we warned earlier,there is no order to the returned records; they arenot in key
order.
If the database is being modified whiledb_nextrecis called from a loop, the
records returned bydb_nextrecaresimply a snapshot of a changing database at some
point in time.db_nextrecalways returns a ‘‘correct’’recordwhen it is called; that is,
it won’t return a recordthat was deleted. But it is possible for a recordreturned by
db_nextrecto be deleted immediately afterdb_nextrecreturns. Similarly, if a
deleted record is reused right afterdb_nextrecskips over the deleted record, we
won’t see that new recordunless we rewind the database and go through it again. If it’s
important to obtain an accurate ‘‘frozen’’snapshot of the database usingdb_nextrec,
then no insertions or deletions can be going on at the same time.
Look at the locking used bydb_nextrec.We’renot going through any hash chain,
and we can’t determine the hash chain that a recordbelongs on. Therefore, it is possible
for an index record to be in the process of being deleted whendb_nextrecis reading
the record. Toprevent this race,db_nextrecread locks the free list, thereby avoiding
any interaction with_db_dodeleteand_db_findfree.
Before we conclude our study of thedb.csource file, we need to describe the
locking when new index records or data records areappended to the end of the file. In
cases 1 and 3,db_storecalls both_db_writeidxand_db_writedatwith a third
argument of 0 and a fourth argument ofSEEK_END.This fourth argument is the flag to
these two functions, indicating that the new record is being appended to the file. The
technique used by_db_writeidxis to write lock the index file from the end of the
hash chain to the end of file. This won’t interferewith any other readers or writers of
the database (since they will lock a hash chain), but it does prevent other callers of
db_store from trying to append at the same time. The technique used by
_db_writedatis to write lock the entiredata file. Again, this won’t interferewith
other readers or writers of the database (since they don’t even try to lock the data file),
but it does prevent other callers ofdb_storefrom trying to append to the data file at
the same time. (See Exercise 20.3.)

20.9 Perfor mance


We wrote a test program to test the database library and to obtain some timing
measurements of the database access patterns of typical applications. This program
takes two command-line arguments: the number of children to create and the number of
database records (nrec)for each child to write to the database. The program then creates
an empty database (by callingdb_open),forksthe number of child processes, and
waits for all the children to terminate. Each child performs the following steps.
Free download pdf