test/hotspot/gtest/jfr/test_threadCpuLoad.cpp
author glaubitz
Mon, 09 Sep 2019 15:14:16 +0200
changeset 58050 9fba708740d6
parent 50630 4cb865aa94b5
child 57360 5d043a159d5c
child 58863 c16ac7a2eba4
permissions -rw-r--r--
8230708: Hotspot fails to build on linux-sparc with gcc-9 Reviewed-by: dholmes, phh
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     1
/*
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     2
 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     4
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     7
 * published by the Free Software Foundation.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     8
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    13
 * accompanied this code).
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    14
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    18
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    21
 * questions.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    22
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    23
 */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    24
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    25
#include "precompiled.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    26
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    27
// This test performs mocking of certain JVM functionality. This works by
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    28
// including the source file under test inside an anonymous namespace (which
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    29
// prevents linking conflicts) with the mocked symbols redefined.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    30
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    31
// The include list should mirror the one found in the included source file -
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    32
// with the ones that should pick up the mocks removed. Those should be included
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    33
// later after the mocks have been defined.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    34
50630
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
    35
#include "logging/log.hpp"
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    36
#include "jfr/jfrEvents.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    37
#include "jfr/support/jfrThreadId.hpp"
50630
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
    38
#include "jfr/support/jfrThreadLocal.hpp"
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    39
#include "jfr/utilities/jfrTime.hpp"
50630
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
    40
#include "utilities/globalDefinitions.hpp"
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    41
#include "runtime/os.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    42
#include "runtime/thread.inline.hpp"
50630
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
    43
#include "runtime/threadSMR.inline.hpp"
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    44
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    45
#include "unittest.hpp"
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    46
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    47
namespace {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    48
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    49
  class MockEventThreadCPULoad : public ::EventThreadCPULoad
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    50
  {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    51
  public:
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    52
    float user;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    53
    float system;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    54
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    55
  public:
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    56
    MockEventThreadCPULoad(EventStartTime timing=TIMED) : ::EventThreadCPULoad(timing) {}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    57
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    58
    void set_user(float new_value) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    59
      user = new_value;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    60
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    61
    void set_system(float new_value) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    62
      system = new_value;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    63
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    64
  };
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    65
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    66
  class MockOs : public ::os {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    67
  public:
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    68
    static jlong user_cpu_time;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    69
    static jlong system_cpu_time;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    70
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    71
    static jlong thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    72
      return user_sys_cpu_time ? user_cpu_time + system_cpu_time : user_cpu_time;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    73
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    74
  };
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    75
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    76
  jlong MockOs::user_cpu_time;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    77
  jlong MockOs::system_cpu_time;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    78
50630
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
    79
  class MockJavaThread : public ::JavaThread {
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
    80
  public:
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
    81
    MockJavaThread() : ::JavaThread() {}
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
    82
  };
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
    83
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
    84
  class MockJavaThreadIteratorWithHandle
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
    85
  {
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
    86
  public:
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
    87
    MockJavaThread* next() { return NULL; }
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
    88
    int length() { return 0; }
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
    89
  };
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
    90
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    91
// Reincluding source files in the anonymous namespace unfortunately seems to
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    92
// behave strangely with precompiled headers (only when using gcc though)
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    93
#ifndef DONT_USE_PRECOMPILED_HEADER
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    94
#define DONT_USE_PRECOMPILED_HEADER
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    95
#endif
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    96
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    97
#define os MockOs
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    98
#define EventThreadCPULoad MockEventThreadCPULoad
50630
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
    99
#define JavaThread MockJavaThread
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
   100
#define JavaThreadIteratorWithHandle MockJavaThreadIteratorWithHandle
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   101
50630
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
   102
#include "jfr/periodic/jfrThreadCPULoadEvent.hpp"
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
   103
#include "jfr/periodic/jfrThreadCPULoadEvent.cpp"
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   104
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   105
#undef os
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   106
#undef EventThreadCPULoad
50630
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
   107
#undef JavaThread
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
   108
#undef JavaThreadIteratorWithHandle
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   109
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   110
} // anonymous namespace
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   111
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   112
class JfrTestThreadCPULoadSingle : public ::testing::Test {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   113
protected:
50630
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
   114
  MockJavaThread* thread;
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   115
  JfrThreadLocal* thread_data;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   116
  MockEventThreadCPULoad event;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   117
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   118
  void SetUp() {
50630
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
   119
    thread = new MockJavaThread();
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   120
    thread_data = thread->jfr_thread_local();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   121
    thread_data->set_wallclock_time(0);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   122
    thread_data->set_user_time(0);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   123
    thread_data->set_cpu_time(0);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   124
  }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   125
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   126
  void TearDown() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   127
    delete thread;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   128
  }
50630
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
   129
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
   130
  // Fix for gcc compilation warning about unused functions
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
   131
  bool TouchUnused() {
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
   132
    return (&JfrThreadCPULoadEvent::send_events &&
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
   133
            &JfrThreadCPULoadEvent::send_event_for_thread);
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
   134
  }
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   135
};
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   136
50630
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
   137
