~ps10gel/ubuntu/xenial/trafficserver/6.2.0

« back to all changes in this revision

Viewing changes to lib/ts/test_atomic.cc

  • Committer: Bazaar Package Importer
  • Author(s): Arno Toell
  • Date: 2011-01-13 11:49:18 UTC
  • Revision ID: james.westby@ubuntu.com-20110113114918-vu422h8dknrgkj15
Tags: upstream-2.1.5-unstable
ImportĀ upstreamĀ versionĀ 2.1.5-unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** @file
 
2
 
 
3
  A brief file description
 
4
 
 
5
  @section license License
 
6
 
 
7
  Licensed to the Apache Software Foundation (ASF) under one
 
8
  or more contributor license agreements.  See the NOTICE file
 
9
  distributed with this work for additional information
 
10
  regarding copyright ownership.  The ASF licenses this file
 
11
  to you under the Apache License, Version 2.0 (the
 
12
  "License"); you may not use this file except in compliance
 
13
  with the License.  You may obtain a copy of the License at
 
14
 
 
15
      http://www.apache.org/licenses/LICENSE-2.0
 
16
 
 
17
  Unless required by applicable law or agreed to in writing, software
 
18
  distributed under the License is distributed on an "AS IS" BASIS,
 
19
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
20
  See the License for the specific language governing permissions and
 
21
  limitations under the License.
 
22
 */
 
23
 
 
24
 
 
25
 
 
26
////////////////////////////////////////////////////////////////////////////
 
27
// NT Build Notes:
 
28
//   1) Enable the following #include.
 
29
//      Visual C++ does not allow this under #ifdef
 
30
// #include "stdafx.h"
 
31
//
 
32
//   2) Create a new project within the existing TS project
 
33
//      which is a "WIN32 Console Application"
 
34
//      with the "Simple Application" option.
 
35
//   3) Replace C/C++ Preprocessor definitions with traffic_server
 
36
//      definitions.
 
37
//   4) Replace C/C++ Additional includes with traffic server
 
38
//      definitions.  Prepend "..\proxy" to directories in proxy.
 
39
//      Add "..\proxy".
 
40
//   5) Replace C/C++ project option "/MLd" with "/MTd"
 
41
//   6) Add libts as dependency.
 
42
////////////////////////////////////////////////////////////////////////////
 
43
 
 
44
#include <unistd.h>
 
45
#include <stdlib.h>
 
46
#include <time.h>
 
47
#include <poll.h>
 
48
#include <pthread.h>
 
49
#include "ink_atomic.h"
 
50
#include "ink_queue.h"
 
51
#include "ink_thread.h"
 
52
#include "ink_unused.h" /* MAGIC_EDITING_TAG */
 
53
 
 
54
 
 
55
#ifndef LONG_ATOMICLIST_TEST
 
56
 
 
57
#define MAX_ALIST_TEST 10
 
58
#define MAX_ALIST_ARRAY 100000
 
59
InkAtomicList al[MAX_ALIST_TEST];
 
60
void *al_test[MAX_ALIST_TEST][MAX_ALIST_ARRAY];
 
61
volatile int al_done = 0;
 
62
 
 
63
void *
 
64
testalist(void *ame)
 
65
{
 
66
  int me = (int) (uintptr_t) ame;
 
67
  int j, k;
 
68
  for (k = 0; k < MAX_ALIST_ARRAY; k++)
 
69
    ink_atomiclist_push(&al[k % MAX_ALIST_TEST], &al_test[me][k]);
 
70
  void *x;
 
71
  for (j = 0; j < 1000000; j++)
 
72
    if ((x = ink_atomiclist_pop(&al[me])))
 
73
      ink_atomiclist_push(&al[rand() % MAX_ALIST_TEST], x);
 
74
  ink_atomic_increment((int *) &al_done, 1);
 
75
  return NULL;
 
76
}
 
77
#endif // !LONG_ATOMICLIST_TEST
 
78
 
 
79
#ifdef LONG_ATOMICLIST_TEST
 
80
/************************************************************************/
 
81
#define MAX_ATOMIC_LISTS        (4 * 1024)
 
82
#define MAX_ITEMS_PER_LIST      (1 * 1024)
 
83
#define MAX_TEST_THREADS        64
 
84
static InkAtomicList alists[MAX_ATOMIC_LISTS];
 
85
struct listItem *items[MAX_ATOMIC_LISTS * MAX_ITEMS_PER_LIST];
 
86
 
 
87
struct listItem
 
88
{
 
89
  int data1;
 
90
  int data2;
 
91
  void *link;
 
92
  int data3;
 
93
  int data4;
 
94
  int check;
 
95
};
 
96
 
 
97
void
 
98
init_data()
 
99
{
 
100
  int j;
 
101
  int ali;
 
102
  struct listItem l;
 
103
  struct listItem *plistItem;
 
104
 
 
105
  for (ali = 0; ali < MAX_ATOMIC_LISTS; ali++)
 
106
    ink_atomiclist_init(&alists[ali], "alist", ((char *) &l.link - (char *) &l));
 
107
 
 
108
  for (ali = 0; ali < MAX_ATOMIC_LISTS; ali++) {
 
109
    for (j = 0; j < MAX_ITEMS_PER_LIST; j++) {
 
110
      plistItem = (struct listItem *) malloc(sizeof(struct listItem));
 
111
      items[ali + j] = plistItem;
 
112
      plistItem->data1 = ali + j;
 
113
      plistItem->data2 = ali + rand();
 
114
      plistItem->link = 0;
 
115
      plistItem->data3 = j + rand();
 
116
      plistItem->data4 = ali + j + rand();
 
117
      plistItem->check = (plistItem->data1 ^ plistItem->data2 ^ plistItem->data3 ^ plistItem->data4);
 
118
      ink_atomiclist_push(&alists[ali], plistItem);
 
119
    }
 
120
  }
 
121
}
 
