Browse code

Add -j option to control parallelism

John Hawthorn authored on 01/02/2017 02:13:27
Showing 3 changed files

... ...
@@ -107,7 +107,11 @@ void choices_init(choices_t *c, options_t *options) {
107 107
 	c->capacity = c->size = 0;
108 108
 	choices_resize(c, INITIAL_CHOICE_CAPACITY);
109 109
 
110
-	c->worker_count = (int)sysconf(_SC_NPROCESSORS_ONLN);
110
+	if (options->workers) {
111
+		c->worker_count = options->workers;
112
+	} else {
113
+		c->worker_count = (int)sysconf(_SC_NPROCESSORS_ONLN);
114
+	}
111 115
 
112 116
 	choices_reset_search(c);
113 117
 }
... ...
@@ -15,6 +15,7 @@ static const char *usage_str =
15 15
     " -e, --show-matches=QUERY Output the sorted matches of QUERY\n"
16 16
     " -t, --tty=TTY            Specify file to use as TTY device (default /dev/tty)\n"
17 17
     " -s, --show-scores        Show the scores of each match\n"
18
+    " -j, --workers NUM        Use NUM workers for searching. (default is number of CPU threads)\n"
18 19
     " -h, --help     Display this help and exit\n"
19 20
     " -v, --version  Output version information and exit\n";
20 21
 
... ...
@@ -44,13 +45,14 @@ void options_init(options_t *options) {
44 45
 	options->num_lines = 10;
45 46
 	options->scrolloff = 1;
46 47
 	options->prompt = "> ";
48
+	options->workers = 0;
47 49
 }
48 50
 
49 51
 void options_parse(options_t *options, int argc, char *argv[]) {
50 52
 	options_init(options);
51 53
 
52 54
 	int c;
53
-	while ((c = getopt_long(argc, argv, "vhse:q:l:t:p:", longopts, NULL)) != -1) {
55
+	while ((c = getopt_long(argc, argv, "vhse:q:l:t:p:j:", longopts, NULL)) != -1) {
54 56
 		switch (c) {
55 57
 			case 'v':
56 58
 				printf("%s " VERSION " (c) 2014 John Hawthorn\n", argv[0]);
... ...
@@ -80,6 +82,12 @@ void options_parse(options_t *options, int argc, char *argv[]) {
80 82
 			case 'p':
81 83
 				options->prompt = optarg;
82 84
 				break;
85
+			case 'j':
86
+				if (sscanf(optarg, "%u", &options->workers) != 1) {
87
+					usage(argv[0]);
88
+					exit(EXIT_FAILURE);
89
+				}
90
+				break;
83 91
 			case 'l': {
84 92
 				int l;
85 93
 				if (!strcmp(optarg, "max")) {
... ...
@@ -10,6 +10,7 @@ typedef struct {
10 10
 	unsigned int num_lines;
11 11
 	unsigned int scrolloff;
12 12
 	const char *prompt;
13
+	unsigned int workers;
13 14
 } options_t;
14 15
 
15 16
 void options_init(options_t *options);