File: thread_readtest.cc

package info (click to toggle)
ardour 1:5.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 74,708 kB
  • sloc: cpp: 512,951; xml: 123,656; ansic: 65,010; python: 22,599; sh: 5,368; asm: 1,347; perl: 888; php: 770; makefile: 253; objc: 28
file content (386 lines) | stat: -rw-r--r-- 7,966 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/* g++ -o thread_readtest thread_readtest.cc `pkg-config --cflags --libs glibmm-2.4` -lm */

#ifndef _WIN32
#  define HAVE_MMAP
#endif

#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <getopt.h>
#include <fcntl.h>
#include <math.h>

#undef HAVE_MMAP
#ifdef HAVE_MMAP
#  include <sys/stat.h>
#  include <sys/mman.h>
#endif

#include <glibmm.h>

char* data = 0;

void
usage ()
{
	fprintf (stderr, "thread_readtest [ -b BLOCKSIZE ] [ -l FILELIMIT] [ -n NTHREADS ] [ -D ] [ -R ] [ -M ] filename-template\n");
}

Glib::Threads::Cond pool_run;
Glib::Threads::Cond pool_done;
Glib::Threads::Mutex pool_lock;
std::list<int> pool_work;
std::vector<Glib::Threads::Thread*> thread_pool;
int pool_errors = 0;
bool thread_pool_lives = true;

struct ThreadData {
	int id;
	char *data;
	size_t block_size;
};

void
thread_pool_work (ThreadData* td)
{
	Glib::Threads::Mutex::Lock lm (pool_lock);

	while (thread_pool_lives) {
		pool_run.wait (pool_lock);

		if (!thread_pool_lives) {
			return;
		}

		/* we're awake ... get some work */

		while (!pool_work.empty()) {

			int file_descriptor = pool_work.front ();
			pool_work.pop_front ();

			/* release the lock while we do work */

			lm.release ();

			/* do the work */

			int err = 0;

			ssize_t nread;

		  again:
			if ((nread = ::read (file_descriptor, td->data, td->block_size)) != td->block_size) {
				if (nread != 0 && errno == EAGAIN) {
					fprintf (stderr, "read requires retry\n");
					goto again;
				}
				if (nread != 0) {
					fprintf (stderr, "thread %d has error = %s\n", td->id, strerror (errno));
				}
				err++;
			}

			/* reacquire lock so that we can check the status of
			 * things and possibly wake the master.
			 */

			lm.acquire ();
			pool_errors += err;

			if (pool_work.empty()) {
				/* work is finished, tell the master */
				pool_done.signal ();
			}
		}

	}

	return;
}

void
stop_thread_pool ()
{
	{
		Glib::Threads::Mutex::Lock lm (pool_lock);
		thread_pool_lives = false;
		pool_work.clear ();
		pool_run.broadcast ();
	}

	/* XXX wait for each thread to finish */
}

void
build_thread_pool (int nthreads, size_t block_size)
{
	for (int n = 0; n < nthreads; ++n) {
		ThreadData* td = new ThreadData;
		td->data = (char*) malloc (sizeof (char) * block_size);
		td->block_size = block_size;
		td->id = n;

		thread_pool.push_back (Glib::Threads::Thread::create (sigc::bind (sigc::ptr_fun (thread_pool_work), td)));
	}
}

int
run_thread_pool (int* files, int nfiles)
{
	Glib::Threads::Mutex::Lock lm (pool_lock);

	/* Queue up all the files */
	for (int n = 0; n < nfiles; ++n) {
		pool_work.push_back (files[n]);
	}

	pool_errors = 0;

	/* wake everybody up */
	pool_run.broadcast ();

	/* wait for everyone to finish */

	pool_done.wait (pool_lock);

	if (pool_errors) {
		return -1;
	}

	return 0;
}

