Esempio n. 1
0
        /// <summary>
        /// Try to create a PartialSymbol from a string.
        /// </summary>
        /// <returns>Return the parser result indicating success, or what was wrong with the parsing.</returns>
        public static ParseResult TryCreate <T>(StringTable table, T partialSymbol, out PartialSymbol result, out int characterWithError)
            where T : struct, ICharSpan <T>
        {
            Contract.Requires(table != null);
            Contract.Ensures((Contract.Result <ParseResult>() == ParseResult.Success) == Contract.ValueAtReturn(out result).IsValid);

            using (var wrap = Pools.GetStringIdList())
            {
                List <StringId> components = wrap.Instance;

                int index = 0;
                int start = 0;
                int last  = partialSymbol.Length - 1;
                while (index < partialSymbol.Length)
                {
                    var ch = partialSymbol[index];

                    // trivial reject of invalid characters
                    if (!SymbolCharacters.IsValidDottedIdentifierChar(ch))
                    {
                        characterWithError = index;
                        result             = Invalid;
                        return(ParseResult.FailureDueToInvalidCharacter);
                    }

                    if (ch == SymbolCharacters.DottedIdentifierSeparatorChar)
                    {
                        // found a component separator
                        if (index == start || index == last)
                        {
                            characterWithError = index;
                            result             = Invalid;
                            return(ParseResult.LeadingOrTrailingDot);
                        }
                        else if (index > start)
                        {
                            // make a identifier atom out of [start..index]
                            SymbolAtom             atom;
                            int                    charError;
                            SymbolAtom.ParseResult papr = SymbolAtom.TryCreate(
                                table,
                                partialSymbol.Subsegment(start, index - start),
                                out atom,
                                out charError);

                            if (papr != SymbolAtom.ParseResult.Success)
                            {
                                characterWithError = index + charError;
                                result             = Invalid;
                                return(ParseResult.FailureDueToInvalidCharacter);
                            }

                            components.Add(atom.StringId);
                        }

                        // skip over the dot
                        index++;
                        start = index;
                        continue;
                    }

                    index++;
                }

                if (index > start)
                {
                    // make a identifier atom out of [start..index]
                    SymbolAtom             atom;
                    int                    charError;
                    SymbolAtom.ParseResult papr = SymbolAtom.TryCreate(
                        table,
                        partialSymbol.Subsegment(start, index - start),
                        out atom,
                        out charError);

                    if (papr != SymbolAtom.ParseResult.Success)
                    {
                        characterWithError = index + charError;
                        result             = Invalid;
                        return(ParseResult.FailureDueToInvalidCharacter);
                    }

                    components.Add(atom.StringId);
                }

                result = new PartialSymbol(components.ToArray());

                characterWithError = -1;
                return(ParseResult.Success);
            }
        }
Esempio n. 2
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);
            }
        /// <summary>
        /// Internal api to try to create a RelativePath from a string.
        /// </summary>
        /// <remarks>This function serves as an internal overload for tryCreate when called from AbsolutePath so we do not get the DotDotOutOfScope error when traversing beyond the root.</remarks>
        /// <param name="table">StringTable instance.</param>
        /// <param name="relativePath">Relative path to pass in.</param>
        /// <param name="result">Output relative path after parsing.</param>
        /// <param name="characterWithError">Output the character that had the error.</param>
        /// <param name="allowDotDotOutOfScope">Whether to allow the function to parse .. beyond the root.</param>
        /// <returns>Return the parser result indicating success, or what was wrong with the parsing.</returns>
        internal static ParseResult TryCreateInternal <T>(StringTable table, T relativePath, out RelativePath result, out int characterWithError, bool allowDotDotOutOfScope = false)
            where T : struct, ICharSpan <T>
        {
            Contract.Requires(table != null);
            //Contract.Ensures((Contract.Result<ParseResult>() == ParseResult.Success) == Contract.ValueAtReturn(out result).IsValid);

            using (var wrap = Pools.GetStringIdList())
            {
                List <StringId> components = wrap.Instance;

                int index = 0;
                int start = 0;
                while (index < relativePath.Length)
                {
                    var ch = relativePath[index];

                    // trivial reject of invalid characters
                    if (!IsValidRelativePathChar(ch))
                    {
                        characterWithError = index;
                        result             = Invalid;
                        return(ParseResult.FailureDueToInvalidCharacter);
                    }

                    if (ch == '\\' || ch == '/')
                    {
                        // found a component separator
                        if (index > start)
                        {
                            // make a path atom out of [start..index]
                            PathAtom.ParseResult papr = PathAtom.TryCreate(
                                table,
                                relativePath.Subsegment(start, index - start),
                                out PathAtom atom,
                                out int charError);

                            if (papr != PathAtom.ParseResult.Success)
                            {
                                characterWithError = index + charError;
                                result             = Invalid;
                                return(ParseResult.FailureDueToInvalidCharacter);
                            }

                            components.Add(atom.StringId);
                        }

                        // skip over the slash
                        index++;
                        start = index;
                        continue;
                    }

                    if (ch == '.' && index == start)
                    {
                        // component starts with a .
                        if ((index == relativePath.Length - 1) ||
                            (relativePath[index + 1] == '\\') ||
                            (relativePath[index + 1] == '/'))
                        {
                            // component is a sole . so skip it
                            index += 2;
                            start  = index;
                            continue;
                        }

                        if (relativePath[index + 1] == '.')
                        {
                            // component starts with ..
                            if ((index == relativePath.Length - 2) ||
                                (relativePath[index + 2] == '\\') ||
                                (relativePath[index + 2] == '/'))
                            {
                                // component is a sole .. so try to go up
                                if (components.Count == 0 && !allowDotDotOutOfScope)
                                {
                                    characterWithError = index;
                                    result             = Invalid;
                                    return(ParseResult.FailureDueToDotDotOutOfScope);
                                }

                                if (components.Count != 0)
                                {
                                    components.RemoveAt(components.Count - 1);
                                }

                                index += 3;
                                start  = index;
                                continue;
                            }
                        }
                    }

                    index++;
                }

                if (index > start)
                {
                    // make a path atom out of [start..index]
                    PathAtom.ParseResult papr = PathAtom.TryCreate(
                        table,
                        relativePath.Subsegment(start, index - start),
                        out PathAtom atom,
                        out int charError);

                    if (papr != PathAtom.ParseResult.Success)
                    {
                        characterWithError = index + charError;
                        result             = Invalid;
                        return(ParseResult.FailureDueToInvalidCharacter);
                    }

                    components.Add(atom.StringId);
                }

                result = new RelativePath(components.ToArray());

                characterWithError = -1;
                return(ParseResult.Success);
            }
        }