-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.c
194 lines (168 loc) · 5.18 KB
/
log.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "log.h"
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
// #define DEBUG
#define PAGE_SIZE 4096
static FILE *handle_log(const char *name, long id, const char *mode) {
char path[MAX_PATH_LEN];
logpath(path, name, id);
FILE *log = fopen(path, mode);
if (!log) {
perror("File open failed");
exit(1);
}
return log;
}
FILE *new_log(const char *name, long id) {
return handle_log(name, id, "w");
}
FILE *open_log(const char *name, long id) {
return handle_log(name, id, "r");
}
int new_mapped_log_path(const char *path, struct mapped_log *log) {
log->fd = open(path, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if (log->fd == -1) {
perror("creat in new_mapped_log");
exit(1);
}
#ifdef DEBUG
fprintf(stderr, "%s fd %d\n", path, log->fd);
#endif
if (ftruncate(log->fd, LOG_BUFFER_SIZE) == -1) {
perror("ftruncate in new_mapped_log");
exit(1);
}
log->start = log->buf = mmap(0, LOG_BUFFER_SIZE, PROT_WRITE|PROT_READ, MAP_SHARED, log->fd, 0);
if (log->buf == MAP_FAILED) {
perror("mmap in new_mapped_log");
exit(1);
}
log->end = log->start + LOG_BUFFER_SIZE;
if (madvise(log->start, LOG_BUFFER_SIZE, MADV_SEQUENTIAL) == -1) {
perror("madvise in new_mapped_log");
exit(1);
}
return 0;
}
int new_mapped_log(const char *name, int id, struct mapped_log *log) {
char path[MAX_PATH_LEN];
logpath(path, name, id);
return new_mapped_log_path(path, log);
}
int enlarge_mapped_log(struct mapped_log *log) {
struct stat sb;
if (fstat(log->fd, &sb) == -1) {
perror("fstat in enlarge_mapped_log");
exit(1);
}
off_t original_size = sb.st_size;
assert(original_size % LOG_BUFFER_SIZE == 0);
#ifdef DEBUG
fprintf(stderr, "fd %d unmap start %p buf %p ", log->fd, log->start, log->buf);
#endif
if (munmap(log->start, log->end - log->start) == -1) {
perror("munmap in enlarge_mapped_log");
exit(1);
}
if (ftruncate(log->fd, original_size + LOG_BUFFER_SIZE) == -1) {
perror("ftruncate in enlarge_mapped_log");
exit(1);
}
int map_size = LOG_BUFFER_SIZE + PAGE_SIZE;
log->start = mmap(0, map_size, PROT_WRITE|PROT_READ, MAP_SHARED,
log->fd, original_size - PAGE_SIZE);
if (log->start == MAP_FAILED) {
perror("mmap in enlarge_mapped_log");
exit(1);
}
log->end = log->start + map_size;
long page_offset = (long)log->buf & 0xFFF;
// If page offset is 0, means we need to start on the new page.
log->buf = log->start + (page_offset ? page_offset : PAGE_SIZE);
#ifdef DEBUG
fprintf(stderr, "new start: %p buf: %p truncate to %ld bytes\n", log->start,
log->buf, (long)(original_size + LOG_BUFFER_SIZE));
#endif
if (madvise(log->start, map_size, MADV_SEQUENTIAL) == -1) {
perror("madvise in enlarge_mapped_log");
exit(1);
}
return 0;
}
int open_mapped_log_path(const char *path, struct mapped_log *log) {
log->fd = open(path, O_RDONLY);
if (log->fd == -1) {
// Make start, buf and end all the same. This marks that the log is empty.
log->start = log->end = log->buf = NULL;
return -1;
}
struct stat sb;
if (fstat(log->fd, &sb) == -1) {
perror("fstat");
exit(1);
}
log->start = log->buf = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, log->fd, 0);
if (log->buf == MAP_FAILED) {
perror("mmap");
exit(1);
}
log->end = log->buf + sb.st_size;
if (madvise(log->buf, sb.st_size, MADV_SEQUENTIAL) == -1) {
perror("madvise");
exit(1);
}
return 0;
}
int open_mapped_log(const char *name, int id, struct mapped_log *log) {
char path[MAX_PATH_LEN];
logpath(path, name, id);
return open_mapped_log_path(path, log);
}
// Truncate unused part of the log.
int truncate_log(struct mapped_log *log) {
struct stat sb;
if (fstat(log->fd, &sb) == -1) {
perror("fstat in unmap log");
exit(1);
}
int ret = 0;
if ((ret = ftruncate(log->fd, sb.st_size - (log->end - log->buf))) == -1) {
printf("size: total %ld, garbage: %ld\n", sb.st_size, log->end - log->buf);
perror("ftruncate in unmap_log");
}
return ret;
}
int unmap_log(struct mapped_log *log) {
if (munmap(log->start, log->end - log->start) == -1) {
perror("munmap in unmap_log");
return -1;
}
return 0;
}
int unmap_truncate_log(struct mapped_log *log) {
int ret = unmap_log(log);
if (ret != 0)
return ret;
return truncate_log(log);
}
void *create_mapped_file(const char *path, unsigned long size) {
int fd = open(path, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if (fd == -1) {
perror("open in create_mapped_file");
exit(1);
}
if (ftruncate(fd, size) == -1) {
perror("ftruncate in create_mapped_file");
exit(1);
}
void *buf = mmap(0, size, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED) {
perror("mmap in create_mapped_file");
exit(1);
}
return buf;
}