int
main (int argc, char* argv[])
{
	int* files;
	char optstring[] = "b:DRMl:q";
	uint32_t block_size = 64 * 1024 * 4;
	int max_files = -1;
	int nthreads = 16;
#ifdef __APPLE__
	int direct = 0;
	int noreadahead = 0;
#endif
#ifdef HAVE_MMAP
	int use_mmap = 0;
	void  **addr;
	size_t *flen;
#endif
	const struct option longopts[] = {
		{ "blocksize", 1, 0, 'b' },
		{ "direct", 0, 0, 'D' },
		{ "mmap", 0, 0, 'M' },
		{ "noreadahead", 0, 0, 'R' },
		{ "limit", 1, 0, 'l' },
		{ "nthreads", 16, 0, 'n' },
		{ 0, 0, 0, 0 }
	};

	int option_index = 0;
	int c = 0;
	char const * name_template = 0;
	int flags = O_RDONLY;
	int n = 0;
	int nfiles = 0;
	int quiet = 0;

	while (1) {
		if ((c = getopt_long (argc, argv, optstring, longopts, &option_index)) == -1) {
			break;
		}

		switch (c) {
		case 'b':
			block_size = atoi (optarg);
			break;
		case 'l':
			max_files = atoi (optarg);
			break;
		case 'D':
#ifdef __APPLE__
			direct = 1;
#endif
			break;
		case 'M':
#ifdef HAVE_MMAP
			use_mmap = 1;
#endif
			break;
		case 'R':
#ifdef __APPLE__
			noreadahead = 1;
#endif
			break;
		case 'q':
			quiet = 1;
			break;
		case 'n':
			nthreads = atoi (optarg);
			break;
		default:
			usage ();
			return 0;
		}
	}

	if (optind < argc) {
		name_template = argv[optind];
	} else {
		usage ();
		return 1;
	}

	while (1) {
		char path[PATH_MAX+1];

		snprintf (path, sizeof (path), name_template, n+1);

		if (access (path, R_OK) != 0) {
			break;
		}

		++n;

		if (max_files > 0 &&  n >= max_files) {
			break;
		}
	}

	if (n == 0) {
		fprintf (stderr, "No matching files found for %s\n", name_template);
		return 1;
	}

	if (!quiet) {
		printf ("# Discovered %d files using %s\n", n, name_template);
	}

	nfiles = n;
	files = (int *) malloc (sizeof (int) * nfiles);
#ifdef HAVE_MMAP
	if (use_mmap) {
		if (!quiet) {
			printf ("# Using mmap().\n");
		}
		addr = malloc (sizeof (void*) * nfiles);
		flen = (size_t*) malloc (sizeof (size_t) * nfiles);
	}
#endif

	for (n = 0; n < nfiles; ++n) {

		char path[PATH_MAX+1];
		int fd;

		snprintf (path, sizeof (path), name_template, n+1);

		if ((fd = open (path, flags, 0644)) < 0) {
			fprintf (stderr, "Could not open file #%d @ %s (%s)\n", n, path, strerror (errno));
			return 1;
		}

#ifdef __APPLE__
		if (direct) {
			/* Apple man pages say only that it returns "a value other than -1 on success",
				 which probably means zero, but you just can't be too careful with
				 those guys.
				 */
			if (fcntl (fd, F_NOCACHE, 1) == -1) {
				fprintf (stderr, "Cannot set F_NOCACHE on file #%d\n", n);
			}
		}

		if (noreadahead) {
			if (fcntl (fd, F_RDAHEAD, 0) == -1) {
				fprintf (stderr, "Cannot set F_READAHED on file #%d\n", n);
			}
		}
#endif

		files[n] = fd;

#ifdef HAVE_MMAP
		if (use_mmap) {
			struct stat s;
			if (fstat (fd, & s)) {
				fprintf (stderr, "Could not stat fd #%d @ %s\n", n, path);
				return 1;
			}
			if (s.st_size < block_size) {
				fprintf (stderr, "file is shorter than blocksize #%d @ %s\n", n, path);
				return 1;
			}
			flen[n] = s.st_size;
			addr[n] = mmap (0, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
			if (addr[n] == MAP_FAILED) {
				fprintf (stderr, "Could not mmap file #%d @ %s (%s)\n", n, path, strerror (errno));
				return 1;
			}
		}
#endif
	}

	data = (char*) malloc (sizeof (char) * block_size);
	uint64_t _read = 0;
	double max_elapsed = 0;
	double total_time = 0;
	double var_m = 0;
	double var_s = 0;
	uint64_t cnt = 0;

	build_thread_pool (nthreads, block_size);

	while (1) {
		gint64 before;
		before = g_get_monotonic_time();

		if (run_thread_pool (files, nfiles)) {
			fprintf (stderr, "thread pool error\n");
			goto out;
		}

		_read += block_size;
		gint64 elapsed = g_get_monotonic_time() - before;
		double bandwidth = ((nfiles * block_size)/1048576.0) / (elapsed/1000000.0);

		if (!quiet) {
			printf ("# BW @ %lu %.3f seconds bandwidth %.4f MB/sec\n", (long unsigned int)_read, elapsed/1000000.0, bandwidth);
		}

		total_time += elapsed;

		++cnt;
		if (max_elapsed == 0) {
			var_m = elapsed;
		} else {
			const double var_m1 = var_m;
			var_m = var_m + (elapsed - var_m) / (double)(cnt);
			var_s = var_s + (elapsed - var_m) * (elapsed - var_m1);
		}

		if (elapsed > max_elapsed) {
			max_elapsed = elapsed;
		}

	}

out:
	if (max_elapsed > 0 && total_time > 0) {
		double stddev = cnt > 1 ? sqrt(var_s / ((double)(cnt-1))) : 0;
		double bandwidth = ((nfiles * _read)/1048576.0) / (total_time/1000000.0);
		double min_throughput = ((nfiles * block_size)/1048576.0) / (max_elapsed/1000000.0);
		printf ("# Min: %.4f MB/sec Avg: %.4f MB/sec  || Max: %.3f sec \n", min_throughput, bandwidth, max_elapsed/1000000.0);
		printf ("# Max Track count: %d @ 48000SPS\n", (int) floor(1048576.0 * bandwidth / (4 * 48000.)));
		printf ("# Sus Track count: %d @ 48000SPS\n", (int) floor(1048576.0 * min_throughput / (4 * 48000.)));
		printf ("# seeks: %llu: bytes: %llu total_time: %f\n", cnt * nfiles, (nfiles * _read), total_time/1000000.0);
		printf ("%d %.4f %.4f %.4f %.5f\n", block_size, min_throughput, bandwidth, max_elapsed/1000000.0, stddev/1000000.0);
	}

	return 0;
}