cheddar调度性模拟器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
LinSched 最初是在北卡罗来纳大学开发的(IBM 和 Intel 提供赞助),但是最近又被 Google 恢复了,用来验证调度策略。这是有趣的举动,可能代表一种未来趋势。虽然 Linux 支持许多调度类并跨许多工作负荷进行了合理的调度工作,但是对于给定硬件拓扑上的特定工作负荷,它可能不是最理想的。因为 Google 在执行特定任务(例如 MapReduce)的大量同型机集群上投资,所以针对该任务和环境调整调度有意义。在 LinSched 使用中,Google 还有助于增强适用于完全公平调度器(Completely Fair Scheduler,CFS)的 Linux,包括当任务间存在很大差异时的带宽设置和负载平衡。 LinSched 是驻留在用户空间中的 Linux 调度模拟器。它隔离 Linux 调度器子系统并围绕它构建足够的内核环境,从而可以在用户空间内执行该模拟器。它还将围绕其构建一系列应用程序编程接口(Application Programming Interfaces,APIs)以提供必要的刺激来验证调度工作负荷并收集足够的相关数据来了解其行为(稍后将介绍其整体架构)。 将调度器放入用户空间很容易就可以执行该调度器(因为调度器的崩溃不会引起整机崩溃)。这也使得收集有关调度器的数据变得简单,因为其可以在本地文件系统中轻松而又有效地收集数据并存储数据。 使用虚拟化是一种方案,但是 LinSched 的优势是简单且有效(因为没有必要启动 LinSched 不仅仅是在用户空间中执行的调度模拟器:它是通过必要组件的瘦模拟将 Linux 调度器迁移到用户空间,以便该调度器能够在内核以外执行。对于包装器和模拟引擎来说,LinSched 源实际上是带有名为 ./linsched 的新子目录的 Linux 版本(目前是 2.6.35)。因为 LinSched 为其模拟在 Linux 内使用 Linux 调度器子系统,所以进行更改,然后将更改集成回内核要简单得多。 LinSched 的总体架构如图 1 所示。底部是主机操作系统。LinSched 是由许多组件构建的用户空间应用程序(包括部分 Linux 内核本身)。另一个内核)。作为用户空间进程,也很容易附加一个调试器(如 GNU Debugger (GDB))来更深入地了解调度器操作。 图1 linsched架构 环境模块提供 Linux 内核的抽象概念(通过在模拟模式中支持编译的标志)。在环境模块上的是模拟引擎,扩展了 API 以用于配置环境并执行模拟。模拟引擎 API 提供同步功能,定义处理器拓扑的初始化函数以及用于任务创建、回调(例如,在调度任务时)的函数和用于执行对一些时钟节拍执行模拟的 run 函数(其反过来调用内部 Linux schedule() 函数来制定调度决策)。 上面的模拟引擎是脚本环境(真实模拟的代码)。虽然它可能是单一脚本,但是正如后面的示例所示,最简单的用法是修改现有脚本环境来增加测试场景。 LinSched 的基本架构非常简单。我们首先来研究如何安装 LinSched,然后再讨论一些扩展的 API 以及如何在模拟中使用它们。 (以上简介选自:http://www.ibm.com/developerworks/cn/linux/l-linux-scheduler-simulator/) 标签:LinSched
不使用LINQ查询和操作集合 改进代码 namespace SandwichCalories { class Program { static void Main(string[] args) { // sandwich ingredients and their associated calories Dictionary<string, int> ingredients = new Dictionary<string, int>() { { "Bread", 66 }, { "Ham", 72 }, { "Bologna", 57 }, { "Chicken", 17 }, { "Corned Beef", 53 }, { "Salami", 40 }, { "Cheese, American", 104 }, { "Cheese, Cheddar", 113 }, { "Cheese, Havarti", 105 }, { "Mayonnaise", 94 }, { "Mustard", 10 }, { "Butter", 102 }, { "Garlic Aioli", 100 }, { "Sriracha", 15 }, { "Dressing, Ranch", 73 }, { "Dressing, 1000 Island", 59 }, { "Lettuce", 5 }, { "Tomato", 4 }, { "Cucumber", 4 }, { "Banana Pepper", 10 }, { "Green Pepper", 3 }, { "Red Onion", 6 }, { "Spinach", 7 }, { "Avocado", 64 } }; // prompt user for calorie range Console.Write("Enter minimum calories: "); int min_calories = int.Parse(Console.ReadLine()); Console.Write("Enter maximum calories: "); int max_calories = int.Parse(Console.ReadLine()); // calculate the minimum and maximum calories for the sandwich int min_sandwich_calories = 2 * ingredients["Bread"] + ingredients.Values.Min() * 2; int max_sandwich_calories = 2 * ingredients["Bread"] + ingredients.Values.Max() * 2; // check if the calorie range is valid if (max_calories < min_sandwich_calories) { Console.WriteLine("Sorry, it is impossible to create a sandwich within the given calorie range."); } else { // create the sandwich List<string> sandwich = new List<string> { "Bread" }; int sandwich_calories = 1 * ingredients["Bread"]; while (sandwich_calories < min_calories) { // add random ingredient string ingredient = ingredients.Keys.ElementAt(new Random().Next(ingredients.Count)); sandwich.Add(ingredient); sandwich_calories += ingredients[ingredient]; } while (sandwich_calories <= max_calories) { // add random ingredient string ingredient = ingredients.Keys.ElementAt(new Random().Next(ingredients.Count)); // check if the ingredient is the same as the previous one if (sandwich.Count >= 3 && ingredient == sandwich[sandwich.Count - 2]) { continue; } sandwich.Add(ingredient); sandwich_calories += ingredients[ingredient]; // check if the sandwich is already at the maximum calorie limit if (sandwich_calories == max_sandwich_calories) { break; } } // add the last slice of bread sandwich.Add("Bread"); // print the sandwich and its total calories Console.WriteLine("Your sandwich: " + string.Join(", ", sandwich)); Console.WriteLine("Total calories: " + sandwich_calories); } } } }
06-10
以下是不使用LINQ查询和操作集合改进的代码,这里使用了foreach循环和if条件语句来实现相同的功能: ```csharp using System; using System.Collections.Generic; namespace SandwichCalories { class Program { static void Main(string[] args) { // sandwich ingredients and their associated calories Dictionary<string, int> ingredients = new Dictionary<string, int>() { { "Bread", 66 }, { "Ham", 72 }, { "Bologna", 57 }, { "Chicken", 17 }, { "Corned Beef", 53 }, { "Salami", 40 }, { "Cheese, American", 104 }, { "Cheese, Cheddar", 113 }, { "Cheese, Havarti", 105 }, { "Mayonnaise", 94 }, { "Mustard", 10 }, { "Butter", 102 }, { "Garlic Aioli", 100 }, { "Sriracha", 15 }, { "Dressing, Ranch", 73 }, { "Dressing, 1000 Island", 59 }, { "Lettuce", 5 }, { "Tomato", 4 }, { "Cucumber", 4 }, { "Banana Pepper", 10 }, { "Green Pepper", 3 }, { "Red Onion", 6 }, { "Spinach", 7 }, { "Avocado", 64 } }; // prompt user for calorie range Console.Write("Enter minimum calories: "); int min_calories = int.Parse(Console.ReadLine()); Console.Write("Enter maximum calories: "); int max_calories = int.Parse(Console.ReadLine()); // calculate the minimum and maximum calories for the sandwich int min_sandwich_calories = 2 * ingredients["Bread"] + GetMinValue(ingredients.Values) * 2; int max_sandwich_calories = 2 * ingredients["Bread"] + GetMaxValue(ingredients.Values) * 2; // check if the calorie range is valid if (max_calories < min_sandwich_calories) { Console.WriteLine("Sorry, it is impossible to create a sandwich within the given calorie range."); } else { // create the sandwich List<string> sandwich = new List<string> { "Bread" }; int sandwich_calories = 1 * ingredients["Bread"]; while (sandwich_calories < min_calories) { // add random ingredient string ingredient = GetRandomIngredient(ingredients); sandwich.Add(ingredient); sandwich_calories += ingredients[ingredient]; } while (sandwich_calories <= max_calories) { // add random ingredient string ingredient = GetRandomIngredient(ingredients); // check if the ingredient is the same as the previous one if (sandwich.Count >= 3 && ingredient == sandwich[sandwich.Count - 2]) { continue; } sandwich.Add(ingredient); sandwich_calories += ingredients[ingredient]; // check if the sandwich is already at the maximum calorie limit if (sandwich_calories == max_sandwich_calories) { break; } } // add the last slice of bread sandwich.Add("Bread"); // print the sandwich and its total calories Console.WriteLine("Your sandwich: " + string.Join(", ", sandwich)); Console.WriteLine("Total calories: " + sandwich_calories); } } // helper methods static int GetMinValue(IEnumerable<int> values) { int minValue = int.MaxValue; foreach (int value in values) { if (value < minValue) { minValue = value; } } return minValue; } static int GetMaxValue(IEnumerable<int> values) { int maxValue = int.MinValue; foreach (int value in values) { if (value > maxValue) { maxValue = value; } } return maxValue; } static string GetRandomIngredient(Dictionary<string, int> ingredients) { int index = new Random().Next(ingredients.Count); int i = 0; foreach (string key in ingredients.Keys) { if (i == index) { return key; } i++; } return null; } } } ``` 在这个改进的代码中,我们使用了GetMinValue和GetMaxValue方法来获取字典中值的最小值和最大值,使用GetRandomIngredient方法来获取随机的食材名称。虽然这种方式代码量增加了,但是这种方式可以更好地理解代码的每个步骤,也可以更好地掌控代码的细节。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值