1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26#pragma ident	"@(#)ctfmerge.c	1.19	08/06/20 SMI"
27
28/*
29 * Given several files containing CTF data, merge and uniquify that data into
30 * a single CTF section in an output file.
31 *
32 * Merges can proceed independently.  As such, we perform the merges in parallel
33 * using a worker thread model.  A given glob of CTF data (either all of the CTF
34 * data from a single input file, or the result of one or more merges) can only
35 * be involved in a single merge at any given time, so the process decreases in
36 * parallelism, especially towards the end, as more and more files are
37 * consolidated, finally resulting in a single merge of two large CTF graphs.
38 * Unfortunately, the last merge is also the slowest, as the two graphs being
39 * merged are each the product of merges of half of the input files.
40 *
41 * The algorithm consists of two phases, described in detail below.  The first
42 * phase entails the merging of CTF data in groups of eight.  The second phase
43 * takes the results of Phase I, and merges them two at a time.  This disparity
44 * is due to an observation that the merge time increases at least quadratically
45 * with the size of the CTF data being merged.  As such, merges of CTF graphs
46 * newly read from input files are much faster than merges of CTF graphs that
47 * are themselves the results of prior merges.
48 *
49 * A further complication is the need to ensure the repeatability of CTF merges.
50 * That is, a merge should produce the same output every time, given the same
51 * input.  In both phases, this consistency requirement is met by imposing an
52 * ordering on the merge process, thus ensuring that a given set of input files
53 * are merged in the same order every time.
54 *
55 *   Phase I
56 *
57 *   The main thread reads the input files one by one, transforming the CTF
58 *   data they contain into tdata structures.  When a given file has been read
59 *   and parsed, it is placed on the work queue for retrieval by worker threads.
60 *
61 *   Central to Phase I is the Work In Progress (wip) array, which is used to
62 *   merge batches of files in a predictable order.  Files are read by the main
63 *   thread, and are merged into wip array elements in round-robin order.  When
64 *   the number of files merged into a given array slot equals the batch size,
65 *   the merged CTF graph in that array is added to the done slot in order by
66 *   array slot.
67 *
68 *   For example, consider a case where we have five input files, a batch size
69 *   of two, a wip array size of two, and two worker threads (T1 and T2).
70 *
71 *    1. The wip array elements are assigned initial batch numbers 0 and 1.
72 *    2. T1 reads an input file from the input queue (wq_queue).  This is the
73 *       first input file, so it is placed into wip[0].  The second file is
74 *       similarly read and placed into wip[1].  The wip array slots now contain
75 *       one file each (wip_nmerged == 1).
76 *    3. T1 reads the third input file, which it merges into wip[0].  The
77 *       number of files in wip[0] is equal to the batch size.
78 *    4. T2 reads the fourth input file, which it merges into wip[1].  wip[1]
79 *       is now full too.
80 *    5. T2 attempts to place the contents of wip[1] on the done queue
81 *       (wq_done_queue), but it can't, since the batch ID for wip[1] is 1.
82 *       Batch 0 needs to be on the done queue before batch 1 can be added, so
83 *       T2 blocks on wip[1]'s cv.
84 *    6. T1 attempts to place the contents of wip[0] on the done queue, and
85 *       succeeds, updating wq_lastdonebatch to 0.  It clears wip[0], and sets
86 *       its batch ID to 2.  T1 then signals wip[1]'s cv to awaken T2.
87 *    7. T2 wakes up, notices that wq_lastdonebatch is 0, which means that
88 *       batch 1 can now be added.  It adds wip[1] to the done queue, clears
89 *       wip[1], and sets its batch ID to 3.  It signals wip[0]'s cv, and
90 *       restarts.
91 *
92 *   The above process continues until all input files have been consumed.  At
93 *   this point, a pair of barriers are used to allow a single thread to move
94 *   any partial batches from the wip array to the done array in batch ID order.
95 *   When this is complete, wq_done_queue is moved to wq_queue, and Phase II
96 *   begins.
97 *
98 *	Locking Semantics (Phase I)
99 *
100 *	The input queue (wq_queue) and the done queue (wq_done_queue) are
101 *	protected by separate mutexes - wq_queue_lock and wq_done_queue.  wip
102 *	array slots are protected by their own mutexes, which must be grabbed
103 *	before releasing the input queue lock.  The wip array lock is dropped
104 *	when the thread restarts the loop.  If the array slot was full, the
105 *	array lock will be held while the slot contents are added to the done
106 *	queue.  The done queue lock is used to protect the wip slot cv's.
107 *
108 *	The pow number is protected by the queue lock.  The master batch ID
109 *	and last completed batch (wq_lastdonebatch) counters are protected *in
110 *	Phase I* by the done queue lock.
111 *
112 *   Phase II
113 *
114 *   When Phase II begins, the queue consists of the merged batches from the
115 *   first phase.  Assume we have five batches:
116 *
117 *	Q:	a b c d e
118 *
119 *   Using the same batch ID mechanism we used in Phase I, but without the wip
120 *   array, worker threads remove two entries at a time from the beginning of
121 *   the queue.  These two entries are merged, and are added back to the tail
122 *   of the queue, as follows:
123 *
124 *	Q:	a b c d e	# start
125 *	Q:	c d e ab	# a, b removed, merged, added to end
126 *	Q:	e ab cd		# c, d removed, merged, added to end
127 *	Q:	cd eab		# e, ab removed, merged, added to end
128 *	Q:	cdeab		# cd, eab removed, merged, added to end
129 *
130 *   When one entry remains on the queue, with no merges outstanding, Phase II
131 *   finishes.  We pre-determine the stopping point by pre-calculating the
132 *   number of nodes that will appear on the list.  In the example above, the
133 *   number (wq_ninqueue) is 9.  When ninqueue is 1, we conclude Phase II by
134 *   signaling the main thread via wq_done_cv.
135 *
136 *	Locking Semantics (Phase II)
137 *
138 *	The queue (wq_queue), ninqueue, and the master batch ID and last
139 *	completed batch counters are protected by wq_queue_lock.  The done
140 *	queue and corresponding lock are unused in Phase II as is the wip array.
141 *
142 *   Uniquification
143 *
144 *   We want the CTF data that goes into a given module to be as small as
145 *   possible.  For example, we don't want it to contain any type data that may
146 *   be present in another common module.  As such, after creating the master
147 *   tdata_t for a given module, we can, if requested by the user, uniquify it
148 *   against the tdata_t from another module (genunix in the case of the SunOS
149 *   kernel).  We perform a merge between the tdata_t for this module and the
150 *   tdata_t from genunix.  Nodes found in this module that are not present in
151 *   genunix are added to a third tdata_t - the uniquified tdata_t.
152 *
153 *   Additive Merges
154 *
155 *   In some cases, for example if we are issuing a new version of a common
156 *   module in a patch, we need to make sure that the CTF data already present
157 *   in that module does not change.  Changes to this data would void the CTF
158 *   data in any module that uniquified against the common module.  To preserve
159 *   the existing data, we can perform what is known as an additive merge.  In
160 *   this case, a final uniquification is performed against the CTF data in the
161 *   previous version of the module.  The result will be the placement of new
162 *   and changed data after the existing data, thus preserving the existing type
163 *   ID space.
164 *
165 *   Saving the result
166 *
167 *   When the merges are complete, the resulting tdata_t is placed into the
168 *   output file, replacing the .SUNW_ctf section (if any) already in that file.
169 *
170 * The person who changes the merging thread code in this file without updating
171 * this comment will not live to see the stock hit five.
172 */
173
174#if !defined(__APPLE__)
175#include <stdio.h>
176#include <stdlib.h>
177#include <unistd.h>
178#include <pthread.h>
179#include <assert.h>
180#include <synch.h>
181#include <signal.h>
182#include <libgen.h>
183#include <string.h>
184#include <errno.h>
185#include <alloca.h>
186#include <sys/param.h>
187#include <sys/types.h>
188#include <sys/mman.h>
189#include <sys/sysconf.h>
190#else
191#include <stdio.h>
192#include <stdlib.h>
193#include <unistd.h>
194#include <pthread.h>
195#include <assert.h>
196
197#include <signal.h>
198#include <libgen.h>
199#include <string.h>
200#include <errno.h>
201#include <alloca.h>
202#include <sys/param.h>
203#include <sys/types.h>
204#include <sys/mman.h>
205
206#endif /* __APPLE__ */
207
208#include "ctf_headers.h"
209#include "ctftools.h"
210#include "ctfmerge.h"
211#include "traverse.h"
212#include "memory.h"
213#include "fifo.h"
214#include "barrier.h"
215
216#if !defined(__APPLE__)
217#pragma init(bigheap)
218#endif /* __APPLE__ */
219
220#define	MERGE_PHASE1_BATCH_SIZE		8
221#define	MERGE_PHASE1_MAX_SLOTS		5
222#define	MERGE_INPUT_THROTTLE_LEN	10
223
224const char *progname;
225static char *outfile = NULL;
226static char *tmpname = NULL;
227static int dynsym;
228int debug_level = DEBUG_LEVEL;
229#if !defined(__APPLE__)
230static size_t maxpgsize = 0x400000;
231#endif /* __APPLE__ */
232
233
234void
235usage(void)
236{
237	(void) fprintf(stderr,
238	    "Usage: %s [-fgstv] -l label | -L labelenv -o outfile file ...\n"
239	    "       %s [-fgstv] -l label | -L labelenv -o outfile -d uniqfile\n"
240	    "       %*s [-g] [-D uniqlabel] file ...\n"
241	    "       %s [-fgstv] -l label | -L labelenv -o outfile -w withfile "
242	    "file ...\n"
243	    "       %s [-g] -c srcfile destfile\n"
244#if defined(__APPLE__)
245	    "       %s [-fgstv] -l label | -L labelenv -o master_macho_file -Z raw_ctf_outfile file ...\n"
246#endif
247	    "\n"
248	    "  Note: if -L labelenv is specified and labelenv is not set in\n"
249	    "  the environment, a default value is used.\n",
250	    progname, progname, (int)strlen(progname), " ",
251	    progname, progname, progname);
252}
253
254#if !defined(__APPLE__)
255static void
256bigheap(void)
257{
258	size_t big, *size;
259	int sizes;
260	struct memcntl_mha mha;
261
262	/*
263	 * First, get the available pagesizes.
264	 */
265	if ((sizes = getpagesizes(NULL, 0)) == -1)
266		return;
267
268	if (sizes == 1 || (size = alloca(sizeof (size_t) * sizes)) == NULL)
269		return;
270
271	if (getpagesizes(size, sizes) == -1)
272		return;
273
274	while (size[sizes - 1] > maxpgsize)
275		sizes--;
276
277	/* set big to the largest allowed page size */
278	big = size[sizes - 1];
279	if (big & (big - 1)) {
280		/*
281		 * The largest page size is not a power of two for some
282		 * inexplicable reason; return.
283		 */
284		return;
285	}
286
287	/*
288	 * Now, align our break to the largest page size.
289	 */
290	if (brk((void *)((((uintptr_t)sbrk(0) - 1) & ~(big - 1)) + big)) != 0)
291		return;
292
293	/*
294	 * set the preferred page size for the heap
295	 */
296	mha.mha_cmd = MHA_MAPSIZE_BSSBRK;
297	mha.mha_flags = 0;
298	mha.mha_pagesize = big;
299
300	(void) memcntl(NULL, 0, MC_HAT_ADVISE, (caddr_t)&mha, 0, 0);
301}
302}
303#else
304static void
305bigheap(void)
306{
307	/* NOOP */
308}
309#endif /* __APPLE__ */
310
311static void
312finalize_phase_one(workqueue_t *wq)
313{
314	int startslot, i;
315
316	/*
317	 * wip slots are cleared out only when maxbatchsz td's have been merged
318	 * into them.  We're not guaranteed that the number of files we're
319	 * merging is a multiple of maxbatchsz, so there will be some partial
320	 * groups in the wip array.  Move them to the done queue in batch ID
321	 * order, starting with the slot containing the next batch that would
322	 * have been placed on the done queue, followed by the others.
323	 * One thread will be doing this while the others wait at the barrier
324	 * back in worker_thread(), so we don't need to worry about pesky things
325	 * like locks.
326	 */
327
328	for (startslot = -1, i = 0; i < wq->wq_nwipslots; i++) {
329		if (wq->wq_wip[i].wip_batchid == wq->wq_lastdonebatch + 1) {
330			startslot = i;
331			break;
332		}
333	}
334
335	assert(startslot != -1);
336
337	for (i = startslot; i < startslot + wq->wq_nwipslots; i++) {
338		int slotnum = i % wq->wq_nwipslots;
339		wip_t *wipslot = &wq->wq_wip[slotnum];
340
341		if (wipslot->wip_td != NULL) {
342			debug(2, "clearing slot %d (%d) (saving %d)\n",
343			    slotnum, i, wipslot->wip_nmerged);
344		} else
345			debug(2, "clearing slot %d (%d)\n", slotnum, i);
346
347		if (wipslot->wip_td != NULL) {
348			fifo_add(wq->wq_donequeue, wipslot->wip_td);
349			wq->wq_wip[slotnum].wip_td = NULL;
350		}
351	}
352
353	wq->wq_lastdonebatch = wq->wq_next_batchid++;
354
355	debug(2, "phase one done: donequeue has %d items\n",
356	    fifo_len(wq->wq_donequeue));
357}
358
359static void
360init_phase_two(workqueue_t *wq)
361{
362	int num;
363
364	/*
365	 * We're going to continually merge the first two entries on the queue,
366	 * placing the result on the end, until there's nothing left to merge.
367	 * At that point, everything will have been merged into one.  The
368	 * initial value of ninqueue needs to be equal to the total number of
369	 * entries that will show up on the queue, both at the start of the
370	 * phase and as generated by merges during the phase.
371	 */
372	wq->wq_ninqueue = num = fifo_len(wq->wq_donequeue);
373	while (num != 1) {
374		wq->wq_ninqueue += num / 2;
375		num = num / 2 + num % 2;
376	}
377
378	/*
379	 * Move the done queue to the work queue.  We won't be using the done
380	 * queue in phase 2.
381	 */
382	assert(fifo_len(wq->wq_queue) == 0);
383	fifo_free(wq->wq_queue, NULL);
384	wq->wq_queue = wq->wq_donequeue;
385}
386
387static void
388wip_save_work(workqueue_t *wq, wip_t *slot, int slotnum)
389{
390	pthread_mutex_lock(&wq->wq_donequeue_lock);
391
392	while (wq->wq_lastdonebatch + 1 < slot->wip_batchid)
393		pthread_cond_wait(&slot->wip_cv, &wq->wq_donequeue_lock);
394	assert(wq->wq_lastdonebatch + 1 == slot->wip_batchid);
395
396	fifo_add(wq->wq_donequeue, slot->wip_td);
397	wq->wq_lastdonebatch++;
398	pthread_cond_signal(&wq->wq_wip[(slotnum + 1) %
399	    wq->wq_nwipslots].wip_cv);
400
401	/* reset the slot for next use */
402	slot->wip_td = NULL;
403	slot->wip_batchid = wq->wq_next_batchid++;
404
405	pthread_mutex_unlock(&wq->wq_donequeue_lock);
406}
407
408static void
409wip_add_work(wip_t *slot, tdata_t *pow)
410{
411	if (slot->wip_td == NULL) {
412		slot->wip_td = pow;
413		slot->wip_nmerged = 1;
414	} else {
415		debug(2, "%d: merging %p into %p\n", pthread_self(),
416		    (void *)pow, (void *)slot->wip_td);
417
418		merge_into_master(pow, slot->wip_td, NULL, 0);
419		tdata_free(pow);
420
421		slot->wip_nmerged++;
422	}
423}
424
425static void
426worker_runphase1(workqueue_t *wq)
427{
428	wip_t *wipslot;
429	tdata_t *pow;
430	int wipslotnum, pownum;
431
432	for (;;) {
433		pthread_mutex_lock(&wq->wq_queue_lock);
434
435		while (fifo_empty(wq->wq_queue)) {
436			if (wq->wq_nomorefiles == 1) {
437				pthread_cond_broadcast(&wq->wq_work_avail);
438				pthread_mutex_unlock(&wq->wq_queue_lock);
439
440				/* on to phase 2 ... */
441				return;
442			}
443
444			pthread_cond_wait(&wq->wq_work_avail,
445			    &wq->wq_queue_lock);
446		}
447
448		/* there's work to be done! */
449		pow = fifo_remove(wq->wq_queue);
450		pownum = wq->wq_nextpownum++;
451		pthread_cond_broadcast(&wq->wq_work_removed);
452
453		assert(pow != NULL);
454
455		/* merge it into the right slot */
456		wipslotnum = pownum % wq->wq_nwipslots;
457		wipslot = &wq->wq_wip[wipslotnum];
458
459		pthread_mutex_lock(&wipslot->wip_lock);
460
461		pthread_mutex_unlock(&wq->wq_queue_lock);
462
463		wip_add_work(wipslot, pow);
464
465		if (wipslot->wip_nmerged == wq->wq_maxbatchsz)
466			wip_save_work(wq, wipslot, wipslotnum);
467
468		pthread_mutex_unlock(&wipslot->wip_lock);
469	}
470}
471
472static void
473worker_runphase2(workqueue_t *wq)
474{
475	tdata_t *pow1, *pow2;
476	int batchid;
477
478	for (;;) {
479		pthread_mutex_lock(&wq->wq_queue_lock);
480
481		if (wq->wq_ninqueue == 1) {
482			pthread_cond_broadcast(&wq->wq_work_avail);
483			pthread_mutex_unlock(&wq->wq_queue_lock);
484
485			debug(2, "%d: entering p2 completion barrier\n",
486			    pthread_self());
487			if (barrier_wait(&wq->wq_bar1)) {
488				pthread_mutex_lock(&wq->wq_queue_lock);
489				wq->wq_alldone = 1;
490				pthread_cond_signal(&wq->wq_alldone_cv);
491				pthread_mutex_unlock(&wq->wq_queue_lock);
492			}
493
494			return;
495		}
496
497		if (fifo_len(wq->wq_queue) < 2) {
498			pthread_cond_wait(&wq->wq_work_avail,
499			    &wq->wq_queue_lock);
500			pthread_mutex_unlock(&wq->wq_queue_lock);
501			continue;
502		}
503
504		/* there's work to be done! */
505		pow1 = fifo_remove(wq->wq_queue);
506		pow2 = fifo_remove(wq->wq_queue);
507		wq->wq_ninqueue -= 2;
508
509		batchid = wq->wq_next_batchid++;
510
511		pthread_mutex_unlock(&wq->wq_queue_lock);
512
513		debug(2, "%d: merging %p into %p\n", pthread_self(),
514		    (void *)pow1, (void *)pow2);
515		merge_into_master(pow1, pow2, NULL, 0);
516		tdata_free(pow1);
517
518		/*
519		 * merging is complete.  place at the tail of the queue in
520		 * proper order.
521		 */
522		pthread_mutex_lock(&wq->wq_queue_lock);
523		while (wq->wq_lastdonebatch + 1 != batchid) {
524			pthread_cond_wait(&wq->wq_done_cv,
525			    &wq->wq_queue_lock);
526		}
527
528		wq->wq_lastdonebatch = batchid;
529
530		fifo_add(wq->wq_queue, pow2);
531		debug(2, "%d: added %p to queue, len now %d, ninqueue %d\n",
532		    pthread_self(), (void *)pow2, fifo_len(wq->wq_queue),
533		    wq->wq_ninqueue);
534		pthread_cond_broadcast(&wq->wq_done_cv);
535		pthread_cond_signal(&wq->wq_work_avail);
536		pthread_mutex_unlock(&wq->wq_queue_lock);
537	}
538}
539
540/*
541 * Main loop for worker threads.
542 */
543static void
544worker_thread(workqueue_t *wq)
545{
546	worker_runphase1(wq);
547
548	debug(2, "%d: entering first barrier\n", pthread_self());
549
550	if (barrier_wait(&wq->wq_bar1)) {
551
552		debug(2, "%d: doing work in first barrier\n", pthread_self());
553
554		finalize_phase_one(wq);
555
556		init_phase_two(wq);
557
558		debug(2, "%d: ninqueue is %d, %d on queue\n", pthread_self(),
559		    wq->wq_ninqueue, fifo_len(wq->wq_queue));
560	}
561
562	debug(2, "%d: entering second barrier\n", pthread_self());
563
564	(void) barrier_wait(&wq->wq_bar2);
565
566	debug(2, "%d: phase 1 complete\n", pthread_self());
567
568	worker_runphase2(wq);
569}
570
571/*
572 * Pass a tdata_t tree, built from an input file, off to the work queue for
573 * consumption by worker threads.
574 */
575static int
576merge_ctf_cb(tdata_t *td, char *name, void *arg)
577{
578	workqueue_t *wq = arg;
579
580	debug(3, "Adding tdata %p for processing\n", (void *)td);
581
582	pthread_mutex_lock(&wq->wq_queue_lock);
583	while (fifo_len(wq->wq_queue) > wq->wq_ithrottle) {
584		debug(2, "Throttling input (len = %d, throttle = %d)\n",
585		    fifo_len(wq->wq_queue), wq->wq_ithrottle);
586		pthread_cond_wait(&wq->wq_work_removed, &wq->wq_queue_lock);
587	}
588
589	fifo_add(wq->wq_queue, td);
590	debug(1, "Thread %d announcing %s\n", pthread_self(), name);
591	pthread_cond_broadcast(&wq->wq_work_avail);
592	pthread_mutex_unlock(&wq->wq_queue_lock);
593
594	return (1);
595}
596
597/*
598 * This program is intended to be invoked from a Makefile, as part of the build.
599 * As such, in the event of a failure or user-initiated interrupt (^C), we need
600 * to ensure that a subsequent re-make will cause ctfmerge to be executed again.
601 * Unfortunately, ctfmerge will usually be invoked directly after (and as part
602 * of the same Makefile rule as) a link, and will operate on the linked file
603 * in place.  If we merely exit upon receipt of a SIGINT, a subsequent make
604 * will notice that the *linked* file is newer than the object files, and thus
605 * will not reinvoke ctfmerge.  The only way to ensure that a subsequent make
606 * reinvokes ctfmerge, is to remove the file to which we are adding CTF
607 * data (confusingly named the output file).  This means that the link will need
608 * to happen again, but links are generally fast, and we can't allow the merge
609 * to be skipped.
610 *
611 * Another possibility would be to block SIGINT entirely - to always run to
612 * completion.  The run time of ctfmerge can, however, be measured in minutes
613 * in some cases, so this is not a valid option.
614 */
615static void
616handle_sig(int sig)
617{
618	terminate("Caught signal %d - exiting\n", sig);
619}
620
621static void
622terminate_cleanup(void)
623{
624	int dounlink = getenv("CTFMERGE_TERMINATE_NO_UNLINK") ? 0 : 1;
625
626	if (tmpname != NULL && dounlink)
627		unlink(tmpname);
628
629	if (outfile == NULL)
630		return;
631
632	if (dounlink) {
633		fprintf(stderr, "Removing %s\n", outfile);
634		unlink(outfile);
635	}
636}
637
638static void
639copy_ctf_data(char *srcfile, char *destfile, int keep_stabs)
640{
641	tdata_t *srctd;
642
643	if (read_ctf(&srcfile, 1, NULL, read_ctf_save_cb, &srctd, 1) == 0)
644		terminate("No CTF data found in source file %s\n", srcfile);
645
646	tmpname = mktmpname(destfile, ".ctf");
647	write_ctf(srctd, destfile, tmpname, CTF_COMPRESS | keep_stabs);
648	if (rename(tmpname, destfile) != 0) {
649		terminate("Couldn't rename temp file %s to %s", tmpname,
650		    destfile);
651	}
652	free(tmpname);
653	tdata_free(srctd);
654}
655
656static void
657wq_init(workqueue_t *wq, int nfiles)
658{
659	int throttle, nslots, i;
660
661	if (getenv("CTFMERGE_MAX_SLOTS"))
662		nslots = atoi(getenv("CTFMERGE_MAX_SLOTS"));
663	else
664		nslots = MERGE_PHASE1_MAX_SLOTS;
665
666	if (getenv("CTFMERGE_PHASE1_BATCH_SIZE"))
667		wq->wq_maxbatchsz = atoi(getenv("CTFMERGE_PHASE1_BATCH_SIZE"));
668	else
669		wq->wq_maxbatchsz = MERGE_PHASE1_BATCH_SIZE;
670
671	nslots = MIN(nslots, (nfiles + wq->wq_maxbatchsz - 1) /
672	    wq->wq_maxbatchsz);
673
674	wq->wq_wip = xcalloc(sizeof (wip_t) * nslots);
675	wq->wq_nwipslots = nslots;
676	wq->wq_nthreads = MIN(sysconf(_SC_NPROCESSORS_ONLN) * 3 / 2, nslots);
677	wq->wq_thread = xmalloc(sizeof (pthread_t) * wq->wq_nthreads);
678
679	if (getenv("CTFMERGE_INPUT_THROTTLE"))
680		throttle = atoi(getenv("CTFMERGE_INPUT_THROTTLE"));
681	else
682		throttle = MERGE_INPUT_THROTTLE_LEN;
683	wq->wq_ithrottle = throttle * wq->wq_nthreads;
684
685	debug(1, "Using %d slots, %d threads\n", wq->wq_nwipslots,
686	    wq->wq_nthreads);
687
688	wq->wq_next_batchid = 0;
689
690	for (i = 0; i < nslots; i++) {
691		pthread_mutex_init(&wq->wq_wip[i].wip_lock, NULL);
692#if defined(__APPLE__)
693		pthread_cond_init(&wq->wq_wip[i].wip_cv, NULL); /* Omitted on Solaris!?! */
694#endif /* __APPLE__ */
695		wq->wq_wip[i].wip_batchid = wq->wq_next_batchid++;
696	}
697
698	pthread_mutex_init(&wq->wq_queue_lock, NULL);
699	wq->wq_queue = fifo_new();
700	pthread_cond_init(&wq->wq_work_avail, NULL);
701	pthread_cond_init(&wq->wq_work_removed, NULL);
702	wq->wq_ninqueue = nfiles;
703	wq->wq_nextpownum = 0;
704
705	pthread_mutex_init(&wq->wq_donequeue_lock, NULL);
706	wq->wq_donequeue = fifo_new();
707	wq->wq_lastdonebatch = -1;
708
709	pthread_cond_init(&wq->wq_done_cv, NULL);
710
711	pthread_cond_init(&wq->wq_alldone_cv, NULL);
712	wq->wq_alldone = 0;
713
714	barrier_init(&wq->wq_bar1, wq->wq_nthreads);
715	barrier_init(&wq->wq_bar2, wq->wq_nthreads);
716
717	wq->wq_nomorefiles = 0;
718}
719
720static void
721start_threads(workqueue_t *wq)
722{
723	sigset_t sets;
724	int i;
725
726	sigemptyset(&sets);
727	sigaddset(&sets, SIGINT);
728	sigaddset(&sets, SIGQUIT);
729	sigaddset(&sets, SIGTERM);
730	pthread_sigmask(SIG_BLOCK, &sets, NULL);
731
732	for (i = 0; i < wq->wq_nthreads; i++) {
733		pthread_create(&wq->wq_thread[i], NULL,
734		    (void *(*)(void *))worker_thread, wq);
735	}
736
737	sigset(SIGINT, handle_sig);
738	sigset(SIGQUIT, handle_sig);
739	sigset(SIGTERM, handle_sig);
740	pthread_sigmask(SIG_UNBLOCK, &sets, NULL);
741}
742
743static void
744join_threads(workqueue_t *wq)
745{
746	int i;
747
748	for (i = 0; i < wq->wq_nthreads; i++) {
749		pthread_join(wq->wq_thread[i], NULL);
750	}
751}
752
753static int
754strcompare(const void *p1, const void *p2)
755{
756	char *s1 = *((char **)p1);
757	char *s2 = *((char **)p2);
758
759	return (strcmp(s1, s2));
760}
761
762/*
763 * Core work queue structure; passed to worker threads on thread creation
764 * as the main point of coordination.  Allocate as a static structure; we
765 * could have put this into a local variable in main, but passing a pointer
766 * into your stack to another thread is fragile at best and leads to some
767 * hard-to-debug failure modes.
768 */
769static workqueue_t wq;
770
771int
772main(int argc, char **argv)
773{
774	tdata_t *mstrtd, *savetd;
775	char *uniqfile = NULL, *uniqlabel = NULL;
776	char *withfile = NULL;
777#if defined(__APPLE__)
778	char *raw_ctf_file = NULL;
779#endif
780	char *label = NULL;
781	char **ifiles, **tifiles;
782	int verbose = 0, docopy = 0;
783	int write_fuzzy_match = 0;
784	int keep_stabs = 0;
785	int require_ctf = 0;
786	int nifiles, nielems;
787	int c, i, idx, tidx, err;
788
789	progname = basename(argv[0]);
790
791	if (getenv("CTFMERGE_DEBUG_LEVEL"))
792		debug_level = atoi(getenv("CTFMERGE_DEBUG_LEVEL"));
793
794	err = 0;
795#if defined(__APPLE__)
796	while ((c = getopt(argc, argv, ":cd:D:fgl:L:o:tvw:sZ:")) != EOF) {
797#else
798	while ((c = getopt(argc, argv, ":cd:D:fgl:L:o:tvw:s")) != EOF) {
799#endif
800		switch (c) {
801		case 'c':
802			docopy = 1;
803			break;
804		case 'd':
805			/* Uniquify against `uniqfile' */
806			uniqfile = optarg;
807			break;
808		case 'D':
809			/* Uniquify against label `uniqlabel' in `uniqfile' */
810			uniqlabel = optarg;
811			break;
812		case 'f':
813			write_fuzzy_match = CTF_FUZZY_MATCH;
814			break;
815		case 'g':
816			keep_stabs = CTF_KEEP_STABS;
817			break;
818		case 'l':
819			/* Label merged types with `label' */
820			label = optarg;
821			break;
822		case 'L':
823			/* Label merged types with getenv(`label`) */
824			if ((label = getenv(optarg)) == NULL)
825				label = CTF_DEFAULT_LABEL;
826			break;
827		case 'o':
828			/* Place merged types in CTF section in `outfile' */
829			outfile = optarg;
830			break;
831		case 't':
832			/* Insist *all* object files built from C have CTF */
833			require_ctf = 1;
834			break;
835		case 'v':
836			/* More debugging information */
837			verbose = 1;
838			break;
839		case 'w':
840			/* Additive merge with data from `withfile' */
841			withfile = optarg;
842			break;
843		case 's':
844			/* use the dynsym rather than the symtab */
845			dynsym = CTF_USE_DYNSYM;
846			break;
847#if defined(__APPLE__)
848		case 'Z':
849			/* Write raw CTF data by itself */
850			raw_ctf_file = optarg;
851			break;
852#endif
853		default:
854			usage();
855			exit(2);
856		}
857	}
858
859	/* Validate arguments */
860	if (docopy) {
861		if (uniqfile != NULL || uniqlabel != NULL || label != NULL ||
862		    outfile != NULL || withfile != NULL || dynsym != 0)
863			err++;
864
865		if (argc - optind != 2)
866			err++;
867	} else {
868		if (uniqfile != NULL && withfile != NULL)
869			err++;
870
871		if (uniqlabel != NULL && uniqfile == NULL)
872			err++;
873
874		if (outfile == NULL || label == NULL)
875			err++;
876
877		if (argc - optind == 0)
878			err++;
879	}
880
881#if defined(__APPLE__)
882	if ((uniqfile != NULL || withfile != NULL) && raw_ctf_file != NULL)
883		err++;
884#endif
885
886	if (err) {
887		usage();
888		exit(2);
889	}
890
891	if (getenv("STRIPSTABS_KEEP_STABS") != NULL)
892		keep_stabs = CTF_KEEP_STABS;
893
894	if (uniqfile && access(uniqfile, R_OK) != 0) {
895		warning("Uniquification file %s couldn't be opened and "
896		    "will be ignored.\n", uniqfile);
897		uniqfile = NULL;
898	}
899	if (withfile && access(withfile, R_OK) != 0) {
900		warning("With file %s couldn't be opened and will be "
901		    "ignored.\n", withfile);
902		withfile = NULL;
903	}
904	if (outfile && access(outfile, R_OK|W_OK) != 0)
905		terminate("Cannot open output file %s for r/w", outfile);
906
907#if defined(__APPLE__)
908	if (raw_ctf_file && access(raw_ctf_file, F_OK) != -1)
909		terminate("Raw CTF output file %s already exists", raw_ctf_file);
910#endif
911
912	/*
913	 * This is ugly, but we don't want to have to have a separate tool
914	 * (yet) just for copying an ELF section with our specific requirements,
915	 * so we shoe-horn a copier into ctfmerge.
916	 */
917	if (docopy) {
918		copy_ctf_data(argv[optind], argv[optind + 1], keep_stabs);
919
920		exit(0);
921	}
922
923	set_terminate_cleanup(terminate_cleanup);
924
925	/* Sort the input files and strip out duplicates */
926	nifiles = argc - optind;
927	ifiles = xmalloc(sizeof (char *) * nifiles);
928	tifiles = xmalloc(sizeof (char *) * nifiles);
929
930	for (i = 0; i < nifiles; i++)
931		tifiles[i] = argv[optind + i];
932	qsort(tifiles, nifiles, sizeof (char *), (int (*)())strcompare);
933
934	ifiles[0] = tifiles[0];
935	for (idx = 0, tidx = 1; tidx < nifiles; tidx++) {
936		if (strcmp(ifiles[idx], tifiles[tidx]) != 0)
937			ifiles[++idx] = tifiles[tidx];
938	}
939	nifiles = idx + 1;
940
941	/* Make sure they all exist */
942	if ((nielems = count_files(ifiles, nifiles)) < 0)
943		terminate("Some input files were inaccessible\n");
944
945	/* Prepare for the merge */
946	wq_init(&wq, nielems);
947
948	start_threads(&wq);
949
950	/*
951	 * Start the merge
952	 *
953	 * We're reading everything from each of the object files, so we
954	 * don't need to specify labels.
955	 */
956	if (read_ctf(ifiles, nifiles, NULL, merge_ctf_cb,
957	    &wq, require_ctf) == 0) {
958		/*
959		 * If we're verifying that C files have CTF, it's safe to
960		 * assume that in this case, we're building only from assembly
961		 * inputs.
962		 */
963		if (require_ctf)
964			exit(0);
965		terminate("No ctf sections found to merge\n");
966	}
967
968	pthread_mutex_lock(&wq.wq_queue_lock);
969	wq.wq_nomorefiles = 1;
970	pthread_cond_broadcast(&wq.wq_work_avail);
971	pthread_mutex_unlock(&wq.wq_queue_lock);
972
973	pthread_mutex_lock(&wq.wq_queue_lock);
974	while (wq.wq_alldone == 0)
975		pthread_cond_wait(&wq.wq_alldone_cv, &wq.wq_queue_lock);
976	pthread_mutex_unlock(&wq.wq_queue_lock);
977
978	join_threads(&wq);
979
980	/*
981	 * All requested files have been merged, with the resulting tree in
982	 * mstrtd.  savetd is the tree that will be placed into the output file.
983	 *
984	 * Regardless of whether we're doing a normal uniquification or an
985	 * additive merge, we need a type tree that has been uniquified
986	 * against uniqfile or withfile, as appropriate.
987	 *
988	 * If we're doing a uniquification, we stuff the resulting tree into
989	 * outfile.  Otherwise, we add the tree to the tree already in withfile.
990	 */
991	assert(fifo_len(wq.wq_queue) == 1);
992	mstrtd = fifo_remove(wq.wq_queue);
993
994	if (verbose || debug_level) {
995		debug(2, "Statistics for td %p\n", (void *)mstrtd);
996
997		iidesc_stats(mstrtd->td_iihash);
998	}
999
1000	if (uniqfile != NULL || withfile != NULL) {
1001		char *reffile, *reflabel = NULL;
1002		tdata_t *reftd;
1003
1004		if (uniqfile != NULL) {
1005			reffile = uniqfile;
1006			reflabel = uniqlabel;
1007		} else
1008			reffile = withfile;
1009
1010		if (read_ctf(&reffile, 1, reflabel, read_ctf_save_cb,
1011		    &reftd, require_ctf) == 0) {
1012			terminate("No CTF data found in reference file %s\n",
1013			    reffile);
1014		}
1015
1016		savetd = tdata_new();
1017
1018		if (CTF_TYPE_ISCHILD(reftd->td_nextid))
1019			terminate("No room for additional types in master\n");
1020
1021		savetd->td_nextid = withfile ? reftd->td_nextid :
1022		    CTF_INDEX_TO_TYPE(1, TRUE);
1023		merge_into_master(mstrtd, reftd, savetd, 0);
1024
1025		tdata_label_add(savetd, label, CTF_LABEL_LASTIDX);
1026
1027		if (withfile) {
1028			/*
1029			 * savetd holds the new data to be added to the withfile
1030			 */
1031			tdata_t *withtd = reftd;
1032
1033			tdata_merge(withtd, savetd);
1034
1035			savetd = withtd;
1036		} else {
1037			char uniqname[MAXPATHLEN];
1038			labelent_t *parle;
1039
1040			parle = tdata_label_top(reftd);
1041
1042			savetd->td_parlabel = xstrdup(parle->le_name);
1043
1044			strncpy(uniqname, reffile, sizeof (uniqname));
1045			uniqname[MAXPATHLEN - 1] = '\0';
1046			savetd->td_parname = xstrdup(basename(uniqname));
1047		}
1048
1049	} else {
1050		/*
1051		 * No post processing.  Write the merged tree as-is into the
1052		 * output file.
1053		 */
1054		tdata_label_free(mstrtd);
1055		tdata_label_add(mstrtd, label, CTF_LABEL_LASTIDX);
1056
1057		savetd = mstrtd;
1058	}
1059
1060#if !defined(__APPLE__)
1061	tmpname = mktmpname(outfile, ".ctf");
1062	write_ctf(savetd, outfile, tmpname,
1063		  CTF_COMPRESS | write_fuzzy_match | dynsym | keep_stabs);
1064	if (rename(tmpname, outfile) != 0)
1065	    terminate("Couldn't rename output temp file %s", tmpname);
1066	free(tmpname);
1067#else
1068	if (raw_ctf_file) {
1069		tmpname = raw_ctf_file;
1070	} else {
1071		tmpname = mktmpname(outfile, ".ctf");
1072	}
1073	write_ctf(savetd, outfile, tmpname,
1074		  CTF_COMPRESS | write_fuzzy_match | dynsym | keep_stabs | CTF_BYTESWAP /* swap as needed to target */ | (raw_ctf_file != NULL ? CTF_RAW_OUTPUT : 0));
1075	if (!raw_ctf_file) {
1076		if (rename(tmpname, outfile) != 0)
1077			terminate("Couldn't rename output temp file %s", tmpname);
1078		free(tmpname);
1079	}
1080#endif /* __APPLE__ */
1081
1082	return (0);
1083}
1084