2.4 قوائم انتظار الأولوية تتطلب العديد من التطبيقات أن نقوم بمعالجة العناصر التي تحتوي على مفاتيح في الترتيب، ولكن ليس بالضرورة بالترتيب الكامل وليس بالضرورة في كل مرة. في كثير من الأحيان، نقوم بجمع مجموعة من العناصر، ثم معالجة واحدة مع أكبر مفتاح، ثم ربما جمع المزيد من العناصر، ثم معالجة واحدة مع أكبر مفتاح الحالي، وهكذا دواليك. نوع البيانات المناسب في مثل هذه البيئة يدعم عمليتين: إزالة الحد الأقصى وإدراج. ويسمى هذا النوع من البيانات طابور الأولوية. وتتميز طوابير الأولوية من خلال إزالة عمليات الحد الأقصى والإدراج. من خلال الاتفاقية، سنقوم بمقارنة مفاتيح فقط مع أقل () الأسلوب، كما كنا نفعل لفرز. وبالتالي، إذا كان يمكن أن يكون للسجلات مفاتيح مكررة، فإن الحد الأقصى يعني أي سجل له أكبر قيمة رئيسية. لإكمال واجهة برمجة التطبيقات، نحتاج أيضا إلى إضافة منشئات واختبار إذا كانت العملية فارغة. للمرونة، نستخدم تنفيذ عام مع مفتاح نوع عام الذي ينفذ مقارنة. برنامج TopM. java هو عميل طابور الأولوية الذي يأخذ وسيطة سطر الأوامر M. يقرأ المعاملات من المدخلات القياسية، ويطبع من أكبر المعاملات M. التطبيقات الأولية. وتتيح لنا هياكل البيانات الأساسية التي ناقشناها في القسم 1.3 أربع نقاط بداية مباشرة لتنفيذ طوابير الأولوية. تمثيل صفيف (غير مرتبة). ولعل أبسط تنفيذ قائمة الانتظار ذات الأولوية يستند إلى التعليمات البرمجية الخاصة بنا للمداخن المتداعية. رمز الإدراج في قائمة انتظار الأولوية هو نفسه للدفع في المكدس. لتنفيذ إزالة الحد الأقصى. يمكننا إضافة رمز مثل الحلقة الداخلية من اختيار نوع لتبادل الحد الأقصى البند مع البند في النهاية ومن ثم حذف ذلك واحد، كما فعلنا مع البوب () للمداخن. برنامج UnorderedArrayMaxPQ. java بتنفيذ طابور الأولوية باستخدام هذا النهج. تمثيل صفيف (أمر). نهج آخر هو إضافة رمز لإدراج لنقل إدخالات أكبر موقف واحد إلى اليمين، وبالتالي حفظ الإدخالات في الصفيف في النظام (كما هو الحال في نوع الإدراج). وبالتالي فإن أكبر عنصر هو دائما في النهاية، ورمز لإزالة الحد الأقصى في قائمة انتظار الأولوية هو نفسه لبوب في المكدس. برنامج orderedArrayMaxPQ. java بتنفيذ طابور الأولوية باستخدام هذا النهج. تمثيلات القائمة المرتبطة (غير مرتبة وعكس ترتيب). وبالمثل، يمكننا أن نبدأ برمزنا المرتبط بالقائمة لمكدسات الضغط، إما تعديل شفرة البوب () لإيجاد وإرجاع الحد الأقصى أو رمز الدفع () للحفاظ على العناصر بترتيب عكسي ورمز البوب () إلى إلغاء الربط وإعادة العنصر الأول (الحد الأقصى) في القائمة. جميع التطبيقات الأولية التي تمت مناقشتها للتو هي الخاصية التي إما إدراج أو إزالة العملية القصوى يأخذ الوقت الخطي في أسوأ الحالات. العثور على التنفيذ حيث يضمن كل من العمليات لتكون سريعة مهمة أكثر إثارة للاهتمام، وهذا هو الموضوع الرئيسي لهذا القسم. تعاريف كومة. كومة الذاكرة المؤقتة هي بنية البيانات التي يمكن أن تدعم بكفاءة العمليات أولوية الأولوية قائمة الانتظار. في كومة ثنائية، يتم تخزين العناصر في مصفوفة بحيث يضمن كل مفتاح أن يكون أكبر من (أو يساوي) مفاتيح في اثنين من المواقف المحددة الأخرى. في المقابل، يجب أن يكون كل من هذه المفاتيح أكبر من مفتاحين آخرين، وهكذا دواليك. هذا الترتيب من السهل أن نرى إذا كنا نرى مفاتيح كما يجري في هيكل شجرة ثنائية مع حواف من كل مفتاح إلى مفتاحين المعروف أن أصغر. فريف. شجرة ثنائية هي كومة أمر إذا كان المفتاح في كل عقدة أكبر من (أو يساوي) مفاتيح في تلك العقد طفلان (إن وجدت). الاقتراح. يتم العثور على أكبر مفتاح في شجرة ثنائية كومة من الكومة في الجذر. يمكننا فرض تقييد كومة الذاكرة المؤقتة على أي شجرة ثنائية. ومن الملائم بشكل خاص، مع ذلك، لاستخدام شجرة ثنائية كاملة مثل واحد أدناه. نحن نمثل الأشجار الثنائية كاملة بالتتابع داخل مصفوفة من خلال وضع العقد مع ترتيب المستوى. مع الجذر في الموضع 1، أطفالها في المواقف 2 و 3، أطفالهم في المواقف 4 و 5 و 6 و 7، وهلم جرا. فريف. كومة ثنائية هي مجموعة من العقد مع مفاتيح مرتبة في شجرة كومة كاملة أمر ثنائي، ممثلة في ترتيب المستوى في صفيف (لا يستخدم الإدخال الأول). في كومة، الوالد من العقدة في موقف k هو في موقف k2، وعلى العكس من ذلك، اثنين من الأطفال من العقدة في موقف k هي في مواقف 2K و 2 K 1. يمكننا السفر صعودا وهبوطا عن طريق القيام حساب بسيط على مجموعة الصفيف : لنقل ما يصل شجرة من أك وضعنا k إلى k2 لنقل أسفل شجرة وضعنا k إلى 2k أو 2k1. خوارزميات على أكوام. ونحن نمثل كومة من حجم N في صفيف خاص يا من طول N1، مع pq0 غير المستخدمة وكومة الذاكرة المؤقتة في pq1 من خلال بن. نحن الوصول إلى مفاتيح فقط من خلال وظائف المساعد الخاص أقل () وتبادل (). عمليات كومة الذاكرة المؤقتة التي نعتبرها العمل من خلال إجراء أول تعديل بسيط يمكن أن ينتهك حالة كومة الذاكرة المؤقتة، ثم السفر من خلال كومة الذاكرة المؤقتة، وتعديل كومة كما هو مطلوب لضمان أن كومة حالة راض في كل مكان. ونشير إلى هذه العملية باعتبارها إعادة تشكيل. أو استعادة كومة النظام. من أسفل إلى أعلى ريهابيفي (السباحة). إذا تم انتهاك أمر كومة الذاكرة المؤقتة لأن مفتاح العقد يصبح أكبر من العقد العقد الآباء، ثم يمكننا أن نحرز تقدما نحو تحديد الانتهاك عن طريق تبادل العقدة مع والدها. بعد تبادل، العقدة أكبر من كل من أطفالها (واحد هو الوالد القديم، والآخر أصغر من الوالد القديم لأنه كان طفلا من تلك العقدة) ولكن العقدة قد لا تزال أكبر من الأصل. يمكننا إصلاح هذا الانتهاك بنفس الطريقة، وهكذا دواليك، والانتقال حتى كومة حتى نصل إلى عقدة مع مفتاح أكبر، أو الجذر. من الأعلى إلى الأسفل هيفيفي (بالوعة). إذا تم انتهاك أمر كومة الذاكرة المؤقتة لأن مفتاح العقد يصبح أصغر من واحد أو كل من تلك العقد مفاتيح الأطفال، ثم يمكننا أن نحرز تقدما نحو تحديد الانتهاك من خلال تبادل العقدة مع أكبر من طفليه. هذا التبديل قد يسبب انتهاكا في الطفل نحن إصلاح هذا الانتهاك في نفس الطريق، وهكذا دواليك، والانتقال إلى كومة حتى نصل إلى عقدة مع كل من الأطفال أصغر، أو أسفل. قائمة انتظار القائمة على كومة الأولوية. هذه العمليات بالوعة () والسباحة () توفر الأساس للتنفيذ الفعال لواجهة برمجة التطبيقات ذات الأولوية في قائمة الانتظار، كما هو مخطط أدناه وتنفيذها في MaxPQ. java و MinPQ. java. إدراج. نضيف العنصر الجديد في نهاية المصفوفة، وزيادة حجم كومة الذاكرة المؤقتة، ومن ثم السباحة حتى من خلال كومة مع هذا البند لاستعادة حالة كومة الذاكرة المؤقتة. إزالة الحد الأقصى. نحن نأخذ أكبر عنصر من أعلى، ووضع هذا البند من نهاية كومة في الجزء العلوي، تناقص حجم كومة، ثم تغرق أسفل من خلال كومة مع هذا البند لاستعادة حالة كومة. الاقتراح. في طابور أولوية N-إيتم، تتطلب خوارزميات كومة الذاكرة المؤقتة أكثر من 1 لغ N يقارن لإدراج وليس أكثر من 2 لغ N يقارن لإزالة الحد الأقصى. اعتبارات عملية. نختتم دراستنا ل أبي قائمة الانتظار كومة مع بعض الاعتبارات العملية. أكوام متعددة الطرق. ليس من الصعب تعديل التعليمات البرمجية لبناء أكوام على أساس تمثيل مجموعة من كومة كومة كاملة أو ثلاثية الأشجار. هناك مقايضة بين انخفاض تكلفة من انخفاض شجرة الشجرة وارتفاع تكلفة العثور على أكبر من ثلاثة أو د الأطفال في كل عقدة. تغيير حجم الصفيف. يمكننا إضافة منشئ لا حجة، رمز لمصفوفة مضاعفة في إدراج (). ورمز لصفيف النصف في دلماكس (). تماما كما فعلنا للمداخن في القسم 1.3. يتم إطفاء حدود الوقت اللوغاريتمي عندما يكون حجم قائمة انتظار الأولوية تعسفية ويتم تغيير حجم المصفوفات. قابلية المفاتيح. يحتوي طابور الأولوية على الكائنات التي تم إنشاؤها بواسطة عملاء ولكن يفترض أن التعليمات البرمجية العميل لا تغيير المفاتيح (التي قد تؤدي إلى إبطال كومة الذاكرة المؤقتة). قائمة انتظار أولوية الفهرس. في العديد من التطبيقات، من المنطقي السماح للعملاء الرجوع إلى العناصر الموجودة بالفعل في قائمة انتظار الأولوية. إحدى الطرق السهلة للقيام بذلك هي ربط مؤشر عدد صحيح فريد مع كل عنصر. IndexMinPQ. java هو تطبيق يستند إلى كومة من هذا أبي IndexMaxPQ. java هو مماثل ولكن بالنسبة لقوائم الانتظار ذات الأولوية القصوى الموجهة. Multiway. java هو العميل الذي يدمج معا عدة تيارات المدخلات فرزها إلى واحد إخراج تيار فرزها. يمكننا استخدام أي طابور أولوية لتطوير طريقة الفرز. نقوم بإدراج كافة المفاتيح ليتم فرزها إلى قائمة انتظار الأولوية ذات الأولوية الدنيا، ثم استخدام مرارا وتكرارا إزالة الحد الأدنى لإزالتها كلها في النظام. عند استخدام كومة ل طابور الأولوية، نحصل على هيبسورت. مع التركيز على مهمة الفرز، ونحن التخلي عن فكرة إخفاء تمثيل كومة من طابور الأولوية واستخدام السباحة () والوعة () مباشرة. القيام بذلك يسمح لنا لفرز صفيف دون الحاجة إلى أي مساحة إضافية، من خلال الحفاظ على كومة داخل المصفوفة ليتم فرزها. هيبسورت ينقسم إلى مرحلتين: كومة البناء. حيث نقوم بإعادة تنظيم الصفيف الأصلي إلى كومة الذاكرة المؤقتة، والانتشار. حيث نقوم بسحب العناصر من كومة الذاكرة المؤقتة في ترتيب تنازلي لبناء النتيجة فرزها. بناء كومة. يمكننا تحقيق هذه المهمة في الوقت المناسب النسبي ل N لغ N، من خلال الانتقال من اليسار إلى اليمين من خلال مجموعة، وذلك باستخدام السباحة () للتأكد من أن الإدخالات على يسار مؤشر المسح تشكل شجرة كومة كاملة أمر، مثل متتالية وإدراج قوائم الانتظار ذات الأولوية. طريقة ذكية هي أكثر كفاءة بكثير هو المضي قدما من اليمين إلى اليسار، وذلك باستخدام بالوعة () لجعل سوبهيابس ونحن نذهب. كل موقف في المصفوفة هو جذر الحوض سوبهياب صغير () يعمل أو مثل سوبهابس، كذلك. إذا كان الطفلان من عقدة هي أكوام، ثم استدعاء بالوعة () على تلك العقدة يجعل الشجرة الفرعية الجذور هناك كومة. Sortdown. معظم العمل خلال هابسورت يتم خلال المرحلة الثانية، حيث نقوم بإزالة أكبر العناصر المتبقية من كومة ووضعها في وضع صفيف تخليت كما يتقلص كومة الذاكرة المؤقتة. Heap. java هو التنفيذ الكامل لل هيبسورت. وفيما يلي أثر لمحتويات الصفيف بعد كل بالوعة. الاقتراح. بناء كومة القائم على بالوعة هو الوقت الخطي. الاقتراح. المستخدمين هيبسورت أقل من 2N لغ ن مقارنة والتبادلات لفرز ن البنود. معظم العناصر إعادة إدراجها في كومة الذاكرة المؤقتة أثناء الفرز تذهب على طول الطريق إلى أسفل. يمكننا بالتالي توفير الوقت عن طريق تجنب التحقق من ما إذا كان هذا البند قد وصلت إلى موقفها، ببساطة تعزيز أكبر من طفلين حتى يتم الوصول إلى أسفل، ثم نقل ما يصل الكومة إلى الموضع الصحيح. وتقلل هذه الفكرة عدد المقارنات بعامل قدره 2 على حساب المحاسبة الإضافية. لنفترض أن التسلسل (حيث تعني الرسالة إدراج و علامة نجمية يعني إزالة الحد الأقصى) يتم تطبيقه على قائمة انتظار أولوية فارغة مبدئيا. إعطاء تسلسل القيم التي تم إرجاعها عن طريق إزالة العمليات القصوى. حل. ننتظر الفكرة التالية: لتنفيذ العثور على الحد الأقصى في الوقت المستمر، لماذا لا تتبع القيمة القصوى إدراج حتى الآن، ثم إرجاع تلك القيمة للعثور على الحد الأقصى. حل. سوف تحتاج إلى تحديث الحد الأقصى للقيمة من الصفر بعد عملية إزالة الحد الأقصى. قم بتوفير تطبيقات أولوية الانتظار التي تدعم إدراج وإزالة الحد الأقصى. واحد لكل من هياكل البيانات الأساسية التالية: صفيف غير مرتبة، صفيف أمر، قائمة مرتبة غير مرتبة، وقائمة مرتبة مرتبة. قدم جدولا لأسوأ الحالات لكل عملية لكل من تطبيقاتك الأربعة من التمرين السابق. حل جزئي. OrderedArrayMaxPQ. java و UnorderedArrayMaxPQ. java هي صفيف يتم فرزه بترتيب تنازلي كومة كومة قصوى التوجه. إجابة. نعم فعلا. لنفترض أن التطبيق الخاص بك سوف يكون عدد كبير من عمليات إدراج، ولكن عدد قليل فقط إزالة العمليات القصوى. ما هي الأولوية في طابور التنفيذ الذي تعتقد أنه سيكون الأكثر فعالية: كومة، صفيف غير مرتبة، أمر صفيف الإجابة. صفيف غير مرتبة. إدراج هو وقت ثابت. لنفترض أن التطبيق الخاص بك وسوف يكون لها عدد كبير من العثور على أقصى قدر من العمليات، ولكن عدد قليل نسبيا من إدراج وإزالة الحد الأقصى للعمليات. أي أولوية تنفيذ طابور تعتقد أنه سيكون الأكثر فعالية: كومة، صفيف غير مرتبة، أمر صفيف الإجابة. ترتيب متناسق. العثور على الحد الأقصى هو الوقت المستمر. ما هو الحد الأدنى لعدد العناصر التي يجب تبادلها أثناء إزالة العملية القصوى في كومة من الحجم N بدون مفاتيح مكررة إعطاء كومة من حجم 15 التي يتحقق الحد الأدنى. الإجابة على نفس السؤال لمدة سنتين وثلاثة متتالية إزالة العمليات القصوى. إجابة جزئية. (أ) 2. تصميم خوارزمية شهادة خطي الوقت للتحقق ما إذا كان صفيف يا هو كومة دقيقة الموجه. حل. انظر طريقة إيسمينهاب () في MinPQ. java. تثبت أن بناء كومة بناء على أساس بالوعة يستخدم في معظم 2 ن يقارن وعلى الأكثر n التبادلات. حل. ويكفي أن يثبت أن بناء كومة القائم على بالوعة يستخدم أقل من n التبادلات لأن عدد المقارنات هو في معظم ضعف عدد التبادلات. للبساطة، نفترض أن كومة ثنائية مثالية (أي شجرة ثنائية التي يتم الانتهاء من كل مستوى شغل) وارتفاع h. نحدد ارتفاع عقدة في شجرة ليكون ارتفاع الشجرة الفرعية الجذور في تلك العقدة. يمكن تبديل مفتاح في الارتفاع k مع مفاتيح k الأكثر تحت تحتها عند غرقها. وبما أن هناك عقدة 2 ناقص k عند ارتفاع k. العدد الإجمالي للتبادلات هو على الأكثر: المساواة الأولى هي لمبلغ غير قياسي، ولكن من السهل التحقق من أن الصيغة يحمل عن طريق الاستقراء الرياضي. المساواة الثانية يحمل لأن شجرة ثنائية الكمال من ارتفاع h له 2 ساعة 1 ناقص 1 العقد. إثبات أن النتيجة يحمل عندما الشجرة الثنائية ليست مثالية يتطلب المزيد من الرعاية. يمكنك القيام بذلك باستخدام حقيقة أن عدد العقد في ارتفاع k في كومة ثنائية على العقد N هو في معظم سيل (ن 2 ك 1). حل بديل. مرة أخرى، للبساطة، نفترض أن كومة ثنائية مثالية (أي شجرة الثنائية التي يتم الانتهاء من كل مستوى). نحدد ارتفاع عقدة في شجرة ليكون ارتفاع الشجرة الفرعية الجذور في تلك العقدة. أولا، لاحظ أن كومة ثنائية على العقد n ن ناقص 1 وصلات (لأن كل وصلة هو الأصل من عقدة واحدة وكل عقدة لديه ارتباط الأصل باستثناء الجذر). يتطلب غرق عقدة من الارتفاع k في معظم التبادلات k. ونحن سوف تهمة k وصلات إلى كل عقدة في ارتفاع k. ولكن ليس بالضرورة الروابط على المسار التي اتخذت عند غرق العقدة. بدلا من ذلك، نحن تهمة العقدة k الروابط على طول المسار من العقدة التي يذهب اليسار واليمين اليمين واليمين. على سبيل المثال، في الرسم البياني أدناه، يتم تحميل العقدة الجذر الروابط الحمراء 4 يتم شحن العقدة الزرقاء 3 وصلات زرقاء وهكذا دواليك. لاحظ أنه لا يتم تحميل أي رابط على أكثر من عقدة واحدة. (في الواقع، هناك نوعان من الارتباطات لا تهمة على أي عقدة: الرابط الصحيح من الجذر والرابط الأصل من عقدة أقصى اليمين.) وهكذا، فإن العدد الإجمالي للتبادلات هي على الأكثر n. وبما أن هناك على الأكثر 2 يقارن في الصرف، وعدد من يقارن هو على الأكثر 2 ن. المشكلات الإبداعية نظرية العدد الحسابي. كتابة برنامج CubeSum. java الذي يطبع جميع الأعداد الصحيحة من النموذج 3 ب 3 حيث a و b هي أعداد صحيحة بين 0 و N في ترتيب فرزها، دون استخدام مساحة مفرطة. وهذا هو، بدلا من حساب صفيف من المبالغ N 2 وفرزها، وبناء طابور الأولوية الدنيا الموجهة، التي تحتوي في البداية (0 3. 0، 0)، (1 3، 1، 0)، (2 3 2 ، 0). (N 3. N، 0). ثم، في حين أن قائمة انتظار الأولوية غير نادرة، قم بإزالة أصغر عنصر (i 3 j 3. i، j)، وطباعته، ثم إذا كان j 3 (j1) 3. i، j1). استخدام هذا البرنامج للعثور على جميع الأعداد الصحيحة المميزة أ، ب، ج، و د بين 0 و 106 مثل أن 3 ب 3 ج 3 د 3. على سبيل المثال. 1729 93 103 13 123. ابحث عن الحد الأدنى. إضافة طريقة دقيقة () إلى MaxPQ. java. يجب أن يستخدم التنفيذ الخاص بك وقت ثابت ومساحة إضافية ثابتة. حل . إضافة متغير مثيل إضافي يشير إلى الحد الأدنى من العنصر. تحديثه بعد كل مكالمة لإدراج (). إعادة تعيينه إلى صفر إذا أصبحت قائمة انتظار الأولوية فارغة. ديناميكية-وسط البحث. تصميم نوع البيانات التي تدعم إدراج في الوقت اللوغاريتمي، والعثور على الوسيط في الوقت المستمر، وإزالة الوسيط في الوقت اللوغاريتمي. حل . الحفاظ على مفتاح وسيط في الخامس استخدام كومة كومة الموجهة للمفاتيح أقل من مفتاح v استخدام كومة دقيقة الموجهة للمفاتيح أكبر من مفتاح v. إدراج، إضافة مفتاح جديد في كومة المناسبة، واستبدال الخامس مع المفتاح المستخرجة من تلك الكومة. الأدنى. إثبات أنه من المستحيل لتطوير تنفيذ أبي مينق بحيث أن كلا إدراج وحذف الحد الأدنى من الضمان لاستخدام N سجل سجل N يقارن. حل. وهذا من شأنه أن يسفر عن خوارزمية الفرز N القائمة على سجل N N (إدراج العناصر N، ثم إزالة مرارا الحد الأدنى)، منتهكة اقتراح القسم 2.3. تنفيذ أولوية قائمة انتظار الفهرس. قم بتنفيذ indexMaxPQ. java عن طريق تعديل MaxPQ. java على النحو التالي: تغيير يا لعقد المؤشرات، إضافة مفاتيح صفيف لعقد قيم المفاتيح، وإضافة كب صفيف هو معكوس ق مدش كي يعطي موقف i في يا (و مؤشر j مثل أن بج هو i). ثم قم بتعديل التعليمات البرمجية للحفاظ على هياكل البيانات هذه. استخدام الاتفاقية أن كي هو -1 إذا لم يكن في قائمة الانتظار، وتشمل طريقة تحتوي على () أن يختبر هذا الشرط. تحتاج إلى تعديل الأساليب مساعد تبادل () وأقل () ولكن لا بالوعة () أو السباحة (). تمارين الويب أفضل، متوسط، وأسوأ حالة هيبسورت. ما هي أفضل حالة، متوسط الحالة، وأسوأ حالة عدد من يقارن ل هيابسورتينغ مجموعة من طول N الحل. إذا سمحنا بالتكرارات، فإن أفضل حالة هي الوقت الخطي (N مفاتيح متساوية) إذا رفضنا التكرارات، وأفضل حالة هي N لغ N يقارن (ولكن أفضل حالة الإدخال غير تافهة). متوسط وأسوأ عدد من حالات المقارنة هو 2 N لغ N يقارن. انظر تحليل هيبسورت لمزيد من التفاصيل. أفضل وأسوأ حالة من هيفيفي. ما هو أقل وعدد من كومبريسيكسشانجيس اللازمة لتكوين صفيف من N البنود الحل. يتطلب تشفير صفيف من العناصر N بترتيب تنازلي 0 تبادل و N - 1 يقارن. ويتطلب تبييض صفيف من العناصر N بترتيب تصاعدي التبادل N و 2N مقارنة. أرقام سيارات الأجرة. البحث عن أصغر الأعداد الصحيحة التي يمكن التعبير عنها كمجموع مكعبات من الأعداد الصحيحة بطريقتين مختلفتين (1،729)، ثلاث طرق مختلفة (87،539،319)، أربع طرق مختلفة (6،963،472،309،248)، خمس طرق مختلفة (48،988،659،276،962،496) وست طرق مختلفة (24،153،319،581،254،312،065،344 ). وتسمى هذه الأعداد الصحيحة أرقام تاكسيكاب بعد قصة رامانوجان الشهيرة. أصغر الأعداد الصحيحة التي يمكن التعبير عنها كمجموع مكعبات من الأعداد الصحيحة في سبع طرق مختلفة غير معروف حاليا. كتابة برنامج Taxicab. java الذي يقرأ في سطر الأوامر المعلمة N ويطبع من جميع الحلول غير تافهة من 3 ب 3 ج 3 د 3. مثل أن، ب، ج، ود، هي أقل من أو يساوي N. الحسابية نظرية الأعداد. اعثر على جميع الحلول للمعادلة a 2b 2 3c 3 4d 4 التي a، b، c، d هي أقل من 100،000. ملحوظة . استخدام كومة واحدة كومة واحدة كومة كحد أقصى. التعامل مع المقاطعة. عند برمجة نظام في الوقت الفعلي يمكن انقطاعه (على سبيل المثال عن طريق النقر بالماوس أو الاتصال اللاسلكي)، من الضروري حضور المقاطعات على الفور، قبل متابعة النشاط الحالي. إذا كان ينبغي التعامل مع المقاطعات بنفس الترتيب الذي يصلون، ثم طابور فيفو هو بنية البيانات المناسبة. ومع ذلك، إذا كانت المقاطعات المختلفة لها أولويات مختلفة (على سبيل المثال)، فإننا نحتاج إلى قائمة انتظار ذات أولوية. محاكاة شبكات الطابور. طابور MM1 لطوابير متوازية مزدوجة، وما إلى ذلك من الصعب تحليل شبكات الطابور المعقدة رياضيا. بدلا من ذلك استخدام محاكاة لرسم توزيع أوقات الانتظار، وما إلى ذلك تحتاج إلى قائمة انتظار الأولوية لتحديد أي حدث لمعالجة المقبل. توزيع زيب. استخدام نتيجة التمرين السابق (ق) لعينة من توزيع زيبيان مع المعلمة s و N. التوزيع يمكن أن تأخذ على عدد صحيح من 1 إلى N، ويأخذ على قيمة k مع احتمال 1X مجموع (i 1 إلى N) 1is . مثال: الكلمات في شكسبيرس تلعب هاملت مع s تساوي تقريبا 1. عملية عشوائية. تبدأ مع صناديق N، تتكون كل واحدة الكرة. اختيار عشوائيا واحدة من الكرات N وتحريك الكرة إلى بن عشوائيا بحيث أن احتمال أن يتم وضع الكرة في بن مع كرات م هو من. ما هو توزيع الكرات التي تنتج بعد العديد من التكرار استخدام طريقة أخذ العينات العشوائية المذكورة أعلاه لجعل كفاءة المحاكاة. أقرب الجيران. نظرا N ناقلات × 1. x 2. x N من طول M وناقل آخر x من نفس الطول، والعثور على 20 ناقلات التي هي الأقرب إلى س. دائرة، تعادل، عن، أداة تعريف إنجليزية غير معروفة، إنكسر، كتابة برنامج للعثور على دائرة نصف قطرها من دائرة، تتمحور حول الأصل، الذي يمس 32 نقطة مع عدد صحيح x - و y - الإحداثيات. تلميح: ابحث عن عدد مما يمكن التعبير عنه على أنه مجموع اثنين من الساحات في عدة طرق مختلفة. الجواب: هناك نوعان من ثلاث مرات فيثاغورس مع الوتر 25: 152 202 252، 72 242 252 ينتج 20 هذه النقاط شعرية هناك 22 ثلاث مرات مختلفة فيثاغورس مع الوتر 5،525 وهذا يؤدي إلى 180 نقطة شعرية. 27،625 هو أصغر دائرة نصف قطرها التي تلامس أكثر من 64. 154،136،450 ديه 35 ثلاث مرات فيثاغورس. القوى المثالية. كتابة برنامج PerfectPower. java لطباعة جميع القوى الكاملة التي يمكن أن تمثل كما 64 بت طويلة الأعداد الصحيحة: 4، 8، 9، 16، 25، 27. قوة مثالية هو عدد التي يمكن أن تكون مكتوبة كما أب لأعداد صحيحة و b غي 2. إضافات النقطة العائمة. إضافة ما يصل N أرقام نقطة عائمة، وتجنب خطأ جولة. حذف أصغر اثنين: إضافة اثنين من بعضها البعض، وإعادة إدراج. أولا-- صالح للتعبئة بن. 1710 أوبت 2، 119 أوبت 4 (دروسينغ). استخدام ماكس بطولة البطولة التي اللاعبين هي صناديق N والقيمة المتاحة القدرات. كومة مع مينماكس. تصميم نوع البيانات التي تدعم دفع، البوب، حجم، دقيقة، والحد الأقصى (حيث الحد الأدنى والحد الأقصى هي الحد الأدنى والحد الأقصى العناصر على المكدس). وينبغي أن تستغرق جميع العمليات وقتا ثابتا في أسوأ الحالات. تلميح: مشارك مع كل كومة دخول الحد الأدنى والحد الأقصى البنود الموجودة حاليا على المكدس. قائمة الانتظار مع مينماكس. قم بتصميم نوع بيانات يدعم إنكيو، ديكو، الحجم، مين، و ماكس (حيث الحد الأدنى والحد الأقصى هو الحد الأدنى والحد الأقصى للعناصر الموجودة في قائمة الانتظار). وينبغي أن تستغرق جميع العمليات وقتا مستهلكا ثابتا. تلميح: القيام التمرين السابق ومحاكاة قائمة الانتظار مع اثنين من مداخن. 2i 5j. أرقام الطباعة من النموذج 2i 5j في ترتيب متزايد. كومة الحد الأدنى. تصميم بنية بيانات تدعم الحد الأدنى والحد الأقصى في الوقت الثابت وإدراج وحذف دقيقة وحذف الحد الأقصى في الوقت لوغاريتمي عن طريق وضع العناصر في صفيف واحد من الحجم N مع الخصائص التالية: يمثل المصفوفة شجرة ثنائية كاملة. المفتاح في عقدة على مستوى متساوي أقل من (أو يساوي) مفاتيح في الشجرة الفرعية المفتاح في عقدة على مستوى فردي أكبر من (أو يساوي) مفاتيح في الشجرة الفرعية. لاحظ أن يتم تخزين القيمة القصوى في الجذر ويتم تخزين القيمة الدنيا في واحدة من الأطفال الجذور. مين-ماكس أكوام وقوائم الأولوية العامة المدى الحد الأدنى الاستعلام. وبالنظر إلى تسلسل من العناصر N، فإن الاستعلام الأدنى للنطاق من الفهرس i إلى j هو فهرس العنصر الأدنى بين i و j. تصميم بنية البيانات التي تعالج مسبقا تسلسل العناصر N في الوقت الخطي لدعم الاستعلامات الدنيا المدى في الوقت اللوغاريتمي. إثبات أن شجرة ثنائية كاملة مع العقد N لديها سقف بالضبط (N2) العقد ورقة (العقد مع عدم وجود أطفال). ماكس المنحى أولوية طابور مع دقيقة. ما هو ترتيب نمو وقت التشغيل للعثور على مفتاح الحد الأدنى في كومة الذاكرة القصوى - oriented. حل . لينارمداشث مفتاح الحد الأدنى يمكن أن يكون في أي من السقف (N2) العقد ورقة. ماكس المنحى أولوية طابور مع دقيقة. تصميم نوع البيانات التي تدعم إدراج وإزالة الحد الأقصى في الوقت اللوغاريتمي جنبا إلى جنب مع حد أقصى الحد الأدنى في الوقت المستمر. حل. إنشاء كومة ثنائي كحد أقصى المنحى وأيضا تخزين الحد الأدنى مفتاح إدراج حتى الآن (والتي لن تزيد أبدا ما لم يصبح هذا كومة فارغة). كث أكبر بند أكبر من x. وبالنظر إلى كومة الذاكرة المؤقتة القصوى الموجهة، تصميم خوارزمية لتحديد ما إذا كان أكبر كث البند أكبر من أو يساوي x. يجب تشغيل الخوارزمية في الوقت المناسب لك. حل . إذا كان المفتاح في العقدة أكبر من أو يساوي x، بشكل متكرر بحث كل من الشجرة الفرعية اليسرى والشجرة الفرعية اليمنى. وقف عندما يكون عدد العقدة المستكشف يساوي k (الجواب نعم) أو لا توجد المزيد من العقد لاستكشاف (لا). كث أصغر عنصر في كومة ثنائي الحد الأدنى المنحى. تصميم خوارزمية k لوغ k للعثور على أصغر عنصر كث في كومة ثنائية الموجة دقيقة H تحتوي على العناصر N. حل. بناء كومة جديدة دقيقة المنحى H. لن نقوم بتعديل H. إدراج جذر H إلى H جنبا إلى جنب مع مؤشر كومة 1. الآن، وحذف مرارا الحد الأدنى من البند س في H وإدراجها في H طفلين من س من H ، البند ك حذف من H هو أصغر كث البند في H. طابور عشوائية. تنفيذ راندومكيو بحيث يتم ضمان كل عملية لاتخاذ في معظم الوقت اللوغاريتمي. ملحوظة . لا يمكن تحمل مجموعة مضاعفة. لا توجد طريقة سهلة مع القوائم المرتبطة لتحديد عنصر عشوائي في O (1) الوقت. بدلا من ذلك، استخدم شجرة ثنائية كاملة مع وصلات صريحة. طابور فيفو مع الحذف العشوائي. تنفيذ نوع بيانات يدعم العمليات التالية: إدراج عنصر. حذف العنصر الذي تمت إضافته مؤخرا. وحذف عنصر عشوائي. يجب أن تأخذ كل عملية (على الأكثر) الوقت اللوغاريتمي في أسوأ الحالات. حل . استخدام شجرة ثنائية كاملة مع ارتباطات صريحة تعيين أولوية عدد صحيح طويلة i إلى العنصر الأول إضافة إلى بنية البيانات. أعلى k من صفيفتين مرتبة. وبالنظر إلى صفيفتين مرتبتين (أ) و (ب)، كل من الطول N، نجد أكبر كمية من شكل k أي. ملحوظة . باستخدام طابور الأولوية (على غرار مشكلة تاكسياب)، يمكنك تحقيق O (k لوغ N) خوارزمية. والمثير للدهشة، أنه من الممكن أن تفعل ذلك في O (ك) الوقت ولكن الخوارزمية معقدة. التحليل التجريبي لبناء كومة الذاكرة المؤقتة. مقارنة تجريبيا البناء خطي من أسفل إلى أعلى الوقت خطي مقابل البناء ساذج الوقت ليناريثميك من أعلى إلى أسفل البناء كومة. ومن المؤكد أن كومبراي على مدى مجموعة من قيم N. لاماركا و لادنر تقرير أنه بسبب ذاكرة التخزين المؤقت المحلية، خوارزمية ساذجة يمكن أن تؤدي بشكل أفضل في الممارسة العملية من نهج أكثر ذكاء لقيم كبيرة من N (عندما كومة لم يعد مناسبا في ذاكرة التخزين المؤقت) على الرغم من أن الأخير يؤدي العديد من المقارنة والتبادلات أقل. التحليل التجريبي للأكوام متعددة الطرق. تجريبيا مقارنة أداء 2-4 و 8-الطريق أكوام. يقترح لاماركا و لادنر العديد من التحسينات، مع الأخذ بعين الاعتبار آثار التخزين المؤقت. التحليل التجريبي لل هيبسورت. تجريبيا مقارنة أداء 2-4 و 8-طريقة هيبسورت. يقترح لاماركا و لادنر العديد من التحسينات، مع الأخذ بعين الاعتبار آثار التخزين المؤقت. وتشير بياناتهم إلى أن هيبسورت 8-واي الأمثل (وضبطها الذاكرة) يمكن أن يكون ضعف سرعة هيبسورت الكلاسيكية. تثبيط بواسطة الإدراج. لنفترض أنك بوليد كومة ثنائية على N مفاتيح عن طريق إدخال مرارا المفتاح التالي في كومة ثنائي. تبين أن العدد الإجمالي للمقارنات هو الأكثر إجابة. وعدد من يقارن هو في معظم لغ 1 لغ 2. لغ N لغ (N) N لغ N. هبيفي الحد الأدنى. (غونيت و مونرو) أن أي خوارزمية تستند إلى المقارنة لبناء كومة ثنائية على N مفاتيح يأخذ ما لا يقل عن 1.3644 N في أسوأ الحالات. إجابة . استخدام حجة نظرية المعلومات، علاء فرز الحد الأدنى. هناك N أكوام الممكنة (التقليب من الأعداد الصحيحة N) على N مفاتيح متميزة، ولكن هناك العديد من أكوام التي تتوافق مع نفس الترتيب. على سبيل المثال، هناك اثنين من أكوام (الكابينة و سبا) التي تتوافق مع 3 عناصر أسيسكو يوس جودة الخدمة حلول تكوين دليل، الإصدار 12.2 الازدحام تجنب نظرة عامة تقنيات تجنب الازدحام رصد حركة المرور الشبكة الأحمال في محاولة لاستباق وتجنب الازدحام في الشبكة المشتركة اختناقات. ويتحقق تجنب الازدحام من خلال إسقاط الرزم. ومن أكثر آليات تجنب الازدحام شيوعا هي الكشف المبكر العشوائي (ريد) الذي يعد مثاليا لشبكات العبور عالية السرعة. يتضمن سوس يوس جودة الخدمة تنفيذ ريد أنه عند تكوينه، يتحكم عندما يسقط الموجه الحزم. إذا لم تقم بتكوين الكشف المبكر العشوائي المرجح (وريد)، فإن الموجه يستخدم آلية إسقاط الحزمة الافتراضية كرودر التي تسمى قطرة الذيل. وللحصول على شرح لازدحام الشبكة، انظر الفصل كوتواليتي أوف سيرفيس أوفيرفيو. quot يقدم هذا الفصل وصفا موجزا لأنواع آليات تجنب الازدحام التي توفرها سوس يوس جودة الخدمة الميزات. ويناقش الميزات التالية: انخفاض الذيل. هذا هو السلوك الافتراضي لتجنب الازدحام عندما لم يتم تكوين وريد. WRED. وريد ووزعت وريد (دريد) وكلاهما تطبيقات سيسكو من ريدكومبين قدرات الخوارزمية الحمراء مع ميزة الأسبقية الملكية الفكرية. في القسم الخاص ب وريد، تتم مناقشة السمات ذات الصلة التالية: وريد القائم على التدفق. ويمتد وريد القائم على تدفق وريد لتوفير مزيد من العدالة لجميع التدفقات على واجهة فيما يتعلق كيفية إسقاط الحزم. ديفيسير المتوافقة وريد. ديفيسير المتوافقة وريد يمتد وريد لدعم خدمات متباينة (ديفسيرف) والتأمين المضمون (أف) في سلوك هوب (فب). وتمكن هذه الميزة العملاء من تنفيذ أف فب عن طريق تلوين الرزم وفقا لقيم نقطة الخدمات المتميزة (دسكب) ومن ثم تعيين احتمالات انخفاض تفضيلية لتلك الرزم. للحصول على معلومات حول كيفية تكوين وريد، دريد، وريد القائم على تدفق، و ديفيسير المتوافقة وريد، راجع الفصل كوكونفيغورينغ الكشف المبكر العشوائي المرجح في هذا الكتاب. ذيل إسقاط يعامل كل حركة المرور على قدم المساواة ولا يفرق بين فئات الخدمة. طوابير ملء خلال فترات الازدحام. عندما يكون طابور الإخراج ممتلئا ويكون قطرة الذيل سارية المفعول، يتم إسقاط الرزم حتى يتم القضاء على الازدحام ولم يعد الطابور كاملا. الكشف المبكر العشوائي المرجح يقدم هذا القسم مقدمة موجزة لمفاهيم ريد ويعالج وريد، وتنفيذ سيسكو من ريد لمنصات سيسكو يوس القياسية. وريد يتجنب مشاكل العولمة التي تحدث عندما يتم استخدام انخفاض الذيل كآلية تجنب الازدحام على جهاز التوجيه. يحدث المزامنة العالمية كموجات من ازدحام قمة تليها فقط القيعان التي لا يتم خلالها استخدام وصلة الإرسال بشكل كامل. يمكن أن يحدث المزامنة العالمية لمضيفات تكب، على سبيل المثال، بسبب إسقاط الحزم في آن واحد. ويظهر المزامنة العالمية عندما تستضيف مضيفات تكب متعددة معدلات انتقالها استجابة لإسقاط الرزم، ثم تزيد معدلات انتقالها مرة أخرى عند تقليل الازدحام. حول الكشف المبكر العشوائي تم اقتراح آلية ريد من قبل سالي فلويد وفان جاكوبسون في أوائل 1990s لمعالجة الازدحام الشبكة بطريقة استجابة بدلا من رد الفعل. تقوم آلية ريد على فرضية أن معظم حركة المرور تعمل على تطبيقات نقل البيانات التي تكون حساسة للخسارة وسوف تبطئ مؤقتا عندما يتم إسقاط بعض حركة المرور الخاصة بهم. تكب، الذي يستجيب بشكل مناسب إلى انخفاض حركة المرور عن طريق تباطؤ انتقال حركة المرور، ويسمح على نحو فعال سلوك حركة المرور انخفاض من ريد للعمل كآلية إشارة الازدحام لتجنب. ويشكل برنامج التعاون الفني النقل الشبكي الأكثر استخداما. ونظرا لوجود برنامج التعاون الفني في كل مكان، فإن ريد يوفر آلية واسعة وفعالة لتفادي الازدحام. ولدى النظر في فائدة ريد عندما تكون وسائل النقل القوية مثل برنامج التعاون الفني منتشرة، من المهم النظر أيضا في الآثار السلبية الخطيرة لاستخدام ريد عندما لا تكون نسبة مئوية كبيرة من الحركة قوية استجابة لفقدان الرزم. لا نوفيل نيتوار ولا أبلتالك قوية بشكل مناسب ردا على فقدان الحزمة، وبالتالي يجب أن لا تستخدم الأحمر بالنسبة لهم. How It Works RED aims to control the average queue size by indicating to the end hosts when they should temporarily slow down transmission of packets. RED takes advantage of the congestion control mechanism of TCP. By randomly dropping packets prior to periods of high congestion, RED tells the packet source to decrease its transmission rate. Assuming the packet source is using TCP, it will decrease its transmission rate until all the packets reach their destination, indicating that the congestion is cleared. You can use RED as a way to cause TCP to slow down transmission of packets. TCP not only pauses, but it also restarts quickly and adapts its transmission rate to the rate that the network can support. RED distributes losses in time and maintains normally low queue depth while absorbing spikes. When enabled on an interface, RED begins dropping packets when congestion occurs at a rate you select during configuration. For an explanation of how the Cisco WRED implementation determines parameters to use in the WRED queue size calculations and how to determine optimum values to use for the weight factor, see the section quotAverage Queue Sizequot later in this chapter. Packet Drop Probability The packet drop probability is based on the minimum threshold, maximum threshold, and mark probability denominator. When the average queue depth is above the minimum threshold, RED starts dropping packets. The rate of packet drop increases linearly as the average queue size increases until the average queue size reaches the maximum threshold. The mark probability denominator is the fraction of packets dropped when the average queue depth is at the maximum threshold. For example, if the denominator is 512, one out of every 512 packets is dropped when the average queue is at the maximum threshold. When the average queue size is above the maximum threshold, all packets are dropped. Figure 9 summarizes the packet drop probability. Figure 9 RED Packet Drop Probability The minimum threshold value should be set high enough to maximize the link utilization. If the minimum threshold is too low, packets may be dropped unnecessarily, and the transmission link will not be fully used. The difference between the maximum threshold and the minimum threshold should be large enough to avoid global synchronization of TCP hosts (global synchronization of TCP hosts can occur as multiple TCP hosts reduce their transmission rates). If the difference between the maximum and minimum thresholds is too small, many packets may be dropped at once, resulting in global synchronization. How TCP Handles Traffic Loss Note The sections quotHow TCP Handles Traffic Lossquot and quotHow the Router Interacts with TCPquot contain detailed information that you need not read in order to use WRED or to have a general sense of the capabilities of RED. If you want to understand why problems of global synchronization occur in response to congestion when tail drop is used by default and how RED addresses them, read these sections. When the recipient of TCP trafficcalled the receiverreceives a data segment, it checks the four octet (32-bit) sequence number of that segment against the number the receiver expected, which would indicate that the data segment was received in order. If the numbers match, the receiver delivers all of the data that it holds to the target application, then it updates the sequence number to reflect the next number in order, and finally it either immediately sends an acknowledgment (ACK) packet to the sender or it schedules an ACK to be sent to the sender after a short delay. The ACK notifies the sender that the receiver received all data segments up to but not including the one marked with the new sequence number. Receivers usually try to send an ACK in response to alternating data segments they receive they send the ACK because for many applications, if the receiver waits out a small delay, it can efficiently include its reply acknowledgment on a normal response to the sender. However, when the receiver receives a data segment out of order, it immediately responds with an ACK to direct the sender to resend the lost data segment. When the sender receives an ACK, it makes this determination: It determines if any data is outstanding. If no data is outstanding, the sender determines that the ACK is a keepalive, meant to keep the line active, and it does nothing. If data is outstanding, the sender determines whether the ACK indicates that the receiver has received some or none of the data. If the ACK indicates receipt of some data sent, the sender determines if new credit has been granted to allow it to send more data. When the ACK indicates receipt of none of the data sent and there is outstanding data, the sender interprets the ACK to be a repeatedly sent ACK. This condition indicates that some data was received out of order, forcing the receiver to remit the first ACK, and that a second data segment was received out of order, forcing the receiver to remit the second ACK. In most cases, the receiver would receive two segments out of order because one of the data segments had been dropped. When a TCP sender detects a dropped data segment, it resends the segment. Then it adjusts its transmission rate to half of what is was before the drop was detected. This is the TCP back-off or slow-down behavior. Although this behavior is appropriately responsive to congestion, problems can arise when multiple TCP sessions are carried on concurrently with the same router and all TCP senders slow down transmission of packets at the same time. How the Router Interacts with TCP Note The sections quotHow TCP Handles Traffic Lossquot and quotHow the Router Interacts with TCPquot contain detailed information that you need not read in order to use WRED or to have a general sense of the capabilities of RED. If you want to understand why problems of global synchronization occur in response to congestion when tail drop is used by default and how RED addresses them, read these sections. To see how the router interacts with TCP, we will look at an example. In this example, on average, the router receives traffic from one particular TCP stream every other, every 10th, and every 100th or 200th message in the interface in MAE-EAST or FIX-WEST. A router can handle multiple concurrent TCP sessions. Because network flows are additive, there is a high probability that when traffic exceeds the Transmit Queue Limit (TQL) at all, it will vastly exceed the limit. However, there is also a high probability that the excessive traffic depth is temporary and that traffic will not stay excessively deep except at points where traffic flows merge or at edge routers. If the router drops all traffic that exceeds the TQL, as is done when tail drop is used by default, many TCP sessions will simultaneously go into slow start. Consequently, traffic temporarily slows down to the extreme and then all flows slow-start again this activity creates a condition of global synchronization. However, if the router drops no traffic, as is the case when queueing features such as fair queueing or custom queueing (CQ) are used, then the data is likely to be stored in main memory, drastically degrading router performance. By directing one TCP session at a time to slow down, RED solves the problems described, allowing for full utilization of the bandwidth rather than utilization manifesting as crests and troughs of traffic. About WRED WRED combines the capabilities of the RED algorithm with the IP Precedence feature to provide for preferential traffic handling of higher priority packets. WRED can selectively discard lower priority traffic when the interface begins to get congested and provide differentiated performance characteristics for different classes of service. You can configure WRED to ignore IP precedence when making drop decisions so that nonweighted RED behavior is achieved. For interfaces configured to use the Resource Reservation Protocol (RSVP) feature, WRED chooses packets from other flows to drop rather than the RSVP flows. Also, IP Precedence governs which packets are droppedtraffic that is at a lower precedence has a higher drop rate and therefore is more likely to be throttled back. WRED differs from other congestion avoidance techniques such as queueing strategies because it attempts to anticipate and avoid congestion rather than control congestion once it occurs. Why Use WRED WRED makes early detection of congestion possible and provides for multiple classes of traffic. It also protects against global synchronization. For these reasons, WRED is useful on any output interface where you expect congestion to occur. However, WRED is usually used in the core routers of a network, rather than at the edge of the network. Edge routers assign IP precedences to packets as they enter the network. WRED uses these precedences to determine how to treat different types of traffic. WRED provides separate thresholds and weights for different IP precedences, allowing you to provide different qualities of service in regard to packet dropping for different traffic types. Standard traffic may be dropped more frequently than premium traffic during periods of congestion. WRED is also RSVP-aware, and it can provide the controlled-load QoS service of integrated service. How It Works By randomly dropping packets prior to periods of high congestion, WRED tells the packet source to decrease its transmission rate. If the packet source is using TCP, it will decrease its transmission rate until all the packets reach their destination, which indicates that the congestion is cleared. WRED generally drops packets selectively based on IP precedence. Packets with a higher IP precedence are less likely to be dropped than packets with a lower precedence. Thus, the higher the priority of a packet, the higher the probability that the packet will be delivered. WRED reduces the chances of tail drop by selectively dropping packets when the output interface begins to show signs of congestion. By dropping some packets early rather than waiting until the queue is full, WRED avoids dropping large numbers of packets at once and minimizes the chances of global synchronization. Thus, WRED allows the transmission line to be used fully at all times. In addition, WRED statistically drops more packets from large users than small. Therefore, traffic sources that generate the most traffic are more likely to be slowed down than traffic sources that generate little traffic. WRED avoids the globalization problems that occur when tail drop is used as the congestion avoidance mechanism. Global synchronization manifests when multiple TCP hosts reduce their transmission rates in response to packet dropping, then increase their transmission rates once again when the congestion is reduced. WRED is only useful when the bulk of the traffic is TCPIP traffic. With TCP, dropped packets indicate congestion, so the packet source will reduce its transmission rate. With other protocols, packet sources may not respond or may resend dropped packets at the same rate. Thus, dropping packets does not decrease congestion. WRED treats non-IP traffic as precedence 0, the lowest precedence. Therefore, non-IP traffic, in general, is more likely to be dropped than IP traffic. Figure 10 illustrates how WRED works. Figure 10 Weighted Random Early Detection Average Queue Size The router automatically determines parameters to use in the WRED calculations. The average queue size is based on the previous average and the current size of the queue. The formula is: where n is the exponential weight factor, a user-configurable value. For high values of n . the previous average becomes more important. A large factor smooths out the peaks and lows in queue length. The average queue size is unlikely to change very quickly, avoiding drastic swings in size. The WRED process will be slow to start dropping packets, but it may continue dropping packets for a time after the actual queue size has fallen below the minimum threshold. The slow-moving average will accommodate temporary bursts in traffic. Note If the value of n gets too high, WRED will not react to congestion. Packets will be sent or dropped as if WRED were not in effect. For low values of n . the average queue size closely tracks the current queue size. The resulting average may fluctuate with changes in the traffic levels. In this case, the WRED process responds quickly to long queues. Once the queue falls below the minimum threshold, the process will stop dropping packets. If the value of n gets too low, WRED will overreact to temporary traffic bursts and drop traffic unnecessarily. Restrictions You cannot configure WRED on the same interface as Route Switch Processor (RSP)-based CQ, priority queueing (PQ), or weighted fair queueing (WFQ). Distributed Weighted Random Early Detection Distributed WRED (DWRED) is an implementation of WRED for the Versatile Interface Processor (VIP). DWRED provides the complete set of functions for the VIP that WRED provides on standard Cisco IOS platforms. The DWRED feature is only supported on Cisco 7000 series routers with an RSP-based RSP7000 interface processor and Cisco 7500 series routers with a VIP-based VIP2-40 or greater interface processor. A VIP2-50 interface processor is strongly recommended when the aggregate line rate of the port adapters on the VIP is greater than DS3. A VIP2-50 interface processor is required for OC-3 rates. DWRED is configured the same way as WRED. If you enable WRED on a suitable VIP interface, such as a VIP2-40 or greater with at least 2 MB of SRAM, DWRED will be enabled instead. In order to use DWRED, distributed Cisco Express Forwarding (dCEF) switching must be enabled on the interface. For information about dCEF, refer to the Cisco IOS Switching Services Configuration Guide and the Cisco IOS Switching Services Command Reference. You can configure both DWRED and distributed weighted fair queueing (DWFQ) on the same interface, but you cannot configure distributed WRED on an interface for which RSP-based CQ, PQ, or WFQ is configured. You can enable DWRED using the Modular Quality of Service Command-Line Interface (Modular QoS CLI) feature. For complete conceptual and configuration information on the Modular QoS CLI feature, see the chapter quotModular Quality of Service Command-Line Interface Overviewquot of this book. How It Works When a packet arrives and DWRED is enabled, the following events occur: The average queue size is calculated. See the quotAverage Queue Sizequot section for details. If the average is less than the minimum queue threshold, the arriving packet is queued. If the average is between the minimum queue threshold and the maximum queue threshold, the packet is either dropped or queued, depending on the packet drop probability. See the quotPacket-Drop Probabilityquot section for details. If the average queue size is greater than the maximum queue threshold, the packet is automatically dropped. Average Queue Size The average queue size is based on the previous average and the current size of the queue. The formula is: where n is the exponential weight factor, a user-configurable value. For high values of n. the previous average queue size becomes more important. A large factor smooths out the peaks and lows in queue length. The average queue size is unlikely to change very quickly, avoiding drastic swings in size. The WRED process will be slow to start dropping packets, but it may continue dropping packets for a time after the actual queue size has fallen below the minimum threshold. The slow-moving average will accommodate temporary bursts in traffic. Note If the value of n gets too high, WRED will not react to congestion. Packets will be sent or dropped as if WRED were not in effect. For low values of n. the average queue size closely tracks the current queue size. The resulting average may fluctuate with changes in the traffic levels. In this case, the WRED process responds quickly to long queues. Once the queue falls below the minimum threshold, the process stops dropping packets. If the value of n gets too low, WRED will overreact to temporary traffic bursts and drop traffic unnecessarily. Packet-Drop Probability The probability that a packet will be dropped is based on the minimum threshold, maximum threshold, and mark probability denominator. When the average queue size is above the minimum threshold, RED starts dropping packets. The rate of packet drop increases linearly as the average queue size increases, until the average queue size reaches the maximum threshold. The mark probability denominator is the fraction of packets dropped when the average queue size is at the maximum threshold. For example, if the denominator is 512, one out of every 512 packets is dropped when the average queue is at the maximum threshold. When the average queue size is above the maximum threshold, all packets are dropped. Figure 11 summarizes the packet drop probability. Figure 11 Packet Drop Probability The minimum threshold value should be set high enough to maximize the link utilization. If the minimum threshold is too low, packets may be dropped unnecessarily, and the transmission link will not be fully used. The difference between the maximum threshold and the minimum threshold should be large enough to avoid global synchronization of TCP hosts (global synchronization of TCP hosts can occur as multiple TCP hosts reduce their transmission rates). If the difference between the maximum and minimum thresholds is too small, many packets may be dropped at once, resulting in global synchronization. Why Use DWRED DWRED provides faster performance than does RSP-based WRED. You should run DWRED on the VIP if you want to achieve very high speed on the Cisco 7500 series platformfor example, you can achieve speed at the OC-3 rates by running WRED on a VIP2-50 interface processor. Additionally, the same reasons you would use WRED on standard Cisco IOS platforms apply to using DWRED. (See the section quotWhy Use WREDquot earlier in this chapter.) For instance, when WRED or DWRED is not configured, tail drop is enacted during periods of congestion. Enabling DWRED obviates the global synchronization problems that result when tail drop is used to avoid congestion. The DWRED feature provides the benefit of consistent traffic flows. When RED is not configured, output buffers fill during periods of congestion. When the buffers are full, tail drop occurs all additional packets are dropped. Because the packets are dropped all at once, global synchronization of TCP hosts can occur as multiple TCP hosts reduce their transmission rates. The congestion clears, and the TCP hosts increase their transmission rates, resulting in waves of congestion followed by periods when the transmission link is not fully used. RED reduces the chances of tail drop by selectively dropping packets when the output interface begins to show signs of congestion. By dropping some packets early rather than waiting until the buffer is full, RED avoids dropping large numbers of packets at once and minimizes the chances of global synchronization. Thus, RED allows the transmission line to be used fully at all times. In addition, RED statistically drops more packets from large users than small. Therefore, traffic sources that generate the most traffic are more likely to be slowed down than traffic sources that generate little traffic. DWRED provides separate thresholds and weights for different IP precedences, allowing you to provide different qualities of service for different traffic. Standard traffic may be dropped more frequently than premium traffic during periods of congestion. Restrictions The following restrictions apply to the DWRED feature: Interface-based DWRED cannot be configured on a subinterface. (A subinterface is one of a number of virtual interfaces on a single physical interface.) DWRED is not supported on Fast EtherChannel and tunnel interfaces. RSVP is not supported on DWRED. DWRED is useful only when the bulk of the traffic is TCPIP traffic. With TCP, dropped packets indicate congestion, so the packet source reduces its transmission rate. With other protocols, packet sources may not respond or may resend dropped packets at the same rate. Thus, dropping packets does not necessarily decrease congestion. DWRED treats non-IP traffic as precedence 0, the lowest precedence. Therefore, non-IP traffic is usually more likely to be dropped than IP traffic. DWRED cannot be configured on the same interface as RSP-based CQ, PQ, or WFQ. However, both DWRED and DWFQ can be configured on the same interface. Note Do not use the match protocol command to create a traffic class with a non-IP protocol as a match criterion. The VIP does not support matching of non-IP protocols. Prerequisites This section provides the prerequisites that must be met before you configure the DWRED feature. Weighted Fair Queueing Attaching a service policy to an interface disables WFQ on that interface if WFQ is configured for the interface. For this reason, you should ensure that WFQ is not enabled on such an interface before configuring DWRED. Attaching a service policy configured to use WRED to an interface disables WRED on that interface. If any of the traffic classes that you configure in a policy map use WRED for packet drop instead of tail drop, you must ensure that WRED is not configured on the interface to which you intend to attach that service policy. Access Control Lists You can specify a numbered access list as the match criterion for any traffic class that you create. For this reason, before configuring DWRED you should know how to configure access lists. Cisco Express Forwarding In order to use DWRED, dCEF switching must be enabled on the interface. For information on dCEF, refer to the Cisco IOS Switching Services Configuration Guide . Flow-Based WRED Flow-based WRED is a feature that forces WRED to afford greater fairness to all flows on an interface in regard to how packets are dropped. Why Use Flow-Based WRED Before you consider the advantages that use of flow-based WRED offers, it helps to think about how WRED (without flow-based WRED configured) affects different kinds of packet flows. Even before flow-based WRED classifies packet flows, flows can be thought of as belonging to one of the following categories: Nonadaptive flows, which are flows that do not respond to congestion. Robust flows, which on average have a uniform data rate and slow down in response to congestion. Fragile flows, which, though congestion-aware, have fewer packets buffered at a gateway than do robust flows. WRED tends toward bias against fragile flows because all flows, even those with relatively fewer packets in the output queue, are susceptible to packet drop during periods of congestion. Though fragile flows have fewer buffered packets, they are dropped at the same rate as packets of other flows. To provide fairness to all flows, flow-based WRED has the following features: It ensures that flows that respond to WRED packet drops (by backing off packet transmission) are protected from flows that do not respond to WRED packet drops. It prohibits a single flow from monopolizing the buffer resources at an interface. How It Works Flow-based WRED relies on the following two main approaches to remedy the problem of unfair packet drop: It classifies incoming traffic into flows based on parameters such as destination and source addresses and ports. It maintains state about active flows, which are flows that have packets in the output queues. Flow-based WRED uses this classification and state information to ensure that each flow does not consume more than its permitted share of the output buffer resources. Flow-based WRED determines which flows monopolize resources and it more heavily penalizes these flows. To ensure fairness among flows, flow-based WRED maintains a count of the number of active flows that exist through an output interface. Given the number of active flows and the output queue size, flow-based WRED determines the number of buffers available per flow. To allow for some burstiness, flow-based WRED scales the number of buffers available per flow by a configured factor and allows each active flow to have a certain number of packets in the output queue. This scaling factor is common to all flows. The outcome of the scaled number of buffers becomes the per-flow limit. When a flow exceeds the per-flow limit, the probability that a packet from that flow will be dropped increases. DiffServ Compliant WRED DiffServ Compliant WRED extends the functionality of WRED to enable support for DiffServ and AF Per Hop Behavior PHB. This feature enables customers to implement AF PHB by coloring packets according to DSCP values and then assigning preferential drop probabilities to those packets. Note This feature can be used with IP packets only. It is not intended for use with Multiprotocol Label Switching (MPLS)-encapsulated packets. The Class-Based Quality of Service MIB supports this feature. This MIB is actually the following two MIBs: The DiffServ Compliant WRED feature supports the following RFCs: RFC 2474, Definition of the Differentiated Services Field (DS Field) in the IPv4 and IPv6 Headers RFC 2475, An Architecture for Differentiated Services Framework RFC 2597, Assured Forwarding PHB RFC 2598, An Expedited Forwarding PHB How It Works The DiffServ Compliant WRED feature enables WRED to use the DSCP value when it calculates the drop probability for a packet. The DSCP value is the first six bits of the IP type of service (ToS) byte. This feature adds two new commands, random-detect dscp and dscp . It also adds two new arguments, dscp-based and prec-based . to two existing WRED-related commandsthe random-detect (interface) command and the random-detect-group command. The dscp-based argument enables WRED to use the DSCP value of a packet when it calculates the drop probability for the packet. The prec-based argument enables WRED to use the IP Precedence value of a packet when it calculates the drop probability for the packet. These arguments are optional (you need not use any of them to use the commands) but they are also mutually exclusive. That is, if you use the dscp-based argument, you cannot use the prec-based argument with the same command. After enabling WRED to use the DSCP value, you can then use the new random-detect dscp command to change the minimum and maximum packet thresholds for that DSCP value. Three scenarios for using these arguments are provided. Usage Scenarios The new dscp-based and prec-based arguments can be used whether you are using WRED at the interface level, at the per-virtual circuit (VC) level, or at the class level (as part of class-based WFQ (CBWFQ) with policy maps). WRED at the Interface Level At the interface level, if you want to have WRED use the DSCP value when it calculates the drop probability, you can use the dscp-based argument with the random-detect (interface) command to specify the DSCP value. Then use the random-detect dscp command to specify the minimum and maximum thresholds for the DSCP value. WRED at the per-VC Level At the per-VC level, if you want to have WRED use the DSCP value when it calculates the drop probability, you can use the dscp-based argument with the random-detect-group command. Then use the dscp command to specify the minimum and maximum thresholds for the DSCP value or the mark-probability denominator. This configuration can then be applied to each VC in the network. WRED at the Class Level If you are using WRED at the class level (with CBWFQ), the dscp-based and prec-based arguments can be used within the policy map. First, specify the policy map, the class, and the bandwidth. Then, if you want WRED to use the DSCP value when it calculates the drop probability, use the dscp-based argument with the random-detect (interface) command to specify the DSCP value. Then use the random-detect dscp command to modify the default minimum and maximum thresholds for the DSCP value. This configuration can then be applied wherever policy maps are attached (for example, at the interface level, the per-VC level, or the shaper level). Usage Points to Note Remember the following points when using the new commands and the new arguments included with this feature: If you use the dscp-based argument, WRED will use the DSCP value to calculate the drop probability. If you use the prec-based argument, WRED will use the IP Precedence value to calculate the drop probability. The dscp-based and prec-based arguments are mutually exclusive. If you do not specify either argument, WRED will use the IP Precedence value to calculate the drop probability (the default method). The random-detect dscp command must be used in conjunction with the random-detect (interface) command. The random-detect dscp command can only be used if you use the dscp-based argument with the random-detect (interface) command. The dscp command must be used in conjunction with the random-detect-group command. The dscp command can only be used if you use the dscp-based argument with the random-detect-group command. For more information about using these commands, refer to the Cisco IOS Quality of Service Command Reference. The purpose of this document is to describe how IOs are queued the disk device driver and the adapter device driver, SDD, and SDDPCM and to explain how these can be tuned to increase performance. And this also includes the use of VIO. This information is also useful for AIX LPARs using other multi-path code as well. Where this stuff fits in the IO stack Following is the IO stack from the application to the disk: Application File system (optional) LVM device driver (optional) SDD or SDDPCM or other multi-path driver (if used) hdisk device driver adapter device driver interconnect to the disk Disk subsystem Disk Note that even though the disk is attached to the adapter, the hdisk driver code is utilized before the adapter driver code. So this stack represents the order software comes into play over time as the IO traverses the stack. Why do we need to simultaneously submit more than one IO to a disk This improves performance. And this would be performance from an applications point of view. This is especially important with disk subsystems where a virtual disk (or LUN) is backed by multiple physical disks. In such a situation, if we only could submit a single IO at a time, wed find we get good IO service times, but very poor thruput. Submitting multiple IOs to a physical disk allows the disk to minimize actuator movement (using an elevator algorithm) and get more IOPS than is possible by submitting one IO at a time. The elevator analogy is appropriate. How long would people be waiting to use an elevator if only one person at a time could get on it In such a situation, wed expect that people would wait quite a while to use the elevator (queueing time), but once they got on it, theyd get to their destination quickly (service time). So submitting multiple in-flight IOs to a disk subsystem allows it to figure out how to get the most thruput and fastest overall IO service time. Theoretically, the IOPS for a disk is limited by queuedepth(average IO service time). Assuming a queuedepth of 3, and an average IO service time of 10 ms, this yields a maximum thruput of 300 IOPS for the hdisk. And for many applications this may not be enough thruput. Where are IOs queued As IOs traverse the IO stack, AIX needs to keep track of them at each layer. So IOs are essentially queued at each layer. Generally, some number of in flight IOs may be issued at each layer and if the number of IO requests exceeds that number, they reside in a wait queue until the required resource becomes available. So there is essentially an in process queue and a wait queue at each layer (SDD and SDDPCM are a little more complicated). At the file system layer, file system buffers limit the maximum number of in flight IOs for each file system. At the LVM device driver layer, hdisk buffers limit the number of in flight IOs. At the SDD layer, IOs are queued if the dpo devices attribute, qdepthenable, is set to yes (which it is by default). Some releases of SDD do not queue IOs so it depends on the release of SDD. SDDPCM on the other hand does not queue IOs before sending them to the disk device driver. The hdisks have a maximum number of in flight IOs thats specified by its queuedepth attribute. And FC adapters also have a maximum number of in flight IOs specified by numcmdelems. The disk subsystems themselves queue IOs and individual physical disks can accept multiple IO requests but only service one at a time. Here are an ESS hdisks attributes: lsattr - El hdisk33 PRkeyvalue none Reserve Key True location Location Label True lunid 0x5515000000000000 Logical Unit Number ID True lunresetspt yes Support SCSI LUN reset True maxtransfer 0x40000 NA True nodename 0x5005076300c096ab FC Node Name False pvid none Physical volume identifier False qtype simple Queuing TYPE True qfulldly 20 delay in seconds for SCSI TASK SET FULL True queuedepth 20 Queue DEPTH True reservepolicy singlepath Reserve Policy True rwtimeout 60 READWRITE time out value True scbsydly 20 delay in seconds for SCSI BUSY True scsiid 0x620713 SCSI ID True starttimeout 180 START unit time out value True wwname 0x5005076300cd96ab FC World Wide Name False The default queuedepth is 20, but can be changed to as high as 256 for ESS, DS6000 and DS8000. One can display allowable values with: lsattr - Rl hdisk33 - a queuedepth 1. 256 (1) indicating the value can be anywhere from 1 to 256 in increments of 1. One can use this command to see any allowable value for attributes which can be changed (showing a value of True in the last field of lsattr - El ltdevicegt for the device using: lsattr - Rl ltdevicegt - a ltattributegt Heres a FC adapters attributes: lsattr - El fcs0 busintrlvl 65703 Bus interrupt level False busioaddr 0xdec00 Bus IO address False busmemaddr 0xe8040000 Bus memory address False initlink al INIT Link flags True intrpriority 3 Interrupt priority False lgtermdma 0x800000 Long term DMA True maxxfersize 0x100000 Maximum Transfer Size True numcmdelems 200 Maximum number of COMMANDS to queue to the adapter True prefalpa 0x1 Preferred ALPA True swfcclass 2 FC Class for Fabric True The default queue depth (numcmdelems) for FC adapters is 200 but can be increased up to 2048 for most adapters. Heres the dpo devices attributes for one release of SDD: lsattr - El dpo Enterprmaxlun 600 Maximum LUNS allowed for Enterprise Products True Virtualmaxlun 512 Maximum LUNS allowed for Virtualization Products False persistentresv yes Subsystem Supports Persistent Reserve Command False qdepthenable yes Queue Depth Control True When qdepthenableyes, SDD will only submit queuedepth IOs to any underlying hdisk (where queuedepth here is the value for the underlying hdisks queuedepth attribute). When qdepthenableno, SDD just passes on the IOs directly to the hdisk driver. So the difference is, if qdepthenableyes (the default), IOs exceeding the queuedepth will queue at SDD, and if qdepthenableno, then IOs exceed the queuedepth will queue in the hdisks wait queue. In other words, SDD with qdepthenableno and SDDPCM do not queue IOs and instead just pass them to the hdisk drivers. Note that at SDD 1.6, its preferable to use the datapath command to change qdepthenable, rather than using chdev, as then its a dynamic change, e. g. datapath set qdepth disable will set it to no. Some releases of SDD dont include SDD queueing, and some do, and some releases dont show the qdepthenable attribute. Either check the manual for your version of SDD or try the datapath command to see if it supports turning this feature off. If youve used both SDD and SDDPCM, youll remember that with SDD, each LUN has a corresponding vpath and an hdisk for each path to the vpath or LUN. And with SDDPCM, you just have one hdisk per LUN. Thus, with SDD one can submit queuedepth x paths to a LUN, while with SDDPCM, one can only submit queuedepth IOs to the LUN. If you switch from SDD using 4 paths to SDDPCM, then youd want to set the SDDPCM hdisks to 4x that of SDD hdisks for an equivalent effective queue depth. And migrating to SDDPCM is recommended as its more strategic than SDD. Both the hdisk and adapter drivers have an in process and wait queues. Once the queue limit is reached, the IOs wait until an IO completes, freeing up a slot in the service queue. The in process queue is also sometimes referred to as the service queue Its worth mentioning, that many applications will not generate many in flight IOs, especially single threaded applications that dont use asynchronous IO. Applications that use asynchronous IO are likely to generate more in flight IOs. What tools are available to monitor the queues For AIX, one can use iostat (at AIX 5.3 or later) and sar (5.1 or later) to monitor the hdisk driver queues. The iostat - D command generates output such as: hdisk6 xfer: tmact bps tps bread bwrtn 4.7 2.2M 19.0 0.0 2.2M read: rps avgserv minserv maxserv timeouts fails 0.0 0.0 0.0 0.0 0 0 write: wps avgserv minserv maxserv timeouts fails 19.0 38.9 1.1 190.2 0 0 queue: avgtime mintime maxtime avgwqsz avgsqsz sqfull 15.0 0.0 83.7 0.0 0.0 136 Here, the avgwqsz is the average wait queue size, and avgsqsz is the average service queue size. The average time spent in the wait queue is avgtime. The sqfull value has changed from initially being a count of the times weve submitted an IO to a full queue, to now where its the rate of IOs per second submitted to a full queue. The example report shows the prior case (a count of IOs submitted to a full queue), while newer releases typically show decimal fractions indicating a rate. Its nice that iostat - D separates reads and writes, as we would expect the IO service times to be different when we have a disk subsystem with cache. The most useful report for tuning is just running iostat - D which shows statistics since system boot, assuming the system is configured to continuously maintain disk IO history (run lsattr - El sys0. or smitty chgsys to see if the iostat attribute is set to true). And the authors favorite iostat command flags are iostat - RDTl ltintervalgt ltintervalsgt. From the applications point of view, the length of time to do an IO is its service time plus the time it waits in the hdisk wait queue. The sar - d command changed at AIX 5.3, and generates output such as: 16:50:59 device busy avque rws Kbss avwait avserv 16:51:00 hdisk1 0 0.0 0 0 0.0 0.0 hdisk0 0 0.0 0 0 0.0 0.0 The avwait and avserv are the average times spent in the wait queue and service queue respectively. And avserv here would correspond to avgserv in the iostat output. The avque value changed at AIX 5.3, it represents the average number of IOs in the wait queue, and prior to 5.3, it represents the average number of IOs in the service queue. SDDPCM provides the pcmpath query devstats and pcmpath query adaptstats commands to show hdisk and adapter queue statistics. SDD similarly has datapath query devstats and datapath query adaptstats. You can refer to the SDDSDDPCM manual for syntax, options and explanations of all the fields. Heres some devstats output for a single LUN: Device : 0 Total Read Total Write Active Read Active Write Maximum IO: 29007501 3037679 1 0 40 SECTOR: 696124015 110460560 8 0 20480 Transfer Size: lt 512 lt 4k lt 16K lt 64K gt 64K 21499 10987037 18892010 1335598 809036 and heres some adaptstats output: Adapter : 0 Total Read Total Write Active Read Active Write Maximum IO: 439690333 24726251 7 0 258 SECTOR: 109851534 960137182 608 0 108625 Here, were mainly interested in the Maximum field which indicates the maximum number of IOs submitted to the device since system boot. For SDD, the Maximum for devstats will not exceed queuedepth x paths when qdepthenableyes. But Maximum for adaptstats can exceed numcmdelems as it represents the maximum number of IOs submitted to the adapter driver and includes IOs for both the service and wait queues. If, in this case, we have 2 paths and are using the default queuedepth of 20, then the 40 indicates weve filled the queue at least once and increasing queuedepth can help performance. For SDDPCM, if the Maximum value equals the hdisks queuedepth, then the hdisk driver queue was filled during the interval, and increasing queuedepth is usually appropriate. One can similarly monitor adapter IOPS with iostat - at ltintervalgt lt of intervalsgt and for adapter queue information, run iostat - aD. optionally with an interval and number of intervals. For FC adapters, the fcstat command provides information on the adapter queue and resource use, and can tell us if we need to increase its queue sizes. For adapter queues, the fcstat command is used and is discussed below. First, one should not indiscriminately just increase these values. Its possible to overload the disk subsystem or cause problems with device configuration at boot. So the approach of adding up the hdisks queuedepths and using that to determine the numcmdelems isnt necessarily the best approach. Instead, its better to use the maximum number of submitted IOs to each device for tuning. When you increase the queuedepths and number of in flight IOs that are sent to the disk subsystem, the IO service times are likely to increase, but throughput will also increase. If IO service times start approaching the disk timeout value, then youre submitting more IOs than the disk subsystem can handle. If you start seeing IO timeouts and errors in the error log indicating problems completing IOs, then this is the time to look for hardware problems or to make the pipe smaller. A good general rule for tuning queuedepths, is that one can increase queuedepths until IO service times start exceeding 15 ms for small random reads or writes or one isnt filling the queues. Once IO service times start increasing, weve pushed the bottleneck from the AIX disk and adapter queues to the disk subsystem. Two approaches to tuning queue depth are 1) base the queue depths on actual IO requests your application generate or 2) use a test tool to see what the disk subsystem can handle and tune the queues based on what the disk subsystem can handle. The ndisk tool (part of the nstress package available on the internet at www-941.ibmcollaborationwikidisplayWikiPtypenstress ) can be used to stress the disk subsystem to see what it can handle. The authors preference is to tune based on your application IO requirements, especially when the disk is shared with other servers. For tuning, we can categorize the situation into four categories: Were filling up the queues and IOs are waiting in the hdisk or adapter drivers Were not filling up the queues, and IO service times are good Were not filling up the queues, and IO service times are poor Were not filling up the queues, and were sending IOs to the storage faster than it can handle and it loses the IOs We want to tune the queues to be in either situation 2 or 3. If were in situation 3, that indicates a bottleneck beyond the hdisk driver which will typically be in the disk subsystem itself, but could also be in the adapter driver or SAN fabric. Situation 4 is something we do want to avoid. All disks and disk subsystem have limits regarding the number of in-flight IOs they can handle, mainly due to memory limitations to hold the IO request and data. When the storage loses IOs, the IO will eventually time out at the host, recovery code will be used and resubmit the IO, but in the meantime transactions waiting on that IO will be stalled. This isnt a desirable situation, as the CPU ends up doing more work to handle IOs than necessary. If the IO eventually fails, then this can lead to an application crash or worse. So be sure to check your storage documentation to understand its limits. Then after running your application during peak IO periods look at the statistics and tune again. Regarding the qdepthenable parameter for SDD, the default is yes which essentially has SDD handling the IOs beyond queuedepth for the underlying hdisks. Setting it to no results in the hdisk device driver handling them in its wait queue. In other words, with qdepthenableyes, SDD handles the wait queue, otherwise the hdisk device driver handles the wait queue. There are error handling benefits to allowing SDD to handle these IOs, e. g. if using LVM mirroring across two ESSs. With heavy IO loads and a lot of queueing in SDD (when qdepthenableyes) its more efficient to allow the hdisk device drivers to handle relatively shorter wait queues rather than SDD handling a very long wait queue by setting qdepthenableno. In other words, SDDs queue handling is single threaded where each hdisk driver has its own thread. So if error handling is of primary importance (e. g. when LVM mirroring across disk subsystems) then leave qdepthenableyes. Otherwise, setting qdepthenableno more efficiently handles the wait queues when they are long. Note that one should set the qdepthenable parameter via the datapath command as its a dynamic change that way (using chdev is not dynamic for this parameter). If error handling is of concern, then its also advisable, assuming the disk is SAN switch attached, to set the fscsi device attribute fcerrrecov to fastfail rather than the default of delayedfail, and also change the fscsi device dyntrk attribute to yes rather than the default of no. These attributes assume a SAN switch that supports this feature. What are reasonable average IO service times What is good or reasonable is somewhat a factor of the technology of the storage and the storage cache sizes. Assuming no IOs are queued to a disk, a typical read will take somewhere from 0 to 15 ms, or so, depending on how far the actuator has to travel (seek time), how long it takes the disk to rotate to the right sector (rotation time), and how long it takes to read the data (transfer time). Then the data must move from the storage to the host. Typically the time is dominated by seek time rotation time, though for large IOs transfer time also can be significant. Sometimes the data will be in disk subsystem read cache, in which case the IO service time is around 1 ms. Typically for large disk subsystems that arent overloaded, IO service times will average around 5-10 ms. When small random reads start averaging greater than 15 ms, this indicates the storage is getting busy. Writes typically go to write cache (assuming it exists) and then these average typically less than about 2.5 ms. But there are exceptions. If the storage is synchronously mirroring the data to a remote site, writes can take much longer. And if the IO is large (say 64 KB or larger) then the transfer time becomes more significant and the average time is slightly worse. If theres no cache, then writes take about the same as reads. If the IO is large block sequential, then besides the increased transfer time, we expect IOs to queue at the physical disk, and IO service times to be much longer on average. E. G. if an application submits 50 IOs (say 50 64 KB IOs reading a file sequentially) then the first few IOs will have reasonably good IO service times, while the last IO will have had to wait for the other 49 to finish first, and will have a very long IO service time. IOs to SSDs are typically less than 1 ms, and for SSDs in disk subsystems, typically less than 2 ms, and on occasion higher. Tuning the FC adapter numcmdelems The fcstat command is perhaps the easiest tool to look for blocked IOs in the adapters queues, e. g. FIBRE CHANNEL STATISTICS REPORT: fcs0 . FC SCSI Adapter Driver Information No DMA Resource Count: 0 No Adapter Elements Count: 104848 No Command Resource Count: 13915968 . The values for No Adapter Elements Count and No Command Resource Count are the number of times since boot that an IO was temporarily blocked due to an inadequate numcmdelems attribute value. Non-zero values indicate that increasing numcmdelems may help improve IO service times. Of course if the value increments slowly, then the improvement may be very small, while quickly incrementing values means tuning is more likely to have a measurable improvement in performance. Like the hdisk queuedepth attribute, changing the numcmdelems value requires stopping use of the resources or a reboot. Queue depths with VSCSI VIO When using VIO, one configures VSCSI adapters (for each virtual adapter in a VIOS, known as a vhost device, there will be a matching VSCSI adapter in a VIOC). These adapters have a fixed queue depth that varies depending on how many VSCSI LUNs are configured for the adapter. There are 512 command elements of which 2 are used by the adapter, 3 are reserved for each VSCSI LUN for error recovery and the rest are used for IO requests. Thus, with the default queuedepth of 3 for VSCSI LUNs, that allows for up to 85 LUNs to use an adapter: (512 - 2) (3 3) 85 rounding down. So if we need higher queue depths for the devices, then the number of LUNs per adapter is reduced. E. G. if we want to use a queuedepth of 25, that allows 51028 18 LUNs. We can configure multiple VSCSI adapters to handle many LUNs with high queue depths. each requiring additional memory. One may have more than one VSCSI adapter on a VIOC connected to the same VIOS if you need more bandwidth. Also, one should set the queuedepth attribute on the VIOCs hdisk to match that of the mapped hdisks queuedepth on the VIOS. For a formula, the maximum number of LUNs per virtual SCSI adapter (vhost on the VIOS or vscsi on the VIOC) is INT(510(Q3)) where Q is the queuedepth of all the LUNs (assuming they are all the same). Note that to change the queuedepth on an hdisk at the VIOS requires that we unmap the disk from the VIOC and remap it back, or a simpler approach is to change the values in the ODM (e. g. chdev - l hdisk30 - a queuedepth20 - P) then reboot the VIOS. For LV VSCSI hdisks, where multiple VIOC hdisks are created from a single VIOS hdisk, then one may take a dedicated resource, shared resource or an in between approach to the VIOS hdisk queue slots. See the section below entitled Further theoretical thoughts on shared vs. dedicated resources . Queue depths with NPIV VIO When using NPIV, we have virtual FC adapters (vFC) and real FC adapters, and often have multiple vFCs tied to a single real FC adapter. If you increase numcmdelems on the virtual FC (vFC) adapter, then you should also increase the setting on the real FC adapter. You can use the fcstat command for both the virtual adapter as well as the real adapter for tuning purposes. A special note on the FC adapter maxxfersize attribute This attribute for the fscsi device, which controls the maximum IO size the adapter device driver will handle, also controls a memory area used by the adapter for data transfers. When the default value is used (maxxfersize0x100000) the memory area is 16 MB in size. When setting this attribute to any other allowable value (say 0x200000) then the memory area is 128 MB in size. At AIX 6.1 TL2 or later a change was made for virtual FC adapters so the DMA memory area is always 128 MB even with the default maxxfersize. This memory area is a DMA memory area, but it is different than the DMA memory area controlled by the lgtermdma attribute (which is used for IO control). The default value for lgtermdma of 0x800000 is usually adequate. So for heavy IO and especially for large IOs (such as for backups) its recommended to set maxxfersize0x200000. The fcstat command can also be used to examine whether or not increasing numcmdelems or maxxfersize could increase performance fcstat fcs0 . FC SCSI Adapter Driver Information No DMA Resource Count: 0 No Adapter Elements Count: 0 No Command Resource Count: 0 This shows an example of an adapter that has sufficient values for numcmdelems and maxxfersize. Non zero value would indicate a situation in which IOs queued at the adapter due to lack of resources, and increasing numcmdelems and maxxfersize would be appropriate. Note that changing maxxfersize uses memory in the PCI Host Bridge chips attached to the PCI slots. The salesmanual, regarding the dual port 4 Gbps PCI-X FC adapter states that If placed in a PCI-X slot rated as SDR compatible andor has the slot speed of 133 MHz, the AIX value of the maxxfersize must be kept at the default setting of 0x100000 (1 megabyte) when both ports are in use. The architecture of the DMA buffer for these slots does not accommodate larger maxxfersize settings If there are too many FC adapters and too many LUNs attached to the adapter, this will lead to issues configuring the LUNs. Errors will look like: LABEL: DMAERR IDENTIFIER: 00530EA6 DateTime: Mon Mar 3 10:48:09 EST 2008 Sequence Number: 863 Machine Id: 00C3BCB04C00 Node Id: p595back Class: H Type: UNKN Resource Name: PCIDMA Resource Class: NONE Resource Type: NONE Location: Description UNDETERMINED ERROR Probable Causes SYSTEM IO BUS SOFTWARE PROGRAM ADAPTER DEVICE Recommended Actions PERFORM PROBLEM DETERMINATION PROCEDURES Detail Data BUS NUMBER FFFF FFFF 9000 00E4 CHANNEL UNIT ADDRESS 0000 0000 0000 018B ERROR CODE 0000 0000 1000 0003 So if you get these errors, youll need to change the maxxfersize back to the default value. Also note that if you are booting from SAN, if you encounter this error, you wont be able to boot, so be sure to have a back out plan if you plan to change this and are booting from SAN. Further theoretical thoughts on shared vs. dedicated resources The astute reader will have considered the fact that typically we have many hdisk drivers sharing multiple adapters and adapter drivers, thus, the FC queue slots are a shared resource for the hdisk drivers: Thus, its possible to ensure that we never fill the adapter queues, by making SUM(hdisk0 queuedepth, hdisk1 queuedepth. hdiskM queuedepth) lt SUM (fcs0 numcmdelems, fcs1 numcmdelems. fcsN numcmdelems). This assumes that IO are evenly spread across the adapters. And most multi-path code does balance IOs across the adapters (or at least can). Though often, environments have many more hdisks than FC ports, and ensuring we wont fill the adapter drivers can lead to small values for queuedepth, and full queues on the hdisk drivers. So there is the dedicated resource approach, the shared resource approach, and in between dedicated and shared. Taking this simple example where Q represents the queue depth for the device driver: This would be considered a dedicated resource approach, where 10 of the adapter driver queue slots are dedicated to each hdisk driver. Here we know well never submit an IO to a full queue on the adapter driver. This would be considered a shared resource approach where the 10 adapter queue slots could be filled up from a single hdisk driver. And heres an example of something in between: Here, there will always be at least 5 queue slots available in the adapter driver for either hdisk driver. There are pros and cons to each approach. The benefit of the dedicated resource approach is that the resources allocated will always be available but typically there will be fewer resources available to each user of the resource (here the resource were considering is the adapter queue slots, and the users of the resource are the hdisk drivers). The benefit of the shared resource approach is that well have more resources for an individual user of the resource when it needs it and it will be able to get greater thruput than in the dedicated resource approach. The author generally prefers a shared resource approach, as generally provides the best thruput and price performance. Note that this situation of shared resources occurs in several possible ways beyond hdisk drivers using adapter drivers. It is also involved when: Several LV VSCSI hdisks for a single hdisk on a VIOS Several vFC adapters using a single real FC adapter Several LPARs using the same disk subsystem
No comments:
Post a Comment