-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathzigzag.c
More file actions
123 lines (107 loc) · 3.45 KB
/
zigzag.c
File metadata and controls
123 lines (107 loc) · 3.45 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
#include"system.h"
#include"zigzag.h"
extern db_server DBServer;
int db_zigzag_init(void *zigzag_info, size_t db_size)
{
db_zigzag_infomation *info;
info = zigzag_info;
info->db_size = db_size;
if (NULL == (info->db_zigzag_as0 = (char *) numa_alloc_onnode(DBServer.unitSize * db_size , 1))) {
perror("db_zigzag_as0 malloc error");
return -1;
}
memset(info->db_zigzag_as0, 'S', DBServer.unitSize * db_size);
if (NULL == (info->db_zigzag_as1 = (char *) numa_alloc_onnode(DBServer.unitSize * db_size , 1))) {
perror("db_zigzag_sa1 malloc error");
return -1;
}
memset(info->db_zigzag_as0, 'S', DBServer.unitSize * db_size);
if (NULL == (info->db_zigzag_mr = (unsigned char *) numa_alloc_onnode(db_size , 1))) {
perror("db_zigzag_mr malloc error");
return -1;
}
memset(info->db_zigzag_mr, 0, db_size);
if (NULL == (info->db_zigzag_mw = (unsigned char *) numa_alloc_onnode(db_size , 1))) {
perror("db_zigzag_mw malloc error");
return -1;
}
memset(info->db_zigzag_mw, 1, db_size);
info->db_zigzag_lock = UNLOCK;
return 0;
}
void* zigzag_read(size_t index)
{
if (index > (DBServer.zigzagInfo).db_size)
index = index % (DBServer.zigzagInfo).db_size;
if (0 == (DBServer.zigzagInfo).db_zigzag_mr[index]) {
return(void *) ((DBServer.zigzagInfo).db_zigzag_as0 + index * DBServer.unitSize);
} else {
return(void *) ((DBServer.zigzagInfo).db_zigzag_as1 + index * DBServer.unitSize);
}
}
int zigzag_write(size_t index, void *value)
{
index = index % (DBServer.zigzagInfo).db_size;
if (0 == (DBServer.zigzagInfo).db_zigzag_mw[index]) {
memcpy((DBServer.zigzagInfo).db_zigzag_as0 + index * DBServer.unitSize , value, 4);
} else {
memcpy((DBServer.zigzagInfo).db_zigzag_as1 + index * DBServer.unitSize , value, 4);
}
(DBServer.zigzagInfo).db_zigzag_mr[index] = (DBServer.zigzagInfo).db_zigzag_mw[index];
return 0;
}
void db_zigzag_ckp(int ckp_order, void *zigzag_info)
{
int ckpfd;
char ckp_name[128];
size_t i;
size_t db_size;
db_zigzag_infomation *info;
long long timeStart;
long long timeEnd;
info = zigzag_info;
sprintf(ckp_name, "./ckp_backup/dump_%d", ckp_order);
if (-1 == (ckpfd = open(ckp_name,O_WRONLY |O_TRUNC | O_SYNC | O_CREAT,666))) {
perror("checkpoint file open error,checkout if the ckp_backup directory is exist");
return;
}
db_size = info->db_size;
//pthread_spin_lock(&(DBServer.presync));
db_lock(&(DBServer.pre_lock));
timeStart = get_ntime();
for (i = 0; i < db_size; i++) {
info->db_zigzag_mw[i] = !(info->db_zigzag_mr[i]);
}
timeEnd = get_ntime();
//pthread_spin_unlock(&(DBServer.presync));
db_unlock(&(DBServer.pre_lock));
add_prepare_log(&DBServer,timeEnd - timeStart);
//write to disk
#ifndef OFF_DUMP
timeStart = get_utime();
for (i = 0; i < db_size; i++) {
if (0 == info->db_zigzag_mw[i]) {
write(ckpfd,info->db_zigzag_as1 + (size_t)i * DBServer.unitSize,
(size_t)DBServer.unitSize);
lseek(ckpfd,0,SEEK_END);
} else {
write(ckpfd,info->db_zigzag_as0 + (size_t)i * DBServer.unitSize,
(size_t)DBServer.unitSize);
lseek(ckpfd,0,SEEK_END);
}
}
#endif
fsync(ckpfd);
close(ckpfd);
timeEnd = get_utime();
add_overhead_log(&DBServer,timeEnd - timeStart);
}
void db_zigzag_destroy(void *zigzag_info)
{
db_zigzag_infomation *info;
info = zigzag_info;
numa_free(info->db_zigzag_as0 , DBServer.unitSize * info->db_size);
numa_free(info->db_zigzag_as1 , DBServer.unitSize * info->db_size);
numa_free(info->db_zigzag_mr , info->db_size);
numa_free(info->db_zigzag_mw , info->db_size);
}