-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc.cpp
More file actions
186 lines (163 loc) · 5.11 KB
/
calc.cpp
File metadata and controls
186 lines (163 loc) · 5.11 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <fcntl.h>
#include <math.h>
#include "util.h"
#include <iostream>
#include <fstream>
using namespace std;
#define REST 0
#define PREPARE 1
#define RESOLVE 2
#define CAPTURE 3
#define COMPLETE 4
#define LINE_SIZE 4096
typedef struct {
long long int size;
char *live;
char *stable;
int *bit;
pthread_mutex_t *live_lock;
pthread_mutex_t *stable_lock;
int STATE;
} database;
database global_db;
int is_finished = 0; //程序是否结束
long long int throughput = 0; // 系统最大并行度
long long int active = 0, prepare = 0, complete = 0;
int *sec_throughput;
long long int timestamp = 0;
void load_db(long long int size) {
global_db.size = size;
global_db.live = (char *) malloc(global_db.size * LINE_SIZE);
global_db.stable = (char *) malloc(global_db.size * LINE_SIZE);
global_db.bit = (int *) malloc(global_db.size * sizeof(int));
global_db.live_lock = (pthread_mutex_t *) malloc(global_db.size * sizeof(pthread_mutex_t));
global_db.stable_lock = (pthread_mutex_t *) malloc(global_db.size * sizeof(pthread_mutex_t));
global_db.STATE = REST;
long long int i = 0;
while (i < global_db.size) {
pthread_mutex_init(&(global_db.live_lock[i]), NULL);
pthread_mutex_init(&(global_db.stable_lock[i]), NULL);
i++;
}
}
void ApplyWrite(int start_state, long long int index) {
if (start_state == PREPARE) {
//if (global_db.bit[index] == 0)
memcpy(global_db.stable + LINE_SIZE * index, global_db.live + LINE_SIZE * index, LINE_SIZE);
} else if (start_state == RESOLVE || start_state == CAPTURE) {
//if (global_db.bit[index] == 0) {
memcpy(global_db.stable + LINE_SIZE * index, global_db.live + LINE_SIZE * index, LINE_SIZE);
global_db.bit[index] = 1;
//}
} else if (start_state == COMPLETE || start_state == REST) {
if (global_db.stable[index]) {
}
}
// set each filed data.
int k = 0;
int rnd;
rnd = rand();
while (k++ < 1024) {
memcpy(global_db.live + LINE_SIZE * index + 4 * k, &rnd, 4);
}
}
// 采用两阶段锁操作并发事务
void Execute(int start_state) {
int i = 0;
long long int index[3];
while (i < 3) {
index[i] = rand() % (global_db.size); //int value1 = rand();
ApplyWrite(start_state, index[i]);
i++;
}
int commit_state = global_db.STATE;
if (start_state == PREPARE) {
if (commit_state == RESOLVE) {
i = 0;
while (i < 3) {
global_db.bit[index[i]] = 1;
i++;
}
}
}
//cout<<start_state;
++sec_throughput[timestamp];
}
void *transaction(void *info) {
while (is_finished == 0) {
int start_state = global_db.STATE;
if (start_state == REST || start_state == COMPLETE) {
__sync_fetch_and_add(&active, 1);
Execute(start_state);
__sync_fetch_and_sub(&active, 1);
} else if (start_state == PREPARE) {
__sync_fetch_and_add(&prepare, 1);
Execute(start_state);
__sync_fetch_and_sub(&prepare, 1);
} else if (start_state == CAPTURE || start_state == RESOLVE) {
__sync_fetch_and_add(&complete, 1);
Execute(start_state);
__sync_fetch_and_sub(&complete, 1);
}
}
}
void *timer(void *info) {
while (is_finished == 0) {
sleep(1);
//printf("%d\n",sec_throughput[timestamp]);
++timestamp;
}
}
void checkpointer(int num) {
while (num--) {
global_db.STATE = REST;
sleep(1);
global_db.STATE = PREPARE;
while (active > 0);
global_db.STATE = RESOLVE;
while (prepare > 0);
global_db.STATE = CAPTURE;
long long int i = 0;
ofstream ckp_fd("dump.dat", ios::binary);
while (i < global_db.size) {
if (global_db.bit[i] == 1) {
ckp_fd.write((global_db.stable + i * LINE_SIZE), LINE_SIZE);
} else if (global_db.bit[i] == 0) {
global_db.bit[i] == 1;
ckp_fd.write(global_db.live + i * LINE_SIZE, LINE_SIZE);
}
i++;
}
global_db.STATE = COMPLETE;
while (complete > 0);
}
is_finished = 1;
}
int main(int argc, char const *argv[]) {
srand((unsigned) time(NULL));
load_db(atoi(argv[1]));
sec_throughput = (int *) malloc(3600 * sizeof(int)); // assume running 1000s
for (int j = 0; j < 1000; ++j) {
sec_throughput[j] = 0;
}
throughput = atoi(argv[2]);
for (int i = 0; i < throughput; ++i) {
pthread_t pid_t;
pthread_create(&pid_t, NULL, transaction, NULL);
}
pthread_t time_thread;
pthread_create(&time_thread, NULL, timer, NULL);
checkpointer(5);
long long int sum = 0;
for (int i = timestamp / 4; i < timestamp / 4 * 3; ++i) {
sum += sec_throughput[i];
}
printf("CALC,%d,%lld,%f\n", atoi(argv[1]), throughput, (float) sum / timestamp * 2);
return 0;
}