-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler.c
More file actions
293 lines (234 loc) · 8.79 KB
/
scheduler.c
File metadata and controls
293 lines (234 loc) · 8.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* SMT-Aware Multi-Process Job Scheduling System
* Central Scheduler Process
*
* The scheduler receives jobs from producers, maintains a priority queue,
* and dispatches jobs to workers based on their load and SMT considerations.
*/
#include "common.h"
/* Priority queue node */
typedef struct pq_node {
job_t job;
struct pq_node *next;
} pq_node_t;
/* Priority queue */
typedef struct {
pq_node_t *head;
int size;
pthread_mutex_t lock;
} priority_queue_t;
static volatile sig_atomic_t running = 1;
static int msg_queue_id;
static shared_state_t *shared_mem;
static int shm_id;
static priority_queue_t job_queue;
void signal_handler(int sig) {
(void)sig;
running = 0;
}
/* Initialize priority queue */
void pq_init(priority_queue_t *pq) {
pq->head = NULL;
pq->size = 0;
pthread_mutex_init(&pq->lock, NULL);
}
/* Insert job into priority queue (higher priority first) */
void pq_insert(priority_queue_t *pq, job_t job) {
pq_node_t *new_node = malloc(sizeof(pq_node_t));
new_node->job = job;
new_node->next = NULL;
pthread_mutex_lock(&pq->lock);
if (pq->head == NULL || job.priority > pq->head->job.priority) {
new_node->next = pq->head;
pq->head = new_node;
} else {
pq_node_t *current = pq->head;
while (current->next != NULL &&
current->next->job.priority >= job.priority) {
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
}
pq->size++;
pthread_mutex_unlock(&pq->lock);
}
/* Remove and return highest priority job */
int pq_pop(priority_queue_t *pq, job_t *job) {
pthread_mutex_lock(&pq->lock);
if (pq->head == NULL) {
pthread_mutex_unlock(&pq->lock);
return -1;
}
pq_node_t *node = pq->head;
*job = node->job;
pq->head = node->next;
pq->size--;
free(node);
pthread_mutex_unlock(&pq->lock);
return 0;
}
/* Select best worker based on load and SMT awareness */
int select_worker(void) {
int best_worker = 0;
int min_load = shared_mem->workers[0].current_load;
for (int i = 1; i < MAX_WORKERS; i++) {
int load = shared_mem->workers[i].current_load;
/* SMT-aware selection: prefer workers with fewer active threads
* to reduce contention on shared physical cores */
if (load < min_load) {
min_load = load;
best_worker = i;
}
/* If loads are equal, prefer workers on different physical cores
* Assuming 2 logical cores per physical core (SMT-2) */
if (load == min_load && (i / 2) != (best_worker / 2)) {
/* Different physical core - reduces SMT contention */
best_worker = i;
}
}
return best_worker;
}
/* Initialize shared memory and IPC */
int init_ipc(void) {
/* Create message queue */
msg_queue_id = msgget(MSG_QUEUE_KEY, IPC_CREAT | 0666);
if (msg_queue_id == -1) {
LOG_ERROR("Failed to create message queue: %s", strerror(errno));
return -1;
}
/* Create shared memory */
shm_id = shmget(SHM_KEY, sizeof(shared_state_t), IPC_CREAT | 0666);
if (shm_id == -1) {
LOG_ERROR("Failed to create shared memory: %s", strerror(errno));
return -1;
}
/* Attach shared memory */
shared_mem = shmat(shm_id, NULL, 0);
if (shared_mem == (void *)-1) {
LOG_ERROR("Failed to attach shared memory: %s", strerror(errno));
return -1;
}
/* Initialize shared memory */
memset(shared_mem, 0, sizeof(shared_state_t));
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&shared_mem->global_lock, &attr);
for (int i = 0; i < MAX_WORKERS; i++) {
shared_mem->workers[i].worker_id = i;
pthread_mutex_init(&shared_mem->workers[i].lock, &attr);
}
pthread_mutexattr_destroy(&attr);
LOG_SCHEDULER("IPC initialized - MsgQ ID: %d, SHM ID: %d",
msg_queue_id, shm_id);
return 0;
}
/* Cleanup IPC resources */
void cleanup_ipc(void) {
/* Signal shutdown to all processes */
shared_mem->shutdown_flag = 1;
/* Send shutdown messages to workers */
for (int i = 0; i < MAX_WORKERS; i++) {
job_message_t shutdown_msg;
shutdown_msg.msg_type = MSG_TYPE_DISPATCH + i;
shutdown_msg.target_worker = i;
shutdown_msg.job.job_id = -1; /* Shutdown signal */
msgsnd(msg_queue_id, &shutdown_msg, sizeof(job_message_t) - sizeof(long), 0);
}
sleep(1); /* Give workers time to shutdown */
/* Detach and remove shared memory */
shmdt(shared_mem);
shmctl(shm_id, IPC_RMID, NULL);
/* Remove message queue */
msgctl(msg_queue_id, IPC_RMID, NULL);
LOG_SCHEDULER("IPC resources cleaned up");
}
/* Dispatcher thread - sends jobs to workers */
void *dispatcher_thread(void *arg) {
(void)arg;
while (running) {
job_t job;
if (pq_pop(&job_queue, &job) == 0) {
int target = select_worker();
job_message_t msg;
msg.msg_type = MSG_TYPE_DISPATCH + target;
msg.target_worker = target;
msg.job = job;
if (msgsnd(msg_queue_id, &msg, sizeof(job_message_t) - sizeof(long), 0) == 0) {
pthread_mutex_lock(&shared_mem->workers[target].lock);
shared_mem->workers[target].current_load++;
pthread_mutex_unlock(&shared_mem->workers[target].lock);
LOG_SCHEDULER("Dispatched %s to Worker %d (load: %d)",
job.job_name, target,
shared_mem->workers[target].current_load);
}
} else {
usleep(10000); /* Wait 10ms if queue empty */
}
}
return NULL;
}
/* Stats printer thread */
void *stats_thread(void *arg) {
(void)arg;
while (running) {
sleep(3);
printf("\n" COLOR_BLUE "═══════════════ SCHEDULER STATS ═══════════════\n");
printf("Queue Size: %d | Submitted: %d | Completed: %d\n",
job_queue.size,
shared_mem->total_jobs_submitted,
shared_mem->total_jobs_completed);
printf("Worker Loads: ");
for (int i = 0; i < MAX_WORKERS; i++) {
printf("W%d[%d] ", i, shared_mem->workers[i].current_load);
}
printf("\n═════════════════════════════════════════════\n" COLOR_RESET);
}
return NULL;
}
int main(void) {
pthread_t dispatcher, stats;
/* Setup signal handlers */
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
LOG_SCHEDULER("Starting SMT-Aware Job Scheduler");
/* Initialize */
if (init_ipc() != 0) {
return EXIT_FAILURE;
}
pq_init(&job_queue);
/* Start dispatcher thread */
pthread_create(&dispatcher, NULL, dispatcher_thread, NULL);
pthread_create(&stats, NULL, stats_thread, NULL);
LOG_SCHEDULER("Waiting for jobs from producers...");
/* Main receive loop */
while (running) {
job_message_t msg;
/* Non-blocking receive with timeout simulation */
ssize_t ret = msgrcv(msg_queue_id, &msg, sizeof(job_message_t) - sizeof(long),
MSG_TYPE_JOB, IPC_NOWAIT);
if (ret > 0) {
shared_mem->total_jobs_submitted++;
pq_insert(&job_queue, msg.job);
LOG_SCHEDULER("Received: %s (priority %d) - Queue size: %d",
msg.job.job_name, msg.job.priority, job_queue.size);
} else if (errno == ENOMSG) {
usleep(50000); /* Wait 50ms if no messages */
}
/* Check for completion messages */
ret = msgrcv(msg_queue_id, &msg, sizeof(job_message_t) - sizeof(long),
MSG_TYPE_COMPLETE, IPC_NOWAIT);
if (ret > 0) {
shared_mem->total_jobs_completed++;
}
}
LOG_SCHEDULER("Shutting down...");
pthread_cancel(dispatcher);
pthread_cancel(stats);
pthread_join(dispatcher, NULL);
pthread_join(stats, NULL);
cleanup_ipc();
LOG_SCHEDULER("Scheduler terminated");
return EXIT_SUCCESS;
}