Example #1
0
        /// <summary>
        /// Returns a string representation of the relative identifier.
        /// </summary>
        /// <param name="table">The string table used when creating the PartialSymbol.</param>
        public string ToString(StringTable table)
        {
            Contract.Requires(IsValid);
            Contract.RequiresNotNull(table);

            int length = 0;

            foreach (StringId component in m_components)
            {
                length += table.GetLength(component) + 1;
            }

            if (length > 0)
            {
                length--;
            }

            using (var wrap = Pools.GetStringBuilder())
            {
                StringBuilder sb = wrap.Instance;

                foreach (StringId component in m_components)
                {
                    if (sb.Length > 0)
                    {
                        sb.Append(SymbolCharacters.DottedIdentifierSeparatorChar);
                    }

                    table.CopyString(component, sb);
                }

                return(sb.ToString());
            }
        }
        /// <summary>
        /// Returns a string representation of the relative path.
        /// </summary>
        /// <param name="table">The string table used when creating the RelativePath.</param>
        /// <param name="pathFormat">Optional override for path format.</param>
        public string ToString(StringTable table, PathFormat pathFormat = PathFormat.HostOs)
        {
            Contract.Requires(IsValid);
            Contract.Requires(table != null);
            Contract.Ensures(Contract.Result <string>() != null);

            var pathSeparator = PathFormatter.GetPathSeparator(pathFormat);

            using (var wrap = Pools.GetStringBuilder())
            {
                StringBuilder sb = wrap.Instance;

                foreach (StringId component in Components)
                {
                    if (sb.Length > 0)
                    {
                        sb.Append(pathSeparator);
                    }

                    table.CopyString(component, sb);
                }

                return(sb.ToString());
            }
        }
Example #3
0
        /// <summary>
        /// Escapes a string for usage as an app name embedded in the lpCommandLine parameter of CreateProcess
        /// (see http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx).
        /// <see cref="System.Diagnostics.Process" /> performs similar escaping for <see cref="System.Diagnostics.ProcessStartInfo.FileName" />
        /// It is invalid to attempt to escape a string that contains only whitespace or contains double-quotes.
        /// </summary>
        /// <param name="exe">Application name / path to escape. May not contain double quotes.</param>
        /// <returns>An escaped path</returns>
        public static string EscapeAsCreateProcessApplicationName(string exe)
        {
            Contract.RequiresNotNull(exe);

            using (PooledObjectWrapper <StringBuilder> wrap = Pools.GetStringBuilder())
            {
                StringBuilder builder = wrap.Instance;
                builder.AppendEscapedCreateProcessApplicationName(exe);
                return(builder.ToString());
            }
        }
Example #4
0
        /// <inheritdoc />
        public override string ToString()
        {
            using (var stringBuilderWrapper = Pools.GetStringBuilder())
            {
                var stringBuilder = stringBuilderWrapper.Instance;
                for (int i = 0; i < Length; i++)
                {
                    stringBuilder.Append(this[i]);
                }

                return(stringBuilder.ToString());
            }
        }
Example #5
0
        /// <summary>
        /// Escapes the given string such that the Microsoft C or C++ runtimes would parse it as a single command-line word
        /// (element in argv after parsing).
        /// A command line can be formed for a conformant parser by joining words with spaces:
        /// <c>string joinedArgs = string.Join(" ", args.Select(EscapeAsCommandLineWord);</c>
        /// A full command line including program name (suitable for CreateProcess) can be formed by using
        /// <see cref="EscapeAsCreateProcessApplicationName" />:
        /// <c>string fullCommandLine = EscapeAsCreateProcessApplicationName(appName) + " " + joinedArgs;</c>
        /// </summary>
        /// <param name="word">String to escape. The string may safely contain spaces, double quotes, and backslashes.</param>
        /// <returns>An escaped string</returns>
        /// <remarks>
        /// See http://msdn.microsoft.com/en-us/library/a1y7w461.aspx and http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
        /// Rules (from the C runtime link):
        /// - Arguments are delimited by white space, which is either a space or a tab.
        /// - A string surrounded by double quotation marks is interpreted as a single argument, regardless of white space
        /// contained within. A quoted string can be embedded in
        /// an argument. Note that the caret (^) is not recognized as an escape character or delimiter.
        /// - A double quotation mark preceded by a backslash, \", is interpreted as a literal double quotation mark (").
        /// - Backslashes are interpreted literally, unless they immediately precede a double quotation mark.
        /// - If an even number of backslashes is followed by a double quotation mark, then one backslash (\) is placed in the argv
        /// array for every pair of backslashes (\\),
        /// and the double quotation mark (") is interpreted as a string delimiter.
        /// - If an odd number of backslashes is followed by a double quotation mark, then one backslash (\) is placed in the argv
        /// array for every pair of backslashes (\\)
        /// and the double quotation mark is interpreted as an escape sequence by the remaining backslash, causing a literal double
        /// quotation mark (") to be placed in argv.
        /// </remarks>
        public static string EscapeAsCommandLineWord(string word)
        {
            Contract.RequiresNotNull(word);

            using (PooledObjectWrapper <StringBuilder> wrap = Pools.GetStringBuilder())
            {
                StringBuilder builder = wrap.Instance;
                var           length  = builder.AppendEscapedCommandLineWord(word);
                var           result  = builder.ToString();
                Contract.Assert(length == result.Length);
                return(result);
            }
        }
