File: rtkit-test.c

package info (click to toggle)
rtkit 0.13-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 864 kB
  • sloc: sh: 4,253; ansic: 2,381; makefile: 92; xml: 44
file content (129 lines) | stat: -rw-r--r-- 4,403 bytes parent folder | download
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
/*-*- Mode: C; c-basic-offset: 8 -*-*/

/***
        This file is part of RealtimeKit.

        Copyright 2009 Lennart Poettering

        RealtimeKit is free software: you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation, either version 3 of the License, or
        (at your option) any later version.

        RealtimeKit 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 RealtimeKit. If not, see <http://www.gnu.org/licenses/>.
***/

#include <errno.h>
#include <string.h>
#include <sched.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/resource.h>

#include "rtkit.h"

#ifndef SCHED_RESET_ON_FORK
#define SCHED_RESET_ON_FORK 0x40000000
#endif

#ifndef RLIMIT_RTTIME
#define RLIMIT_RTTIME 15
#endif

static void print_status(const char *t) {
        int ret;

        if ((ret = sched_getscheduler(0)) < 0) {
                fprintf(stderr, "sched_getscheduler() failed: %s\n", strerror(errno));
                return;
        }

        printf("%s:\n"
               "\tSCHED_RESET_ON_FORK: %s\n",
               t,
               (ret & SCHED_RESET_ON_FORK) ? "yes" : "no");

        if ((ret & ~SCHED_RESET_ON_FORK) == SCHED_RR) {
                struct sched_param param;

                if (sched_getparam(0, &param) < 0) {
                        fprintf(stderr, "sched_getschedparam() failed: %s\n", strerror(errno));
                        return;
                }

                printf("\tSCHED_RR with priority %i\n", param.sched_priority);

        } else if ((ret & ~SCHED_RESET_ON_FORK) == SCHED_OTHER) {
                errno = 0;
                ret = getpriority(PRIO_PROCESS, 0);
                if (errno != 0) {
                        fprintf(stderr, "getpriority() failed: %s\n", strerror(errno));
                        return;
                }

                printf("\tSCHED_OTHER with nice level: %i\n", ret);

        } else
                fprintf(stderr, "Neither SCHED_RR nor SCHED_OTHER.\n");
}

int main(int argc, char *argv[]) {
        DBusError error;
        DBusConnection *bus;
        int r, max_realtime_priority, min_nice_level;
        long long rttime_usec_max;
        struct rlimit rlim;

        dbus_error_init(&error);

        if (!(bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error))) {
                fprintf(stderr, "Failed to connect to system bus: %s\n", error.message);
                return 1;
        }

        if ((max_realtime_priority = rtkit_get_max_realtime_priority(bus)) < 0)
                fprintf(stderr, "Failed to retrieve max realtime priority: %s\n", strerror(-max_realtime_priority));
        else
                printf("Max realtime priority is: %d\n", max_realtime_priority);

        if ((r = rtkit_get_min_nice_level(bus, &min_nice_level)))
                fprintf(stderr, "Failed to retrieve min nice level: %s\n", strerror(-r));
        else
                printf("Min nice level is: %d\n", min_nice_level);

        if ((rttime_usec_max = rtkit_get_rttime_usec_max(bus)) < 0)
                fprintf(stderr, "Failed to retrieve rttime limit: %s\n", strerror(-rttime_usec_max));
        else
                printf("Rttime limit is: %lld ns\n", rttime_usec_max);

        memset(&rlim, 0, sizeof(rlim));
        rlim.rlim_cur = rlim.rlim_max = 100000ULL; /* 100ms */
        if ((setrlimit(RLIMIT_RTTIME, &rlim) < 0))
                fprintf(stderr, "Failed to set RLIMIT_RTTIME: %s\n", strerror(errno));

        print_status("before");

        if ((r = rtkit_make_high_priority(bus, 0, -10)) < 0)
                fprintf(stderr, "Failed to become high priority: %s\n", strerror(-r));
        else
                printf("Successfully became high priority.\n");

        print_status("after high priority");

        if ((r = rtkit_make_realtime(bus, 0, 10)) < 0)
                fprintf(stderr, "Failed to become realtime: %s\n", strerror(-r));
        else
                printf("Successfully became realtime.\n");

        print_status("after realtime");

        dbus_connection_unref(bus);

        return 0;
}