Realm
A distributed, event-based tasking library
Loading...
Searching...
No Matches
bgwork.h
Go to the documentation of this file.
1/*
2 * Copyright 2025 Stanford University, NVIDIA Corporation
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
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, 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
18// manager for background work that can be performed by available threads
19
20#ifndef REALM_BGWORK_H
21#define REALM_BGWORK_H
22
23#include "realm/atomics.h"
24#include "realm/threads.h"
25#include "realm/mutex.h"
26#include "realm/cmdline.h"
27#include "realm/timers.h"
28
29#include <string>
30
31namespace Realm {
32
33 class BackgroundWorkItem;
34 class BackgroundWorkThread;
35
37 public:
40
41 struct Config {
42 unsigned generic_workers = 2; // non-numa-specific workers
43 unsigned per_numa_workers = 0;
44 bool pin_generic = false;
45 bool pin_numa = false;
47 long long worker_spin_interval = 0;
48 long long work_item_timeslice = 100000;
49 };
50
51 void configure_from_cmdline(std::vector<std::string> &cmdline);
52
54
56
57 typedef unsigned long long BitMask;
58 static const size_t MAX_WORK_ITEMS = 256;
59 static const size_t BITMASK_BITS = 8 * sizeof(BitMask);
60 static const size_t BITMASK_ARRAY_SIZE =
62
63 class Worker {
64 public:
65 Worker(void);
66 ~Worker(void);
67
69
70 // configuration settings impact which work items this worker can handle
71 void set_max_timeslice(long long _timeslice_in_ns);
72 void set_numa_domain(int _numa_domain); // -1 == dont care
73
74 bool do_work(long long max_time_in_ns, atomic<bool> *interrupt_flag);
75
76 protected:
78 unsigned starting_slot;
81 long long max_timeslice;
83 };
84
85 protected:
87 friend class BackgroundWorkItem;
88
90 void release_slot(unsigned slot);
91 void advertise_work(unsigned slot);
92
94
95 // mutex protects assignment of work items to slots
99
102
104
105 // to manage sleeping workers, we need to stuff three things into a
106 // single atomically-updatable state variable:
107 // a) the number of active work items - no worker should sleep if there are
108 // any active work items, and any increment of the active work items
109 // should wake up one sleeping worker (unless there are none)
110 // (NOTE: this counter can temporarily underflow, so needs to be the top
111 // field in the variable to avoid temporarily corrupting other fields)
112 // b) the number of sleeping workers
113 // c) a bit indicating if a shutdown has been requested (which should wake
114 // up all remaining workers)
115 static const uint32_t STATE_SHUTDOWN_BIT = 1;
116 static const uint32_t STATE_ACTIVE_ITEMS_MASK = 0xFFFF;
117 static const unsigned STATE_ACTIVE_ITEMS_SHIFT = 16;
118 static const uint32_t STATE_SLEEPING_WORKERS_MASK = 0xFFF;
119 static const unsigned STATE_SLEEPING_WORKERS_SHIFT = 4;
121
122 // sleeping workers go in a doorbell list with a delegating mutex
125
126 std::vector<BackgroundWorkThread *> dedicated_workers;
127 };
128
130 public:
131 BackgroundWorkItem(const std::string &_name);
132 virtual ~BackgroundWorkItem(void);
133
134 void add_to_manager(BackgroundWorkManager *_manager, int _numa_domain = -1,
135 long long _min_timeslice_needed = -1);
136
137 // perform work, trying to respect the 'work_until' time limit - return
138 // true to request requeuing (this is more efficient than calling
139 // 'make_active' at the end of 'do_work') or false if all work has been
140 // completed (or if 'make_active' has already been called)
141 virtual bool do_work(TimeLimit work_until) = 0;
142
143 protected:
145
146 // mark this work item as active (i.e. having work to do)
147 void make_active(void);
148
149 std::string name;
153 unsigned index;
154
155#ifdef DEBUG_REALM
156 public:
157 // in debug mode, we'll track the state of a work item to avoid
158 // duplicate activations or activations after shutdown
159 enum State
160 {
161 STATE_IDLE,
162 STATE_ACTIVE,
163 STATE_SHUTDOWN,
164 };
165
166 void make_inactive(void); // called immediately before 'do_work'
167 void shutdown_work_item(void);
168
169 protected:
170 atomic<State> state;
171#endif
172 };
173
174}; // namespace Realm
175
176#endif
Definition bgwork.h:129
std::string name
Definition bgwork.h:149
long long min_timeslice_needed
Definition bgwork.h:152
virtual ~BackgroundWorkItem(void)
BackgroundWorkManager * manager
Definition bgwork.h:150
virtual bool do_work(TimeLimit work_until)=0
unsigned index
Definition bgwork.h:153
BackgroundWorkItem(const std::string &_name)
void add_to_manager(BackgroundWorkManager *_manager, int _numa_domain=-1, long long _min_timeslice_needed=-1)
int numa_domain
Definition bgwork.h:151
void set_numa_domain(int _numa_domain)
long long max_timeslice
Definition bgwork.h:81
BackgroundWorkManager * manager
Definition bgwork.h:77
int numa_domain
Definition bgwork.h:82
unsigned starting_slot
Definition bgwork.h:78
BitMask allowed_work_item_mask[BITMASK_ARRAY_SIZE]
Definition bgwork.h:80
BitMask known_work_item_mask[BITMASK_ARRAY_SIZE]
Definition bgwork.h:79
bool do_work(long long max_time_in_ns, atomic< bool > *interrupt_flag)
void set_manager(BackgroundWorkManager *_manager)
void set_max_timeslice(long long _timeslice_in_ns)
Definition bgwork.h:36
static const unsigned STATE_ACTIVE_ITEMS_SHIFT
Definition bgwork.h:117
atomic< unsigned > num_work_items
Definition bgwork.h:97
static const size_t BITMASK_BITS
Definition bgwork.h:59
atomic< BitMask > active_work_item_mask[BITMASK_ARRAY_SIZE]
Definition bgwork.h:98
static const unsigned STATE_SLEEPING_WORKERS_SHIFT
Definition bgwork.h:119
Config cfg
Definition bgwork.h:93
void release_slot(unsigned slot)
unsigned long long BitMask
Definition bgwork.h:57
BackgroundWorkItem * work_items[MAX_WORK_ITEMS]
Definition bgwork.h:101
void start_dedicated_workers(Realm::CoreReservationSet &crs)
friend class BackgroundWorkThread
Definition bgwork.h:103
void configure_from_cmdline(std::vector< std::string > &cmdline)
DoorbellList db_list
Definition bgwork.h:124
std::vector< BackgroundWorkThread * > dedicated_workers
Definition bgwork.h:126
static const uint32_t STATE_SLEEPING_WORKERS_MASK
Definition bgwork.h:118
static const uint32_t STATE_ACTIVE_ITEMS_MASK
Definition bgwork.h:116
static const uint32_t STATE_SHUTDOWN_BIT
Definition bgwork.h:115
atomic< uint32_t > worker_state
Definition bgwork.h:120
static const size_t BITMASK_ARRAY_SIZE
Definition bgwork.h:60
atomic< int > work_item_usecounts[MAX_WORK_ITEMS]
Definition bgwork.h:100
DelegatingMutex db_mutex
Definition bgwork.h:123
Mutex mutex
Definition bgwork.h:96
unsigned assign_slot(BackgroundWorkItem *item)
void advertise_work(unsigned slot)
static const size_t MAX_WORK_ITEMS
Definition bgwork.h:58
Definition threads.h:382
Definition mutex.h:299
Definition mutex.h:187
Definition timers.h:129
Definition mutex.h:223
Definition atomics.h:31
#define REALM_INTERNAL_API_EXTERNAL_LINKAGE
Definition compiler_support.h:218
Definition activemsg.h:38
unsigned per_numa_workers
Definition bgwork.h:43
long long work_item_timeslice
Definition bgwork.h:48
size_t worker_stacksize_in_kb
Definition bgwork.h:46
long long worker_spin_interval
Definition bgwork.h:47
bool pin_numa
Definition bgwork.h:45
unsigned generic_workers
Definition bgwork.h:42
bool pin_generic
Definition bgwork.h:44