Beispiel #1
0
        public OutputData DoWork(InputData idata)
        {
            CompilerData cdata = null;

            try
            {
                OutputData odata = new OutputData();
                cdata = CreateExecutable(idata);
                if (!cdata.Success)
                {
                    odata.Errors   = cdata.Error;
                    odata.Warnings = cdata.Warning;
                    odata.Stats    = string.Format("Compilation time: {0} sec", Math.Round((double)cdata.CompileTimeMs / (double)1000, 2));
                    return(odata);
                }
                if (!string.IsNullOrEmpty(cdata.Warning))
                {
                    odata.Warnings = cdata.Warning;
                }

                Stopwatch watch = new Stopwatch();
                watch.Start();
                using (Process process = new Process())
                {
                    process.StartInfo.FileName = ParentRootPath + "parent.py";
                    //process.StartInfo.FileName = "/home/ren/a.out";
                    process.StartInfo.Arguments              = cdata.Executor + (string.IsNullOrEmpty(cdata.Executor) ? "" : " ") + cdata.ExecuteThis;
                    process.StartInfo.UseShellExecute        = false;
                    process.StartInfo.CreateNoWindow         = true;
                    process.StartInfo.RedirectStandardError  = true;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardInput  = true;

                    process.Start();

                    if (!string.IsNullOrEmpty(idata.Input))
                    {
                        InputWriter input       = new InputWriter(process.StandardInput, idata.Input);
                        Thread      inputWriter = new Thread(new ThreadStart(input.Writeinput));
                        inputWriter.Start();
                    }

                    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();

                    process.WaitForExit();

                    errorReader.Join(5000);
                    outputReader.Join(5000);

                    if (!string.IsNullOrEmpty(error.Output))
                    {
                        int index = error.Output.LastIndexOf('\n');
                        int exitcode;
                        if (index != -1 && index + 1 < error.Output.Length)
                        {
                            string[] info = error.Output.Substring(index + 1).Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                            if (info.Length > 0 && Int32.TryParse(info[0], out exitcode))
                            {
                                odata.ExitCode = exitcode;
                                switch (exitcode)
                                {
                                case -8:
                                    odata.Exit_Status = "Floating point exception (SIGFPE)";
                                    break;

                                case -9:
                                    odata.Exit_Status = "Kill signal (SIGKILL)";
                                    break;

                                case -11:
                                    odata.Exit_Status = "Invalid memory reference (SIGSEGV)";
                                    break;

                                case -6:
                                    odata.Exit_Status = "Abort signal from abort(3) (SIGABRT)";
                                    break;

                                case -4:
                                    odata.Exit_Status = "Illegal instruction (SIGILL)";
                                    break;

                                case -13:
                                    odata.Exit_Status = "Broken pipe: write to pipe with no readers (SIGPIPE)";
                                    break;

                                case -14:
                                    odata.Exit_Status = "Timer signal from alarm(2) (SIGALRM)";
                                    break;

                                case -15:
                                    odata.Exit_Status = "Termination signal (SIGTERM)";
                                    break;

                                case -19:
                                    odata.Exit_Status = "Stop process (SIGSTOP)";
                                    break;

                                case -17:
                                    odata.Exit_Status = "Child stopped or terminated (SIGCHLD)";
                                    break;

                                default:
                                    odata.Exit_Status = string.Format("Exit code: {0} (see 'man 7 signal' for explanation)", exitcode);
                                    break;
                                }

                                error.Output = error.Output.Substring(0, index);
                                if (info.Length > 1)
                                {
                                    double cpuTime;
                                    Double.TryParse(info[1], out cpuTime);
                                    CpuTimeInSec = cpuTime;
                                }
                                if (info.Length > 2)
                                {
                                    int memory;
                                    Int32.TryParse(info[2], out memory);
                                    MemoryPickInKilobytes = memory;
                                }
                            }
                        }
                    }
                    odata.Errors = error.Output;
                    odata.Output = output.Output;
                    if (idata.Lang == Languages.Octave)
                    {
                        string bad_err = "error: No such file or directory" + Environment.NewLine + "error: ignoring octave_execution_exception while preparing to exit";
                        if (odata.Errors != null && odata.Errors.Contains(bad_err))
                        {
                            odata.Errors = odata.Errors.Replace(bad_err, "");
                            if (string.IsNullOrEmpty(odata.Errors.Trim()))
                            {
                                odata.Errors = null;
                            }
                        }
                        List <FileData> files = new List <FileData>();
                        foreach (string file_name in Directory.GetFiles(cdata.CleanThis, "*.png"))
                        {
                            var file = new FileData();
                            file.Data         = File.ReadAllBytes(file_name);
                            file.CreationDate = File.GetCreationTime(file_name);
                            files.Add(file);
                        }
                        odata.Files = files.OrderBy(f => f.CreationDate).Select(f => f.Data).ToList();
                    }
                    if (idata.Lang == Languages.R)
                    {
                        string bad_err = "sh: /bin/rm: Permission denied";
                        if (odata.Errors != null && odata.Errors.Contains(bad_err))
                        {
                            odata.Errors = odata.Errors.Replace(bad_err, "");
                            if (string.IsNullOrEmpty(odata.Errors.Trim()))
                            {
                                odata.Errors = null;
                            }
                        }
                        if (File.Exists(Path.Combine(cdata.CleanThis, "Rplots.pdf")))
                        {
                            using (var p = new Process())
                            {
                                process.StartInfo.FileName         = "pdftoppm";
                                process.StartInfo.WorkingDirectory = cdata.CleanThis;
                                process.StartInfo.Arguments        = "-png Rplots.pdf plots";
                                process.StartInfo.UseShellExecute  = false;
                                process.StartInfo.CreateNoWindow   = true;
                                process.Start();
                                process.WaitForExit();
                            }
                            List <FileData> files = new List <FileData>();
                            foreach (string file_name in Directory.GetFiles(cdata.CleanThis, "*.png"))
                            {
                                var file = new FileData();
                                file.Data         = File.ReadAllBytes(file_name);
                                file.CreationDate = File.GetCreationTime(file_name);
                                files.Add(file);
                            }
                            odata.Files = files.OrderBy(f => f.CreationDate).Select(f => f.Data).ToList();
                        }
                    }
                }
                watch.Stop();

                if (Utils.IsCompiled(idata.Lang))
                {
                    odata.Stats = string.Format("Compilation time: {0} sec, absolute running time: {1} sec, cpu time: {2} sec, memory peak: {3} Mb", Math.Round((double)cdata.CompileTimeMs / (double)1000, 2), Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2), Math.Round(CpuTimeInSec, 2), MemoryPickInKilobytes / 1024);
                }
                else
                {
                    odata.Stats = string.Format("Absolute running time: {0} sec, cpu time: {1} sec, memory peak: {2} Mb", Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2), Math.Round(CpuTimeInSec, 2), MemoryPickInKilobytes / 1024);
                }
                return(odata);
            }
            catch (Exception ex)
            {
                return(new OutputData()
                {
                    System_Error = ex.Message
                });
            }
            finally
            {
                if (cdata != null)
                {
                    Cleanup(cdata.CleanThis);
                }
            }
        }
