-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapreduce.c
More file actions
164 lines (134 loc) · 4.73 KB
/
Copy pathmapreduce.c
File metadata and controls
164 lines (134 loc) · 4.73 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <string.h>
#include <unistd.h>
#include "mapreduce.h"
#include "common.h"
size_t partition(char* src, size_t start, size_t size, size_t LIMIT, bool last, char DELIM) {
if (last || start + size >= LIMIT) {
return LIMIT;
}
size_t pos = start + size;
while (pos < LIMIT) {
if (*(src + pos) == DELIM) {
return pos - 1;
}
++pos;
}
return pos;
}
void mapreduce(MAPREDUCE_SPEC * spec, MAPREDUCE_RESULT * result)
{
DATA_SPLIT* partitions[spec->split_num];
pid_t partition_pids[spec->split_num];
int partition_fout[spec->split_num];
int fd;
if ((fd = open(spec->input_data_filepath, O_RDONLY)) < 0) {
perror("Failed to open file");
return;
}
struct stat fd_stat;
if ((fstat(fd, &fd_stat)) < 0) {
perror("Failed to open file");
return;
}
char *src;
if ((src = (char *)mmap(NULL, fd_stat.st_size, PROT_READ,
MAP_SHARED, fd, 0)) == MAP_FAILED) {
perror("Failed to map file bytes into memory");
return;
}
size_t pos = 0;
for (int i = 0; i < spec->split_num; ++i) {
partitions[i] = mmap(NULL, sizeof(DATA_SPLIT),
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if ((partitions[i]->fd = open(spec->input_data_filepath, O_RDONLY)) < 0) {
printf("[fd %d] ", i);
perror("Failed to open fd");
}
lseek(partitions[i]->fd, pos, SEEK_SET);
size_t index;
if (spec->usr_data == NULL) {
index = partition(src, pos,
fd_stat.st_size / spec->split_num,
fd_stat.st_size, false, ' ');
} else {
index = partition(src, pos,
fd_stat.st_size / spec->split_num,
fd_stat.st_size, false, '\n');
}
partitions[i]->size = index;
pos = index + 1;
}
if (munmap(src, fd_stat.st_size) < 0) {
perror("Failed to unmap memory");
return;
}
if (close(fd) < 0) {
perror("Failed to close file");
return;
}
struct timeval start, end;
if (NULL == spec || NULL == result) {
EXIT_ERROR(ERROR, "NULL pointer!\n");
}
gettimeofday(&start, NULL);
if (spec->usr_data != NULL) {
for (int i = 0; i < spec->split_num; ++i) {
partitions[i]->usr_data = malloc(sizeof(char) * strlen(spec->usr_data) + 1);
memset(partitions[i]->usr_data, 0, strlen(spec->usr_data) + 1);
memcpy(partitions[i]->usr_data, spec->usr_data, strlen(spec->usr_data));
}
}
for (int i = 0; i < spec->split_num - 1; ++i) {
if ((partition_pids[i] = fork()) < 0) {
perror("Failed to fork process");
return;
} else if (partition_pids[i] == 0) {
char itm_file[32];
sprintf(itm_file, "partition-%d.itm", i);
partition_fout[i] = open(itm_file, O_RDWR |
O_CREAT | O_TRUNC, 0644);
spec->map_func(partitions[i], partition_fout[i]);
exit(0);
} else {
*(result->map_worker_pid + i) = partition_pids[i];
}
}
char itm_file[32];
sprintf(itm_file, "partition-%d.itm", spec->split_num - 1);
partition_fout[spec->split_num - 1] = open(itm_file, O_RDWR |
O_CREAT | O_TRUNC, 0644);
spec->map_func(partitions[spec->split_num - 1], partition_fout[spec->split_num - 1]);
*(result->map_worker_pid + spec->split_num - 1) = getpid();
int status, waiting_on = spec->split_num - 1;
while (waiting_on > 0) {
wait(&status);
--waiting_on;
}
for (int i = 0; i < spec->split_num; ++i) {
close(partition_pids[i]);
char itm[32];
sprintf(itm, "partition-%d.itm", i);
partition_fout[i] = open(itm, O_RDONLY, 0644);
}
int fd_rst = open(result->filepath, O_RDWR | O_CREAT | O_TRUNC, 0644);
result->reduce_worker_pid = getpid();
spec->reduce_func(partition_fout, spec->split_num, fd_rst);
gettimeofday(&end, NULL);
result->processing_time = ( end.tv_sec - start.tv_sec) * US_PER_SEC +
(end.tv_usec - start.tv_usec);
for (int i = 0; i < spec->split_num; ++i) {
free(partitions[i]->usr_data);
if (munmap(partitions[i], sizeof(DATA_SPLIT))) {
perror("Failed to unmap memory for DATA_SPLIT");
}
}
}