Seastar
High performance C++ framework for concurrent servers
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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#ifndef SEASTAR_MODULE
25#include <chrono>
26#include <concepts>
27#include <functional>
28#include <typeindex>
29#endif
30#include <seastar/core/sstring.hh>
31#include <seastar/core/function_traits.hh>
32#include <seastar/util/modules.hh>
33
35
36namespace seastar {
37
38SEASTAR_MODULE_EXPORT_BEGIN
39constexpr unsigned max_scheduling_groups() { return SEASTAR_SCHEDULING_GROUPS_COUNT; }
40
41template <typename T = void>
42class future;
43
44class reactor;
45
46class scheduling_group;
47class scheduling_group_key;
48
49using sched_clock = std::chrono::steady_clock;
50SEASTAR_MODULE_EXPORT_END
51
52namespace internal {
53
54// Returns an index between 0 and max_scheduling_groups()
55unsigned scheduling_group_index(scheduling_group sg) noexcept;
56scheduling_group scheduling_group_from_index(unsigned index) noexcept;
57
58unsigned long scheduling_group_key_id(scheduling_group_key) noexcept;
59
60template<typename T>
61T* scheduling_group_get_specific_ptr(scheduling_group sg, scheduling_group_key key) noexcept;
62
63}
64
65SEASTAR_MODULE_EXPORT_BEGIN
66
77future<scheduling_group> create_scheduling_group(sstring name, float shares) noexcept;
78
92future<scheduling_group> create_scheduling_group(sstring name, sstring shortname, float shares) noexcept;
93
104
115future<> rename_scheduling_group(scheduling_group sg, sstring new_name) noexcept;
127future<> rename_scheduling_group(scheduling_group sg, sstring new_name, sstring new_shortname) noexcept;
128
129
148 scheduling_group_key_config(typeid(void)) {}
158 scheduling_group_key_config(const std::type_info& type_info) :
159 type_index(type_info) {}
163 size_t alignment;
165 std::type_index type_index;
167 std::function<void (void*)> constructor;
169 std::function<void (void*)> rename;
172 std::function<void (void*)> destructor;
173
174};
175
176
184public:
186 scheduling_group_key(const scheduling_group_key&) noexcept = default;
187 scheduling_group_key(scheduling_group_key&&) noexcept = default;
188private:
189 scheduling_group_key(unsigned long id) noexcept :
190 _id(id) {}
191 unsigned long _id;
192 unsigned long id() const noexcept {
193 return _id;
194 }
196 template<typename T>
197 friend T* internal::scheduling_group_get_specific_ptr(scheduling_group sg, scheduling_group_key key) noexcept;
198 template<typename T>
200
201 friend unsigned long internal::scheduling_group_key_id(scheduling_group_key key) noexcept;
202};
203
204SEASTAR_MODULE_EXPORT_END
205namespace internal {
206
207inline unsigned long scheduling_group_key_id(scheduling_group_key key) noexcept {
208 return key.id();
209}
210
229template<typename ConstructorType, typename Tuple, size_t...Idx>
230void apply_constructor(void* pre_alocated_mem, Tuple args, std::index_sequence<Idx...>) {
231 new (pre_alocated_mem) ConstructorType(std::get<Idx>(args)...);
232}
233}
234SEASTAR_MODULE_EXPORT_BEGIN
235
246template <typename T, typename... ConstructorArgs>
247scheduling_group_key_config
248make_scheduling_group_key_config(ConstructorArgs... args) {
249 scheduling_group_key_config sgkc(typeid(T));
250 sgkc.allocation_size = sizeof(T);
251 sgkc.alignment = alignof(T);
252 sgkc.constructor = [args = std::make_tuple(args...)] (void* p) {
253 internal::apply_constructor<T>(p, args, std::make_index_sequence<sizeof...(ConstructorArgs)>());
254 };
255 sgkc.destructor = [] (void* p) {
256 static_cast<T*>(p)->~T();
257 };
258 return sgkc;
259}
260
269
277template<typename T>
279
280
286 unsigned _id;
287private:
288 explicit scheduling_group(unsigned id) noexcept : _id(id) {}
289public:
291 constexpr scheduling_group() noexcept : _id(0) {} // must be constexpr for current_scheduling_group_holder
292 bool active() const noexcept;
293 const sstring& name() const noexcept;
294 const sstring& short_name() const noexcept;
295 bool operator==(scheduling_group x) const noexcept { return _id == x._id; }
296 bool operator!=(scheduling_group x) const noexcept { return _id != x._id; }
297 bool is_main() const noexcept { return _id == 0; }
298 bool is_at_exit() const noexcept { return _id == 1; }
299 template<typename T>
307 return *internal::scheduling_group_get_specific_ptr<T>(*this, key);
308 }
321 void set_shares(float shares) noexcept;
322
327 float get_shares() const noexcept;
328
336 future<> update_io_bandwidth(uint64_t bandwidth) const;
337
338 friend future<scheduling_group> create_scheduling_group(sstring name, sstring shortname, float shares) noexcept;
340 friend future<> rename_scheduling_group(scheduling_group sg, sstring new_name, sstring new_shortname) noexcept;
341 friend class reactor;
342 friend unsigned internal::scheduling_group_index(scheduling_group sg) noexcept;
343 friend scheduling_group internal::scheduling_group_from_index(unsigned index) noexcept;
344
345 template<typename SpecificValType, typename Mapper, typename Reducer, typename Initial>
346 requires requires(SpecificValType specific_val, Mapper mapper, Reducer reducer, Initial initial) {
347 {reducer(initial, mapper(specific_val))} -> std::convertible_to<Initial>;
348 }
350 map_reduce_scheduling_group_specific(Mapper mapper, Reducer reducer, Initial initial_val, scheduling_group_key key);
351
352 template<typename SpecificValType, typename Reducer, typename Initial>
353 requires requires(SpecificValType specific_val, Reducer reducer, Initial initial) {
354 {reducer(initial, specific_val)} -> std::convertible_to<Initial>;
355 }
356 friend future<typename function_traits<Reducer>::return_type>
357 reduce_scheduling_group_specific(Reducer reducer, Initial initial_val, scheduling_group_key key);
358
359
360};
361
363SEASTAR_MODULE_EXPORT_END
364namespace internal {
365
366inline
367unsigned
368scheduling_group_index(scheduling_group sg) noexcept {
369 return sg._id;
370}
371
372inline
373scheduling_group
374scheduling_group_from_index(unsigned index) noexcept {
375 return scheduling_group(index);
376}
377
378#ifdef SEASTAR_BUILD_SHARED_LIBS
379scheduling_group*
380current_scheduling_group_ptr() noexcept;
381#else
382inline
383scheduling_group*
384current_scheduling_group_ptr() noexcept {
385 // Slow unless constructor is constexpr
386 static thread_local scheduling_group sg;
387 return &sg;
388}
389#endif
390}
392
393SEASTAR_MODULE_EXPORT_BEGIN
395inline
396scheduling_group
398 return *internal::current_scheduling_group_ptr();
399}
400
401inline
402scheduling_group
403default_scheduling_group() noexcept {
404 return scheduling_group();
405}
406
407SEASTAR_MODULE_EXPORT_END
408
409inline
410bool
411scheduling_group::active() const noexcept {
412 return *this == current_scheduling_group();
413}
414
415}
416
417namespace std {
418
419SEASTAR_MODULE_EXPORT
420template <>
421struct hash<seastar::scheduling_group> {
422 size_t operator()(seastar::scheduling_group sg) const noexcept {
423 return seastar::internal::scheduling_group_index(sg);
424 }
425};
426
427}
A representation of a possibly not-yet-computed value.
Definition: future.hh:1240
Definition: reactor.hh:146
Definition: scheduling.hh:183
friend future< scheduling_group_key > scheduling_group_key_create(scheduling_group_key_config cfg) noexcept
friend T & scheduling_group_get_specific(scheduling_group_key key) noexcept
Definition: scheduling_specific.hh:120
scheduling_group_key(const scheduling_group_key &) noexcept=default
The only user allowed operation on a key is copying.
Identifies function calls that are accounted as a group.
Definition: scheduling.hh:285
float get_shares() const noexcept
constexpr scheduling_group() noexcept
Creates a scheduling_group object denoting the default group.
Definition: scheduling.hh:291
friend future rename_scheduling_group(scheduling_group sg, sstring new_name, sstring new_shortname) noexcept
friend future destroy_scheduling_group(scheduling_group sg) noexcept
future update_io_bandwidth(uint64_t bandwidth) const
Updates the current IO bandwidth for a given scheduling group.
friend future< scheduling_group > create_scheduling_group(sstring name, sstring shortname, float shares) noexcept
void set_shares(float shares) noexcept
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:184
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:150
T & get_specific(scheduling_group_key key) noexcept
Definition: scheduling.hh:306
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
scheduling_group current_scheduling_group() noexcept
Returns the current scheduling group.
Definition: scheduling.hh:397
T & scheduling_group_get_specific(scheduling_group sg, scheduling_group_key key)
Definition: scheduling_specific.hh:104
future destroy_scheduling_group(scheduling_group sg) noexcept
future rename_scheduling_group(scheduling_group sg, sstring new_name) noexcept
future< scheduling_group > create_scheduling_group(sstring name, float shares) noexcept
scheduling_group_key_config make_scheduling_group_key_config(ConstructorArgs... args)
Definition: scheduling.hh:248
future< scheduling_group_key > scheduling_group_key_create(scheduling_group_key_config cfg) noexcept
STL namespace.
Definition: scheduling.hh:143
size_t allocation_size
The allocation size for the value (usually: sizeof(T))
Definition: scheduling.hh:161
std::function< void(void *)> constructor
A function that will be called for each newly allocated value.
Definition: scheduling.hh:167
std::function< void(void *)> rename
A function that will be called for each value after the scheduling group is renamed.
Definition: scheduling.hh:169
std::type_index type_index
Holds the type information for debug mode runtime validation.
Definition: scheduling.hh:165
scheduling_group_key_config()
Definition: scheduling.hh:147
size_t alignment
The required alignment of the value (usually: alignof(T))
Definition: scheduling.hh:163
scheduling_group_key_config(const std::type_info &type_info)
Definition: scheduling.hh:158
std::function< void(void *)> destructor
Definition: scheduling.hh:172