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>
20 /* Process Lists. 'unrunnable' is a holding list for SCPs that are running or
21 * waiting or otherwise not considered for sched decisions. */
22 struct proc_list unrunnable_scps = TAILQ_HEAD_INITIALIZER(unrunnable_scps);
23 struct proc_list runnable_scps = TAILQ_HEAD_INITIALIZER(runnable_scps);
24 /* mcp lists. we actually could get by with one list and a TAILQ_CONCAT, but
25 * I'm expecting to want the flexibility of the pointers later. */
26 struct proc_list all_mcps_1 = TAILQ_HEAD_INITIALIZER(all_mcps_1);
27 struct proc_list all_mcps_2 = TAILQ_HEAD_INITIALIZER(all_mcps_2);
28 struct proc_list *primary_mcps = &all_mcps_1;
29 struct proc_list *secondary_mcps = &all_mcps_2;
31 /* Helper, defined below */
32 static void __core_request(struct proc *p, uint32_t amt_needed);
33 static void add_to_list(struct proc *p, struct proc_list *list);
34 static void remove_from_list(struct proc *p, struct proc_list *list);
35 static void switch_lists(struct proc *p, struct proc_list *old,
36 struct proc_list *new);
37 static void __run_mcp_ksched(void *arg); /* don't call directly */
38 static uint32_t get_cores_needed(struct proc *p);
40 /* Locks / sync tools */
42 /* poke-style ksched - ensures the MCP ksched only runs once at a time. since
43 * only one mcp ksched runs at a time, while this is set, the ksched knows no
44 * cores are being allocated by other code (though they could be dealloc, due to
47 * The main value to this sync method is to make the 'make sure the ksched runs
48 * only once at a time and that it actually runs' invariant/desire wait-free, so
49 * that it can be called anywhere (deep event code, etc).
51 * As the ksched gets smarter, we'll probably embedd this poker in a bigger
52 * struct that can handle the posting of different types of work. */
53 struct poke_tracker ksched_poker = POKE_INITIALIZER(__run_mcp_ksched);
55 /* this 'big ksched lock' protects a bunch of things, which i may make fine
57 /* - protects the integrity of proc tailqs/structures, as well as the membership
58 * of a proc on those lists. proc lifetime within the ksched but outside this
59 * lock is protected by the proc kref. */
60 //spinlock_t proclist_lock = SPINLOCK_INITIALIZER; /* subsumed by bksl */
61 /* - protects the provisioning assignment, and the integrity of all prov
62 * lists (the lists of each proc). */
63 //spinlock_t prov_lock = SPINLOCK_INITIALIZER;
64 /* - protects allocation structures */
65 //spinlock_t alloc_lock = SPINLOCK_INITIALIZER;
66 spinlock_t sched_lock = SPINLOCK_INITIALIZER;
68 /* Alarm struct, for our example 'timer tick' */
69 struct alarm_waiter ksched_waiter;
71 #define TIMER_TICK_USEC 10000 /* 10msec */
73 /* Helper: Sets up a timer tick on the calling core to go off 10 msec from now.
74 * This assumes the calling core is an LL core, etc. */
75 static void set_ksched_alarm(void)
77 set_awaiter_rel(&ksched_waiter, TIMER_TICK_USEC);
78 set_alarm(&per_cpu_info[core_id()].tchain, &ksched_waiter);
81 /* Need a kmsg to just run the sched, but not to rearm */
82 static void __just_sched(uint32_t srcid, long a0, long a1, long a2)
87 /* RKM alarm, to run the scheduler tick (not in interrupt context) and reset the
88 * alarm. Note that interrupts will be disabled, but this is not the same as
89 * interrupt context. We're a routine kmsg, which means the core is in a
91 static void __ksched_tick(struct alarm_waiter *waiter)
93 /* TODO: imagine doing some accounting here */
95 /* Set our alarm to go off, incrementing from our last tick (instead of
96 * setting it relative to now, since some time has passed since the alarm
97 * first went off. Note, this may be now or in the past! */
98 set_awaiter_inc(&ksched_waiter, TIMER_TICK_USEC);
99 set_alarm(&per_cpu_info[core_id()].tchain, &ksched_waiter);
102 void schedule_init(void)
104 spin_lock(&sched_lock);
105 assert(!core_id()); /* want the alarm on core0 for now */
106 init_awaiter(&ksched_waiter, __ksched_tick);
109 spin_unlock(&sched_lock);
111 #ifdef CONFIG_ARSC_SERVER
112 int arsc_coreid = get_any_idle_core();
113 assert(arsc_coreid >= 0);
114 send_kernel_message(arsc_coreid, arsc_server, 0, 0, 0, KMSG_ROUTINE);
115 printk("Using core %d for the ARSC server\n", arsc_coreid);
116 #endif /* CONFIG_ARSC_SERVER */
119 /* Round-robins on whatever list it's on */
120 static void add_to_list(struct proc *p, struct proc_list *new)
122 assert(!(p->ksched_data.cur_list));
123 TAILQ_INSERT_TAIL(new, p, ksched_data.proc_link);
124 p->ksched_data.cur_list = new;
127 static void remove_from_list(struct proc *p, struct proc_list *old)
129 assert(p->ksched_data.cur_list == old);
130 TAILQ_REMOVE(old, p, ksched_data.proc_link);
131 p->ksched_data.cur_list = 0;
134 static void switch_lists(struct proc *p, struct proc_list *old,
135 struct proc_list *new)
137 remove_from_list(p, old);
141 /* Removes from whatever list p is on */
142 static void remove_from_any_list(struct proc *p)
144 if (p->ksched_data.cur_list) {
145 TAILQ_REMOVE(p->ksched_data.cur_list, p, ksched_data.proc_link);
146 p->ksched_data.cur_list = 0;
150 /************** Process Management Callbacks **************/
152 * - the proc lock is NOT held for any of these calls. currently, there is no
153 * lock ordering between the sched lock and the proc lock. since the proc
154 * code doesn't know what we do, it doesn't hold its lock when calling our
156 * - since the proc lock isn't held, the proc could be dying, which means we
157 * will receive a __sched_proc_destroy() either before or after some of these
158 * other CBs. the CBs related to list management need to check and abort if
160 void __sched_proc_register(struct proc *p)
162 assert(p->state != PROC_DYING); /* shouldn't be abel to happen yet */
163 /* one ref for the proc's existence, cradle-to-grave */
164 proc_incref(p, 1); /* need at least this OR the 'one for existing' */
165 spin_lock(&sched_lock);
166 corealloc_proc_init(p);
167 add_to_list(p, &unrunnable_scps);
168 spin_unlock(&sched_lock);
171 /* Returns 0 if it succeeded, an error code otherwise. */
172 void __sched_proc_change_to_m(struct proc *p)
174 spin_lock(&sched_lock);
175 /* Need to make sure they aren't dying. if so, we already dealt with their
176 * list membership, etc (or soon will). taking advantage of the 'immutable
177 * state' of dying (so long as refs are held). */
178 if (p->state == PROC_DYING) {
179 spin_unlock(&sched_lock);
182 /* Catch user bugs */
183 if (!p->procdata->res_req[RES_CORES].amt_wanted) {
184 printk("[kernel] process needs to specify amt_wanted\n");
185 p->procdata->res_req[RES_CORES].amt_wanted = 1;
187 /* For now, this should only ever be called on an unrunnable. It's
188 * probably a bug, at this stage in development, to do o/w. */
189 remove_from_list(p, &unrunnable_scps);
190 //remove_from_any_list(p); /* ^^ instead of this */
191 add_to_list(p, primary_mcps);
192 spin_unlock(&sched_lock);
193 //poke_ksched(p, RES_CORES);
196 /* Sched callback called when the proc dies. pc_arr holds the cores the proc
197 * had, if any, and nr_cores tells us how many are in the array.
199 * An external, edible ref is passed in. when we return and they decref,
200 * __proc_free will be called (when the last one is done). */
201 void __sched_proc_destroy(struct proc *p, uint32_t *pc_arr, uint32_t nr_cores)
203 spin_lock(&sched_lock);
204 /* Unprovision any cores. Note this is different than track_core_dealloc.
205 * The latter does bookkeeping when an allocation changes. This is a
206 * bulk *provisioning* change. */
207 __unprovision_all_cores(p);
208 /* Remove from whatever list we are on (if any - might not be on one if it
209 * was in the middle of __run_mcp_sched) */
210 remove_from_any_list(p);
212 __track_core_dealloc_bulk(p, pc_arr, nr_cores);
213 spin_unlock(&sched_lock);
214 /* Drop the cradle-to-the-grave reference, jet-li */
218 /* ksched callbacks. p just woke up and is UNLOCKED. */
219 void __sched_mcp_wakeup(struct proc *p)
221 spin_lock(&sched_lock);
222 if (p->state == PROC_DYING) {
223 spin_unlock(&sched_lock);
226 /* could try and prioritize p somehow (move it to the front of the list). */
227 spin_unlock(&sched_lock);
228 /* note they could be dying at this point too. */
229 poke(&ksched_poker, p);
232 /* ksched callbacks. p just woke up and is UNLOCKED. */
233 void __sched_scp_wakeup(struct proc *p)
235 spin_lock(&sched_lock);
236 if (p->state == PROC_DYING) {
237 spin_unlock(&sched_lock);
240 /* might not be on a list if it is new. o/w, it should be unrunnable */
241 remove_from_any_list(p);
242 add_to_list(p, &runnable_scps);
243 spin_unlock(&sched_lock);
244 /* we could be on a CG core, and all the mgmt cores could be halted. if we
245 * don't tell one of them about the new proc, they will sleep until the
246 * timer tick goes off. */
247 if (!management_core()) {
248 /* TODO: pick a better core and only send if halted.
250 * FYI, a POKE on x86 might lose a rare race with halt code, since the
251 * poke handler does not abort halts. if this happens, the next timer
252 * IRQ would wake up the core.
254 * ideally, we'd know if a specific mgmt core is sleeping and wake it
255 * up. o/w, we could interrupt an already-running mgmt core that won't
256 * get to our new proc anytime soon. also, by poking core 0, a
257 * different mgmt core could remain idle (and this process would sleep)
258 * until its tick goes off */
259 send_ipi(0, I_POKE_CORE);
263 /* Callback to return a core to the ksched, which tracks it as idle and
264 * deallocated from p. The proclock is held (__core_req depends on that).
266 * This also is a trigger, telling us we have more cores. We could/should make
267 * a scheduling decision (or at least plan to). */
268 void __sched_put_idle_core(struct proc *p, uint32_t coreid)
270 spin_lock(&sched_lock);
271 __track_core_dealloc(p, coreid);
272 spin_unlock(&sched_lock);
275 /* Callback, bulk interface for put_idle. The proclock is held for this. */
276 void __sched_put_idle_cores(struct proc *p, uint32_t *pc_arr, uint32_t num)
278 spin_lock(&sched_lock);
279 __track_core_dealloc_bulk(p, pc_arr, num);
280 spin_unlock(&sched_lock);
281 /* could trigger a sched decision here */
284 /* mgmt/LL cores should call this to schedule the calling core and give it to an
285 * SCP. will also prune the dead SCPs from the list. hold the lock before
286 * calling. returns TRUE if it scheduled a proc. */
287 static bool __schedule_scp(void)
289 // TODO: sort out lock ordering (proc_run_s also locks)
291 uint32_t pcoreid = core_id();
292 struct per_cpu_info *pcpui = &per_cpu_info[pcoreid];
293 /* if there are any runnables, run them here and put any currently running
294 * SCP on the tail of the runnable queue. */
295 if ((p = TAILQ_FIRST(&runnable_scps))) {
296 /* someone is currently running, dequeue them */
297 if (pcpui->owning_proc) {
298 spin_lock(&pcpui->owning_proc->proc_lock);
299 /* process might be dying, with a KMSG to clean it up waiting on
300 * this core. can't do much, so we'll attempt to restart */
301 if (pcpui->owning_proc->state == PROC_DYING) {
302 send_kernel_message(core_id(), __just_sched, 0, 0, 0,
304 spin_unlock(&pcpui->owning_proc->proc_lock);
307 printd("Descheduled %d in favor of %d\n", pcpui->owning_proc->pid,
309 __proc_set_state(pcpui->owning_proc, PROC_RUNNABLE_S);
310 /* Saving FP state aggressively. Odds are, the SCP was hit by an
311 * IRQ and has a HW ctx, in which case we must save. */
312 __proc_save_fpu_s(pcpui->owning_proc);
313 __proc_save_context_s(pcpui->owning_proc);
314 vcore_account_offline(pcpui->owning_proc, 0);
315 __seq_start_write(&p->procinfo->coremap_seqctr);
317 __seq_end_write(&p->procinfo->coremap_seqctr);
318 spin_unlock(&pcpui->owning_proc->proc_lock);
319 /* round-robin the SCPs (inserts at the end of the queue) */
320 switch_lists(pcpui->owning_proc, &unrunnable_scps, &runnable_scps);
321 clear_owning_proc(pcoreid);
322 /* Note we abandon core. It's not strictly necessary. If
323 * we didn't, the TLB would still be loaded with the old
324 * one, til we proc_run_s, and the various paths in
325 * proc_run_s would pick it up. This way is a bit safer for
326 * future changes, but has an extra (empty) TLB flush. */
329 /* Run the new proc */
330 switch_lists(p, &runnable_scps, &unrunnable_scps);
331 printd("PID of the SCP i'm running: %d\n", p->pid);
332 proc_run_s(p); /* gives it core we're running on */
338 /* Returns how many new cores p needs. This doesn't lock the proc, so your
339 * answer might be stale. */
340 static uint32_t get_cores_needed(struct proc *p)
342 uint32_t amt_wanted, amt_granted;
343 amt_wanted = p->procdata->res_req[RES_CORES].amt_wanted;
344 /* Help them out - if they ask for something impossible, give them 1 so they
345 * can make some progress. (this is racy, and unnecessary). */
346 if (amt_wanted > p->procinfo->max_vcores) {
347 printk("[kernel] proc %d wanted more than max, wanted %d\n", p->pid,
349 p->procdata->res_req[RES_CORES].amt_wanted = 1;
352 /* There are a few cases where amt_wanted is 0, but they are still RUNNABLE
353 * (involving yields, events, and preemptions). In these cases, give them
354 * at least 1, so they can make progress and yield properly. If they are
355 * not WAITING, they did not yield and may have missed a message. */
357 /* could ++, but there could be a race and we don't want to give them
358 * more than they ever asked for (in case they haven't prepped) */
359 p->procdata->res_req[RES_CORES].amt_wanted = 1;
362 /* amt_granted is racy - they could be *yielding*, but currently they can't
363 * be getting any new cores if the caller is in the mcp_ksched. this is
364 * okay - we won't accidentally give them more cores than they *ever* wanted
365 * (which could crash them), but our answer might be a little stale. */
366 amt_granted = p->procinfo->res_grant[RES_CORES];
367 /* Do not do an assert like this: it could fail (yield in progress): */
368 //assert(amt_granted == p->procinfo->num_vcores);
369 if (amt_wanted <= amt_granted)
371 return amt_wanted - amt_granted;
374 /* Actual work of the MCP kscheduler. if we were called by poke_ksched, *arg
375 * might be the process who wanted special service. this would be the case if
376 * we weren't already running the ksched. Sort of a ghetto way to "post work",
377 * such that it's an optimization. */
378 static void __run_mcp_ksched(void *arg)
380 struct proc *p, *temp;
382 struct proc_list *temp_mcp_list;
383 /* locking to protect the MCP lists' integrity and membership */
384 spin_lock(&sched_lock);
385 /* 2-pass scheme: check each proc on the primary list (FCFS). if they need
386 * nothing, put them on the secondary list. if they need something, rip
387 * them off the list, service them, and if they are still not dying, put
388 * them on the secondary list. We cull the entire primary list, so that
389 * when we start from the beginning each time, we aren't repeatedly checking
390 * procs we looked at on previous waves.
392 * TODO: we could modify this such that procs that we failed to service move
393 * to yet another list or something. We can also move the WAITINGs to
394 * another list and have wakeup move them back, etc. */
395 while (!TAILQ_EMPTY(primary_mcps)) {
396 TAILQ_FOREACH_SAFE(p, primary_mcps, ksched_data.proc_link, temp) {
397 if (p->state == PROC_WAITING) { /* unlocked peek at the state */
398 switch_lists(p, primary_mcps, secondary_mcps);
401 amt_needed = get_cores_needed(p);
403 switch_lists(p, primary_mcps, secondary_mcps);
406 /* o/w, we want to give cores to this proc */
407 remove_from_list(p, primary_mcps);
408 /* now it won't die, but it could get removed from lists and have
409 * its stuff unprov'd when we unlock */
411 /* GIANT WARNING: __core_req will unlock the sched lock for a bit.
412 * It will return with it locked still. We could unlock before we
413 * pass in, but they will relock right away. */
414 // notionally_unlock(&ksched_lock); /* for mouse-eyed viewers */
415 __core_request(p, amt_needed);
416 // notionally_lock(&ksched_lock);
417 /* Peeking at the state is okay, since we hold a ref. Once it is
418 * DYING, it'll remain DYING until we decref. And if there is a
419 * concurrent death, that will spin on the ksched lock (which we
420 * hold, and which protects the proc lists). */
421 if (p->state != PROC_DYING)
422 add_to_list(p, secondary_mcps);
423 proc_decref(p); /* fyi, this may trigger __proc_free */
424 /* need to break: the proc lists may have changed when we unlocked
425 * in core_req in ways that the FOREACH_SAFE can't handle. */
429 /* at this point, we moved all the procs over to the secondary list, and
430 * attempted to service the ones that wanted something. now just swap the
431 * lists for the next invocation of the ksched. */
432 temp_mcp_list = primary_mcps;
433 primary_mcps = secondary_mcps;
434 secondary_mcps = temp_mcp_list;
435 spin_unlock(&sched_lock);
438 /* Something has changed, and for whatever reason the scheduler should
441 * Don't call this if you are processing a syscall or otherwise care about your
442 * kthread variables, cur_proc/owning_proc, etc.
444 * Don't call this from interrupt context (grabs proclocks). */
445 void run_scheduler(void)
447 /* MCP scheduling: post work, then poke. for now, i just want the func to
448 * run again, so merely a poke is sufficient. */
449 poke(&ksched_poker, 0);
450 if (management_core()) {
451 spin_lock(&sched_lock);
453 spin_unlock(&sched_lock);
457 /* A process is asking the ksched to look at its resource desires. The
458 * scheduler is free to ignore this, for its own reasons, so long as it
459 * eventually gets around to looking at resource desires. */
460 void poke_ksched(struct proc *p, unsigned int res_type)
462 /* ignoring res_type for now. could post that if we wanted (would need some
463 * other structs/flags) */
464 if (!__proc_is_mcp(p))
466 poke(&ksched_poker, p);
469 /* The calling cpu/core has nothing to do and plans to idle/halt. This is an
470 * opportunity to pick the nature of that halting (low power state, etc), or
471 * provide some other work (_Ss on LL cores). Note that interrupts are
472 * disabled, and if you return, the core will cpu_halt(). */
475 bool new_proc = FALSE;
476 if (!management_core())
478 spin_lock(&sched_lock);
479 new_proc = __schedule_scp();
480 spin_unlock(&sched_lock);
481 /* if we just scheduled a proc, we need to manually restart it, instead of
482 * returning. if we return, the core will halt. */
487 /* Could drop into the monitor if there are no processes at all. For now,
488 * the 'call of the giraffe' suffices. */
491 /* Available resources changed (plus or minus). Some parts of the kernel may
492 * call this if a particular resource that is 'quantity-based' changes. Things
493 * like available RAM to processes, bandwidth, etc. Cores would probably be
494 * inappropriate, since we need to know which specific core is now free. */
495 void avail_res_changed(int res_type, long change)
497 printk("[kernel] ksched doesn't track any resources yet!\n");
500 int get_any_idle_core(void)
502 spin_lock(&sched_lock);
503 int ret = __get_any_idle_core();
504 spin_unlock(&sched_lock);
508 int get_specific_idle_core(int coreid)
510 spin_lock(&sched_lock);
511 int ret = __get_specific_idle_core(coreid);
512 spin_unlock(&sched_lock);
516 /* similar to __sched_put_idle_core, but without the prov tracking */
517 void put_idle_core(int coreid)
519 spin_lock(&sched_lock);
520 __put_idle_core(coreid);
521 spin_unlock(&sched_lock);
524 /* This deals with a request for more cores. The amt of new cores needed is
525 * passed in. The ksched lock is held, but we are free to unlock if we want
526 * (and we must, if calling out of the ksched to anything high-level).
528 * Side note: if we want to warn, then we can't deal with this proc's prov'd
529 * cores until we wait til the alarm goes off. would need to put all
530 * alarmed cores on a list and wait til the alarm goes off to do the full
531 * preempt. and when those cores come in voluntarily, we'd need to know to
532 * give them to this proc. */
533 static void __core_request(struct proc *p, uint32_t amt_needed)
535 uint32_t nr_to_grant = 0;
536 uint32_t corelist[num_cores];
538 struct proc *proc_to_preempt;
540 /* we come in holding the ksched lock, and we hold it here to protect
541 * allocations and provisioning. */
542 /* get all available cores from their prov_not_alloc list. the list might
543 * change when we unlock (new cores added to it, or the entire list emptied,
544 * but no core allocations will happen (we hold the poke)). */
545 while (nr_to_grant != amt_needed) {
546 /* Find the next best core to allocate to p. It may be a core
547 * provisioned to p, and it might not be. */
548 pcoreid = __find_best_core_to_alloc(p);
549 /* If no core is returned, we know that there are no more cores to give
550 * out, so we exit the loop. */
553 /* If the pcore chosen currently has a proc allocated to it, we know
554 * it must be provisioned to p, but not allocated to it. We need to try
555 * to preempt. After this block, the core will be track_dealloc'd and
556 * on the idle list (regardless of whether we had to preempt or not) */
557 if (get_alloc_proc(pcoreid)) {
558 proc_to_preempt = get_alloc_proc(pcoreid);
559 /* would break both preemption and maybe the later decref */
560 assert(proc_to_preempt != p);
561 /* need to keep a valid, external ref when we unlock */
562 proc_incref(proc_to_preempt, 1);
563 spin_unlock(&sched_lock);
564 /* sending no warning time for now - just an immediate preempt. */
565 success = proc_preempt_core(proc_to_preempt, pcoreid, 0);
566 /* reaquire locks to protect provisioning and idle lists */
567 spin_lock(&sched_lock);
569 /* we preempted it before the proc could yield or die.
570 * alloc_proc should not have changed (it'll change in death and
571 * idle CBs). the core is not on the idle core list. (if we
572 * ever have proc alloc lists, it'll still be on the old proc's
574 assert(get_alloc_proc(pcoreid));
575 /* regardless of whether or not it is still prov to p, we need
576 * to note its dealloc. we are doing some excessive checking of
577 * p == prov_proc, but using this helper is a lot clearer. */
578 __track_core_dealloc(proc_to_preempt, pcoreid);
580 /* the preempt failed, which should only happen if the pcore was
581 * unmapped (could be dying, could be yielding, but NOT
582 * preempted). whoever unmapped it also triggered (or will soon
583 * trigger) a track_core_dealloc and put it on the idle list.
584 * Our signal for this is get_alloc_proc() being 0. We need to
585 * spin and let whoever is trying to free the core grab the
586 * ksched lock. We could use an 'ignore_next_idle' flag per
587 * sched_pcore, but it's not critical anymore.
589 * Note, we're relying on us being the only preemptor - if the
590 * core was unmapped by *another* preemptor, there would be no
591 * way of knowing the core was made idle *yet* (the success
592 * branch in another thread). likewise, if there were another
593 * allocator, the pcore could have been put on the idle list and
594 * then quickly removed/allocated. */
596 while (get_alloc_proc(pcoreid)) {
597 /* this loop should be very rare */
598 spin_unlock(&sched_lock);
600 spin_lock(&sched_lock);
603 /* no longer need to keep p_to_pre alive */
604 proc_decref(proc_to_preempt);
605 /* might not be prov to p anymore (rare race). pcoreid is idle - we
606 * might get it later, or maybe we'll give it to its rightful proc*/
607 if (get_prov_proc(pcoreid) != p)
610 /* At this point, the pcore is idle, regardless of how we got here
611 * (successful preempt, failed preempt, or it was idle in the first
612 * place). We also know the core is still provisioned to us. Lets add
613 * it to the corelist for p (so we can give it to p in bulk later), and
614 * track its allocation with p (so our internal data structures stay in
615 * sync). We rely on the fact that we are the only allocator (pcoreid is
616 * still idle, despite (potentially) unlocking during the preempt
617 * attempt above). It is guaranteed to be track_dealloc'd()
618 * (regardless of how we got here). */
619 corelist[nr_to_grant] = pcoreid;
621 __track_core_alloc(p, pcoreid);
623 /* Now, actually give them out */
625 /* Need to unlock before calling out to proc code. We are somewhat
626 * relying on being the only one allocating 'thread' here, since another
627 * allocator could have seen these cores (if they are prov to some proc)
628 * and could be trying to give them out (and assuming they are already
629 * on the idle list). */
630 spin_unlock(&sched_lock);
631 /* give them the cores. this will start up the extras if RUNNING_M. */
632 spin_lock(&p->proc_lock);
633 /* if they fail, it is because they are WAITING or DYING. we could give
634 * the cores to another proc or whatever. for the current type of
635 * ksched, we'll just put them back on the pile and return. Note, the
636 * ksched could check the states after locking, but it isn't necessary:
637 * just need to check at some point in the ksched loop. */
638 if (__proc_give_cores(p, corelist, nr_to_grant)) {
639 spin_unlock(&p->proc_lock);
640 /* we failed, put the cores and track their dealloc. lock is
641 * protecting those structures. */
642 spin_lock(&sched_lock);
643 __track_core_dealloc_bulk(p, corelist, nr_to_grant);
645 /* at some point after giving cores, call proc_run_m() (harmless on
646 * RUNNING_Ms). You can give small groups of cores, then run them
647 * (which is more efficient than interleaving runs with the gives
648 * for bulk preempted processes). */
650 spin_unlock(&p->proc_lock);
651 /* main mcp_ksched wants this held (it came to __core_req held) */
652 spin_lock(&sched_lock);
655 /* note the ksched lock is still held */
658 /* Provision a core to a process. This function wraps the primary logic
659 * implemented in __provision_core, with a lock, error checking, etc. */
660 int provision_core(struct proc *p, uint32_t pcoreid)
662 /* Make sure we aren't asking for something that doesn't exist (bounds check
663 * on the pcore array) */
664 if (!(pcoreid < num_cores)) {
668 /* Don't allow the provisioning of LL cores */
669 if (is_ll_core(pcoreid)) {
673 /* Note the sched lock protects the tailqs for all procs in this code.
674 * If we need a finer grained sched lock, this is one place where we could
675 * have a different lock */
676 spin_lock(&sched_lock);
677 __provision_core(p, pcoreid);
678 spin_unlock(&sched_lock);
682 /************** Debugging **************/
683 void sched_diag(void)
686 spin_lock(&sched_lock);
687 TAILQ_FOREACH(p, &runnable_scps, ksched_data.proc_link)
688 printk("Runnable _S PID: %d\n", p->pid);
689 TAILQ_FOREACH(p, &unrunnable_scps, ksched_data.proc_link)
690 printk("Unrunnable _S PID: %d\n", p->pid);
691 TAILQ_FOREACH(p, primary_mcps, ksched_data.proc_link)
692 printk("Primary MCP PID: %d\n", p->pid);
693 TAILQ_FOREACH(p, secondary_mcps, ksched_data.proc_link)
694 printk("Secondary MCP PID: %d\n", p->pid);
695 spin_unlock(&sched_lock);
699 void print_resources(struct proc *p)
701 printk("--------------------\n");
702 printk("PID: %d\n", p->pid);
703 printk("--------------------\n");
704 for (int i = 0; i < MAX_NUM_RESOURCES; i++)
705 printk("Res type: %02d, amt wanted: %08d, amt granted: %08d\n", i,
706 p->procdata->res_req[i].amt_wanted, p->procinfo->res_grant[i]);
709 void print_all_resources(void)
712 void __print_resources(void *item, void *opaque)
714 print_resources((struct proc*)item);
716 spin_lock(&pid_hash_lock);
717 hash_for_each(pid_hash, __print_resources, NULL);
718 spin_unlock(&pid_hash_lock);
721 void next_core_to_alloc(uint32_t pcoreid)
723 spin_lock(&sched_lock);
724 __next_core_to_alloc(pcoreid);
725 spin_unlock(&sched_lock);
728 void sort_idle_cores(void)
730 spin_lock(&sched_lock);
732 spin_unlock(&sched_lock);