1 /* Copyright (c) 2009, 2012 The Regents of the University of California
2 * Barret Rhoden <brho@cs.berkeley.edu>
3 * See LICENSE for details.
5 * Scheduling and dispatching. */
8 #include <corerequest.h>
17 #include <sys/queue.h>
18 #include <arsc_server.h>
19 #include <hashtable.h>
21 /* Process Lists. 'unrunnable' is a holding list for SCPs that are running or
22 * waiting or otherwise not considered for sched decisions. */
23 struct proc_list unrunnable_scps = TAILQ_HEAD_INITIALIZER(unrunnable_scps);
24 struct proc_list runnable_scps = TAILQ_HEAD_INITIALIZER(runnable_scps);
25 /* mcp lists. we actually could get by with one list and a TAILQ_CONCAT, but
26 * I'm expecting to want the flexibility of the pointers later. */
27 struct proc_list all_mcps_1 = TAILQ_HEAD_INITIALIZER(all_mcps_1);
28 struct proc_list all_mcps_2 = TAILQ_HEAD_INITIALIZER(all_mcps_2);
29 struct proc_list *primary_mcps = &all_mcps_1;
30 struct proc_list *secondary_mcps = &all_mcps_2;
32 /* Helper, defined below */
33 static void __core_request(struct proc *p, uint32_t amt_needed);
34 static void add_to_list(struct proc *p, struct proc_list *list);
35 static void remove_from_list(struct proc *p, struct proc_list *list);
36 static void switch_lists(struct proc *p, struct proc_list *old,
37 struct proc_list *new);
38 static void __run_mcp_ksched(void *arg); /* don't call directly */
39 static uint32_t get_cores_needed(struct proc *p);
41 /* Locks / sync tools */
43 /* poke-style ksched - ensures the MCP ksched only runs once at a time. since
44 * only one mcp ksched runs at a time, while this is set, the ksched knows no
45 * cores are being allocated by other code (though they could be dealloc, due to
48 * The main value to this sync method is to make the 'make sure the ksched runs
49 * only once at a time and that it actually runs' invariant/desire wait-free, so
50 * that it can be called anywhere (deep event code, etc).
52 * As the ksched gets smarter, we'll probably embedd this poker in a bigger
53 * struct that can handle the posting of different types of work. */
54 struct poke_tracker ksched_poker = POKE_INITIALIZER(__run_mcp_ksched);
56 /* this 'big ksched lock' protects a bunch of things, which i may make fine
58 /* - protects the integrity of proc tailqs/structures, as well as the membership
59 * of a proc on those lists. proc lifetime within the ksched but outside this
60 * lock is protected by the proc kref. */
61 //spinlock_t proclist_lock = SPINLOCK_INITIALIZER; /* subsumed by bksl */
62 /* - protects the provisioning assignment, and the integrity of all prov
63 * lists (the lists of each proc). */
64 //spinlock_t prov_lock = SPINLOCK_INITIALIZER;
65 /* - protects allocation structures */
66 //spinlock_t alloc_lock = SPINLOCK_INITIALIZER;
67 spinlock_t sched_lock = SPINLOCK_INITIALIZER;
69 /* Alarm struct, for our example 'timer tick' */
70 struct alarm_waiter ksched_waiter;
72 #define TIMER_TICK_USEC 10000 /* 10msec */
74 /* Helper: Sets up a timer tick on the calling core to go off 10 msec from now.
75 * This assumes the calling core is an LL core, etc. */
76 static void set_ksched_alarm(void)
78 set_awaiter_rel(&ksched_waiter, TIMER_TICK_USEC);
79 set_alarm(&per_cpu_info[core_id()].tchain, &ksched_waiter);
82 /* Need a kmsg to just run the sched, but not to rearm */
83 static void __just_sched(uint32_t srcid, long a0, long a1, long a2)
88 /* RKM alarm, to run the scheduler tick (not in interrupt context) and reset the
89 * alarm. Note that interrupts will be disabled, but this is not the same as
90 * interrupt context. We're a routine kmsg, which means the core is in a
92 static void __ksched_tick(struct alarm_waiter *waiter)
94 /* TODO: imagine doing some accounting here */
96 /* Set our alarm to go off, relative to now. This means we might lag a bit,
97 * and our ticks won't match wall clock time. But if we do incremental,
98 * we'll actually punish the next process because the kernel took too long
99 * for the previous process. Ultimately, if we really care, we should
100 * account for the actual time used. */
101 set_awaiter_rel(&ksched_waiter, TIMER_TICK_USEC);
102 set_alarm(&per_cpu_info[core_id()].tchain, &ksched_waiter);
105 void schedule_init(void)
107 spin_lock(&sched_lock);
108 assert(!core_id()); /* want the alarm on core0 for now */
109 init_awaiter(&ksched_waiter, __ksched_tick);
112 spin_unlock(&sched_lock);
114 #ifdef CONFIG_ARSC_SERVER
115 /* Most likely we'll have a syscall and a process that dedicates itself to
116 * running this. Or if it's a kthread, we don't need a core. */
117 #error "Find a way to get a core. Probably a syscall to run a server."
118 int arsc_coreid = get_any_idle_core();
119 assert(arsc_coreid >= 0);
120 send_kernel_message(arsc_coreid, arsc_server, 0, 0, 0, KMSG_ROUTINE);
121 printk("Using core %d for the ARSC server\n", arsc_coreid);
122 #endif /* CONFIG_ARSC_SERVER */
125 /* Round-robins on whatever list it's on */
126 static void add_to_list(struct proc *p, struct proc_list *new)
128 assert(!(p->ksched_data.cur_list));
129 TAILQ_INSERT_TAIL(new, p, ksched_data.proc_link);
130 p->ksched_data.cur_list = new;
133 static void remove_from_list(struct proc *p, struct proc_list *old)
135 assert(p->ksched_data.cur_list == old);
136 TAILQ_REMOVE(old, p, ksched_data.proc_link);
137 p->ksched_data.cur_list = 0;
140 static void switch_lists(struct proc *p, struct proc_list *old,
141 struct proc_list *new)
143 remove_from_list(p, old);
147 /* Removes from whatever list p is on */
148 static void remove_from_any_list(struct proc *p)
150 if (p->ksched_data.cur_list) {
151 TAILQ_REMOVE(p->ksched_data.cur_list, p, ksched_data.proc_link);
152 p->ksched_data.cur_list = 0;
156 /************** Process Management Callbacks **************/
158 * - the proc lock is NOT held for any of these calls. currently, there is no
159 * lock ordering between the sched lock and the proc lock. since the proc
160 * code doesn't know what we do, it doesn't hold its lock when calling our
162 * - since the proc lock isn't held, the proc could be dying, which means we
163 * will receive a __sched_proc_destroy() either before or after some of these
164 * other CBs. the CBs related to list management need to check and abort if
166 void __sched_proc_register(struct proc *p)
168 assert(!proc_is_dying(p)); /* shouldn't be able to happen yet */
169 /* one ref for the proc's existence, cradle-to-grave */
170 proc_incref(p, 1); /* need at least this OR the 'one for existing' */
171 spin_lock(&sched_lock);
172 corealloc_proc_init(p);
173 add_to_list(p, &unrunnable_scps);
174 spin_unlock(&sched_lock);
177 /* Returns 0 if it succeeded, an error code otherwise. */
178 void __sched_proc_change_to_m(struct proc *p)
180 spin_lock(&sched_lock);
181 /* Need to make sure they aren't dying. if so, we already dealt with their
182 * list membership, etc (or soon will). taking advantage of the 'immutable
183 * state' of dying (so long as refs are held). */
184 if (proc_is_dying(p)) {
185 spin_unlock(&sched_lock);
188 /* Catch user bugs */
189 if (!p->procdata->res_req[RES_CORES].amt_wanted) {
190 printk("[kernel] process needs to specify amt_wanted\n");
191 p->procdata->res_req[RES_CORES].amt_wanted = 1;
193 /* For now, this should only ever be called on an unrunnable. It's
194 * probably a bug, at this stage in development, to do o/w. */
195 remove_from_list(p, &unrunnable_scps);
196 //remove_from_any_list(p); /* ^^ instead of this */
197 add_to_list(p, primary_mcps);
198 spin_unlock(&sched_lock);
199 //poke_ksched(p, RES_CORES);
202 /* Sched callback called when the proc dies. pc_arr holds the cores the proc
203 * had, if any, and nr_cores tells us how many are in the array.
205 * An external, edible ref is passed in. when we return and they decref,
206 * __proc_free will be called (when the last one is done). */
207 void __sched_proc_destroy(struct proc *p, uint32_t *pc_arr, uint32_t nr_cores)
209 spin_lock(&sched_lock);
210 /* Unprovision any cores. Note this is different than track_core_dealloc.
211 * The latter does bookkeeping when an allocation changes. This is a
212 * bulk *provisioning* change. */
213 __unprovision_all_cores(p);
214 /* Remove from whatever list we are on (if any - might not be on one if it
215 * was in the middle of __run_mcp_sched) */
216 remove_from_any_list(p);
218 __track_core_dealloc_bulk(p, pc_arr, nr_cores);
219 spin_unlock(&sched_lock);
220 /* Drop the cradle-to-the-grave reference, jet-li */
224 /* ksched callbacks. p just woke up and is UNLOCKED. */
225 void __sched_mcp_wakeup(struct proc *p)
227 spin_lock(&sched_lock);
228 if (proc_is_dying(p)) {
229 spin_unlock(&sched_lock);
232 /* could try and prioritize p somehow (move it to the front of the list). */
233 spin_unlock(&sched_lock);
234 /* note they could be dying at this point too. */
235 poke(&ksched_poker, p);
238 /* ksched callbacks. p just woke up and is UNLOCKED. */
239 void __sched_scp_wakeup(struct proc *p)
241 spin_lock(&sched_lock);
242 if (proc_is_dying(p)) {
243 spin_unlock(&sched_lock);
246 /* might not be on a list if it is new. o/w, it should be unrunnable */
247 remove_from_any_list(p);
248 add_to_list(p, &runnable_scps);
249 spin_unlock(&sched_lock);
250 /* we could be on a CG core, and all the mgmt cores could be halted. if we
251 * don't tell one of them about the new proc, they will sleep until the
252 * timer tick goes off. */
253 if (!management_core()) {
254 /* TODO: pick a better core and only send if halted.
256 * ideally, we'd know if a specific mgmt core is sleeping and wake it
257 * up. o/w, we could interrupt an already-running mgmt core that won't
258 * get to our new proc anytime soon. also, by poking core 0, a
259 * different mgmt core could remain idle (and this process would sleep)
260 * until its tick goes off */
261 send_ipi(0, I_POKE_CORE);
265 /* Callback to return a core to the ksched, which tracks it as idle and
266 * deallocated from p. The proclock is held (__core_req depends on that).
268 * This also is a trigger, telling us we have more cores. We could/should make
269 * a scheduling decision (or at least plan to). */
270 void __sched_put_idle_core(struct proc *p, uint32_t coreid)
272 spin_lock(&sched_lock);
273 __track_core_dealloc(p, coreid);
274 spin_unlock(&sched_lock);
277 /* Callback, bulk interface for put_idle. The proclock is held for this. */
278 void __sched_put_idle_cores(struct proc *p, uint32_t *pc_arr, uint32_t num)
280 spin_lock(&sched_lock);
281 __track_core_dealloc_bulk(p, pc_arr, num);
282 spin_unlock(&sched_lock);
283 /* could trigger a sched decision here */
286 /* mgmt/LL cores should call this to schedule the calling core and give it to an
287 * SCP. will also prune the dead SCPs from the list. hold the lock before
288 * calling. returns TRUE if it scheduled a proc. */
289 static bool __schedule_scp(void)
291 // TODO: sort out lock ordering (proc_run_s also locks)
293 uint32_t pcoreid = core_id();
294 struct per_cpu_info *pcpui = &per_cpu_info[pcoreid];
295 /* if there are any runnables, run them here and put any currently running
296 * SCP on the tail of the runnable queue. */
297 if ((p = TAILQ_FIRST(&runnable_scps))) {
298 /* someone is currently running, dequeue them */
299 if (pcpui->owning_proc) {
300 spin_lock(&pcpui->owning_proc->proc_lock);
301 /* process might be dying, with a KMSG to clean it up waiting on
302 * this core. can't do much, so we'll attempt to restart */
303 if (proc_is_dying(pcpui->owning_proc)) {
304 send_kernel_message(core_id(), __just_sched, 0, 0, 0,
306 spin_unlock(&pcpui->owning_proc->proc_lock);
309 printd("Descheduled %d in favor of %d\n", pcpui->owning_proc->pid,
311 __proc_set_state(pcpui->owning_proc, PROC_RUNNABLE_S);
312 /* Saving FP state aggressively. Odds are, the SCP was hit by an
313 * IRQ and has a HW ctx, in which case we must save. */
314 __proc_save_fpu_s(pcpui->owning_proc);
315 __proc_save_context_s(pcpui->owning_proc);
316 vcore_account_offline(pcpui->owning_proc, 0);
317 __seq_start_write(&p->procinfo->coremap_seqctr);
319 __seq_end_write(&p->procinfo->coremap_seqctr);
320 spin_unlock(&pcpui->owning_proc->proc_lock);
321 /* round-robin the SCPs (inserts at the end of the queue) */
322 switch_lists(pcpui->owning_proc, &unrunnable_scps, &runnable_scps);
323 clear_owning_proc(pcoreid);
324 /* Note we abandon core. It's not strictly necessary. If
325 * we didn't, the TLB would still be loaded with the old
326 * one, til we proc_run_s, and the various paths in
327 * proc_run_s would pick it up. This way is a bit safer for
328 * future changes, but has an extra (empty) TLB flush. */
331 /* Run the new proc */
332 switch_lists(p, &runnable_scps, &unrunnable_scps);
333 printd("PID of the SCP i'm running: %d\n", p->pid);
334 proc_run_s(p); /* gives it core we're running on */
340 /* Returns how many new cores p needs. This doesn't lock the proc, so your
341 * answer might be stale. */
342 static uint32_t get_cores_needed(struct proc *p)
344 uint32_t amt_wanted, amt_granted;
345 amt_wanted = p->procdata->res_req[RES_CORES].amt_wanted;
346 /* Help them out - if they ask for something impossible, give them 1 so they
347 * can make some progress. (this is racy, and unnecessary). */
348 if (amt_wanted > p->procinfo->max_vcores) {
349 printk("[kernel] proc %d wanted more than max, wanted %d\n", p->pid,
351 p->procdata->res_req[RES_CORES].amt_wanted = 1;
354 /* There are a few cases where amt_wanted is 0, but they are still RUNNABLE
355 * (involving yields, events, and preemptions). In these cases, give them
356 * at least 1, so they can make progress and yield properly. If they are
357 * not WAITING, they did not yield and may have missed a message. */
359 /* could ++, but there could be a race and we don't want to give them
360 * more than they ever asked for (in case they haven't prepped) */
361 p->procdata->res_req[RES_CORES].amt_wanted = 1;
364 /* amt_granted is racy - they could be *yielding*, but currently they can't
365 * be getting any new cores if the caller is in the mcp_ksched. this is
366 * okay - we won't accidentally give them more cores than they *ever* wanted
367 * (which could crash them), but our answer might be a little stale. */
368 amt_granted = p->procinfo->res_grant[RES_CORES];
369 /* Do not do an assert like this: it could fail (yield in progress): */
370 //assert(amt_granted == p->procinfo->num_vcores);
371 if (amt_wanted <= amt_granted)
373 return amt_wanted - amt_granted;
376 /* Actual work of the MCP kscheduler. if we were called by poke_ksched, *arg
377 * might be the process who wanted special service. this would be the case if
378 * we weren't already running the ksched. Sort of a ghetto way to "post work",
379 * such that it's an optimization. */
380 static void __run_mcp_ksched(void *arg)
382 struct proc *p, *temp;
384 struct proc_list *temp_mcp_list;
385 /* locking to protect the MCP lists' integrity and membership */
386 spin_lock(&sched_lock);
387 /* 2-pass scheme: check each proc on the primary list (FCFS). if they need
388 * nothing, put them on the secondary list. if they need something, rip
389 * them off the list, service them, and if they are still not dying, put
390 * them on the secondary list. We cull the entire primary list, so that
391 * when we start from the beginning each time, we aren't repeatedly checking
392 * procs we looked at on previous waves.
394 * TODO: we could modify this such that procs that we failed to service move
395 * to yet another list or something. We can also move the WAITINGs to
396 * another list and have wakeup move them back, etc. */
397 while (!TAILQ_EMPTY(primary_mcps)) {
398 TAILQ_FOREACH_SAFE(p, primary_mcps, ksched_data.proc_link, temp) {
399 if (p->state == PROC_WAITING) { /* unlocked peek at the state */
400 switch_lists(p, primary_mcps, secondary_mcps);
403 amt_needed = get_cores_needed(p);
405 switch_lists(p, primary_mcps, secondary_mcps);
408 /* o/w, we want to give cores to this proc */
409 remove_from_list(p, primary_mcps);
410 /* now it won't die, but it could get removed from lists and have
411 * its stuff unprov'd when we unlock */
413 /* GIANT WARNING: __core_req will unlock the sched lock for a bit.
414 * It will return with it locked still. We could unlock before we
415 * pass in, but they will relock right away. */
416 // notionally_unlock(&ksched_lock); /* for mouse-eyed viewers */
417 __core_request(p, amt_needed);
418 // notionally_lock(&ksched_lock);
419 /* Peeking at the state is okay, since we hold a ref. Once it is
420 * DYING, it'll remain DYING until we decref. And if there is a
421 * concurrent death, that will spin on the ksched lock (which we
422 * hold, and which protects the proc lists). */
423 if (!proc_is_dying(p))
424 add_to_list(p, secondary_mcps);
425 proc_decref(p); /* fyi, this may trigger __proc_free */
426 /* need to break: the proc lists may have changed when we unlocked
427 * in core_req in ways that the FOREACH_SAFE can't handle. */
431 /* at this point, we moved all the procs over to the secondary list, and
432 * attempted to service the ones that wanted something. now just swap the
433 * lists for the next invocation of the ksched. */
434 temp_mcp_list = primary_mcps;
435 primary_mcps = secondary_mcps;
436 secondary_mcps = temp_mcp_list;
437 spin_unlock(&sched_lock);
440 /* Something has changed, and for whatever reason the scheduler should
443 * Don't call this if you are processing a syscall or otherwise care about your
444 * kthread variables, cur_proc/owning_proc, etc.
446 * Don't call this from interrupt context (grabs proclocks). */
447 void run_scheduler(void)
449 /* MCP scheduling: post work, then poke. for now, i just want the func to
450 * run again, so merely a poke is sufficient. */
451 poke(&ksched_poker, 0);
452 if (management_core()) {
453 spin_lock(&sched_lock);
455 spin_unlock(&sched_lock);
459 /* A process is asking the ksched to look at its resource desires. The
460 * scheduler is free to ignore this, for its own reasons, so long as it
461 * eventually gets around to looking at resource desires. */
462 void poke_ksched(struct proc *p, unsigned int res_type)
464 /* ignoring res_type for now. could post that if we wanted (would need some
465 * other structs/flags) */
466 if (!__proc_is_mcp(p))
468 poke(&ksched_poker, p);
471 /* The calling cpu/core has nothing to do and plans to idle/halt. This is an
472 * opportunity to pick the nature of that halting (low power state, etc), or
473 * provide some other work (_Ss on LL cores). Note that interrupts are
474 * disabled, and if you return, the core will cpu_halt(). */
477 bool new_proc = FALSE;
478 if (!management_core())
480 spin_lock(&sched_lock);
481 new_proc = __schedule_scp();
482 spin_unlock(&sched_lock);
483 /* if we just scheduled a proc, we need to manually restart it, instead of
484 * returning. if we return, the core will halt. */
489 /* Could drop into the monitor if there are no processes at all. For now,
490 * the 'call of the giraffe' suffices. */
493 /* Available resources changed (plus or minus). Some parts of the kernel may
494 * call this if a particular resource that is 'quantity-based' changes. Things
495 * like available RAM to processes, bandwidth, etc. Cores would probably be
496 * inappropriate, since we need to know which specific core is now free. */
497 void avail_res_changed(int res_type, long change)
499 printk("[kernel] ksched doesn't track any resources yet!\n");
502 /* This deals with a request for more cores. The amt of new cores needed is
503 * passed in. The ksched lock is held, but we are free to unlock if we want
504 * (and we must, if calling out of the ksched to anything high-level).
506 * Side note: if we want to warn, then we can't deal with this proc's prov'd
507 * cores until we wait til the alarm goes off. would need to put all
508 * alarmed cores on a list and wait til the alarm goes off to do the full
509 * preempt. and when those cores come in voluntarily, we'd need to know to
510 * give them to this proc. */
511 static void __core_request(struct proc *p, uint32_t amt_needed)
513 uint32_t nr_to_grant = 0;
514 uint32_t corelist[num_cores];
516 struct proc *proc_to_preempt;
518 /* we come in holding the ksched lock, and we hold it here to protect
519 * allocations and provisioning. */
520 /* get all available cores from their prov_not_alloc list. the list might
521 * change when we unlock (new cores added to it, or the entire list emptied,
522 * but no core allocations will happen (we hold the poke)). */
523 while (nr_to_grant != amt_needed) {
524 /* Find the next best core to allocate to p. It may be a core
525 * provisioned to p, and it might not be. */
526 pcoreid = __find_best_core_to_alloc(p);
527 /* If no core is returned, we know that there are no more cores to give
528 * out, so we exit the loop. */
531 /* If the pcore chosen currently has a proc allocated to it, we know
532 * it must be provisioned to p, but not allocated to it. We need to try
533 * to preempt. After this block, the core will be track_dealloc'd and
534 * on the idle list (regardless of whether we had to preempt or not) */
535 if (get_alloc_proc(pcoreid)) {
536 proc_to_preempt = get_alloc_proc(pcoreid);
537 /* would break both preemption and maybe the later decref */
538 assert(proc_to_preempt != p);
539 /* need to keep a valid, external ref when we unlock */
540 proc_incref(proc_to_preempt, 1);
541 spin_unlock(&sched_lock);
542 /* sending no warning time for now - just an immediate preempt. */
543 success = proc_preempt_core(proc_to_preempt, pcoreid, 0);
544 /* reaquire locks to protect provisioning and idle lists */
545 spin_lock(&sched_lock);
547 /* we preempted it before the proc could yield or die.
548 * alloc_proc should not have changed (it'll change in death and
549 * idle CBs). the core is not on the idle core list. (if we
550 * ever have proc alloc lists, it'll still be on the old proc's
552 assert(get_alloc_proc(pcoreid));
553 /* regardless of whether or not it is still prov to p, we need
554 * to note its dealloc. we are doing some excessive checking of
555 * p == prov_proc, but using this helper is a lot clearer. */
556 __track_core_dealloc(proc_to_preempt, pcoreid);
558 /* the preempt failed, which should only happen if the pcore was
559 * unmapped (could be dying, could be yielding, but NOT
560 * preempted). whoever unmapped it also triggered (or will soon
561 * trigger) a track_core_dealloc and put it on the idle list.
562 * Our signal for this is get_alloc_proc() being 0. We need to
563 * spin and let whoever is trying to free the core grab the
564 * ksched lock. We could use an 'ignore_next_idle' flag per
565 * sched_pcore, but it's not critical anymore.
567 * Note, we're relying on us being the only preemptor - if the
568 * core was unmapped by *another* preemptor, there would be no
569 * way of knowing the core was made idle *yet* (the success
570 * branch in another thread). likewise, if there were another
571 * allocator, the pcore could have been put on the idle list and
572 * then quickly removed/allocated. */
574 while (get_alloc_proc(pcoreid)) {
575 /* this loop should be very rare */
576 spin_unlock(&sched_lock);
578 spin_lock(&sched_lock);
581 /* no longer need to keep p_to_pre alive */
582 proc_decref(proc_to_preempt);
583 /* might not be prov to p anymore (rare race). pcoreid is idle - we
584 * might get it later, or maybe we'll give it to its rightful proc*/
585 if (get_prov_proc(pcoreid) != p)
588 /* At this point, the pcore is idle, regardless of how we got here
589 * (successful preempt, failed preempt, or it was idle in the first
590 * place). We also know the core is still provisioned to us. Lets add
591 * it to the corelist for p (so we can give it to p in bulk later), and
592 * track its allocation with p (so our internal data structures stay in
593 * sync). We rely on the fact that we are the only allocator (pcoreid is
594 * still idle, despite (potentially) unlocking during the preempt
595 * attempt above). It is guaranteed to be track_dealloc'd()
596 * (regardless of how we got here). */
597 corelist[nr_to_grant] = pcoreid;
599 __track_core_alloc(p, pcoreid);
601 /* Now, actually give them out */
603 /* Need to unlock before calling out to proc code. We are somewhat
604 * relying on being the only one allocating 'thread' here, since another
605 * allocator could have seen these cores (if they are prov to some proc)
606 * and could be trying to give them out (and assuming they are already
607 * on the idle list). */
608 spin_unlock(&sched_lock);
609 /* give them the cores. this will start up the extras if RUNNING_M. */
610 spin_lock(&p->proc_lock);
611 /* if they fail, it is because they are WAITING or DYING. we could give
612 * the cores to another proc or whatever. for the current type of
613 * ksched, we'll just put them back on the pile and return. Note, the
614 * ksched could check the states after locking, but it isn't necessary:
615 * just need to check at some point in the ksched loop. */
616 if (__proc_give_cores(p, corelist, nr_to_grant)) {
617 spin_unlock(&p->proc_lock);
618 /* we failed, put the cores and track their dealloc. lock is
619 * protecting those structures. */
620 spin_lock(&sched_lock);
621 __track_core_dealloc_bulk(p, corelist, nr_to_grant);
623 /* at some point after giving cores, call proc_run_m() (harmless on
624 * RUNNING_Ms). You can give small groups of cores, then run them
625 * (which is more efficient than interleaving runs with the gives
626 * for bulk preempted processes). */
628 spin_unlock(&p->proc_lock);
629 /* main mcp_ksched wants this held (it came to __core_req held) */
630 spin_lock(&sched_lock);
633 /* note the ksched lock is still held */
636 /* Provision a core to a process. This function wraps the primary logic
637 * implemented in __provision_core, with a lock, error checking, etc. */
638 int provision_core(struct proc *p, uint32_t pcoreid)
640 /* Make sure we aren't asking for something that doesn't exist (bounds check
641 * on the pcore array) */
642 if (!(pcoreid < num_cores)) {
646 /* Don't allow the provisioning of LL cores */
647 if (is_ll_core(pcoreid)) {
651 /* Note the sched lock protects the tailqs for all procs in this code.
652 * If we need a finer grained sched lock, this is one place where we could
653 * have a different lock */
654 spin_lock(&sched_lock);
655 __provision_core(p, pcoreid);
656 spin_unlock(&sched_lock);
660 /************** Debugging **************/
661 void sched_diag(void)
664 spin_lock(&sched_lock);
665 TAILQ_FOREACH(p, &runnable_scps, ksched_data.proc_link)
666 printk("Runnable _S PID: %d\n", p->pid);
667 TAILQ_FOREACH(p, &unrunnable_scps, ksched_data.proc_link)
668 printk("Unrunnable _S PID: %d\n", p->pid);
669 TAILQ_FOREACH(p, primary_mcps, ksched_data.proc_link)
670 printk("Primary MCP PID: %d\n", p->pid);
671 TAILQ_FOREACH(p, secondary_mcps, ksched_data.proc_link)
672 printk("Secondary MCP PID: %d\n", p->pid);
673 spin_unlock(&sched_lock);
677 void print_resources(struct proc *p)
679 printk("--------------------\n");
680 printk("PID: %d\n", p->pid);
681 printk("--------------------\n");
682 for (int i = 0; i < MAX_NUM_RESOURCES; i++)
683 printk("Res type: %02d, amt wanted: %08d, amt granted: %08d\n", i,
684 p->procdata->res_req[i].amt_wanted, p->procinfo->res_grant[i]);
687 void print_all_resources(void)
690 void __print_resources(void *item, void *opaque)
692 print_resources((struct proc*)item);
694 spin_lock(&pid_hash_lock);
695 hash_for_each(pid_hash, __print_resources, NULL);
696 spin_unlock(&pid_hash_lock);
699 void next_core_to_alloc(uint32_t pcoreid)
701 spin_lock(&sched_lock);
702 __next_core_to_alloc(pcoreid);
703 spin_unlock(&sched_lock);
706 void sort_idle_cores(void)
708 spin_lock(&sched_lock);
710 spin_unlock(&sched_lock);