Linux System Setup Ulimit and core file generation

Source: Internet
Author: User
Tags cpu usage


View the limitations of ulimit-a

You can see the following information

Core file size (blocks,-c) 0
Data seg Size (Kbytes,-D) Unlimited
File size (blocks,-f) Unlimited
Pending signals (-i) 1024
Max locked Memory (Kbytes, L) 32
Max memory Size (Kbytes,-m) unlimited
Open files (-N) 1024
Pipe Size (bytes,-p) 8
POSIX message queues (bytes,-Q) 819200
Stack size (Kbytes,-s) 10240
CPU time (seconds,-t) unlimited
MAX User Processes (-u) 4096
Virtual Memory (Kbytes,-V) Unlimited
File locks (-X) Unlimited

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

function Description:Control the resources of the shell program.

Syntax:ulimit [-ahs][-c <core file Cap >][-d < data section size >][-f < file size >][-m < memory size >][-n < number of files >][-p < buffer size >][-s < stack size >][-t <cpu time >][-u < number of programs >][-v < virtual memory size;]

Additional notes:Ulimit is a shell built-in directive that can be used to control the resources of shell execution programs.

Parameters:
-a displays the current resource limit settings.
-C <core File cap > set the maximum value of the core file, in chunks.
-D < data section size > Maximum value of the Program Data section, in kilobytes.
-F < file size > The largest file the shell can create, in chunks.
-H sets the hard limit for the resource, which is the limit set by the administrator.
-m < memory size > Specifies the maximum amount of memory that can be used, in kilobytes.
-N < number of files > specifies the maximum number of files that can be opened at the same time.
-p < buffer size > Specifies the size of the pipe buffer, in 512 bytes.
-s < stack size > specifies the upper limit of the stack in kilobytes.
-S sets the elastic limit for the resource.
-T &LT;CPU time > Specifies the maximum CPU usage time in seconds.
-U < number of programs > number of programs the user can open.
-v < virtual memory size > Specifies the maximum amount of virtual memory that can be used, in kilobytes.

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

1, Description:
ulimit The resources used by the shell startup process.

2, Category:
Shell built-in commands

3, Syntax format:
Ulimit [-ACDFHLMNPSSTVW] [size]

4, parameter description:

-H sets the hardware resource limit.
-S sets the software resource limit.
-a displays all current resource limits.
-C Size: Sets the maximum value of the core file. Unit: Blocks
-D Size: Sets the maximum value for the data segment. Unit: Kbytes
-F Size: Sets the maximum value of the created file. Unit: Blocks
-L Size: Sets the maximum value of the locked process in memory. Unit: Kbytes
-M Size: Sets the maximum number of resident memory that can be used. Unit: Kbytes
-N Size: Sets the maximum number of file descriptors that the kernel can open at the same time. Unit: N
-P Size: Sets the maximum value of the pipe buffer. Unit: Kbytes
-S size: Sets the maximum value of the stack. Unit: Kbytes
-T size: Sets the maximum CPU usage time limit. Unit: Seconds
-V Size: Sets the maximum value for virtual memory. Unit: Kbytes



5, simple example:

1] If we want to make some restrictions on the size of the file created by the shell, such as:

CODE:

#ll h
-rw-r--r--1 Lee Lee 150062 July 02:39 H
#ulimit-F #设置创建文件的最大块 (piece = 512 bytes)
#cat H>NEWH
File size limit exceeded
#ll NEWH
-rw-r--r--1 Lee Lee 51200 November 8 11:47 NEWH

The size of the file h is 150062 bytes, and the size of the created file we set is 512 bytes x100 block = 51200 bytes
Of course, the system generates a 51200-byte NEWH file based on your settings.

3] Put the ulimit you want to set in/etc/profile this environment file.


When some programs in the system encounter some errors and crash, the system automatically generates core file records crash time system information, including memory and register information, which can be used by programmers in future Debug. These errors include segment errors, illegal instructions, bus errors, or user-generated exit information, and so on, in general, the core file is stored in the current folder.

Sometimes the core file may not appear in your current folder when you have an error, for two reasons: one is that the current terminal is set to not eject the core file, and the other is that the core file is assigned a path.

For the former, we can use the ulimit command to set the size of the core file. By default, the core file size is set to 0 so that the system does not dump the core file. At this point, using the command:ulimit-c unlimiTEd to set up, you can set the core file size to infinity, but also can use the number to replace the unlimited, the core file on the upper limit for more accurate settings. (This method is temporarily active, exit is invalid)

In addition to setting the size of the core file, you can also specify the name of the core file. This setting is to modify the two files for/proc/sys/kernel/core_pattern and/proc/sys/kernel/core_uses_pid. Here are some ways to change these two files:

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

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

Also note that only Superuser can modify these two tables.

