~ubuntu-branches/ubuntu/wily/mir/wily-proposed

« back to all changes in this revision

Viewing changes to benchmarks/benchmark_multiplexing_dispatchable.cpp

  • Committer: Package Import Robot
  • Author(s): CI Train Bot
  • Date: 2015-05-12 13:12:55 UTC
  • mto: This revision was merged to the branch mainline in revision 96.
  • Revision ID: package-import@ubuntu.com-20150512131255-y7z12i8n4pbvo70x
Tags: upstream-0.13.0+15.10.20150512
Import upstream version 0.13.0+15.10.20150512

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 <vector>
 
24
#include <memory>
 
25
#include <chrono>
 
26
#include <poll.h>
 
27
#include <unistd.h>
 
28
 
 
29
namespace md = mir::dispatch;
 
30
 
 
31
class TestDispatchable : public md::Dispatchable
 
32
{
 
33
public:
 
34
    TestDispatchable(uint64_t limit)
 
35
        : dispatch_limit{limit}
 
36
    {
 
37
        int pipefds[2];
 
38
        if (pipe(pipefds) < 0)
 
39
        {
 
40
            throw std::system_error{errno, std::system_category(), "Failed to create pipe"};
 
41
        }
 
42
 
 
43
        read_fd = mir::Fd{pipefds[0]};
 
44
        write_fd = mir::Fd{pipefds[1]};
 
45
 
 
46
        char dummy{0};
 
47
        if (::write(write_fd, &dummy, sizeof(dummy)) != sizeof(dummy))
 
48
        {
 
49
            throw std::system_error{errno, std::system_category(), "Failed to mark dispatchable"};
 
50
        }
 
51
    }
 
52
 
 
53
    mir::Fd watch_fd() const override
 
54
    {
 
55
        return read_fd;
 
56
    }
 
57
    bool dispatch(md::FdEvents) override
 
58
    {
 
59
        ++dispatch_count;
 
60
        return (dispatch_count < dispatch_limit);
 
61
    }
 
62
    md::FdEvents relevant_events() const override
 
63
    {
 
64
        return md::FdEvent::readable;
 
65
    }
 
66
 
 
67
private:
 
68
    static thread_local uint64_t dispatch_count;
 
69
    uint64_t const dispatch_limit;
 
70
    mir::Fd read_fd, write_fd;
 
71
};
 
72
 
 
73
thread_local uint64_t TestDispatchable::dispatch_count = 0;
 
74
 
 
75
bool fd_is_readable(int fd)
 
76
{
 
77
    struct pollfd poller {
 
78
        fd,
 
79
        POLLIN,
 
80
        0
 
81
    };
 
82
    return poll(&poller, 1, 0);
 
83
}
 
84
 
 
85
int main(int argc, char** argv)
 
86
{
 
87
    if (argc != 3)
 
88
    {
 
89
        std::cout<<"Usage: "<<argv[0]<<" <number of threads> <dispatch count>"<<std::endl;
 
90
        exit(1);
 
91
    }
 
92
 
 
93
    int const thread_count = std::atoi(argv[1]);
 
94
    uint64_t const dispatch_count = std::atoll(argv[2]);
 
95
 
 
96
    auto dispatcher = std::make_shared<md::MultiplexingDispatchable>();
 
97
    dispatcher->add_watch(std::make_shared<TestDispatchable>(dispatch_count / thread_count), md::DispatchReentrancy::reentrant);
 
98
 
 
99
    auto start = std::chrono::steady_clock::now();
 
100
 
 
101
    std::vector<std::thread> thread_loops;
 
102
    for (int i = 0; i < thread_count; ++i)
 
103
    {
 
104
        thread_loops.emplace_back([](md::Dispatchable& dispatch)
 
105
        {
 
106
            while(fd_is_readable(dispatch.watch_fd()))
 
107
            {
 
108
                dispatch.dispatch(md::FdEvent::readable);
 
109
            }
 
110
        }, std::ref(*dispatcher));
 
111
    }
 
112
 
 
113
    for (auto& thread : thread_loops)
 
114
    {
 
115
        thread.join();
 
116
    }
 
117
 
 
118
    auto duration = std::chrono::steady_clock::now() - start;
 
119
    std::cout<<"Dispatching "<<dispatch_count<<" times took "<<std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count()<<"ns"<<std::endl;
 
120
    exit(0);
 
121
}