Example #6
0
        public string ToString(HierarchicalNameTable symbolTable, char separator = '.')
        {
            Contract.RequiresNotNull(symbolTable);

            using (PooledObjectWrapper <StringBuilder> wrap = Pools.GetStringBuilder())
            {
                symbolTable.StringTable.CopyString(Head.StringId, wrap.Instance);
                DottedIdentifier tail = m_tail;
                while (tail != null)
                {
                    wrap.Instance.Append(separator);
                    symbolTable.StringTable.CopyString(tail.Head.StringId, wrap.Instance);
                    tail = tail.m_tail;
                }

                return(wrap.Instance.ToString());
            }
        }
Example #7
0
            /// <summary>
            /// Compute machine perf info to get more human-readable resource usage info
            /// </summary>
            /// <param name="ensureSample">when true and no performance measurement samples are registered, immediately forces a collection of a performance
            /// measurement sample</param>
            public MachinePerfInfo ComputeMachinePerfInfo(bool ensureSample = false)
            {
                if (ensureSample && Volatile.Read(ref m_sampleCount) == 0)
                {
                    m_parent.CollectOnce();
                }

                MachinePerfInfo perfInfo = default(MachinePerfInfo);

                using (var sbPool = Pools.GetStringBuilder())
                    using (var sbPool2 = Pools.GetStringBuilder())
                    {
                        StringBuilder consoleSummary = sbPool.Instance;
                        StringBuilder logFileSummary = sbPool2.Instance;

                        perfInfo.CpuUsagePercentage = SafeConvert.ToInt32(MachineCpu.Latest);
                        consoleSummary.AppendFormat("CPU:{0}%", perfInfo.CpuUsagePercentage);
                        logFileSummary.AppendFormat("CPU:{0}%", perfInfo.CpuUsagePercentage);
                        if (MachineTotalPhysicalMB.Latest > 0)
                        {
                            var availableRam = SafeConvert.ToInt32(MachineAvailablePhysicalMB.Latest);
                            var totalRam     = SafeConvert.ToInt32(MachineTotalPhysicalMB.Latest);

                            var ramUsagePercentage = SafeConvert.ToInt32(((100.0 * (totalRam - availableRam)) / totalRam));
                            Contract.Assert(ramUsagePercentage >= 0 && ramUsagePercentage <= 100);

                            perfInfo.RamUsagePercentage = ramUsagePercentage;
                            perfInfo.TotalRamMb         = totalRam;
                            perfInfo.AvailableRamMb     = availableRam;
                            consoleSummary.AppendFormat(" RAM:{0}%", ramUsagePercentage);
                            logFileSummary.AppendFormat(" RAM:{0}%", ramUsagePercentage);
                        }

                        if (CommitLimitMB.Latest > 0)
                        {
                            var commitTotal           = SafeConvert.ToInt32(CommitTotalMB.Latest);
                            var commitLimit           = SafeConvert.ToInt32(CommitLimitMB.Latest);
                            var commitUsagePercentage = SafeConvert.ToInt32(((100.0 * commitTotal) / commitLimit));

                            perfInfo.CommitUsagePercentage = commitUsagePercentage;
                            perfInfo.CommitTotalMb         = commitTotal;
                            perfInfo.CommitLimitMb         = commitLimit;
                        }

                        if (MachineBandwidth.Latest > 0)
                        {
                            perfInfo.MachineBandwidth           = (long)MachineBandwidth.Latest;
                            perfInfo.MachineKbitsPerSecSent     = MachineKbitsPerSecSent.Latest;
                            perfInfo.MachineKbitsPerSecReceived = MachineKbitsPerSecReceived.Latest;
                        }

                        if (DiskStats != null)
                        {
                            perfInfo.DiskUsagePercentages = new int[DiskStats.Count];
                            perfInfo.DiskQueueDepths      = new int[DiskStats.Count];

                            string worstDrive        = "N/A";
                            int    highestActiveTime = -1;
                            int    highestQueueDepth = 0;

                            // Loop through and find the worst looking disk
                            int diskIndex = 0;
                            foreach (var disk in DiskStats)
                            {
                                if (disk.ReadTime.Maximum == 0)
                                {
                                    perfInfo.DiskUsagePercentages[diskIndex] = 0;
                                    perfInfo.DiskQueueDepths[diskIndex]      = 0;
                                    diskIndex++;
                                    // Don't consider samples unless some activity has been registered
                                    continue;
                                }

                                var activeTime = disk.CalculateActiveTime(lastOnly: true);
                                var queueDepth = SafeConvert.ToInt32(disk.QueueDepth.Latest);
                                perfInfo.DiskUsagePercentages[diskIndex] = activeTime;
                                perfInfo.DiskQueueDepths[diskIndex]      = queueDepth;
                                diskIndex++;

                                logFileSummary.Append(FormatDiskUtilization(disk.Drive, activeTime));

                                if (activeTime > highestActiveTime)
                                {
                                    worstDrive        = disk.Drive;
                                    highestActiveTime = activeTime;
                                    highestQueueDepth = queueDepth;
                                }
                            }

                            if (highestActiveTime != -1)
                            {
                                consoleSummary.Append(FormatDiskUtilization(worstDrive, highestActiveTime));
                            }
                        }

                        perfInfo.ProcessCpuPercentage = SafeConvert.ToInt32(ProcessCpu.Latest);
                        logFileSummary.AppendFormat(" DominoCPU:{0}%", perfInfo.ProcessCpuPercentage);

                        perfInfo.ProcessWorkingSetMB = SafeConvert.ToInt32(ProcessWorkingSetMB.Latest);
                        logFileSummary.AppendFormat(" DominoRAM:{0}MB", perfInfo.ProcessWorkingSetMB);

                        perfInfo.ConsoleResourceSummary = consoleSummary.ToString();
                        perfInfo.LogResourceSummary     = logFileSummary.ToString();
                    }

                return(perfInfo);
            }