1/*
2 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25#include "precompiled.hpp"
26#include "classfile/classLoader.hpp"
27#include "logging/log.hpp"
28#include "runtime/vm_version.hpp"
29#include "services/attachListener.hpp"
30#include "services/management.hpp"
31#include "services/runtimeService.hpp"
32#include "utilities/dtrace.hpp"
33#include "utilities/exceptions.hpp"
34#include "utilities/macros.hpp"
35
36#if INCLUDE_MANAGEMENT
37TimeStamp RuntimeService::_app_timer;
38TimeStamp RuntimeService::_safepoint_timer;
39PerfCounter*  RuntimeService::_sync_time_ticks = NULL;
40PerfCounter*  RuntimeService::_total_safepoints = NULL;
41PerfCounter*  RuntimeService::_safepoint_time_ticks = NULL;
42PerfCounter*  RuntimeService::_application_time_ticks = NULL;
43double RuntimeService::_last_safepoint_sync_time_sec = 0.0;
44
45void RuntimeService::init() {
46
47  if (UsePerfData) {
48    EXCEPTION_MARK;
49
50    _sync_time_ticks =
51              PerfDataManager::create_counter(SUN_RT, "safepointSyncTime",
52                                              PerfData::U_Ticks, CHECK);
53
54    _total_safepoints =
55              PerfDataManager::create_counter(SUN_RT, "safepoints",
56                                              PerfData::U_Events, CHECK);
57
58    _safepoint_time_ticks =
59              PerfDataManager::create_counter(SUN_RT, "safepointTime",
60                                              PerfData::U_Ticks, CHECK);
61
62    _application_time_ticks =
63              PerfDataManager::create_counter(SUN_RT, "applicationTime",
64                                              PerfData::U_Ticks, CHECK);
65
66
67    // create performance counters for jvm_version and its capabilities
68    PerfDataManager::create_constant(SUN_RT, "jvmVersion", PerfData::U_None,
69                                     (jlong) Abstract_VM_Version::jvm_version(), CHECK);
70
71    // The capabilities counter is a binary representation of the VM capabilities in string.
72    // This string respresentation simplifies the implementation of the client side
73    // to parse the value.
74    char capabilities[65];
75    size_t len = sizeof(capabilities);
76    memset((void*) capabilities, '0', len);
77    capabilities[len-1] = '\0';
78    capabilities[0] = AttachListener::is_attach_supported() ? '1' : '0';
79#if INCLUDE_SERVICES
80    capabilities[1] = '1';
81#endif // INCLUDE_SERVICES
82    PerfDataManager::create_string_constant(SUN_RT, "jvmCapabilities",
83                                            capabilities, CHECK);
84  }
85}
86
87void RuntimeService::record_safepoint_begin() {
88  HS_PRIVATE_SAFEPOINT_BEGIN();
89
90  // Print the time interval in which the app was executing
91  if (_app_timer.is_updated()) {
92    log_info(safepoint)("Application time: %3.7f seconds", last_application_time_sec());
93  }
94
95  // update the time stamp to begin recording safepoint time
96  _safepoint_timer.update();
97  _last_safepoint_sync_time_sec = 0.0;
98  if (UsePerfData) {
99    _total_safepoints->inc();
100    if (_app_timer.is_updated()) {
101      _application_time_ticks->inc(_app_timer.ticks_since_update());
102    }
103  }
104}
105
106void RuntimeService::record_safepoint_synchronized() {
107  if (UsePerfData) {
108    _sync_time_ticks->inc(_safepoint_timer.ticks_since_update());
109  }
110  if (log_is_enabled(Info, safepoint)) {
111    _last_safepoint_sync_time_sec = last_safepoint_time_sec();
112  }
113}
114
115void RuntimeService::record_safepoint_end() {
116  HS_PRIVATE_SAFEPOINT_END();
117
118  // Print the time interval for which the app was stopped
119  // during the current safepoint operation.
120  log_info(safepoint)("Total time for which application threads were stopped: %3.7f seconds, Stopping threads took: %3.7f seconds",
121                      last_safepoint_time_sec(), _last_safepoint_sync_time_sec);
122
123  // update the time stamp to begin recording app time
124  _app_timer.update();
125  if (UsePerfData) {
126    _safepoint_time_ticks->inc(_safepoint_timer.ticks_since_update());
127  }
128}
129
130void RuntimeService::record_application_start() {
131  // update the time stamp to begin recording app time
132  _app_timer.update();
133}
134
135// Don't need to record application end because we currently
136// exit at a safepoint and record_safepoint_begin() handles updating
137// the application time counter at VM exit.
138
139jlong RuntimeService::safepoint_sync_time_ms() {
140  return UsePerfData ?
141    Management::ticks_to_ms(_sync_time_ticks->get_value()) : -1;
142}
143
144jlong RuntimeService::safepoint_count() {
145  return UsePerfData ?
146    _total_safepoints->get_value() : -1;
147}
148jlong RuntimeService::safepoint_time_ms() {
149  return UsePerfData ?
150    Management::ticks_to_ms(_safepoint_time_ticks->get_value()) : -1;
151}
152
153jlong RuntimeService::application_time_ms() {
154  return UsePerfData ?
155    Management::ticks_to_ms(_application_time_ticks->get_value()) : -1;
156}
157
158#endif // INCLUDE_MANAGEMENT
159