sync

来源:互联网 发布:shader编程教程 编辑:程序博客网 时间:2024/05/22 01:45

1.      linuxsync命令

busybox-1.14.3sync命令相关代码非常简单,

int sync_main(int argc, char **argv UNUSED_PARAM)

{

        /* coreutils-6.9 compat */

        bb_warn_ignoring_args(argc - 1);

 

        sync();

 

        return EXIT_SUCCESS;

}

2.      sync系统调用

fs/sync.c

/*

 * sync everything.  Start out by waking pdflush, because that writes back

 * all queues in parallel.

 */

SYSCALL_DEFINE0(sync)

{

       wakeup_flusher_threads(0);

       sync_filesystems(0);

       sync_filesystems(1);

       if (unlikely(laptop_mode))

              laptop_sync_completion();

       return 0;

}

值得注意的是sync函数只是将所有修改过的块缓冲区排入写队列,然后它就返回,它并不等待实际写磁盘操作结束,幸运的是,通常成为update的系统守护进程会周期(30s)调用sync函数,这就保证了定期冲洗内核的块缓冲区,所以我们在linux上更新一个文件后,不要着急重启服务器,最好等待实际的磁盘写操作完成,避免数据丢失。

3.      mips芯片的sync指令

防止不需要的乱序执行。

SYNC affects only uncached and cached coherent loads and stores. The loads and stores that occur before the SYNC must be completed before the loads and stores after the SYNC are allowed to start.

Loads are completed when the destination register is written. Stores are completed when the stored value is visible to every other processor in the system.

SYNC is required, potentially in conjunction with SSNOP, to guarantee that memory reference results are visible

across operating mode changes. For example, a SYNC is required on some implementations on entry to and exit

from Debug Mode to guarantee that memory affects are handled correctly.

Detailed Description:

When the stype field has a value of zero, every synchronizable load and store that occurs in the instruction stream

before the SYNC instruction must be globally performed before any synchronizable load or store that occurs after the

SYNC can be performed, with respect to any other processor or coherent I/O module.

SYNC does not guarantee the order in which instruction fetches are performed. The stype values 1-31 are reserved

for future extensions to the architecture. A value of zero will always be defined such that it performs all defined

synchronization operations. Non-zero values may be defined to remove some synchronization operations. As such,

software should never use a non-zero value of the stype field, as this may inadvertently cause future failures if

non-zero values remove synchronization operations

 

基于mips架构的linuxbarrier就是使用sync指令:

在文件 arch/mips/include/asm/barrier.h

#ifdef CONFIG_CPU_HAS_WB

 

#include <asm/wbflush.h>

 

#define wmb()              fast_wmb()

#define rmb()         fast_rmb()

#define mb()          wbflush()

#define iob()          wbflush()

 

#else /* !CONFIG_CPU_HAS_WB */

 

#define wmb()              fast_wmb()

#define rmb()         fast_rmb()

#define mb()          fast_mb()

#define iob()          fast_iob()

 

#endif /* !CONFIG_CPU_HAS_WB */

我们所处的平台没有CONFIG_CPU_HAS_WB,所以是红色的定义。

 

 

其中的fast_wmb/fast_rmb/fast_mb等定义可参考同一个文件的代码:

#ifdef CONFIG_CPU_HAS_SYNC

#define __sync()                         \

       __asm__ __volatile__(                 \

              ".set push\n\t"        \

              ".set noreorder\n\t"        \

              ".set mips2\n\t"              \

              "sync\n\t"                     \

              ".set pop"               \

              : /* no output */            \

              : /* no input */              \

              : "memory")

#else

#define __sync()    do { } while(0)

#endif

 

#define __fast_iob()                            \

       __asm__ __volatile__(                 \

              ".set push\n\t"        \

              ".set noreorder\n\t"        \

              "lw  $0,%0\n\t"             \

              "nop\n\t"                \

              ".set pop"               \

              : /* no output */            \

              : "m" (*(int *)CKSEG1)        \

              : "memory")

#ifdef CONFIG_CPU_CAVIUM_OCTEON

# define OCTEON_SYNCW_STR ".set push\n.set arch=octeon\nsyncw\nsyncw\n.set pop\n"

# define __syncw()       __asm__ __volatile__(OCTEON_SYNCW_STR : : : "memory")

 

# define fast_wmb()      __syncw()

# define fast_rmb() barrier()

# define fast_mb()  __sync()

# define fast_iob()  do { } while (0)

#else /* ! CONFIG_CPU_CAVIUM_OCTEON */

# define fast_wmb()      __sync()

# define fast_rmb() __sync()

# define fast_mb()  __sync()

# ifdef CONFIG_SGI_IP28

#  define fast_iob()                            \

       __asm__ __volatile__(                 \

              ".set push\n\t"        \

              ".set noreorder\n\t"        \

              "lw  $0,%0\n\t"             \

              "sync\n\t"                     \

              "lw  $0,%0\n\t"             \

              ".set pop"               \

              : /* no output */            \

              : "m" (*(int *)CKSEG1ADDR(0x1fa00004)) \

              : "memory")

# else

#  define fast_iob()                            \

       do {                             \

              __sync();                     \

              __fast_iob();                 \

       } while (0)

# endif

#endif /* CONFIG_CPU_CAVIUM_OCTEON */

 

可看到没有CONFIG_CPU_CAVIUM_OCTEON时,

# define fast_wmb()      __sync()

# define fast_rmb() __sync()

# define fast_mb()  __sync()

都调用了__sync宏。

 

其中CONFIG_CPU_HAS_SYNC可参考arch/mips/Kconfig

config CPU_HAS_SYNC

        bool

        depends on !CPU_R3000

        default y

原创粉丝点击