~andreas-pokorny/mir/activate-pointer-acceleration

« back to all changes in this revision

Viewing changes to benchmarks/benchmark_multiplexing_dispatchable.cpp

  • Committer: Tarmac
  • Author(s): Christopher James Halse Rogers, Christopher James Halse Rogers
  • Date: 2015-02-18 04:25:26 UTC
  • mfrom: (2239.7.33 add-multiplexing-dispatchable)
  • Revision ID: tarmac-20150218042526-j1ei9ni2k4tua4e9
Add MultiplexingDispatchable.

This does exactly what it says; multiplexes multiple Dispatchables into a single Dispatchable. Each dispatch() of the MultiplexingDispatchable dispatches to a child Dispatchable with active events.

Approved by Alexandros Frantzis, PS Jenkins bot, Robert Carr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2015 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Christopher James Halse Rogers <christopher.halse.rogers@canonical.com>
 
17
 */
 
18
 
 
19
#include "mir/dispatch/multiplexing_dispatchable.h"
 
20
#include "mir/dispatch/simple_dispatch_thread.h"
 
21
 
 
22
#include <iostream>
 
23
#include <atomic>
 
24
#include <vector>
 
25
#include <memory>
 
26
#include <chrono>
 
27
#include <poll.h>
 
28
#include <unistd.h>
 
29
 
 
30
namespace md = mir::dispatch;
 
31
 
 
32
class TestDispatchable : public md::Dispatchable
 
33
{
 
34
public:
 
35
    TestDispatchable(uint64_t limit)
 
36
        : dispatch_limit{limit}
 
37
    {
 
38
        int pipefds[2];
 
39
        if (pipe(pipefds) < 0)
 
40
        {
 
41
            throw std::system_error{errno, std::system_category(), "Failed to create pipe"};
 
42
        }
 
43
 
 
44
        read_fd = mir::Fd{pipefds[0]};
 
45
        write_fd = mir::Fd{pipefds[1]};
 
46
 
 
47
        char dummy{0};
 
48
        if (::write(write_fd, &dummy, sizeof(dummy)) != sizeof(dummy))
 
49
        {
 
50
            throw std::system_error{errno, std::system_category(), "Failed to mark dispatchable"};
 
51
        }
 
52
    }
 
53
 
 
54
    mir::Fd watch_fd() const override
 
55
    {
 
56
        return read_fd;
 
57
    }
 
58
    bool dispatch(md::FdEvents) override
 
59
    {
 
60
        ++dispatch_count;
 
61
        return (dispatch_count < dispatch_limit);
 
62
    }
 
63
    md::FdEvents relevant_events() const override
 
64
    {
 
65
        return md::FdEvent::readable;
 
66
    }
 
67
 
 
68
private:
 
69
    static thread_local uint64_t dispatch_count;
 
70
    uint64_t const dispatch_limit;
 
71
    mir::Fd read_fd, write_fd;
 
72
};
 
73
 
 
74
thread_local uint64_t TestDispatchable::dispatch_count = 0;
 
75
 
 
76
bool fd_is_readable(int fd)
 
77
{
 
78
    struct pollfd poller {
 
79
        fd,
 
80
        POLLIN,
 
81
        0
 
82
    };
 
83
    return poll(&poller, 1, 0);
 
84
}
 
85
 
 
86
int main(int argc, char** argv)
 
87
{
 
88
    if (argc != 3)
 
89
    {
 
90
        std::cout<<"Usage: "<<argv[0]<<" <number of threads> <dispatch count>"<<std::endl;
 
91
        exit(1);
 
92
    }
 
93
 
 
94
    int const thread_count = std::atoi(argv[1]);
 
95
    uint64_t const dispatch_count = std::atoll(argv[2]);
 
96
 
 
97
    auto dispatcher = std::make_shared<md::MultiplexingDispatchable>();
 
98
    dispatcher->add_watch(std::make_shared<TestDispatchable>(dispatch_count / thread_count), md::DispatchReentrancy::reentrant);
 
99
 
 
100
    auto start = std::chrono::steady_clock::now();
 
101
 
 
102
    std::vector<std::thread> thread_loops;
 
103
    for (int i = 0; i < thread_count; ++i)
 
104
    {
 
105
        thread_loops.emplace_back([](md::Dispatchable& dispatch)
 
106
        {
 
107
            while(fd_is_readable(dispatch.watch_fd()))
 
108
            {
 
109
                dispatch.dispatch(md::FdEvent::readable);
 
110
            }
 
111
        }, std::ref(*dispatcher));
 
112
    }
 
113
 
 
114
    for (auto& thread : thread_loops)
 
115
    {
 
116
        thread.join();
 
117
    }
 
118
 
 
119
    auto duration = std::chrono::steady_clock::now() - start;
 
120
    std::cout<<"Dispatching "<<dispatch_count<<" times took "<<std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count()<<"ns"<<std::endl;
 
121
    exit(0);
 
122
}