`
standalone
  • 浏览: 611028 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Test performance of allocating a zeroed file via various methods

    博客分类:
  • c++
阅读更多

/*
 * Test performance of allocating a zeroed file via various methods on
 * various file systems
 *
 *   Copyright (C) 2009 Red Hat Inc.
 *   Author(s): Amit Shah
 *
 *    This program is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU General Public License
 *    version 2 as published by the Free Software Foundation
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <asm/unistd.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <sys/mman.h>

#define GB(x) ((unsigned long long)x << 30)

int pre_test_setup(char *source, char *target, char *fstype,
		   unsigned long mntflags, char *mntopts, char *name, int *fd)
{
	int r;

	if (source) {
		r = mount(source, target, fstype, mntflags, mntopts);
		if (r < 0) {
			perror("mount");
			return -1;
		}
	}
	unlink(name);
	*fd = open(name, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
	if (*fd < 0) {
		perror("open");
		return -1;
	}

	/* Flush out all other data that might be pending writing to
	 * the disk. We don't want to count the time we wait for the
	 * buffers of other apps to clear. (Ideally, this test would
	 * be run in single user mode with nothing else running on the
	 * system.)
	 */
	sync();
	sync();
	/* Just to be sure - http://lwn.net/Articles/325420/ */
	sync();

	return 0;
}

int post_test_cleanup(char *target, int *fd)
{
	int r;

	r = close(*fd);
	if (r < 0)
		perror("close");

	if (strcmp(target, ".")) {
		r = umount(target);
		if (r < 0)
			perror("unmount");
	}
	return r;
}

void run_test(int (test)(char *, int *, off_t, size_t),
	      char *source, char *target, char *fstype, unsigned long mntflags,
	      char *mntopts, char *filename, off_t len, size_t data)
{
	int r, fd;

	r = pre_test_setup(source, target, fstype, mntflags, mntopts,
			   filename, &fd);
	if (r < 0)
		goto error_exit;

	(test)(target, &fd, len, data);
	return;

error_exit:
	post_test_cleanup(target, &fd);
	return;
}

int do_posix_fallocate(char *target, int *fd, off_t len, size_t data)
{
	int r;
	struct timeval tv1, tv2;

	printf("posix-fallocate run time:\n");
	gettimeofday(&tv1, NULL);
	r = posix_fallocate(*fd, 0, len);
	post_test_cleanup(target, fd);
	gettimeofday(&tv2, NULL);
	if (r < 0) {
		printf("posix_fallocate, error %d\n", r);
		return r;
	}
	printf("\tseconds:microseconds: %llu:%llu\n", tv1.tv_sec, tv1.tv_usec);
	printf("\tseconds:microseconds: %llu:%llu\n", tv2.tv_sec, tv2.tv_usec);
	printf("\t(approx %us)\n", tv2.tv_sec - tv1.tv_sec);

	return 0;
}

int do_fallocate(char *target, int *fd, off_t len, size_t data)
{
	int r;
	struct timeval tv1, tv2;

	printf("fallocate run time:\n");
	gettimeofday(&tv1, NULL);
	r = syscall(__NR_fallocate, *fd, 0, len);
	post_test_cleanup(target, fd);
	gettimeofday(&tv2, NULL);
	if (r < 0) {
		perror("fallocate");
		return r;
	}
	printf("\tseconds:microseconds: %llu:%llu\n", tv1.tv_sec, tv1.tv_usec);
	printf("\tseconds:microseconds: %llu:%llu\n", tv2.tv_sec, tv2.tv_usec);
	printf("\t(approx %us)\n", tv2.tv_sec - tv1.tv_sec);

	return 0;
}

int do_mmap(char *target, int *fd, off_t len, size_t data)
{
	struct timeval tv1, tv2;
	char *addr;

	/* memset has to have the mmap'ed file backed by something on
	 * disk -- bus error otherwise
	 */
	ftruncate(*fd, len);

	addr = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, *fd, 0);
	if (addr == MAP_FAILED) {
		perror("mmap");
		return -2;
	}

	printf("mmap run time:\n");
	gettimeofday(&tv1, NULL);
	memset(addr, 0, len);
	munmap(addr, len);
	post_test_cleanup(target, fd);
	gettimeofday(&tv2, NULL);

	printf("\tseconds:microseconds: %llu:%llu\n", tv1.tv_sec, tv1.tv_usec);
	printf("\tseconds:microseconds: %llu:%llu\n", tv2.tv_sec, tv2.tv_usec);
	printf("\t(approx %us)\n", tv2.tv_sec - tv1.tv_sec);

	return 0;
}

int do_write_chunks(char *target, int *fd, off_t len, size_t data)
{
	struct timeval tv1, tv2;

	unsigned long long remain = len;
	char *zeros;

	zeros = calloc(1, data);

	printf("%llu-sized chunk run time:\n", data);
	gettimeofday(&tv1, NULL);
	while (remain) {
		int bytes = data;
		if (bytes > remain)
			bytes = remain;
		if ((bytes = write(*fd, zeros, bytes)) < 0) {
			perror("write");
			return -2;
		}
		remain -= bytes;
	}
	post_test_cleanup(target, fd);
	gettimeofday(&tv2, NULL);

	printf("\tseconds:microseconds: %llu:%llu\n", tv1.tv_sec, tv1.tv_usec);
	printf("\tseconds:microseconds: %llu:%llu\n", tv2.tv_sec, tv2.tv_usec);
	printf("\t(approx %us)\n", tv2.tv_sec - tv1.tv_sec);
	free(zeros);

	return 0;
}

int main(int argc, char **argv)
{
	char *source, *target, *fstype, *mntopts;
	char filename[200];
	unsigned long mntflags;
	unsigned long long filesize;

	/* FIXME! use getopt */
	if (argc < 2 || argc > 2 && argc < 5) {
		printf("usage: %s <filesize-in-gb> <device> <mountpoint> <fstype> [mntflags] [mntopts]\n",
		       argv[0]);
		return -1;
	}

	filesize = GB(atol(argv[1]));
	if (argc > 2) {
		source = argv[2];
		target = argv[3];
		fstype = argv[4];
	} else {
		source = NULL;
		target = ".";
		fstype = "native";
	}
	if (argc > 5)
		mntflags = atol(argv[5]);
	else
		mntflags = 0;
	if (argc > 6)
		mntopts = argv[6];
	else
		mntopts = NULL;

	sprintf(filename, "%s/%s-pf", target, fstype);
	run_test(do_posix_fallocate, source, target, fstype, mntflags, mntopts,
		 filename, filesize, 0);

#if 0
	sprintf(filename, "%s/%s-fallocate", target, fstype);
	run_test(do_fallocate, source, target, fstype, mntflags, mntopts,
		 filename, filesize, 0);
#endif

	sprintf(filename, "%s/%s-mmap", target, fstype);
	run_test(do_mmap, source, target, fstype, mntflags, mntopts,
		 filename, filesize, 0);

	sprintf(filename, "%s/%s-chunk4k", target, fstype);
	run_test(do_write_chunks, source, target, fstype, mntflags, mntopts,
		 filename, filesize, 4 * 1024);

	sprintf(filename, "%s/%s-chunk8k", target, fstype);
	run_test(do_write_chunks, source, target, fstype, mntflags, mntopts,
		 filename, filesize, 8 * 1024);

	return 0;
}
 
分享到:
评论

相关推荐

    微软内部资料-SQL性能优化2

    A hard page fault results in a read from disk, either a page file or memory-mapped file. A soft page fault is resolved from one of the modified, standby, free or zero page transition lists. Paging is...

    Advanced Programming in the UNIX Environment

    Typical services include executing a new program, opening a file, reading a file, allocating a region of memory, getting the current time of day, and so on. The focus of this text is to describe the ...

    C# - CSharp 12 in a Nutshell The Definitive Reference

    Unlike languages like C or C++, where developers must manually manage memory, C# takes care of allocating and deallocating memory automatically. This simplifies development and reduces the risk of ...

    cc1plus.exe

    C++的编译器

    RadControls.for.ASP.NET2.Q1.2007.SP2.RadUpload.Net2.v2.3.2.0

    a highly efficient proprietary HttpModule, which enables uploading of files with size up to 2GB, while allocating a minimum amount of server memory. UI control for single- and multi-file uploads, ...

    IOS5 Programming Cookbook

    Based on the provided information from "iOS 5 Programming Cookbook" by Vandad Nahavandipoor, we can derive a comprehensive set of knowledge points related to iOS development using Objective-C....

    c++数据结构与算法实现

    Fig01_02.cpp: A simple recursive routine with a test program Fig01_03.cpp: An example of infinite recursion Fig01_04.cpp: Recursive routine to print numbers, with a test program Fig01_05.cpp: ...

    The Art of Assembly Language Programming

    Allocating Storage for Multidimensional Arrays 5.6.2.4 - Accessing Multidimensional Array Elements in Assembly Language 5.6.3 - Structures 5.6.4 - Arrays of Structures and Arrays/Structures ...

    Transmission Sequence Design and Allocation for Wide Area Ad Hoc Networks

    In this paper we examine the question of designing and allocating transmission sequences to users in a mobile ad hoc network that has no spatially boundary. A basic tenet of the transmission sequence ...

    The art of Molecular Dynamics Simulations ,2nd Edition,Rapaport

    18.3 Allocating arrays 487 18.4 Utility functions 488 18.5 Organizing input data 495 18.6 Configuration snapshot files 498 18.7 Managing extensive computations 500 18.8 Header files 504 19 The future ...

    C Programming

    - **Data Processing**: This involves the transformation of raw data into meaningful information through various steps such as input, processing, output, and storage. #### Programming Languages This ...

    基于遗传算法的作业调度优化研究.docx

    The job shop scheduling problem involves optimizing the sequence, timing, and allocation of tasks on various machines to minimize production time or cost while meeting specific constraints. ...

    DB2pureScale_Practical_Design_Guide_2016-Feb-5.pptx

    3-2 Allocating a portion of the LBP size to LOCKLIST 3-3 Avoid lock escalation with GLM Size 3-4 PAGE_AGE_TRGT_GCR – Improve Performance but also Increases Recovery time 3-5 Setting the correct ...

    Allocating fixed costs or resources and setting targets via data envelopment analysis

    这篇文章的标题为《通过数据包络分析(DEA)分配固定成本或资源并设定目标》,描述中重复了标题的内容。文章的关键词包括“数据包络分析”、“效率”、“固定成本分配”、“资源分配”和“目标设定”。...

    Linux System Administrator Guide Version0.9

    The guide provides a detailed overview of the various components that make up a Linux system, including: - **Operating System Components**: The guide explains the different parts of an operating ...

    BUS Hound

    Note: allocating a very large buffer size under Windows 95/98/Me can take a minute. &lt;br&gt;Max Phase &lt;br&gt;Specifies the maximum number of bytes that will be captured on each phase. Example: if Max ...

    Problem Solving with C++ (7th edition)

    - **Dynamic Allocation of Arrays**: Explanation of allocating arrays dynamically using the `new` keyword and freeing them with `delete`. - **Operator Overloading**: Introduction to operator ...

    APICS DICTIONARY

    It involves identifying and measuring the cost drivers of various activities and allocating indirect costs more accurately. **Relevance in IT**: ABC is highly relevant in IT because it helps in ...

Global site tag (gtag.js) - Google Analytics