Skip to content

Commit 7a3c1f2

Browse files
committed
dtmd improved tests 🐮
1 parent c25adce commit 7a3c1f2

File tree

6 files changed

+110
-161
lines changed

6 files changed

+110
-161
lines changed

contrib/pg_xtm/dtmd/src/main.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -352,21 +352,21 @@ static char *onnoise(void *client, cmd_t *cmd) {
352352
return strdup("-");
353353
}
354354

355-
static float now_s() {
356-
// current time in seconds
357-
struct timespec t;
358-
if (clock_gettime(CLOCK_MONOTONIC, &t) == 0) {
359-
return t.tv_sec + t.tv_nsec * 1e-9;
360-
} else {
361-
printf("Error while clock_gettime()\n");
362-
exit(0);
363-
}
364-
}
355+
// static float now_s() {
356+
// // current time in seconds
357+
// struct timespec t;
358+
// if (clock_gettime(CLOCK_MONOTONIC, &t) == 0) {
359+
// return t.tv_sec + t.tv_nsec * 1e-9;
360+
// } else {
361+
// printf("Error while clock_gettime()\n");
362+
// exit(0);
363+
// }
364+
// }
365365

366366
static char *oncmd(void *client, cmd_t *cmd) {
367367
//shout_cmd(client, cmd);
368368

369-
float started = now_s();
369+
// float started = now_s();
370370
char *result = NULL;
371371
switch (cmd->cmd) {
372372
case CMD_BEGIN:
@@ -387,8 +387,8 @@ static char *oncmd(void *client, cmd_t *cmd) {
387387
default:
388388
return onnoise(client, cmd);
389389
}
390-
float elapsed = now_s() - started;
391-
shout("cmd '%c' processed in %0.4f sec\n", cmd->cmd, elapsed);
390+
// float elapsed = now_s() - started;
391+
// shout("cmd '%c' processed in %0.4f sec\n", cmd->cmd, elapsed);
392392
return result;
393393
}
394394

contrib/pg_xtm/libdtm.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -161,29 +161,29 @@ bool DtmGlobalStartTransaction(DTMConn dtm, GlobalTransactionId *gtid) {
161161
return ok;
162162
}
163163

164-
void DtmInitSnapshot(Snapshot snapshot)
165-
{
166-
if (snapshot->xip == NULL)
167-
{
168-
/*
169-
* First call for this snapshot. Snapshot is same size whether or not
170-
* we are in recovery, see later comments.
171-
*/
172-
snapshot->xip = (TransactionId *)
173-
malloc(GetMaxSnapshotXidCount() * sizeof(TransactionId));
174-
if (snapshot->xip == NULL)
175-
ereport(ERROR,
176-
(errcode(ERRCODE_OUT_OF_MEMORY),
177-
errmsg("out of memory")));
178-
Assert(snapshot->subxip == NULL);
179-
snapshot->subxip = (TransactionId *)
180-
malloc(GetMaxSnapshotSubxidCount() * sizeof(TransactionId));
181-
if (snapshot->subxip == NULL)
182-
ereport(ERROR,
183-
(errcode(ERRCODE_OUT_OF_MEMORY),
184-
errmsg("out of memory")));
185-
}
186-
}
164+
// void DtmInitSnapshot(Snapshot snapshot)
165+
// {
166+
// if (snapshot->xip == NULL)
167+
// {
168+
// /*
169+
// * First call for this snapshot. Snapshot is same size whether or not
170+
// * we are in recovery, see later comments.
171+
// */
172+
// snapshot->xip = (TransactionId *)
173+
// malloc(GetMaxSnapshotXidCount() * sizeof(TransactionId));
174+
// if (snapshot->xip == NULL)
175+
// ereport(ERROR,
176+
// (errcode(ERRCODE_OUT_OF_MEMORY),
177+
// errmsg("out of memory")));
178+
// Assert(snapshot->subxip == NULL);
179+
// snapshot->subxip = (TransactionId *)
180+
// malloc(GetMaxSnapshotSubxidCount() * sizeof(TransactionId));
181+
// if (snapshot->subxip == NULL)
182+
// ereport(ERROR,
183+
// (errcode(ERRCODE_OUT_OF_MEMORY),
184+
// errmsg("out of memory")));
185+
// }
186+
// }
187187

188188
// Asks DTM for a fresh snapshot. Returns 'true' on success, or 'false'
189189
// otherwise.
@@ -213,7 +213,7 @@ bool DtmGlobalGetSnapshot(DTMConn dtm, NodeId nodeid, TransactionId xid, Snapsho
213213
s->xcnt = number;
214214
Assert(s->xcnt == number); // the number should definitely fit into xcnt field size
215215

216-
DtmInitSnapshot(s);
216+
// DtmInitSnapshot(s);
217217
for (i = 0; i < s->xcnt; i++) {
218218
if (!dtm_read_hex16(dtm, &number)) return false;
219219
s->xip[i] = number;

contrib/pg_xtm/libdtm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ DTMConn DtmConnect(char *host, int port);
2020
// bad things will happen.
2121
void DtmDisconnect(DTMConn dtm);
2222

23-
void DtmInitSnapshot(Snapshot snapshot);
23+
// void DtmInitSnapshot(Snapshot snapshot);
2424

2525
typedef struct {
2626
TransactionId* xids;

contrib/pg_xtm/libdtm/Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
CC=clang -DTEST
2-
CFLAGS=-g -Wall -I"../../../src/include"
2+
CFLAGS=-g -Wall -I"../../../src/include"
33

44

5-
all: lib/libdtm.a bin/example bin/bench
5+
all: lib/libdtm.a bin/example bin/bench bin/redbench
66

77
bin/bench: bindir lib/libdtm.a src/bench.c
88
$(CC) $(CFLAGS) -o bin/bench -Iinclude -Llib src/bench.c -ldtm
99

10+
bin/redbench: bindir lib/libdtm.a src/redbench.c
11+
$(CC) $(CFLAGS) -o bin/redbench -Iinclude -Llib src/redbench.c -ldtm -lhiredis -I"/usr/local/include/hiredis/"
12+
1013
bin/example: bindir lib/libdtm.a src/example.c
1114
$(CC) $(CFLAGS) -o bin/example -Iinclude -Llib src/example.c -ldtm
1215

contrib/pg_xtm/libdtm/src/bench.c

Lines changed: 22 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -4,149 +4,50 @@
44

55
#include "libdtm.h"
66

7-
#define NODES 1
7+
#define unless(x) if (!(x))
88

9-
#ifdef WIN32
10-
#include <windows.h>
11-
static float li2f(LARGE_INTEGER x) {
12-
float result = ((float)x.HighPart) * 4.294967296E9 + (float)((x).LowPart);
13-
return result;
14-
}
9+
#define NODES 1
1510

16-
static float now_s() {
17-
LARGE_INTEGER freq, count;
18-
QueryPerformanceFrequency(&freq);
19-
QueryPerformanceCounter(&count);
20-
return li2f(count) / li2f(freq);
21-
}
22-
#else
23-
static float now_s() {
24-
// current time in seconds
25-
struct timespec t;
26-
if (clock_gettime(CLOCK_MONOTONIC, &t) == 0) {
27-
return t.tv_sec + t.tv_nsec * 1e-9;
28-
} else {
29-
printf("Error while clock_gettime()\n");
30-
exit(0);
31-
}
32-
}
33-
#endif
3411

35-
static void start_transaction(DTMConn conn, TransactionId base) {
12+
int main(int argc, char **argv) {
3613
GlobalTransactionId gtid;
14+
TransactionId base = 42;
15+
int transactions = 10000;
16+
int i;
3717
gtid.nNodes = NODES;
3818
gtid.xids = malloc(sizeof(TransactionId) * gtid.nNodes);
3919
gtid.nodes = malloc(sizeof(NodeId) * gtid.nNodes);
4020

41-
int n;
42-
for (n = 0; n < gtid.nNodes; n++) {
43-
gtid.xids[n] = base + n;
44-
gtid.nodes[n] = n;
45-
}
46-
47-
if (!DtmGlobalStartTransaction(conn, &gtid)) {
48-
fprintf(stdout, "global transaction not started\n");
49-
exit(EXIT_FAILURE);
21+
DTMConn conn = DtmConnect("localhost", 5431);
22+
if (!conn) {
23+
exit(1);
5024
}
51-
//fprintf(stdout, "global transaction started\n");
5225

53-
free(gtid.xids);
54-
free(gtid.nodes);
55-
}
26+
for (i = 0; i < transactions; i++) {
5627

57-
static void commit_transaction(DTMConn conn, TransactionId base) {
58-
int n;
59-
for (n = 0; n < NODES; n++) {
60-
if (!DtmGlobalSetTransStatus(conn, n, base + n, TRANSACTION_STATUS_COMMITTED)) {
61-
fprintf(stdout, "global transaction not committed\n");
62-
exit(EXIT_FAILURE);
28+
int n;
29+
for (n = 0; n < gtid.nNodes; n++) {
30+
gtid.xids[n] = base + n;
31+
gtid.nodes[n] = n;
6332
}
64-
}
65-
//fprintf(stdout, "global transaction committed\n");
66-
}
6733

68-
static void abort_transaction(DTMConn conn, TransactionId base) {
69-
if (!DtmGlobalSetTransStatus(conn, 0, base + 0, TRANSACTION_STATUS_ABORTED)) {
70-
fprintf(stdout, "global transaction not aborted\n");
71-
exit(EXIT_FAILURE);
72-
}
73-
//fprintf(stdout, "global transaction aborted\n");
74-
}
75-
76-
static void show_snapshots(DTMConn conn, TransactionId base) {
77-
int i, n;
78-
for (n = 0; n < NODES; n++) {
79-
Snapshot s = malloc(sizeof(SnapshotData));
80-
s->xip = NULL;
81-
82-
if (!DtmGlobalGetSnapshot(conn, n, base + n, s)) {
83-
fprintf(stdout, "failed to get a snapshot[%d]\n", n);
34+
if (!DtmGlobalStartTransaction(conn, &gtid)) {
35+
fprintf(stdout, "global transaction not started\n");
8436
exit(EXIT_FAILURE);
8537
}
86-
//fprintf(stdout, "snapshot[%d, %#x]: xmin = %#x, xmax = %#x, active =", n, base + n, s->xmin, s->xmax);
87-
//for (i = 0; i < s->xcnt; i++) {
88-
// fprintf(stdout, " %#x", s->xip[i]);
89-
//}
90-
//fprintf(stdout, "\n");
91-
92-
free(s->xip);
93-
free(s);
94-
}
95-
}
9638

97-
static void show_status(DTMConn conn, TransactionId base) {
98-
int n;
99-
for (n = 0; n < NODES; n++) {
100-
XidStatus s = DtmGlobalGetTransStatus(conn, n, base + n);
101-
if (s == -1) {
102-
fprintf(stdout, "failed to get transaction status [%d, %#x]\n", n, base + n);
39+
if (!DtmGlobalSetTransStatus(conn, 0, base + 0, TRANSACTION_STATUS_COMMITTED)) {
40+
fprintf(stdout, "global transaction not committed\n");
10341
exit(EXIT_FAILURE);
10442
}
105-
//fprintf(stdout, "status[%d, %#x]: ", n, base + n);
106-
//switch (s) {
107-
// case TRANSACTION_STATUS_COMMITTED:
108-
// fprintf(stdout, "committed\n");
109-
// break;
110-
// case TRANSACTION_STATUS_ABORTED:
111-
// fprintf(stdout, "aborted\n");
112-
// break;
113-
// case TRANSACTION_STATUS_IN_PROGRESS:
114-
// fprintf(stdout, "in progress\n");
115-
// break;
116-
// default:
117-
// fprintf(stdout, "(error)\n");
118-
// break;
119-
//}
120-
}
121-
}
12243

123-
int main(int argc, char **argv) {
124-
DTMConn conn = DtmConnect("localhost", 5431);
125-
if (!conn) {
126-
exit(1);
127-
}
128-
129-
TransactionId base = 42;
44+
unless (i%10) {
45+
printf("Commited %u txs.\n", i+1);
46+
}
13047

131-
int transactions = atoi(argv[1]);
132-
int i;
133-
float started = now_s();
134-
for (i = 0; i < transactions; i++) {
135-
printf("-------- %d, base = %d -------- %0.6f sec \n", i, base, now_s() - started);
136-
start_transaction(conn, base);
137-
//show_snapshots(conn, base);
138-
//show_status(conn, base);
139-
commit_transaction(conn, base);
140-
base += NODES;
48+
base++;
14149
}
142-
float elapsed = now_s() - started;
14350

144-
printf(
145-
"%d transactions in %0.2f sec -> %0.2f tps\n",
146-
transactions,
147-
elapsed,
148-
transactions / elapsed
149-
);
15051

15152
DtmDisconnect(conn);
15253
return EXIT_SUCCESS;

contrib/pg_xtm/libdtm/src/redbench.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <hiredis.h>
2+
#include <execinfo.h>
3+
#include <stdio.h>
4+
5+
static redisContext *c = NULL;
6+
static redisReply *reply;
7+
const char *hostname = "127.0.0.1";
8+
static int port = 6379;
9+
static struct timeval timeout = { 1, 500000 }; // 1.5 seconds
10+
11+
12+
int main(){
13+
char buf[100];
14+
int xid;
15+
int i;
16+
17+
18+
if (c == NULL) {
19+
c = redisConnectWithTimeout(hostname, port, timeout);
20+
if (c->err) {
21+
printf("Connection error: %s\n", c->errstr);
22+
redisFree(c);
23+
} else {
24+
printf("Connected to redis \n");
25+
}
26+
}
27+
28+
for (i=0; i<100000; i++){
29+
reply = redisCommand(c,"INCR pgXid");
30+
xid = reply->integer;
31+
freeReplyObject(reply);
32+
33+
// sprintf(buf, "HSET pgXids tx%u 0", xid);
34+
// reply = redisCommand(c, buf);
35+
// freeReplyObject(reply);
36+
37+
// sprintf(buf, "HSET pgXids tx%u 1", xid);
38+
// reply = redisCommand(c, buf);
39+
// freeReplyObject(reply);
40+
}
41+
42+
43+
44+
}
45+

0 commit comments

Comments
 (0)