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 54 55
  HeapDumper(bool gc_before_heap_dump, bool print_to_tty, bool oome) :
    _gc_before_heap_dump(gc_before_heap_dump), _error(NULL), _print_to_tty(print_to_tty), _oome(oome) { }

D
duke 已提交
56 57 58 59 60 61 62 63 64 65
  // 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; }

66 67
  static void dump_heap(bool oome);

D
duke 已提交
68
 public:
C
Chuansheng Lu 已提交
69 70 71 72 73 74
  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 已提交
75 76 77 78 79 80 81 82 83

  ~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;

84
  static void dump_heap()    NOT_SERVICES_RETURN;
85

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

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

#endif // SHARE_VM_SERVICES_HEAPDUMPER_HPP