TEST_VM_F(JfrTestThreadCPULoadSingle, SingleCpu) {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   138
  MockOs::user_cpu_time = 100 * NANOSECS_PER_MILLISEC;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   139
  MockOs::system_cpu_time = 100 * NANOSECS_PER_MILLISEC;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   140
  EXPECT_TRUE(JfrThreadCPULoadEvent::update_event(event, thread, 400 * NANOSECS_PER_MILLISEC, 1));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   141
  EXPECT_FLOAT_EQ(0.25, event.user);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   142
  EXPECT_FLOAT_EQ(0.25, event.system);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   143
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   144
50630
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
   145
TEST_VM_F(JfrTestThreadCPULoadSingle, MultipleCpus) {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   146
  MockOs::user_cpu_time = 100 * NANOSECS_PER_MILLISEC;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   147
  MockOs::system_cpu_time = 100 * NANOSECS_PER_MILLISEC;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   148
  EXPECT_TRUE(JfrThreadCPULoadEvent::update_event(event, thread, 400 * NANOSECS_PER_MILLISEC, 2));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   149
  EXPECT_FLOAT_EQ(0.125, event.user);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   150
  EXPECT_FLOAT_EQ(0.125, event.system);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   151
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   152
50630
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
   153
TEST_VM_F(JfrTestThreadCPULoadSingle, BelowThreshold) {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   154
  MockOs::user_cpu_time = 100;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   155
  MockOs::system_cpu_time = 100;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   156
  EXPECT_FALSE(JfrThreadCPULoadEvent::update_event(event, thread, 400 * NANOSECS_PER_MILLISEC, 2));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   157
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   158
50630
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
   159
TEST_VM_F(JfrTestThreadCPULoadSingle, UserAboveMaximum) {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   160
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   161
  // First call will not report above 100%
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   162
  MockOs::user_cpu_time = 200 * NANOSECS_PER_MILLISEC;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   163
  MockOs::system_cpu_time = 100 * NANOSECS_PER_MILLISEC;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   164
  EXPECT_TRUE(JfrThreadCPULoadEvent::update_event(event, thread, 200 * NANOSECS_PER_MILLISEC, 1));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   165
  EXPECT_FLOAT_EQ(0.5, event.user);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   166
  EXPECT_FLOAT_EQ(0.5, event.system);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   167
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   168
  // Second call will see an extra 100 millisecs user time from the remainder
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   169
  EXPECT_TRUE(JfrThreadCPULoadEvent::update_event(event, thread, (200 + 400) * NANOSECS_PER_MILLISEC, 1));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   170
  EXPECT_FLOAT_EQ(0.25, event.user);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   171
  EXPECT_FLOAT_EQ(0, event.system);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   172
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   173
50630
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
   174
TEST_VM_F(JfrTestThreadCPULoadSingle, SystemAboveMaximum) {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   175
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   176
  // First call will not report above 100%
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   177
  MockOs::user_cpu_time = 100 * NANOSECS_PER_MILLISEC;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   178
  MockOs::system_cpu_time = 300 * NANOSECS_PER_MILLISEC;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   179
  EXPECT_TRUE(JfrThreadCPULoadEvent::update_event(event, thread, 200 * NANOSECS_PER_MILLISEC, 1));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   180
  EXPECT_FLOAT_EQ(0, event.user);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   181
  EXPECT_FLOAT_EQ(1, event.system);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   182
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   183
  // Second call will see an extra 100 millisecs user and system time from the remainder
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   184
  EXPECT_TRUE(JfrThreadCPULoadEvent::update_event(event, thread, (200 + 400) * NANOSECS_PER_MILLISEC, 1));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   185
  EXPECT_FLOAT_EQ(0.25, event.user);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   186
  EXPECT_FLOAT_EQ(0.25, event.system);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   187
}
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   188
50630
4cb865aa94b5 8204236: JFR unit test of thread CPU load should be enabled
rwestberg
parents: 50113
diff changeset
   189
TEST_VM_F(JfrTestThreadCPULoadSingle, SystemTimeDecreasing) {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   190
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   191
  // As seen in an actual run - caused by different resolution for total and user time
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   192
  // Total time    User time    (Calculated system time)
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   193
  //       200          100         100
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   194
  //       210          200          10
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   195
  //       400          300         100
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   196
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   197
  MockOs::user_cpu_time = 100 * NANOSECS_PER_MILLISEC;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   198
  MockOs::system_cpu_time = 100 * NANOSECS_PER_MILLISEC;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   199
  EXPECT_TRUE(JfrThreadCPULoadEvent::update_event(event, thread, 400 * NANOSECS_PER_MILLISEC, 1));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   200
  EXPECT_FLOAT_EQ(0.25, event.user);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   201
  EXPECT_FLOAT_EQ(0.25, event.system);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   202
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   203
  MockOs::user_cpu_time += 100 * NANOSECS_PER_MILLISEC;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   204
  MockOs::system_cpu_time -= 90 * NANOSECS_PER_MILLISEC;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   205
  EXPECT_TRUE(JfrThreadCPULoadEvent::update_event(event, thread, (400 + 400) * NANOSECS_PER_MILLISEC, 1));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   206
  EXPECT_FLOAT_EQ(0.25, event.user);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   207
  EXPECT_FLOAT_EQ(0, event.system);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   208
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   209
  MockOs::user_cpu_time += 100 * NANOSECS_PER_MILLISEC;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   210
  MockOs::system_cpu_time += 90 * NANOSECS_PER_MILLISEC;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   211
  EXPECT_TRUE(JfrThreadCPULoadEvent::update_event(event, thread, (400 + 400 + 400) * NANOSECS_PER_MILLISEC, 1));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   212
  EXPECT_FLOAT_EQ(0.25, event.user);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   213
  EXPECT_FLOAT_EQ(0, event.system);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   214
}