File: memlock.c

package info (click to toggle)
fio 3.41-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,012 kB
  • sloc: ansic: 82,290; python: 9,862; sh: 6,067; makefile: 813; yacc: 204; lex: 184
file content (63 lines) | stat: -rw-r--r-- 1,179 bytes parent folder | download | duplicates (4)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

static struct thread_data {
	unsigned long mib;
} td;

static void *worker(void *data)
{
	struct thread_data *td = data;
	unsigned long index;
	size_t size;
	char *buf;
	int i, first = 1;

	size = td->mib * 1024UL * 1024UL;
	buf = malloc(size);

	for (i = 0; i < 100000; i++) {
		for (index = 0; index + 4096 < size; index += 4096)
			memset(&buf[index+512], 0x89, 512);
		if (first) {
			printf("loop%d: did %lu MiB\n", i+1, td->mib);
			first = 0;
		}
	}
	free(buf);
	return NULL;
}

int main(int argc, char *argv[])
{
	unsigned long mib, threads;
	pthread_t *pthreads;
	int i;

	if (argc < 3) {
		printf("%s: <MiB per thread> <threads>\n", argv[0]);
		return 1;
	}

	mib = strtoul(argv[1], NULL, 10);
	threads = strtoul(argv[2], NULL, 10);
	if (threads < 1 || threads > 65536) {
		printf("%s: invalid 'threads' argument\n", argv[0]);
		return 1;
	}

	pthreads = calloc(threads, sizeof(pthread_t));
	td.mib = mib;

	for (i = 0; i < threads; i++)
		pthread_create(&pthreads[i], NULL, worker, &td);

	for (i = 0; i < threads; i++) {
		void *ret;

		pthread_join(pthreads[i], &ret);
	}
	return 0;
}