summaryrefslogtreecommitdiff
path: root/tests/test-performance/dbus-spammer/dbus-spammer.cpp
blob: 2d84a605da9df6699a08ed36aec8976110f49288 (plain)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <stdio.h>
#include <signal.h>
#include "dbus-services.h"

sig_atomic_t volatile g_exit = 0;

Maintainer::Maintainer(Threads* t) : QObject()
{
    allOnline = false;
    threads = t;
    timerID = startTimer(0);
}

Maintainer::~Maintainer()
{
    killTimer(timerID);
}

void Maintainer::timerEvent(QTimerEvent*)
{
    if( !allOnline )
    {
        int count = 0;
        Threads::iterator th;
        for( th = threads->begin(); th != threads->end(); ++th )
        {
            DbusSpammer* t = dynamic_cast<DbusSpammer*>(*th);
            if( t )
            {
                if( t->firstTime )
                    count++;
            }
        }

        allOnline = (count == 0) && (threads->count() > 0);

        if( allOnline )
            printf("All thread online and working!\n");
    }

    fflush(stdout);
    fflush(stderr);

    if( g_exit )
        QMetaObject::invokeMethod(QCoreApplication::instance(), "quit");
}

class CommandLineParser
{
public:
    CommandLineParser(int argc, char** argv)
    {
        threadCount = 10;
        dataSize = 512;

        parseArguments(argc, argv);
    }

public:
    int threadCount;
    int dataSize;

private:
    void parseArguments(int argc, char** argv)
    {
        int option, tmp;

        while( (option = getopt(argc, argv, "ht:s:")) != -1 )
        {
            switch( option )
            {
            case 'h':
                usage(0);
                return;
            case 't':
                tmp = atoi(optarg);
                if( tmp > 0 )
                {
                    threadCount = tmp;
                    printf("Thread count: %d\n", threadCount);
                }
                else
                {
                    printf("Invalid thread count, using default value %d!\n", threadCount);
                }
                break;
            case 's':
                tmp = atoi(optarg);
                if( tmp > 0 )
                {
                    dataSize = tmp;
                    printf("Data size: %d\n", dataSize);
                }
                else
                {
                    printf("Invalid data size, using default value %d!\n", dataSize);
                }
                break;
            default:
                usage(-1);
                return;
            }
        }
    }

    void usage(int theExitCode)
    {
        printf("usage: dbus-spammer [-h] [-t thread_count] [-s data_size]\n");
        printf("default values:\n");
        printf("  thread_count: %d\n", threadCount);
        printf("  data_size   : %d\n", dataSize);
        exit(theExitCode);
    }
};

void signal_handler(int signo, siginfo_t *info, void *data)
{
    switch( signo )
    {
    case SIGHUP:
    case SIGTERM:
    case SIGINT:
    default:
        puts("Caught exit signal!");
        g_exit = 1;
    }

    (void)info;
    (void)data;
}

bool installSignals()
{
    struct sigaction sa;

    memset(&sa, 0, sizeof(struct sigaction));
    sa.sa_sigaction = signal_handler;
    sa.sa_flags = SA_SIGINFO;

    if( sigaction(SIGHUP, &sa, NULL) < 0 || sigaction(SIGTERM, &sa, NULL) < 0 || sigaction(SIGINT, &sa, NULL) < 0 )
    {
        puts("Failed to install signal handlers");
        return false;
    }

    return true;
}

int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);
    CommandLineParser cmd(argc, argv);

    int count = cmd.threadCount;

    Threads threads;
    Servers servers;

    if( !installSignals() )
    {
        return -2;
    }

    if( !QDBusConnection::sessionBus().isConnected() )
    {
        puts("Cannot connect to the D-Bus session bus.");
        return -1;
    }

    Maintainer maintainer(&threads);

    printf("Starting pair: ");
    for( int i = 0; i < count; i++ )
    {
        printf("#%d", i);
        fflush(stdout);
        DbusServer* s = new DbusServer(i);
        s->start(QThread::NormalPriority);
        servers.push_back(s);
        usleep(100000);

        QThread* t = new DbusSpammer(i, s->getServiceName(), cmd.dataSize);
        threads.push_back(t);
        t->start(QThread::NormalPriority);
    }

    puts("");

    app.exec();

    Threads::iterator th;
    printf("Cleaning thread: ");
    for( th = threads.begin(); th != threads.end(); ++th )
    {
        DbusSpammer* t = dynamic_cast<DbusSpammer*>(*th);
        if( t )
        {
            printf("#%d", t->threadID);

            t->quit();
            int cnt = 0;
            while( t->isRunning() )
            {
                fflush(stdout);
                fflush(stderr);
                if( ++cnt == 1000 )
                {
                    printf("[T!]");
                    t->terminate();
                    break;
                }
                usleep(1000);
            }
            delete t;
        }
    }
    puts(" ... done!");

    Servers::iterator sv;
    printf("Cleaning service: ");
    for( sv = servers.begin(); sv != servers.end(); ++sv )
    {
        DbusServer* s = dynamic_cast<DbusServer*>(*sv);
        if( s )
        {
            printf("#%d", s->getServiceID());
            s->quit();
            while( !s->isFinished() )
                usleep(1000);

            delete s;
        }
    }

    puts(" ... done!");

    return 0;
}