- Change --nthreads parameter to --parallel for GNU compatibility

- Change default sort method to mergesort, which has a better worst case
  performance than qsort

Submitted by:	Oleg Moskalenko <oleg.moskalenko@citrix.com>
This commit is contained in:
Gabor Kovesdan 2012-07-04 16:25:11 +00:00
parent 7f8492ba48
commit 5d5151ae7e
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=238108
5 changed files with 21 additions and 13 deletions

View File

@ -1297,7 +1297,7 @@ sort_list_to_file(struct sort_list *list, const char *outfile)
} }
if (sort_opts_vals.sort_method == SORT_DEFAULT) if (sort_opts_vals.sort_method == SORT_DEFAULT)
sort_opts_vals.sort_method = SORT_QSORT; sort_opts_vals.sort_method = DEFAULT_SORT_ALGORITHM;
if (debug_sort) if (debug_sort)
printf("sort_method=%s\n", printf("sort_method=%s\n",
@ -1314,9 +1314,12 @@ sort_list_to_file(struct sort_list *list, const char *outfile)
case SORT_HEAPSORT: case SORT_HEAPSORT:
mt_sort(list, heapsort, outfile); mt_sort(list, heapsort, outfile);
break; break;
default: case SORT_QSORT:
mt_sort(list, sort_qsort, outfile); mt_sort(list, sort_qsort, outfile);
break; break;
default:
mt_sort(list, DEFAULT_SORT_FUNC, outfile);
break;
} }
} }

View File

@ -39,6 +39,9 @@
#define SORT_HEAPSORT 3 #define SORT_HEAPSORT 3
#define SORT_RADIXSORT 4 #define SORT_RADIXSORT 4
#define DEFAULT_SORT_ALGORITHM SORT_HEAPSORT
#define DEFAULT_SORT_FUNC heapsort
/* /*
* List of data to be sorted. * List of data to be sorted.
*/ */

View File

@ -45,6 +45,8 @@ __FBSDID("$FreeBSD$");
#include "coll.h" #include "coll.h"
#include "radixsort.h" #include "radixsort.h"
#define DEFAULT_SORT_FUNC_RADIXSORT mergesort
#define TINY_NODE(sl) ((sl)->tosort_num < 65) #define TINY_NODE(sl) ((sl)->tosort_num < 65)
#define SMALL_NODE(sl) ((sl)->tosort_num < 5) #define SMALL_NODE(sl) ((sl)->tosort_num < 5)
@ -349,7 +351,7 @@ run_sort_level_next(struct sort_level *sl)
/* NOTREACHED */ /* NOTREACHED */
err(2, "Radix sort error 3"); err(2, "Radix sort error 3");
} else } else
qsort(sl->leaves, sl->leaves_num, DEFAULT_SORT_FUNC_RADIXSORT(sl->leaves, sl->leaves_num,
sizeof(struct sort_list_item *), sizeof(struct sort_list_item *),
(int(*)(const void *, const void *)) func); (int(*)(const void *, const void *)) func);
@ -389,12 +391,12 @@ run_sort_level_next(struct sort_level *sl)
sizeof(struct sort_list_item *), sizeof(struct sort_list_item *),
(int(*)(const void *, const void *)) list_coll); (int(*)(const void *, const void *)) list_coll);
} else { } else {
qsort(sl->leaves, sl->leaves_num, DEFAULT_SORT_FUNC_RADIXSORT(sl->leaves, sl->leaves_num,
sizeof(struct sort_list_item *), sizeof(struct sort_list_item *),
(int(*)(const void *, const void *)) list_coll); (int(*)(const void *, const void *)) list_coll);
} }
} else if (!sort_opts_vals.sflag && sort_opts_vals.complex_sort) { } else if (!sort_opts_vals.sflag && sort_opts_vals.complex_sort) {
qsort(sl->leaves, sl->leaves_num, DEFAULT_SORT_FUNC_RADIXSORT(sl->leaves, sl->leaves_num,
sizeof(struct sort_list_item *), sizeof(struct sort_list_item *),
(int(*)(const void *, const void *)) list_coll_by_str_only); (int(*)(const void *, const void *)) list_coll_by_str_only);
} }
@ -541,12 +543,12 @@ run_top_sort_level(struct sort_level *sl)
sizeof(struct sort_list_item *), sizeof(struct sort_list_item *),
(int(*)(const void *, const void *)) list_coll); (int(*)(const void *, const void *)) list_coll);
} else { } else {
qsort(sl->leaves, sl->leaves_num, DEFAULT_SORT_FUNC_RADIXSORT(sl->leaves, sl->leaves_num,
sizeof(struct sort_list_item *), sizeof(struct sort_list_item *),
(int(*)(const void *, const void *)) list_coll); (int(*)(const void *, const void *)) list_coll);
} }
} else if (!sort_opts_vals.sflag && sort_opts_vals.complex_sort) { } else if (!sort_opts_vals.sflag && sort_opts_vals.complex_sort) {
qsort(sl->leaves, sl->leaves_num, DEFAULT_SORT_FUNC_RADIXSORT(sl->leaves, sl->leaves_num,
sizeof(struct sort_list_item *), sizeof(struct sort_list_item *),
(int(*)(const void *, const void *)) list_coll_by_str_only); (int(*)(const void *, const void *)) list_coll_by_str_only);
} }

