heapDumper.hpp 2.9 KB
Newer Older
D
duke 已提交
1
/*
C
Chuansheng Lu 已提交
2
 * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31
#ifndef SHARE_VM_SERVICES_HEAPDUMPER_HPP
#define SHARE_VM_SERVICES_HEAPDUMPER_HPP

#include "memory/allocation.hpp"
#include "oops/oop.hpp"
#include "runtime/os.hpp"

D
duke 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
// HeapDumper is used to dump the java heap to file in HPROF binary format:
//
//  { HeapDumper dumper(true /* full GC before heap dump */);
//    if (dumper.dump("/export/java.hprof")) {
//      ResourceMark rm;
//      tty->print_cr("Dump failed: %s", dumper.error_as_C_string());
//    } else {
//      // dump succeeded
//    }
//  }
//

class HeapDumper : public StackObj {
 private:
  char* _error;
  bool _print_to_tty;
  bool _gc_before_heap_dump;
49
  bool _oome;
C
Chuansheng Lu 已提交
50
  bool _mini_dump;
D
duke 已提交
51 52
  elapsedTimer _t;

53
  HeapDumper(bool gc_before_heap_dump, bool print_to_tty, bool oome) :
54 55
    _gc_before_heap_dump(gc_before_heap_dump), _error(NULL),
    _print_to_tty(print_to_tty), _oome(oome), _mini_dump(false) { }
56

D
duke 已提交
57 58 59 60 61 62 63 64 65 66
  // string representation of error
  char* error() const                   { return _error; }
  void set_error(char* error);

  // indicates if progress messages can be sent to tty
  bool print_to_tty() const             { return _print_to_tty; }

  // internal timer.
  elapsedTimer* timer()                 { return &_t; }

67 68
  static void dump_heap(bool oome);

D
duke 已提交
69
 public:
C
Chuansheng Lu 已提交
70 71 72 73 74 75
  HeapDumper(bool gc_before_heap_dump, bool mini_dump = false) :
    _gc_before_heap_dump(gc_before_heap_dump),
    _error(NULL),
    _print_to_tty(false),
    _oome(false),
    _mini_dump(mini_dump) { }
D
duke 已提交
76 77 78 79 80 81 82 83 84

  ~HeapDumper();

  // dumps the heap to the specified file, returns 0 if success.
  int dump(const char* path);

  // returns error message (resource allocated), or NULL if no error
  char* error_as_C_string() const;

85
  static void dump_heap()    NOT_SERVICES_RETURN;
86

87
  static void dump_heap_from_oome()    NOT_SERVICES_RETURN;
C
Chuansheng Lu 已提交
88 89

  inline bool is_mini_dump() const { return _mini_dump; }
D
duke 已提交
90
};
91 92

#endif // SHARE_VM_SERVICES_HEAPDUMPER_HPP