转自:http://blog.yufeng.info/archives/1485
Linux下的pipe使用非常广泛, shell本身就大量用pipe来粘合生产者和消费者的. 我们的服务器程序通常会用pipe来做线程间的ipc通讯. 由于unix下的任何东西都是文件,只要是文件,在读取的时候,,就会设置last access time, 所以pipe也不例外., 但是这个时间对我们没有意义 如果pipe使用的非常频繁的时候会碰到由于设置访问时间导致的性能问题. 这个开销远比pipe读写的本身开销大. 相比文件读写的开销, atime微不足道,但是对pipe来讲就不同了.
这个事情是上次和多隆同学在把玩他的网络框架的时候,无意发现的.
我们来分析下pipe的这部分代码:
pipe_read(struct kiocb *iocb, const struct iovec *_iov, |
unsigned long nr_segs, loff_t pos) |
我们可以看到在pipe读的时候要设置 file_accessed时间的,接着:
extern void touch_atime(struct vfsmount *mnt, struct dentry *dentry); |
static inline void file_accessed(struct file *file) |
if (!(file->f_flags & O_NOATIME)) |
touch_atime(file->f_path.mnt, file->f_path.dentry); |
如果文件没设置 O_NOATIME就真正动手设置atime,接着:
void touch_atime(struct vfsmount *mnt, struct dentry *dentry) |
struct inode *inode = dentry->d_inode; |
if (inode->i_flags & S_NOATIME) |
if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)) |
if (mnt->mnt_flags & MNT_NOATIME) |
if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)) |
now = current_fs_time(inode->i_sb); |
if (!relatime_need_update(mnt, inode, now)) |
if (timespec_equal(&inode->i_atime, &now)) |
mark_inode_dirty_sync(inode); |
我们可以看出上面的流程还是比较复杂的,开销也很大.
我们来演示下:
#include <linux/unistd.h> |
static void *rp_entry(void *arg) { |
while (1 == read(fds[0], c, 1)) { |
fprintf(stderr, "pipe read ok\n"); |
int main(int argc, char *argv[]) { |
fprintf(stderr, "usage: pipe_test NNNNNN\n"); |
//fcntl(fds[0], F_SETFL, O_NOATIME); |
pthread_create(&rp, NULL, rp_entry, NULL); |
fprintf(stderr, "pipe write %ld...", n); |
for (i = 0; i < n; i++) { |
$ gcc -D_GNU_SOURCE pipe_test.c -lpthread |
$ sudo opcontrol --setup --vmlinux=/usr/lib/debug/lib/modules/2.6.18-164.el5/vmlinux |
$ sudo opcontrol --init && sudo opcontrol --reset && sudo opcontrol --start |
$ sudo opcontrol --shutdown |
samples % app name symbol name |
378654 92.7742 vmlinux .text.acpi_processor_idle |
12978 3.1797 vmlinux current_fs_time |
2530 0.6199 vmlinux thread_return |
2345 0.5745 vmlinux touch_atime |
2253 0.5520 vmlinux .text.acpi_safe_halt |
1597 0.3913 vmlinux timespec_trunc |
1368 0.3352 vmlinux file_update_time |
1253 0.3070 vmlinux __mark_inode_dirty |
901 0.2208 vmlinux pipe_writev |
768 0.1882 vmlinux __mutex_lock_slowpath |
763 0.1869 vmlinux try_to_wake_up |
270 0.0662 vmlinux copy_user_generic_unrolled |
254 0.0622 vmlinux acpi_set_register |
254 0.0622 vmlinux system_call |
233 0.0571 vmlinux pipe_readv |
188 0.0461 vmlinux dnotify_parent |
167 0.0409 vmlinux mutex_unlock |
我们可以看到touch_atime的开销很大,远比pipe的读写大.
这次把这行注释去掉: fcntl(fds[0], F_SETFL, O_NOATIME); 指示pipe在读的时候不更新atime,看下效果:
samples % app name symbol name |
599018 95.2466 vmlinux .text.acpi_processor_idle |
4140 0.6583 vmlinux .text.acpi_safe_halt |
3281 0.5217 vmlinux thread_return |
2812 0.4471 vmlinux current_fs_time |
2615 0.4158 vmlinux file_update_time |
1790 0.2846 vmlinux __mutex_lock_slowpath |
1657 0.2635 vmlinux timespec_trunc |
1341 0.2132 vmlinux try_to_wake_up |
1281 0.2037 vmlinux mutex_unlock |
1080 0.1717 vmlinux mutex_lock |
1001 0.1592 vmlinux pipe_readv |
925 0.1471 vmlinux pipe_writev |
这下看不到touch_atime了,开销省了,对于高性能服务器是很重要的.
小结: 细节很重要,记得开文件open的时候设置O_NOATIME或者用fcntl搞定它.
祝玩得开心!
- ecmall在linux下的安装注意事项(转)
今天跟ecshop客服胡娇沟通后知道ecmall基本配置是[LAMP] linux+apache+mysql+php,然后自己开始在linux下安装ecmall并做迁移,整理了一下中间碰到的问题.1. ...
- ecmall在linux下的安装注意事项(转) ----ecmall系统迁移
linux+apache+mysql+php,然后自己开始在linux下安装ecmall并做迁移,整理了一下中间碰到的问题.1.系统选择的环境是centos6.3,安装不做介绍. 2.安装 MySQL ...
- anaconda在linux下的安装注意事项
不应该做什么 官网原文: Installation Instructions Linux Install These instructions explain how to install Anaco ...
- linux 下SPI通信注意事项(待续)
一.2台Linux设备之间使用SPI通信 1.标准Linux只支持Master 模式.但是可以在驱动中修改为Slave模式: 2.硬件SPI可能支持Slave模式,也可能不支持.这个要提前确认好: 3 ...
- 在linux下用tomcat部署java web项目的过程与注意事项
在linux下用tomcat部署java web项目的过程与注意事项 一.安装JDK 到http://www.oracle.com/technetwork/java/javase/downloads/ ...
- linux 下 tomcat 运行报错 Broken pipe
linux 下 tomcat 运行报错 Broken pipe 感谢:http://hi.baidu.com/liupenglover/blog/item/4048c23ff19f1cd67d1e71 ...
- linux下高并发网络应用注意事项
本文转自:http://www.blogjava.net/bacoo/archive/2012/06/11/380500.html linux下高并发网络应用注意事项 vi /etc/sysctl.c ...
- 在linux下用tomcat部署java web项目的过程与注意事项(转)
在linux下用tomcat部署java web项目的过程与注意事项一.安装JDK到http://www.oracle.com/technetwork/java/javase/downloads/in ...
- linux下php中文UTF-8转换Unicode方法和注意事项
先说下遇到问题:1.php没有内置unicode_ecode函数可以直接使用 2.网上很多资料都是用$str = iconv($encoding, 'UCS-2', $str); window下转换出 ...
随机推荐
- 跟着百度学PHP[4]OOP面对对象编程-6-构造方法(__construct)和构析方法(__destruct)
函数就是成员方法(方法有三:构造方法.成员方法.析构方法) 下面是两种方法. 构造方法和构析方法 00x1 构造方法 构造方法会在创建对象之后自动调用.其名称为__construct <?php ...
- MVPHelper
在MVP的使用过程中,我们需要反复的去写各种MVP的接口和实现类, 实在是 太麻烦了!!所以抽时间撸了一款插件(只可用于Intellj IDEA 和 Android Studio),用于生成MVP接口 ...
- BZOJ 1461: 字符串的匹配
Description 同上题. Sol KMP+树状数组. 写这题的时候我灰常naive...不管了...直接贴代码... Code /******************************* ...
- phpcms 无法显示缩略图 Call to undefined function image_type_to_extension
问题背景: 线下的phpcms项目没问题,线上的phpcms新添加的图片缩略图显示有问题,查看了一下php版本,线下是5.5的,线上的是5.1的 问题原因: 看了一下线上的错误日志,显示: PHP F ...
- UITableView 系列之自定义 UITableViewCell
http://www.360doc.com/content/14/0225/14/11029609_355567657.shtml
- SQL关于分页的sql查询语句 limit 和row_number() OVER函数
在做项目的时候需要些分页,用的数据库是mysql,之前看到的参考例子是用MS SQL做的,在MS SQL.ORACLE里面有ROW_NUMBER() OVER函数可以在数据库里对数据进行分组.百度后的 ...
- IIS 处理请求 原理
有时候我们会发现当我们访问一个IIS网站时,使用网址可以正常访问,但是使用IP却不行,这是什么原因呢? 原来IIS可以使用一个IP地址和端口绑定多个网站,这些网站的IP地址与端口都一样,因此在客户端或 ...
- Intel项目所用jquery小知识点总结
1.$("#tdGeo input[type='checkbox']:checked") ---筛选出所有已经Check的Checkbox 2.$("#tdCount ...
- oracle异常记录
2015年9月14 1.在csdn论坛中看帖子,遇到的一个问题:ORA-01422 实际返回的行数超出请求的行数.
- word to word
Question: For each word, you can get a list of neighbor words by calling getWords(String), find all ...