View File

@ -33,7 +33,7 @@
.\" .\"
.\" @(#)sort.1 8.1 (Berkeley) 6/6/93 .\" @(#)sort.1 8.1 (Berkeley) 6/6/93
.\" .\"
.Dd May 25, 2012 .Dd July 3, 2012
.Dt SORT 1 .Dt SORT 1
.Os .Os
.Sh NAME .Sh NAME
@ -329,7 +329,7 @@ is used.
.It Fl Fl debug .It Fl Fl debug
Print some extra information about the sorting process to the Print some extra information about the sorting process to the
standard output. standard output.
%%THREADS%%.It Fl Fl nthreads %%THREADS%%.It Fl Fl parallel
%%THREADS%%Set the maximum number of execution threads. %%THREADS%%Set the maximum number of execution threads.
%%THREADS%%Default number equals to the number of CPUs. %%THREADS%%Default number equals to the number of CPUs.
.It Fl Fl files0-from Ns = Ns Ar filename .It Fl Fl files0-from Ns = Ns Ar filename

View File

@ -91,7 +91,7 @@ const char *nlsstr[] = { "",
"[--heapsort] [--mergesort] [--radixsort] [--qsort] " "[--heapsort] [--mergesort] [--radixsort] [--qsort] "
"[--mmap] " "[--mmap] "
#if defined(SORT_THREADS) #if defined(SORT_THREADS)
"[--nthreads thread_no] " "[--parallel thread_no] "
#endif #endif
"[--human-numeric-sort] " "[--human-numeric-sort] "
"[--version-sort] [--random-sort [--random-source file]] " "[--version-sort] [--random-sort [--random-source file]] "
@ -132,7 +132,7 @@ enum
VERSION_OPT, VERSION_OPT,
DEBUG_OPT, DEBUG_OPT,
#if defined(SORT_THREADS) #if defined(SORT_THREADS)
NTHREADS_OPT, PARALLEL_OPT,
#endif #endif
RANDOMSOURCE_OPT, RANDOMSOURCE_OPT,
COMPRESSPROGRAM_OPT, COMPRESSPROGRAM_OPT,
@ -171,7 +171,7 @@ struct option long_options[] = {
{ "numeric-sort", no_argument, NULL, 'n' }, { "numeric-sort", no_argument, NULL, 'n' },
{ "output", required_argument, NULL, 'o' }, { "output", required_argument, NULL, 'o' },
#if defined(SORT_THREADS) #if defined(SORT_THREADS)
{ "nthreads", required_argument, NULL, NTHREADS_OPT }, { "parallel", required_argument, NULL, PARALLEL_OPT },
#endif #endif
{ "qsort", no_argument, NULL, QSORT_OPT }, { "qsort", no_argument, NULL, QSORT_OPT },
{ "radixsort", no_argument, NULL, RADIXSORT_OPT }, { "radixsort", no_argument, NULL, RADIXSORT_OPT },
@ -1119,7 +1119,7 @@ main(int argc, char **argv)
} }
break; break;
#if defined(SORT_THREADS) #if defined(SORT_THREADS)
case NTHREADS_OPT: case PARALLEL_OPT:
nthreads = (size_t)(atoi(optarg)); nthreads = (size_t)(atoi(optarg));
if (nthreads < 1) if (nthreads < 1)
nthreads = 1; nthreads = 1;