Seastar
High performance C++ framework for concurrent servers
scheduling.hh
Go to the documentation of this file.
1 /*
2  * This file is open source software, licensed to you under the terms
3  * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4  * distributed with this work for additional information regarding copyright
5  * ownership. You may not use this file except in compliance with the License.
6  *
7  * 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,
12  * software distributed under the License is distributed on an
13  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14  * KIND, either express or implied. See the License for the
15  * specific language governing permissions and limitations
16  * under the License.
17  */
18 /*
19  * Copyright (C) 2016 Scylla DB Ltd
20  */
21 
22 #pragma once
23 
24 #include <typeindex>
25 #include <seastar/core/sstring.hh>
26 #include <seastar/core/function_traits.hh>
27 #include <seastar/util/gcc6-concepts.hh>
28 
30 
31 namespace seastar {
32 
33 constexpr unsigned max_scheduling_groups() { return 16; }
34 
35 template <typename... T>
36 class future;
37 
38 class reactor;
39 
40 class scheduling_group;
41 
42 namespace internal {
43 
44 // Returns an index between 0 and max_scheduling_groups()
45 unsigned scheduling_group_index(scheduling_group sg);
46 scheduling_group scheduling_group_from_index(unsigned index);
47 
48 }
49 
50 
61 future<scheduling_group> create_scheduling_group(sstring name, float shares);
62 
72 future<> destroy_scheduling_group(scheduling_group sg);
73 
84 future<> rename_scheduling_group(scheduling_group sg, sstring new_name);
85 
86 
105  scheduling_group_key_config(typeid(void)) {}
115  scheduling_group_key_config(const std::type_info& type_info) :
116  type_index(type_info) {}
120  size_t alignment;
122  std::type_index type_index;
124  std::function<void (void*)> constructor;
127  std::function<void (void*)> destructor;
128 
129 };
130 
131 
139 public:
141  scheduling_group_key(const scheduling_group_key&) = default;
142 private:
143  scheduling_group_key(unsigned long id) :
144  _id(id) {}
145  unsigned long _id;
146  unsigned long id() const {
147  return _id;
148  }
149  friend class reactor;
151  template<typename T>
153  template<typename T>
155 };
156 
157 namespace internal {
176 template<typename ConstructorType, typename Tuple, size_t...Idx>
177 void apply_constructor(void* pre_alocated_mem, Tuple args, std::index_sequence<Idx...> idx_seq) {
178  new (pre_alocated_mem) ConstructorType(std::get<Idx>(args)...);
179 }
180 }
181 
192 template <typename T, typename... ConstructorArgs>
193 scheduling_group_key_config
194 make_scheduling_group_key_config(ConstructorArgs... args) {
195  scheduling_group_key_config sgkc(typeid(T));
196  sgkc.allocation_size = sizeof(T);
197  sgkc.alignment = alignof(T);
198  sgkc.constructor = [args = std::make_tuple(args...)] (void* p) {
199  internal::apply_constructor<T>(p, args, std::make_index_sequence<sizeof...(ConstructorArgs)>());
200  };
201  sgkc.destructor = [] (void* p) {
202  static_cast<T*>(p)->~T();
203  };
204  return sgkc;
205 }
206 
214 future<scheduling_group_key> scheduling_group_key_create(scheduling_group_key_config cfg);
215 
223 template<typename T>
224 T& scheduling_group_get_specific(scheduling_group sg, scheduling_group_key key);
225 
226 
232  unsigned _id;
233 private:
234  explicit scheduling_group(unsigned id) : _id(id) {}
235 public:
237  constexpr scheduling_group() noexcept : _id(0) {} // must be constexpr for current_scheduling_group_holder
238  bool active() const;
239  const sstring& name() const;
240  bool operator==(scheduling_group x) const { return _id == x._id; }
241  bool operator!=(scheduling_group x) const { return _id != x._id; }
242  bool is_main() const { return _id == 0; }
243  template<typename T>
251  return scheduling_group_get_specific<T>(*this, key);
252  }
265  void set_shares(float shares);
266  friend future<scheduling_group> create_scheduling_group(sstring name, float shares);
269  friend class reactor;
270  friend unsigned internal::scheduling_group_index(scheduling_group sg);
271  friend scheduling_group internal::scheduling_group_from_index(unsigned index);
272 
273  template<typename SpecificValType, typename Mapper, typename Reducer, typename Initial>
274  GCC6_CONCEPT( requires requires(SpecificValType specific_val, Mapper mapper, Reducer reducer, Initial initial) {
275  {reducer(initial, mapper(specific_val))} -> Initial;
276  })
277  friend future<typename function_traits<Reducer>::return_type>
278  map_reduce_scheduling_group_specific(Mapper mapper, Reducer reducer, Initial initial_val, scheduling_group_key key);
279 
280  template<typename SpecificValType, typename Reducer, typename Initial>
281  GCC6_CONCEPT( requires requires(SpecificValType specific_val, Reducer reducer, Initial initial) {
282  {reducer(initial, specific_val)} -> Initial;
283  })
284  friend future<typename function_traits<Reducer>::return_type>
285  reduce_scheduling_group_specific(Reducer reducer, Initial initial_val, scheduling_group_key key);
286 
287 
288 };
289 
291 namespace internal {
292 
293 inline
294 unsigned
295 scheduling_group_index(scheduling_group sg) {
296  return sg._id;
297 }
298 
299 inline
300 scheduling_group
301 scheduling_group_from_index(unsigned index) {
302  return scheduling_group(index);
303 }
304 
305 inline
306 scheduling_group*
307 current_scheduling_group_ptr() {
308  // Slow unless constructor is constexpr
309  static thread_local scheduling_group sg;
310  return &sg;
311 }
312 
313 }
315 
317 inline
318 scheduling_group
320  return *internal::current_scheduling_group_ptr();
321 }
322 
323 inline
324 scheduling_group
325 default_scheduling_group() {
326  return scheduling_group();
327 }
328 
329 inline
330 bool
331 scheduling_group::active() const {
332  return *this == current_scheduling_group();
333 }
334 
335 }
336 
337 namespace std {
338 
339 template <>
340 struct hash<seastar::scheduling_group> {
341  size_t operator()(seastar::scheduling_group sg) const {
342  return seastar::internal::scheduling_group_index(sg);
343  }
344 };
345 
346 }
A representation of a possibly not-yet-computed value.
Definition: future.hh:79
scheduling_group_key(const scheduling_group_key &)=default
The only user allowed operation on a key is copying.
scheduling_group current_scheduling_group()
Returns the current scheduling group.
Definition: scheduling.hh:319
size_t allocation_size
The allocation size for the value (usually: sizeof(T))
Definition: scheduling.hh:118
std::function< void(void *)> destructor
Definition: scheduling.hh:127
future< scheduling_group_key > scheduling_group_key_create(scheduling_group_key_config cfg)
STL namespace.
friend future< typename function_traits< Reducer >::return_type > map_reduce_scheduling_group_specific(Mapper mapper, Reducer reducer, Initial initial_val, scheduling_group_key key)
Definition: scheduling_specific.hh:80
Definition: reactor.hh:153
friend future rename_scheduling_group(scheduling_group sg, sstring new_name)
friend future< scheduling_group_key > scheduling_group_key_create(scheduling_group_key_config cfg)
T & scheduling_group_get_specific(scheduling_group sg, scheduling_group_key key)
Definition: scheduling_specific.hh:38
Identifies function calls that are accounted as a group.
Definition: scheduling.hh:231
future destroy_scheduling_group(scheduling_group sg)
std::type_index type_index
Holds the type information for debug mode runtime validation.
Definition: scheduling.hh:122
scheduling_group_key_config()
Definition: scheduling.hh:104
friend T & scheduling_group_get_specific(scheduling_group sg, scheduling_group_key key)
Definition: scheduling_specific.hh:38
future< scheduling_group > create_scheduling_group(sstring name, float shares)
constexpr scheduling_group() noexcept
Creates a scheduling_group object denoting the default group.
Definition: scheduling.hh:237
size_t alignment
The required alignment of the value (usually: alignof(T))
Definition: scheduling.hh:120
friend future< scheduling_group > create_scheduling_group(sstring name, float shares)
future rename_scheduling_group(scheduling_group sg, sstring new_name)
scheduling_group_key_config(const std::type_info &type_info)
Definition: scheduling.hh:115
Definition: scheduling.hh:100
void set_shares(float shares)
scheduling_group_key_config make_scheduling_group_key_config(ConstructorArgs... args)
Definition: scheduling.hh:194
friend future destroy_scheduling_group(scheduling_group sg)
T & get_specific(scheduling_group_key key)
Definition: scheduling.hh:250
Seastar API namespace.
Definition: abort_on_ebadf.hh:24
friend future< typename function_traits< Reducer >::return_type > reduce_scheduling_group_specific(Reducer reducer, Initial initial_val, scheduling_group_key key)
Definition: scheduling_specific.hh:112
Definition: scheduling.hh:138
std::function< void(void *)> constructor
A function that will be called for each newly allocated value.
Definition: scheduling.hh:124