测试代码出自:块设备IO优化的典型案例分析_papaofdoudou的博客-CSDN博客_kworker/u2:2-2-
#include
#include
#include
#include
#include
#include
#include
#include //O_DIRECT
#define __USE_GNU 1
#include
#include
#include
#include int fallocate(int , int , off_t, off_t);
static void write_test(int fdno)
{int origin_len;int reallen;void *mp;int len = 1 * 1024 * 1024;origin_len = len;len = len + (getpagesize() - len%getpagesize());//printf("%s line %d, len = %d.\n", __func__, __LINE__, len);unsigned char *p = malloc(len + getpagesize());if(p == NULL){printf("%s line %d, malloc failure.\n", __func__, __LINE__);return;}mp = p;unsigned char *q = (unsigned char *)(((unsigned long)p + getpagesize())&(~(getpagesize()-1)));p = q;//printf("q = %p, mp = %p.\n", q, mp);memset(p, 0x00, len);//lseek(fdno, 0, SEEK_SET);//int reallen = read(fdno, p,len);//printf("%s line %d, reallen %d read failure.er %s\n", __func__, __LINE__, reallen, strerror(errno));//printf("%s line %d, fdno = %d\n", __func__, __LINE__, fdno);//lseek(fdno, 0, SEEK_SET);reallen = write(fdno, p, origin_len);//printf("%s line %d, reallen = %d.\n", __func__, __LINE__, reallen);//lseek(fdno, 0, SEEK_SET);free(mp);mp = NULL;return;
}static void* write_thread1(void *p)
{int fdno;fdno = open("./fuck.bin", O_DIRECT|O_RDWR|O_CREAT, 0666);//fdno = open("./fuck.bin", O_RDWR|O_CREAT, 0666);if(fdno < 0){printf("%s line %d, open file failure.\n", __func__, __LINE__);return NULL;}fallocate(fdno, 1, 0, 100*1024*1024);//fallocate(fdno, 0, 0, 100*1024*1024);//return NULL;while(1){write_test(fdno);}close(fdno);return NULL;
}static void* write_thread2(void *p)
{int fdno;fdno = open("./new.bin", O_DIRECT|O_RDWR|O_CREAT, 0666);if(fdno < 0){printf("%s line %d, open file failure.\n", __func__, __LINE__);return NULL;}fallocate(fdno, 1, 0, 100*1024*1024);//fallocate(fdno, 0, 0, 100*1024*1024);//return NULL;while(1){write_test(fdno);}close(fdno);return NULL;
}int main(int argc,char **argv)
{pthread_t t1,t2;pthread_create(&t1,0,write_thread1,NULL);pthread_create(&t2,0,write_thread2,NULL);pthread_join(t1,NULL);pthread_join(t2,NULL);return 0;
}
块起始分别为9961472、9961984、9962496、9963008,每个需要512 sectiors 扇区,本次写1M的文件,需要4*512=1024个扇区,即每个扇区是1K
[281277.215000] test_emmc(14438): dirtied inode 1572868 (fuck.bin) on mmcblk0
[281277.215000] test_emmc(14438): WRITE block 9961472 on mmcblk0 (512 sectors)
[281277.215000] test_emmc(14438): WRITE block 9961984 on mmcblk0 (512 sectors)
[281277.219000] test_emmc(14438): WRITE block 9962496 on mmcblk0 (512 sectors)
[281277.220000] test_emmc(14438): WRITE block 9963008 on mmcblk0 (512 sectors)
[281283.008000] jbd2/mmcblk0-8(833): WRITE block 29730136 on mmcblk0 (8 sectors)
[281283.008000] jbd2/mmcblk0-8(833): WRITE block 29730144 on mmcblk0 (8 sectors)
[281283.013000] jbd2/mmcblk0-8(833): WRITE block 29730152 on mmcblk0 (8 sectors)
[281284.018000] kworker/u2:0(14315): WRITE block 50331904 on mmcblk0 (8 sectors)
kworker线程创建
路径:kernel\workqueue.c
JBD2(journaling block device 2)
关于JBD介绍:https://help.aliyun.com/document_detail/155605.html
JDB的工作原理
JBD2(journaling block device 2)系统分析(一)-dessasic-ChinaUnix博客
性能分析之解决 jbd2 引起 IO 高问题_zuozewei的博客-CSDN博客
JDB2线程创建
[root@szclou mmcblk0-8]$ps -aux | grep jbd*
root 833 0.0 0.0 0 0 ? S Jan04 0:06 [jbd2/mmcblk0-8]
root 14546 0.0 0.3 1860 876 pts/2 S+ 14:49 0:00 grep jbd*
JBD2源代码路径:fs\jbd2\journal.c
某大神发现jbd2日志更新时会引发io访问100%,是内核代码有bug,链接:性能分析之解决 jbd2 引起 IO 高问题_zuozewei的博客-CSDN博客
而本人的内核却是还存在这个bug
int jbd2_log_start_commit(journal_t *journal, tid_t tid)
{int ret;write_lock(&journal->j_state_lock);ret = __jbd2_log_start_commit(journal, tid);write_unlock(&journal->j_state_lock);return ret;
}