Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to check some resource restrictions under Linux

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to check some resource restrictions under Linux". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to view some resource restrictions under Linux".

Preface

When we write programs, we often don't pay attention to the critical values of some system resources, but sometimes these thresholds will hurt us badly, such as a file descriptor that forgets to close, such as malloc will return an error, or a stack burst, how can we solve or prevent these problems?

The following experiments are valid only in the local system environment:

User-level resource restrictions

The ulimit command can view system resource limits at the user level. This is the description in / etc/security/limits.conf:

This file sets resource limits for users who log in through PAM.

It does not affect the resource limitations of system services.

Also note the configuration file in the / etc/security/limits.d directory, which is read in alphabetical order, please overwrite the file if this setting domain is the same or more specific.

For example, this means that the restrictions for setting wildcard domains here can override subdirectories with wildcard settings in the configuration file, but user-specific settings here can only be overridden with user-specific settings in subdirectories.

So ulimit is really looking at resource constraints at the user level.

We can view all our resource ceilings through ulimit-a:

Just talk about the ones we're more concerned about:

-s stack size: 8MB

-u process limit: more than 30000

-n upper limit of file descriptor: 1024

At the same time, you can use ulimit-Ha or ulimit-Sa to view hard and soft limits. Hard limits refer to absolute limits on resource nodes and data blocks, and hard limits are set by root users. While other users can reduce the hard limit, only root users can increase it. As for the soft limit, the online material doesn't say much, probably that non-root users can't exceed the soft limit, but what non-root users can do is add their soft limit to their hard limit.

Our server program may have more than 1024 file descriptors open. Is there any way to modify the upper limit of these resources?

E.G. Ulimit-n 1024 can modify the system's restrictions on file descriptors, but the current shell takes effect temporarily. If you use which ulimit, you will find that ulimit is a shell built-in command script.

We should modify / etc/security/limits.conf to make our changes permanent. (reboot is required. There may be a way to load the configuration directly. I don't know yet.)

Experiment 1. Modify the upper limit of file descriptor

Add the following fragment to / etc/security/limits.conf:

After restarting, then check to see if the resource has really been modified:

This indicates that the modification was successful. So now let's test whether our program can open so many file descriptors? To do a little test, here's how to open 10240 temporary files, here we expect error EFILE:

Then let's take a look at the results:

Before the modification, the default value of ulimit is 1024, and then the maximum number of open file descriptors tested is 1001. Now 10217 file descriptors can be opened after being modified to 10240. The experiment is successful. Then why not exactly 10240 of the total we can open? This problem is because the program itself opens some files or loads some dynamic libraries, stdin/stdout/stderr, and / etc/ld.so.cache,/usr/lib/libm.so.6,/usr/lib/libstdc++.so.6...

Experiment 2. Modify the upper limit of stack space

Again, add two sentences to / etc/security/limits.conf:

Then test the upper limit of the stack frame in the c program

The program is normal. Set the stack to the critical value:

A segment error occurred in the program.

But it can only be conserved here: the stack space of a process after adjustment is about 8192000B.

System-level resource constraints

The maximum number of file descriptors opened by a single process is 1 billion.

The upper limit of pid allocated by the system is more than 4 million.

File-max is the maximum file descriptor (FD) enforced at the kernel level, with an upper limit of 6 million.

The number of file descriptors assigned, the number of file descriptors assigned but not used, and the maximum number of file descriptors (non-tunable).

The number of buses in the system is limited to 60,000.

The amount of memory-mapped space that can be used by a single program is 60,000.

The total number of threads that can be created is related to these:

Resource constraints for a process

Adjustment of the upper limit of file descriptors in redis

You don't think resource restrictions have anything to do with you? When you opened redis-server, didn't you notice the following paragraph:

Increased maximum number of open files to 10032 (it was originally set to 1024).

The meaning is to adjust the file descriptor from the default upper limit to 10032 in order to accommodate more network connections.

Api is only called in the source code:

Setrlimit (RLIMIT_NOFILE,&limit)

To make a temporary adjustment of the resource ceiling, which will not be discussed in detail here.

Prlimit

Finally, I introduce another ulimit-like command, prlimit:

Conclusion

The resource limit of linux can't be said to be wonderful, but it is worth paying attention to by programmers who do linux server programming, and we can modify the resource limit by / etc/security/limits.conf. I suddenly thought of asking the senior student last time: why do we need to restrict these resources under linux? Wouldn't it be nice to adjust them to ulimited?

It can be said that our linux machine limits the upper limit of these resources in the hope that we can make full use of it and maximize its performance, rather than leaving resources such as CPU or files idle and wasting the life of the computer.

At this point, I believe you have a deeper understanding of "how to view some resource restrictions under Linux". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report