static RundotnetData RunDotNet(RundotnetData data) { string language = ""; switch (data.LanguageChoice) { case "1": language = "CSharp"; break; case "2": language = "VisualBasic"; break; case "3": language = "FSharp"; break; } int compilationTimeInMs; CompilerParameters cp = new CompilerParameters(); cp.GenerateExecutable = false; Random rg = new Random(); string folder = reExp.Utils.Utils.RootFolder + @"\executables\usercode\"; string assemblyName = "userAssembly_" + rg.Next(0, 10000000); string path = folder + assemblyName + ".dll"; cp.OutputAssembly = path; // Save the assembly as a physical file. cp.GenerateInMemory = false; // Set whether to treat all warnings as errors. cp.TreatWarningsAsErrors = false; cp.WarningLevel = 4; cp.IncludeDebugInformation = false; cp.ReferencedAssemblies.Add("System.dll"); cp.ReferencedAssemblies.Add("System.Core.dll"); if (language != "FSharp") { cp.ReferencedAssemblies.Add("System.Data.dll"); cp.ReferencedAssemblies.Add("System.Data.DataSetExtensions.dll"); cp.ReferencedAssemblies.Add("System.Xml.dll"); cp.ReferencedAssemblies.Add("System.Xml.Linq.dll"); if (language == "CSharp") cp.ReferencedAssemblies.Add("Microsoft.CSharp.dll"); else if (language == "VisualBasic") cp.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll"); } else { cp.ReferencedAssemblies.Add("System.Numerics.dll"); //string fspath = reExp.Utils.Utils.RootFolder + @"executables\usercode\"; //cp.ReferencedAssemblies.Add(fspath + @"FSharp.Core.dll"); //cp.ReferencedAssemblies.Add(fspath + @"FSharp.Powerpack.dll"); //cp.ReferencedAssemblies.Add(fspath + @"FSharp.PowerPack.Compatibility.dll"); //cp.ReferencedAssemblies.Add(fspath + @"FSharp.PowerPack.Linq.dll"); //cp.ReferencedAssemblies.Add(fspath + @"FSharp.PowerPack.Metadata.dll"); //cp.ReferencedAssemblies.Add(fspath + @"FSharp.PowerPack.Parallel.Seq.dll"); } CompilerResults cr = null; if (language == "FSharp") { using (CodeDomProvider provider = new Microsoft.FSharp.Compiler.CodeDom.FSharpCodeProvider()) { DateTime comp_start = DateTime.Now; // Invoke compilation of the source file. cr = provider.CompileAssemblyFromSource(cp, new string[] { data.Program }); compilationTimeInMs = (int)(DateTime.Now - comp_start).TotalMilliseconds; } } else { using (CodeDomProvider provider = CodeDomProvider.CreateProvider(language)) { DateTime comp_start = DateTime.Now; // Invoke compilation of the source file. cr = provider.CompileAssemblyFromSource(cp, new string[] { data.Program }); compilationTimeInMs = (int)(DateTime.Now - comp_start).TotalMilliseconds; } } var messages = cr.Errors.Cast<CompilerError>(); var warnings = messages.Where(f => f.IsWarning == true); var errors = messages.Where(f => f.IsWarning == false); if (warnings.Count() != 0) { foreach (var warn in warnings) data.Warnings.Add(string.Format("({0}:{1}) {2}", warn.Line, warn.Column, warn.ErrorText)); } if (errors.Count() != 0) { foreach (var ce in errors) { data.Errors.Add(string.Format("({0}:{1}) {2}", ce.Line, ce.Column, ce.ErrorText)); } Utils.Log.LogCodeToDB(data.Program, "Compilation errors"); data.RunStats = string.Format("Compilation time: {0} s", Math.Round((compilationTimeInMs / (double)1000), 2)); return data; } else { using (Process process = new Process()) { try { double TotalMemoryInBytes = 0; double TotalThreadCount = 0; int samplesCount = 0; process.StartInfo.FileName = reExp.Utils.Utils.RootFolder + "executables/SpawnedProcess.exe"; process.StartInfo.Arguments = folder.Replace(" ", "|_|") + " " + assemblyName + " Rextester|Program|Main"; process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; DateTime start = DateTime.Now; process.Start(); //try //{ // process.PriorityClass = ProcessPriorityClass.BelowNormal; //} //catch (Exception) //{ } OutputReader output = new OutputReader(process.StandardOutput); Thread outputReader = new Thread(new ThreadStart(output.ReadOutput)); outputReader.Start(); OutputReader error = new OutputReader(process.StandardError); Thread errorReader = new Thread(new ThreadStart(error.ReadOutput)); errorReader.Start(); do { // Refresh the current process property values. process.Refresh(); if (!process.HasExited) { try { var proc = process.TotalProcessorTime; // Update the values for the overall peak memory statistics. var mem1 = process.PagedMemorySize64; var mem2 = process.PrivateMemorySize64; //update stats TotalMemoryInBytes += (mem1 + mem2); TotalThreadCount += (process.Threads.Count); samplesCount++; if (proc.TotalSeconds > 5 || mem1 + mem2 > 100000000 || process.Threads.Count > 100 || start + TimeSpan.FromSeconds(10) < DateTime.Now) { var time = proc.TotalSeconds; var mem = mem1 + mem2; process.Kill(); var res = string.Format("Process killed because it exceeded given resources.\nCpu time used {0} sec, absolute running time {1} sec, memory used {2} Mb, nr of threads {3}", time, (int)(DateTime.Now - start).TotalSeconds, (int)(mem / 1048576), process.Threads.Count); data.Errors.Add(res); string partialResult = output.Builder.ToString(); data.Output = partialResult; Utils.Log.LogCodeToDB(data.Program, res); data.RunStats = string.Format("Compilation time: {0} sec, absolute running time: {1} sec, cpu time {2} sec, average memory usage: {3} Mb, average nr of threads: {4}", Math.Round((compilationTimeInMs / (double)1000), 2), Math.Round((DateTime.Now - start).TotalSeconds, 2), Math.Round(proc.TotalSeconds, 2), samplesCount != 0 ? (int?)((TotalMemoryInBytes / samplesCount) / 1048576) : null, samplesCount != 0 ? (int?)(TotalThreadCount / samplesCount) : null); return data; } } catch (InvalidOperationException) { break; } } } while (!process.WaitForExit(10)); process.WaitForExit(); data.RunStats = string.Format("Compilation time: {0} sec, absolute running time: {1} sec, cpu time {2} sec, average memory usage: {3} Mb, average nr of threads: {4}", Math.Round((compilationTimeInMs / (double)1000), 2), Math.Round((process.ExitTime - process.StartTime).TotalSeconds, 2), Math.Round(process.TotalProcessorTime.TotalSeconds, 2), samplesCount != 0 ? (int?)((TotalMemoryInBytes / samplesCount) / 1048576) : null, samplesCount != 0 ? (int?)(TotalThreadCount / samplesCount) : null); errorReader.Join(5000); outputReader.Join(5000); if (!string.IsNullOrEmpty(error.Output)) { data.Output = output.Builder.ToString(); data.Errors.Add(error.Output); Utils.Log.LogCodeToDB(data.Program, error.Output); return data; } data.Output = output.Output; Utils.Log.LogCodeToDB(data.Program, "OK"); return data; } catch (Exception e) { if (!process.HasExited) { reExp.Utils.Log.LogInfo("Process left running " + e.Message, "RunDotNet"); } throw; } finally { reExp.Utils.CleanUp.DeleteFile(path); } } } }
private static void GenerateCode(Options options) { var packageTransform = new NMF.Models.Meta.Meta2ClassesTransformation(); var stopWatch = new Stopwatch(); packageTransform.ForceGeneration = options.Force; packageTransform.CreateOperations = options.Operations; packageTransform.DefaultNamespace = options.OverallNamespace; var metaPackage = LoadPackageFromFiles(options.InputFiles, options.OverallNamespace); var model = metaPackage.Model; if (model == null) { model = new Model(); model.RootElements.Add(metaPackage); } model.ModelUri = metaPackage.Uri; if (options.NMeta != null) { using (var fs = File.OpenWrite(options.NMeta)) { MetaRepository.Instance.Serializer.Serialize(model, fs); } } stopWatch.Start(); var compileUnit = TransformationEngine.Transform<INamespace, CodeCompileUnit>(metaPackage, options.Parallel ? (ITransformationEngineContext)new ParallelTransformationContext(packageTransform) : new TransformationContext(packageTransform)); stopWatch.Stop(); Console.WriteLine("Operation took {0}ms", stopWatch.Elapsed.TotalMilliseconds); CodeDomProvider generator = null; switch (options.Language) { case SupportedLanguage.CS: generator = new Microsoft.CSharp.CSharpCodeProvider(); break; case SupportedLanguage.VB: generator = new Microsoft.VisualBasic.VBCodeProvider(); break; case SupportedLanguage.FS: generator = new Microsoft.FSharp.Compiler.CodeDom.FSharpCodeProvider(); break; case SupportedLanguage.CPP: generator = new Microsoft.VisualC.CppCodeProvider(); break; case SupportedLanguage.JS: generator = new Microsoft.JScript.JScriptCodeProvider(); break; default: Console.WriteLine("Unknown language detected. Falling back to default C#"); generator = new Microsoft.CSharp.CSharpCodeProvider(); break; } var genOptions = new System.CodeDom.Compiler.CodeGeneratorOptions() { BlankLinesBetweenMembers = true, VerbatimOrder = false, ElseOnClosing = false, BracingStyle = "C", IndentString = " " }; if (options.UseFolders) { foreach (var file in MetaFacade.SplitCompileUnit(compileUnit)) { var fileInfo = new FileInfo(Path.Combine(options.OutputFile, file.Key) + "." + generator.FileExtension); CheckDirectoryExists(fileInfo.Directory); using (var fs = fileInfo.Create()) { using (var sw = new StreamWriter(fs)) { generator.GenerateCodeFromCompileUnit(file.Value, sw, genOptions); } } } } else { using (var sw = new StreamWriter(options.OutputFile)) { generator.GenerateCodeFromCompileUnit(compileUnit, sw, genOptions); } } Console.WriteLine("Code generated successfully!"); }
static RundotnetData RunDotNet(RundotnetData data) { string language = ""; switch (data.LanguageChoice) { case "1": language = "CSharp"; break; case "2": language = "VisualBasic"; break; case "3": language = "FSharp"; break; } int compilationTimeInMs; CompilerParameters cp = new CompilerParameters(); cp.GenerateExecutable = false; Random rg = new Random(); string folder = reExp.Utils.Utils.RootFolder + @"\executables\usercode\"; string assemblyName = "userAssembly_" + rg.Next(0, 10000000); string path = folder + assemblyName + ".dll"; cp.OutputAssembly = path; // Save the assembly as a physical file. cp.GenerateInMemory = false; // Set whether to treat all warnings as errors. cp.TreatWarningsAsErrors = false; cp.WarningLevel = 4; cp.IncludeDebugInformation = false; cp.ReferencedAssemblies.Add("System.dll"); cp.ReferencedAssemblies.Add("System.Core.dll"); if (language != "FSharp") { cp.ReferencedAssemblies.Add("System.Data.dll"); cp.ReferencedAssemblies.Add("System.Data.DataSetExtensions.dll"); cp.ReferencedAssemblies.Add("System.Xml.dll"); cp.ReferencedAssemblies.Add("System.Xml.Linq.dll"); if (language == "CSharp") { cp.ReferencedAssemblies.Add("Microsoft.CSharp.dll"); } else if (language == "VisualBasic") { cp.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll"); } } else { cp.ReferencedAssemblies.Add("System.Numerics.dll"); //string fspath = reExp.Utils.Utils.RootFolder + @"executables\usercode\"; //cp.ReferencedAssemblies.Add(fspath + @"FSharp.Core.dll"); //cp.ReferencedAssemblies.Add(fspath + @"FSharp.Powerpack.dll"); //cp.ReferencedAssemblies.Add(fspath + @"FSharp.PowerPack.Compatibility.dll"); //cp.ReferencedAssemblies.Add(fspath + @"FSharp.PowerPack.Linq.dll"); //cp.ReferencedAssemblies.Add(fspath + @"FSharp.PowerPack.Metadata.dll"); //cp.ReferencedAssemblies.Add(fspath + @"FSharp.PowerPack.Parallel.Seq.dll"); } CompilerResults cr = null; if (language == "FSharp") { using (CodeDomProvider provider = new Microsoft.FSharp.Compiler.CodeDom.FSharpCodeProvider()) { DateTime comp_start = DateTime.Now; // Invoke compilation of the source file. cr = provider.CompileAssemblyFromSource(cp, new string[] { data.Program }); compilationTimeInMs = (int)(DateTime.Now - comp_start).TotalMilliseconds; } } else { using (CodeDomProvider provider = CodeDomProvider.CreateProvider(language)) { DateTime comp_start = DateTime.Now; // Invoke compilation of the source file. cr = provider.CompileAssemblyFromSource(cp, new string[] { data.Program }); compilationTimeInMs = (int)(DateTime.Now - comp_start).TotalMilliseconds; } } var messages = cr.Errors.Cast <CompilerError>(); var warnings = messages.Where(f => f.IsWarning == true); var errors = messages.Where(f => f.IsWarning == false); if (warnings.Count() != 0) { foreach (var warn in warnings) { data.Warnings.Add(string.Format("({0}:{1}) {2}", warn.Line, warn.Column, warn.ErrorText)); } } if (errors.Count() != 0) { foreach (var ce in errors) { data.Errors.Add(string.Format("({0}:{1}) {2}", ce.Line, ce.Column, ce.ErrorText)); } Utils.Log.LogCodeToDB(data.Program, "Compilation errors"); data.RunStats = string.Format("Compilation time: {0} s", Math.Round((compilationTimeInMs / (double)1000), 2)); return(data); } else { using (Process process = new Process()) { try { double TotalMemoryInBytes = 0; double TotalThreadCount = 0; int samplesCount = 0; process.StartInfo.FileName = reExp.Utils.Utils.RootFolder + "executables/SpawnedProcess.exe"; process.StartInfo.Arguments = folder.Replace(" ", "|_|") + " " + assemblyName + " Rextester|Program|Main"; process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; DateTime start = DateTime.Now; process.Start(); //try //{ // process.PriorityClass = ProcessPriorityClass.BelowNormal; //} //catch (Exception) //{ } OutputReader output = new OutputReader(process.StandardOutput); Thread outputReader = new Thread(new ThreadStart(output.ReadOutput)); outputReader.Start(); OutputReader error = new OutputReader(process.StandardError); Thread errorReader = new Thread(new ThreadStart(error.ReadOutput)); errorReader.Start(); do { // Refresh the current process property values. process.Refresh(); if (!process.HasExited) { try { var proc = process.TotalProcessorTime; // Update the values for the overall peak memory statistics. var mem1 = process.PagedMemorySize64; var mem2 = process.PrivateMemorySize64; //update stats TotalMemoryInBytes += (mem1 + mem2); TotalThreadCount += (process.Threads.Count); samplesCount++; if (proc.TotalSeconds > 5 || mem1 + mem2 > 100000000 || process.Threads.Count > 100 || start + TimeSpan.FromSeconds(10) < DateTime.Now) { var time = proc.TotalSeconds; var mem = mem1 + mem2; process.Kill(); var res = string.Format("Process killed because it exceeded given resources.\nCpu time used {0} sec, absolute running time {1} sec, memory used {2} Mb, nr of threads {3}", time, (int)(DateTime.Now - start).TotalSeconds, (int)(mem / 1048576), process.Threads.Count); data.Errors.Add(res); string partialResult = output.Builder.ToString(); data.Output = partialResult; Utils.Log.LogCodeToDB(data.Program, res); data.RunStats = string.Format("Compilation time: {0} sec, absolute running time: {1} sec, cpu time {2} sec, average memory usage: {3} Mb, average nr of threads: {4}", Math.Round((compilationTimeInMs / (double)1000), 2), Math.Round((DateTime.Now - start).TotalSeconds, 2), Math.Round(proc.TotalSeconds, 2), samplesCount != 0 ? (int?)((TotalMemoryInBytes / samplesCount) / 1048576) : null, samplesCount != 0 ? (int?)(TotalThreadCount / samplesCount) : null); return(data); } } catch (InvalidOperationException) { break; } } }while (!process.WaitForExit(10)); process.WaitForExit(); data.RunStats = string.Format("Compilation time: {0} sec, absolute running time: {1} sec, cpu time {2} sec, average memory usage: {3} Mb, average nr of threads: {4}", Math.Round((compilationTimeInMs / (double)1000), 2), Math.Round((process.ExitTime - process.StartTime).TotalSeconds, 2), Math.Round(process.TotalProcessorTime.TotalSeconds, 2), samplesCount != 0 ? (int?)((TotalMemoryInBytes / samplesCount) / 1048576) : null, samplesCount != 0 ? (int?)(TotalThreadCount / samplesCount) : null); errorReader.Join(5000); outputReader.Join(5000); if (!string.IsNullOrEmpty(error.Output)) { data.Output = output.Builder.ToString(); data.Errors.Add(error.Output); Utils.Log.LogCodeToDB(data.Program, error.Output); return(data); } data.Output = output.Output; Utils.Log.LogCodeToDB(data.Program, "OK"); return(data); } catch (Exception e) { if (!process.HasExited) { reExp.Utils.Log.LogInfo("Process left running " + e.Message, "RunDotNet"); } throw; } finally { reExp.Utils.CleanUp.DeleteFile(path); } } } }