Beispiel #2
0
        public OutputData DoWork(InputData idata)
        {
            CompilerData cdata = null;
            try
            {
                OutputData odata = new OutputData();
                cdata = CreateExecutable(idata);
                if(!cdata.Success)
                {
                    odata.Errors = cdata.Error;
                    odata.Warnings = cdata.Warning;
                    odata.Stats = string.Format("Compilation time: {0} sec", Math.Round((double)cdata.CompileTimeMs/(double)1000, 2));
                    return odata;
                }
                if(!string.IsNullOrEmpty(cdata.Warning))
                {
                    odata.Warnings = cdata.Warning;
                }

                Stopwatch watch = new Stopwatch();
                watch.Start();
                using(Process process = new Process())
                {
                    process.StartInfo.FileName = ParentRootPath+"parent.py";
                    //process.StartInfo.FileName = "/home/ren/a.out";
                    process.StartInfo.Arguments = cdata.Executor+(string.IsNullOrEmpty(cdata.Executor) ? "" : " ")+cdata.ExecuteThis;
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.CreateNoWindow = true;
                    process.StartInfo.RedirectStandardError = true;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardInput = true;

                    process.Start();

                    if (!string.IsNullOrEmpty(idata.Input))
                    {
                        InputWriter input = new InputWriter(process.StandardInput, idata.Input);
                        Thread inputWriter = new Thread(new ThreadStart(input.Writeinput));
                        inputWriter.Start();
                    }

                    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();

                    process.WaitForExit();

                    errorReader.Join(5000);
                    outputReader.Join(5000);

                    if(!string.IsNullOrEmpty(error.Output))
                    {
                        int index = error.Output.LastIndexOf('\n');
                        int exitcode;
                        if(index != -1 && index+1 < error.Output.Length)
                        {
                            string[] info = error.Output.Substring(index+1).Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                            if(info.Length > 0 && Int32.TryParse(info[0], out exitcode))
                            {
                                odata.ExitCode = exitcode;
                                switch(exitcode)
                                {
                                    case -8:
                                        odata.Exit_Status = "Floating point exception (SIGFPE)";
                                        break;
                                    case -9:
                                        odata.Exit_Status = "Kill signal (SIGKILL)";
                                        break;
                                    case -11:
                                        odata.Exit_Status = "Invalid memory reference (SIGSEGV)";
                                        break;
                                    case -6:
                                        odata.Exit_Status = "Abort signal from abort(3) (SIGABRT)";
                                        break;
                                    case -4:
                                        odata.Exit_Status = "Illegal instruction (SIGILL)";
                                        break;
                                    case -13:
                                        odata.Exit_Status = "Broken pipe: write to pipe with no readers (SIGPIPE)";
                                        break;
                                    case -14:
                                        odata.Exit_Status = "Timer signal from alarm(2) (SIGALRM)";
                                        break;
                                    case -15:
                                        odata.Exit_Status = "Termination signal (SIGTERM)";
                                        break;
                                    case -19:
                                        odata.Exit_Status = "Stop process (SIGSTOP)";
                                        break;
                                    case -17:
                                        odata.Exit_Status = "Child stopped or terminated (SIGCHLD)";
                                        break;
                                    default:
                                        odata.Exit_Status = string.Format("Exit code: {0} (see 'man 7 signal' for explanation)", exitcode);
                                        break;
                                }

                                error.Output = error.Output.Substring(0, index);
                                if(info.Length > 1)
                                {
                                    double cpuTime;
                                    Double.TryParse(info[1], out cpuTime);
                                    CpuTimeInSec = cpuTime;
                                }
                                if(info.Length > 2)
                                {
                                    int memory;
                                    Int32.TryParse(info[2], out memory);
                                    MemoryPickInKilobytes = memory;
                                }
                            }

                        }
                    }
                    odata.Errors = error.Output;
                    odata.Output = output.Output;
                    if(idata.Lang == Languages.Octave)
                    {
                        string bad_err = "error: No such file or directory"+Environment.NewLine+"error: ignoring octave_execution_exception while preparing to exit";
                        if(odata.Errors != null && odata.Errors.Contains(bad_err))
                        {
                            odata.Errors = odata.Errors.Replace(bad_err, "");
                            if(string.IsNullOrEmpty(odata.Errors.Trim()))
                            {
                                odata.Errors = null;
                            }
                        }
                        List<FileData> files = new List<FileData>();
                        foreach (string file_name in Directory.GetFiles(cdata.CleanThis, "*.png"))
                        {
                            var file = new FileData();
                            file.Data = File.ReadAllBytes(file_name);
                            file.CreationDate = File.GetCreationTime(file_name);
                            files.Add(file);
                        }
                        odata.Files = files.OrderBy(f => f.CreationDate).Select(f => f.Data).ToList();
                    }
                    if(idata.Lang == Languages.R)
                    {
                        string bad_err = "sh: /bin/rm: Permission denied";
                        if(odata.Errors != null && odata.Errors.Contains(bad_err))
                        {
                            odata.Errors = odata.Errors.Replace(bad_err, "");
                            if(string.IsNullOrEmpty(odata.Errors.Trim()))
                            {
                                odata.Errors = null;
                            }
                        }
                        if(File.Exists(Path.Combine(cdata.CleanThis, "Rplots.pdf")))
                        {
                            using(var p = new Process())
                            {
                                process.StartInfo.FileName = "pdftoppm";
                                process.StartInfo.WorkingDirectory = cdata.CleanThis;
                                process.StartInfo.Arguments = "-png Rplots.pdf plots";
                                process.StartInfo.UseShellExecute = false;
                                process.StartInfo.CreateNoWindow = true;
                                process.Start();
                                process.WaitForExit();
                            }
                            List<FileData> files = new List<FileData>();
                            foreach (string file_name in Directory.GetFiles(cdata.CleanThis, "*.png"))
                            {
                                var file = new FileData();
                                file.Data = File.ReadAllBytes(file_name);
                                file.CreationDate = File.GetCreationTime(file_name);
                                files.Add(file);
                            }
                            odata.Files = files.OrderBy(f => f.CreationDate).Select(f => f.Data).ToList();
                        }
                    }
                }
                watch.Stop();

                if(Utils.IsCompiled(idata.Lang))
                {
                    odata.Stats = string.Format("Compilation time: {0} sec, absolute running time: {1} sec, cpu time: {2} sec, memory peak: {3} Mb", Math.Round((double)cdata.CompileTimeMs / (double)1000, 2), Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2), Math.Round(CpuTimeInSec, 2), MemoryPickInKilobytes/1024);
                }
                else
                {
                    odata.Stats = string.Format("Absolute running time: {0} sec, cpu time: {1} sec, memory peak: {2} Mb", Math.Round((double)watch.ElapsedMilliseconds / (double)1000, 2), Math.Round(CpuTimeInSec, 2), MemoryPickInKilobytes/1024);
                }
                return odata;
            }
            catch(Exception ex)
            {
                return new OutputData()
                    {
                        System_Error = ex.Message
                    };
            }
            finally
            {
                if(cdata != null)
                    Cleanup(cdata.CleanThis);
            }
        }