Set ulimit and core file generation in Linux

Source: Internet
Author: User

Set ulimit in Linux
And core file generation

View limits ulimit-

The following information is displayed:

Core filesize (blocks,-C) 0
Data seg size (Kbytes,-d) Unlimited
Filesize (blocks,-f) Unlimited
Pendingsignals (-I) 1024
Max locked memory (Kbytes,-l) 32
Max memory size (Kbytes,-m) Unlimited
Openfile (-N) 1024
Pipe size (512 bytes,-p) 8
POSIX message queues (bytes,-q) 819200
Stacksize (Kbytes,-S) 10240
Cputime (seconds,-T) Unlimited
Max userprocesses (-u) 4096
Virtual Memory (Kbytes,-v) Unlimited
Filelocks (-x) Unlimited

What we need to modify is the value of open files (-N) 1024.

The command is limit-N 2048 (set as needed)

Bytes -----------------------------------------------------------------------------------

Function Description:Controls shell program resources.

Syntax:Ulimit [-ahs] [-C <Core File limit>] [-D <Data Partition size>] [-F <file size>] [-M <memory size>] [-n <number of files>] [-P <buffer size>] [-S <stack size>] [-T <CPU time>] [-u <program count>] [-v <virtual memory size>]

Note:Ulimit is a built-in shell command that can be used to control shell execution program resources.

Parameters:
-A: displays the current resource limit settings.
-C <Core File limit> sets the maximum value of the core file, in blocks.
-D <Data Partition size> maximum value of the Program Data Partition, in KB.
-F <file size> the maximum file that can be created by shell. The unit is block.
-H: Set the hard limit of resources, that is, the limit set by the Administrator.
-M <memory size> specifies the maximum memory available, in KB.
-N <file quantity> specifies the maximum number of files that can be opened at the same time.
-P <buffer size> specifies the size of the MPs queue buffer, in 512 bytes.
-S <stack size> specifies the maximum number of stacks, in KB.
-S: Set the elastic limit of resources.
-T <CPU time> specifies the upper limit of CPU usage time, in seconds.
-U <program count> the maximum number of programs that can be opened by users.
-V <virtual memory size> specifies the upper limit of virtual memory available, in KB.

------------------

1, note:
Ulimit is used for resources occupied by Shell startup processes.

2. Category:
Shell built-in commands

3. Syntax format:
Ulimit [-acdfhlmnpsstvw] [size]

4. Parameter introduction:

5. Simple Example:

1] In the rh8 environment file/etc/profile, we can see how the system configures ulimit:

This statement sets the software resources and core file size.

2] If we want to limit the file size created by shell, for example:

The file H size is 150062 bytes, and the size of the file we set to create is 512 bytes X 51200 bytes = bytes
Of course, the system will generate a 51200-byte newh file based on your settings.

3] Put the ulimit you want to set in the environment file/etc/profile, just like instance 1.

Bytes ------------------------------------------------------------------------------------------------------------------

When some programs in the system encounter some errors and crash, the system will automatically generate core files to record the crash time system information, including memory and register information, which can be used by programmers in future debugging. These errors include segment errors, invalid commands, bus errors, or user-generated exit information. Generally, core files are stored in the current folder.

The core file may not appear in your current folder when an error occurs. There are two reasons for this: one is that the current terminal is set to not be able to pop up the core file; the other is that the path of the core file is specified.

For the former, we can useUlimitCommand to set the core file size. By default, the core file size is set to 0, so that the system will not dump the core file. In this case, run the following command:Ulimit-C UnlimitedYou can set the size of the core file to an infinitely large value. You can also use numbers to replace unlimited to set the upper limit of the Core File more accurately.

In addition to setting the core file size, you can also specify the core file name. This setting modifies the/proc/sys/kernel/core_pattern and/proc/sys/kernel/core_uses_pid files. The methods for modifying these two files are as follows:

Echo <pattern>/proc/sys/kernel/core_pattern

Echo <"0"/"1">/proc/sys/kernel/core_uses_pid

Note that only the Super User can modify the two tables.

Core_pattern accepts the pattern of the core file name. It contains any string and uses % as the transfer symbol to generate some identifiers, adding special meanings to the core file name. The defined identifiers are as follows:

%: Equivalent to %

% P: equivalent to <pid>

% U: equivalent to <uid>

% G: equivalent to <GID>

% S: number equivalent to the signal that causes dump

% T: equivalent to dump time

% E: equivalent to the name of the execution File

% H: equivalent to hostname

In addition to the above signs, it also stipulates:

1. The single % at the end can be directly removed;

2. Add % to any character other than the above, and the % and this character will be removed;

3. All other characters are added as common characters;

4. The maximum name of the core file is 64 bytes (including '\ 0 ');

5. The default pattern in core_pattern is core;

6. To maintain compatibility, you can add % P to the end of the core file by setting core_uses_pid;

7. pattern can contain path information.

