-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathseqv1.sc
More file actions
565 lines (499 loc) · 14.2 KB
/
seqv1.sc
File metadata and controls
565 lines (499 loc) · 14.2 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*
** Copyright (c) 2005, 2007 Ingre Corp.
** This file is distributed under the CA Trusted Open Source License (CATOSL).
** For the exact terms of the license go to http://ca.com/opensource/catosl
**
** Stress Test Application Suite
**
** seqv1.sc
**
** History:
**
** 06-Oct-2005 sarjo01: Created from updv1 + seq1
** 14-Oct-2005 sarjo01: Feature sync up
** 31-May-2007 (Ralph Loen) Bug 118428
** Ported to VMS.
*/
#ifdef _WIN32
#include <windows.h>
#define pthread_t int
#else
#include <pthread.h>
#define HANDLE int
#define min(a,b) ((a<b)?a:b)
#define stricmp strcasecmp
#define strnicmp strncasecmp
#endif
#include <stdio.h>
#include <stdlib.h>
EXEC SQL include sqlca;
#define MAXCHILDTHREADS 99
#define MAXKEY 2147483647
#define MAXVERBOSE 2
#define MINVERBOSE 0
void doit(int *p);
void checkit();
void print_err();
void print_sqlcode_exit();
void createobjs(int createall);
void cleanup(int cleanall);
HANDLE h[MAXCHILDTHREADS];
int xactscompleted[MAXCHILDTHREADS];
int deadlocks[MAXCHILDTHREADS];
int lockwaits[MAXCHILDTHREADS];
int fatalerrs[MAXCHILDTHREADS];
int rollbacks[MAXCHILDTHREADS];
int seqcounts[MAXCHILDTHREADS];
int nthreads = 8;
int iters = 1000;
int lockwait = 0;
int rbfreq = 0;
int verbose = MINVERBOSE;
int xperconn = 0;
int abortfatal = 0;
int running;
int cache = 1000;
char *syntax =
"\n"
"Syntax: seqv1 <database> <function> [ <option> <option> ... ]\n\n"
"<database> target database name\n" \
"<function> init - initialize seqv1 database objects\n"
" run - execute seqv1 program, display results\n"
" cleanup - delete all seqv1 database objects\n"
"<option> program option of the form -x[value]\n\n"
" Option Function Description Param Values Default\n"
" ------ -------- ------------------------------ ---------------- -------\n"
" -b run Set forced rollback frequency 0 to 100 0\n"
" -c init Set cache value (0=no caching) 0 to 10000 1000\n"
" -i run Set transaction count (per 1 to 1000000 1000\n"
" thread)\n"
" -t run Set no. of client threads 1 to 99 8\n"
" -v run Set verbose output level 0 to 2 0\n"
" 0=no output\n"
" -w run Set lock wait seconds 0 to 10 0\n"
" 99=nowait\n"
" -x run Set no. of transactions per 1 to 1000, 0\n"
" connection 0=never disconn\n"
" -y run Abort thread on fatal error none disabled\n"
"\n";
EXEC SQL begin declare section;
char *dbname;
char starttime[257];
char endtime[257];
char etime[257];
char stmtbuff[1024];
EXEC SQL end declare section;
int irng(int val, int minval, int maxval)
{
if (val < minval)
val = minval;
else if (val > maxval)
val = maxval;
return val;
}
main(int argc, char *argv[])
{
int i, intparm;
char *a;
int param[MAXCHILDTHREADS];
pthread_t lpThreadId[MAXCHILDTHREADS];
# ifdef VMS
pthread_attr_t attr;
size_t stksize = 320000;
void *stkaddr;
int statp;
# endif
if (argc < 3)
{
printf(syntax);
exit(-1);
}
dbname = argv[1];
/*
** Process command line options
*/
for (i = 3; i < argc ; i++)
{
if (*argv[i] == '-' && *(argv[i]+1) != '\0')
{
intparm = atoi(argv[i]+2);
switch (toupper(*(argv[i]+1)))
{
case 'B':
rbfreq = irng(intparm, 0, 100);
break;
case 'C':
cache = irng(intparm, 0, 10000);
break;
case 'I':
iters = irng(intparm, 1, 1000000);
break;
case 'T':
nthreads = irng(intparm, 1, MAXCHILDTHREADS);
break;
case 'V':
verbose = irng(intparm, MINVERBOSE, MAXVERBOSE);
break;
case 'W':
if (intparm == 99)
lockwait = 99;
else
lockwait = irng(intparm, 0, 10);
break;
case 'X':
xperconn = irng(intparm, 1, 1000);
break;
case 'Y':
abortfatal = 1;
break;
}
}
}
running = nthreads;
EXEC SQL whenever sqlerror call print_sqlcode_exit;
EXEC SQL connect :dbname as 'mastercon';
EXEC SQL set autocommit on;
if (stricmp(argv[2], "cleanup") == 0)
{
cleanup(1);
EXEC SQL disconnect 'mastercon';
printf("\n");
exit(0);
}
if (stricmp(argv[2], "init") == 0)
{
createobjs(1);
EXEC SQL disconnect 'mastercon';
printf("\n");
exit(0);
}
else if (stricmp(argv[2], "run") != 0)
{
printf(syntax);
exit(-1);
}
memset(seqcounts, '\0', sizeof(seqcounts));
memset(xactscompleted, 0, sizeof(xactscompleted));
memset(deadlocks, 0, sizeof(deadlocks));
memset(lockwaits, 0, sizeof(lockwaits));
memset(fatalerrs, 0, sizeof(fatalerrs));
memset(rollbacks, 0, sizeof(fatalerrs));
createobjs(0);
for (i = 1; i <= nthreads; i++)
param[i-1] = i;
sprintf(stmtbuff,
"set random_seed %d",
iters * nthreads);
EXEC SQL execute immediate :stmtbuff;
#ifdef _WIN32
for (i = 0; i < nthreads; i++)
{
h[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)doit,
¶m[i], 0, &lpThreadId[i]);
}
if (verbose < MAXVERBOSE)
printf("STARTED %d THREADS...\n", nthreads);
EXEC SQL select date('now') into :starttime;
WaitForMultipleObjects(nthreads, h, 1, INFINITE);
#else
for (i = 0; i < nthreads; i++)
{
# ifdef VMS
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, stksize);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&lpThreadId[i],&attr, doit, ¶m[i]);
pthread_attr_destroy(&attr);
# else
pthread_create(&lpThreadId[i],NULL, doit, ¶m[i]);
# endif
}
if (verbose < MAXVERBOSE)
printf("STARTED %d THREADS...\n", nthreads);
EXEC SQL select date('now') into :starttime;
for (i = 0; i < nthreads; i++)
{
pthread_join(lpThreadId[i], NULL);
}
#endif
/*
** Child threads have all terminated.
*/
EXEC SQL select date('now') into :endtime;
if (verbose < MAXVERBOSE)
printf("%d THREADS DONE\n", nthreads);
checkit();
EXEC SQL commit;
EXEC SQL disconnect 'mastercon';
printf("\n");
exit(0);
}
void print_sqlcode_exit()
{
EXEC SQL begin declare section;
char dsp[550];
EXEC SQL end declare section;
EXEC SQL inquire_sql (:dsp = ERRORTEXT);
printf("%s\n", dsp);
exit(-1);
}
void print_err(int ecode)
{
EXEC SQL begin declare section;
char dsp[550];
EXEC SQL end declare section;
EXEC SQL inquire_sql (:dsp = ERRORTEXT);
printf("(%d) %s\n", ecode, dsp);
}
/*
** Child thread entry point
*/
void doit(int *p)
{
EXEC SQL begin declare section;
int pval, dval;
int hk,lk,rb,tabid;
char connectName[45];
char nodename[65];
char stmtbuff[257];
EXEC SQL end declare section;
int upded, loopcnt, q, cmd, xs, xst, i,j,k, error_code, reconn, neverdisc;
int updedx, rows, trows, dorb;
char *rbstr;
pval = *p;
tabid = pval;
loopcnt = iters;
xst = xs = xperconn;
if (verbose == MAXVERBOSE)
printf("T%02d START...\n", pval);
/*
** Generate a distinct connection name string
*/
sprintf(connectName, "slavecon%d", pval);
neverdisc = (xst == 0) ? 1 : 0;
reconn = 1;
/*
** Main loop
*/
for (i = 0; i < loopcnt; i++)
{
retry2:
if (reconn == 1)
{
EXEC SQL whenever sqlerror goto CONNerrorhandler;
EXEC SQL connect :dbname as :connectName;
reconn = 0;
EXEC SQL whenever sqlerror goto SQLerrorhandler;
EXEC SQL set autocommit off;
EXEC SQL set session with on_error = rollback transaction;
if (lockwait == 99)
EXEC SQL set lockmode session where level=row, timeout=nowait;
else
{
sprintf(stmtbuff,
"set lockmode session where level=row, timeout=%d",
lockwait);
EXEC SQL execute immediate :stmtbuff;
}
}
/*
** Transaction's queries begin here
*/
EXEC SQL repeated select random(1, 100) into :rb;
dorb = (rb > rbfreq) ? 0 : 1;
if (dorb == 1)
rbstr = "ROLLBACK";
else
rbstr = "";
retry1:
cmd = 1;
q = 1;
sprintf(stmtbuff,
"insert into seqv1tbl%02d select seqv1seq1.nextval",
tabid);
EXEC SQL execute immediate :stmtbuff;
if (dorb == 1)
{
EXEC SQL rollback;
rollbacks[pval-1]++;
}
else
{
EXEC SQL commit;
seqcounts[pval - 1]++;
xactscompleted[pval-1]++;
}
if (verbose == MAXVERBOSE)
{
printf("T%02d:%07d %s\n", pval, i+1, rbstr);
}
if (neverdisc == 0 && --xs == 0)
{
EXEC SQL disconnect :connectName;
xs = xst;
reconn = 1;
}
continue;
SQLerrorhandler:
error_code = sqlca.sqlcode;
if (error_code == -49900)
{
EXEC SQL commit;
if (verbose > MINVERBOSE)
printf("T%02d: DEADLOCK (%d,%d)\n",
pval, cmd, q);
deadlocks[pval-1]++;
goto retry1;
}
else if (error_code == -39100)
{
EXEC SQL commit;
if (verbose > MINVERBOSE)
printf("T%02d: LOCKWAIT TIMEOUT (%d,%d)\n",
pval, cmd, q);
lockwaits[pval-1]++;
PCsleep(1000);
goto retry1;
}
else /* fatal SQL error */
{
print_err(error_code);
EXEC SQL whenever sqlerror continue;
EXEC SQL rollback;
EXEC SQL disconnect :connectName;
printf("T%02d: Fatal SQL error (%d,%d)\n",
pval, cmd, q);
fatalerrs[pval-1]++;
reconn = 1;
if (abortfatal)
break;
else
goto retry2;
}
CONNerrorhandler:
printf("Connect error: %s\n", connectName);
print_err(sqlca.sqlcode);
break;
}
if (reconn == 0)
EXEC SQL disconnect :connectName;
running--;
if (verbose == MAXVERBOSE)
printf("T%02d DONE, %d running\n", pval, running);
}
void cleanup(int cleanall)
{
EXEC SQL begin declare section;
int i;
EXEC SQL end declare section;
EXEC SQL whenever sqlerror continue;
EXEC SQL set autocommit on;
printf("Cleaning up...\n");
if (cleanall)
{
EXEC SQL drop sequence seqv1seq1;
EXEC SQL drop table sessionseqv1;
}
for (i = 1; i <= MAXCHILDTHREADS; i++)
{
sprintf(stmtbuff, "drop table seqv1tbl%02d", i);
EXEC SQL execute immediate :stmtbuff;
}
EXEC SQL commit;
}
/*
** Function to create db objects
*/
void createobjs(int createall)
{
EXEC SQL begin declare section;
int i, keyval;
EXEC SQL end declare section;
EXEC SQL set autocommit on;
cleanup(createall);
EXEC SQL whenever sqlerror call print_sqlcode_exit;
printf("Creating objects...\n");
for (i = 1; i <= MAXCHILDTHREADS; i++)
{
sprintf(stmtbuff,
"create table seqv1tbl%02d (seqval int) with page_size=4096",
i);
EXEC SQL execute immediate :stmtbuff;
}
if (createall)
{
if (cache == 0)
EXEC SQL create sequence seqv1seq1 nocache;
else
{
sprintf(stmtbuff, "create sequence seqv1seq1 cache %d", cache);
EXEC SQL execute immediate :stmtbuff;
}
EXEC SQL create table sessionseqv1 (seqval int);
EXEC SQL modify sessionseqv1 to btree on seqval;
}
EXEC SQL commit;
}
/*
** Function to check database integrity and display results
** at completion of run.
*/
void checkit()
{
EXEC SQL begin declare section;
int totrows, totxacts, totdeadlocks, totlockwaits;
int totx, totfatal;
int totsecs;
int totrollbacks, totseqsexp;
int totalseq, distseq, minseq, maxseq;
EXEC SQL end declare section;
int i;
totfatal = totxacts = totdeadlocks = totlockwaits = 0;
totrollbacks = totseqsexp = 0;
printf("\nCompiling results...\n\n");
for (i=0; i < nthreads; i++)
{
totseqsexp += seqcounts[i];
totxacts += xactscompleted[i];
totdeadlocks += deadlocks[i];
totlockwaits += lockwaits[i];
totfatal += fatalerrs[i];
totrollbacks += rollbacks[i];
}
EXEC SQL select
int4(interval('seconds', date(:endtime) - date(:starttime)))
into :totsecs;
if (totsecs == 0)
totsecs = 1;
EXEC SQL select date(:endtime) - date(:starttime) into :etime;
for (i = 1; i <= nthreads; i++)
{
sprintf(stmtbuff,
"insert into sessionseqv1 select * from seqv1tbl%02d", i);
EXEC SQL execute immediate :stmtbuff;
}
EXEC SQL select count(*), count(distinct seqval),
min(seqval), max(seqval)
into :totalseq, :distseq, :minseq, :maxseq
from sessionseqv1;
printf(" Data Integrity Check\n");
printf("-------------------------------------\n");
printf(" Nextvals taken : % 8d\n", totalseq);
printf(" Distinct values : % 8d\n", distseq);
printf(" Min value : % 8d\n", minseq);
printf(" Max value : % 8d\n\n", maxseq);
printf(" Runtime Summary\n");
printf("-------------------------------------------------\n");
printf(" Start time : %s\n", starttime);
printf(" End time : %s\n", endtime);
printf(" Elapsed time : %s\n", etime);
printf(" Threads started : %d\n", nthreads);
printf(" Deadlocks : %d\n", totdeadlocks);
printf(" Lockwait timeouts : %d\n", totlockwaits);
printf(" Fatal SQL errors : %d\n", totfatal);
printf(" Committed transactions : %d\n", totxacts);
printf(" Forced rollbacks : %d\n", totrollbacks);
printf(" Total transactions : %d\n", totxacts+totrollbacks);
printf(" TPS : %d\n",
(totxacts+totrollbacks)/totsecs);
}