Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > ger.ct > #535142

A thousand files ...

From Bonita Montero <Bonita.Montero@gmail.com>
Newsgroups ger.ct, de.comp.os.unix.linux.misc
Subject A thousand files ...
Date 2021-12-24 20:08 +0100
Organization A noiseless patient Spider
Message-ID <sq55rr$kik$1@dont-email.me> (permalink)

Cross-posted to 2 groups.

Show all headers | View raw


Heißt bei mir ein kleines Programm mit dem ich die Performance für
Filesystem-Metadaten von Windows / NTFS ggü. Linux / ext4 getestet
habe.
Und zwar macht das folgendes: Es kreiert in einem gegenenen Ver-
zeichnis dir (erster Kommandozeilen-Parameter) aus t Threads (zwei-
ter Parameter) die jeweils Files kreieren (dritter Parameter) und
die in Abhängigkeit des vierten Parameters mit einer gewissen Menge
an Bytes füllen, oder eben auch gar nicht wenn der Parameter Null
ist.
Ergebnis: Das Linux-System hat einen Ryzen 7 1800X, das Windows 10
System hat einen Threadripper 3990X (SMT wg. des Scheduler-Problems
aus), beide die gleichen SATA3-SSDs. Wenn ich aus 100 Threads paral-
lel je 1.000 files à 4kB in Serie kreieren lasse, dann ist ext4 unter
Linux etwas mehr als 20 mal so schnell wie Windows und Windows ver-
braucht für die selbe Aufgabe fast 200 mal mehr CPU-Zeit.
Ich hab ja auch erwartet, dass das Windows-System langsamer ist bzw.
mehr CPU-Last macht, aber dass das Ergebnis so dramatisch ausfällt,
das hab ich mir wirklich nicht gedacht. Absoluter Murks, was MS da
abliefert.

Hier ist das kleine Progrämmchen:

#if defined(_MSC_VER)
	#include <Windows.h>
#elif defined(__unix__)
	#include <fcntl.h>
	#include <unistd.h>
#endif
#include <iostream>
#include <system_error>
#include <vector>
#include <thread>
#include <cassert>
#include <sstream>
#include <mutex>
#include <exception>
#include <iomanip>
#include <charconv>
#include <stdexcept>
#include <cstring>
#include <type_traits>

using namespace std;

#if defined(_MSC_VER)
using handle_t = HANDLE;
#elif defined(__unix__)
using handle_t = int;
#endif

template<typename ParseType>
	requires is_scalar_v<ParseType>
static ParseType parseInt( char const *str );
static void throwSysErr( char const *errStr );
static void changeDir( char const *path );
static handle_t createFile( char const *fileName );
static void closeFile( handle_t handle );
static void writeFile( handle_t handle, void const *p, unsigned n );
static void whatExit( char const *what );

int main( int argc, char **argv )
{
	try
	{
		vector<char> writeBuff;
		auto theThread = [&]( string prefix, unsigned nFilesPerThread )
		{
			try
			{
				ostringstream ossSuffix;
				for( unsigned iFile = nFilesPerThread; iFile--; )
				{
					ossSuffix.str( "" );
					ossSuffix << prefix << setfill( '0' ) << setw( 16 ) << hex << iFile;
					handle_t h = createFile( ossSuffix.str().c_str() );
					if( writeBuff.size() )
						try
						{
							writeFile( h, &writeBuff[0], (unsigned)writeBuff.size() );
						}
						catch( ... )
						{
							closeFile( h );
							throw;
						}
					closeFile( h );
				}
			}
			catch( exception &exc )
			{
				whatExit( exc.what() );
			}
		};
		if( argc < 1 + 4 )
			return EXIT_FAILURE;
		changeDir( argv[1] );
		unsigned nThreads = parseInt<unsigned>( argv[2] ),
		         nFilesPerThread = parseInt<unsigned>( argv[3] ),
		         fileSize = parseInt<unsigned>( argv[4] );
		vector<jthread> threads;
		threads.reserve( nThreads );
		writeBuff.resize( fileSize );
		ostringstream ossPrefix;
		for( unsigned iThread = nThreads; iThread--; )
		{
			ossPrefix.str( "" );
			ossPrefix << setfill( '0' ) << setw( 16 ) << hex << iThread;
			threads.emplace_back( theThread, ossPrefix.str(), nFilesPerThread );
		}
	}
	catch( exception &exc )
	{
		whatExit( exc.what() );
	}
}

template<typename ParseType>
	requires is_scalar_v<ParseType>
static ParseType parseInt( char const *str )
{
	ParseType p;
	from_chars_result fcr = from_chars( str, str + strlen( str ), p );
	if( fcr.ec != errc() || *fcr.ptr )
		throw invalid_argument( "parameter-error" );
	return p;
}

