Apache Mesos
sched.hpp
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef __LINUX_SCHED_HPP__
18 #define __LINUX_SCHED_HPP__
19 
20 // This file contains Linux-only OS utilities.
21 #ifndef __linux__
22 #error "linux/sched.hpp is only available on Linux systems."
23 #endif
24 
25 #include <sched.h>
26 // Some old distributions, e.g., Redhat 5.5, do not correctly include
27 // linux/sched.h.
28 #include <linux/sched.h>
29 
30 
31 #include <sys/types.h>
32 
33 #include <stout/error.hpp>
34 #include <stout/none.hpp>
35 #include <stout/nothing.hpp>
36 #include <stout/option.hpp>
37 #include <stout/try.hpp>
38 
39 namespace sched {
40 
41 enum Policy
42 {
43  OTHER = SCHED_OTHER, // Default policy.
44  BATCH = SCHED_BATCH, // Non-interactive, CPU intensive.
45  IDLE = SCHED_IDLE, // Very low priority.
46  FIFO = SCHED_FIFO, // Realtime.
47  RR = SCHED_RR // Realtime, round-robin.
48 };
49 
50 
51 namespace policy {
52 
53 // Return the current scheduling policy for the specified pid, or for
54 // the caller if pid is None() or zero.
55 inline Try<Policy> get(const Option<pid_t>& pid = None())
56 {
57  int status = sched_getscheduler(pid.isSome() ? pid.get() : 0);
58 
59  if (status == -1) {
60  return ErrnoError("Failed to get scheduler policy");
61  }
62 
63  return static_cast<Policy>(status);
64 }
65 
66 
67 // Set the scheduling policy for the specified pid, or for the caller
68 // if pid is None() or zero. Only realtime scheduling policies (FIFO
69 // and RR) accept a priority (within a range, see
70 // sched_setscheduler(2)).
71 // TODO(idownes): Add wrappers around sched_get_priority_{min,max}.
72 inline Try<Nothing> set(
73  Policy policy,
74  const Option<pid_t>& pid = None(),
75  int priority = 0)
76 {
77  if ((policy == OTHER || policy == BATCH || policy == IDLE) &&
78  priority != 0) {
79  return Error("Non-real-time scheduling policies only support priority = 0");
80  }
81 
82  sched_param param;
83  param.sched_priority = priority;
84 
85  if (sched_setscheduler(pid.isSome() ? pid.get() : 0, policy, &param) == -1) {
86  return ErrnoError("Failed to set scheduler policy");
87  }
88 
89  return Nothing();
90 }
91 
92 } // namespace policy {
93 } // namespace sched {
94 
95 #endif // __LINUX_SCHED_HPP__
Definition: sched.hpp:46
Definition: nothing.hpp:16
Definition: errorbase.hpp:35
T & get()&
Definition: try.hpp:73
Definition: check.hpp:33
Result< ProcessStatus > status(pid_t pid)
Definition: proc.hpp:166
Definition: errorbase.hpp:49
Policy
Definition: sched.hpp:41
Definition: sched.hpp:39
Definition: sched.hpp:45
Definition: sched.hpp:47
bool isSome() const
Definition: try.hpp:70
Definition: sched.hpp:43
Definition: sched.hpp:44
Definition: none.hpp:27