You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by xi...@apache.org on 2025/02/07 23:45:03 UTC

(spark) branch branch-4.0 updated: [SPARK-51087][PYTHON][CONNECT] Raise a warning when memory-profiler is not installed for memory profiling

This is an automated email from the ASF dual-hosted git repository.

xinrong pushed a commit to branch branch-4.0
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/branch-4.0 by this push:
     new bbf0d7a3ebad [SPARK-51087][PYTHON][CONNECT] Raise a warning when memory-profiler is not installed for memory profiling
bbf0d7a3ebad is described below

commit bbf0d7a3ebad73d417be70f2c18d6e18350992f7
Author: Xinrong Meng <xi...@apache.org>
AuthorDate: Fri Feb 7 15:44:39 2025 -0800

    [SPARK-51087][PYTHON][CONNECT] Raise a warning when memory-profiler is not installed for memory profiling
    
    ### What changes were proposed in this pull request?
    Raise a warning when memory-profiler is not installed for memory profiling.
    
    ### Why are the changes needed?
    Better usability of PySpark UDF memory profiling.
    
    ### Does this PR introduce _any_ user-facing change?
    No.
    
    ### How was this patch tested?
    Existing and manual tests as shown below:
    ```py
    >>> import memory_profiler  # when memory-profiler is not installed
    Traceback (most recent call last):
    ...
    ModuleNotFoundError: No module named 'memory_profiler'
    
    >>> from pyspark.sql.functions import pandas_udf
    >>>
    >>> df = spark.range(10)
    >>> pandas_udf("long")
    ... def add1(x):
    ...   return x + 1
    ...
    >>> added = df.select(add1("id"))
    >>>
    >>> spark.conf.set("spark.sql.pyspark.udf.profiler", "memory")
    >>> added.show()
    +--------+
    |add1(id)|
    +--------+
    |       1|
    |       2|
    |       3|
    |       4|
    |       5|
    |       6|
    |       7|
    |       8|
    |       9|
    |      10|
    +--------+
    >>> spark.profile.show(type="memory")
    /Users/xinrong.meng/spark/python/pyspark/sql/profiler.py:141: UserWarning: Install the 'memory_profiler' library in the cluster to enable memory profiling
    ...
    >>> spark.profile.dump(path='...', type="memory")
    /Users/xinrong.meng/spark/python/pyspark/sql/profiler.py:225: UserWarning: Install the 'memory_profiler' library in the cluster to enable memory profiling
    ...
    ```
    
    ### Was this patch authored or co-authored using generative AI tooling?
    No.
    
    Closes #49797 from xinrong-meng/memory_profiler_install.
    
    Authored-by: Xinrong Meng <xi...@apache.org>
    Signed-off-by: Xinrong Meng <xi...@apache.org>
    (cherry picked from commit df89c8ed40b0a51b4c15029b98fc00e8d4232874)
    Signed-off-by: Xinrong Meng <xi...@apache.org>
---
 python/pyspark/sql/profiler.py | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/python/pyspark/sql/profiler.py b/python/pyspark/sql/profiler.py
index 6924cde9a292..1e1be6ab79f9 100644
--- a/python/pyspark/sql/profiler.py
+++ b/python/pyspark/sql/profiler.py
@@ -20,6 +20,7 @@ import os
 import pstats
 from threading import RLock
 from typing import Any, Callable, Dict, Literal, Optional, Tuple, Union, TYPE_CHECKING, overload
+import warnings
 
 from pyspark.accumulators import (
     Accumulator,
@@ -28,7 +29,13 @@ from pyspark.accumulators import (
     _accumulatorRegistry,
 )
 from pyspark.errors import PySparkValueError
-from pyspark.profiler import CodeMapDict, MemoryProfiler, MemUsageParam, PStatsParam
+from pyspark.profiler import (
+    CodeMapDict,
+    MemoryProfiler,
+    MemUsageParam,
+    PStatsParam,
+    has_memory_profiler,
+)
 
 if TYPE_CHECKING:
     from pyspark.sql._typing import ProfileResults
@@ -130,6 +137,12 @@ class ProfilerCollector(ABC):
         with self._lock:
             code_map = self._memory_profile_results
 
+        if not has_memory_profiler and not code_map:
+            warnings.warn(
+                "Install the 'memory_profiler' library in the cluster to enable memory profiling",
+                UserWarning,
+            )
+
         def show(id: int) -> None:
             cm = code_map.get(id)
             if cm is not None:
@@ -208,6 +221,12 @@ class ProfilerCollector(ABC):
         with self._lock:
             code_map = self._memory_profile_results
 
+        if not has_memory_profiler and not code_map:
+            warnings.warn(
+                "Install the 'memory_profiler' library in the cluster to enable memory profiling",
+                UserWarning,
+            )
+
         def dump(id: int) -> None:
             cm = code_map.get(id)
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org