22// SPDX-License-Identifier: MIT-0
33package com .amazonaws .services .lambda .extension ;
44
5+ import com .sun .net .httpserver .HttpExchange ;
6+ import com .sun .net .httpserver .HttpHandler ;
7+ import com .sun .net .httpserver .HttpServer ;
58import java .io .File ;
69import java .io .IOException ;
710import java .io .OutputStream ;
811import java .lang .instrument .Instrumentation ;
912import java .net .InetSocketAddress ;
1013import java .nio .charset .StandardCharsets ;
14+ import one .profiler .AsyncProfiler ;
1115
12- import com .sun .net .httpserver .HttpExchange ;
13- import com .sun .net .httpserver .HttpHandler ;
14- import com .sun .net .httpserver .HttpServer ;
16+ import static com .amazonaws .services .lambda .extension .Constants .PROFILER_START_COMMAND ;
17+ import static com .amazonaws .services .lambda .extension .Constants .PROFILER_STOP_COMMAND ;
18+
19+ public class PreMain {
1520
16- import one .profiler .AsyncProfiler ;
1721
18- public class PreMain {
22+ private static final String INTERNAL_COMMUNICATION_PORT =
23+ System .getenv ().getOrDefault (
24+ "AWS_LAMBDA_PROFILER_COMMUNICATION_PORT" ,
25+ "1234"
26+ );
1927
20- private static final String DEFAULT_AWS_LAMBDA_PROFILER_START_COMMAND = "start,event=wall,interval=1us" ;
21- private static final String DEFAULT_AWS_LAMBDA_PROFILER_STOP_COMMAND = "stop,file=%s,include=*AWSLambda.main,include=start_thread" ;
22- private static final String PROFILER_START_COMMAND = System .getenv ().getOrDefault ("AWS_LAMBDA_PROFILER_START_COMMAND" , DEFAULT_AWS_LAMBDA_PROFILER_START_COMMAND );
23- private static final String PROFILER_STOP_COMMAND = System .getenv ().getOrDefault ("AWS_LAMBDA_PROFILER_STOP_COMMAND" , DEFAULT_AWS_LAMBDA_PROFILER_STOP_COMMAND );
24- private static final String INTERNAL_COMMUNICATION_PORT = System .getenv ().getOrDefault ("AWS_LAMBDA_PROFILER_COMMUNICATION_PORT" , "1234" );
28+
29+ private String filepath ;
2530
2631 public static void premain (String agentArgs , Instrumentation inst ) {
2732 Logger .debug ("premain is starting" );
28- if (!createFileIfNotExist ("/tmp/aws-lambda-java-profiler" )) {
33+ if (!createFileIfNotExist ("/tmp/aws-lambda-java-profiler" )) {
2934 Logger .debug ("starting the profiler for coldstart" );
3035 startProfiler ();
3136 registerShutdownHook ();
3237 try {
3338 Integer port = Integer .parseInt (INTERNAL_COMMUNICATION_PORT );
3439 Logger .debug ("using profile communication port = " + port );
35- HttpServer server = HttpServer .create (new InetSocketAddress (port ), 0 );
40+ HttpServer server = HttpServer .create (
41+ new InetSocketAddress (port ),
42+ 0
43+ );
3644 server .createContext ("/profiler/start" , new StartProfiler ());
3745 server .createContext ("/profiler/stop" , new StopProfiler ());
3846 server .setExecutor (null ); // Use the default executor
3947 server .start ();
40- } catch (Exception e ) {
48+ } catch (Exception e ) {
4149 e .printStackTrace ();
4250 }
4351 }
4452 }
4553
4654 private static boolean createFileIfNotExist (String filePath ) {
47- File file = new File (filePath );
55+ File file = new File (filePath );
4856 try {
4957 return file .createNewFile ();
5058 } catch (IOException e ) {
@@ -54,10 +62,13 @@ private static boolean createFileIfNotExist(String filePath) {
5462 }
5563
5664 public static class StopProfiler implements HttpHandler {
65+
5766 @ Override
5867 public void handle (HttpExchange exchange ) throws IOException {
5968 Logger .debug ("hit /profiler/stop" );
60- final String fileName = exchange .getRequestHeaders ().getFirst (ExtensionMain .HEADER_NAME );
69+ final String fileName = exchange
70+ .getRequestHeaders ()
71+ .getFirst (ExtensionMain .HEADER_NAME );
6172 stopProfiler (fileName );
6273 String response = "ok" ;
6374 exchange .sendResponseHeaders (200 , response .length ());
@@ -68,6 +79,7 @@ public void handle(HttpExchange exchange) throws IOException {
6879 }
6980
7081 public static class StartProfiler implements HttpHandler {
82+
7183 @ Override
7284 public void handle (HttpExchange exchange ) throws IOException {
7385 Logger .debug ("hit /profiler/start" );
@@ -80,21 +92,32 @@ public void handle(HttpExchange exchange) throws IOException {
8092 }
8193 }
8294
83-
8495 public static void stopProfiler (String fileNameSuffix ) {
8596 try {
86- final String fileName = String .format ("/tmp/profiling-data-%s.html" , fileNameSuffix );
87- Logger .debug ("stopping the profiler with filename = " + fileName + " with command = " + PROFILER_STOP_COMMAND );
88- AsyncProfiler .getInstance ().execute (String .format (PROFILER_STOP_COMMAND , fileName ));
89- } catch (Exception e ) {
97+ final String fileName = String .format (
98+ Constants .getFilePathFromEnv (),
99+ fileNameSuffix
100+ );
101+ Logger .debug (
102+ "stopping the profiler with filename = " +
103+ fileName +
104+ " with command = " +
105+ PROFILER_STOP_COMMAND
106+ );
107+ AsyncProfiler .getInstance ().execute (
108+ String .format (PROFILER_STOP_COMMAND , fileName )
109+ );
110+ } catch (Exception e ) {
90111 Logger .error ("could not stop the profiler" );
91112 e .printStackTrace ();
92113 }
93114 }
94115
95116 public static void startProfiler () {
96117 try {
97- Logger .debug ("staring the profiler with command = " + PROFILER_START_COMMAND );
118+ Logger .debug (
119+ "staring the profiler with command = " + PROFILER_START_COMMAND
120+ );
98121 AsyncProfiler .getInstance ().execute (PROFILER_START_COMMAND );
99122 } catch (IOException e ) {
100123 throw new RuntimeException (e );
@@ -103,8 +126,9 @@ public static void startProfiler() {
103126
104127 public static void registerShutdownHook () {
105128 Logger .debug ("registering shutdown hook" );
106- Thread shutdownHook = new Thread (new ShutdownHook (PROFILER_STOP_COMMAND ));
129+ Thread shutdownHook = new Thread (
130+ new ShutdownHook (PROFILER_STOP_COMMAND )
131+ );
107132 Runtime .getRuntime ().addShutdownHook (shutdownHook );
108133 }
109-
110- }
134+ }
0 commit comments