runtimeService.cpp revision 6134:9ba2269eafd4
1/*
2 * Copyright (c) 2003, 2014, 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 "services/attachListener.hpp"
28#include "services/management.hpp"
29#include "services/runtimeService.hpp"
30#include "utilities/dtrace.hpp"
31#include "utilities/exceptions.hpp"
32#include "utilities/macros.hpp"
33
34#if INCLUDE_MANAGEMENT
35TimeStamp RuntimeService::_app_timer;
36TimeStamp RuntimeService::_safepoint_timer;
37PerfCounter*  RuntimeService::_sync_time_ticks = NULL;
38PerfCounter*  RuntimeService::_total_safepoints = NULL;
39PerfCounter*  RuntimeService::_safepoint_time_ticks = NULL;
40PerfCounter*  RuntimeService::_application_time_ticks = NULL;
41
42void RuntimeService::init() {
43  // Make sure the VM version is initialized
44  Abstract_VM_Version::initialize();
45
46  if (UsePerfData) {
47    EXCEPTION_MARK;
48
49    _sync_time_ticks =
50              PerfDataManager::create_counter(SUN_RT, "safepointSyncTime",
51                                              PerfData::U_Ticks, CHECK);
52
53    _total_safepoints =
54              PerfDataManager::create_counter(SUN_RT, "safepoints",
55                                              PerfData::U_Events, CHECK);
56
57    _safepoint_time_ticks =
58              PerfDataManager::create_counter(SUN_RT, "safepointTime",
59                                              PerfData::U_Ticks, CHECK);
60
61    _application_time_ticks =
62              PerfDataManager::create_counter(SUN_RT, "applicationTime",
63                                              PerfData::U_Ticks, CHECK);
64
65
66    // create performance counters for jvm_version and its capabilities
67    PerfDataManager::create_constant(SUN_RT, "jvmVersion", PerfData::U_None,
68                                     (jlong) Abstract_VM_Version::jvm_version(), CHECK);
69
70    // The capabilities counter is a binary representation of the VM capabilities in string.
71    // This string respresentation simplifies the implementation of the client side
72    // to parse the value.
73    char capabilities[65];
74    size_t len = sizeof(capabilities);
75    memset((void*) capabilities, '0', len);
76    capabilities[len-1] = '\0';
77    capabilities[0] = AttachListener::is_attach_supported() ? '1' : '0';
78#if INCLUDE_SERVICES
79    capabilities[1] = '1';
80#endif // INCLUDE_SERVICES
81    PerfDataManager::create_string_constant(SUN_RT, "jvmCapabilities",
82                                            capabilities, CHECK);
83  }
84}
85
86void RuntimeService::record_safepoint_begin() {
87  HS_PRIVATE_SAFEPOINT_BEGIN();
88
89  // Print the time interval in which the app was executing
90  if (PrintGCApplicationConcurrentTime && _app_timer.is_updated()) {
91    gclog_or_tty->date_stamp(PrintGCDateStamps);
92    gclog_or_tty->stamp(PrintGCTimeStamps);
93    gclog_or_tty->print_cr("Application time: %3.7f seconds",
94                                last_application_time_sec());
95  }
96
97  // update the time stamp to begin recording safepoint time
98  _safepoint_timer.update();
99  if (UsePerfData) {
100    _total_safepoints->inc();
101    if (_app_timer.is_updated()) {
102      _application_time_ticks->inc(_app_timer.ticks_since_update());
103    }
104  }
105}
106
107void RuntimeService::record_safepoint_synchronized() {
108  if (UsePerfData) {
109    _sync_time_ticks->inc(_safepoint_timer.ticks_since_update());
110  }
111}
112
113void RuntimeService::record_safepoint_end() {
114  HS_PRIVATE_SAFEPOINT_END();
115
116  // Print the time interval for which the app was stopped
117  // during the current safepoint operation.
118  if (PrintGCApplicationStoppedTime) {
119    gclog_or_tty->date_stamp(PrintGCDateStamps);
120    gclog_or_tty->stamp(PrintGCTimeStamps);
121    gclog_or_tty->print_cr("Total time for which application threads "
122                           "were stopped: %3.7f seconds",
123                           last_safepoint_time_sec());
124  }
125
126  // update the time stamp to begin recording app time
127  _app_timer.update();
128  if (UsePerfData) {
129    _safepoint_time_ticks->inc(_safepoint_timer.ticks_since_update());
130  }
131}
132
133void RuntimeService::record_application_start() {
134  // update the time stamp to begin recording app time
135  _app_timer.update();
136}
137
138// Don't need to record application end because we currently
139// exit at a safepoint and record_safepoint_begin() handles updating
140// the application time counter at VM exit.
141
142jlong RuntimeService::safepoint_sync_time_ms() {
143  return UsePerfData ?
144    Management::ticks_to_ms(_sync_time_ticks->get_value()) : -1;
145}
146
147jlong RuntimeService::safepoint_count() {
148  return UsePerfData ?
149    _total_safepoints->get_value() : -1;
150}
151jlong RuntimeService::safepoint_time_ms() {
152  return UsePerfData ?
153    Management::ticks_to_ms(_safepoint_time_ticks->get_value()) : -1;
154}
155
156jlong RuntimeService::application_time_ms() {
157  return UsePerfData ?
158    Management::ticks_to_ms(_application_time_ticks->get_value()) : -1;
159}
160
161#endif // INCLUDE_MANAGEMENT
162