summaryrefslogtreecommitdiff
path: root/unittest/maria/trxman-t.c
blob: a29bf5abc8e676caaabda4684c2c4a77a3739d86 (plain)
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
/* Copyright (C) 2006 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include <tap.h>

#include <my_global.h>
#include <my_sys.h>
#include <my_atomic.h>
#include <lf.h>
#include "../../storage/maria/trxman.h"

pthread_attr_t rt_attr;
pthread_mutex_t rt_mutex;
pthread_cond_t rt_cond;
int rt_num_threads;

int litmus;

/* template for a test: the goal is to have litmus==0 if the test passed

#define ITER nnn
pthread_handler_t test_XXXXXXXX(void *arg)
{
  int    m=(*(int *)arg)/ITER, x;

  for (x=((int)(intptr)(&m)); m ; m--)
  {
    // do something with litmus
  }
  // do something more with litmus

  pthread_mutex_lock(&rt_mutex);
  rt_num_threads--;
  if (!rt_num_threads)
  {
    diag("whatever diagnostics we want", blabla, foobar);
    pthread_cond_signal(&rt_cond);
  }
  pthread_mutex_unlock(&rt_mutex);
  return 0;
}
#undef ITER

*/

/*
  create and end (commit or rollback) transactions randomly
*/
#define MAX_ITER 100
pthread_handler_t test_trxman(void *arg)
{
  int    m=(*(int *)arg);
  uint   x, y, i, j, n;
  TRX    *trx[MAX_ITER];

  for (x=((int)(intptr)(&m)); m > 0; )
  {
    y= x= (x*3628273133 + 1500450271) % 9576890767; /* three prime numbers */
    m-= n= x % MAX_ITER;
    for (i=0; i < n; i++)
      trx[i]=trxman_new_trx();
    for (i=0; i < n; i++)
    {
      y=(y*19 + 7) % 31;
      trxman_end_trx(trx[i], y & 1);
    }
  }

  pthread_mutex_lock(&rt_mutex);
  rt_num_threads--;
  if (!rt_num_threads)
    pthread_cond_signal(&rt_cond);
  pthread_mutex_unlock(&rt_mutex);
  return 0;
}
#undef MAX_ITER

void run_test(const char *test, pthread_handler handler, int n, int m)
{
  pthread_t t;
  ulonglong now=my_getsystime();

  litmus= 0;

  diag("Testing %s with %d threads, %d iterations... ", test, n, m);
  for (rt_num_threads=n ; n ; n--)
    pthread_create(&t, &rt_attr, handler, &m);
  pthread_mutex_lock(&rt_mutex);
  while (rt_num_threads)
    pthread_cond_wait(&rt_cond, &rt_mutex);
  pthread_mutex_unlock(&rt_mutex);
  now=my_getsystime()-now;
  ok(litmus == 0, "tested %s in %g secs (%d)", test, ((double)now)/1e7, litmus);
}

int global_malloc=0;
int main()
{
  plan(1);

  if (my_atomic_initialize())
    return exit_status();

  my_init();

  pthread_attr_init(&rt_attr);
  pthread_attr_setdetachstate(&rt_attr,PTHREAD_CREATE_DETACHED);
  pthread_mutex_init(&rt_mutex, 0);
  pthread_cond_init(&rt_cond, 0);

#define CYCLES 10000
#define THREADS 10

  trxman_init();
  run_test("trxman", test_trxman, THREADS,CYCLES);
  trxman_destroy();
  diag("mallocs: %d\n", global_malloc);

  pthread_mutex_destroy(&rt_mutex);
  pthread_cond_destroy(&rt_cond);
  pthread_attr_destroy(&rt_attr);
  my_end(0);
  return exit_status();
}