aboutsummaryrefslogtreecommitdiff
path: root/tests/ubi-tests/mkvol_paral.c
blob: 95b5e104ee1598b967543f3e69d7db5f96a52f54 (plain)
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
/*
 * Copyright (c) International Business Machines Corp., 2006
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Author: Artem B. Bityutskiy
 *
 * This test creates and deletes volumes in parallel.
 */

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include "libubi.h"
#define PROGRAM_NAME "mkvol_paral"
#include "common.h"
#include "helpers.h"

#define THREADS_NUM 4
#define ITERATIONS  500

static libubi_t libubi;
static struct ubi_dev_info dev_info;
const char *node;
static int iterations = ITERATIONS;

/**
 * the_thread - the testing thread.
 *
 * @ptr  thread number
 */
static void * the_thread(void *ptr)
{
	int n = (long)ptr, iter = iterations;
	struct ubi_mkvol_request req;
	const char *name =  PROGRAM_NAME ":the_thread()";
	char nm[strlen(name) + 50];

	req.alignment = 1;
	req.bytes = dev_info.avail_bytes/ITERATIONS;
	req.vol_type = UBI_DYNAMIC_VOLUME;
	sprintf(nm, "%s:%d", name, n);
	req.name = nm;

	while (iter--) {
		req.vol_id = UBI_VOL_NUM_AUTO;
		if (ubi_mkvol(libubi, node, &req)) {
			failed("ubi_mkvol");
			return NULL;
		}
		if (ubi_rmvol(libubi, node, req.vol_id)) {
			failed("ubi_rmvol");
			return NULL;
		}
	}

	return NULL;
}

int main(int argc, char * const argv[])
{
	int i, ret;
	pthread_t threads[THREADS_NUM];

	if (initial_check(argc, argv))
		return 1;

	node = argv[1];

	libubi = libubi_open();
	if (libubi == NULL) {
		failed("libubi_open");
		return 1;
	}

	if (ubi_get_dev_info(libubi, node, &dev_info)) {
		failed("ubi_get_dev_info");
		goto close;
	}

	for (i = 0; i < THREADS_NUM; i++) {
		ret = pthread_create(&threads[i], NULL, &the_thread, (void*)(long)i);
		if (ret) {
			failed("pthread_create");
			goto close;
		}
	}

	for (i = 0; i < THREADS_NUM; i++)
		pthread_join(threads[i], NULL);

	libubi_close(libubi);
	return 0;

close:
	libubi_close(libubi);
	return 1;
}