Bytes ------------------------------------------------------------------------------------------------------------------

The following documents are collected from the Internet. The references are as follows:

Http://blog.csdn.net/hanchaoman/archive/2009/08/03/4405655.aspx

Http://www.mysys-admin.org/category/general/

1. You can use ulimit-a to view the stack size.
In kernel 2.6.20, the stack size is 8192 Kbytes.
If there is no limit, the size of the stack is limited by the memory. 2 GB is the upper limit.

2. Core File
* Enable or disable the generation of core files
Ulimit-C can check whether this option is enabled. If it is 0, it is disabled;
Ulimit-C 0 can be manually disabled
Ulimit-C 1000 sets the core file size to 1000 kb

Ulimit-cunlimited: Set the core file size to unlimited.

Many systems disable generating core files by default. This command can be added to your profile.

3. Set the core dump file directory and naming rules for core dump.

By default, many system core files are generated in the directory where you run the program, or the directory after chdir in the program, then a PID is added to the back of the core file. In actual work, this may cause many directories to generate core files, which is not easy to manage. In fact, in 2.6, the location where the core file is generated and the name of the file name can be configured.

/Proc/sys/kernel/core_uses_pid can be used to control whether the PID is added as an extension to the file name of the generated core file. If it is added, the file content is 1; otherwise, it is 0.
Proc/sys/kernel/core_pattern can be used to set the location or file name of the formatted core file. For example, the original file content is core-% E
You can modify it as follows:
Echo "/tmp/Core-% E-% P"> core_pattern
The generated core file is stored in the/corefile directory, and the generated file name is core-command name-PID-timestamp.
The following is a list of parameters:
% P-insert PID into filename add PID
% U-insert current uid into filename add current uid
% G-insert current GID into filename add current GID
% S-insert signal that caused the coredump into thefilename is added to generate core Signal
% T-insert UNIX time that the coredump occurred into filename added the Unix time when the core file was generated
% H-insert hostname where the coredump happened into filename Add the Host Name
% E-insert coredumping executable name into filename add command name

Of course, you can do this in the following ways:
Sysctl-W kernel. core_pattern =/tmp/Core-% E-% P

These operations will be lost once the computer restarts. If you want to persist these operations, you can add the following in the/etc/sysctl. conf file:
Kernel. core_pattern =/tmp/Core % P

If you want to check the effect without restarting, run the following command:
Sysctl-P/etc/sysctl. conf

Bytes ------------------------------------------------------------------------------------------------------------------

Expert advice:

Solved problems:
A program P has been running on the server for a long time, and is often killed once every day (segment error ).

Current solution:
Use securecrt to open a terminal, set ulimit-cnulimited on the service, and then start the program p. The query result using the ulimite-a command is as follows:

Core File size (blocks,-C) Unlimited
Data seg size (Kbytes,-d) Unlimited
File size (blocks,-f) Unlimited
Pending signals (-I) 1024
Max locked memory (Kbytes,-l) 32
............
Indicates that the core file can be generated.

And test the core file using the kill-6 PID.

Current difficulties:

When you run the ulimit-C nulimited terminal (and the terminal starts program P to the background./P &) to close it, the program P does not generate a core file after it dies.
After the test, it is found that the ulimit command is related to the terminal.

Expert advice:
How to configure the ability to generate core files is not related to the terminal
That is, the program starts and closes the terminal. When the program dies (segment error), the core file can be generated.

In
/Etc/security/limits. conf (the RedHat derivative is set in Linux)
Or
In/etc/profile:
# No core files by default
Ulimit-s-c 0>/dev/null 2> & 1

Comment out the above line.

Other UNIX operating systems also have their own configuration files.

Bytes ------------------------------------------------------------------------------------------------------------------

GDB core Multithreading

Debugging with multiple threads in Linux is not as convenient as. net. These days I 've been struggling to find a deadlock bug for a long time. Let's introduce the methods I 've used.

Multi-thread dump is often a segment error, which generally involves illegal memory read/write. You can use the following command to open the system switch so that it can generate a core file when it dies.
Ulimit-cunlimited
In this case, you can see the core. PID (PID is the process number) file in the current directory. Next, use GDB:
GDB./bin./CORE. PID
After entering, use BT to view the stack situation when it is dead, and use the frame command.

