4sphinx.addnodesdocument)}( rawsourcechildren]( translations LanguagesNode)}(hhh](h pending_xref)}(hhh]docutils.nodesTextEnglish}parenthsba attributes}(ids]classes]names]dupnames]backrefs] refdomainstdreftypedoc reftarget/admin-guide/cpu-loadmodnameN classnameN refexplicitutagnamehhh ubh)}(hhh]hChinese (Traditional)}hh2sbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget(/translations/zh_TW/admin-guide/cpu-loadmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hItalian}hhFsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget(/translations/it_IT/admin-guide/cpu-loadmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hJapanese}hhZsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget(/translations/ja_JP/admin-guide/cpu-loadmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hKorean}hhnsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget(/translations/ko_KR/admin-guide/cpu-loadmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hSpanish}hhsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget(/translations/sp_SP/admin-guide/cpu-loadmodnameN classnameN refexplicituh1hhh ubeh}(h]h ]h"]h$]h&]current_languageChinese (Simplified)uh1h hh _documenthsourceNlineNubhsection)}(hhh](htitle)}(h CPU 负载h]h CPU 负载}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhU/var/lib/git/docbuild/linux/Documentation/translations/zh_CN/admin-guide/cpu-load.rsthKubh paragraph)}(hLinux通过``/proc/stat``和``/proc/uptime``导出各种信息,用户空间工具 如top(1)使用这些信息计算系统花费在某个特定状态的平均时间。 例如:h]hLinux通过``/proc/stat``和``/proc/uptime``导出各种信息,用户空间工具 如top(1)使用这些信息计算系统花费在某个特定状态的平均时间。 例如:}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh block_quote)}(h$ iostat Linux 2.6.18.3-exp (linmac) 02/20/2007 avg-cpu: %user %nice %system %iowait %steal %idle 10.01 0.00 2.92 5.44 0.00 81.63 ... h](h)}(h3$ iostat Linux 2.6.18.3-exp (linmac) 02/20/2007h]h3$ iostat Linux 2.6.18.3-exp (linmac) 02/20/2007}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK hhubhdefinition_list)}(hhh]hdefinition_list_item)}(hfavg-cpu: %user %nice %system %iowait %steal %idle 10.01 0.00 2.92 5.44 0.00 81.63 h](hterm)}(h7avg-cpu: %user %nice %system %iowait %steal %idleh]h7avg-cpu: %user %nice %system %iowait %steal %idle}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK hhubh definition)}(hhh]h)}(h-10.01 0.00 2.92 5.44 0.00 81.63h]h-10.01 0.00 2.92 5.44 0.00 81.63}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK hhubah}(h]h ]h"]h$]h&]uh1hhhubeh}(h]h ]h"]h$]h&]uh1hhhhK hhubah}(h]h ]h"]h$]h&]uh1hhhubh)}(h...h]h...}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhubeh}(h]h ]h"]h$]h&]uh1hhhhK hhhhubh)}(h这里系统认为在默认采样周期內有10.01%的时间工作在用户空间,2.92%的时 间用在系统空间,总体上有81.63%的时间是空闲的。h]h这里系统认为在默认采样周期內有10.01%的时间工作在用户空间,2.92%的时 间用在系统空间,总体上有81.63%的时间是空闲的。}(hj/hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh)}(h大多数情况下``/proc/stat``的信息几乎真实反映了系统信息,然而,由于内 核采集这些数据的方式/时间的特点,有时这些信息根本不可靠。h]h大多数情况下``/proc/stat``的信息几乎真实反映了系统信息,然而,由于内 核采集这些数据的方式/时间的特点,有时这些信息根本不可靠。}(hj=hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh)}(hXZ那么这些信息是如何被搜集的呢?每当时间中断触发时,内核查看此刻运行的 进程类型,并增加与此类型/状态进程对应的计数器的值。这种方法的问题是 在两次时间中断之间系统(进程)能够在多种状态之间切换多次,而计数器只 增加最后一种状态下的计数。h]hXZ那么这些信息是如何被搜集的呢?每当时间中断触发时,内核查看此刻运行的 进程类型,并增加与此类型/状态进程对应的计数器的值。这种方法的问题是 在两次时间中断之间系统(进程)能够在多种状态之间切换多次,而计数器只 增加最后一种状态下的计数。}(hjKhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh)}(h 举例 ---h]h 举例 ---}(hjYhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh)}(hA假设系统有一个进程以如下方式周期性地占用cpu::h]h@假设系统有一个进程以如下方式周期性地占用cpu:}(hjghhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh literal_block)}(h 两个时钟中断之间的时间线 |-----------------------| ^ ^ |_ 开始运行 | |_ 开始睡眠 (很快会被唤醒)h]h 两个时钟中断之间的时间线 |-----------------------| ^ ^ |_ 开始运行 | |_ 开始睡眠 (很快会被唤醒)}hjwsbah}(h]h ]h"]h$]h&] xml:spacepreserveuh1juhhhK!hhhhubh)}(h在上面的情况下,根据``/proc/stat``的信息(由于当系统处于空闲状态时, 时间中断经常会发生)系统的负载将会是0h]h在上面的情况下,根据``/proc/stat``的信息(由于当系统处于空闲状态时, 时间中断经常会发生)系统的负载将会是0}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK(hhhhubh)}(h大家能够想象内核的这种行为会发生在许多情况下,这将导致``/proc/stat`` 中存在相当古怪的信息::h]h大家能够想象内核的这种行为会发生在许多情况下,这将导致``/proc/stat`` 中存在相当古怪的信息:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK+hhhhubjv)}(hX/* gcc -o hog smallhog.c */ #include #include #include #include #define HIST 10 static volatile sig_atomic_t stop; static void sighandler (int signr) { (void) signr; stop = 1; } static unsigned long hog (unsigned long niters) { stop = 0; while (!stop && --niters); return niters; } int main (void) { int i; struct itimerval it = { .it_interval = { .tv_sec = 0, .tv_usec = 1 }, .it_value = { .tv_sec = 0, .tv_usec = 1 } }; sigset_t set; unsigned long v[HIST]; double tmp = 0.0; unsigned long n; signal (SIGALRM, &sighandler); setitimer (ITIMER_REAL, &it, NULL); hog (ULONG_MAX); for (i = 0; i < HIST; ++i) v[i] = ULONG_MAX - hog (ULONG_MAX); for (i = 0; i < HIST; ++i) tmp += v[i]; tmp /= HIST; n = tmp - (tmp / 3.0); sigemptyset (&set); sigaddset (&set, SIGALRM); for (;;) { hog (n); sigwait (&set, &i); } return 0; }h]hX/* gcc -o hog smallhog.c */ #include #include #include #include #define HIST 10 static volatile sig_atomic_t stop; static void sighandler (int signr) { (void) signr; stop = 1; } static unsigned long hog (unsigned long niters) { stop = 0; while (!stop && --niters); return niters; } int main (void) { int i; struct itimerval it = { .it_interval = { .tv_sec = 0, .tv_usec = 1 }, .it_value = { .tv_sec = 0, .tv_usec = 1 } }; sigset_t set; unsigned long v[HIST]; double tmp = 0.0; unsigned long n; signal (SIGALRM, &sighandler); setitimer (ITIMER_REAL, &it, NULL); hog (ULONG_MAX); for (i = 0; i < HIST; ++i) v[i] = ULONG_MAX - hog (ULONG_MAX); for (i = 0; i < HIST; ++i) tmp += v[i]; tmp /= HIST; n = tmp - (tmp / 3.0); sigemptyset (&set); sigaddset (&set, SIGALRM); for (;;) { hog (n); sigwait (&set, &i); } return 0; }}hjsbah}(h]h ]h"]h$]h&]jjuh1juhhhK.hhhhubh)}(h 参考 ---h]h 参考 ---}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK_hhhhubh bullet_list)}(hhh](h list_item)}(hAhttps://lore.kernel.org/r/loom.20070212T063225-663@post.gmane.orgh]h)}(hjh]h reference)}(hjh]hAhttps://lore.kernel.org/r/loom.20070212T063225-663@post.gmane.org}(hjhhhNhNubah}(h]h ]h"]h$]h&]refurijuh1jhjubah}(h]h ]h"]h$]h&]uh1hhhhKbhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(h*Documentation/filesystems/proc.rst (1.8) h]h)}(h(Documentation/filesystems/proc.rst (1.8)h]h(Documentation/filesystems/proc.rst (1.8)}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKchjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubeh}(h]h ]h"]h$]h&]bullet-uh1jhhhKbhhhhubh)}(h 谢谢 ---h]h 谢谢 ---}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKfhhhhubh)}(hCon Kolivas, Pavel Machekh]hCon Kolivas, Pavel Machek}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKihhhhubeh}(h]cpuah ]h"] cpu 负载ah$]h&]uh1hhhhhhhhKubeh}(h]h ]h"]h$]h&]sourcehuh1hcurrent_sourceN current_lineNsettingsdocutils.frontendValues)}(hN generatorN datestampN source_linkN source_urlN toc_backlinksentryfootnote_backlinksK sectnum_xformKstrip_commentsNstrip_elements_with_classesN strip_classesN report_levelK halt_levelKexit_status_levelKdebugNwarning_streamN tracebackinput_encoding utf-8-siginput_encoding_error_handlerstrictoutput_encodingutf-8output_encoding_error_handlerjPerror_encodingutf-8error_encoding_error_handlerbackslashreplace language_codeenrecord_dependenciesNconfigN id_prefixhauto_id_prefixid dump_settingsNdump_internalsNdump_transformsNdump_pseudo_xmlNexpose_internalsNstrict_visitorN_disable_configN_sourceh _destinationN _config_files]7/var/lib/git/docbuild/linux/Documentation/docutils.confafile_insertion_enabled raw_enabledKline_length_limitM'pep_referencesN pep_base_urlhttps://peps.python.org/pep_file_url_templatepep-%04drfc_referencesN rfc_base_url&https://datatracker.ietf.org/doc/html/ tab_widthKtrim_footnote_reference_spacesyntax_highlightlong smart_quotessmartquotes_locales]character_level_inline_markupdoctitle_xform docinfo_xformKsectsubtitle_xform image_loadinglinkembed_stylesheetcloak_email_addressessection_self_linkenvNubreporterNindirect_targets]substitution_defs}substitution_names}refnames}refids}nameids}j*j's nametypes}j*sh}j'hs footnote_refs} citation_refs} autofootnotes]autofootnote_refs]symbol_footnotes]symbol_footnote_refs] footnotes] citations]autofootnote_startKsymbol_footnote_startK id_counter collectionsCounter}Rparse_messages](hsystem_message)}(hhh]h)}(heUnexpected possible title overline or transition. Treating it as ordinary text because it's so short.h]hgUnexpected possible title overline or transition. Treating it as ordinary text because it’s so short.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]levelKtypeINFOlineKsourcehuh1jhhubj)}(hhh]h)}(hfPossible title underline, too short for the title. Treating it as ordinary text because it's so short.h]hhPossible title underline, too short for the title. Treating it as ordinary text because it’s so short.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]levelKtypejlineKsourcehuh1jhhhhhhhKubj)}(hhh]h)}(hfPossible title underline, too short for the title. Treating it as ordinary text because it's so short.h]hhPossible title underline, too short for the title. Treating it as ordinary text because it’s so short.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]levelKtypejlineK`sourcehuh1jhhhhhhhK`ubj)}(hhh]h)}(hfPossible title underline, too short for the title. Treating it as ordinary text because it's so short.h]hhPossible title underline, too short for the title. Treating it as ordinary text because it’s so short.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]levelKtypejlineKgsourcehuh1jhhhhhhhKgubetransform_messages] transformerN include_log] decorationNhhub.