~vanvugt/mir/gpu-load-balance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Copyright © 2012-2015 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Alan Griffiths <alan@octopull.co.uk>
 */

#include "server_example_log_options.h"
#include "server_example_input_event_filter.h"
#include "server_example_input_filter.h"
#include "server_example_display_configuration_policy.h"
#include "server_example_host_lifecycle_event_listener.h"
#include "server_example_window_management.h"
#include "server_example_custom_compositor.h"
#include "server_example_test_client.h"
#include "server_example_cursor_images.h"
#include "server_example_input_device_config.h"

#include "mir/abnormal_exit.h"
#include "mir/server.h"
#include "mir/main_loop.h"
#include "mir/fd.h"

#include "mir/report_exception.h"
#include "mir/options/option.h"

#include <boost/exception/diagnostic_information.hpp>

#include <chrono>
#include <cstdlib>

namespace me = mir::examples;

///\example server_example.cpp
/// A simple server illustrating several customisations

namespace
{
auto connection(int fd) -> std::string
{
    char connect_string[64] = {0};
    // We can't have both the server and the client owning the same fd, since
    // that will result in a double-close(). We give the client a duplicate which
    // the client can safely own (and should close when done).
    sprintf(connect_string, "fd://%d", dup(fd));
    return connect_string;
}

void add_launcher_option_to(mir::Server& server)
{
    static const char* const launch_child_opt = "launch-client";
    static const char* const launch_client_descr = "system() command to launch client";

    server.add_configuration_option(launch_child_opt, launch_client_descr, mir::OptionType::string);
    server.add_init_callback([&]
    {
        const auto options = server.get_options();
        if (options->is_set(launch_child_opt))
        {
            unsetenv("DISPLAY");                                // Discourage toolkits from using X11
            setenv("GDK_BACKEND", "mir", true);                 // configure GTK to use Mir
            setenv("QT_QPA_PLATFORM", "ubuntumirclient", true); // configure Qt to use Mir
            unsetenv("QT_QPA_PLATFORMTHEME");                   // Discourage Qt from unsupported theme
            setenv("SDL_VIDEODRIVER", "mir", true);             // configure SDL to use Mir

            auto const value = options->get<std::string>(launch_child_opt);

            for (auto i = begin(value); i != end(value); )
            {
                auto const j = find(i, end(value), '&');

                auto const cmd ="MIR_SOCKET=" + connection(server.open_client_socket()) + " " +
                    std::string{i, j} + "&";

                auto ignore = std::system(cmd.c_str());
                (void)(ignore);

                if ((i = j) != end(value)) ++i;
            }
        }
    });
}

void add_timeout_option_to(mir::Server& server)
{
    static const char* const timeout_opt = "timeout";
    static const char* const timeout_descr = "Seconds to run before exiting";

    server.add_configuration_option(timeout_opt, timeout_descr, mir::OptionType::integer);

    server.add_init_callback([&server]
    {
        const auto options = server.get_options();
        if (options->is_set(timeout_opt))
        {
            static auto const exit_action = server.the_main_loop()->create_alarm([&server] { server.stop(); });
            exit_action->reschedule_in(std::chrono::seconds(options->get<int>(timeout_opt)));
        }
    });
}

void exception_handler()
try
{
    throw;
}
catch (mir::AbnormalExit const& /*error*/)
{
}
catch (std::exception const& error)
{
    char const command_fmt[] = "/usr/share/apport/recoverable_problem --pid %d";
    char command[sizeof(command_fmt)+32];
    snprintf(command, sizeof(command), command_fmt, getpid());
    char const options[] = "we";
    char const key[] = "UnhandledException";
    auto const value = boost::diagnostic_information(error);

    if (auto const output = popen(command, options))
    {
        fwrite(key, sizeof(key), 1, output);            // the null terminator is used intentionally as a separator
        fwrite(value.c_str(), value.size(), 1, output);
        pclose(output);
    }

    mir::report_exception();
}
catch (...)
{
    mir::report_exception();
}
}

int main(int argc, char const* argv[])
try
{
    mir::Server server;

    // Use config options file in e.g. ~/.config/mir/mir_demo_server.config
    server.set_config_filename("mir/mir_demo_server.config");

    // Add example options for display layout, logging, launching clients and timeout
    me::add_display_configuration_options_to(server);
    me::add_log_host_lifecycle_option_to(server);
    me::add_glog_options_to(server);
    me::add_window_manager_option_to(server);
    me::add_custom_compositor_option_to(server);
    me::add_input_device_configuration_options_to(server);
    add_launcher_option_to(server);
    add_timeout_option_to(server);
    me::add_x_cursor_images(server);

    server.set_exception_handler(exception_handler);

    me::ClientContext context;
    me::add_test_client_option_to(server, context);

    // Create some input filters (we need to keep them or they deactivate)
    auto const quit_filter = me::make_quit_filter_for(server);
    auto const printing_filter = me::make_printing_input_filter_for(server);
    auto const screen_rotation_filter = me::make_screen_rotation_filter_for(server);

    // Provide the command line and run the server
    server.set_command_line(argc, argv);
    server.apply_settings();
    server.run();

    // Propagate any test failure
    if (context.test_failed)
    {
        return EXIT_FAILURE;
    }

    return server.exited_normally() ? EXIT_SUCCESS : EXIT_FAILURE;
}
catch (...)
{
    mir::report_exception();
    return EXIT_FAILURE;
}