There is also a thread in it that stops and does not die. This is generally a deadlock or timeout problem involving message acceptance (I have never encountered it, as I have heard of it ). In this case, you can use:
Gcore PID(PID Number of the debugging process)
Manually generate a core file and view the stack by using pstack (which does not seem to work properly in Linux. If you cannot see it, check the code carefully to see if it is in the IF, return, break, and continue statements. You need to analyze the nested locks.

Finally, it is useless to look at the code with a calm mind.

-------------------------------------

1, note:
Ulimit is used for resources occupied by Shell startup processes.
2. Category:
Shell built-in commands
3. Syntax format:
Ulimit [-acdfhlmnpsstvw] [size]
4. Parameter introduction:
-H: Set hardware resource limits.
-S: Set software resource limits.
-A: displays all current resource limits.
-C size: sets the maximum value of the core file. Unit: Blocks
-D size: sets the maximum value of the data segment. Unit: Kbytes
-F size: sets the maximum value of the file to be created. Unit: Blocks
-L size: sets the maximum value of the locked process in the memory. Unit: Kbytes
-M size: sets the maximum value of resident memory that can be used. Unit: Kbytes
-N size: sets the maximum value of the file descriptor that the kernel can open at the same time. Unit: N
-P size: sets the maximum value of the MPs queue buffer. Unit: Kbytes
-S size: sets the maximum value of the stack. Unit: Kbytes
-T size: sets the maximum CPU usage time. Unit: seconds
-V Size: sets the maximum value of virtual memory. Unit: Kbytes 5, simple instance:
5. Example
When writing a program in Linux, if the program is large, you will often encounter problems such as "segment error" (segmentation fault, this is mainly because the initial stack size (stack size) of the Linux system is too small, generally 10 m. I usually set the stack size to 256 m, so there is no segment error! Command:
Ulimit-s 262140
If you want the system to automatically remember this configuration, edit the/etc/profile file, under the "ulimit-s-c 0>/dev/null 2> & 1" line, add "ulimit-s 262140" and save and restart the system!
1] In the rh8 environment file/etc/profile, we can see how the system configures ulimit:
# Grep ulimit/etc/profile
Ulimit-s-c 0>/dev/null 2> & 1
This statement sets the software resources and core file size.
2] If we want to limit the file size created by shell, for example:
# Ll H
-RW-r -- 1 Lee 150062 July 22 02:39 H
# Ulimit-F 100 # set the maximum size of the file to be created (one block = 512 bytes)
# Cat h> newh
File size limit exceeded
# Ll newh
-RW-r -- 1 Lee 51200 November 8 11:47 newh
The file H size is 150062 bytes, and the size of the file we set to create is 512 bytes X 51200 bytes = bytes
Of course, the system will generate a 51200-byte newh file based on your settings.
3] Put the ulimit you want to set in the environment file/etc/profile, just like instance 1.
Purpose
Set or report user resource limits.
Syntax
Ulimit [-H] [-S] [-A] [-C] [-D] [-F] [-M] [-N] [-S] [-T] [limit]
Description
The ulimit command sets or reports the resource limits of user processes, as defined in the/etc/security/limits file. The file contains the following default limits:
Fsize = 2097151
Core = 2097151
CPU =-1
Data = 262144
RSS = 65536
Stacks = 65536
No files = 2000
These values are used as default values when new users are added to the system. When a user is added to the system, the preceding values are set using the mkuser command or the chuser command.
The limit can be soft or hard. With the ulimit command, you can change the soft limit to the maximum value of the hard limit. To change the hard limit of a resource, you must have the root user permission.
Many systems do not include one or more limits. The limit of a specific resource is set when the limit parameter is specified. The value of the limit parameter can be a number in a specified unit in each resource or an unlimited value. To set a specific ulimit to unlimited, you can use the word unlimited.
Note: In the/etc/security/limits file, setting the default limit is to set the system Width limit, not just the limit required by the user when creating the user.
When the limit parameter is omitted, the current resource limit is printed. The soft limit is printed unless the-H flag is specified. When you specify more than one resource, the limit name and unit are printed before the value. If no option is provided, it is assumed that the-F flag is included.
Because the ulimit command affects the current shell environment, it will be provided as a general built-in shell command. If you call this command in an independent command execution environment, the file size limit of the caller environment is not affected. This is the case in the following example:
Nohup ulimit-F 10000
Env ulimit 10000
Once the hard limit is reduced through the process, it cannot be increased without the root privilege, even if it is returned to the original value.
For more information about user and system resource limits, see getrlimit, setrlimit, or vlimit subroutines in Aix 5l version5.3 Technical Reference: base operating system and extensions volume 1.
Flag
-A lists the limits of all current resources.
-C uses 512 bytes to specify the core dump size.
-D specifies the size of the data area in K bytes.
-F sets the file size limit (in blocks) when the limit parameter is used, or reports the file size limit when no parameter is specified. The default value is the-F flag.
-H specifies the hard limit of a given resource. If you have root user permissions, you can increase the hard limit. Any user can reduce the hard limit.
-M specifies the size of the physical memory in K bytes.
-N specifies the maximum number of file descriptors that a process can possess.
-S specifies the size of the stack in K bytes.
-S specifies the soft limit for the given resource. The soft limit can be increased to the hard limit value. If the-H and-s signs are not specified, the limit applies to the above two.
-T specifies the number of seconds used by each process.
Exit status
The following exit values are returned:
0.
> 0: a request to a higher limit is rejected or an error occurs.
Example
To set the file size limit to 51,200 bytes, enter:
Ulimit-F 100

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.