xref: /onnv-gate/usr/src/tools/ctf/cvt/ctfmerge.c (revision 6936:72189fcd99e4)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53446Smrj  * Common Development and Distribution License (the "License").
63446Smrj  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*6936Ssommerfe  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * Given several files containing CTF data, merge and uniquify that data into
300Sstevel@tonic-gate  * a single CTF section in an output file.
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  * Merges can proceed independently.  As such, we perform the merges in parallel
330Sstevel@tonic-gate  * using a worker thread model.  A given glob of CTF data (either all of the CTF
340Sstevel@tonic-gate  * data from a single input file, or the result of one or more merges) can only
350Sstevel@tonic-gate  * be involved in a single merge at any given time, so the process decreases in
360Sstevel@tonic-gate  * parallelism, especially towards the end, as more and more files are
370Sstevel@tonic-gate  * consolidated, finally resulting in a single merge of two large CTF graphs.
380Sstevel@tonic-gate  * Unfortunately, the last merge is also the slowest, as the two graphs being
390Sstevel@tonic-gate  * merged are each the product of merges of half of the input files.
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * The algorithm consists of two phases, described in detail below.  The first
420Sstevel@tonic-gate  * phase entails the merging of CTF data in groups of eight.  The second phase
430Sstevel@tonic-gate  * takes the results of Phase I, and merges them two at a time.  This disparity
440Sstevel@tonic-gate  * is due to an observation that the merge time increases at least quadratically
450Sstevel@tonic-gate  * with the size of the CTF data being merged.  As such, merges of CTF graphs
460Sstevel@tonic-gate  * newly read from input files are much faster than merges of CTF graphs that
470Sstevel@tonic-gate  * are themselves the results of prior merges.
480Sstevel@tonic-gate  *
490Sstevel@tonic-gate  * A further complication is the need to ensure the repeatability of CTF merges.
500Sstevel@tonic-gate  * That is, a merge should produce the same output every time, given the same
510Sstevel@tonic-gate  * input.  In both phases, this consistency requirement is met by imposing an
520Sstevel@tonic-gate  * ordering on the merge process, thus ensuring that a given set of input files
530Sstevel@tonic-gate  * are merged in the same order every time.
540Sstevel@tonic-gate  *
550Sstevel@tonic-gate  *   Phase I
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  *   The main thread reads the input files one by one, transforming the CTF
580Sstevel@tonic-gate  *   data they contain into tdata structures.  When a given file has been read
590Sstevel@tonic-gate  *   and parsed, it is placed on the work queue for retrieval by worker threads.
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  *   Central to Phase I is the Work In Progress (wip) array, which is used to
620Sstevel@tonic-gate  *   merge batches of files in a predictable order.  Files are read by the main
630Sstevel@tonic-gate  *   thread, and are merged into wip array elements in round-robin order.  When
640Sstevel@tonic-gate  *   the number of files merged into a given array slot equals the batch size,
650Sstevel@tonic-gate  *   the merged CTF graph in that array is added to the done slot in order by
660Sstevel@tonic-gate  *   array slot.
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  *   For example, consider a case where we have five input files, a batch size
690Sstevel@tonic-gate  *   of two, a wip array size of two, and two worker threads (T1 and T2).
700Sstevel@tonic-gate  *
710Sstevel@tonic-gate  *    1. The wip array elements are assigned initial batch numbers 0 and 1.
720Sstevel@tonic-gate  *    2. T1 reads an input file from the input queue (wq_queue).  This is the
730Sstevel@tonic-gate  *       first input file, so it is placed into wip[0].  The second file is
740Sstevel@tonic-gate  *       similarly read and placed into wip[1].  The wip array slots now contain
750Sstevel@tonic-gate  *       one file each (wip_nmerged == 1).
760Sstevel@tonic-gate  *    3. T1 reads the third input file, which it merges into wip[0].  The
770Sstevel@tonic-gate  *       number of files in wip[0] is equal to the batch size.
780Sstevel@tonic-gate  *    4. T2 reads the fourth input file, which it merges into wip[1].  wip[1]
790Sstevel@tonic-gate  *       is now full too.
800Sstevel@tonic-gate  *    5. T2 attempts to place the contents of wip[1] on the done queue
810Sstevel@tonic-gate  *       (wq_done_queue), but it can't, since the batch ID for wip[1] is 1.
820Sstevel@tonic-gate  *       Batch 0 needs to be on the done queue before batch 1 can be added, so
830Sstevel@tonic-gate  *       T2 blocks on wip[1]'s cv.
840Sstevel@tonic-gate  *    6. T1 attempts to place the contents of wip[0] on the done queue, and
850Sstevel@tonic-gate  *       succeeds, updating wq_lastdonebatch to 0.  It clears wip[0], and sets
860Sstevel@tonic-gate  *       its batch ID to 2.  T1 then signals wip[1]'s cv to awaken T2.
870Sstevel@tonic-gate  *    7. T2 wakes up, notices that wq_lastdonebatch is 0, which means that
880Sstevel@tonic-gate  *       batch 1 can now be added.  It adds wip[1] to the done queue, clears
890Sstevel@tonic-gate  *       wip[1], and sets its batch ID to 3.  It signals wip[0]'s cv, and
900Sstevel@tonic-gate  *       restarts.
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  *   The above process continues until all input files have been consumed.  At
930Sstevel@tonic-gate  *   this point, a pair of barriers are used to allow a single thread to move
940Sstevel@tonic-gate  *   any partial batches from the wip array to the done array in batch ID order.
950Sstevel@tonic-gate  *   When this is complete, wq_done_queue is moved to wq_queue, and Phase II
960Sstevel@tonic-gate  *   begins.
970Sstevel@tonic-gate  *
980Sstevel@tonic-gate  *	Locking Semantics (Phase I)
990Sstevel@tonic-gate  *
1000Sstevel@tonic-gate  *	The input queue (wq_queue) and the done queue (wq_done_queue) are
1010Sstevel@tonic-gate  *	protected by separate mutexes - wq_queue_lock and wq_done_queue.  wip
1020Sstevel@tonic-gate  *	array slots are protected by their own mutexes, which must be grabbed
1030Sstevel@tonic-gate  *	before releasing the input queue lock.  The wip array lock is dropped
1040Sstevel@tonic-gate  *	when the thread restarts the loop.  If the array slot was full, the
1050Sstevel@tonic-gate  *	array lock will be held while the slot contents are added to the done
1060Sstevel@tonic-gate  *	queue.  The done queue lock is used to protect the wip slot cv's.
1070Sstevel@tonic-gate  *
1080Sstevel@tonic-gate  *	The pow number is protected by the queue lock.  The master batch ID
1090Sstevel@tonic-gate  *	and last completed batch (wq_lastdonebatch) counters are protected *in
1100Sstevel@tonic-gate  *	Phase I* by the done queue lock.
1110Sstevel@tonic-gate  *
1120Sstevel@tonic-gate  *   Phase II
1130Sstevel@tonic-gate  *
1140Sstevel@tonic-gate  *   When Phase II begins, the queue consists of the merged batches from the
1150Sstevel@tonic-gate  *   first phase.  Assume we have five batches:
1160Sstevel@tonic-gate  *
1170Sstevel@tonic-gate  *	Q:	a b c d e
1180Sstevel@tonic-gate  *
1190Sstevel@tonic-gate  *   Using the same batch ID mechanism we used in Phase I, but without the wip
1200Sstevel@tonic-gate  *   array, worker threads remove two entries at a time from the beginning of
1210Sstevel@tonic-gate  *   the queue.  These two entries are merged, and are added back to the tail
1220Sstevel@tonic-gate  *   of the queue, as follows:
1230Sstevel@tonic-gate  *
1240Sstevel@tonic-gate  *	Q:	a b c d e	# start
1250Sstevel@tonic-gate  *	Q:	c d e ab	# a, b removed, merged, added to end
1260Sstevel@tonic-gate  *	Q:	e ab cd		# c, d removed, merged, added to end
1270Sstevel@tonic-gate  *	Q:	cd eab		# e, ab removed, merged, added to end
1280Sstevel@tonic-gate  *	Q:	cdeab		# cd, eab removed, merged, added to end
1290Sstevel@tonic-gate  *
1300Sstevel@tonic-gate  *   When one entry remains on the queue, with no merges outstanding, Phase II
1310Sstevel@tonic-gate  *   finishes.  We pre-determine the stopping point by pre-calculating the
1320Sstevel@tonic-gate  *   number of nodes that will appear on the list.  In the example above, the
1330Sstevel@tonic-gate  *   number (wq_ninqueue) is 9.  When ninqueue is 1, we conclude Phase II by
1340Sstevel@tonic-gate  *   signaling the main thread via wq_done_cv.
1350Sstevel@tonic-gate  *
1360Sstevel@tonic-gate  *	Locking Semantics (Phase II)
1370Sstevel@tonic-gate  *
1380Sstevel@tonic-gate  *	The queue (wq_queue), ninqueue, and the master batch ID and last
1390Sstevel@tonic-gate  *	completed batch counters are protected by wq_queue_lock.  The done
1400Sstevel@tonic-gate  *	queue and corresponding lock are unused in Phase II as is the wip array.
1410Sstevel@tonic-gate  *
1420Sstevel@tonic-gate  *   Uniquification
1430Sstevel@tonic-gate  *
1440Sstevel@tonic-gate  *   We want the CTF data that goes into a given module to be as small as
1450Sstevel@tonic-gate  *   possible.  For example, we don't want it to contain any type data that may
1460Sstevel@tonic-gate  *   be present in another common module.  As such, after creating the master
1470Sstevel@tonic-gate  *   tdata_t for a given module, we can, if requested by the user, uniquify it
1480Sstevel@tonic-gate  *   against the tdata_t from another module (genunix in the case of the SunOS
1490Sstevel@tonic-gate  *   kernel).  We perform a merge between the tdata_t for this module and the
1500Sstevel@tonic-gate  *   tdata_t from genunix.  Nodes found in this module that are not present in
1510Sstevel@tonic-gate  *   genunix are added to a third tdata_t - the uniquified tdata_t.
1520Sstevel@tonic-gate  *
1530Sstevel@tonic-gate  *   Additive Merges
1540Sstevel@tonic-gate  *
1550Sstevel@tonic-gate  *   In some cases, for example if we are issuing a new version of a common
1560Sstevel@tonic-gate  *   module in a patch, we need to make sure that the CTF data already present
1570Sstevel@tonic-gate  *   in that module does not change.  Changes to this data would void the CTF
1580Sstevel@tonic-gate  *   data in any module that uniquified against the common module.  To preserve
1590Sstevel@tonic-gate  *   the existing data, we can perform what is known as an additive merge.  In
1600Sstevel@tonic-gate  *   this case, a final uniquification is performed against the CTF data in the
1610Sstevel@tonic-gate  *   previous version of the module.  The result will be the placement of new
1620Sstevel@tonic-gate  *   and changed data after the existing data, thus preserving the existing type
1630Sstevel@tonic-gate  *   ID space.
1640Sstevel@tonic-gate  *
1650Sstevel@tonic-gate  *   Saving the result
1660Sstevel@tonic-gate  *
1670Sstevel@tonic-gate  *   When the merges are complete, the resulting tdata_t is placed into the
1680Sstevel@tonic-gate  *   output file, replacing the .SUNW_ctf section (if any) already in that file.
1690Sstevel@tonic-gate  *
1700Sstevel@tonic-gate  * The person who changes the merging thread code in this file without updating
1710Sstevel@tonic-gate  * this comment will not live to see the stock hit five.
1720Sstevel@tonic-gate  */
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate #include <stdio.h>
1750Sstevel@tonic-gate #include <stdlib.h>
1760Sstevel@tonic-gate #include <unistd.h>
1770Sstevel@tonic-gate #include <pthread.h>
1780Sstevel@tonic-gate #include <assert.h>
1790Sstevel@tonic-gate #include <synch.h>
1800Sstevel@tonic-gate #include <signal.h>
1810Sstevel@tonic-gate #include <libgen.h>
1820Sstevel@tonic-gate #include <string.h>
1830Sstevel@tonic-gate #include <errno.h>
1840Sstevel@tonic-gate #include <alloca.h>
1850Sstevel@tonic-gate #include <sys/param.h>
1860Sstevel@tonic-gate #include <sys/types.h>
1870Sstevel@tonic-gate #include <sys/mman.h>
1880Sstevel@tonic-gate #include <sys/sysconf.h>
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate #include "ctf_headers.h"
1910Sstevel@tonic-gate #include "ctftools.h"
1920Sstevel@tonic-gate #include "ctfmerge.h"
1930Sstevel@tonic-gate #include "traverse.h"
1940Sstevel@tonic-gate #include "memory.h"
1950Sstevel@tonic-gate #include "fifo.h"
1960Sstevel@tonic-gate #include "barrier.h"
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate #pragma init(bigheap)
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate #define	MERGE_PHASE1_BATCH_SIZE		8
2010Sstevel@tonic-gate #define	MERGE_PHASE1_MAX_SLOTS		5
2020Sstevel@tonic-gate #define	MERGE_INPUT_THROTTLE_LEN	10
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate const char *progname;
2050Sstevel@tonic-gate static char *outfile = NULL;
2060Sstevel@tonic-gate static char *tmpname = NULL;
2070Sstevel@tonic-gate static int dynsym;
2080Sstevel@tonic-gate int debug_level = DEBUG_LEVEL;
2095349Skchow static size_t maxpgsize = 0x400000;
2105349Skchow 
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate void
usage(void)2130Sstevel@tonic-gate usage(void)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate 	(void) fprintf(stderr,
2160Sstevel@tonic-gate 	    "Usage: %s [-fgstv] -l label | -L labelenv -o outfile file ...\n"
2170Sstevel@tonic-gate 	    "       %s [-fgstv] -l label | -L labelenv -o outfile -d uniqfile\n"
2180Sstevel@tonic-gate 	    "       %*s [-g] [-D uniqlabel] file ...\n"
2190Sstevel@tonic-gate 	    "       %s [-fgstv] -l label | -L labelenv -o outfile -w withfile "
2200Sstevel@tonic-gate 	    "file ...\n"
2210Sstevel@tonic-gate 	    "       %s [-g] -c srcfile destfile\n"
2220Sstevel@tonic-gate 	    "\n"
2230Sstevel@tonic-gate 	    "  Note: if -L labelenv is specified and labelenv is not set in\n"
2240Sstevel@tonic-gate 	    "  the environment, a default value is used.\n",
2250Sstevel@tonic-gate 	    progname, progname, strlen(progname), " ",
2260Sstevel@tonic-gate 	    progname, progname);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate static void
bigheap(void)2300Sstevel@tonic-gate bigheap(void)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate 	size_t big, *size;
2335349Skchow 	int sizes;
2340Sstevel@tonic-gate 	struct memcntl_mha mha;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	/*
2370Sstevel@tonic-gate 	 * First, get the available pagesizes.
2380Sstevel@tonic-gate 	 */
2390Sstevel@tonic-gate 	if ((sizes = getpagesizes(NULL, 0)) == -1)
2400Sstevel@tonic-gate 		return;
2410Sstevel@tonic-gate 
2425349Skchow 	if (sizes == 1 || (size = alloca(sizeof (size_t) * sizes)) == NULL)
2430Sstevel@tonic-gate 		return;
2440Sstevel@tonic-gate 
2455349Skchow 	if (getpagesizes(size, sizes) == -1)
2460Sstevel@tonic-gate 		return;
2470Sstevel@tonic-gate 
2485349Skchow 	while (size[sizes - 1] > maxpgsize)
2495349Skchow 		sizes--;
2505349Skchow 
2515349Skchow 	/* set big to the largest allowed page size */
2520Sstevel@tonic-gate 	big = size[sizes - 1];
2530Sstevel@tonic-gate 	if (big & (big - 1)) {
2540Sstevel@tonic-gate 		/*
2550Sstevel@tonic-gate 		 * The largest page size is not a power of two for some
2560Sstevel@tonic-gate 		 * inexplicable reason; return.
2570Sstevel@tonic-gate 		 */
2580Sstevel@tonic-gate 		return;
2590Sstevel@tonic-gate 	}
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	/*
2620Sstevel@tonic-gate 	 * Now, align our break to the largest page size.
2630Sstevel@tonic-gate 	 */
2640Sstevel@tonic-gate 	if (brk((void *)((((uintptr_t)sbrk(0) - 1) & ~(big - 1)) + big)) != 0)
2650Sstevel@tonic-gate 		return;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	/*
2685349Skchow 	 * set the preferred page size for the heap
2690Sstevel@tonic-gate 	 */
2700Sstevel@tonic-gate 	mha.mha_cmd = MHA_MAPSIZE_BSSBRK;
2710Sstevel@tonic-gate 	mha.mha_flags = 0;
2725349Skchow 	mha.mha_pagesize = big;
2730Sstevel@tonic-gate 
2745349Skchow 	(void) memcntl(NULL, 0, MC_HAT_ADVISE, (caddr_t)&mha, 0, 0);
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate static void
finalize_phase_one(workqueue_t * wq)2780Sstevel@tonic-gate finalize_phase_one(workqueue_t *wq)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate 	int startslot, i;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	/*
2830Sstevel@tonic-gate 	 * wip slots are cleared out only when maxbatchsz td's have been merged
2840Sstevel@tonic-gate 	 * into them.  We're not guaranteed that the number of files we're
2850Sstevel@tonic-gate 	 * merging is a multiple of maxbatchsz, so there will be some partial
2860Sstevel@tonic-gate 	 * groups in the wip array.  Move them to the done queue in batch ID
2870Sstevel@tonic-gate 	 * order, starting with the slot containing the next batch that would
2880Sstevel@tonic-gate 	 * have been placed on the done queue, followed by the others.
2890Sstevel@tonic-gate 	 * One thread will be doing this while the others wait at the barrier
2900Sstevel@tonic-gate 	 * back in worker_thread(), so we don't need to worry about pesky things
2910Sstevel@tonic-gate 	 * like locks.
2920Sstevel@tonic-gate 	 */
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	for (startslot = -1, i = 0; i < wq->wq_nwipslots; i++) {
2950Sstevel@tonic-gate 		if (wq->wq_wip[i].wip_batchid == wq->wq_lastdonebatch + 1) {
2960Sstevel@tonic-gate 			startslot = i;
2970Sstevel@tonic-gate 			break;
2980Sstevel@tonic-gate 		}
2990Sstevel@tonic-gate 	}
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	assert(startslot != -1);
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	for (i = startslot; i < startslot + wq->wq_nwipslots; i++) {
3040Sstevel@tonic-gate 		int slotnum = i % wq->wq_nwipslots;
3050Sstevel@tonic-gate 		wip_t *wipslot = &wq->wq_wip[slotnum];
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 		if (wipslot->wip_td != NULL) {
3080Sstevel@tonic-gate 			debug(2, "clearing slot %d (%d) (saving %d)\n",
3090Sstevel@tonic-gate 			    slotnum, i, wipslot->wip_nmerged);
3100Sstevel@tonic-gate 		} else
3110Sstevel@tonic-gate 			debug(2, "clearing slot %d (%d)\n", slotnum, i);
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 		if (wipslot->wip_td != NULL) {
3140Sstevel@tonic-gate 			fifo_add(wq->wq_donequeue, wipslot->wip_td);
3150Sstevel@tonic-gate 			wq->wq_wip[slotnum].wip_td = NULL;
3160Sstevel@tonic-gate 		}
3170Sstevel@tonic-gate 	}
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	wq->wq_lastdonebatch = wq->wq_next_batchid++;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	debug(2, "phase one done: donequeue has %d items\n",
3220Sstevel@tonic-gate 	    fifo_len(wq->wq_donequeue));
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate static void
init_phase_two(workqueue_t * wq)3260Sstevel@tonic-gate init_phase_two(workqueue_t *wq)
3270Sstevel@tonic-gate {
3280Sstevel@tonic-gate 	int num;
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	/*
3310Sstevel@tonic-gate 	 * We're going to continually merge the first two entries on the queue,
3320Sstevel@tonic-gate 	 * placing the result on the end, until there's nothing left to merge.
3330Sstevel@tonic-gate 	 * At that point, everything will have been merged into one.  The
3340Sstevel@tonic-gate 	 * initial value of ninqueue needs to be equal to the total number of
3350Sstevel@tonic-gate 	 * entries that will show up on the queue, both at the start of the
3360Sstevel@tonic-gate 	 * phase and as generated by merges during the phase.
3370Sstevel@tonic-gate 	 */
3380Sstevel@tonic-gate 	wq->wq_ninqueue = num = fifo_len(wq->wq_donequeue);
3390Sstevel@tonic-gate 	while (num != 1) {
3400Sstevel@tonic-gate 		wq->wq_ninqueue += num / 2;
3410Sstevel@tonic-gate 		num = num / 2 + num % 2;
3420Sstevel@tonic-gate 	}
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	/*
3450Sstevel@tonic-gate 	 * Move the done queue to the work queue.  We won't be using the done
3460Sstevel@tonic-gate 	 * queue in phase 2.
3470Sstevel@tonic-gate 	 */
3480Sstevel@tonic-gate 	assert(fifo_len(wq->wq_queue) == 0);
3490Sstevel@tonic-gate 	fifo_free(wq->wq_queue, NULL);
3500Sstevel@tonic-gate 	wq->wq_queue = wq->wq_donequeue;
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate static void
wip_save_work(workqueue_t * wq,wip_t * slot,int slotnum)3540Sstevel@tonic-gate wip_save_work(workqueue_t *wq, wip_t *slot, int slotnum)
3550Sstevel@tonic-gate {
3560Sstevel@tonic-gate 	pthread_mutex_lock(&wq->wq_donequeue_lock);
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	while (wq->wq_lastdonebatch + 1 < slot->wip_batchid)
3590Sstevel@tonic-gate 		pthread_cond_wait(&slot->wip_cv, &wq->wq_donequeue_lock);
3600Sstevel@tonic-gate 	assert(wq->wq_lastdonebatch + 1 == slot->wip_batchid);
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	fifo_add(wq->wq_donequeue, slot->wip_td);
3630Sstevel@tonic-gate 	wq->wq_lastdonebatch++;
3640Sstevel@tonic-gate 	pthread_cond_signal(&wq->wq_wip[(slotnum + 1) %
3650Sstevel@tonic-gate 	    wq->wq_nwipslots].wip_cv);
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	/* reset the slot for next use */
3680Sstevel@tonic-gate 	slot->wip_td = NULL;
3690Sstevel@tonic-gate 	slot->wip_batchid = wq->wq_next_batchid++;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	pthread_mutex_unlock(&wq->wq_donequeue_lock);
3720Sstevel@tonic-gate }
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate static void
wip_add_work(wip_t * slot,tdata_t * pow)3750Sstevel@tonic-gate wip_add_work(wip_t *slot, tdata_t *pow)
3760Sstevel@tonic-gate {
3770Sstevel@tonic-gate 	if (slot->wip_td == NULL) {
3780Sstevel@tonic-gate 		slot->wip_td = pow;
3790Sstevel@tonic-gate 		slot->wip_nmerged = 1;
3800Sstevel@tonic-gate 	} else {
3810Sstevel@tonic-gate 		debug(2, "%d: merging %p into %p\n", pthread_self(),
3820Sstevel@tonic-gate 		    (void *)pow, (void *)slot->wip_td);
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 		merge_into_master(pow, slot->wip_td, NULL, 0);
3850Sstevel@tonic-gate 		tdata_free(pow);
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 		slot->wip_nmerged++;
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate static void
worker_runphase1(workqueue_t * wq)3920Sstevel@tonic-gate worker_runphase1(workqueue_t *wq)
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate 	wip_t *wipslot;
3950Sstevel@tonic-gate 	tdata_t *pow;
3960Sstevel@tonic-gate 	int wipslotnum, pownum;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	for (;;) {
3990Sstevel@tonic-gate 		pthread_mutex_lock(&wq->wq_queue_lock);
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 		while (fifo_empty(wq->wq_queue)) {
4020Sstevel@tonic-gate 			if (wq->wq_nomorefiles == 1) {
4030Sstevel@tonic-gate 				pthread_cond_broadcast(&wq->wq_work_avail);
4040Sstevel@tonic-gate 				pthread_mutex_unlock(&wq->wq_queue_lock);
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 				/* on to phase 2 ... */
4070Sstevel@tonic-gate 				return;
4080Sstevel@tonic-gate 			}
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 			pthread_cond_wait(&wq->wq_work_avail,
4110Sstevel@tonic-gate 			    &wq->wq_queue_lock);
4120Sstevel@tonic-gate 		}
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 		/* there's work to be done! */
4150Sstevel@tonic-gate 		pow = fifo_remove(wq->wq_queue);
4160Sstevel@tonic-gate 		pownum = wq->wq_nextpownum++;
4170Sstevel@tonic-gate 		pthread_cond_broadcast(&wq->wq_work_removed);
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 		assert(pow != NULL);
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 		/* merge it into the right slot */
4220Sstevel@tonic-gate 		wipslotnum = pownum % wq->wq_nwipslots;
4230Sstevel@tonic-gate 		wipslot = &wq->wq_wip[wipslotnum];
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 		pthread_mutex_lock(&wipslot->wip_lock);
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 		pthread_mutex_unlock(&wq->wq_queue_lock);
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 		wip_add_work(wipslot, pow);
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 		if (wipslot->wip_nmerged == wq->wq_maxbatchsz)
4320Sstevel@tonic-gate 			wip_save_work(wq, wipslot, wipslotnum);
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 		pthread_mutex_unlock(&wipslot->wip_lock);
4350Sstevel@tonic-gate 	}
4360Sstevel@tonic-gate }
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate static void
worker_runphase2(workqueue_t * wq)4390Sstevel@tonic-gate worker_runphase2(workqueue_t *wq)
4400Sstevel@tonic-gate {
4410Sstevel@tonic-gate 	tdata_t *pow1, *pow2;
4420Sstevel@tonic-gate 	int batchid;
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	for (;;) {
4450Sstevel@tonic-gate 		pthread_mutex_lock(&wq->wq_queue_lock);
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 		if (wq->wq_ninqueue == 1) {
4480Sstevel@tonic-gate 			pthread_cond_broadcast(&wq->wq_work_avail);
4490Sstevel@tonic-gate 			pthread_mutex_unlock(&wq->wq_queue_lock);
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 			debug(2, "%d: entering p2 completion barrier\n",
4520Sstevel@tonic-gate 			    pthread_self());
4530Sstevel@tonic-gate 			if (barrier_wait(&wq->wq_bar1)) {
4540Sstevel@tonic-gate 				pthread_mutex_lock(&wq->wq_queue_lock);
4550Sstevel@tonic-gate 				wq->wq_alldone = 1;
4560Sstevel@tonic-gate 				pthread_cond_signal(&wq->wq_alldone_cv);
4570Sstevel@tonic-gate 				pthread_mutex_unlock(&wq->wq_queue_lock);
4580Sstevel@tonic-gate 			}
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 			return;
4610Sstevel@tonic-gate 		}
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 		if (fifo_len(wq->wq_queue) < 2) {
4640Sstevel@tonic-gate 			pthread_cond_wait(&wq->wq_work_avail,
4650Sstevel@tonic-gate 			    &wq->wq_queue_lock);
4660Sstevel@tonic-gate 			pthread_mutex_unlock(&wq->wq_queue_lock);
4670Sstevel@tonic-gate 			continue;
4680Sstevel@tonic-gate 		}
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 		/* there's work to be done! */
4710Sstevel@tonic-gate 		pow1 = fifo_remove(wq->wq_queue);
4720Sstevel@tonic-gate 		pow2 = fifo_remove(wq->wq_queue);
4730Sstevel@tonic-gate 		wq->wq_ninqueue -= 2;
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 		batchid = wq->wq_next_batchid++;
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 		pthread_mutex_unlock(&wq->wq_queue_lock);
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 		debug(2, "%d: merging %p into %p\n", pthread_self(),
4800Sstevel@tonic-gate 		    (void *)pow1, (void *)pow2);
4810Sstevel@tonic-gate 		merge_into_master(pow1, pow2, NULL, 0);
4820Sstevel@tonic-gate 		tdata_free(pow1);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 		/*
4850Sstevel@tonic-gate 		 * merging is complete.  place at the tail of the queue in
4860Sstevel@tonic-gate 		 * proper order.
4870Sstevel@tonic-gate 		 */
4880Sstevel@tonic-gate 		pthread_mutex_lock(&wq->wq_queue_lock);
4890Sstevel@tonic-gate 		while (wq->wq_lastdonebatch + 1 != batchid) {
4900Sstevel@tonic-gate 			pthread_cond_wait(&wq->wq_done_cv,
4910Sstevel@tonic-gate 			    &wq->wq_queue_lock);
4920Sstevel@tonic-gate 		}
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 		wq->wq_lastdonebatch = batchid;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 		fifo_add(wq->wq_queue, pow2);
4970Sstevel@tonic-gate 		debug(2, "%d: added %p to queue, len now %d, ninqueue %d\n",
4980Sstevel@tonic-gate 		    pthread_self(), (void *)pow2, fifo_len(wq->wq_queue),
4990Sstevel@tonic-gate 		    wq->wq_ninqueue);
5000Sstevel@tonic-gate 		pthread_cond_broadcast(&wq->wq_done_cv);
5010Sstevel@tonic-gate 		pthread_cond_signal(&wq->wq_work_avail);
5020Sstevel@tonic-gate 		pthread_mutex_unlock(&wq->wq_queue_lock);
5030Sstevel@tonic-gate 	}
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate /*
5070Sstevel@tonic-gate  * Main loop for worker threads.
5080Sstevel@tonic-gate  */
5090Sstevel@tonic-gate static void
worker_thread(workqueue_t * wq)5100Sstevel@tonic-gate worker_thread(workqueue_t *wq)
5110Sstevel@tonic-gate {
5120Sstevel@tonic-gate 	worker_runphase1(wq);
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	debug(2, "%d: entering first barrier\n", pthread_self());
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	if (barrier_wait(&wq->wq_bar1)) {
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 		debug(2, "%d: doing work in first barrier\n", pthread_self());
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 		finalize_phase_one(wq);
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 		init_phase_two(wq);
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 		debug(2, "%d: ninqueue is %d, %d on queue\n", pthread_self(),
5250Sstevel@tonic-gate 		    wq->wq_ninqueue, fifo_len(wq->wq_queue));
5260Sstevel@tonic-gate 	}
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	debug(2, "%d: entering second barrier\n", pthread_self());
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	(void) barrier_wait(&wq->wq_bar2);
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	debug(2, "%d: phase 1 complete\n", pthread_self());
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	worker_runphase2(wq);
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate /*
5380Sstevel@tonic-gate  * Pass a tdata_t tree, built from an input file, off to the work queue for
5390Sstevel@tonic-gate  * consumption by worker threads.
5400Sstevel@tonic-gate  */
5410Sstevel@tonic-gate static int
merge_ctf_cb(tdata_t * td,char * name,void * arg)5420Sstevel@tonic-gate merge_ctf_cb(tdata_t *td, char *name, void *arg)
5430Sstevel@tonic-gate {
5440Sstevel@tonic-gate 	workqueue_t *wq = arg;
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	debug(3, "Adding tdata %p for processing\n", (void *)td);
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	pthread_mutex_lock(&wq->wq_queue_lock);
5490Sstevel@tonic-gate 	while (fifo_len(wq->wq_queue) > wq->wq_ithrottle) {
5500Sstevel@tonic-gate 		debug(2, "Throttling input (len = %d, throttle = %d)\n",
5510Sstevel@tonic-gate 		    fifo_len(wq->wq_queue), wq->wq_ithrottle);
5520Sstevel@tonic-gate 		pthread_cond_wait(&wq->wq_work_removed, &wq->wq_queue_lock);
5530Sstevel@tonic-gate 	}
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	fifo_add(wq->wq_queue, td);
5560Sstevel@tonic-gate 	debug(1, "Thread %d announcing %s\n", pthread_self(), name);
5570Sstevel@tonic-gate 	pthread_cond_broadcast(&wq->wq_work_avail);
5580Sstevel@tonic-gate 	pthread_mutex_unlock(&wq->wq_queue_lock);
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	return (1);
5610Sstevel@tonic-gate }
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate /*
5640Sstevel@tonic-gate  * This program is intended to be invoked from a Makefile, as part of the build.
5650Sstevel@tonic-gate  * As such, in the event of a failure or user-initiated interrupt (^C), we need
5660Sstevel@tonic-gate  * to ensure that a subsequent re-make will cause ctfmerge to be executed again.
5670Sstevel@tonic-gate  * Unfortunately, ctfmerge will usually be invoked directly after (and as part
5680Sstevel@tonic-gate  * of the same Makefile rule as) a link, and will operate on the linked file
5690Sstevel@tonic-gate  * in place.  If we merely exit upon receipt of a SIGINT, a subsequent make
5700Sstevel@tonic-gate  * will notice that the *linked* file is newer than the object files, and thus
5710Sstevel@tonic-gate  * will not reinvoke ctfmerge.  The only way to ensure that a subsequent make
5720Sstevel@tonic-gate  * reinvokes ctfmerge, is to remove the file to which we are adding CTF
5730Sstevel@tonic-gate  * data (confusingly named the output file).  This means that the link will need
5740Sstevel@tonic-gate  * to happen again, but links are generally fast, and we can't allow the merge
5750Sstevel@tonic-gate  * to be skipped.
5760Sstevel@tonic-gate  *
5770Sstevel@tonic-gate  * Another possibility would be to block SIGINT entirely - to always run to
5780Sstevel@tonic-gate  * completion.  The run time of ctfmerge can, however, be measured in minutes
5790Sstevel@tonic-gate  * in some cases, so this is not a valid option.
5800Sstevel@tonic-gate  */
5810Sstevel@tonic-gate static void
handle_sig(int sig)5820Sstevel@tonic-gate handle_sig(int sig)
5830Sstevel@tonic-gate {
5840Sstevel@tonic-gate 	terminate("Caught signal %d - exiting\n", sig);
5850Sstevel@tonic-gate }
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate static void
terminate_cleanup(void)5880Sstevel@tonic-gate terminate_cleanup(void)
5890Sstevel@tonic-gate {
5900Sstevel@tonic-gate 	int dounlink = getenv("CTFMERGE_TERMINATE_NO_UNLINK") ? 0 : 1;
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	if (tmpname != NULL && dounlink)
5930Sstevel@tonic-gate 		unlink(tmpname);
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	if (outfile == NULL)
5960Sstevel@tonic-gate 		return;
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	if (dounlink) {
5990Sstevel@tonic-gate 		fprintf(stderr, "Removing %s\n", outfile);
6000Sstevel@tonic-gate 		unlink(outfile);
6010Sstevel@tonic-gate 	}
6020Sstevel@tonic-gate }
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate static void
copy_ctf_data(char * srcfile,char * destfile,int keep_stabs)6050Sstevel@tonic-gate copy_ctf_data(char *srcfile, char *destfile, int keep_stabs)
6060Sstevel@tonic-gate {
6070Sstevel@tonic-gate 	tdata_t *srctd;
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	if (read_ctf(&srcfile, 1, NULL, read_ctf_save_cb, &srctd, 1) == 0)
6100Sstevel@tonic-gate 		terminate("No CTF data found in source file %s\n", srcfile);
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	tmpname = mktmpname(destfile, ".ctf");
6130Sstevel@tonic-gate 	write_ctf(srctd, destfile, tmpname, CTF_COMPRESS | keep_stabs);
6140Sstevel@tonic-gate 	if (rename(tmpname, destfile) != 0) {
6150Sstevel@tonic-gate 		terminate("Couldn't rename temp file %s to %s", tmpname,
6160Sstevel@tonic-gate 		    destfile);
6170Sstevel@tonic-gate 	}
6180Sstevel@tonic-gate 	free(tmpname);
6190Sstevel@tonic-gate 	tdata_free(srctd);
6200Sstevel@tonic-gate }
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate static void
wq_init(workqueue_t * wq,int nfiles)6230Sstevel@tonic-gate wq_init(workqueue_t *wq, int nfiles)
6240Sstevel@tonic-gate {
6250Sstevel@tonic-gate 	int throttle, nslots, i;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	if (getenv("CTFMERGE_MAX_SLOTS"))
6280Sstevel@tonic-gate 		nslots = atoi(getenv("CTFMERGE_MAX_SLOTS"));
6290Sstevel@tonic-gate 	else
6300Sstevel@tonic-gate 		nslots = MERGE_PHASE1_MAX_SLOTS;
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	if (getenv("CTFMERGE_PHASE1_BATCH_SIZE"))
6330Sstevel@tonic-gate 		wq->wq_maxbatchsz = atoi(getenv("CTFMERGE_PHASE1_BATCH_SIZE"));
6340Sstevel@tonic-gate 	else
6350Sstevel@tonic-gate 		wq->wq_maxbatchsz = MERGE_PHASE1_BATCH_SIZE;
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	nslots = MIN(nslots, (nfiles + wq->wq_maxbatchsz - 1) /
6380Sstevel@tonic-gate 	    wq->wq_maxbatchsz);
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	wq->wq_wip = xcalloc(sizeof (wip_t) * nslots);
6410Sstevel@tonic-gate 	wq->wq_nwipslots = nslots;
6420Sstevel@tonic-gate 	wq->wq_nthreads = MIN(sysconf(_SC_NPROCESSORS_ONLN) * 3 / 2, nslots);
643*6936Ssommerfe 	wq->wq_thread = xmalloc(sizeof (pthread_t) * wq->wq_nthreads);
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	if (getenv("CTFMERGE_INPUT_THROTTLE"))
6460Sstevel@tonic-gate 		throttle = atoi(getenv("CTFMERGE_INPUT_THROTTLE"));
6470Sstevel@tonic-gate 	else
6480Sstevel@tonic-gate 		throttle = MERGE_INPUT_THROTTLE_LEN;
6490Sstevel@tonic-gate 	wq->wq_ithrottle = throttle * wq->wq_nthreads;
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	debug(1, "Using %d slots, %d threads\n", wq->wq_nwipslots,
6520Sstevel@tonic-gate 	    wq->wq_nthreads);
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	wq->wq_next_batchid = 0;
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	for (i = 0; i < nslots; i++) {
6570Sstevel@tonic-gate 		pthread_mutex_init(&wq->wq_wip[i].wip_lock, NULL);
6580Sstevel@tonic-gate 		wq->wq_wip[i].wip_batchid = wq->wq_next_batchid++;
6590Sstevel@tonic-gate 	}
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	pthread_mutex_init(&wq->wq_queue_lock, NULL);
6620Sstevel@tonic-gate 	wq->wq_queue = fifo_new();
6630Sstevel@tonic-gate 	pthread_cond_init(&wq->wq_work_avail, NULL);
6640Sstevel@tonic-gate 	pthread_cond_init(&wq->wq_work_removed, NULL);
6650Sstevel@tonic-gate 	wq->wq_ninqueue = nfiles;
6660Sstevel@tonic-gate 	wq->wq_nextpownum = 0;
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	pthread_mutex_init(&wq->wq_donequeue_lock, NULL);
6690Sstevel@tonic-gate 	wq->wq_donequeue = fifo_new();
6700Sstevel@tonic-gate 	wq->wq_lastdonebatch = -1;
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	pthread_cond_init(&wq->wq_done_cv, NULL);
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	pthread_cond_init(&wq->wq_alldone_cv, NULL);
6750Sstevel@tonic-gate 	wq->wq_alldone = 0;
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	barrier_init(&wq->wq_bar1, wq->wq_nthreads);
6780Sstevel@tonic-gate 	barrier_init(&wq->wq_bar2, wq->wq_nthreads);
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	wq->wq_nomorefiles = 0;
6810Sstevel@tonic-gate }
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate static void
start_threads(workqueue_t * wq)6840Sstevel@tonic-gate start_threads(workqueue_t *wq)
6850Sstevel@tonic-gate {
6860Sstevel@tonic-gate 	sigset_t sets;
6870Sstevel@tonic-gate 	int i;
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	sigemptyset(&sets);
6900Sstevel@tonic-gate 	sigaddset(&sets, SIGINT);
6910Sstevel@tonic-gate 	sigaddset(&sets, SIGQUIT);
6920Sstevel@tonic-gate 	sigaddset(&sets, SIGTERM);
6930Sstevel@tonic-gate 	pthread_sigmask(SIG_BLOCK, &sets, NULL);
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	for (i = 0; i < wq->wq_nthreads; i++) {
696*6936Ssommerfe 		pthread_create(&wq->wq_thread[i], NULL,
697*6936Ssommerfe 		    (void *(*)(void *))worker_thread, wq);
6980Sstevel@tonic-gate 	}
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	sigset(SIGINT, handle_sig);
7010Sstevel@tonic-gate 	sigset(SIGQUIT, handle_sig);
7020Sstevel@tonic-gate 	sigset(SIGTERM, handle_sig);
7030Sstevel@tonic-gate 	pthread_sigmask(SIG_UNBLOCK, &sets, NULL);
7040Sstevel@tonic-gate }
7050Sstevel@tonic-gate 
706*6936Ssommerfe static void
join_threads(workqueue_t * wq)707*6936Ssommerfe join_threads(workqueue_t *wq)
708*6936Ssommerfe {
709*6936Ssommerfe 	int i;
710*6936Ssommerfe 
711*6936Ssommerfe 	for (i = 0; i < wq->wq_nthreads; i++) {
712*6936Ssommerfe 		pthread_join(wq->wq_thread[i], NULL);
713*6936Ssommerfe 	}
714*6936Ssommerfe }
715*6936Ssommerfe 
7160Sstevel@tonic-gate static int
strcompare(const void * p1,const void * p2)7170Sstevel@tonic-gate strcompare(const void *p1, const void *p2)
7180Sstevel@tonic-gate {
7190Sstevel@tonic-gate 	char *s1 = *((char **)p1);
7200Sstevel@tonic-gate 	char *s2 = *((char **)p2);
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	return (strcmp(s1, s2));
7230Sstevel@tonic-gate }
7240Sstevel@tonic-gate 
725*6936Ssommerfe /*
726*6936Ssommerfe  * Core work queue structure; passed to worker threads on thread creation
727*6936Ssommerfe  * as the main point of coordination.  Allocate as a static structure; we
728*6936Ssommerfe  * could have put this into a local variable in main, but passing a pointer
729*6936Ssommerfe  * into your stack to another thread is fragile at best and leads to some
730*6936Ssommerfe  * hard-to-debug failure modes.
731*6936Ssommerfe  */
732*6936Ssommerfe static workqueue_t wq;
733*6936Ssommerfe 
7340Sstevel@tonic-gate int
main(int argc,char ** argv)7350Sstevel@tonic-gate main(int argc, char **argv)
7360Sstevel@tonic-gate {
7370Sstevel@tonic-gate 	tdata_t *mstrtd, *savetd;
7380Sstevel@tonic-gate 	char *uniqfile = NULL, *uniqlabel = NULL;
7390Sstevel@tonic-gate 	char *withfile = NULL;
7400Sstevel@tonic-gate 	char *label = NULL;
7410Sstevel@tonic-gate 	char **ifiles, **tifiles;
7420Sstevel@tonic-gate 	int verbose = 0, docopy = 0;
7430Sstevel@tonic-gate 	int write_fuzzy_match = 0;
7440Sstevel@tonic-gate 	int keep_stabs = 0;
7450Sstevel@tonic-gate 	int require_ctf = 0;
7460Sstevel@tonic-gate 	int nifiles, nielems;
7470Sstevel@tonic-gate 	int c, i, idx, tidx, err;
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	progname = basename(argv[0]);
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	if (getenv("CTFMERGE_DEBUG_LEVEL"))
7520Sstevel@tonic-gate 		debug_level = atoi(getenv("CTFMERGE_DEBUG_LEVEL"));
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	err = 0;
7550Sstevel@tonic-gate 	while ((c = getopt(argc, argv, ":cd:D:fgl:L:o:tvw:s")) != EOF) {
7560Sstevel@tonic-gate 		switch (c) {
7570Sstevel@tonic-gate 		case 'c':
7580Sstevel@tonic-gate 			docopy = 1;
7590Sstevel@tonic-gate 			break;
7600Sstevel@tonic-gate 		case 'd':
7610Sstevel@tonic-gate 			/* Uniquify against `uniqfile' */
7620Sstevel@tonic-gate 			uniqfile = optarg;
7630Sstevel@tonic-gate 			break;
7640Sstevel@tonic-gate 		case 'D':
7650Sstevel@tonic-gate 			/* Uniquify against label `uniqlabel' in `uniqfile' */
7660Sstevel@tonic-gate 			uniqlabel = optarg;
7670Sstevel@tonic-gate 			break;
7680Sstevel@tonic-gate 		case 'f':
7690Sstevel@tonic-gate 			write_fuzzy_match = CTF_FUZZY_MATCH;
7700Sstevel@tonic-gate 			break;
7710Sstevel@tonic-gate 		case 'g':
7720Sstevel@tonic-gate 			keep_stabs = CTF_KEEP_STABS;
7730Sstevel@tonic-gate 			break;
7740Sstevel@tonic-gate 		case 'l':
7750Sstevel@tonic-gate 			/* Label merged types with `label' */
7760Sstevel@tonic-gate 			label = optarg;
7770Sstevel@tonic-gate 			break;
7780Sstevel@tonic-gate 		case 'L':
7790Sstevel@tonic-gate 			/* Label merged types with getenv(`label`) */
7800Sstevel@tonic-gate 			if ((label = getenv(optarg)) == NULL)
7810Sstevel@tonic-gate 				label = CTF_DEFAULT_LABEL;
7820Sstevel@tonic-gate 			break;
7830Sstevel@tonic-gate 		case 'o':
7840Sstevel@tonic-gate 			/* Place merged types in CTF section in `outfile' */
7850Sstevel@tonic-gate 			outfile = optarg;
7860Sstevel@tonic-gate 			break;
7870Sstevel@tonic-gate 		case 't':
7880Sstevel@tonic-gate 			/* Insist *all* object files built from C have CTF */
7890Sstevel@tonic-gate 			require_ctf = 1;
7900Sstevel@tonic-gate 			break;
7910Sstevel@tonic-gate 		case 'v':
7920Sstevel@tonic-gate 			/* More debugging information */
7930Sstevel@tonic-gate 			verbose = 1;
7940Sstevel@tonic-gate 			break;
7950Sstevel@tonic-gate 		case 'w':
7960Sstevel@tonic-gate 			/* Additive merge with data from `withfile' */
7970Sstevel@tonic-gate 			withfile = optarg;
7980Sstevel@tonic-gate 			break;
7990Sstevel@tonic-gate 		case 's':
8000Sstevel@tonic-gate 			/* use the dynsym rather than the symtab */
8010Sstevel@tonic-gate 			dynsym = CTF_USE_DYNSYM;
8020Sstevel@tonic-gate 			break;
8030Sstevel@tonic-gate 		default:
8040Sstevel@tonic-gate 			usage();
8050Sstevel@tonic-gate 			exit(2);
8060Sstevel@tonic-gate 		}
8070Sstevel@tonic-gate 	}
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	/* Validate arguments */
8100Sstevel@tonic-gate 	if (docopy) {
8110Sstevel@tonic-gate 		if (uniqfile != NULL || uniqlabel != NULL || label != NULL ||
8120Sstevel@tonic-gate 		    outfile != NULL || withfile != NULL || dynsym != 0)
8130Sstevel@tonic-gate 			err++;
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 		if (argc - optind != 2)
8160Sstevel@tonic-gate 			err++;
8170Sstevel@tonic-gate 	} else {
8180Sstevel@tonic-gate 		if (uniqfile != NULL && withfile != NULL)
8190Sstevel@tonic-gate 			err++;
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 		if (uniqlabel != NULL && uniqfile == NULL)
8220Sstevel@tonic-gate 			err++;
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 		if (outfile == NULL || label == NULL)
8250Sstevel@tonic-gate 			err++;
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 		if (argc - optind == 0)
8280Sstevel@tonic-gate 			err++;
8290Sstevel@tonic-gate 	}
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	if (err) {
8320Sstevel@tonic-gate 		usage();
8330Sstevel@tonic-gate 		exit(2);
8340Sstevel@tonic-gate 	}
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	if (getenv("STRIPSTABS_KEEP_STABS") != NULL)
8370Sstevel@tonic-gate 		keep_stabs = CTF_KEEP_STABS;
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	if (uniqfile && access(uniqfile, R_OK) != 0) {
8400Sstevel@tonic-gate 		warning("Uniquification file %s couldn't be opened and "
8410Sstevel@tonic-gate 		    "will be ignored.\n", uniqfile);
8420Sstevel@tonic-gate 		uniqfile = NULL;
8430Sstevel@tonic-gate 	}
8440Sstevel@tonic-gate 	if (withfile && access(withfile, R_OK) != 0) {
8450Sstevel@tonic-gate 		warning("With file %s couldn't be opened and will be "
8460Sstevel@tonic-gate 		    "ignored.\n", withfile);
8470Sstevel@tonic-gate 		withfile = NULL;
8480Sstevel@tonic-gate 	}
8490Sstevel@tonic-gate 	if (outfile && access(outfile, R_OK|W_OK) != 0)
8500Sstevel@tonic-gate 		terminate("Cannot open output file %s for r/w", outfile);
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	/*
8530Sstevel@tonic-gate 	 * This is ugly, but we don't want to have to have a separate tool
8540Sstevel@tonic-gate 	 * (yet) just for copying an ELF section with our specific requirements,
8550Sstevel@tonic-gate 	 * so we shoe-horn a copier into ctfmerge.
8560Sstevel@tonic-gate 	 */
8570Sstevel@tonic-gate 	if (docopy) {
8580Sstevel@tonic-gate 		copy_ctf_data(argv[optind], argv[optind + 1], keep_stabs);
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 		exit(0);
8610Sstevel@tonic-gate 	}
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	set_terminate_cleanup(terminate_cleanup);
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 	/* Sort the input files and strip out duplicates */
8660Sstevel@tonic-gate 	nifiles = argc - optind;
8670Sstevel@tonic-gate 	ifiles = xmalloc(sizeof (char *) * nifiles);
8680Sstevel@tonic-gate 	tifiles = xmalloc(sizeof (char *) * nifiles);
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	for (i = 0; i < nifiles; i++)
8710Sstevel@tonic-gate 		tifiles[i] = argv[optind + i];
8720Sstevel@tonic-gate 	qsort(tifiles, nifiles, sizeof (char *), (int (*)())strcompare);
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	ifiles[0] = tifiles[0];
8750Sstevel@tonic-gate 	for (idx = 0, tidx = 1; tidx < nifiles; tidx++) {
8760Sstevel@tonic-gate 		if (strcmp(ifiles[idx], tifiles[tidx]) != 0)
8770Sstevel@tonic-gate 			ifiles[++idx] = tifiles[tidx];
8780Sstevel@tonic-gate 	}
8790Sstevel@tonic-gate 	nifiles = idx + 1;
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	/* Make sure they all exist */
8820Sstevel@tonic-gate 	if ((nielems = count_files(ifiles, nifiles)) < 0)
8830Sstevel@tonic-gate 		terminate("Some input files were inaccessible\n");
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	/* Prepare for the merge */
8860Sstevel@tonic-gate 	wq_init(&wq, nielems);
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	start_threads(&wq);
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	/*
8910Sstevel@tonic-gate 	 * Start the merge
8920Sstevel@tonic-gate 	 *
8930Sstevel@tonic-gate 	 * We're reading everything from each of the object files, so we
8940Sstevel@tonic-gate 	 * don't need to specify labels.
8950Sstevel@tonic-gate 	 */
8960Sstevel@tonic-gate 	if (read_ctf(ifiles, nifiles, NULL, merge_ctf_cb,
8973446Smrj 	    &wq, require_ctf) == 0) {
8983446Smrj 		/*
8993446Smrj 		 * If we're verifying that C files have CTF, it's safe to
9003446Smrj 		 * assume that in this case, we're building only from assembly
9013446Smrj 		 * inputs.
9023446Smrj 		 */
9033446Smrj 		if (require_ctf)
9043446Smrj 			exit(0);
9050Sstevel@tonic-gate 		terminate("No ctf sections found to merge\n");
9063446Smrj 	}
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	pthread_mutex_lock(&wq.wq_queue_lock);
9090Sstevel@tonic-gate 	wq.wq_nomorefiles = 1;
9100Sstevel@tonic-gate 	pthread_cond_broadcast(&wq.wq_work_avail);
9110Sstevel@tonic-gate 	pthread_mutex_unlock(&wq.wq_queue_lock);
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 	pthread_mutex_lock(&wq.wq_queue_lock);
9140Sstevel@tonic-gate 	while (wq.wq_alldone == 0)
9150Sstevel@tonic-gate 		pthread_cond_wait(&wq.wq_alldone_cv, &wq.wq_queue_lock);
9160Sstevel@tonic-gate 	pthread_mutex_unlock(&wq.wq_queue_lock);
9170Sstevel@tonic-gate 
918*6936Ssommerfe 	join_threads(&wq);
919*6936Ssommerfe 
9200Sstevel@tonic-gate 	/*
9210Sstevel@tonic-gate 	 * All requested files have been merged, with the resulting tree in
9220Sstevel@tonic-gate 	 * mstrtd.  savetd is the tree that will be placed into the output file.
9230Sstevel@tonic-gate 	 *
9240Sstevel@tonic-gate 	 * Regardless of whether we're doing a normal uniquification or an
9250Sstevel@tonic-gate 	 * additive merge, we need a type tree that has been uniquified
9260Sstevel@tonic-gate 	 * against uniqfile or withfile, as appropriate.
9270Sstevel@tonic-gate 	 *
9280Sstevel@tonic-gate 	 * If we're doing a uniquification, we stuff the resulting tree into
9290Sstevel@tonic-gate 	 * outfile.  Otherwise, we add the tree to the tree already in withfile.
9300Sstevel@tonic-gate 	 */
9310Sstevel@tonic-gate 	assert(fifo_len(wq.wq_queue) == 1);
9320Sstevel@tonic-gate 	mstrtd = fifo_remove(wq.wq_queue);
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	if (verbose || debug_level) {
9350Sstevel@tonic-gate 		debug(2, "Statistics for td %p\n", (void *)mstrtd);
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 		iidesc_stats(mstrtd->td_iihash);
9380Sstevel@tonic-gate 	}
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 	if (uniqfile != NULL || withfile != NULL) {
9410Sstevel@tonic-gate 		char *reffile, *reflabel = NULL;
9420Sstevel@tonic-gate 		tdata_t *reftd;
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 		if (uniqfile != NULL) {
9450Sstevel@tonic-gate 			reffile = uniqfile;
9460Sstevel@tonic-gate 			reflabel = uniqlabel;
9470Sstevel@tonic-gate 		} else
9480Sstevel@tonic-gate 			reffile = withfile;
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 		if (read_ctf(&reffile, 1, reflabel, read_ctf_save_cb,
9510Sstevel@tonic-gate 		    &reftd, require_ctf) == 0) {
9520Sstevel@tonic-gate 			terminate("No CTF data found in reference file %s\n",
9530Sstevel@tonic-gate 			    reffile);
9540Sstevel@tonic-gate 		}
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 		savetd = tdata_new();
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 		if (CTF_TYPE_ISCHILD(reftd->td_nextid))
9590Sstevel@tonic-gate 			terminate("No room for additional types in master\n");
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 		savetd->td_nextid = withfile ? reftd->td_nextid :
9620Sstevel@tonic-gate 		    CTF_INDEX_TO_TYPE(1, TRUE);
9630Sstevel@tonic-gate 		merge_into_master(mstrtd, reftd, savetd, 0);
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 		tdata_label_add(savetd, label, CTF_LABEL_LASTIDX);
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 		if (withfile) {
9680Sstevel@tonic-gate 			/*
9690Sstevel@tonic-gate 			 * savetd holds the new data to be added to the withfile
9700Sstevel@tonic-gate 			 */
9710Sstevel@tonic-gate 			tdata_t *withtd = reftd;
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 			tdata_merge(withtd, savetd);
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 			savetd = withtd;
9760Sstevel@tonic-gate 		} else {
9770Sstevel@tonic-gate 			char uniqname[MAXPATHLEN];
9780Sstevel@tonic-gate 			labelent_t *parle;
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 			parle = tdata_label_top(reftd);
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 			savetd->td_parlabel = xstrdup(parle->le_name);
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 			strncpy(uniqname, reffile, sizeof (uniqname));
9850Sstevel@tonic-gate 			uniqname[MAXPATHLEN - 1] = '\0';
9860Sstevel@tonic-gate 			savetd->td_parname = xstrdup(basename(uniqname));
9870Sstevel@tonic-gate 		}
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	} else {
9900Sstevel@tonic-gate 		/*
9910Sstevel@tonic-gate 		 * No post processing.  Write the merged tree as-is into the
9920Sstevel@tonic-gate 		 * output file.
9930Sstevel@tonic-gate 		 */
9940Sstevel@tonic-gate 		tdata_label_free(mstrtd);
9950Sstevel@tonic-gate 		tdata_label_add(mstrtd, label, CTF_LABEL_LASTIDX);
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 		savetd = mstrtd;
9980Sstevel@tonic-gate 	}
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	tmpname = mktmpname(outfile, ".ctf");
10010Sstevel@tonic-gate 	write_ctf(savetd, outfile, tmpname,
10020Sstevel@tonic-gate 	    CTF_COMPRESS | write_fuzzy_match | dynsym | keep_stabs);
10030Sstevel@tonic-gate 	if (rename(tmpname, outfile) != 0)
10040Sstevel@tonic-gate 		terminate("Couldn't rename output temp file %s", tmpname);
10050Sstevel@tonic-gate 	free(tmpname);
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 	return (0);
10080Sstevel@tonic-gate }
1009