static void throwSysErr( char const *errStr )
{
#if defined(_MSC_VER)
	int errc = GetLastError();
#elif defined(__unix__)
	int errc = errno;
#endif
	throw system_error( error_code( errc, system_category() ), errStr );
}

static void changeDir( char const *path )
{
#if defined(_MSC_VER)
	if( !SetCurrentDirectoryA( path ) )
#elif defined(__unix__)
	if( chdir( path ) )
#endif
		throwSysErr( (ostringstream() << "Can't set current directory to \"" 
<< path << "\"").str().c_str() );
}

static handle_t createFile( char const *fileName )
{
#if defined(_MSC_VER)
	handle_t handle = CreateFileA( fileName, GENERIC_READ | GENERIC_WRITE, 
0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
	if( handle == INVALID_HANDLE_VALUE )
		throwSysErr( "CreateFile() failed" );
#elif defined(__unix__)
	int handle = creat( fileName, S_IRUSR | S_IWUSR );
	if( handle == -1 )
		throwSysErr( "creat() failed" );
#endif
	return handle;
}

static void closeFile( handle_t handle )
{
#if defined(_MSC_VER)
	BOOL succ = CloseHandle( handle );
#elif defined(__unix__)
	bool succ = !close( handle );
#endif
	assert(succ);
}

static void writeFile( handle_t handle, void const *p, unsigned n )
{
#if defined(_MSC_VER)
	DWORD dwWritten;
	if( !WriteFile( handle, p, n, &dwWritten, nullptr ) || dwWritten != n )
		throwSysErr( "WriteFile() failed" );
#elif defined(__unix__)
	if( write( handle, p, n ) != n )
		throwSysErr( "write() failed" );
#endif
}

static void whatExit( char const *what )
{
	static mutex mtx;
	lock_guard lock( mtx );
	cout << what << endl;
#if defined(_MSC_VER)
	ExitProcess( EXIT_FAILURE );
#elif defined(__unix__)
	::exit( EXIT_FAILURE );
#endif
}

Back to ger.ct | Previous | NextNext in thread | Find similar | Unroll thread


Thread

A thousand files ... Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-24 20:08 +0100
  Re: A thousand files ... Hendrik van der Heijden <hvdh@gmx.de> - 2021-12-24 21:48 +0100
    Re: A thousand files ... Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-25 03:04 +0100
      Re: A thousand files ... Andreas Kohlbach <ank@spamfence.net> - 2021-12-25 05:16 -0500
        Re: A thousand files ... Gerrit Heitsch <gerrit@laosinh.s.bawue.de> - 2021-12-25 11:23 +0100
        Re: A thousand files ... Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-25 11:51 +0100
          Re: A thousand files ... Andreas Kohlbach <ank@spamfence.net> - 2021-12-25 12:01 -0500
      Re: A thousand files ... Hendrik van der Heijden <hvdh@gmx.de> - 2021-12-26 22:12 +0100
        Re: A thousand files ... Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-27 10:33 +0100
          Re: A thousand files ... Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-27 11:21 +0100
            Re: A thousand files ... Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-27 18:32 +0100
              Re: A thousand files ... Dietz Proepper <dietz-usenet@rotfl.franken.de> - 2021-12-28 11:33 +0100
                Re: A thousand files ... Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-28 11:47 +0100
                Re: A thousand files ... Dietz Proepper <dietz-usenet@rotfl.franken.de> - 2021-12-29 00:12 +0100
                Re: A thousand files ... Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-29 06:20 +0100
                Re: A thousand files ... Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-29 06:29 +0100
                Re: A thousand files ... Dietz Proepper <dietz-usenet@rotfl.franken.de> - 2021-12-29 10:49 +0100
                Re: A thousand files ... Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-29 11:58 +0100
                Re: A thousand files ... Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-29 11:59 +0100
                Re: A thousand files ... Dietz Proepper <dietz-usenet@rotfl.franken.de> - 2021-12-29 12:01 +0100
                Re: A thousand files ... Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-29 12:23 +0100
                Re: A thousand files ... "Dr. Joachim Neudert" <neudert@5sl.org> - 2021-12-29 12:44 +0100
                Re: A thousand files ... Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-29 14:30 +0100
  Re: A thousand files ... spamfalle2@arcor.de (Marc Stibane) - 2021-12-25 19:46 +0100
    Re: A thousand files ... Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-25 20:25 +0100
      Re: A thousand files ... spamfalle2@arcor.de (Marc Stibane) - 2021-12-25 22:32 +0100
  Re: A thousand files ... Bonita Montero <Bonita.Montero@gmail.com> - 2021-12-31 12:27 +0100

csiph-web