/
main.c
163 lines (122 loc) · 3.36 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
//#include <attr/xattr.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "util.h"
const int FILE_NAME_LEN = 16;
inline long getus()
{
struct timeval t;
gettimeofday(&t, NULL);
return t.tv_sec*1000000+t.tv_usec;
}
inline long getusinterval(long start)
{
struct timeval t;
gettimeofday(&t, NULL);
return (long) (t.tv_sec*1000000+t.tv_usec - start);
}
inline char randomChar()
{
char a;
do {
a = rand() % ('z'+1);
}
while(a<'0' || (a>'9' && a<'A') || (a>'Z' && a<'a') || a>'z');
return a;
}
inline char* randomStr(int len)
{
static char str[4096] = {0};
int i;
for(i=0;i<len && i<4096; i++)
str[i] = randomChar();
str[4095]=0;
str[len-1]=0;
return str;
}
int main(int argc, char **argv)
{
int i;
srand ( time(NULL) );
if(argc < 4) {
printf("usage: mdtest dir num_of_files bytes_of_extattr_per_file\n");
return -1;
}
const char *dir = argv[1];
struct stat st;
if(stat(dir,&st) != 0) {
printf("Directory %s is not present\n", dir);
return -2;
}
if(chdir(dir) != 0) {
printf("Can not set %s as a working directory\n", dir);
return -3;
}
printf("Dir OK!\n");
int num_files = atoi(argv[2]);
if(num_files <=0) {
printf("Invalid number of files %d\n", num_files);
return -3;
}
int attr_size = atoi(argv[3]);
if(attr_size < 0) {
printf("Invalid size of ext. attributes %d\n", attr_size);
return -3;
}
printf("Using dir %s. Creating %d files. Writing %d bytes of extended attributes per file.\n\n", dir, num_files,attr_size);
char *file_names = (char*) calloc((FILE_NAME_LEN+1)*sizeof(char), num_files);
for(i=0; i<num_files; i++) {
memcpy(file_names+(i*(FILE_NAME_LEN+1)), randomStr(FILE_NAME_LEN), FILE_NAME_LEN+1);
(file_names+i*(FILE_NAME_LEN+1))[FILE_NAME_LEN-1] = '7';
}
char *attrval = NULL;
if (attr_size > 0 ) {
attrval = calloc(1, attr_size+1);
for(i=0; i<attr_size; i++)
attrval[i]=randomChar();
}
progress_bar_t pbar;
init_progress_bar(&pbar, "Creating files");
update_progress_bar(&pbar, .0);
long start = getus();
int report_every = num_files/1000>0?num_files/1000:1;
for(i=0; i<num_files; i++) {
char *fname=file_names+(i*(FILE_NAME_LEN+1));
if(mknod(fname, S_IFREG|0666, 0) ==0 ) {
if(attr_size>0) {
if(setxattr(fname, "user.mdtest.test", attrval, attr_size, 0) != 0) {
if(ENOTSUP == errno)
printf("Extended attributes not supported.\n");
else if(ENOSPC == errno)
printf("No space for extended attributes.\n");
else if(EEXIST == errno)
printf("Extended attributes exist.\n");
else if(EACCES == errno)
printf("EACCES.\n");
else if(EBADF == errno)
printf("EBADF.\n");
printf("Error setting extended attributes! Ignoring option.\n");
attr_size = 0;
}
}
}
if(i%report_every==0)
update_progress_bar(&pbar, (float)i/(float)num_files*100.);
}
long duration = getusinterval(start);
update_progress_bar(&pbar, 100.); printf("\n\n");
printf("Created %d files in %ld us. %f files/s\n", num_files, duration, (float)num_files/((float)duration/1000000.));
//for(i=0; i<num_files; i++) {
//unlink(file_names+(i*(FILE_NAME_LEN+1)));
//}
if(file_names) free(file_names);
if(attrval) free(attrval);
return 0;
}