3232import java .net .URL ;
3333import java .nio .file .Files ;
3434import java .nio .file .Path ;
35+ import java .util .Arrays ;
36+ import java .util .List ;
3537import java .util .Locale ;
3638import java .util .Objects ;
39+ import java .util .Set ;
3740import java .util .logging .Level ;
41+ import java .util .stream .Collectors ;
3842
3943/**
4044 * Provides a bridge between spark and async-profiler.
@@ -70,12 +74,20 @@ public static synchronized AsyncProfilerAccess getInstance(SparkPlatform platfor
7074
7175 try {
7276 profiler = load (platform );
73- if (isEventSupported (profiler , ProfilingEvent .ALLOC , false )) {
77+ Set <ProfilingEvent > supportedEvents = getSupportedEvents (profiler );
78+
79+ // allocation profiler
80+ if (supportedEvents .contains (ProfilingEvent .ALLOC )) {
7481 allocationProfilingEvent = ProfilingEvent .ALLOC ;
7582 }
76- if (isEventSupported (profiler , ProfilingEvent .WALL , true )) {
83+
84+ // normal profiler
85+ if (supportedEvents .contains (ProfilingEvent .WALL )) {
7786 profilingEvent = ProfilingEvent .WALL ;
87+ } else {
88+ throw new IllegalStateException ("'wall' event is not supported" );
7889 }
90+
7991 } catch (Exception e ) {
8092 profiler = null ;
8193 setupException = e ;
@@ -179,25 +191,27 @@ private static AsyncProfiler load(SparkPlatform platform) throws Exception {
179191 }
180192
181193 /**
182- * Checks the {@code profiler} to ensure the CPU event is supported .
194+ * Gets the set of supported events from the profiler .
183195 *
184196 * @param profiler the profiler instance
185- * @return if the event is supported
197+ * @return the set of supported events
186198 */
187- private static boolean isEventSupported (AsyncProfiler profiler , ProfilingEvent event , boolean throwException ) {
199+ private static Set <ProfilingEvent > getSupportedEvents (AsyncProfiler profiler ) {
200+ String resp ;
188201 try {
189- String resp = profiler .execute ("check,event=" + event ).trim ();
190- if (resp .equalsIgnoreCase ("ok" )) {
191- return true ;
192- } else if (throwException ) {
193- throw new IllegalArgumentException (resp );
194- }
202+ resp = profiler .execute ("list" );
195203 } catch (Exception e ) {
196- if (throwException ) {
197- throw new RuntimeException ("Event " + event + " is not supported" , e );
198- }
204+ throw new RuntimeException ("Unable to list supported events" , e );
199205 }
200- return false ;
206+
207+ List <String > respLines = Arrays .stream (resp .split ("\\ r?\\ n" ))
208+ .filter (line -> line .startsWith (" " ))
209+ .map (String ::trim )
210+ .collect (Collectors .toList ());
211+
212+ return Arrays .stream (ProfilingEvent .values ())
213+ .filter (event -> respLines .contains (event .getId ()))
214+ .collect (Collectors .toSet ());
201215 }
202216
203217 public enum ProfilingEvent {
@@ -210,6 +224,10 @@ public enum ProfilingEvent {
210224 this .id = id ;
211225 }
212226
227+ public String getId () {
228+ return this .id ;
229+ }
230+
213231 @ Override
214232 public String toString () {
215233 return this .id ;
0 commit comments