undefined

Linux 共享内存使用常见的陷阱与分析

所谓共享内存就是使得多个进程可以访问同一块内存空间,是最快的可用IPC形式。是针对其他通信机制运行效率较低而设计的。往往与其它通信机制,如 信号量结合使用,来达到进程间的同步及互斥。其他进程能把同一段共享内存段“连接到”他们自己的地址空间里去。所有进程都能访问共享内存中的地址。如果一 个进程向这段共享内存写了数据,所做的改动会即时被有访问同一段共享内存的其他进程看到。共享内存的使用大大降低了在大规模数据处理过程中内存的消耗,但 是共享内存的使用中有很多的陷阱,一不注意就很容易导致程序崩溃。

超过共享内存的大小限制?

在一个linux服务器上,共享内存的总体大小是有限制的,这个大小通过SHMMAX参数来定义(以字节为单位),您可以通过执行以下命令来确定 SHMMAX 的值:

  1. cat /proc/sys/kernel/shmmax

查看更多

undefined

Linux中如何查看大文件大目录

搜索当前目录喜下超过800M的文件:find . -type f -size +800M

打印大文件的一些信息:find . -type f -size +800M -print0 | xargs -0 ls -lh

还可以对大文件进行排序:find . -type f -size +800M -print0 | xargs -0 du -h | sort -nr


搜索大目录:du -h –max-depth=1

查看更多

undefined

https://cizixs.com/2018/01/13/linux-udp-packet-drop-debug/
//todo
// ethtool 命令的熟悉

Linux系统UDP丢包分析

UDP是不可靠的传输协议,没有TCP的流量控制、丢包重传等机制。可能会出现丢包,出现丢包时我们该如何排查确认是什么原因导致的丢包呢?

一、Linux系统接收网络报文过程

  1. 首先网络报文通过物理网线发送到网卡
  2. 网络驱动程序会把网络中的报文读出来放到 ring buffer 中,这个过程使用 DMA(Direct Memory Access),不需要CPU参与
查看更多

undefined

压力测试工具之 ab

ab 命令会创建多个并发访问线程,模拟多个访问者同时对某一 URL 地址进行访问

一、参数说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
-n requests Number of requests to perform
-c concurrency Number of multiple requests to make at a time
-t timelimit Seconds to max. to spend on benchmarking
This implies -n 50000
-s timeout Seconds to max. wait for each response
Default is 30 seconds
-b windowsize Size of TCP send/receive buffer, in bytes
-B address Address to bind to when making outgoing connections
-p postfile File containing data to POST. Remember also to set -T
-u putfile File containing data to PUT. Remember also to set -T
-T content-type Content-type header to use for POST/PUT data, eg.
'application/x-www-form-urlencoded'
Default is 'text/plain'
-v verbosity How much troubleshooting info to print
-w Print out results in HTML tables
-i Use HEAD instead of GET
-x attributes String to insert as table attributes
-y attributes String to insert as tr attributes
-z attributes String to insert as td or th attributes
-C attribute Add cookie, eg. 'Apache=1234'. (repeatable)
-H attribute Add Arbitrary header line, eg. 'Accept-Encoding: gzip'
Inserted after all normal header lines. (repeatable)
-A attribute Add Basic WWW Authentication, the attributes
are a colon separated username and password.
-P attribute Add Basic Proxy Authentication, the attributes
are a colon separated username and password.
-X proxy:port Proxyserver and port number to use
-V Print version number and exit
-k Use HTTP KeepAlive feature
-d Do not show percentiles served table.
-S Do not show confidence estimators and warnings.
-q Do not show progress when doing more than 150 requests
-g filename Output collected data to gnuplot format file.
-e filename Output CSV file with percentages served
-r Don't exit on socket receive errors.
-h Display usage information (this message)
-Z ciphersuite Specify SSL/TLS cipher suite (See openssl ciphers)
-f protocol Specify SSL/TLS protocol
(SSL3, TLS1, TLS1.1, TLS1.2 or ALL)

参数说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
-n:在测试会话中所执行的请求个数。默认时,仅执行一个请求。

-c:一次产生的请求个数。默认是一次一个。

-t:测试所进行的最大秒数。其内部隐含值是-n 50000,它可以使对服务器的测试限制在一个固定的总时间以内。默认时,没有时间限制。

-p:包含了需要POST的数据的文件。

