Skip to content

Instantly share code, notes, and snippets.

@parttimenerd
Last active November 15, 2024 13:32
Show Gist options
  • Save parttimenerd/ae850d005c6961f2c8cb31d5b233d71f to your computer and use it in GitHub Desktop.
Save parttimenerd/ae850d005c6961f2c8cb31d5b233d71f to your computer and use it in GitHub Desktop.
Code comprehension task

If any issues arise, please don't hesitate to ask.

Setup

Before diving into the code, please clone the OpenJDK JDK repository on a machine running Linux and build it according to the build instructions.

This should give you a running OpenJDK. Add it to your path and run a simple Java "Hello, World" program to confirm everything is set up correctly.

Testing

Refer to the testing documentation for guidance. Download a pre-built JTReg JAR from this link:

wget https://builds.shipilev.net/jtreg/jtreg-7.4+1.zip
unzip jtreg-7.4+1.zip

Configure the tests:

bash configure --with-jtreg=jtreg --with-debug-level=fastdebug

This should allow you to run tests, such as the TestActiveSettingEvent test:

make test TEST=jtreg:test/jdk/jdk/jfr/event/runtime/TestActiveSettingEvent.java

Warm-Up Task

Try to get a rough understanding of what this test case does.

Task 1

Our team is currently developing a new CPU time profiler for OpenJDK. The work-in-progress code can be found here.

The main files you’ll need for your task are:

These files contain the new CPU time sampler implementation, which should be understandable without extensive OpenJDK knowledge. To get started, here’s an annotated version of the header interface:

class JfrCPUTimeThreadSampling : public JfrCHeapObj {
  friend class JfrRecorder;
  
  /* ... */
  
 public:
 
  // Called externally to set the sampling rate
  // rate == 0 stops the sampling
  static void set_rate(double rate, bool autoadapt);
  
  // Called whenever a Java thread is created
  static void on_javathread_create(JavaThread* thread);
  
  // Called whenever a Java thread is terminated
  static void on_javathread_terminate(JavaThread* thread);
  
  // Called in the signal handler for CPU time timer events
  void handle_timer_signal(void* context);

  // Prevents the queue from being processed,
  // for specific test cases only, so it can be ignored
  static void set_process_queue(bool process_queue);
};

Steps:

  1. Clone this branch and attempt to build it.
  2. Run the sampler, following the information here.
  3. Explore the relationship between all methods in the JfrCPUTimeThreadSampling class
  4. Try to understand how the sampler works. Feel free to use printf debugging or a debugger (e.g., vsreg). You can ignore the details of the stack walking.

Bug Task

A bug exists in this pull request. The TestActiveSettingEvent test case (which doesn't involve the new sampler) fails, causing the JVM to become stuck in the disenroll method indefinitely.

Do you have any ideas why? Feel free to use (printf) debugging. A hint: the fix is quite simple and requires only one line of code to be changed in jfrCPUTimeThreadSampler.cpp, but it may not be entirely obvious.


License

CC-BY

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment