⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blastout.c

📁 iscsi源代码 UNH的progect 有initiator端和target端的源码
💻 C
字号:
/*	blastout.c	*/
/*  Writes a file and report the I/O results */
/*  By Bob Russell */



/*	vi: set autoindent tabstop=4 shiftwidth=4 :	*/

#define	_POSIX_C_SOURCE	199506L

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/time.h>
#include <fcntl.h>

#define	MAX_BYTES	(1024 * 1024)
//#define	MAX_BYTES	(4096)

char	buffer[MAX_BYTES];


int main( int argc, char *argv[] )
	{
	int				fd, secs, usecs, bytes, n, nbytes, k;
	struct timeval	start_time, stop_time;
	double			dtime, dbytes;
	char			*ptr;


	if( argc != 2  &&  argc != 3 )
		{
		printf("Usage: blastout file-name [Megabytes] \n");
		exit(EXIT_FAILURE);
		}
	if( (fd = open(argv[1],O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0 )
		{
		printf("open %s: %s\n", argv[1], strerror(errno));
		exit(EXIT_FAILURE);
		}
	if( argc == 3 )
		{
		errno = 0;
		nbytes = strtoul(argv[2], &ptr, 0);
		if( errno != 0  ||  ptr == argv[2]  ||  *ptr != '\0' )
			{
			printf("Illegal number: %s\n", argv[2]);
			exit(EXIT_FAILURE);
			}
		nbytes *= MAX_BYTES;
		}
	else
		nbytes = MAX_BYTES;

	/*	fill up the array with arbitrary bytes */
	ptr = buffer;
	for( n = 0;  n < MAX_BYTES;  n++ )
		{
		//*ptr++ = n;
		*ptr++ = 0x7f;
		}

	printf("Writing %10d bytes total\n", nbytes);
	if( gettimeofday(&start_time, NULL) < 0 )
		{
		printf("gettimeofday start: %s\n", strerror(errno));
		exit(EXIT_FAILURE);
		}

	n = bytes = 0;
	while( nbytes > 0 )
		{
		if( nbytes > MAX_BYTES )
			k = MAX_BYTES;
		else
			k = nbytes;
		if( (n = write(fd, buffer, k)) != k )
			{
			if ( n <= 0 )
				exit(1);
			printf("wrote %d bytes, expected %d bytes\n", n, k);
			if( n > 0 )
				{
				bytes += n;
				nbytes -= n;
				}
			break;
			}
		bytes += n;
		nbytes -= n;
		}

	if( fdatasync(fd) < 0 )
		{
		printf("fdatasync: %s\n", strerror(errno));
		}

	if( gettimeofday(&stop_time, NULL) < 0 )
		{
		printf("gettimeofday stop: %s\n", strerror(errno));
		}

	if( start_time.tv_usec > stop_time.tv_usec )
		{
		stop_time.tv_usec += 1000000;
		stop_time.tv_sec -= 1;
		}
	secs = stop_time.tv_sec - start_time.tv_sec;
	usecs = stop_time.tv_usec - start_time.tv_usec;
	printf("%18d bytes written\n", bytes);
	printf("%11d.%06d seconds\n", secs, usecs);
	dtime = secs;
	dtime *= 1000000.0;
	dtime += usecs;
	dbytes = bytes;
	if( dtime != 0 )
		{
		printf("%18.3f Megabytes/second\n", dbytes/dtime);
		printf("%18.3f Megabits/second\n\n", (8.0*dbytes)/dtime);
		}


	return 0;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -