xref: /trafficserver/src/tscore/test_freelist.cc (revision 4cfd5a73)
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 #include <cstdlib>
25 #include <cstring>
26 #include "tscore/ink_thread.h"
27 #include "tscore/ink_queue.h"
28 
29 #define NTHREADS 64
30 InkFreeList *flist = nullptr;
31 
32 void *
33 test(void *d)
34 {
35   int id;
36   void *m1, *m2, *m3;
37 
38   id = (intptr_t)d;
39 
40   time_t start = time(nullptr);
41   int count    = 0;
42   for (;;) {
43     m1 = ink_freelist_new(flist);
44     m2 = ink_freelist_new(flist);
45     m3 = ink_freelist_new(flist);
46 
47     if ((m1 == m2) || (m1 == m3) || (m2 == m3)) {
48       printf("0x%08" PRIx64 "   0x%08" PRIx64 "   0x%08" PRIx64 "\n", static_cast<uint64_t>((uintptr_t)m1),
49              static_cast<uint64_t>((uintptr_t)m2), static_cast<uint64_t>((uintptr_t)m3));
50       exit(1);
51     }
52 
53     memset(m1, id, 64);
54     memset(m2, id, 64);
55     memset(m3, id, 64);
56 
57     ink_freelist_free(flist, m1);
58     ink_freelist_free(flist, m2);
59     ink_freelist_free(flist, m3);
60 
61     // break out of the test if we have run more then 60 seconds
62     if (++count % 1000 == 0 && (start + 60) < time(nullptr)) {
63       return nullptr;
64     }
65   }
66 }
67 
68 int
69 main(int /* argc ATS_UNUSED */, char * /*argv ATS_UNUSED */ [])
70 {
71   int i;
72 
73   flist = ink_freelist_create("woof", 64, 256, 8);
74 
75   for (i = 0; i < NTHREADS; i++) {
76     fprintf(stderr, "Create thread %d\n", i);
77     ink_thread_create(nullptr, test, (void *)(static_cast<intptr_t>(i)), 0, 0, nullptr);
78   }
79 
80   test((void *)NTHREADS);
81 
82   return 0;
83 }
84