-P:对一个中转代理提供BASIC认证信任。用户名和密码由一个:隔开,并以base64编码形式发送。无论服务器是否需要(即是否发送了401认证需求代码),此字符串都会被发送。

-T:POST数据所使用的Content-type头信息。

-v:设置显示信息的详细程度-4或更大值会显示头信息,3或更大值可以显示响应代码(404,200等),2或更大值可以显示警告和其他信息。

-V:显示版本号并退出。

-w:以HTML表的格式输出结果。默认时,它是白色背景的两列宽度的一张表。

-i:执行HEAD请求,而不是GET。

-x:设置<table>属性的字符串。

-X:对请求使用代理服务器。

-y:设置<tr>属性的字符串。

-z:设置<td>属性的字符串。

-C:对请求附加一个Cookie:行。其典型形式是name=value的一个参数对,此参数可以重复。

-H:对请求附加额外的头信息。此参数的典型形式是一个有效的头信息行,其中包含了以冒号分隔的字段和值的对(如,"Accept-Encoding:zip/zop;8bit")。

-A:对服务器提供BASIC认证信任。用户名和密码由一个:隔开,并以base64编码形式发送。无论服务器是否需要(即,是否发送了401认证需求代码),此字符串都会被发送。

-h:显示使用方法。

-d:不显示"percentage served within XX [ms] table"的消息(为以前的版本提供支持)。

-e:产生一个以逗号分隔的(CSV)文件,其中包含了处理每个相应百分比的请求所需要(从1%到100%)的相应百分比的(以微妙为单位)时间。由于这种格式已经“二进制化”,所以比'gnuplot'格式更有用。

-g:把所有测试结果写入一个'gnuplot'或者TSV(以Tab分隔的)文件。此文件可以方便地导入到Gnuplot,IDL,Mathematica,Igor甚至Excel中。其中的第一行为标题。

-i:执行HEAD请求,而不是GET。

-k:启用HTTP KeepAlive功能,即在一个HTTP会话中执行多个请求。默认时,不启用KeepAlive功能。

-q:如果处理的请求数大于150,ab每处理大约10%或者100个请求时,会在stderr输出一个进度计数。此-q标记可以抑制这些信息。

查看更多

undefined

Crontab

一、使用

1
2
3
4
crontab [-u username] // 省略用户表示操作当前用户的 crontab
-e:编辑工作表
-l:列出工作表里的命令
-r:删除工作表

crontab 的命令构成 时间+动作,其时间有 分、时、日、月、周 五种,操作符有

1
2
3
4
*  取值范围内的所有数字
/ 每过多少个数字
- 从X到Z
, 散列函数

查看更多

undefined

core 文件生成配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1. 设置 core 文件大小,unlimited 表示 core 文件的大小无限制
ulimit -c unlimited (只对当前 shell 生效)
放入 /etc/profile 中,对全局生效
放入 ~/.bashrc 或 ~/.bash_profile 文件,对某一用户生效

2. 设置 core 文件的目录和命名规则
默认 corefile 是生成在程序的执行目录下或者程序启动调用了 chdir 之后的目录,我们可以通过设置生成corefile的格式来控制它,让其生成在固定的目录下。
/proc/sys/kernel/core_uses_pid 可以控制产生的 core 文件的文件名中是否添加 pid 作为扩展,如果添加则文件内容为 1,否则为 0
/proc/sys/kernel/core_pattern 可以设置格式化的 core 文件保存位置或文件名,比如原来文件内容是 core-%e

控制所产生的 core 文件存放到 corefile 目录下,产生的文件名为 core-命令名-pid-时间戳
echo "/home/saneri/corefile/core-%e-%p-%t" > /proc/sys/kernel/core_pattern
或者 sysctl -w kernel.core_pattern=/corefile/core-%e-%p-%t

重启也能生效的配置,则需要写入配置文件中
echo "kernel.core_pattern=/home/saneri/corefile/core-%e-%p-%t" >> /etc/sysctl.conf
sysctl -p /etc/sysctl.conf
kernel.core_pattern = /home/saneri/corefile/core-%e-%p-%t

关于 core 文件格式控制的参数

1
2
3
4
5
6
7
8
%%:相当于%
%p:相当于<pid>
%u:相当于<uid>
%g:相当于<gid>
%s:相当于导致dump的信号的数字
%t:相当于dump的时间
%e:相当于执行文件的名称
%h:相当于hostname

查看更多