]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/fiber/performance/fiber/skynet.cpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / fiber / performance / fiber / skynet.cpp
1 #include <algorithm>
2 #include <cassert>
3 #include <chrono>
4 #include <cstddef>
5 #include <cstdint>
6 #include <cstdlib>
7 #include <iostream>
8 #include <memory>
9 #include <numeric>
10 #include <vector>
11
12 #include <boost/fiber/all.hpp>
13 #include <boost/pool/pool.hpp>
14 #include <boost/pool/pool_alloc.hpp>
15
16 #include "bind/bind_processor.hpp"
17
18 using clock_type = std::chrono::steady_clock;
19 using duration_type = clock_type::duration;
20 using time_point_type = clock_type::time_point;
21 using channel_type = boost::fibers::unbounded_channel< std::uint64_t >;
22 using allocator_type = boost::fibers::pooled_fixedsize_stack;
23
24 void prepare( allocator_type & salloc) {
25 auto sctx = salloc.allocate();
26 salloc.deallocate( sctx);
27 };
28
29 // microbenchmark
30 void skynet( allocator_type & salloc, channel_type & c, std::size_t num, std::size_t size, std::size_t div) {
31 if ( 1 == size) {
32 c.push( num);
33 } else {
34 channel_type rc;
35 for ( std::size_t i = 0; i < div; ++i) {
36 auto sub_num = num + i * size / div;
37 boost::fibers::fiber{ boost::fibers::launch::dispatch,
38 std::allocator_arg, salloc,
39 skynet,
40 std::ref( salloc), std::ref( rc), sub_num, size / div, div }.detach();
41 }
42 std::uint64_t sum{ 0 };
43 for ( std::size_t i = 0; i < div; ++i) {
44 sum += rc.value_pop();
45 }
46 c.push( sum);
47 }
48 }
49
50 int main() {
51 try {
52 bind_to_processor( 0);
53 std::size_t stack_size{ 4048 };
54 std::size_t size{ 100000 };
55 std::size_t div{ 10 };
56 allocator_type salloc{ stack_size, size };
57 prepare( salloc);
58 std::uint64_t result{ 0 };
59 duration_type duration{ duration_type::zero() };
60 channel_type rc;
61 time_point_type start{ clock_type::now() };
62 skynet( salloc, rc, 0, size, div);
63 result = rc.value_pop();
64 duration = clock_type::now() - start;
65 std::cout << "Result: " << result << " in " << duration.count() / 1000000 << " ms" << std::endl;
66 std::cout << "done." << std::endl;
67 return EXIT_SUCCESS;
68 } catch ( std::exception const& e) {
69 std::cerr << "exception: " << e.what() << std::endl;
70 } catch (...) {
71 std::cerr << "unhandled exception" << std::endl;
72 }
73 return EXIT_FAILURE;
74 }