Repository: ray-project/ray
File: python/ray/dashboard/modules/reporter/reporter_agent.py (Python)

Code before:
        Returns:
            a list of Record class that will be exposed to Prometheus.
        """
        total_cpu_percentage = 0.0
        total_gpu_percentage = 0.0
        total_gpu_memory = 0.0
        total_rss = 0.0
        total_uss = 0.0
        total_shm = 0.0
        total_num_fds = 0
        for stat in stats:
            total_cpu_percentage += float(stat.get("cpu_percent", 0.0))  # noqa

            # Aggregate GPU stats if available
            total_gpu_percentage += float(stat.get("gpu_utilization", 0.0))
            total_gpu_memory += float(stat.get("gpu_memory_usage", 0.0))

            memory_info = stat.get("memory_info")
            if memory_info:
                total_rss += float(memory_info.rss) / 1.0e6
                if hasattr(memory_info, "shared"):
                    total_shm += float(memory_info.shared)
            mem_full_info = stat.get("memory_full_info")
            if mem_full_info is not None:
                total_uss += float(mem_full_info.uss) / 1.0e6
            elif memory_info is not None:
                # If memory_full_info is not collected, approximated USS from memory_info
                if hasattr(memory_info, "shared"):
                    # Linux: USS ≈ RSS - shared
                    total_uss += float(memory_info.rss - memory_info.shared) / 1.0e6
                elif hasattr(memory_info, "private"):
                    # Windows: private IS USS
                    total_uss += float(memory_info.private) / 1.0e6
            total_num_fds += int(stat.get("num_fds", 0))

        tags = {"ip": self._ip, "Component": component_name}
        if pid:
            tags["pid"] = pid

        records = []
        records.append(
            Record(
                gauge=METRICS_GAUGES["component_cpu_percentage"],
                value=total_cpu_percentage,
                tags=tags,
            )
        )
        records.append(
            Record(
                gauge=METRICS_GAUGES["component_mem_shared_bytes"],
                value=total_shm,

Review comment (performance):
I don't think either of these works on MacOS going by the documentation.  So that's a downside.

I wonder.  I did a bit more reading on this.  the psutil documentation reccommends making use of oneshot() for getting this information in a way that avoids impacting performance.  Could I get you to try that out in the environment where you've observed the slowdown?  Benefit being, we can just keep all the old behavior and metric understandings, but maybe avoid this perf issue you've described?

Code after:
        total_rss = 0.0
        total_uss = 0.0
        total_shm = 0.0
        total_num_fds = 0
        for stat in stats:
            total_cpu_percentage += float(stat.get("cpu_percent", 0.0))  # noqa

            # Aggregate GPU stats if available
            total_gpu_percentage += float(stat.get("gpu_utilization", 0.0))
            total_gpu_memory += float(stat.get("gpu_memory_usage", 0.0))

            memory_info = stat.get("memory_info")
            if memory_info:
                total_rss += float(memory_info.rss) / 1.0e6
                if hasattr(memory_info, "shared"):
                    total_shm += float(memory_info.shared)
            mem_full_info = stat.get("memory_full_info")
            if mem_full_info is not None:
                # For Mac OS X, directly get USS metric from memory_full_info
                total_uss += float(mem_full_info.uss) / 1.0e6
            elif memory_info is not None:
                # For linux or windows, memory_full_info is not collected. Approximated USS from memory_info
                if hasattr(memory_info, "shared"):
                    # Linux: USS ≈ RSS - shared
                    total_uss += float(memory_info.rss - memory_info.shared) / 1.0e6
                elif hasattr(memory_info, "private"):
                    # Windows: private IS USS
                    total_uss += float(memory_info.private) / 1.0e6
            total_num_fds += int(stat.get("num_fds", 0))

        tags = {"ip": self._ip, "Component": component_name}
        if pid:
            tags["pid"] = pid

        records = []
        records.append(
            Record(
                gauge=METRICS_GAUGES["component_cpu_percentage"],
                value=total_cpu_percentage,
                tags=tags,
            )
        )
        records.append(
            Record(
                gauge=METRICS_GAUGES["component_mem_shared_bytes"],
                value=total_shm,
                tags=tags,
            )
        )
        records.append(
            Record(