引用http://www.linuxatemyram.com/,其中描述了磁盘i / o如何在内存中缓冲。我想说明这一点,并实际撤消它,以衡量将不同数量的数据写入文件所需的时间。 我要做的是了解基于两种情况的i / o的运行时间:
mount -t tmpfs -o size=500g tmpfs /ramdisk
/scratch/
,其中900GB seagate sas硬盘格式为EXT3或XFS 我的程序在将文件写入硬盘时非常快,我确定这是因为linux操作系统正在缓冲RAM中的数据使磁盘i / o透明...因为我的程序完成后如果在提示我做rm temp_speed*
这需要一分钟或更长时间才能实现。
例如,如果程序正在写入10gb,则完成写入ramdisk需要7秒,完成写入/ scratch需要20秒。但是在写入ramdisk后我可以做rm temp*
并在1-3秒内完成,而如果它是在临时删除命令需要90秒才能完成。
任何人都知道如何在程序中编写下面的程序来了解磁盘i / o何时完全完成?感谢。
预先警告,如果您尝试运行此代码,则可能会很快填满您的硬盘和/或内存并导致系统崩溃。
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
# define MAX_F 256
/* WARNING: running this code you risk quickly filling up */
/* your hard drive and/or memory and crashing your system. */
/* compile with -O0 no optimization */
long int get_time ( void )
{
long int t;
t = (long int) time( NULL );
return( t );
}
void diff_time ( long int *start, long int *finish )
{
time_t s, e;
long int diff;
long int h = 0, m = 0;
s = (time_t) *start;
e = (time_t) *finish;
diff = (long int) difftime( e, s );
h = diff / 3600;
diff -= ( h * 3600 );
m = diff / 60;
diff -= ( m * 60 );
printf(" problem runtime (h:m:s) = %02ld:%02ld:%02ld\n", h, m, diff );
printf("\n");
}
int main ( int argc, char *argv[] )
{
FILE *fp[MAX_F];
char fname[MAX_F][64];
long int a, i, amount, num_times, num_files;
long int maxfilesize;
long int start_time, end_time;
double ff[512]; /* 512 doubles = 4096 = 4k worth of data */
if ( argc != 3 )
{
printf("\n usage: readwrite_speed <total amount in gb> <max file size in gb>\n\n");
exit( 0 );
}
system( "date" );
amount = atol( argv[1] );
maxfilesize = atol( argv[2] );
if ( maxfilesize > amount )
maxfilesize = amount;
num_files = amount / maxfilesize;
if ( num_files > MAX_F )
{
printf("\n increase max # files abouve %d\n\n", MAX_F );
exit( 0 );
}
num_times = ( amount * 1024 * 1024 * 1024 ) / 4096;
num_times /= num_files;
printf("\n");
printf(" amount = %ldgb, num_times = %ld, num_files = %ld\n", amount, num_times, num_files );
printf("\n");
for ( i = 0; i < num_files; i++ )
sprintf( fname[i], "temp_speed%03d", i );
start_time = get_time();
for ( i = 0; i < num_files; i++ )
{
fp[i] = fopen( fname[i], "wb" );
if ( fp[i] == NULL )
printf(" can't write binary %s\n", fname[i] );
}
for ( i = 0; i < 512; i++ )
ff[i] = rand() / RAND_MAX;
/* 1 gb = 262,144 times writing 4096 bytes */
for ( a = 0; a < num_times; a++ )
{
for ( i = 0; i < num_files; i++ )
{
fwrite( ff, sizeof( double ), 512, fp[i] );
fflush( fp[i] );
}
}
for ( i = 0; i < num_files; i++ )
fclose( fp[i] );
end_time = get_time();
diff_time( &start_time, &end_time );
system( "date" );
}
答案 0 :(得分:0)
首先,看看here 您无法通过100%保证知道磁盘上的数据 您可以使用fsync告诉内核将数据刷新到磁盘。但是磁盘和磁盘控制器都有自己的缓存,所以即使内核也不能总是知道它什么时候完成。