1 /*
   2  * Copyright (c) 2014, 2017, 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 #ifndef SHARE_VM_SERVICES_NMT_COMMON_HPP
  26 #define SHARE_VM_SERVICES_NMT_COMMON_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "utilities/globalDefinitions.hpp"
  30 
  31 #define CALC_OBJ_SIZE_IN_TYPE(obj, type) (align_up_(sizeof(obj), sizeof(type))/sizeof(type))
  32 
  33 // Native memory tracking level
  34 enum NMT_TrackingLevel {
  35   NMT_unknown = 0xFF,
  36   NMT_off     = 0x00,
  37   NMT_minimal = 0x01,
  38   NMT_summary = 0x02,
  39   NMT_detail  = 0x03
  40 };
  41 
  42 // Number of stack frames to capture. This is a
  43 // build time decision.
  44 const int NMT_TrackingStackDepth = 4;
  45 
  46 // A few common utilities for native memory tracking
  47 class NMTUtil : AllStatic {
  48  public:
  49   // Map memory type to index
  50   static inline int flag_to_index(MEMFLAGS flag) {
  51     const int index = flag & 0xff;
  52     assert(index >= 0 && index < (int)mt_number_of_types, "Index out of bounds");
  53     return index;
  54   }
  55 
  56   // Map memory type to human readable name
  57   static const char* flag_to_name(MEMFLAGS flag) {
  58     return _memory_type_names[flag_to_index(flag)];
  59   }
  60 
  61   // Map an index to memory type
  62   static MEMFLAGS index_to_flag(int index) {
  63     assert(index >= 0 && index < (int) mt_number_of_types, "Index out of bounds");
  64     return (MEMFLAGS)index;
  65   }
  66 
  67   // Memory size scale
  68   static const char* scale_name(size_t scale);
  69   static size_t scale_from_name(const char* scale);
  70 
  71   // Translate memory size in specified scale
  72   static size_t amount_in_scale(size_t amount, size_t scale) {
  73     return (amount + scale / 2) / scale;
  74   }
  75  private:
  76   static const char* _memory_type_names[mt_number_of_types];
  77 };
  78 
  79 
  80 #endif