分类: 计算机 Computer

  • 用 mmap 将某些 struct 存放到文件中 (map virtual memory to a file)

    背景

    在 RHEL7 中, dovecot 在运行的过程中其中一个 imap 进程挂掉,出来这么一个 coredump.

    [cc]
    Program terminated with signal 7, Bus error.

    (gdb) f 0
    #0 0x00007fbb15868c17 in mail_cache_transaction_open_if_needed (ctx=ctx@entry=0x7fbb173c1430)
    at mail-cache-transaction.c:218
    218 if (ext->reset_id == cache->hdr->file_seq || i == 2)

    (gdb) p cache
    $1 = (struct mail_cache *) 0x7fbb173af2d0

    (gdb) p cache->hdr
    $3 = (const struct mail_cache_header *) 0x7fbb15ce5000

    (gdb) p cache->hdr->file_seq
    Cannot access memory at address 0x7fbb15ce5008

    # cat maps | grep 7fbb15ce5000
    7fbb15ce5000-7fbb15ced000 r–s 00000000 00:2c xxxxxxxxx /xxxxxxx/dovecot.index.cache
    [/cc]

    挂掉的原因是 “signal 7, bus error” ((bad memory access)). 从 gdb 中可以看到,进程挂的时候在尝试访问 ext->reset_id 和 cache->hdr->file_seq, 而从 maps 可以看出 cache->hdr 指向的是 file-backend 的地址空间。

    那么问题来了,怎样可以把一个结构体,映射到一个文件,而不是匿名页?另外一个问题是,在 gdb 中看到 “Cannot access memory at address” 是意味着这段地址存在问题吗?

    测试环境

    Debian 8 – jessie
    (更多…)

  • Linux Kernel 的双向链表实现

    在一个太阳毒辣的周五,刚到公司的我还没吃完手里的面包,肯尼斯把我抓到他的屏幕前:“你来说说这段代码什么意思。”

    [cc lang=”C”]
    #define container_of(ptr, type, member) ({ \
    const typeof( ((type *)0)->member ) *__mptr = (ptr); \
    (type *)( (char *)__mptr – offsetof(type,member) );})
    [/cc]

    我凝视屏幕一分钟,问道:“这…有注释吗?”
    见状,肯尼斯渐渐露出笑容。我仿佛从肯尼斯的笑脸中,看到了下一分钟认怂的我。

    一分钟后,肯尼斯从 Kernel 的双向链表给我讲起…

    测试环境

    下面的 Kernel 源码以及测试环境均为 RHEL7.3:
    kernel-3.10.0-514.el7.x86_64
    gcc-4.8.5-11.el7.x86_64
    (更多…)

  • 简单的 xinetd 示例程序

    通过 xinetd 的管理,可以按需启动某些“用完即走”的网络服务。实际上,xinetd 相当于网络层面的 wrapper, 应用程序只需要处理标准输入输出,网络层面的事情由 xinetd 代为处理即可。

    以下是在 RHEL7 中实现的一个简单示例程序。
    (更多…)

  • Python attributes 笔记

    上个月在 Python Meetup 上有人分享了 Python attributes 的一些玩法,记录一下。
    (更多…)

  • 为什么不同的 LC_ALL 设定会导致 sort 命令输出顺序不一样?

    在使用 sort 命令对文本进行排序时,如果语言环境不同,得到的排序结果也会不同。

    [root@rhel674 tmp]# export LC_ALL=C; sort test.txt 
    1234
    AAA
    BBB
    aaa
    aab
    
    [root@rhel674 tmp]# export LC_ALL=en_US; sort test.txt 
    1234
    aaa
    AAA
    aab
    BBB
    

    如果用 C (POSIX) 作为语言环境,得到的排序结果是按照字符对应的 ascii 码大小来排序的。而如果用 en_US 等语言环境,得到的排序结果会不同(从上可以看到,先按字母顺序排序,字母都一样的时候才区分大小写)。
    (更多…)

  • 锁定 C-state 能提高 CPU 性能吗?

    tl;dr

    不能。

    背景

    用户发现,尽管设置了 ‘performance’ 作为 cpuspeed 的 governor (/etc/sysconfig/cpuspeed), 但是 CPU 并不是一直处于最高频率运行。用户希望保持最高频率运行,不考虑节能,要发挥出 CPU 的最高性能。
    (更多…)

  • 比较 kdump makedumpfile 中的压缩方法

    背景

    在出现 Kernel Panic 的时候,kdump 可以帮助我们收集 vmcore, 以便后续对故障原因进行分析。然而,近些年的服务器动辄上百G的内存,转储一个 vmcore 所耗费的时间显得相对较长,增加了 down time.

    在 RHEL7/CentOS7 中, kdump 提供了三种压缩方法(zlib, lzo, snappy),我们可以选择一种较快速的压缩方法来节省收集 vmcore 的时间。

    压缩方法可以在 kdump.conf 中的 makedumpfile 一行里设置。
    [cc lang=”text”]
    # man makedumpfile

    -c,-l,-p
    Compress dump data by each page using zlib for -c option, lzo for -l option or snappy for -p option. (-l option needs USELZO=on and -p option needs USESNAPPY=on when building)
    A user cannot specify this option with -E option, because the ELF format does not support compressed data.
    Example:
    # makedumpfile -c -d 31 -x vmlinux /proc/vmcore dumpfile
    [/cc]

    zlib 是 gzip 所使用的压缩方法,通常而言它比 lzo 和 snappy 慢,而压缩比稍微高于 lzo 和 snappy.
    (更多…)

  • How to request continuous physical memory in Linux?

    Background

    A customer noticed an issue. His system has 100+GB free memory (which is pure free memory, not buffer/cache), but system starts reclaiming pages from buffer/cache and swapping out pages at some point.

    From system log, we can see things like below at the reclaiming moment.
    [cc lang=”text”]
    May 2 10:03:56 rhel68-kmalloc kernel: insmod: page allocation failure. order:10, mode:0xd0
    May 2 10:03:56 rhel68-kmalloc kernel: Pid: 22319, comm: insmod Not tainted 2.6.32-642.el6.x86_64 #1
    May 2 10:03:56 rhel68-kmalloc kernel: Call Trace:
    May 2 10:03:56 rhel68-kmalloc kernel: [] ? __alloc_pages_nodemask+0x7dc/0x950
    May 2 10:03:56 rhel68-kmalloc kernel: [] ? kmem_getpages+0x62/0x170
    May 2 10:03:56 rhel68-kmalloc kernel: [] ? fallback_alloc+0x1ba/0x270
    May 2 10:03:56 rhel68-kmalloc kernel: [] ? cache_grow+0x2cf/0x320
    May 2 10:03:56 rhel68-kmalloc kernel: [] ? ____cache_alloc_node+0x99/0x160
    [/cc]
    (更多…)

  • Why do services like sendmail/httpd still query outdated DNS servers after resolv.conf is changed?

    Question

    One of my colleague raised a question: After changing resolv.conf, sendmail still query the old resolver, why?

    Steps:

    1. Set 192.168.122.65 as nameserver in resolv.conf, start sendmail service, send a mail to root@hat.com, and take a tcpdump. We can see there’s a query asking 192.168.122.65 for MX record of hat.com .

    2. Change nameserver from 192.168.122.65 to 192.168.122.72 in resolv.conf, send a mail to root@hat.com and take a tcpdump again. This time we expect a query to 192.168.122.72 (new), but the actual query is to 192.168.122.65 (old).

    3. Restart sendmail service, send a mail to root@hat.com, this time we can see a query to 192.168.122.72 (new) as expected.

    When we were thinking about what’s going wrong with sendmail, another colleague joined in discussion, and she mentioned that httpd and some other long-running services also have this behavior.

    Since sendmail is not the only case, I think we should have a check on glibc.

    (更多…)