Affinity AffinityRestrictions::get_all_cpus() const { Affinity all; foreach(affinities, af) { all.insert(af->begin(), af->end()); }
AffinityTask::TaskSet AffinityTask::generateTaskSet(Computer* computer, Size numTask, RandomDistribution* affinityDistribution, Time minPeriod, Time maxPeriod, RandomDistribution* periodDistribution, Real targetUtilization, RandomDistribution* utilDistribution) { AffinityTask::TaskSet ret; CPUID maxAffinity = computer->getNumCPU(); UniformDistribution selector; Real sum = 0; for(Real util : utilDistribution->distribute(numTask, targetUtilization)) { util = std::min(1.0, util); Real realAffinity = affinityDistribution->nextDistribution(0, maxAffinity); Size currentAffinityCount = ceil(realAffinity); currentAffinityCount = std::min(currentAffinityCount, maxAffinity); currentAffinityCount = std::max(currentAffinityCount, (Size)1); Real realPeriod = periodDistribution->nextDistribution(minPeriod, maxPeriod); Time period = ceil(realPeriod); period = std::max(minPeriod, period); period = std::min(maxPeriod, period); Real realExecution = util * (Real)period; Time execution = floor(realExecution); if(execution == 0) continue; sum+=util; Affinity affinity; while(affinity.size() != currentAffinityCount) { CPUID selected = floor(selector.nextDistribution(0, maxAffinity)); if(selected == maxAffinity) selected = maxAffinity-1; affinity.insert(selected); } AffinityTask* task = new AffinityTask(affinity, computer, period, execution, 0); ret.insert(task); } //printf("Sum of util %f\n", sum); return ret; }
std::set<Affinity> AffinityTask::powerSet(const Affinity& affinity) { assert(affinity.size() < 64); assert(affinity.size() > 0); std::set<Affinity> ret; std::vector<CPUID> listedAffinity; for(CPUID cpu : affinity) listedAffinity.push_back(cpu); uint64_t powerCount = 1 << (affinity.size()); for(uint64_t k=1; k<powerCount; k++) { Affinity subset; for(uint64_t index=0; index<affinity.size(); index++) { if( (1UL << index) & k ) { subset.insert(listedAffinity[index]); } } ret.insert(subset); } return ret; }
bool AffinityTask::staticStrongAnalysis(const TaskSet& taskSet, Time overhead) { AffinityTask::Compare compare; std::unordered_map<AffinityTask*, Time> responseTime; Affinity allCPU; for(AffinityTask* task : taskSet) { responseTime.insert(std::pair<AffinityTask*, Time>(task, task->worstExecution)); for(auto cpu : task->affinity) allCPU.insert(cpu); } while(true) { bool changed = false; bool overflow = false; std::unordered_map<AffinityTask*, Time> newResponseTime; for(auto current : responseTime) { AffinityTask* curTask = current.first; std::set<Affinity> powerSet = AffinityTask::powerSet(curTask->affinity); Time currentResponse = responseTime.find(curTask)->second; TaskSet ignoreTask; ignoreTask.insert(curTask); Time min_sumInterfere = std::numeric_limits<Time>::max(); for(Affinity s : powerSet) { assert(s.size() != 0); Size s_Size = s.size(); //Time sumInterference = 0; std::unordered_map<CPUID, std::list<TaskSet>> possibleReplacement; for(auto cpu : s) { possibleReplacement.insert(std::pair<CPUID, std::list<TaskSet>> (cpu, std::list<TaskSet>())); } for(CPUID selectedCPU : s) { Affinity ignoreCPU(s); ignoreCPU.erase(selectedCPU); for(auto alternative : allCPU) { if(ignoreCPU.find(alternative) != ignoreCPU.end()) continue; auto allPaths = allPath(taskSet, selectedCPU, alternative, ignoreCPU, ignoreTask); for(auto path : allPaths) { if(path.size() > 0) { TaskSet ignoredTask; Affinity moreCheck; for(auto item : path) { if(item.isTask()) ignoredTask.insert(item.getTask()); else moreCheck.insert(item.getCPUID()); } TaskSet highTaskSet; for(AffinityTask* highPriorityTask : taskSet) { //if(compare(curTask, highPriorityTask)) // continue; if(highPriorityTask == curTask) continue; if(ignoredTask.find(highPriorityTask) != ignoredTask.end()) continue; bool intersect = false; for(auto cpu : highPriorityTask->affinity) { if(moreCheck.find(cpu) != moreCheck.end()) { intersect = true; break; } } if(!intersect) continue; highTaskSet.insert(highPriorityTask); } possibleReplacement.find(selectedCPU)->second.push_back(highTaskSet); } } } } for(auto possibleSet : combinePossibleTaskSet(possibleReplacement)) { Time sumInterference = 0; /* if(possibleSet.size() ==0) continue; assert(possibleSet.size() > 0); */ sumInterference += overhead; for(auto highPriorityTask : possibleSet) { Time interferenceCount = currentResponse/highPriorityTask->minPeriod; Time remaining = currentResponse % highPriorityTask->minPeriod; Time interference = interferenceCount * highPriorityTask->worstExecution + std::min(remaining, highPriorityTask->worstExecution); Time contextSwitchCount = interferenceCount; if(remaining > 0) contextSwitchCount++; sumInterference += 2*(contextSwitchCount) * overhead; if(compare(curTask, highPriorityTask)) continue; sumInterference += interference; } Time floorValue = floor((Real)sumInterference / (Real)s_Size); min_sumInterfere = std::min(min_sumInterfere, floorValue); } } assert(min_sumInterfere != std::numeric_limits<Time>::max()); Time nextResponse = curTask->worstExecution + min_sumInterfere; newResponseTime.insert(std::pair<AffinityTask*, Time>(curTask, nextResponse)); if(currentResponse != nextResponse) changed = true; if(currentResponse > curTask->minPeriod) overflow = true; } if(changed) responseTime = newResponseTime; else break; if(overflow) break; } bool possible = true; for(auto iter : responseTime) { if(iter.second > iter.first->minPeriod) { possible = false; iter.first->print_log(WARN, "Execution time: %lu, Period: %lu, Response time: %lu", iter.first->worstExecution, iter.first->minPeriod, iter.second); } else { iter.first->print_log(INFO, "Execution time: %lu, Period: %lu, Response time: %lu", iter.first->worstExecution, iter.first->minPeriod, iter.second); } } return possible; }