bthread_execution_queue_unittest.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. #include <gtest/gtest.h>
  18. #include <bthread/execution_queue.h>
  19. #include <bthread/sys_futex.h>
  20. #include <bthread/countdown_event.h>
  21. #include "butil/time.h"
  22. #include "butil/fast_rand.h"
  23. #include "butil/gperftools_profiler.h"
  24. namespace {
  25. bool stopped = false;
  26. class ExecutionQueueTest : public testing::Test {
  27. protected:
  28. void SetUp() { stopped = false; }
  29. void TearDown() {}
  30. };
  31. struct LongIntTask {
  32. long value;
  33. bthread::CountdownEvent* event;
  34. LongIntTask(long v)
  35. : value(v), event(NULL)
  36. {}
  37. LongIntTask(long v, bthread::CountdownEvent* e)
  38. : value(v), event(e)
  39. {}
  40. LongIntTask() : value(0), event(NULL) {}
  41. };
  42. int add(void* meta, bthread::TaskIterator<LongIntTask> &iter) {
  43. stopped = iter.is_queue_stopped();
  44. int64_t* result = (int64_t*)meta;
  45. for (; iter; ++iter) {
  46. *result += iter->value;
  47. if (iter->event) { iter->event->signal(); }
  48. }
  49. return 0;
  50. }
  51. TEST_F(ExecutionQueueTest, single_thread) {
  52. int64_t result = 0;
  53. int64_t expected_result = 0;
  54. stopped = false;
  55. bthread::ExecutionQueueId<LongIntTask> queue_id;
  56. bthread::ExecutionQueueOptions options;
  57. ASSERT_EQ(0, bthread::execution_queue_start(&queue_id, &options,
  58. add, &result));
  59. for (int i = 0; i < 100; ++i) {
  60. expected_result += i;
  61. ASSERT_EQ(0, bthread::execution_queue_execute(queue_id, i));
  62. }
  63. LOG(INFO) << "stop";
  64. ASSERT_EQ(0, bthread::execution_queue_stop(queue_id));
  65. ASSERT_NE(0, bthread::execution_queue_execute(queue_id, 0));
  66. ASSERT_EQ(0, bthread::execution_queue_join(queue_id));
  67. ASSERT_EQ(expected_result, result);
  68. ASSERT_TRUE(stopped);
  69. }
  70. struct PushArg {
  71. bthread::ExecutionQueueId<LongIntTask> id;
  72. butil::atomic<int64_t> total_num;
  73. butil::atomic<int64_t> total_time;
  74. butil::atomic<int64_t> expected_value;
  75. volatile bool stopped;
  76. bool wait_task_completed;
  77. PushArg() {
  78. memset(this, 0, sizeof(*this));
  79. }
  80. };
  81. void* push_thread(void *arg) {
  82. PushArg* pa = (PushArg*)arg;
  83. int64_t sum = 0;
  84. butil::Timer timer;
  85. timer.start();
  86. int num = 0;
  87. bthread::CountdownEvent e;
  88. LongIntTask t(num, pa->wait_task_completed ? &e : NULL);
  89. if (pa->wait_task_completed) {
  90. e.reset(1);
  91. }
  92. while (bthread::execution_queue_execute(pa->id, t) == 0) {
  93. sum += num;
  94. t.value = ++num;
  95. if (pa->wait_task_completed) {
  96. e.wait();
  97. e.reset(1);
  98. }
  99. }
  100. timer.stop();
  101. pa->expected_value.fetch_add(sum, butil::memory_order_relaxed);
  102. pa->total_num.fetch_add(num);
  103. pa->total_time.fetch_add(timer.n_elapsed());
  104. return NULL;
  105. }
  106. void* push_thread_which_addresses_execq(void *arg) {
  107. PushArg* pa = (PushArg*)arg;
  108. int64_t sum = 0;
  109. butil::Timer timer;
  110. timer.start();
  111. int num = 0;
  112. bthread::ExecutionQueue<LongIntTask>::scoped_ptr_t ptr
  113. = bthread::execution_queue_address(pa->id);
  114. EXPECT_TRUE(ptr);
  115. while (ptr->execute(num) == 0) {
  116. sum += num;
  117. ++num;
  118. }
  119. EXPECT_TRUE(ptr->stopped());
  120. timer.stop();
  121. pa->expected_value.fetch_add(sum, butil::memory_order_relaxed);
  122. pa->total_num.fetch_add(num);
  123. pa->total_time.fetch_add(timer.n_elapsed());
  124. return NULL;
  125. }
  126. TEST_F(ExecutionQueueTest, performance) {
  127. pthread_t threads[8];
  128. bthread::ExecutionQueueId<LongIntTask> queue_id = { 0 }; // to suppress warnings
  129. bthread::ExecutionQueueOptions options;
  130. int64_t result = 0;
  131. ASSERT_EQ(0, bthread::execution_queue_start(&queue_id, &options,
  132. add, &result));
  133. PushArg pa;
  134. pa.id = queue_id;
  135. pa.total_num = 0;
  136. pa.total_time = 0;
  137. pa.expected_value = 0;
  138. pa.stopped = false;
  139. ProfilerStart("execq.prof");
  140. for (size_t i = 0; i < ARRAY_SIZE(threads); ++i) {
  141. pthread_create(&threads[i], NULL, &push_thread_which_addresses_execq, &pa);
  142. }
  143. usleep(500 * 1000);
  144. ASSERT_EQ(0, bthread::execution_queue_stop(queue_id));
  145. for (size_t i = 0; i < ARRAY_SIZE(threads); ++i) {
  146. pthread_join(threads[i], NULL);
  147. }
  148. ProfilerStop();
  149. ASSERT_EQ(0, bthread::execution_queue_join(queue_id));
  150. ASSERT_EQ(pa.expected_value.load(), result);
  151. LOG(INFO) << "With addressed execq, each execution_queue_execute takes "
  152. << pa.total_time.load() / pa.total_num.load()
  153. << " total_num=" << pa.total_num
  154. << " ns with " << ARRAY_SIZE(threads) << " threads";
  155. #define BENCHMARK_BOTH
  156. #ifdef BENCHMARK_BOTH
  157. result = 0;
  158. ASSERT_EQ(0, bthread::execution_queue_start(&queue_id, &options,
  159. add, &result));
  160. pa.id = queue_id;
  161. pa.total_num = 0;
  162. pa.total_time = 0;
  163. pa.expected_value = 0;
  164. pa.stopped = false;
  165. ProfilerStart("execq_id.prof");
  166. for (size_t i = 0; i < ARRAY_SIZE(threads); ++i) {
  167. pthread_create(&threads[i], NULL, &push_thread, &pa);
  168. }
  169. usleep(500 * 1000);
  170. ASSERT_EQ(0, bthread::execution_queue_stop(queue_id));
  171. for (size_t i = 0; i < ARRAY_SIZE(threads); ++i) {
  172. pthread_join(threads[i], NULL);
  173. }
  174. ProfilerStop();
  175. ASSERT_EQ(0, bthread::execution_queue_join(queue_id));
  176. ASSERT_EQ(pa.expected_value.load(), result);
  177. LOG(INFO) << "With id explicitly, execution_queue_execute takes "
  178. << pa.total_time.load() / pa.total_num.load()
  179. << " total_num=" << pa.total_num
  180. << " ns with " << ARRAY_SIZE(threads) << " threads";
  181. #endif // BENCHMARK_BOTH
  182. }
  183. volatile bool g_suspending = false;
  184. volatile bool g_should_be_urgent = false;
  185. int urgent_times = 0;
  186. int add_with_suspend(void* meta, bthread::TaskIterator<LongIntTask>& iter) {
  187. int64_t* result = (int64_t*)meta;
  188. if (iter.is_queue_stopped()) {
  189. stopped = true;
  190. return 0;
  191. }
  192. if (g_should_be_urgent) {
  193. g_should_be_urgent = false;
  194. EXPECT_EQ(-1, iter->value) << urgent_times;
  195. if (iter->event) { iter->event->signal(); }
  196. ++iter;
  197. EXPECT_FALSE(iter) << urgent_times;
  198. ++urgent_times;
  199. } else {
  200. for (; iter; ++iter) {
  201. if (iter->value == -100) {
  202. g_suspending = true;
  203. while (g_suspending) {
  204. bthread_usleep(100);
  205. }
  206. g_should_be_urgent = true;
  207. if (iter->event) { iter->event->signal(); }
  208. EXPECT_FALSE(++iter);
  209. return 0;
  210. } else {
  211. *result += iter->value;
  212. if (iter->event) { iter->event->signal(); }
  213. }
  214. }
  215. }
  216. return 0;
  217. }
  218. TEST_F(ExecutionQueueTest, execute_urgent) {
  219. g_should_be_urgent = false;
  220. pthread_t threads[10];
  221. bthread::ExecutionQueueId<LongIntTask> queue_id = { 0 }; // to suppress warnings
  222. bthread::ExecutionQueueOptions options;
  223. int64_t result = 0;
  224. ASSERT_EQ(0, bthread::execution_queue_start(&queue_id, &options,
  225. add_with_suspend, &result));
  226. PushArg pa;
  227. pa.id = queue_id;
  228. pa.total_num = 0;
  229. pa.total_time = 0;
  230. pa.expected_value = 0;
  231. pa.stopped = false;
  232. pa.wait_task_completed = true;
  233. for (size_t i = 0; i < ARRAY_SIZE(threads); ++i) {
  234. pthread_create(&threads[i], NULL, &push_thread, &pa);
  235. }
  236. g_suspending = false;
  237. usleep(1000);
  238. for (int i = 0; i < 100; ++i) {
  239. ASSERT_EQ(0, bthread::execution_queue_execute(queue_id, -100));
  240. while (!g_suspending) {
  241. usleep(100);
  242. }
  243. ASSERT_EQ(0, bthread::execution_queue_execute(
  244. queue_id, -1, &bthread::TASK_OPTIONS_URGENT));
  245. g_suspending = false;
  246. usleep(100);
  247. }
  248. usleep(500* 1000);
  249. pa.stopped = true;
  250. ASSERT_EQ(0, bthread::execution_queue_stop(queue_id));
  251. for (size_t i = 0; i < ARRAY_SIZE(threads); ++i) {
  252. pthread_join(threads[i], NULL);
  253. }
  254. LOG(INFO) << "result=" << result;
  255. ASSERT_EQ(0, bthread::execution_queue_join(queue_id));
  256. ASSERT_EQ(pa.expected_value.load(), result);
  257. }
  258. TEST_F(ExecutionQueueTest, urgent_task_is_the_last_task) {
  259. g_should_be_urgent = false;
  260. g_suspending = false;
  261. bthread::ExecutionQueueId<LongIntTask> queue_id = { 0 }; // to suppress warnings
  262. bthread::ExecutionQueueOptions options;
  263. int64_t result = 0;
  264. ASSERT_EQ(0, bthread::execution_queue_start(&queue_id, &options,
  265. add_with_suspend, &result));
  266. g_suspending = false;
  267. ASSERT_EQ(0, bthread::execution_queue_execute(queue_id, -100));
  268. while (!g_suspending) {
  269. usleep(10);
  270. }
  271. LOG(INFO) << "Going to push";
  272. int64_t expected = 0;
  273. for (int i = 1; i < 100; ++i) {
  274. expected += i;
  275. ASSERT_EQ(0, bthread::execution_queue_execute(queue_id, i));
  276. }
  277. ASSERT_EQ(0, bthread::execution_queue_execute(queue_id, -1, &bthread::TASK_OPTIONS_URGENT));
  278. usleep(100);
  279. g_suspending = false;
  280. butil::atomic_thread_fence(butil::memory_order_acq_rel);
  281. usleep(10 * 1000);
  282. LOG(INFO) << "going to quit";
  283. ASSERT_EQ(0, bthread::execution_queue_stop(queue_id));
  284. ASSERT_EQ(0, bthread::execution_queue_join(queue_id));
  285. ASSERT_EQ(expected, result);
  286. }
  287. long next_task[1024];
  288. butil::atomic<int> num_threads(0);
  289. void* push_thread_with_id(void* arg) {
  290. bthread::ExecutionQueueId<LongIntTask> id = { (uint64_t)arg };
  291. int thread_id = num_threads.fetch_add(1, butil::memory_order_relaxed);
  292. LOG(INFO) << "Start thread" << thread_id;
  293. for (int i = 0; i < 100000; ++i) {
  294. bthread::execution_queue_execute(id, ((long)thread_id << 32) | i);
  295. }
  296. return NULL;
  297. }
  298. int check_order(void* meta, bthread::TaskIterator<LongIntTask>& iter) {
  299. for (; iter; ++iter) {
  300. long value = iter->value;
  301. int thread_id = value >> 32;
  302. long task = value & 0xFFFFFFFFul;
  303. if (task != next_task[thread_id]++) {
  304. EXPECT_TRUE(false) << "task=" << task << " thread_id=" << thread_id;
  305. ++*(long*)meta;
  306. }
  307. if (iter->event) { iter->event->signal(); }
  308. }
  309. return 0;
  310. }
  311. TEST_F(ExecutionQueueTest, multi_threaded_order) {
  312. memset(next_task, 0, sizeof(next_task));
  313. long disorder_times = 0;
  314. bthread::ExecutionQueueId<LongIntTask> queue_id = { 0 }; // to suppress warnings
  315. bthread::ExecutionQueueOptions options;
  316. ASSERT_EQ(0, bthread::execution_queue_start(&queue_id, &options,
  317. check_order, &disorder_times));
  318. pthread_t threads[12];
  319. for (size_t i = 0; i < ARRAY_SIZE(threads); ++i) {
  320. pthread_create(&threads[i], NULL, &push_thread_with_id, (void *)queue_id.value);
  321. }
  322. for (size_t i = 0; i < ARRAY_SIZE(threads); ++i) {
  323. pthread_join(threads[i], NULL);
  324. }
  325. ASSERT_EQ(0, bthread::execution_queue_stop(queue_id));
  326. ASSERT_EQ(0, bthread::execution_queue_join(queue_id));
  327. ASSERT_EQ(0, disorder_times);
  328. }
  329. int check_running_thread(void* arg, bthread::TaskIterator<LongIntTask>& iter) {
  330. if (iter.is_queue_stopped()) {
  331. return 0;
  332. }
  333. for (; iter; ++iter) {}
  334. EXPECT_EQ(pthread_self(), (pthread_t)arg);
  335. return 0;
  336. }
  337. TEST_F(ExecutionQueueTest, in_place_task) {
  338. pthread_t thread_id = pthread_self();
  339. bthread::ExecutionQueueId<LongIntTask> queue_id = { 0 }; // to suppress warnings
  340. bthread::ExecutionQueueOptions options;
  341. ASSERT_EQ(0, bthread::execution_queue_start(&queue_id, &options,
  342. check_running_thread,
  343. (void*)thread_id));
  344. ASSERT_EQ(0, bthread::execution_queue_execute(
  345. queue_id, 0, &bthread::TASK_OPTIONS_INPLACE));
  346. ASSERT_EQ(0, bthread::execution_queue_stop(queue_id));
  347. ASSERT_EQ(0, bthread::execution_queue_join(queue_id));
  348. }
  349. struct InPlaceTask {
  350. bool first_task;
  351. pthread_t thread_id;
  352. };
  353. void *run_first_tasks(void* arg) {
  354. bthread::ExecutionQueueId<InPlaceTask> queue_id = { (uint64_t)arg };
  355. InPlaceTask task;
  356. task.first_task = true;
  357. task.thread_id = pthread_self();
  358. EXPECT_EQ(0, bthread::execution_queue_execute(queue_id, task,
  359. &bthread::TASK_OPTIONS_INPLACE));
  360. return NULL;
  361. }
  362. int stuck_and_check_running_thread(void* arg, bthread::TaskIterator<InPlaceTask>& iter) {
  363. if (iter.is_queue_stopped()) {
  364. return 0;
  365. }
  366. butil::atomic<int>* futex = (butil::atomic<int>*)arg;
  367. if (iter->first_task) {
  368. EXPECT_EQ(pthread_self(), iter->thread_id);
  369. futex->store(1);
  370. bthread::futex_wake_private(futex, 1);
  371. while (futex->load() != 2) {
  372. bthread::futex_wait_private(futex, 1, NULL);
  373. }
  374. ++iter;
  375. EXPECT_FALSE(iter);
  376. } else {
  377. for (; iter; ++iter) {
  378. EXPECT_FALSE(iter->first_task);
  379. EXPECT_NE(pthread_self(), iter->thread_id);
  380. }
  381. }
  382. return 0;
  383. }
  384. TEST_F(ExecutionQueueTest, should_start_new_thread_on_more_tasks) {
  385. bthread::ExecutionQueueId<InPlaceTask> queue_id = { 0 };
  386. bthread::ExecutionQueueOptions options;
  387. butil::atomic<int> futex(0);
  388. ASSERT_EQ(0, bthread::execution_queue_start(&queue_id, &options,
  389. stuck_and_check_running_thread,
  390. (void*)&futex));
  391. pthread_t thread;
  392. ASSERT_EQ(0, pthread_create(&thread, NULL, run_first_tasks, (void*)queue_id.value));
  393. while (futex.load() != 1) {
  394. bthread::futex_wait_private(&futex, 0, NULL);
  395. }
  396. for (size_t i = 0; i < 100; ++i) {
  397. InPlaceTask task;
  398. task.first_task = false;
  399. task.thread_id = pthread_self();
  400. ASSERT_EQ(0, bthread::execution_queue_execute(queue_id, task,
  401. &bthread::TASK_OPTIONS_INPLACE));
  402. }
  403. futex.store(2);
  404. bthread::futex_wake_private(&futex, 1);
  405. ASSERT_EQ(0, bthread::execution_queue_stop(queue_id));
  406. ASSERT_EQ(0, bthread::execution_queue_join(queue_id));
  407. }
  408. void* inplace_push_thread(void* arg) {
  409. bthread::ExecutionQueueId<LongIntTask> id = { (uint64_t)arg };
  410. int thread_id = num_threads.fetch_add(1, butil::memory_order_relaxed);
  411. LOG(INFO) << "Start thread" << thread_id;
  412. for (int i = 0; i < 100000; ++i) {
  413. bthread::execution_queue_execute(id, ((long)thread_id << 32) | i,
  414. &bthread::TASK_OPTIONS_INPLACE);
  415. }
  416. return NULL;
  417. }
  418. TEST_F(ExecutionQueueTest, inplace_and_order) {
  419. memset(next_task, 0, sizeof(next_task));
  420. long disorder_times = 0;
  421. bthread::ExecutionQueueId<LongIntTask> queue_id = { 0 }; // to suppress warnings
  422. bthread::ExecutionQueueOptions options;
  423. ASSERT_EQ(0, bthread::execution_queue_start(&queue_id, &options,
  424. check_order, &disorder_times));
  425. pthread_t threads[12];
  426. for (size_t i = 0; i < ARRAY_SIZE(threads); ++i) {
  427. pthread_create(&threads[i], NULL, &inplace_push_thread, (void *)queue_id.value);
  428. }
  429. for (size_t i = 0; i < ARRAY_SIZE(threads); ++i) {
  430. pthread_join(threads[i], NULL);
  431. }
  432. ASSERT_EQ(0, bthread::execution_queue_stop(queue_id));
  433. ASSERT_EQ(0, bthread::execution_queue_join(queue_id));
  434. ASSERT_EQ(0, disorder_times);
  435. }
  436. TEST_F(ExecutionQueueTest, size_of_task_node) {
  437. LOG(INFO) << "sizeof(TaskNode)=" << sizeof(bthread::TaskNode);
  438. }
  439. int add_with_suspend2(void* meta, bthread::TaskIterator<LongIntTask>& iter) {
  440. int64_t* result = (int64_t*)meta;
  441. if (iter.is_queue_stopped()) {
  442. stopped = true;
  443. return 0;
  444. }
  445. for (; iter; ++iter) {
  446. if (iter->value == -100) {
  447. g_suspending = true;
  448. while (g_suspending) {
  449. usleep(10);
  450. }
  451. if (iter->event) { iter->event->signal(); }
  452. } else {
  453. *result += iter->value;
  454. if (iter->event) { iter->event->signal(); }
  455. }
  456. }
  457. return 0;
  458. }
  459. TEST_F(ExecutionQueueTest, cancel) {
  460. bthread::ExecutionQueueId<LongIntTask> queue_id = { 0 }; // to suppress warnings
  461. bthread::ExecutionQueueOptions options;
  462. int64_t result = 0;
  463. ASSERT_EQ(0, bthread::execution_queue_start(&queue_id, &options,
  464. add_with_suspend2, &result));
  465. g_suspending = false;
  466. bthread::TaskHandle handle0;
  467. ASSERT_EQ(0, bthread::execution_queue_execute(queue_id, -100, NULL, &handle0));
  468. while (!g_suspending) {
  469. usleep(10);
  470. }
  471. ASSERT_EQ(1, bthread::execution_queue_cancel(handle0));
  472. ASSERT_EQ(1, bthread::execution_queue_cancel(handle0));
  473. bthread::TaskHandle handle1;
  474. ASSERT_EQ(0, bthread::execution_queue_execute(queue_id, 100, NULL, &handle1));
  475. ASSERT_EQ(0, bthread::execution_queue_cancel(handle1));
  476. g_suspending = false;
  477. ASSERT_EQ(-1, bthread::execution_queue_cancel(handle1));
  478. ASSERT_EQ(0, bthread::execution_queue_stop(queue_id));
  479. ASSERT_EQ(0, bthread::execution_queue_join(queue_id));
  480. ASSERT_EQ(0, result);
  481. }
  482. struct CancelSelf {
  483. butil::atomic<bthread::TaskHandle*> handle;
  484. };
  485. int cancel_self(void* /*meta*/, bthread::TaskIterator<CancelSelf*>& iter) {
  486. for (; iter; ++iter) {
  487. while ((*iter)->handle == NULL) {
  488. usleep(10);
  489. }
  490. EXPECT_EQ(1, bthread::execution_queue_cancel(*(*iter)->handle.load()));
  491. EXPECT_EQ(1, bthread::execution_queue_cancel(*(*iter)->handle.load()));
  492. EXPECT_EQ(1, bthread::execution_queue_cancel(*(*iter)->handle.load()));
  493. }
  494. return 0;
  495. }
  496. TEST_F(ExecutionQueueTest, cancel_self) {
  497. bthread::ExecutionQueueId<CancelSelf*> queue_id = { 0 }; // to suppress warnings
  498. bthread::ExecutionQueueOptions options;
  499. ASSERT_EQ(0, bthread::execution_queue_start(&queue_id, &options,
  500. cancel_self, NULL));
  501. CancelSelf task;
  502. task.handle = NULL;
  503. bthread::TaskHandle handle;
  504. ASSERT_EQ(0, bthread::execution_queue_execute(queue_id, &task, NULL, &handle));
  505. task.handle.store(&handle);
  506. ASSERT_EQ(0, bthread::execution_queue_stop(queue_id));
  507. ASSERT_EQ(0, bthread::execution_queue_join(queue_id));
  508. }
  509. struct AddTask {
  510. int value;
  511. bool cancel_task;
  512. int cancel_value;
  513. bthread::TaskHandle handle;
  514. };
  515. struct AddMeta {
  516. int64_t sum;
  517. butil::atomic<int64_t> expected;
  518. butil::atomic<int64_t> succ_times;
  519. butil::atomic<int64_t> race_times;
  520. butil::atomic<int64_t> fail_times;
  521. };
  522. int add_with_cancel(void* meta, bthread::TaskIterator<AddTask>& iter) {
  523. if (iter.is_queue_stopped()) {
  524. return 0;
  525. }
  526. AddMeta* m = (AddMeta*)meta;
  527. for (; iter; ++iter) {
  528. if (iter->cancel_task) {
  529. const int rc = bthread::execution_queue_cancel(iter->handle);
  530. if (rc == 0) {
  531. m->expected.fetch_sub(iter->cancel_value);
  532. m->succ_times.fetch_add(1);
  533. } else if (rc < 0) {
  534. m->fail_times.fetch_add(1);
  535. } else {
  536. m->race_times.fetch_add(1);
  537. }
  538. } else {
  539. m->sum += iter->value;
  540. }
  541. }
  542. return 0;
  543. }
  544. TEST_F(ExecutionQueueTest, random_cancel) {
  545. bthread::ExecutionQueueId<AddTask> queue_id = { 0 };
  546. AddMeta m;
  547. m.sum = 0;
  548. m.expected.store(0);
  549. m.succ_times.store(0);
  550. m.fail_times.store(0);
  551. m.race_times.store(0);
  552. ASSERT_EQ(0, bthread::execution_queue_start(&queue_id, NULL,
  553. add_with_cancel, &m));
  554. int64_t expected = 0;
  555. for (int i = 0; i < 100000; ++i) {
  556. bthread::TaskHandle h;
  557. AddTask t;
  558. t.value = i;
  559. t.cancel_task = false;
  560. ASSERT_EQ(0, bthread::execution_queue_execute(queue_id, t, NULL, &h));
  561. const int r = butil::fast_rand_less_than(4);
  562. expected += i;
  563. if (r == 0) {
  564. if (bthread::execution_queue_cancel(h) == 0) {
  565. expected -= i;
  566. }
  567. } else if (r == 1) {
  568. t.cancel_task = true;
  569. t.cancel_value = i;
  570. t.handle = h;
  571. ASSERT_EQ(0, bthread::execution_queue_execute(queue_id, t, NULL));
  572. } else if (r == 2) {
  573. t.cancel_task = true;
  574. t.cancel_value = i;
  575. t.handle = h;
  576. ASSERT_EQ(0, bthread::execution_queue_execute(queue_id, t,
  577. &bthread::TASK_OPTIONS_URGENT));
  578. } else {
  579. // do nothing;
  580. }
  581. }
  582. m.expected.fetch_add(expected);
  583. ASSERT_EQ(0, bthread::execution_queue_stop(queue_id));
  584. ASSERT_EQ(0, bthread::execution_queue_join(queue_id));
  585. ASSERT_EQ(m.sum, m.expected.load());
  586. LOG(INFO) << "sum=" << m.sum << " race_times=" << m.race_times
  587. << " succ_times=" << m.succ_times
  588. << " fail_times=" << m.fail_times;
  589. }
  590. int add2(void* meta, bthread::TaskIterator<LongIntTask> &iter) {
  591. if (iter) {
  592. int64_t* result = (int64_t*)meta;
  593. *result += iter->value;
  594. if (iter->event) { iter->event->signal(); }
  595. }
  596. return 0;
  597. }
  598. TEST_F(ExecutionQueueTest, not_do_iterate_at_all) {
  599. int64_t result = 0;
  600. int64_t expected_result = 0;
  601. bthread::ExecutionQueueId<LongIntTask> queue_id;
  602. bthread::ExecutionQueueOptions options;
  603. ASSERT_EQ(0, bthread::execution_queue_start(&queue_id, &options,
  604. add2, &result));
  605. for (int i = 0; i < 100; ++i) {
  606. expected_result += i;
  607. ASSERT_EQ(0, bthread::execution_queue_execute(queue_id, i));
  608. }
  609. ASSERT_EQ(0, bthread::execution_queue_stop(queue_id));
  610. ASSERT_NE(0, bthread::execution_queue_execute(queue_id, 0));
  611. ASSERT_EQ(0, bthread::execution_queue_join(queue_id));
  612. ASSERT_EQ(expected_result, result);
  613. }
  614. int add_with_suspend3(void* meta, bthread::TaskIterator<LongIntTask>& iter) {
  615. int64_t* result = (int64_t*)meta;
  616. if (iter.is_queue_stopped()) {
  617. stopped = true;
  618. return 0;
  619. }
  620. for (; iter; ++iter) {
  621. if (iter->value == -100) {
  622. g_suspending = true;
  623. while (g_suspending) {
  624. usleep(10);
  625. }
  626. if (iter->event) { iter->event->signal(); }
  627. } else {
  628. *result += iter->value;
  629. if (iter->event) { iter->event->signal(); }
  630. }
  631. }
  632. return 0;
  633. }
  634. TEST_F(ExecutionQueueTest, cancel_unexecuted_high_priority_task) {
  635. g_should_be_urgent = false;
  636. bthread::ExecutionQueueId<LongIntTask> queue_id = { 0 }; // to suppress warnings
  637. bthread::ExecutionQueueOptions options;
  638. int64_t result = 0;
  639. ASSERT_EQ(0, bthread::execution_queue_start(&queue_id, &options,
  640. add_with_suspend3, &result));
  641. // Push a normal task to make the executor suspend
  642. ASSERT_EQ(0, bthread::execution_queue_execute(queue_id, -100));
  643. while (!g_suspending) {
  644. usleep(10);
  645. }
  646. // At this point, executor is suspended by the first task. Then we put
  647. // a high_priority task which is going to be cancelled immediately,
  648. // expecting that both operations are successful.
  649. bthread::TaskHandle h;
  650. ASSERT_EQ(0, bthread::execution_queue_execute(
  651. queue_id, -100, &bthread::TASK_OPTIONS_URGENT, &h));
  652. ASSERT_EQ(0, bthread::execution_queue_cancel(h));
  653. // Resume executor
  654. g_suspending = false;
  655. // Push a normal task
  656. ASSERT_EQ(0, bthread::execution_queue_execute(queue_id, 12345));
  657. // The execq should stop normally
  658. ASSERT_EQ(0, bthread::execution_queue_stop(queue_id));
  659. ASSERT_EQ(0, bthread::execution_queue_join(queue_id));
  660. ASSERT_EQ(12345, result);
  661. }
  662. } // namespace