что такое idle cpu idle
Русские Блоги
Объяснение каждого параметра CPU User, Nice, System, Wait, Idle
использоватьМониторинг ганглиевДля всего кластера Hadoop см. Различные индикаторы, собранные Ganglia: значение каждого конкретного индикатора ЦП объясняется следующим образом:
①CPU (загрузка ЦП на контролируемом главном хосте)
Из рисунка видно, что всего пять индикаторов CPU. Вот они:
Ницца сказала: это можно понимать какПроцесс пользовательского пространстваПриоритет планирования ЦП, диапазон [-20,19]
В компьютере операции чтения и записи на диск намного медленнее, чем у ЦП.ЦП загружает данные, и данные, как правило, должны быть считаны в память на диске для обработки. Когда ЦП инициирует операцию чтения и записи, ему необходимо дождаться, пока дисковый накопитель прочитает данные в память (см.:Понимание JAVA IO и NIO), в результате чего ЦП ничего не делает в течение этого периода ожидания. Время, в течение которого ЦП находится в этом состоянии ожидания, измеряется параметром ожидания.
ⓔIdle
Идель означает: процент времени, в течение которого ЦП находится в состоянии ожидания. Вообще говоря, idel + user + nice примерно равно 100%
Кроме того, команда top в Linux может отображать использование ЦП в системе в реальном времени. На рисунке ниже показано, что на gmond Ganglia приходится много ЦП.
pro4gl.ru
Управление загрузкой процессора
Управление загрузкой процессора
Все ресурсы влияют тем или иным образом на загрузку процессора. К примеру, медленные дисковые устройства заставляют CPU выполнять холостые циклы в ожидании завершения операции ввода/вывода. Смена контекста (context switch) аналогичным образом увеличивает время отклика системы.
Что такое загрузка процессора
Чтобы понять, чем занят процессор необходимо знать – из каких компонент состоит его загрузка. На UNIX-системах работу процессора разделяют на следующие компоненты:
На Windows-системах загрузку процессора разделяют на следующие компоненты:
Настройка операционной системы.
Любое время, потраченное на ожидание ввода-вывода кажется неопытному администратору нежелательным. Но, на самом деле, не все I/O wait являются поводом для беспокойства. Время I/O Wait возникает каждый раз, когда процессор выполнил свою работу и находится в ожидании других ресурсов : дисков или памяти. Это естественно – центральный процессор является быстрейшим из ресурсов и должен ждать отклика более медленных устройств. Если же ожидание ввода-вывода оказывает заметное влияние на систему, то это является причиной для беспокойства.
При настройке системы желательно (но необязательно) иметь всегда ненулевое значение Idle time (время простоя). Да, вполне возможно нагрузить под завязку двухпроцессорную систему на 100% полезной работой (иными словами мы будем иметь 100% User Time). Это не означает, что система имеет плохую производительность, напротив, она работает так быстро насколько может. При настройке производительности мы пытаемся перенести узкое место (bottleneck) на самый быстрый ресурс – CPU. Если процессоры на 100% заняты пользовательскими задачами, то система настроена просто прекрасно. Дальнейшее увеличение производительности возможно только при увеличении тактовой частоты или при добавлении новых ядер. Утилизацию процессора необходимо рассматривать только в совокупности показателей. В общих чертах, если время I/O Wait увеличивается, то совсем необязательно, что узким местом стала подсистема ввода-вывода. Если мы говорим об идеальных условиях, то достаточно иметь 70 % времени на пользовательские задачи (User Time), 20% – время системы (System Time), 0% – ожидание ввода-вывода (I/O Wait) и 10% – время простоя (Idle Time). Еще одной метрикой наблюдения является отсутствие 100 % загрузки CPU на пользовательские задачи, если, конечно, это не обычное поведение вашей системы. Такую нагрузку могут вызвать неконтролируемые процессы. Неконтролируемые процессы – это, в основном, клиентские процессы, главный отличительный признак которых – отсутствие присоединенных к себе терминалов (tty) и исполнение в режиме отличном от пакетного (-b).
Отношение User Time к System Time должно быть приблизительно как 3:1 на host-based системах, в то время как на системах клиент-сервер это время может быть ближе как 1.5:1. Если время, затраченное на пользовательские задачи ниже 20% от общего использования CPU и меньше времени системы, то такое отношение может изменяться в широких пределах. В некоторых случаях System Time больше User Time из-за плохого выделения ресурсов. Таким образом, во время увеличения User Time при настройке сервера следует стремиться к тому, чтобы System Time составляло треть или менее от пользовательского времени. Это легко увидеть, взглянув на ресурсы CPU в любом средстве мониторинга.
Иллюстрация 5 показывает пример информации отображаемой Performance Monitor
Время простоя CPU (CPU Idle Time)
Наличие Idle time может являться хорошим знаком. Это означает, что вашей системе есть куда расти. Необязательно, чтобы время простоя было всегда больше нуля. Если Idle Time равно нулю длительный период времени и нет существенного времени I/O Wait, то для полного понимания ситуации необходимо пристальнее взглянуть на работу CPU.
Например, посмотрите на глубину очереди CPU (cpu queue depth). Значение CPU queue depth показывает количество процессов в ожидании исполнения. Если в очереди всегда есть некоторое количество процессов, то необходимо предпринять один из следующих вариантов:
Если время I/O wait высокое и нет времени Idle Time, то необходимо повысить эффективность дисковой подсистемы (см главу 2 “Управление ресурсами OpenEdge”) – увеличить пропускную способность дисковой подсистемы или изменить временной график ваших вычислений. Если I/O Wait занимает 10 % или меньше и значение Idle Time не равно нулю, то необходимость в каких-либо действиях отсутствует.
Наблюдения за системой
Круглосуточный мониторинг за вашей системой всегда помогает принять решение при оптимизации загрузки процессора. Ваша система может выглядеть отлично в течение рабочего времени (пока вы видите, что происходит), но ночью могут быть серьезные проблемы производительности. Есть множество приложений, которые производят 70% своих вычислений в вечерние часы. Большинство систем выполняют в ночные часы совсем другие задачи, чем днем. Типичная система делает OLTP-транзакции с 9 утра до 5 вечера, производит закрытие дня с 7 вечера до 12 ночи и запускает большие “тяжелые” отчеты после полуночи.
Есть некоторые приёмы, которые вы можете использовать для более эффективного использования существующих ресурсов. К примеру, можно определить такое время суток, когда базе данных необходимо больше процессов APW (page writers); или запускать “тяжелый” отчет в ночные часы.
Чем раньше будет обнаружено потенциальное узкое место, тем больше времени останется для его вредупреждения.
Использование OE Management для мониторинга производителоьности CPU
Информацию о CPU лучше разместить на странице My Dashboard для того, чтобы она все время была на виду. При настройке страницы My Dashboard вы можете выбрать CPU в пункте Other system resources to show, чтобы увидеть фрейм-viewlet со статистикой использования CPU.
Быстрый процессор или многопроцессорная конфигурация?
Итак, что же решить? Лучше всего – провести анализ вашего приложения и его использования. Например, если у вас большинство однопоточных задач, то выигрыш от быстрого процессора будет больше, даже за счет уменьшения количества CPU. А приложение с большим количеством ввода данных и малой долей однопоточных задач выиграет от наличия бОльшего количества CPU, даже если они будут медленными. В любом случае – залогом правильного решения будет детальный анализ утилизации процессоров.
В этой главе мы узнали как эффективно управлять системными ресурсами.
System Idle Process постоянно грузит процессор – что делать?
Во время простоя нагрузка на компоненты компьютера должны быть минимальна. Если вы не запустили ни одной программы, не поставили скачиваться файлы, а процессор уже загружен на полную – что-то в системе работает неверно. Разберемся с ситуацией, когда System idle process все время грузит процессор – что это за утилита и как ее отключить.
Что такое System Idle Process
Если компьютер тормозит, откройте «Диспетчер задач», нажав Win+X и выбрав одноименную утилиту. На первой вкладке приведен перечень выполняющихся процессов, кликните на столбец ЦП, чтобы отсортировать их по убыванию нагрузки на центральный процессор. На первой строке окажется главный виновник загрузки системы, иногда это System Idle Process или утилита «Бездействие системы» в русскоязычных версиях Windows.
System Idle Process – что же это такое? Это специальный процесс бездействия, но он не показывает процент простоя процессора. В Windows существует специальная утилита, которую ОС запускает в то время, когда компьютер не нагружен другими операциями. System Idle Process помогает снизить потребление энергии и температуру процессора, выполняя в цикле процедуру приостановки.
Во время остановки ЦП постоянно ожидает появления какого-либо прерывания – как только появится какая-нибудь задача, компьютер сразу проснется. Утилита бездействия имеет наименьший приоритет среди всех других, она не стартует, пока есть хоть один другой процесс для запуска.
Поэтому проценты у System Idle Process в диспетчере показывают не загруженность компьютера, а количество свободных ресурсов.
Как отключить процесс
Отключить системный System Idle Process нельзя. Если вы кликните по нему правой кнопкой мышки, то в подменю будут недоступны стандартные операции «Снять задачу» или «Задать приоритет». Утилита обращается к ядру ОС, поэтому в Windows запрещено давать пользователям возможности управления такими системными процессами.
Как снизить нагрузку на процессор
В нормально работающей системе System Idle Process совсем не снижает быстродействие. Если же при больших показателях бездействия процессора в 60-70% компьютер заметно тормозит, надо искать причину. Проверьте в «Диспетчере задач», что вызывает повышенную нагрузку на ЦП. Также можно использовать «Мониторинг ресурсов»: зажмите Win+R и впишите resmon. Сервис покажет более подробную информацию по использованию ресурсов ЦП.
Часто нагрузку вызывают «Системные прерывания» — те самые, которые должны отслеживать возникновение запросов к ПК во время бездействия. Если они занимают больше 10% ресурсов, проделайте следующее:
Заключение
Мы разобрались, что означает загрузка системы процессом System Idle Process и как от него избавиться. В норме утилита понижает энергопотребление во время простоя ПК и не нагружает его ресурсы. Если компьютер работает медленно – ищите другие причины проблемы.
Метрика загруженности процессора (CPU utiliztion) — это не то что вы думаете
Всем привет. Предлагаю вашему вниманию свой перевод поста «CPU Utilization is Wrong» из блога Брендана Грегга.
Как вы думаете, что значит нагрузка на процессор 90% на картинке ниже?
Вот что это значит на самом деле:
Stalled, то есть «приостановлено» значит, что в данный момент процессор не обрабатывает инструкции, обычно это означает, что он ожидает завершения операций ввода/вывода связанных с памятью (здесь и далее речь о RAM, а не дисковом вводе/выводе). Соотношение между «занято» и «приостановлено» (busy/stalled), которое я привел выше, это то что я обычно вижу в продакшене. Вероятно, что ваш процессор тоже большую часть времени находится в stalled состоянии, но вы об этом и не догадываетесь.
Что это значит для вас? Понимание того насколько много ваш процессор находится в приостановленном состоянии может помочь вам понять куда направить усилия по оптимизации производительности приложения: на ускорение кода или уменьшение числа операций ввода/вывода связанных с памятью. Всем кто заинтересован в оптимизации нагрузки на процессор, в особенности в облаках с настроенным автомасштабированием на основе нагрузки на CPU, будет полезно знать насколько долго процессор находится в приостановленном состоянии.
Что такое нагрузка на процессор на самом деле?
Метрика, которую мы называем нагрузкой на процессор (CPU utilization) на самом деле это «не-idle время», то есть время, которое процессор не выполняет idle-тред. Ядро вашей операционной системы (какую бы ОС вы не использовали) обычно следит за этим во время переключения контекста. Если не-idle тред запустился, а затем спустя 100 милисекунд остановился, то ядро посчитает, что процессор был использован в течение всего этого времени.
Эта метрика так же стара как и системы совместного использования времени (time sharing systems). В бортовом компьютере лунного модуля Apollo (это пионер среди систем совместного использования времени) idle-тред назывался «DUMMY JOB» и инженеры мониторили циклы выполняющие его в сравнении с реальными задачами, это было важной метрикой измерения нагрузки. (Я писал об этом ранее).
Что же с этой метрикой не так?
Со временем все становится только хуже. Долгое время производители процессоров увеличивали тактовые частоты своих процессоров быстрее чем производители памяти уменьшали задержки доступа к памяти (CPU DRAM gap). Примерно в 2005 году процессоры достигли частот в 3 GHz и с тех пор мощность процессоров растет не за счет увеличения тактовой частоты, а за счет большего числа ядер, гипертрединга и многопроцессорных конфигураций. Все это предъявляет еще больше требований к памяти. Производители процессоров пытались снизить задержки связанные с памятью за счет больших по размеру и более умных CPU-кешей, более быстрых шин и соединений. Но проблема со stalled-состоянием все еще не решена.
Как понять, что процессор на самом деле делает
Сделать это можно используя Performance Monitoring Counters (PMC-счетчики): хардверные счетчики, которые могут быть прочитаны с помощью Linux pref (пакет linux-tools-generic в Линуксе) и других утилит. Для примера понаблюдаем за всей системой в течение 10 секунд:
Ключевая метрика здесь instructions per cycle (insns per cycle: IPC, число инструкций за один цикл), которая показывает сколько в среднем инструкций было выполнено за каждый такт. Чем больше, тем лучше. В примере выше значение 0.78 кажется очень неплохим (нагрузка 78%?) до тех пор пока вы не узнаете, что максимальная скорость процессора это IPC 4.0. Такие процессоры называют 4-wide, это название пошло от особенностей пути извлечения/декодирования инструкций в процессоре (подробнее об этом в Википедии).
Существуют сотни PMC-счетчиков, которые позволяют детальнее разобраться с производительностью системы, например, посчитать число приостановленных циклов по типам.
В облаках
Если вы работаете в виртуальном окружении, то вероятно у вас нет доступа к PMC-счетчикам, это зависит от поддержки этой фичи гипервизором. Я недавно писал о том, что PMC-счетчики теперь доступны в AWS EC2 в виртуальных машинах базирующихся на Xen.
Как интерпретировать и что делать
Если ваш IPC 1.0, то вероятно, вы ограничены числом инструкций, которые может выполнять процессор. Попробуйте найти способ уменьшить число выполняемых инструкций: уменьшить число ненужной работы, кешировать операции и т.п. CPU flame графы — отличная утилита для этих целей. С точки зрения тюнинга железа, попробуйте использовать процессор с большей тактовой частотой и большим числом ядер и гипертредов.
Для моих правил выше я выбрал значение IPC 1.0, почему именно его? Я пришел к нему из своего опыта работы с PMC-счетчиками. Вы можете выбрать для себя другое значение. Сделайте два тестовых приложения, одно упирающееся по производительности в процессор, другое — в память. Посчитайте IPC для них и возьмите среднее значение.
Что инструменты мониторинга производительности должны сообщать вам?
Другие причины почему CPU utilization вводит в заблуждение
Проблема со stalled-циклами может быть не только в задержках связанных с памятью:
— изменение температуры может влиять на приостановленность процессора,
— турбобуст может менять тактовую частоту процессора,
— ядро варьирует частоту процессора с определенным шагом,
— проблема с усреднением: 80% нагрузки в течение минуты скроет кратковременный всплеск до 100%,
— спинлоки: процессор нагружен, имеет высокий IPC, но приложение ничего не делает.
Заключение
Нагрузка на процессор (CPU utilization) это обычно неправильно интерпретируемая метрика, так как она включает циклы, потраченные на ожидание ответа от основной памяти, которые могут доминировать в современных нагрузках. Вы можете понять что на самом деле стоит за %CPU используя дополнительные метрики, включая число инструкций за цикл (IPC). Если IPC 1.0, то в скорость процессора. Я писал про IPC в своем предыдущем посте, в том числе написал и о использовании PMC-счетчиках, необходимых для измерения IPC.
Инструменты мониторинга производительности, которые показывают %CPU должны показывать PMC-счетчики, чтобы не вводить пользователей в заблуждение. Например, они могут показывать %CPU с IPC и/или число instruction-retired и stalled циклов. Вооруженные этими метриками разработчики и админы могут решить как правильнее тюнинговать их приложения и системы.
CPU Idle Time ManagementВ¶
ConceptsВ¶
Modern processors are generally able to enter states in which the execution of a program is suspended and instructions belonging to it are not fetched from memory or executed. Those states are the idle states of the processor.
Since part of the processor hardware is not used in idle states, entering them generally allows power drawn by the processor to be reduced and, in consequence, it is an opportunity to save energy.
CPU idle time management is an energy-efficiency feature concerned about using the idle states of processors for this purpose.
Logical CPUsВ¶
CPU idle time management operates on CPUs as seen by the CPU scheduler (that is the part of the kernel responsible for the distribution of computational work in the system). In its view, CPUs are logical units. That is, they need not be separate physical entities and may just be interfaces appearing to software as individual single-core processors. In other words, a CPU is an entity which appears to be fetching instructions that belong to one sequence (program) from memory and executing them, but it need not work this way physically. Generally, three different cases can be consider here.
First, if the whole processor can only follow one sequence of instructions (one program) at a time, it is a CPU. In that case, if the hardware is asked to enter an idle state, that applies to the processor as a whole.
Second, if the processor is multi-core, each core in it is able to follow at least one program at a time. The cores need not be entirely independent of each other (for example, they may share caches), but still most of the time they work physically in parallel with each other, so if each of them executes only one program, those programs run mostly independently of each other at the same time. The entire cores are CPUs in that case and if the hardware is asked to enter an idle state, that applies to the core that asked for it in the first place, but it also may apply to a larger unit (say a “package” or a “cluster”) that the core belongs to (in fact, it may apply to an entire hierarchy of larger units containing the core). Namely, if all of the cores in the larger unit except for one have been put into idle states at the “core level” and the remaining core asks the processor to enter an idle state, that may trigger it to put the whole larger unit into an idle state which also will affect the other cores in that unit.
Finally, each core in a multi-core processor may be able to follow more than one program in the same time frame (that is, each core may be able to fetch instructions from multiple locations in memory and execute them in the same time frame, but not necessarily entirely in parallel with each other). In that case the cores present themselves to software as “bundles” each consisting of multiple individual single-core “processors”, referred to as hardware threads (or hyper-threads specifically on Intel hardware), that each can follow one sequence of instructions. Then, the hardware threads are CPUs from the CPU idle time management perspective and if the processor is asked to enter an idle state by one of them, the hardware thread (or CPU) that asked for it is stopped, but nothing more happens, unless all of the other hardware threads within the same core also have asked the processor to enter an idle state. In that situation, the core may be put into an idle state individually or a larger unit containing it may be put into an idle state as a whole (if the other cores within the larger unit are in idle states already).
Idle CPUsВ¶
Logical CPUs, simply referred to as “CPUs” in what follows, are regarded as idle by the Linux kernel when there are no tasks to run on them except for the special “idle” task.
Tasks are the CPU scheduler’s representation of work. Each task consists of a sequence of instructions to execute, or code, data to be manipulated while running that code, and some context information that needs to be loaded into the processor every time the task’s code is run by a CPU. The CPU scheduler distributes work by assigning tasks to run to the CPUs present in the system.
Tasks can be in various states. In particular, they are runnable if there are no specific conditions preventing their code from being run by a CPU as long as there is a CPU available for that (for example, they are not waiting for any events to occur or similar). When a task becomes runnable, the CPU scheduler assigns it to one of the available CPUs to run and if there are no more runnable tasks assigned to it, the CPU will load the given task’s context and run its code (from the instruction following the last one executed so far, possibly by another CPU). [If there are multiple runnable tasks assigned to one CPU simultaneously, they will be subject to prioritization and time sharing in order to allow them to make some progress over time.]
The special “idle” task becomes runnable if there are no other runnable tasks assigned to the given CPU and the CPU is then regarded as idle. In other words, in Linux idle CPUs run the code of the “idle” task called the idle loop. That code may cause the processor to be put into one of its idle states, if they are supported, in order to save energy, but if the processor does not support any idle states, or there is not enough time to spend in an idle state before the next wakeup event, or there are strict latency constraints preventing any of the available idle states from being used, the CPU will simply execute more or less useless instructions in a loop until it is assigned a new task to run.
The Idle LoopВ¶
The idle loop code takes two major steps in every iteration of it. First, it calls into a code module referred to as the governor that belongs to the CPU idle time management subsystem called CPUIdle to select an idle state for the CPU to ask the hardware to enter. Second, it invokes another code module from the CPUIdle subsystem, called the driver, to actually ask the processor hardware to enter the idle state selected by the governor.
The role of the governor is to find an idle state most suitable for the conditions at hand. For this purpose, idle states that the hardware can be asked to enter by logical CPUs are represented in an abstract way independent of the platform or the processor architecture and organized in a one-dimensional (linear) array. That array has to be prepared and supplied by the CPUIdle driver matching the platform the kernel is running on at the initialization time. This allows CPUIdle governors to be independent of the underlying hardware and to work with any platforms that the Linux kernel can run on.
Each idle state present in that array is characterized by two parameters to be taken into account by the governor, the target residency and the (worst-case) exit latency. The target residency is the minimum time the hardware must spend in the given state, including the time needed to enter it (which may be substantial), in order to save more energy than it would save by entering one of the shallower idle states instead. [The “depth” of an idle state roughly corresponds to the power drawn by the processor in that state.] The exit latency, in turn, is the maximum time it will take a CPU asking the processor hardware to enter an idle state to start executing the first instruction after a wakeup from that state. Note that in general the exit latency also must cover the time needed to enter the given state in case the wakeup occurs when the hardware is entering it and it must be entered completely to be exited in an ordered manner.
There are two types of information that can influence the governor’s decisions. First of all, the governor knows the time until the closest timer event. That time is known exactly, because the kernel programs timers and it knows exactly when they will trigger, and it is the maximum time the hardware that the given CPU depends on can spend in an idle state, including the time necessary to enter and exit it. However, the CPU may be woken up by a non-timer event at any time (in particular, before the closest timer triggers) and it generally is not known when that may happen. The governor can only see how much time the CPU actually was idle after it has been woken up (that time will be referred to as the idle duration from now on) and it can use that information somehow along with the time until the closest timer to estimate the idle duration in future. How the governor uses that information depends on what algorithm is implemented by it and that is the primary reason for having more than one governor in the CPUIdle subsystem.
Idle CPUs and The Scheduler TickВ¶
The scheduler tick is a timer that triggers periodically in order to implement the time sharing strategy of the CPU scheduler. Of course, if there are multiple runnable tasks assigned to one CPU at the same time, the only way to allow them to make reasonable progress in a given time frame is to make them share the available CPU time. Namely, in rough approximation, each task is given a slice of the CPU time to run its code, subject to the scheduling class, prioritization and so on and when that time slice is used up, the CPU should be switched over to running (the code of) another task. The currently running task may not want to give the CPU away voluntarily, however, and the scheduler tick is there to make the switch happen regardless. That is not the only role of the tick, but it is the primary reason for using it.
The scheduler tick is problematic from the CPU idle time management perspective, because it triggers periodically and relatively often (depending on the kernel configuration, the length of the tick period is between 1 ms and 10 ms). Thus, if the tick is allowed to trigger on idle CPUs, it will not make sense for them to ask the hardware to enter idle states with target residencies above the tick period length. Moreover, in that case the idle duration of any CPU will never exceed the tick period length and the energy used for entering and exiting idle states due to the tick wakeups on idle CPUs will be wasted.
Fortunately, it is not really necessary to allow the tick to trigger on idle CPUs, because (by definition) they have no tasks to run except for the special “idle” one. In other words, from the CPU scheduler perspective, the only user of the CPU time on them is the idle loop. Since the time of an idle CPU need not be shared between multiple runnable tasks, the primary reason for using the tick goes away if the given CPU is idle. Consequently, it is possible to stop the scheduler tick entirely on idle CPUs in principle, even though that may not always be worth the effort.
Whether or not it makes sense to stop the scheduler tick in the idle loop depends on what is expected by the governor. First, if there is another (non-tick) timer due to trigger within the tick range, stopping the tick clearly would be a waste of time, even though the timer hardware may not need to be reprogrammed in that case. Second, if the governor is expecting a non-timer wakeup within the tick range, stopping the tick is not necessary and it may even be harmful. Namely, in that case the governor will select an idle state with the target residency within the time until the expected wakeup, so that state is going to be relatively shallow. The governor really cannot select a deep idle state then, as that would contradict its own expectation of a wakeup in short order. Now, if the wakeup really occurs shortly, stopping the tick would be a waste of time and in this case the timer hardware would need to be reprogrammed, which is expensive. On the other hand, if the tick is stopped and the wakeup does not occur any time soon, the hardware may spend indefinite amount of time in the shallow idle state selected by the governor, which will be a waste of energy. Hence, if the governor is expecting a wakeup of any kind within the tick range, it is better to allow the tick trigger. Otherwise, however, the governor will select a relatively deep idle state, so the tick should be stopped so that it does not wake up the CPU too early.
In any case, the governor knows what it is expecting and the decision on whether or not to stop the scheduler tick belongs to it. Still, if the tick has been stopped already (in one of the previous iterations of the loop), it is better to leave it as is and the governor needs to take that into account.
The kernel can be configured to disable stopping the scheduler tick in the idle loop altogether. That can be done through the build-time configuration of it (by unsetting the CONFIG_NO_HZ_IDLE configuration option) or by passing nohz=off to it in the command line. In both cases, as the stopping of the scheduler tick is disabled, the governor’s decisions regarding it are simply ignored by the idle loop code and the tick is never stopped.
The menu GovernorВ¶
The menu governor is the default CPUIdle governor for tickless systems. It is quite complex, but the basic principle of its design is straightforward. Namely, when invoked to select an idle state for a CPU (i.e. an idle state that the CPU will ask the processor hardware to enter), it attempts to predict the idle duration and uses the predicted value for idle state selection.
It first obtains the time until the closest timer event with the assumption that the scheduler tick will be stopped. That time, referred to as the sleep length in what follows, is the upper bound on the time before the next CPU wakeup. It is used to determine the sleep length range, which in turn is needed to get the sleep length correction factor.
The menu governor maintains two arrays of sleep length correction factors. One of them is used when tasks previously running on the given CPU are waiting for some I/O operations to complete and the other one is used when that is not the case. Each array contains several correction factor values that correspond to different sleep length ranges organized so that each range represented in the array is approximately 10 times wider than the previous one.
The correction factor for the given sleep length range (determined before selecting the idle state for the CPU) is updated after the CPU has been woken up and the closer the sleep length is to the observed idle duration, the closer to 1 the correction factor becomes (it must fall between 0 and 1 inclusive). The sleep length is multiplied by the correction factor for the range that it falls into to obtain the first approximation of the predicted idle duration.
Next, the governor uses a simple pattern recognition algorithm to refine its idle duration prediction. Namely, it saves the last 8 observed idle duration values and, when predicting the idle duration next time, it computes the average and variance of them. If the variance is small (smaller than 400 square milliseconds) or it is small relative to the average (the average is greater that 6 times the standard deviation), the average is regarded as the “typical interval” value. Otherwise, the longest of the saved observed idle duration values is discarded and the computation is repeated for the remaining ones. Again, if the variance of them is small (in the above sense), the average is taken as the “typical interval” value and so on, until either the “typical interval” is determined or too many data points are disregarded, in which case the “typical interval” is assumed to equal “infinity” (the maximum unsigned integer value). The “typical interval” computed this way is compared with the sleep length multiplied by the correction factor and the minimum of the two is taken as the predicted idle duration.
Then, the governor computes an extra latency limit to help “interactive” workloads. It uses the observation that if the exit latency of the selected idle state is comparable with the predicted idle duration, the total time spent in that state probably will be very short and the amount of energy to save by entering it will be relatively small, so likely it is better to avoid the overhead related to entering that state and exiting it. Thus selecting a shallower state is likely to be a better option then. The first approximation of the extra latency limit is the predicted idle duration itself which additionally is divided by a value depending on the number of tasks that previously ran on the given CPU and now they are waiting for I/O operations to complete. The result of that division is compared with the latency limit coming from the power management quality of service, or PM QoS, framework and the minimum of the two is taken as the limit for the idle states’ exit latency.
Now, the governor is ready to walk the list of idle states and choose one of them. For this purpose, it compares the target residency of each state with the predicted idle duration and the exit latency of it with the computed latency limit. It selects the state with the target residency closest to the predicted idle duration, but still below it, and exit latency that does not exceed the limit.
In the final step the governor may still need to refine the idle state selection if it has not decided to stop the scheduler tick. That happens if the idle duration predicted by it is less than the tick period and the tick has not been stopped already (in a previous iteration of the idle loop). Then, the sleep length used in the previous computations may not reflect the real time until the closest timer event and if it really is greater than that time, the governor may need to select a shallower state with a suitable target residency.
Representation of Idle StatesВ¶
For the CPU idle time management purposes all of the physical idle states supported by the processor have to be represented as a one-dimensional array of struct cpuidle_state objects each allowing an individual (logical) CPU to ask the processor hardware to enter an idle state of certain properties. If there is a hierarchy of units in the processor, one struct cpuidle_state object can cover a combination of idle states supported by the units at different levels of the hierarchy. In that case, the target residency and exit latency parameters of it, must reflect the properties of the idle state at the deepest level (i.e. the idle state of the unit containing all of the other units).
For example, take a processor with two cores in a larger unit referred to as a “module” and suppose that asking the hardware to enter a specific idle state (say “X”) at the “core” level by one core will trigger the module to try to enter a specific idle state of its own (say “MX”) if the other core is in idle state “X” already. In other words, asking for idle state “X” at the “core” level gives the hardware a license to go as deep as to idle state “MX” at the “module” level, but there is no guarantee that this is going to happen (the core asking for idle state “X” may just end up in that state by itself instead). Then, the target residency of the struct cpuidle_state object representing idle state “X” must reflect the minimum time to spend in idle state “MX” of the module (including the time needed to enter it), because that is the minimum time the CPU needs to be idle to save any energy in case the hardware enters that state. Analogously, the exit latency parameter of that object must cover the exit time of idle state “MX” of the module (and usually its entry time too), because that is the maximum delay between a wakeup signal and the time the CPU will start to execute the first new instruction (assuming that both cores in the module will always be ready to execute instructions as soon as the module becomes operational as a whole).
There are processors without direct coordination between different levels of the hierarchy of units inside them, however. In those cases asking for an idle state at the “core” level does not automatically affect the “module” level, for example, in any way and the CPUIdle driver is responsible for the entire handling of the hierarchy. Then, the definition of the idle state objects is entirely up to the driver, but still the physical properties of the idle state that the processor hardware finally goes into must always follow the parameters used by the governor for idle state selection (for instance, the actual exit latency of that idle state must not exceed the exit latency parameter of the idle state object selected by the governor).
above Total number of times this idle state had been asked for, but the observed idle duration was certainly too short to match its target residency. below Total number of times this idle state had been asked for, but cerainly a deeper idle state would have been a better match for the observed idle duration. desc Description of the idle state. disable Whether or not this idle state is disabled. latency Exit latency of the idle state in microseconds. name Name of the idle state. power Power drawn by hardware in this idle state in milliwatts (if specified, 0 otherwise). residency Target residency of the idle state in microseconds. time Total time spent in this idle state by the given CPU (as measured by the kernel) in microseconds. usage Total number of times the hardware has been asked by the given CPU to enter this idle state.
The desc and name files both contain strings. The difference between them is that the name is expected to be more concise, while the description may be longer and it may contain white space or special characters. The other files listed above contain integer numbers.
The disable attribute is the only writeable one. If it contains 1, the given idle state is disabled for this particular CPU, which means that the governor will never select it for this particular CPU and the CPUIdle driver will never ask the hardware to enter it for that CPU as a result. However, disabling an idle state for one CPU does not prevent it from being asked for by the other CPUs, so it must be disabled for all of them in order to never be asked for by any of them. [Note that, due to the way the ladder governor is implemented, disabling an idle state prevents that governor from selecting any idle states deeper than the disabled one too.]
If the disable attribute contains 0, the given idle state is enabled for this particular CPU, but it still may be disabled for some or all of the other CPUs in the system at the same time. Writing 1 to it causes the idle state to be disabled for this particular CPU and writing 0 to it allows the governor to take it into consideration for the given CPU and the driver to ask for it, unless that state was disabled globally in the driver (in which case it cannot be used at all).
The power attribute is not defined very well, especially for idle state objects representing combinations of idle states at different levels of the hierarchy of units in the processor, and it generally is hard to obtain idle state power numbers for complex hardware, so power often contains 0 (not available) and if it contains a nonzero number, that number may not be very accurate and it should not be relied on for anything meaningful.
The number in the time file generally may be greater than the total time really spent by the given CPU in the given idle state, because it is measured by the kernel and it may not cover the cases in which the hardware refused to enter this idle state and entered a shallower one instead of it (or even it did not enter any idle state at all). The kernel can only measure the time span between asking the hardware to enter an idle state and the subsequent wakeup of the CPU and it cannot say what really happened in the meantime at the hardware level. Moreover, if the idle state object in question represents a combination of idle states at different levels of the hierarchy of units in the processor, the kernel can never say how deep the hardware went down the hierarchy in any particular case. For these reasons, the only reliable way to find out how much time has been spent by the hardware in different idle states supported by it is to use idle state residency counters in the hardware, if available.
Power Management Quality of Service for CPUsВ¶
The power management quality of service (PM QoS) framework in the Linux kernel allows kernel code and user space processes to set constraints on various energy-efficiency features of the kernel to prevent performance from dropping below a required level. The PM QoS constraints can be set globally, in predefined categories referred to as PM QoS classes, or against individual devices.
The requested value is not automatically applied as a new constraint, however, as it may be less restrictive (greater in this particular case) than another constraint previously requested by someone else. For this reason, the PM QoS framework maintains a list of requests that have been made so far in each global class and for each device, aggregates them and applies the effective (minimum in this particular case) value as the new constraint.
In fact, opening the cpu_dma_latency special device file causes a new PM QoS request to be created and added to the priority list of requests in the PM_QOS_CPU_DMA_LATENCY class and the file descriptor coming from the “open” operation represents that request. If that file descriptor is then used for writing, the number written to it will be associated with the PM QoS request represented by it as a new requested constraint value. Next, the priority list mechanism will be used to determine the new effective value of the entire list of requests and that effective value will be set as a new constraint. Thus setting a new requested constraint value will only change the real constraint if the effective “list” value is affected by it. In particular, for the PM_QOS_CPU_DMA_LATENCY class it only affects the real constraint if it is the minimum of the requested constraints in the list. The process holding a file descriptor obtained by opening the cpu_dma_latency special device file controls the PM QoS request associated with that file descriptor, but it controls this particular PM QoS request only.
Closing the cpu_dma_latency special device file or, more precisely, the file descriptor obtained while opening it, causes the PM QoS request associated with that file descriptor to be removed from the PM_QOS_CPU_DMA_LATENCY class priority list and destroyed. If that happens, the priority list mechanism will be used, again, to determine the new effective value for the whole list and that value will become the new real constraint.
In turn, for each CPU there is only one resume latency PM QoS request associated with the power/pm_qos_resume_latency_us file under /sys/devices/system/cpu/cpu / in sysfs and writing to it causes this single PM QoS request to be updated regardless of which user space process does that. In other words, this PM QoS request is shared by the entire user space, so access to the file associated with it needs to be arbitrated to avoid confusion. [Arguably, the only legitimate use of this mechanism in practice is to pin a process to the CPU in question and let it use the sysfs interface to control the resume latency constraint for it.] It still only is a request, however. It is a member of a priority list used to determine the effective value to be set as the resume latency constraint for the CPU in question every time the list of requests is updated this way or another (there may be other requests coming from kernel code in that list).
CPU idle time governors are expected to regard the minimum of the global effective PM_QOS_CPU_DMA_LATENCY class constraint and the effective resume latency constraint for the given CPU as the upper limit for the exit latency of the idle states they can select for that CPU. They should never select any idle states with exit latency beyond that limit.
Idle States Control Via Kernel Command LineВ¶
In addition to the sysfs interface allowing individual idle states to be disabled for individual CPUs, there are kernel command line parameters affecting CPU idle time management.
The cpuidle.off=1 kernel command line option can be used to disable the CPU idle time management entirely. It does not prevent the idle loop from running on idle CPUs, but it prevents the CPU idle time governors and drivers from being invoked. If it is added to the kernel command line, the idle loop will ask the hardware to enter idle states on idle CPUs via the CPU architecture support code that is expected to provide a default mechanism for this purpose. That default mechanism usually is the least common denominator for all of the processors implementing the architecture (i.e. CPU instruction set) in question, however, so it is rather crude and not very energy-efficient. For this reason, it is not recommended for production use.
The cpuidle.governor= kernel command line switch allows the CPUIdle governor to use to be specified. It has to be appended with a string matching the name of an available governor (e.g. cpuidle.governor=menu ) and that governor will be used instead of the default one. It is possible to force the menu governor to be used on the systems that use the ladder governor by default this way, for example.
The other kernel command line parameters controlling CPU idle time management described below are only relevant for the x86 architecture and some of them affect Intel processors only.
The idle=nomwait option disables the intel_idle driver and causes acpi_idle to be used (as long as all of the information needed by it is there in the system’s ACPI tables), but it is not allowed to use the MWAIT instruction of the CPUs to ask the hardware to enter idle states.
© Copyright The kernel development community.