static void WriteStatusLine(StatusLineWriter statusLine, MaxTasks processes, Process currProcess, Stats stats) { currProcess.Refresh(); string statsThreads = String.Join(" | ", currProcess.Threads.Cast <ProcessThread>() .GroupBy(keySelector: t => t.ThreadState) .Select(grp => $"{grp.Key.ToString()} ({(grp.Key == System.Diagnostics.ThreadState.Running ? grp.Count()-1 : grp.Count())})")); string all; string finished; if (stats.AllItems.HasValue) { all = stats.AllItems.ToString(); float ffinished = (float)processes.Done / (float)stats.AllItems.Value; finished = ffinished.ToString("P"); } else { all = "?"; finished = "?"; } statusLine.Write($"running/done/all/finished\t{processes.Running}/{processes.Done}/{all}/{finished}" //+ $"\tthreads: {currProcess.Threads.Count}" + $"\t(forp thread states: {statsThreads})"); }
private static void RunCompare(Opts opts, CancellationToken CtrlCEvent, TextWriter errWriter) { DateTime start = DateTime.Now; using (DiffWriter diffWriters = new DiffWriter()) { Stats stats = new Stats(); var enumOpts = new EnumOptions() { followJunctions = opts.FollowJunctions, maxDepth = opts.Depth, forceSortSource = opts.forceSortSource, forceSortTarget = opts.forceSortTarget }; DiffProcessing diffProcessor = new DiffProcessing(stats, diffWriters, opts.reportSameFile); string src = Spi.Long.GetLongFilenameNotation(opts.sourceDir.TrimEnd('\\')); string trg = Spi.Long.GetLongFilenameNotation(opts.targetDir.TrimEnd('\\')); Console.Error.WriteLine($"source: {src}\ntarget: {trg}"); var paraCmp = new CmpDirsParallel( sourceDir: src, targetDir: trg, opts: enumOpts, diffHandler: diffProcessor.DiffCallback, errorHandler: (int RetCode, string Message) => { Interlocked.Increment(ref stats.Errors); errWriter.WriteLine($"E: rc={RetCode}\t{Message}"); }, CtrlCEvent: CtrlCEvent, maxThreadsToRun: opts.MaxThreads); paraCmp.Start(); StatusLineWriter statWriter = new StatusLineWriter(); using (Process currProc = System.Diagnostics.Process.GetCurrentProcess()) { Misc.ExecUtilWaitHandleSet(paraCmp.Finished, 2000, () => WriteProgress(stats, currProc, paraCmp.Queued, paraCmp.Running, paraCmp.Done, statWriter)); } WriteStatistics(new TimeSpan(DateTime.Now.Ticks - start.Ticks), paraCmp.Done, stats); if (stats.Errors > 0) { Console.Error.WriteLine($"{stats.Errors} erros occoured. please see file {opts.errorFilename}."); } } }
public static Stats Run(IEnumerable <ProcCtx> ProcessesToStart, TextWriter outStream, TextWriter errStream, int maxParallel, bool skipEmptyLines, bool quiet, bool writeStderr, Func <long> numberJobs) { ProcessRedirectAsync.Init(); Stats stats = new Stats(); if (numberJobs != null) { Task.Run(() => stats.AllItems = numberJobs()); } ICollection <uint> runningProcIDs = new HashSet <uint>(); using (CancellationTokenSource cts = new CancellationTokenSource()) using (TextWriter exitcodeWriter = TextWriter.Synchronized(new StreamWriter(@".\forp.ExitCode.txt", append: false, encoding: Encoding.UTF8))) { log.dbg($"starting with maxParallel: {maxParallel}"); var cancel = cts.Token; if (!quiet) { Console.Error.WriteLine("press 'q' to quit. 'h' for more keys."); } Task.Run(() => HandleKeys(cts, outStream, errStream, runningProcIDs)); var procs = new MaxTasks(); var procsTask = procs.Start( tasks: ProcessesToStart.Select( async(procToRun) => { ProcessStats procStats = await RunOneProcess(procToRun.commandline, procToRun.prefix, outStream, errStream, cancel, skipEmptyLines, writeStderr, runningProcIDs).ConfigureAwait(false); exitcodeWriter.WriteLine($"{procStats.ExitCode}\t{procToRun.commandline}"); Interlocked.Add(ref stats.procTotalTime, procStats.TotalTime.Ticks); Interlocked.Add(ref stats.procKernelTime, procStats.KernelTime.Ticks); Interlocked.Add(ref stats.procUserTime, procStats.UserTime.Ticks); }), MaxParallel: maxParallel, cancel: cts.Token); if (!quiet) { var status = new StatusLineWriter(); var currProcess = Process.GetCurrentProcess(); Misc.DoUntilTaskFinished(procsTask, TimeSpan.FromSeconds(2), () => WriteStatusLine(status, procs, currProcess, stats)); Console.Error.WriteLine(); } else { procsTask.Wait(); } } return(stats); }
private static void WriteProgress(Stats stats, Process currProc, long queued, long running, long cmpsDone, StatusLineWriter statWriter) { currProc.Refresh(); string privMem = currProc == null ? "n/a" : Misc.GetPrettyFilesize(currProc.PrivateMemorySize64); //string threadcount = currProc == null ? "n/a" : currProc.Threads.Count.ToString(); statWriter.Write($"dirs queued/running/done/errors: {queued:N0}/{running}/{cmpsDone:N0}/{stats.Errors:N0}" + $" | new/mod/del/same: {stats.FilesNew:N0}/{stats.FilesMod:N0}/{stats.FilesDel:N0}/{stats.FilesSame:N0}" + $" | privateMem: {privMem}"); }
static int Main(string[] args) { int rc = 99; try { Opts opts; if ((opts = CommandlineOpts.GetOpts(args)) == null) { return(1); } string FullSrcDir = Misc.GetLongFilenameNotation(Path.GetFullPath(opts.SrcBase)); string FullTrgDir = Misc.GetLongFilenameNotation(Path.GetFullPath(opts.TrgBase)); int checkArgs = 0; if (!File.Exists(opts.FilenameWithFiles)) { Console.Error.WriteLine($"input file does not exists. [{opts.FilenameWithFiles}]"); checkArgs = 2; } if (!Misc.IsDirectory(FullSrcDir)) { Console.Error.WriteLine($"source base is not a directory. [{FullSrcDir}]"); checkArgs += 2; } if (!Misc.IsDirectory(FullTrgDir)) { Console.Error.WriteLine($"target base is not a directory. [{FullTrgDir}]"); checkArgs += 2; } if (checkArgs > 0) { return(checkArgs); } Console.Error.WriteLine($"I: setting ThreadPool.SetMinThreads() to {opts.MaxThreads}"); ThreadPool.SetMinThreads(opts.MaxThreads, opts.MaxThreads); Console.Error.WriteLine( $"source base: {FullSrcDir}\n" + $"target base: {FullTrgDir}"); Stats stats = new Stats(); Task <bool> copyFileTask = null; Task.Run(() => CopyFiles.ReadFilesAndSizes(opts.FilenameWithFiles, out stats.totalFiles, out stats.totalBytes)).ConfigureAwait(false); DateTime started = DateTime.Now; using (TextWriter errorWriter = TextWriter.Synchronized(new StreamWriter(@".\cpError.txt", append: false, encoding: System.Text.Encoding.UTF8))) { copyFileTask = CopyFiles.Start(FullSrcDir, FullTrgDir, files: CopyFiles.ReadInputfile(CopyFiles.ReadLines(opts.FilenameWithFiles)), OnCopy: (string filename, UInt64? filesize) => { Interlocked.Increment(ref stats.copiedCount); if (filesize.HasValue) { Interlocked.Add(ref stats.copiedBytes, (long)filesize.Value); } }, OnWin32Error: (LastErrorCode, Api, Message) => { errorWriter.WriteLine($"{LastErrorCode}\t{Api}\t{Message}"); Interlocked.Increment(ref stats.errorsCount); }, OnDirectoryCreated: () => { Interlocked.Increment(ref stats.CreateDirectoryCalled); }, MaxThreads: opts.MaxThreads, dryRun: opts.dryrun); Spi.StatusLineWriter linewriter = new StatusLineWriter(Console.Error); DateTime StartTime = DateTime.Now; WaitUntilFinished(copyFileTask, 2000, ExecEvery: () => linewriter.Write(GetStatsLine(stats, StartTime)), ExecAtEnd: () => Console.Error.WriteLine()); } WriteEndReport(started, stats); rc = copyFileTask.Result ? 8 : 0; } catch (Exception ex) { Console.Error.WriteLine("Na so wirst ned ooohhooiidd... so dastehst Di boohooid..."); ConsoleAndFileWriter.WriteException(ex, Console.Error); rc = 999; } return(rc); }