static void cpudl_heapify_up(struct cpudl *cp, int idx) { int p; int orig_cpu = cp->elements[idx].cpu; u64 orig_dl = cp->elements[idx].dl; if (idx == 0) return; do { p = parent(idx); if (dl_time_before(orig_dl, cp->elements[p].dl)) break; /* pull parent onto idx */ cp->elements[idx].cpu = cp->elements[p].cpu; cp->elements[idx].dl = cp->elements[p].dl; cp->elements[cp->elements[idx].cpu].idx = idx; idx = p; } while (idx != 0); /* actual push up of saved original values orig_* */ cp->elements[idx].cpu = orig_cpu; cp->elements[idx].dl = orig_dl; cp->elements[cp->elements[idx].cpu].idx = idx; }
static void cpudl_change_key(struct cpudl *cp, int idx, u64 new_dl) { WARN_ON(idx == IDX_INVALID || !cpu_present(idx)); if (dl_time_before(new_dl, cp->elements[idx].dl)) { cp->elements[idx].dl = new_dl; cpudl_heapify(cp, idx); } else { cp->elements[idx].dl = new_dl; while (idx > 0 && dl_time_before(cp->elements[parent(idx)].dl, cp->elements[idx].dl)) { cpudl_exchange(cp, idx, parent(idx)); idx = parent(idx); } } }
static void cpudl_heapify(struct cpudl *cp, int idx) { if (idx > 0 && dl_time_before(cp->elements[parent(idx)].dl, cp->elements[idx].dl)) cpudl_heapify_up(cp, idx); else cpudl_heapify_down(cp, idx); }
static void cpudl_heapify_down(struct cpudl *cp, int idx) { int l, r, largest; int orig_cpu = cp->elements[idx].cpu; u64 orig_dl = cp->elements[idx].dl; if (left_child(idx) >= cp->size) return; /* adapted from lib/prio_heap.c */ while(1) { u64 largest_dl; l = left_child(idx); r = right_child(idx); largest = idx; largest_dl = orig_dl; if ((l < cp->size) && dl_time_before(orig_dl, cp->elements[l].dl)) { largest = l; largest_dl = cp->elements[l].dl; } if ((r < cp->size) && dl_time_before(largest_dl, cp->elements[r].dl)) largest = r; if (largest == idx) break; /* pull largest child onto idx */ cp->elements[idx].cpu = cp->elements[largest].cpu; cp->elements[idx].dl = cp->elements[largest].dl; cp->elements[cp->elements[idx].cpu].idx = idx; idx = largest; } /* actual push down of saved original values orig_* */ cp->elements[idx].cpu = orig_cpu; cp->elements[idx].dl = orig_dl; cp->elements[cp->elements[idx].cpu].idx = idx; }
void dline_insert() { dline_t deadline = ask_deadline(); int p = heap_get_max_proc(&myheap); if (dl_time_before(heap_get_max_dline(&myheap), deadline)) printf("Too large!\n"); else { heap_preempt(&myheap, p, deadline); heap_print(&myheap); if (!heap_check(&myheap)) exit(-1); } }
/* * cpudl_set - update the cpudl max-heap * @cp: the cpudl max-heap context * @cpu: the target cpu * @dl: the new earliest deadline for this cpu * * Notes: assumes cpu_rq(cpu)->lock is locked * * Returns: (void) */ void cpudl_set(struct cpudl *cp, int cpu, u64 dl, int is_valid) { int old_idx, new_cpu; unsigned long flags; WARN_ON(!cpu_present(cpu)); raw_spin_lock_irqsave(&cp->lock, flags); old_idx = cp->cpu_to_idx[cpu]; if (!is_valid) { /* remove item */ if (old_idx == IDX_INVALID) { /* * Nothing to remove if old_idx was invalid. * This could happen if a rq_offline_dl is * called for a CPU without -dl tasks running. */ goto out; } new_cpu = cp->elements[cp->size - 1].cpu; cp->elements[old_idx].dl = cp->elements[cp->size - 1].dl; cp->elements[old_idx].cpu = new_cpu; cp->size--; cp->cpu_to_idx[new_cpu] = old_idx; cp->cpu_to_idx[cpu] = IDX_INVALID; while (old_idx > 0 && dl_time_before( cp->elements[parent(old_idx)].dl, cp->elements[old_idx].dl)) { cpudl_exchange(cp, old_idx, parent(old_idx)); old_idx = parent(old_idx); } cpumask_set_cpu(cpu, cp->free_cpus); cpudl_heapify(cp, old_idx); goto out; } if (old_idx == IDX_INVALID) { cp->size++; cp->elements[cp->size - 1].dl = 0; cp->elements[cp->size - 1].cpu = cpu; cp->cpu_to_idx[cpu] = cp->size - 1; cpudl_change_key(cp, cp->size - 1, dl); cpumask_clear_cpu(cpu, cp->free_cpus); } else { cpudl_change_key(cp, old_idx, dl); } out: raw_spin_unlock_irqrestore(&cp->lock, flags); }
static void cpudl_heapify(struct cpudl *cp, int idx) { int l, r, largest; /* adapted from lib/prio_heap.c */ while(1) { l = left_child(idx); r = right_child(idx); largest = idx; if ((l < cp->size) && dl_time_before(cp->elements[idx].dl, cp->elements[l].dl)) largest = l; if ((r < cp->size) && dl_time_before(cp->elements[largest].dl, cp->elements[r].dl)) largest = r; if (largest == idx) break; /* Push idx down the heap one level and bump one up */ cpudl_exchange(cp, largest, idx); idx = largest; } }
void dline_update() { printf("Select a processor (0-7): "); int proc; scanf("%d", &proc); if (proc >7 || proc <0) printf("Wrong processor!\n"); else { dline_t deadline = ask_deadline(); if (dl_time_before(deadline, myheap.nodes[proc].deadline)) printf("deadline too short\n"); else { heap_finish(&myheap, proc, deadline); heap_print(&myheap); if (!heap_check(&myheap)) exit(-1); } } }
/* * cpudl_find - find the best (later-dl) CPU in the system * @cp: the cpudl max-heap context * @p: the task * @later_mask: a mask to fill in with the selected CPUs (or NULL) * * Returns: int - best CPU (heap maximum if suitable) */ int cpudl_find(struct cpudl *cp, struct task_struct *p, struct cpumask *later_mask) { int best_cpu = -1; const struct sched_dl_entity *dl_se = &p->dl; if (later_mask && cpumask_and(later_mask, later_mask, cp->free_cpus)) { best_cpu = cpumask_any(later_mask); goto out; } else if (cpumask_test_cpu(cpudl_maximum(cp), &p->cpus_allowed) && dl_time_before(dl_se->deadline, cp->elements[0].dl)) { best_cpu = cpudl_maximum(cp); if (later_mask) cpumask_set_cpu(best_cpu, later_mask); } out: WARN_ON(best_cpu != -1 && !cpu_present(best_cpu)); return best_cpu; }