Skip to content

Instantly share code, notes, and snippets.

@caoxudong
Last active January 4, 2016 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caoxudong/8567764 to your computer and use it in GitHub Desktop.
Save caoxudong/8567764 to your computer and use it in GitHub Desktop.
hotspot中,标记字中各字段的位置

test.cpp用于打印出hotspot中,标记字中各个字段的位置和相关掩码的值,其中**_in_place是指将当前字段的掩码与该字段的便宜结合之后得到的值

在32位平台上,test.cpp打印如下结果:

age_bits = 4
lock_bits = 2
biased_lock_bits = 1
max_hash_bits = 25
hash_bits = 25
cms_bits = 0
epoch_bits = 2

lock_shift = 0
biased_lock_shift = 2
age_shift = 3
cms_shift = 7
hash_shift = 7
epoch_shift = 7

lock_mask = 3
lock_mask_in_place = 3
biased_lock_mask = 7
biased_lock_bit_in_place = 4
age_mask = 15
age_mask_in_place = 120
epoch_mask = 3
epoch_mask_in_place = 384
cms_mask = 0
cms_mask_in_place = 0
hash_mask = 33554431
hash_mask_in_place = -128
#include "stdio.h"
//from macros.hpp
#ifdef _LP64
#define LP64_ONLY(code) code
#define NOT_LP64(code)
#else // !_LP64
#define LP64_ONLY(code)
#define NOT_LP64(code) code
#endif // _LP64
//from globalDefinitions_gcc.hpp
typedef int intptr_t;
typedef unsigned int uintptr_t;
//from globalDefinitions.hpp
typedef uintptr_t address_word; // unsigned integer which will hold a pointer
#ifdef _LP64
const int LogBytesPerWord = 3;
#else
const int LogBytesPerWord = 2;
#endif
const int LogBitsPerByte = 3;
const int LogBitsPerWord = LogBitsPerByte + LogBytesPerWord;
const int BytesPerWord = 1 << LogBytesPerWord;
const int BitsPerWord = 1 << LogBitsPerWord;
const intptr_t OneBit = 1; // only right_most bit set in a word
#define nth_bit(n) (n >= BitsPerWord ? 0 : OneBit << (n))
#define right_n_bits(n) (nth_bit(n) - 1)
//from markOop.hpp
enum { age_bits = 4,
lock_bits = 2,
biased_lock_bits = 1,
max_hash_bits = BitsPerWord - age_bits - lock_bits - biased_lock_bits,
hash_bits = max_hash_bits > 31 ? 31 : max_hash_bits,
cms_bits = LP64_ONLY(1) NOT_LP64(0),
epoch_bits = 2
};
// The biased locking code currently requires that the age bits be
// contiguous to the lock bits.
enum { lock_shift = 0,
biased_lock_shift = lock_bits,
age_shift = lock_bits + biased_lock_bits,
cms_shift = age_shift + age_bits,
hash_shift = cms_shift + cms_bits,
epoch_shift = hash_shift
};
enum { lock_mask = right_n_bits(lock_bits),
lock_mask_in_place = lock_mask << lock_shift,
biased_lock_mask = right_n_bits(lock_bits + biased_lock_bits),
biased_lock_mask_in_place= biased_lock_mask << lock_shift,
biased_lock_bit_in_place = 1 << biased_lock_shift,
age_mask = right_n_bits(age_bits),
age_mask_in_place = age_mask << age_shift,
epoch_mask = right_n_bits(epoch_bits),
epoch_mask_in_place = epoch_mask << epoch_shift,
cms_mask = right_n_bits(cms_bits),
cms_mask_in_place = cms_mask << cms_shift
#ifndef _WIN64
,hash_mask = right_n_bits(hash_bits),
hash_mask_in_place = (address_word)hash_mask << hash_shift
#endif
};
#ifdef _WIN64
// These values are too big for Win64
const static uintptr_t hash_mask = right_n_bits(hash_bits);
const static uintptr_t hash_mask_in_place =
(address_word)hash_mask << hash_shift;
#endif
//test
int main(int argc, char* argv[]) {
printf("age_bits = %d\n", age_bits);
printf("lock_bits = %d\n", lock_bits);
printf("biased_lock_bits = %d\n", biased_lock_bits);
printf("max_hash_bits = %d\n", max_hash_bits);
printf("hash_bits = %d\n", hash_bits);
printf("cms_bits = %d\n", cms_bits);
printf("epoch_bits = %d\n", epoch_bits);
printf("\n");
printf("lock_shift = %d\n", lock_shift);
printf("biased_lock_shift = %d\n", biased_lock_shift);
printf("age_shift = %d\n", age_shift);
printf("cms_shift = %d\n", cms_shift);
printf("hash_shift = %d\n", hash_shift);
printf("epoch_shift = %d\n", epoch_shift);
printf("\n");
printf("lock_mask = %d\n", lock_mask);
printf("lock_mask_in_place = %d\n", lock_mask_in_place);
printf("biased_lock_mask = %d\n", biased_lock_mask);
printf("biased_lock_bit_in_place = %d\n", biased_lock_bit_in_place);
printf("age_mask = %d\n", age_mask);
printf("age_mask_in_place = %d\n", age_mask_in_place);
printf("epoch_mask = %d\n", epoch_mask);
printf("epoch_mask_in_place = %d\n", epoch_mask_in_place);
printf("cms_mask = %d\n", cms_mask);
printf("cms_mask_in_place = %d\n", cms_mask_in_place);
printf("hash_mask = %d\n", hash_mask);
printf("hash_mask_in_place = %d\n", hash_mask_in_place);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment