The complexity of app environments and growing number of processes will cause contention and waste of resources if restrictions are applied on containers. Process Limits (plimits) is a mechanism provided by the kernel to limit the resources used by a single process or multiple processes. It can implement refined control on resources such as CPUs and memory.
plimitsfs is a file system that provides an interface for creating and deleting plimits. plimitsfs enables processes and process resources to be grouped for management through operations on files. Plimiters are configured to restrict the usage of resources, such as memory and sched, in process groups.
## Basic Concepts
- plimits: a feature provided by the kernel to limit, record, and isolate the resources used by a group of processes.
- plimitsfs: a file system, which provides an interface for users to create and delete plimits and displays the plimits directories.
- plimiter: a collection of resource limiters. plimiter includes the memory limiter, pids limiter, and sched limiter.
- sched limiter: limits the time to use CPUs for all processes in a plimits group in a specified period.
- memory limiter: limits the total memory that can be used by all processes in a plimits group.
- pids limiter: limits the maximum number of processes that can be mounted in a plimits group.
## Working Principles
During the system initialization process, the **plimits** directory is mounted to the **proc** directory.
```
├─proc
│ ├─plimits
│ │ ├─plimits.plimiter_add
│ │ ├─plimits.plimiter_delete
│ │ ├─plimits.procs
│ │ ├─plimits.limiters
│ │ ├─pids.max
│ │ ├─sched.period
│ │ ├─sched.quota
│ │ ├─sched.stat
│ │ ├─memory.failcnt
│ │ ├─memory.limit
│ │ ├─memory.peak
│ │ ├─memory.usage
│ │ ├─memory.oom_ctrl
│ │ └─memory.stat
```
- plimits groups
**Figure 1** Creating or deleting plimits
![](figures/create_delete_plimits.png)
- sched limiter
**Figure 2** sched limiter configuration
![](figures/sched_limiter.png)
- Memory limiter
**Figure 3** Memory limiter configuration
![](figures/memory_limiter.png)
- pids limiter
**Figure 4** pids limiter configuration
![](figures/pids_limiter.png)
## How to Develop
### Available APIs
The plimits root directory of LiteOS-A is in **/proc/plimits**. All files in this directory are read-only. The values setting in the limiter file are maximum values by default. You can view the status of process resources in the group from the files.
Run the **mkdir** command to create a **plimitsA** directory to group process resources and restrict resource allocation. The created **plimitsA** directory inherits its parent **plimits** directory.
The following table lists the files in the **plimitsA** directory.
| Permissions | Size| User| User Group| File | Description|
|-r--r--r-- | 0 | u:0 | g:0 | sched.stat | Time slice of each thread in the last period. The time slice information is used for test and verification.|
|-rw-r--r-- | 0 | u:0 | g:0 | sched.quota | Sum of time slices of all processes in a group in a period, in ns.|
|-rw-r--r-- | 0 | u:0 | g:0 | sched.period | Statistical period of the time slice, in ns.|
|-r--r--r-- | 0 | u:0 | g:0 | memory.failcnt | Number of memory allocation failures when the memory usage exceeds the limit.|
|-rw-r--r-- | 0 | u:0 | g:0 | pids.max | Maximum number of processes that can be mounted to a group.|
|-rw-r--r-- | 0 | u:0 | g:0 | plimits.procs | All processes mounted to a group.|
|-rw-r--r-- | 0 | u:0 | g:0 | plimits.limiter_delete | Used to delete a limiter. |
|-rw-r--r-- | 0 | u:0 | g:0 | plimits.limiter_add | Used to add a limiter. |
|-r--r--r-- | 0 | u:0 | g:0 | plimits.limiters | Used to view the limiters of a group. |
All the files in the **plimitsA** directory in **/proc/plimits/** are readable, and some are writable. You can allocate and restrict process resources by writing data to the files in the **plimitsA** directory. You can:
- Write time, in ns, to the **sched.quota** file to limit the time for all processes in the group to use CPUs.
- Write time, in ns, to the **sched.period** file to set the period for collecting statistics in a group.
- Write the memory, in bytes, to the **memory.limit** file to limit the memory that can be used by a group.
- Write a decimal number to the **pids.max** file to limit the number of processes that can be mounted to a group.
- Write process IDs (PIDs) to the **plimits.procs** file to mount processes to different plimits groups.
- Read files to view the resource usage configuration of a group.
#### Deleting the **plimitsA** Group
Write **sched**, **memory**, and **pids** to the **/proc/plimits/plimitsA/plimits.limiter_delete** file in sequence to delete the limiters, and then run the **rmdir** command to delete **plimitsA**.
1. Create **plimitsA** and write PIDs to **/plimitsA/plimits.procs** to group process resources.
2. Write the **/plimitsA/memory.limit** file to limit the maximum memory that can be used by the **plimitsA** group.
3. Write a decimal number to the **/plimitsA/pids.max** file to limit the number of processes that can be mounted to the **plimitsA** group.
4. Configure the limiter files in the **plimitsA** group to allocate and limit resources. If you do not want to limit the use of resources, delete **plimitsA**.
### Development Example
The following example demonstrates how to create the **plimitsA** group and implement resource control of this group by reading and writing the files of **plimitsA**.