GNU/Linux >> Linux の 問題 >  >> Linux

lxc-execute でメモリと CPU を制限する

まず、LXC ユーティリティの一部である Cgroup について理解していただきたいと思います。コンテナーがある場合は、実行しているさまざまなコンテナーが他のコンテナーまたはプロセス内で不足していることを確認したいことは明らかです。これを念頭に置いて、LXC プロジェクトのナイスガイ a.k.a Daniel Lezcano は、彼が作成していたコンテナ技術、つまり LXC に cgroup を統合しました。リソース使用量を割り当てたい場合は、CGROUP の構成を検討する必要があります。Cgroups を使用すると、CPU 時間、システム メモリ、ネットワーク帯域幅、またはこれらのリソースの組み合わせなどのリソースを、ユーザー定義のタスク グループ間で割り当てることができます。システム上で実行されている (プロセス)。設定した cgroup を監視したり、特定のリソースへの cgroup のアクセスを拒否したり、実行中のシステムで cgroup を動的に再設定したりすることもできます。 cgconfig (コントロール グループ構成) サービスは、起動時に起動し、定義済みの cgroup を再確立するように構成できます。これにより、再起動後も保持されます。各階層は 1 つ以上のサブシステム (リソースとも呼ばれる) に接続されているため、cgroup は複数の階層を持つことができます。コントローラーまたはコントローラー)。これにより、接続されていない複数のツリーが作成されます。 9 つのサブシステムが利用可能です。

