1 : /*
2 : * Copyright (c) 2011 The Native Client Authors. All rights reserved.
3 : * Use of this source code is governed by a BSD-style license that can be
4 : * found in the LICENSE file.
5 : */
6 :
7 : /*
8 : * Linux thread priority support.
9 : */
10 : #include <errno.h>
11 : #include <sched.h>
12 : #include <stdio.h>
13 : #include <string.h>
14 : #include <sys/resource.h>
15 :
16 : #include "native_client/src/shared/platform/nacl_log.h"
17 : #include "native_client/src/trusted/service_runtime/include/sys/nacl_nice.h"
18 :
19 294 : void NaClThreadNiceInit(void) {
20 294 : }
21 :
22 : /* Linux version - threads are processes, so use setpriority in lieu
23 : * of RLIMIT_RTPRIO.
24 : * This appears to be lockup-free. To enable the privileged realtime
25 : * threads, /etc/security/limits.conf typically needs to include a
26 : * line like this:
27 : * @audio - nice -10
28 : * Ubuntu systems (and others?) automatically grant audio group permission
29 : * to user who login on the console.
30 : */
31 : /* Linux realtime scheduling is pretty broken at this time. See
32 : * http://lwn.net/Articles/339316/ for a useful overview.
33 : */
34 0 : int nacl_thread_nice(int nacl_nice) {
35 0 : const int kRealTimePriority = -10;
36 0 : const int kBackgroundPriority = 10;
37 0 : const int kNormalPriority = 0;
38 :
39 0 : switch (nacl_nice) {
40 : case NICE_REALTIME:
41 0 : if (0 == setpriority(PRIO_PROCESS, 0, kRealTimePriority)) {
42 0 : return 0; /* success */
43 : }
44 : /* Sorry; no RT priviledges. Fall through to NICE_NORMAL */
45 : case NICE_NORMAL:
46 0 : if (0 == setpriority(PRIO_PROCESS, 0, kNormalPriority)) {
47 0 : return 0; /* success */
48 : }
49 0 : break;
50 : case NICE_BACKGROUND:
51 0 : if (0 == setpriority(PRIO_PROCESS, 0, kBackgroundPriority)) {
52 0 : return 0; /* success */
53 : }
54 0 : break;
55 : default:
56 0 : NaClLog(LOG_WARNING, "nacl_thread_nice failed (bad nice value)\n");
57 0 : return -1;
58 : break;
59 : }
60 0 : return -1;
61 : }
|