122
 
 
123
void
 
124
cycle_data(void *d)
 
125
{
 
126
  InkAtomicList *l;
 
127
  struct listItem *pli;
 
128
  struct listItem *pli_next;
 
129
  int iterations;
 
130
  int me;
 
131
 
 
132
  me = (int) d;
 
133
  iterations = 0;
 
134
 
 
135
  while (1) {
 
136
    l = &alists[(me + rand()) % MAX_ATOMIC_LISTS];
 
137
 
 
138
    pli = (struct listItem *) ink_atomiclist_popall(l);
 
139
    if (!pli)
 
140
      continue;
 
141
 
 
142
    // Place listItems into random queues
 
143
    while (pli) {
 
144
      assert((pli->data1 ^ pli->data2 ^ pli->data3 ^ pli->data4) == pli->check);
 
145
      pli_next = (struct listItem *) pli->link;
 
146
      pli->link = 0;
 
147
      ink_atomiclist_push(&alists[(me + rand()) % MAX_ATOMIC_LISTS], (void *) pli);
 
148
      pli = pli_next;
 
149
    }
 
150
    iterations++;
 
151
    poll(0, 0, 10);             // 10 msec delay
 
152
    if ((iterations % 100) == 0)
 
153
      printf("%d ", me);
 
154
  }
 
155
}
 
156
 
 
157
/************************************************************************/
 
158
#endif // LONG_ATOMICLIST_TEST
 
159
 
 
160
int
 
161
main(int argc, const char *argv[])
 
162
{
 
163
#ifndef LONG_ATOMICLIST_TEST
 
164
  int32_t m = 1, n = 100;
 
165
  //int64 lm = 1LL, ln = 100LL;
 
166
  const char* m2 = "hello";
 
167
  char* n2;
 
168
 
 
169
  printf("sizeof(int32_t)==%d   sizeof(void *)==%d\n", (int)sizeof(int32_t), (int)sizeof(void *));
 
170
 
 
171
 
 
172
  printf("CAS: %d == 1  then  2\n", m);
 
173
  n = ink_atomic_cas(&m, 1, 2);
 
174
  printf("changed to: %d,  result=%s\n", m, n ? "true" : "false");
 
175
 
 
176
  printf("CAS: %d == 1  then  3\n", m);
 
177
  n = ink_atomic_cas(&m, 1, 3);
 
178
  printf("changed to: %d,  result=%s\n", m, n ? "true" : "false");
 
179
 
 
180
  printf("CAS pointer: '%s' == 'hello'  then  'new'\n", m2);
 
181
  n = ink_atomic_cas_ptr((pvvoidp) &m2, (char *) "hello", (char *) "new");
 
182
  printf("changed to: %s, result=%s\n", m2, n ? (char *) "true" : (char *) "false");
 
183
 
 
184
  printf("CAS pointer: '%s' == 'hello'  then  'new2'\n", m2);
 
185
  n = ink_atomic_cas_ptr((pvvoidp)&m2, (char*)m2, (char *) "new2");
 
186
  printf("changed to: %s, result=%s\n", m2, n ? "true" : "false");
 
187
 
 
188
  n = 100;
 
189
  printf("Atomic Inc of %d\n", n);
 
190
  m = ink_atomic_increment((int *) &n, 1);
 
191
  printf("changed to: %d,  result=%d\n", n, m);
 
192
 
 
193
 
 
194
  printf("Atomic Fetch-and-Add 2 to pointer to '%s'\n", m2);
 
195
  n2 = (char *)ink_atomic_increment_ptr((pvvoidp)&m2, 2);
 
196
  printf("changed to: %s,  result=%s\n", m2, n2);
 
197
 
 
198
  printf("Testing atomic lists\n");
 
199
  {
 
200
    int ali;
 
201
    srand(time(NULL));
 
202
    printf("sizeof(al_test) = %d\n", (int)sizeof(al_test));
 
203
    memset(&al_test[0][0], 0, sizeof(al_test));
 
204
    for (ali = 0; ali < MAX_ALIST_TEST; ali++)
 
205
      ink_atomiclist_init(&al[ali], "foo", 0);
 
206
    for (ali = 0; ali < MAX_ALIST_TEST; ali++) {
 
207
      ink_thread tid;
 
208
      pthread_attr_t attr;
 
209
 
 
210
      pthread_attr_init(&attr);
 
211
#if !defined(freebsd)
 
212
      pthread_attr_setstacksize(&attr, 1024 * 1024);
 
213
#endif
 
214
      ink_assert(pthread_create(&tid, &attr, testalist, (void *) ali) == 0);
 
215
    }
 
216
    while (al_done != MAX_ALIST_TEST)
 
217
      sleep(1);
 
218
  }
 
219
#endif // !LONG_ATOMICLIST_TEST
 
220
 
 
221
#ifdef LONG_ATOMICLIST_TEST
 
222
  printf("Testing atomic lists (long version)\n");
 
223
  {
 
224
    int id;
 
225
 
 
226
    init_data();
 
227
    for (id = 0; id < MAX_TEST_THREADS; id++) {
 
228
      assert(thr_create(NULL, 0, cycle_data, (void *) id, THR_NEW_LWP, NULL) == 0);
 
229
    }
 
230
  }
 
231
  while (1) {
 
232
    poll(0, 0, 10);             // 10 msec delay
 
233
  }
 
234
#endif // LONG_ATOMICLIST_TEST
 
235
 
 
236
  return 0;
 
237
}