<オール>
  • blkio は、ブロック デバイスの入出力アクセスに制限を設定します
  • CPU への cgroup タスク アクセス用の cpu スケジューラ
  • cpuacct は、CPU 使用率と cgroup のレポートを生成します
  • cpuset は CPU とメモリを cgroup に割り当てます
  • デバイスはタスクごとにデバイスへのアクセスを管理します
  • 冷凍庫の一時停止/再開タスク
  • メモリ制限メモリ
  • ネットワーク パケットに net_cls タグを付けて、Linux トラフィック コントローラがタスク トラフィックを識別できるようにする
  • ns 名前空間
  • コマンドでカーネルにあるサブシステムを一覧表示できます:

    lssubsys –am
    

    lxc-cgroup コンテナ名に関連付けられたコントロール グループから値を取得または設定します。コンテナに関連付けられたコントロール グループを管理します。使用例:

    lxc-cgroup -n foo cpuset.cpus "0,3" 
    

    プロセッサ 0 と 3 をコンテナに割り当てます。

    さて、私はあなたの最初の質問に答えたと思います。しかし、lxc を使用するためにコンテナーを構成するのに役立つかもしれないパラメーターを少し追加させてください。 redhat によるリソース制御のドキュメントの要約があります

    BLKIO の変更可能なパラメーター:

        blkio.reset_stats : any int to reset the statistics of BLKIO
        blkio.weight : 100 - 1000 (relative proportion of block I/O access)
        blkio.weight_device : major, minor , weight 100 - 1000 
        blkio.time : major, minor and time (device type and node numbers and length of access in milli seconds)
        blkio.throttle.read_bps_device : major, minor specifies the upper limit on the number of read operations a device can perform. The rate of the read operations is specified in bytes per second.
        blkio.throttle.read_iops_device :major, minor and operations_per_second specifies the upper limit on the number of read operations a device can  perform
        blkio.throttle.write_bps_device : major, minor and bytes_per_second (bytes per second)
        blkio.throttle.write_iops_device : major, minor and operations_per_second
    
    <ブロック引用>

    CFS の変更可能なパラメータ:

        cpu.cfs_period_us : specifies a period of time in microseconds for how regularly a cgroup's access to CPU resources should be reallocated. If tasks in a cgroup should be able to access a single CPU for 0.2 seconds out of every 1 second, set cpu.cfs_quota_us to 200000 and cpu.cfs_period_us to 1000000.
        cpu.cfs_quota_us : total amount of time in microseconds that all tasks in a cgroup can run during one period. Once limit has reached, they are not allowed to run beyond that. 
        cpu.shares : contains an integer value that specifies the relative share of CPU time available to tasks in a cgroup.
    
    
        Note: For example, tasks in two cgroups that have cpu.shares set to 1 will receive equal CPU time, but tasks in a cgroup that has cpu.shares set to 2 receive twice the CPU time of tasks in a cgroup where cpu.shares is set to 1. Note that shares of CPU time are distributed per CPU. If one cgroup is limited to 25% of CPU and another cgroup is limited to 75% of CPU, on a multi-core system, both cgroups will use 100% of two different CPUs. 
    

    RT の変更可能なパラメーター:

    cpu.rt_period_us : time in microseconds for how regularly a cgroups access to CPU resources should be reallocated. 
    cpu.rt_runtime_us : same as above.
    

    CPU セット :

    cpuset subsystem assigns individual CPUs and memory nodes to cgroups.
    Note: here some parameters are mandatory
    Mandatory: 
    
    
    cpuset.cpus : specifies the CPUs that tasks in this cgroup are permitted to access. This is a comma-separated list in ASCII format, with dashes (" -")                 to represent ranges. For example 0-2,16 represents CPUs 0, 1, 2, and 16. 
            cpuset.mems : specifies the memory nodes that tasks in this cgroup are permitted to access. same as above format
    
    
    Optional: 
            cpuset.cpu_exclusive : contains a flag ( 0 or 1) that specifies whether cpusets other than this one and its parents and children can share the CPUs specified for this cpuset. By default ( 0), CPUs are not allocated exclusively to one cpuset. 
            cpuset.mem_exclusive : contains a flag ( 0 or 1) that specifies whether other cpusets can share the memory nodes specified for this cpuset. By default ( 0), memory nodes are not allocated exclusively to one cpuset. Reserving memory nodes for the exclusive use of a cpuset ( 1) is functionally the same as enabling a memory hardwall with the cpuset.mem_hardwall parameter.
            cpuset.mem_hardwall : contains a flag ( 0 or 1) that specifies whether kernel allocations of memory page and buffer data should be restricted to the memory nodes specified for this cpuset. By default ( 0), page and buffer data is shared across processes belonging to multiple users. With a hardwall enabled ( 1), each tasks' user allocation can be kept separate.
            cpuset.memory_pressure_enabled : contains a flag ( 0 or 1) that specifies whether the system should compute the memory pressure created by the processes in this cgroup
            cpuset.memory_spread_page : contains a flag ( 0 or 1) that specifies whether file system buffers should be spread evenly across the memory nodes allocated to this cpuset. By default ( 0), no attempt is made to spread memory pages for these buffers evenly, and buffers are placed on the same node on which the process that created them is running. 
            cpuset.memory_spread_slab : contains a flag ( 0 or 1) that specifies whether kernel slab caches for file input/output operations should be spread evenly across the cpuset. By default ( 0), no attempt is made to spread kernel slab caches evenly, and slab caches are placed on the same node on which the process that created them is running.
            cpuset.sched_load_balance : contains a flag ( 0 or 1) that specifies whether the kernel will balance loads across the CPUs in this cpuset. By default ( 1), the kernel balances loads by moving processes from overloaded CPUs to less heavily used CPUs.
    

    デバイス:

    The devices subsystem allows or denies access to devices by tasks in a cgroup. 
        devices.allow : specifies devices to which tasks in a cgroup have access. Each entry has four fields: type, major, minor, and access.
        type can be of following three values: 
            a - applies to all devices
            b - block devices
            c - character devices
        access is a sequence of one or more letters: 
            r read from device
            w write to device
            m create device files that do not yet exist
    
        devices.deny : similar syntax as above
        devices.list : reports devices for which access control has been set for tasks in this cgroup
    

    メモリ:

    メモリ サブシステムは、cgroup 内のタスクによって使用されるメモリ リソースに関する自動レポートを生成し、それらのタスクによるメモリ使用の制限を設定します。キロの場合は K、メガの場合は M などの接尾辞を使用できます。これは、階層内の下位のグループのみを制限します。つまり、ルート cgroup を制限することはできませんmemory.memsw.limit_in_bytes :メモリとスワップの合計使用量の最大量を設定します。繰り返しますが、これはルート cgroup を制限できません。

        Note: memory.limit_in_bytes should always be set before memory.memsw.limit_in_bytes because only after limit, can swp limit be set
        memory.force_empty : when set to 0, empties memory of all pages used by tasks in this cgroup
        memory.swappiness : sets the tendency of the kernel to swap out process memory used by tasks in this cgroup instead of reclaiming pages from the page cache. he default value is 60. Values lower than 60 decrease the kernel's tendency to swap out process memory, values greater than 60 increase the kernel's tendency to swap out process memory, and values greater than 100 permit the kernel to swap out pages that are part of the address space of the processes in this cgroup. 
    
    
        Note: Swappiness can only be asssigned to leaf groups in the cgroups architecture. i.e if any cgroup has a child cgroup, we cannot set the swappiness for that
        memory.oom_control : contains a flag ( 0 or 1) that enables or disables the Out of Memory killer for a cgroup. If enabled ( 0), tasks that attempt to consume more memory than they are allowed are immediately killed by the OOM killer. 
    

    net_cls:

    net_cls サブシステムは、Linux トラフィック コントローラー (tc) が特定の cgroup から発信されたパケットを識別できるようにするクラス識別子 (classid) でネットワーク パケットにタグを付けます。トラフィック コントローラーは、異なる cgroup からのパケットに異なる優先順位を割り当てるように構成できます。

    net_cls.classid : 0XAAAABBBB AAAA = major number (hex)
                             BBBB = minor number (hex)
            net_cls.classid contains a single value that indicates a traffic control handle. The value of classid read from the net_cls.classid file is presented in the decimal format while the value to be written to the file is expected in the hexadecimal format. e.g. 0X100001 = 10:1
    

    net_prio :

    Network Priority ( net_prio) サブシステムは、さまざまな cgroup 内のアプリケーションの各ネットワーク インターフェイスごとに、ネットワーク トラフィックの優先度を動的に設定する方法を提供します。ネットワークの優先順位は、ネットワーク トラフィックに割り当てられ、システムおよびネットワーク デバイスによって内部的に使用される番号です。ネットワーク プライオリティは、送信、キューイング、またはドロップされるパケットを区別するために使用されます。トラフィック コントローラ (tc) は、ネットワークの優先順位を設定する責任があります。

    net_prio.ifpriomap : networkinterface , priority (/cgroup/net_prio/iscsi/net_prio.ifpriomap)
            Contents of the net_prio.ifpriomap file can be modified by echoing a string into the file using the above format, for example:
    
                ~]# echo "eth0 5" > /cgroup/net_prio/iscsi/net_prio.ifpriomap
    

    Linux
    1. Linuxで最も高いメモリとCPU使用率で上位のプロセスを検索する

    2. メモリとCPU使用率で上位のプロセスを見つける方法

    3. PHPのメモリ制限

    1. メモリとCPUの使用量で実行中のプロセスのトップ10を探す

    2. Linuxで単一プロセスのCPU使用率とメモリ使用率を取得しますか?

    3. LAMP を使用したサーバー CPU および GPU

    1. Glanceを使用したLinuxおよびWindowsホストの監視

    2. DockerのメモリとCPU使用率の制限を設定する方法

    3. Apache と PHP でメモリ リークを調査するにはどうすればよいですか?