summaryrefslogtreecommitdiff
path: root/src/daemon/cpulimit.c
blob: 579b91e3f17559feff21d6b17566738e706c829d (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
239
240
241
242
243
244
245
246
247
/* $Id$ */

/***
  This file is part of PulseAudio.

  Copyright 2004-2006 Lennart Poettering

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

  PulseAudio 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 Lesser General Public
  License along with PulseAudio; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <pulse/error.h>

#include <pulsecore/core-util.h>
#include <pulsecore/core-error.h>
#include <pulsecore/log.h>
#include <pulsecore/macro.h>

#include "cpulimit.h"

#ifdef HAVE_SIGXCPU

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <signal.h>

#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif

/* This module implements a watchdog that makes sure that the current
 * process doesn't consume more than 70% CPU time for 10 seconds. This
 * is very useful when using SCHED_FIFO scheduling which effectively
 * disables multitasking. */

/* Method of operation: Using SIGXCPU a signal handler is called every
 * 10s process CPU time. That function checks if less than 14s system
 * time have passed. In that case, it tries to contact the main event
 * loop through a pipe. After two additional seconds it is checked
 * whether the main event loop contact was successful. If not, the
 * program is terminated forcibly. */

/* Utilize this much CPU time at maximum */
#define CPUTIME_PERCENT 70

/* Check every 10s */
#define CPUTIME_INTERVAL_SOFT (10)

/* Recheck after 5s */
#define CPUTIME_INTERVAL_HARD (5)

/* Time of the last CPU load check */
static time_t last_time = 0;

/* Pipe for communicating with the main loop */
static int the_pipe[2] = {-1, -1};

/* Main event loop and IO event for the FIFO */
static pa_mainloop_api *api = NULL;
static pa_io_event *io_event = NULL;

/* Saved sigaction struct for SIGXCPU */
static struct sigaction sigaction_prev;

/* Nonzero after pa_cpu_limit_init() */
static pa_bool_t installed = FALSE;

/* The current state of operation */
static enum  {
    PHASE_IDLE,   /* Normal state */
    PHASE_SOFT    /* After CPU overload has been detected */
} phase = PHASE_IDLE;

/* Reset the SIGXCPU timer to the next t seconds */
static void reset_cpu_time(int t) {
    long n;
    struct rlimit rl;
    struct rusage ru;

    /* Get the current CPU time of the current process */
    pa_assert_se(getrusage(RUSAGE_SELF, &ru) >= 0);

    n = ru.ru_utime.tv_sec + ru.ru_stime.tv_sec + t;
    pa_assert_se(getrlimit(RLIMIT_CPU, &rl) >= 0);

    rl.rlim_cur = n;
    pa_assert_se(setrlimit(RLIMIT_CPU, &rl) >= 0);
}

/* A simple, thread-safe puts() work-alike */
static void write_err(const char *p) {
    pa_loop_write(2, p, strlen(p), NULL);
}

/* The signal handler, called on every SIGXCPU */
static void signal_handler(int sig) {
    int saved_errno;

    saved_errno = errno;
    pa_assert(sig == SIGXCPU);

    if (phase == PHASE_IDLE) {
        time_t now;

#ifdef PRINT_CPU_LOAD
        char t[256];
#endif

        time(&now);

#ifdef PRINT_CPU_LOAD
        pa_snprintf(t, sizeof(t), "Using %0.1f%% CPU\n", (double)CPUTIME_INTERVAL_SOFT/(now-last_time)*100);
        write_err(t);
#endif

        if (CPUTIME_INTERVAL_SOFT >= ((now-last_time)*(double)CPUTIME_PERCENT/100)) {
            static const char c = 'X';

            write_err("Soft CPU time limit exhausted, terminating.\n");

            /* Try a soft cleanup */
            write(the_pipe[1], &c, sizeof(c));
            phase = PHASE_SOFT;
            reset_cpu_time(CPUTIME_INTERVAL_HARD);

        } else {

            /* Everything's fine */
            reset_cpu_time(CPUTIME_INTERVAL_SOFT);
            last_time = now;
        }

    } else if (phase == PHASE_SOFT) {
        write_err("Hard CPU time limit exhausted, terminating forcibly.\n");
        abort(); /* Forced exit */
    }

    errno = saved_errno;
}

/* Callback for IO events on the FIFO */
static void callback(pa_mainloop_api*m, pa_io_event*e, int fd, pa_io_event_flags_t f, void *userdata) {
    char c;
    pa_assert(m);
    pa_assert(e);
    pa_assert(f == PA_IO_EVENT_INPUT);
    pa_assert(e == io_event);
    pa_assert(fd == the_pipe[0]);

    pa_read(the_pipe[0], &c, sizeof(c), NULL);
    m->quit(m, 1); /* Quit the main loop */
}

/* Initializes CPU load limiter */
int pa_cpu_limit_init(pa_mainloop_api *m) {
    struct sigaction sa;

    pa_assert(m);
    pa_assert(!api);
    pa_assert(!io_event);
    pa_assert(the_pipe[0] == -1);
    pa_assert(the_pipe[1] == -1);
    pa_assert(!installed);

    time(&last_time);

    /* Prepare the main loop pipe */
    if (pipe(the_pipe) < 0) {
        pa_log("pipe() failed: %s", pa_cstrerror(errno));
        return -1;
    }

    pa_make_fd_nonblock(the_pipe[0]);
    pa_make_fd_nonblock(the_pipe[1]);
    pa_make_fd_cloexec(the_pipe[0]);
    pa_make_fd_cloexec(the_pipe[1]);

    api = m;
    io_event = api->io_new(m, the_pipe[0], PA_IO_EVENT_INPUT, callback, NULL);

    phase = PHASE_IDLE;

    /* Install signal handler for SIGXCPU */
    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = signal_handler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;

    if (sigaction(SIGXCPU, &sa, &sigaction_prev) < 0) {
        pa_cpu_limit_done();
        return -1;
    }

    installed = TRUE;

    reset_cpu_time(CPUTIME_INTERVAL_SOFT);

    return 0;
}

/* Shutdown CPU load limiter */
void pa_cpu_limit_done(void) {

    if (io_event) {
        pa_assert(api);
        api->io_free(io_event);
        io_event = NULL;
        api = NULL;
    }

    pa_close_pipe(the_pipe);

    if (installed) {
        pa_assert_se(sigaction(SIGXCPU, &sigaction_prev, NULL) >= 0);
        installed = FALSE;
    }
}

#else /* HAVE_SIGXCPU */

int pa_cpu_limit_init(PA_GCC_UNUSED pa_mainloop_api *m) {
    return 0;
}

void pa_cpu_limit_done(void) {
}

#endif