Core_pattern accepts the pattern of the core file name, which contains any string, and generates some identifiers with% as the transfer symbol, adding special meanings to the core file name. The identifiers that you have defined include the following:

Percent: equal to%

%p: Equivalent to <pid>

%u: equivalent to <uid>

%g: Equivalent to <gid>

%s: number equivalent to the signal that caused the dump

%t: equivalent to dump time

%e: Equivalent to the name of the execution file

%h: Equivalent to hostname

In addition to the above-mentioned signs, it also stipulates that:

1, the end of a single% can be directly removed;

2,% plus any character except above,% and the character will be removed;

3, all other words nonalphanumeric as a general character added to the name;

4, the core file name maximum value is 64 bytes (including '/0 ');

5, the default pattern in Core_pattern is the core;

6, in order to maintain compatibility, by setting core_uses_pid, you can add%p at the end of the core file;

7. The pattern can contain path information


1. You can use ULIMIT-A to see the size of the stack.

Under Kernel 2.6.20, stack size is 8192 kbytes
If there is no limit, the size of the stack is only limited by memory. 2 g is the upper limit.

2. Core file
* Turn core file generation on or off
Ulimit-c can see if this option is turned on, and if 0 is off;
Ulimit-c 0 can be manually closed
ULIMIT-C 1000 to set the core file size to 1000k max

ULIMIT-C Unlimited Set core file size to unlimited size

Many systems, by default, turn off the build core file, and this command can be added to your profile

3. Set the core dump file directory and naming rules

By default, many of the system's core files are generated in the directory where you run the program, or in the directory after you chdir the program, and then add a PID to the core file. In the actual work, this may cause a lot of directories to produce core files, not easy to manage, in fact, under 2.6, the core file generation location and the name of the filename is configurable.

/proc/sys/kernel/core_uses_pid can control whether a PID is added to the file name of the resulting core file, and if it is added, the content is 1, otherwise 0
Proc/sys/kernel/core_pattern can set the format of the core file save location or file name, such as the original file content is core-%e
This can be modified:
echo "/tmp/core-%e-%p" > Core_pattern
Will control the resulting core file will be stored in the/corefile directory, resulting in a file named core-command name-pid-timestamp
The following is a list of parameters:
%p-insert pid into filename add PID
%u-insert current UID to filename to add the present UID
%g-insert current GID into filename to add an existing GID
%s-insert signal that caused the coredump into the filename adds a signal that causes the core to be generated
%t-insert Unix time, the coredump occurred into filename when you add a core file when you build Unix
%h-insert hostname where the coredump happened into filename adds host name
%e-insert coredumping executable name into filename add command name

Of course, you can do it in the following ways
Sysctl-w kernel.core_pattern=/tmp/core-%e-%p

These actions are lost once the computer restarts, and if you want to persist these operations, you can add them in the/etc/sysctl.conf file:
Kernel.core_pattern=/tmp/core%p

When you're ready, if you want to see the effect without restarting, use the following command:
Sysctl-p/etc/sysctl.conf

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

Expert advice:

The problem solved:
The existing program P runs on the server for a long time, and is often dead once every 1 days (segment error).

Current Workarounds:
Open a terminal with SECURECRT, and set ulimit-c nulimited on the service, then start program p. The query results with the ULIMITE-A command are 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 using kill-6 pid to be able to core file.

The current difficulties:

When you run the Ulimit-c nulimited terminal (and the terminal starts the program P into the background./P &) off, the program P is dead and the core file is not generated.
After the experiment, the Ulimit command was found to be related to terminal.

Expert advice:
How to set up a core file that can be generated, regardless of terminal
That is, the program starts, shuts down the terminal, and can generate a core file when the program dies (a segment error).

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

Comment out the line above.

There are other Unix-like operating systems that also have their own configuration files to set.

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

GdB Core Multithreading
            Multithreading if dump, more than a segment error, generally involves the memory of illegal read and write. This can be done by using the following command to turn on the system switch so that it can generate a core file when it dies.     
            Ulimit-c Unlimited
    &N Bsp       In this case, you can see the Core.pid (PID is the process number) file in the current directory when you die. Then use GDB:
            gdb/bin./core.pid
      &NBS P     After entering, use the frame command when using BT to view the situation of the dead stack.
           
            and there is a thread in it that stops and does not die, which is typically a deadlock or and the message to accept the timeout problem (heard people say, not encountered). In this case, you can use:
            gcore pid (PID number of the debug process)
            Manually generating a core file, using Pstack (which looks bad in Linux) to view the stack. If you can not see it, look at the code carefully to see if it is in the If,return,break,continue this statement operation is to forget to unlock, as well as nested lock problem, need to be analyzed clearly.
                     

Linux System Setup Ulimit and the build of the core file

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.