libcopp  1.1.0
stack_pool_test.cpp
Go to the documentation of this file.
1 #include <cstdio>
2 #include <cstring>
3 #include <iostream>
4 #include <vector>
5 
7 #include <libcotask/task.h>
8 
9 #include "frame/test_macros.h"
10 
14 
16 };
18 
20 
21 static int stack_pool_test_task_action(void*) { return 0; }
22 
23 CASE_TEST(stack_pool_test, basic) {
25  std::vector<stack_pool_test_task_t::ptr_t> task_arr;
26  const size_t task_arr_sz = 64;
27 
28  // alloc
29  for (size_t i = 0; i < task_arr_sz; ++i) {
32  task_arr.push_back(tp);
33  }
34  CASE_EXPECT_EQ(task_arr_sz, global_stack_pool->get_limit().used_stack_number);
35  CASE_EXPECT_EQ(task_arr_sz * (global_stack_pool->get_stack_size() + global_stack_pool->get_stack_size_offset()),
36  global_stack_pool->get_limit().used_stack_size);
37 
38  CASE_EXPECT_EQ(0, global_stack_pool->get_limit().free_stack_number);
39  CASE_EXPECT_EQ(0, global_stack_pool->get_limit().free_stack_size);
40 
41  // recycle to free list
42  for (size_t i = 0; i < task_arr_sz / 2; ++i) {
43  task_arr.pop_back();
44  }
45  CASE_EXPECT_EQ(task_arr_sz - task_arr_sz / 2, global_stack_pool->get_limit().used_stack_number);
46  CASE_EXPECT_EQ(task_arr_sz / 2, global_stack_pool->get_limit().free_stack_number);
47  CASE_EXPECT_EQ((task_arr_sz - task_arr_sz / 2) * (global_stack_pool->get_stack_size() + global_stack_pool->get_stack_size_offset()),
48  global_stack_pool->get_limit().used_stack_size);
49  CASE_EXPECT_EQ((task_arr_sz / 2) * (global_stack_pool->get_stack_size() + global_stack_pool->get_stack_size_offset()),
50  global_stack_pool->get_limit().free_stack_size);
51 
52  // auto_gc
53  task_arr.pop_back();
54  CASE_EXPECT_EQ(task_arr_sz / 4, global_stack_pool->get_limit().free_stack_number);
55  CASE_EXPECT_EQ((task_arr_sz / 4) * (global_stack_pool->get_stack_size() + global_stack_pool->get_stack_size_offset()),
56  global_stack_pool->get_limit().free_stack_size);
57 
58  global_stack_pool.reset();
59 }
60 
61 CASE_TEST(stack_pool_test, full_number) {
63  // full
64 
65  std::vector<stack_pool_test_task_t::ptr_t> task_arr;
66  const size_t task_arr_sz = 64;
67  global_stack_pool->set_max_stack_number(task_arr_sz);
68  // alloc
69  for (size_t i = 0; i < task_arr_sz; ++i) {
72  CASE_EXPECT_TRUE(!!tp);
73  task_arr.push_back(tp);
74  }
75 
76  {
79  CASE_EXPECT_TRUE(!tp);
80  }
81 
82  global_stack_pool.reset();
83 }
84 
85 CASE_TEST(stack_pool_test, custom_gc) {
87  std::vector<stack_pool_test_task_t::ptr_t> task_arr;
88  const size_t task_arr_sz = 64;
89 
90  global_stack_pool->set_min_stack_number(task_arr_sz);
91 
92  // alloc
93  for (size_t i = 0; i < task_arr_sz; ++i) {
96  task_arr.push_back(tp);
97  }
98 
99  CASE_EXPECT_EQ(task_arr_sz, global_stack_pool->get_limit().used_stack_number);
100  CASE_EXPECT_EQ(task_arr_sz * (global_stack_pool->get_stack_size() + global_stack_pool->get_stack_size_offset()),
101  global_stack_pool->get_limit().used_stack_size);
102 
103  CASE_EXPECT_EQ(0, global_stack_pool->get_limit().free_stack_number);
104  CASE_EXPECT_EQ(0, global_stack_pool->get_limit().free_stack_size);
105 
106  for (size_t i = 0; i < task_arr_sz; ++i) {
107  task_arr.pop_back();
108  }
109 
110  CASE_EXPECT_EQ(0, global_stack_pool->get_limit().used_stack_number);
111  CASE_EXPECT_EQ(0, global_stack_pool->get_limit().used_stack_size);
112 
113  CASE_EXPECT_EQ(task_arr_sz, global_stack_pool->get_limit().free_stack_number);
114  CASE_EXPECT_EQ(task_arr_sz * (global_stack_pool->get_stack_size() + global_stack_pool->get_stack_size_offset()),
115  global_stack_pool->get_limit().free_stack_size);
116 
117  global_stack_pool->set_min_stack_number(0);
118  global_stack_pool.reset();
119 }
120 
121 CASE_TEST(stack_pool_test, gc_once) {
123  // gc_once
124  std::vector<stack_pool_test_task_t::ptr_t> task_arr;
125  const size_t task_arr_sz = 64;
126 
127  global_stack_pool->set_auto_gc(false);
128  global_stack_pool->set_gc_once_number(10);
129 
130  // alloc
131  for (size_t i = 0; i < task_arr_sz; ++i) {
134  task_arr.push_back(tp);
135  }
136 
137  CASE_EXPECT_EQ(task_arr_sz, global_stack_pool->get_limit().used_stack_number);
138  CASE_EXPECT_EQ(task_arr_sz * (global_stack_pool->get_stack_size() + global_stack_pool->get_stack_size_offset()),
139  global_stack_pool->get_limit().used_stack_size);
140 
141  CASE_EXPECT_EQ(0, global_stack_pool->get_limit().free_stack_number);
142  CASE_EXPECT_EQ(0, global_stack_pool->get_limit().free_stack_size);
143 
144  for (size_t i = 0; i < task_arr_sz; ++i) {
145  task_arr.pop_back();
146  }
147 
148  CASE_EXPECT_EQ(0, global_stack_pool->get_limit().used_stack_number);
149  CASE_EXPECT_EQ(0, global_stack_pool->get_limit().used_stack_size);
150 
151  CASE_EXPECT_EQ(task_arr_sz, global_stack_pool->get_limit().free_stack_number);
152  CASE_EXPECT_EQ(task_arr_sz * (global_stack_pool->get_stack_size() + global_stack_pool->get_stack_size_offset()),
153  global_stack_pool->get_limit().free_stack_size);
154 
155  global_stack_pool->gc();
156 
157  CASE_EXPECT_EQ(task_arr_sz - 10, global_stack_pool->get_limit().free_stack_number);
158  CASE_EXPECT_EQ((task_arr_sz - 10) * (global_stack_pool->get_stack_size() + global_stack_pool->get_stack_size_offset()),
159  global_stack_pool->get_limit().free_stack_size);
160 
161  global_stack_pool.reset();
162 }
163 
164 CASE_TEST(stack_pool_test, full_size) {
166  // full size
167 
168  std::vector<stack_pool_test_task_t::ptr_t> task_arr;
171 
172  global_stack_pool->set_max_stack_size(128 * 1024);
173  global_stack_pool->set_stack_size(100 * 1024);
174  // alloc
178  CASE_EXPECT_TRUE(!!tp1);
179 
181  CASE_EXPECT_TRUE(!tp2);
182 
183  global_stack_pool.reset();
184 }
cotask::task< stack_pool_test_macro_coroutine > stack_pool_test_task_t
#define CASE_EXPECT_EQ(l, r)
Definition: test_macros.h:62
copp::allocator::stack_allocator_pool< stack_pool_t > stack_allocator_t
#define CASE_EXPECT_TRUE(c)
Definition: test_macros.h:60
static ptr_t create()
Definition: stack_pool.h:52
static stack_pool_t::ptr_t global_stack_pool
static ptr_t create(const Ty &functor, size_t stack_size=0, size_t private_buffer_size=0)
create task with functor
Definition: task.h:155
copp::coroutine_context_container< stack_allocator_t > coroutine_t
copp::stack_pool< copp::allocator::stack_allocator_malloc > stack_pool_t
static int stack_pool_test_task_action(void *)
CASE_TEST(stack_pool_test, basic)
coroutine container contain stack context, stack allocator and runtime fcontext
memory allocator this allocator will maintain buffer using malloc/free function
std::shared_ptr< stack_pool< TAlloc > > ptr_t
Definition: stack_pool.h:25