-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstat_perf_tool.c
144 lines (136 loc) · 4.25 KB
/
stat_perf_tool.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
#define _GNU_SOURCE
#include<fcntl.h>
#include<dirent.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<unistd.h>
#include<stddef.h>
#include<pthread.h>
#include<time.h>
#include<stdio.h>
int do_write;
long files_stat;
long files_setattr;
pthread_mutex_t lock;
void _print_entries(char *name, char *parent) {
DIR *dp = NULL;
char *path;
int alloc = 0;
int entry_len = 0, path_len = 0;
struct stat buf;
//printf("name %s par %s\n", name, parent);
if (parent) {
path_len = strlen(parent) + pathconf(parent, _PC_NAME_MAX) + 1;
path = malloc(path_len);
memset(path, 0, path_len);
sprintf(path, "%s/%s", parent, name);
alloc = 1;
} else {
path = name;
}
dp = opendir(path);
if (!dp) {
printf("Dir %s\n", path);
perror("Opendir failed");
exit(1);
}
entry_len = offsetof(struct dirent, d_name) + pathconf(path, _PC_NAME_MAX) + 1;
struct dirent *entry = malloc(entry_len);
memset(entry, 0, entry_len);
struct dirent *result;
readdir_r(dp, entry, &result);
while (result) {
if(strcmp(entry->d_name, ".") &&
strcmp(entry->d_name, "..")) {
int entry_path_len = strlen(path) + strlen(entry->d_name) + 2;
char *entry_path = malloc(entry_path_len);
memset(entry_path, 0, entry_path_len);
if (!entry_path) {
printf("No memory \n");
exit(1);
}
sprintf(entry_path, "%s/%s", path, entry->d_name);
memset(&buf, 0, sizeof(buf));
/*int fd = open(entry_path, O_DIRECT | O_RDWR | O_SYNC);
if(!fd) {
printf("File open failed %s\n", entry_path);
exit(1);
}*/
if (lstat(entry_path, &buf)) {
printf("Stat failed on path %s\n", entry_path);
exit(1);
} else {
if (do_write && !chmod(entry_path, buf.st_mode)) {
pthread_mutex_lock(&lock);
files_setattr++;
files_stat++;
pthread_mutex_unlock(&lock);
//printf("Stat and Chmod done %s\n", entry_path);
} else {
if (!do_write) {
pthread_mutex_lock(&lock);
files_stat++;
pthread_mutex_unlock(&lock);
//printf("Stat done on %s\n", entry_path);
} else {
printf("Chmod failed on %s\n", entry_path);
exit(1);
}
}
}
//close(fd);
free(entry_path);
}
if (entry->d_type == DT_DIR) {
if (!strcmp(entry->d_name, ".") ||
!strcmp(entry->d_name, "..")) {
memset(entry, 0, entry_len);
readdir_r(dp, entry, &result);
continue;
}
_print_entries(entry->d_name, path);
}
//printf("%s\n", entry->d_name);
memset(entry, 0, entry_len);
readdir_r(dp, entry, &result);
}
closedir(dp);
if (alloc)
free(path);
if (entry)
free(entry);
}
void * print_entries(void *name) {
_print_entries((char *)name, NULL);
pthread_exit(NULL);
}
int main(int argc, char *argv[]) {
if (argc != 4) {
printf("Usage program <path> <do_write> <num_threads>\n");
exit(1);
}
int i = 0;
do_write = atoi(argv[2]);
int num_threads = atoi(argv[3]);
pthread_t *threads = malloc(sizeof(pthread_t) * num_threads);
pthread_mutex_init(&lock, NULL);
for (i = 0; i < num_threads; i++) {
int ret = pthread_create(&threads[i], NULL, print_entries, (void *)argv[1]);
if (ret) {
printf("Thread creation failed %d\n", i);
exit(1);
}
}
for (i = 0; i < num_threads; i++) {
void *status;
int ret = pthread_join(threads[i], &status);
if (ret) {
printf("Thread join faied %d\n", i);
exit(1);
}
}
printf("Files stat %ld Files setattr %ld\n", files_stat, files_setattr);
pthread_exit(NULL);
//print_entries(argv[1], NULL);
}