test/hotspot/gtest/jfr/test_threadCpuLoad.cpp
changeset 50630 4cb865aa94b5
parent 50113 caf115bb98ad
child 57360 5d043a159d5c
child 58863 c16ac7a2eba4
equal deleted inserted replaced
50629:645a5962201d 50630:4cb865aa94b5
    30 
    30 
    31 // The include list should mirror the one found in the included source file -
    31 // The include list should mirror the one found in the included source file -
    32 // with the ones that should pick up the mocks removed. Those should be included
    32 // with the ones that should pick up the mocks removed. Those should be included
    33 // later after the mocks have been defined.
    33 // later after the mocks have been defined.
    34 
    34 
    35 #include "jvm.h"
    35 #include "logging/log.hpp"
    36 #include "classfile/classLoaderStats.hpp"
       
    37 #include "classfile/javaClasses.hpp"
       
    38 #include "code/codeCache.hpp"
       
    39 #include "compiler/compileBroker.hpp"
       
    40 #include "gc/g1/g1HeapRegionEventSender.hpp"
       
    41 #include "gc/shared/gcConfiguration.hpp"
       
    42 #include "gc/shared/gcTrace.hpp"
       
    43 #include "gc/shared/objectCountEventSender.hpp"
       
    44 #include "gc/shared/vmGCOperations.hpp"
       
    45 #include "jfr/jfrEvents.hpp"
    36 #include "jfr/jfrEvents.hpp"
    46 #include "jfr/periodic/jfrModuleEvent.hpp"
       
    47 #include "jfr/periodic/jfrOSInterface.hpp"
       
    48 #include "jfr/periodic/jfrThreadCPULoadEvent.hpp"
       
    49 #include "jfr/periodic/jfrThreadDumpEvent.hpp"
       
    50 #include "jfr/recorder/jfrRecorder.hpp"
       
    51 #include "jfr/support/jfrThreadId.hpp"
    37 #include "jfr/support/jfrThreadId.hpp"
       
    38 #include "jfr/support/jfrThreadLocal.hpp"
    52 #include "jfr/utilities/jfrTime.hpp"
    39 #include "jfr/utilities/jfrTime.hpp"
    53 #include "logging/log.hpp"
    40 #include "utilities/globalDefinitions.hpp"
    54 #include "memory/heapInspection.hpp"
       
    55 #include "memory/resourceArea.hpp"
       
    56 #include "oops/oop.inline.hpp"
       
    57 #include "runtime/arguments.hpp"
       
    58 #include "runtime/flags/jvmFlag.hpp"
       
    59 #include "runtime/globals.hpp"
       
    60 #include "runtime/os.hpp"
    41 #include "runtime/os.hpp"
    61 #include "runtime/os_perf.hpp"
       
    62 #include "runtime/thread.inline.hpp"
    42 #include "runtime/thread.inline.hpp"
    63 #include "runtime/threadSMR.hpp"
    43 #include "runtime/threadSMR.inline.hpp"
    64 #include "runtime/sweeper.hpp"
       
    65 #include "runtime/vmThread.hpp"
       
    66 #include "services/classLoadingService.hpp"
       
    67 #include "services/management.hpp"
       
    68 #include "services/threadService.hpp"
       
    69 #include "utilities/exceptions.hpp"
       
    70 #include "utilities/globalDefinitions.hpp"
       
    71 
    44 
    72 #include "unittest.hpp"
    45 #include "unittest.hpp"
    73 
    46 
    74 namespace {
    47 namespace {
    75 
    48 
   101   };
    74   };
   102 
    75 
   103   jlong MockOs::user_cpu_time;
    76   jlong MockOs::user_cpu_time;
   104   jlong MockOs::system_cpu_time;
    77   jlong MockOs::system_cpu_time;
   105 
    78 
       
    79   class MockJavaThread : public ::JavaThread {
       
    80   public:
       
    81     MockJavaThread() : ::JavaThread() {}
       
    82   };
       
    83 
       
    84   class MockJavaThreadIteratorWithHandle
       
    85   {
       
    86   public:
       
    87     MockJavaThread* next() { return NULL; }
       
    88     int length() { return 0; }
       
    89   };
       
    90 
   106 // Reincluding source files in the anonymous namespace unfortunately seems to
    91 // Reincluding source files in the anonymous namespace unfortunately seems to
   107 // behave strangely with precompiled headers (only when using gcc though)
    92 // behave strangely with precompiled headers (only when using gcc though)
   108 #ifndef DONT_USE_PRECOMPILED_HEADER
    93 #ifndef DONT_USE_PRECOMPILED_HEADER
   109 #define DONT_USE_PRECOMPILED_HEADER
    94 #define DONT_USE_PRECOMPILED_HEADER
   110 #endif
    95 #endif
   111 
    96 
   112 #define os MockOs
    97 #define os MockOs
   113 #define EventThreadCPULoad MockEventThreadCPULoad
    98 #define EventThreadCPULoad MockEventThreadCPULoad
   114 
    99 #define JavaThread MockJavaThread
   115 #include "jfrfiles/jfrPeriodic.hpp"
   100 #define JavaThreadIteratorWithHandle MockJavaThreadIteratorWithHandle
   116 #include "jfr/periodic/jfrPeriodic.cpp"
   101 
       
   102 #include "jfr/periodic/jfrThreadCPULoadEvent.hpp"
       
   103 #include "jfr/periodic/jfrThreadCPULoadEvent.cpp"
   117 
   104 
   118 #undef os
   105 #undef os
   119 #undef EventThreadCPULoad
   106 #undef EventThreadCPULoad
       
   107 #undef JavaThread
       
   108 #undef JavaThreadIteratorWithHandle
   120 
   109 
   121 } // anonymous namespace
   110 } // anonymous namespace
   122 
   111 
   123 class JfrTestThreadCPULoadSingle : public ::testing::Test {
   112 class JfrTestThreadCPULoadSingle : public ::testing::Test {
   124 protected:
   113 protected:
   125   JavaThread* thread;
   114   MockJavaThread* thread;
   126   JfrThreadLocal* thread_data;
   115   JfrThreadLocal* thread_data;
   127   MockEventThreadCPULoad event;
   116   MockEventThreadCPULoad event;
   128 
   117 
   129   void SetUp() {
   118   void SetUp() {
   130     thread = new JavaThread();
   119     thread = new MockJavaThread();
   131     thread_data = thread->jfr_thread_local();
   120     thread_data = thread->jfr_thread_local();
   132     thread_data->set_wallclock_time(0);
   121     thread_data->set_wallclock_time(0);
   133     thread_data->set_user_time(0);
   122     thread_data->set_user_time(0);
   134     thread_data->set_cpu_time(0);
   123     thread_data->set_cpu_time(0);
   135   }
   124   }
   136 
   125 
   137   void TearDown() {
   126   void TearDown() {
   138     delete thread;
   127     delete thread;
   139   }
   128   }
       
   129 
       
   130   // Fix for gcc compilation warning about unused functions
       
   131   bool TouchUnused() {
       
   132     return (&JfrThreadCPULoadEvent::send_events &&
       
   133             &JfrThreadCPULoadEvent::send_event_for_thread);
       
   134   }
   140 };
   135 };
   141 
   136 
   142 TEST_VM_F(JfrTestThreadCPULoadSingle, DISABLED_SingleCpu) {
   137 TEST_VM_F(JfrTestThreadCPULoadSingle, SingleCpu) {
   143   MockOs::user_cpu_time = 100 * NANOSECS_PER_MILLISEC;
   138   MockOs::user_cpu_time = 100 * NANOSECS_PER_MILLISEC;
   144   MockOs::system_cpu_time = 100 * NANOSECS_PER_MILLISEC;
   139   MockOs::system_cpu_time = 100 * NANOSECS_PER_MILLISEC;
   145   EXPECT_TRUE(JfrThreadCPULoadEvent::update_event(event, thread, 400 * NANOSECS_PER_MILLISEC, 1));
   140   EXPECT_TRUE(JfrThreadCPULoadEvent::update_event(event, thread, 400 * NANOSECS_PER_MILLISEC, 1));
   146   EXPECT_FLOAT_EQ(0.25, event.user);
   141   EXPECT_FLOAT_EQ(0.25, event.user);
   147   EXPECT_FLOAT_EQ(0.25, event.system);
   142   EXPECT_FLOAT_EQ(0.25, event.system);
   148 }
   143 }
   149 
   144 
   150 TEST_VM_F(JfrTestThreadCPULoadSingle, DISABLED_MultipleCpus) {
   145 TEST_VM_F(JfrTestThreadCPULoadSingle, MultipleCpus) {
   151   MockOs::user_cpu_time = 100 * NANOSECS_PER_MILLISEC;
   146   MockOs::user_cpu_time = 100 * NANOSECS_PER_MILLISEC;
   152   MockOs::system_cpu_time = 100 * NANOSECS_PER_MILLISEC;
   147   MockOs::system_cpu_time = 100 * NANOSECS_PER_MILLISEC;
   153   EXPECT_TRUE(JfrThreadCPULoadEvent::update_event(event, thread, 400 * NANOSECS_PER_MILLISEC, 2));
   148   EXPECT_TRUE(JfrThreadCPULoadEvent::update_event(event, thread, 400 * NANOSECS_PER_MILLISEC, 2));
   154   EXPECT_FLOAT_EQ(0.125, event.user);
   149   EXPECT_FLOAT_EQ(0.125, event.user);
   155   EXPECT_FLOAT_EQ(0.125, event.system);
   150   EXPECT_FLOAT_EQ(0.125, event.system);
   156 }
   151 }
   157 
   152 
   158 TEST_VM_F(JfrTestThreadCPULoadSingle, DISABLED_BelowThreshold) {
   153 TEST_VM_F(JfrTestThreadCPULoadSingle, BelowThreshold) {
   159   MockOs::user_cpu_time = 100;
   154   MockOs::user_cpu_time = 100;
   160   MockOs::system_cpu_time = 100;
   155   MockOs::system_cpu_time = 100;
   161   EXPECT_FALSE(JfrThreadCPULoadEvent::update_event(event, thread, 400 * NANOSECS_PER_MILLISEC, 2));
   156   EXPECT_FALSE(JfrThreadCPULoadEvent::update_event(event, thread, 400 * NANOSECS_PER_MILLISEC, 2));
   162 }
   157 }
   163 
   158 
   164 TEST_VM_F(JfrTestThreadCPULoadSingle, DISABLED_UserAboveMaximum) {
   159 TEST_VM_F(JfrTestThreadCPULoadSingle, UserAboveMaximum) {
   165 
   160 
   166   // First call will not report above 100%
   161   // First call will not report above 100%
   167   MockOs::user_cpu_time = 200 * NANOSECS_PER_MILLISEC;
   162   MockOs::user_cpu_time = 200 * NANOSECS_PER_MILLISEC;
   168   MockOs::system_cpu_time = 100 * NANOSECS_PER_MILLISEC;
   163   MockOs::system_cpu_time = 100 * NANOSECS_PER_MILLISEC;
   169   EXPECT_TRUE(JfrThreadCPULoadEvent::update_event(event, thread, 200 * NANOSECS_PER_MILLISEC, 1));
   164   EXPECT_TRUE(JfrThreadCPULoadEvent::update_event(event, thread, 200 * NANOSECS_PER_MILLISEC, 1));
   174   EXPECT_TRUE(JfrThreadCPULoadEvent::update_event(event, thread, (200 + 400) * NANOSECS_PER_MILLISEC, 1));
   169   EXPECT_TRUE(JfrThreadCPULoadEvent::update_event(event, thread, (200 + 400) * NANOSECS_PER_MILLISEC, 1));
   175   EXPECT_FLOAT_EQ(0.25, event.user);
   170   EXPECT_FLOAT_EQ(0.25, event.user);
   176   EXPECT_FLOAT_EQ(0, event.system);
   171   EXPECT_FLOAT_EQ(0, event.system);
   177 }
   172 }
   178 
   173 
   179 TEST_VM_F(JfrTestThreadCPULoadSingle, DISABLED_SystemAboveMaximum) {
   174 TEST_VM_F(JfrTestThreadCPULoadSingle, SystemAboveMaximum) {
   180 
   175 
   181   // First call will not report above 100%
   176   // First call will not report above 100%
   182   MockOs::user_cpu_time = 100 * NANOSECS_PER_MILLISEC;
   177   MockOs::user_cpu_time = 100 * NANOSECS_PER_MILLISEC;
   183   MockOs::system_cpu_time = 300 * NANOSECS_PER_MILLISEC;
   178   MockOs::system_cpu_time = 300 * NANOSECS_PER_MILLISEC;
   184   EXPECT_TRUE(JfrThreadCPULoadEvent::update_event(event, thread, 200 * NANOSECS_PER_MILLISEC, 1));
   179   EXPECT_TRUE(JfrThreadCPULoadEvent::update_event(event, thread, 200 * NANOSECS_PER_MILLISEC, 1));
   189   EXPECT_TRUE(JfrThreadCPULoadEvent::update_event(event, thread, (200 + 400) * NANOSECS_PER_MILLISEC, 1));
   184   EXPECT_TRUE(JfrThreadCPULoadEvent::update_event(event, thread, (200 + 400) * NANOSECS_PER_MILLISEC, 1));
   190   EXPECT_FLOAT_EQ(0.25, event.user);
   185   EXPECT_FLOAT_EQ(0.25, event.user);
   191   EXPECT_FLOAT_EQ(0.25, event.system);
   186   EXPECT_FLOAT_EQ(0.25, event.system);
   192 }
   187 }
   193 
   188 
   194 TEST_VM_F(JfrTestThreadCPULoadSingle, DISABLED_SystemTimeDecreasing) {
   189 TEST_VM_F(JfrTestThreadCPULoadSingle, SystemTimeDecreasing) {
   195 
   190 
   196   // As seen in an actual run - caused by different resolution for total and user time
   191   // As seen in an actual run - caused by different resolution for total and user time
   197   // Total time    User time    (Calculated system time)
   192   // Total time    User time    (Calculated system time)
   198   //       200          100         100
   193   //       200          100         100
   199   //       210          200          10
   194   //       210          200          10