2013-04-17 00:00:59 -04:00
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
2013-04-16 23:57:48 -04:00
# ident "$Id$"
2013-04-17 00:00:59 -04:00
# ident "Copyright (c) 2007-2012 Tokutek Inc. All rights reserved."
2013-04-16 23:57:48 -04:00
# ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11 / 760379 and to the patents and / or patent applications resulting from it."
2013-04-16 23:57:21 -04:00
2008-01-18 16:01:25 +00:00
/* Tell me the diff between two brt files. */
2008-02-08 03:17:38 +00:00
refs #5592 move circular_buffer, omt, threadpool, growable_array, rwlock, frwlock, kibbutz, mempool, partitioned_counter, nb_mutex, sort to util/, and some other cleanup stuff
git-svn-id: file:///svn/toku/tokudb@48763 c7de825b-a66e-492c-adef-691d508d4ae1
2013-04-17 00:01:13 -04:00
# include "cachetable.h"
# include "ft.h"
# include "fttypes.h"
# include "ft-internal.h"
# include <ctype.h>
# include <stdint.h>
# include <stdio.h>
2013-04-17 00:01:10 -04:00
# include <stdlib.h>
2013-04-17 00:00:25 -04:00
# include <inttypes.h>
2013-04-17 00:01:10 -04:00
# include <limits.h>
2008-01-18 16:01:25 +00:00
2013-04-16 23:59:36 -04:00
static void
format_time ( const uint64_t time_int , char * buf ) {
time_t timer = ( time_t ) time_int ;
ctime_r ( & timer , buf ) ;
2013-04-16 23:59:40 -04:00
assert ( buf [ 24 ] = = ' \n ' ) ;
buf [ 24 ] = 0 ;
2013-04-16 23:59:36 -04:00
}
2008-04-30 13:23:04 +00:00
static int dump_data = 1 ;
2013-04-16 23:58:54 -04:00
static CACHETABLE ct ;
2013-04-16 23:57:20 -04:00
static void
print_item ( bytevec val , ITEMLEN len ) {
2008-04-27 17:54:24 +00:00
printf ( " \" " ) ;
ITEMLEN i ;
for ( i = 0 ; i < len ; i + + ) {
unsigned char ch = ( ( unsigned char * ) val ) [ i ] ;
if ( isprint ( ch ) & & ch ! = ' \\ ' & & ch ! = ' " ' ) {
printf ( " %c " , ch ) ;
} else {
printf ( " \\ %03o " , ch ) ;
}
}
printf ( " \" " ) ;
}
2013-04-16 23:59:34 -04:00
static void
2013-04-17 00:01:01 -04:00
simple_hex_dump ( unsigned char * vp , uint64_t size ) {
for ( uint64_t i = 0 ; i < size ; i + + ) {
2013-04-16 23:59:34 -04:00
unsigned char c = vp [ i ] ;
printf ( " %2.2X " , c ) ;
}
}
static void
2013-04-17 00:01:01 -04:00
hex_dump ( unsigned char * vp , uint64_t offset , uint64_t size ) {
uint64_t n = size / 32 ;
for ( uint64_t i = 0 ; i < n ; i + + ) {
2013-04-17 00:00:58 -04:00
printf ( " % " PRIu64 " : " , offset ) ;
2013-04-17 00:01:01 -04:00
for ( uint64_t j = 0 ; j < 32 ; j + + ) {
2013-04-16 23:59:34 -04:00
unsigned char c = vp [ j ] ;
printf ( " %2.2X " , c ) ;
if ( ( ( j + 1 ) % 4 ) = = 0 )
2013-04-16 23:59:40 -04:00
printf ( " " ) ;
2013-04-16 23:59:34 -04:00
}
2013-04-17 00:01:01 -04:00
for ( uint64_t j = 0 ; j < 32 ; j + + ) {
2013-04-16 23:59:40 -04:00
unsigned char c = vp [ j ] ;
2013-04-16 23:59:34 -04:00
printf ( " %c " , isprint ( c ) ? c : ' ' ) ;
}
printf ( " \n " ) ;
vp + = 32 ;
offset + = 32 ;
}
size = size % 32 ;
2013-04-17 00:01:01 -04:00
for ( uint64_t i = 0 ; i < size ; i + + ) {
2013-04-16 23:59:40 -04:00
if ( ( i % 32 ) = = 0 )
2013-04-17 00:00:58 -04:00
printf ( " % " PRIu64 " : " , offset + i ) ;
2013-04-16 23:59:40 -04:00
printf ( " %2.2X " , vp [ i ] ) ;
if ( ( ( i + 1 ) % 4 ) = = 0 )
printf ( " " ) ;
if ( ( ( i + 1 ) % 32 ) = = 0 )
printf ( " \n " ) ;
2013-04-16 23:59:34 -04:00
}
printf ( " \n " ) ;
}
static void
dump_descriptor ( DESCRIPTOR d ) {
2013-04-16 23:59:37 -04:00
printf ( " descriptor size %u " , d - > dbt . size ) ;
2013-04-17 00:00:58 -04:00
simple_hex_dump ( ( unsigned char * ) d - > dbt . data , d - > dbt . size ) ;
2013-04-16 23:59:34 -04:00
printf ( " \n " ) ;
}
2013-04-16 23:57:20 -04:00
static void
2013-04-17 00:00:35 -04:00
dump_header ( int f , FT * header , CACHEFILE cf ) {
2013-04-17 00:00:58 -04:00
FT ft = NULL ;
2008-01-18 16:01:25 +00:00
int r ;
2013-04-16 23:59:36 -04:00
char timestr [ 26 ] ;
2013-04-17 00:00:37 -04:00
r = toku_deserialize_ft_from ( f , MAX_LSN , & ft ) ;
2013-04-16 23:59:35 -04:00
assert ( r = = 0 ) ;
2013-04-17 00:00:37 -04:00
ft - > cf = cf ;
2013-04-17 00:00:35 -04:00
printf ( " ft: \n " ) ;
2013-04-17 00:00:37 -04:00
printf ( " layout_version=%d \n " , ft - > h - > layout_version ) ;
printf ( " layout_version_original=%d \n " , ft - > h - > layout_version_original ) ;
printf ( " layout_version_read_from_disk=%d \n " , ft - > layout_version_read_from_disk ) ;
printf ( " build_id=%d \n " , ft - > h - > build_id ) ;
printf ( " build_id_original=%d \n " , ft - > h - > build_id_original ) ;
format_time ( ft - > h - > time_of_creation , timestr ) ;
2013-04-17 00:00:58 -04:00
printf ( " time_of_creation= % " PRIu64 " %s \n " , ft - > h - > time_of_creation , timestr ) ;
2013-04-17 00:00:37 -04:00
format_time ( ft - > h - > time_of_last_modification , timestr ) ;
2013-04-17 00:00:58 -04:00
printf ( " time_of_last_modification=% " PRIu64 " %s \n " , ft - > h - > time_of_last_modification , timestr ) ;
2013-04-17 00:00:37 -04:00
printf ( " dirty=%d \n " , ft - > h - > dirty ) ;
printf ( " checkpoint_count=% " PRId64 " \n " , ft - > h - > checkpoint_count ) ;
printf ( " checkpoint_lsn=% " PRId64 " \n " , ft - > h - > checkpoint_lsn . lsn ) ;
printf ( " nodesize=%u \n " , ft - > h - > nodesize ) ;
printf ( " basementnodesize=%u \n " , ft - > h - > basementnodesize ) ;
printf ( " compression_method=%u \n " , ( unsigned ) ft - > h - > compression_method ) ;
printf ( " unnamed_root=% " PRId64 " \n " , ft - > h - > root_blocknum . b ) ;
printf ( " flags=%u \n " , ft - > h - > flags ) ;
dump_descriptor ( & ft - > descriptor ) ;
printf ( " estimated numrows=% " PRId64 " \n " , ft - > in_memory_stats . numrows ) ;
printf ( " estimated numbytes=% " PRId64 " \n " , ft - > in_memory_stats . numbytes ) ;
* header = ft ;
2008-01-22 19:30:02 +00:00
}
2013-04-16 23:57:20 -04:00
static int
2013-04-17 00:01:01 -04:00
print_le ( OMTVALUE lev , uint32_t UU ( idx ) , void * UU ( v ) ) {
2013-04-17 00:00:59 -04:00
LEAFENTRY CAST_FROM_VOIDP ( le , lev ) ;
2008-07-24 21:25:31 +00:00
print_leafentry ( stdout , le ) ;
printf ( " \n " ) ;
return 0 ;
}
2013-04-16 23:57:20 -04:00
static void
2013-04-17 00:00:35 -04:00
dump_node ( int f , BLOCKNUM blocknum , FT h ) {
FTNODE n ;
struct ftnode_fetch_extra bfe ;
FTNODE_DISK_DATA ndd = NULL ;
2013-04-16 23:59:54 -04:00
fill_bfe_for_full_read ( & bfe , h ) ;
2013-04-17 00:00:35 -04:00
int r = toku_deserialize_ftnode_from ( f , blocknum , 0 /*pass zero for hash, it doesn't matter*/ , & n , & ndd , & bfe ) ;
2008-01-22 16:27:54 +00:00
assert ( r = = 0 ) ;
2008-03-17 02:40:59 +00:00
assert ( n ! = 0 ) ;
2013-04-17 00:00:35 -04:00
printf ( " ftnode \n " ) ;
2013-04-16 23:57:42 -04:00
DISKOFF disksize , diskoffset ;
2013-04-16 23:57:47 -04:00
toku_translate_blocknum_to_offset_size ( h - > blocktable , blocknum , & diskoffset , & disksize ) ;
2013-04-16 23:57:42 -04:00
printf ( " diskoffset =% " PRId64 " \n " , diskoffset ) ;
printf ( " disksize =% " PRId64 " \n " , disksize ) ;
2013-04-17 00:00:35 -04:00
printf ( " serialize_size =%u \n " , toku_serialize_ftnode_size ( n ) ) ;
2008-01-22 19:30:02 +00:00
printf ( " flags =%u \n " , n - > flags ) ;
2013-04-16 23:57:18 -04:00
printf ( " thisnodename=% " PRId64 " \n " , n - > thisnodename . b ) ;
2008-01-23 18:06:23 +00:00
//printf(" log_lsn =%lld\n", n->log_lsn.lsn); // The log_lsn is a memory-only value.
2008-01-22 19:30:02 +00:00
printf ( " height =%d \n " , n - > height ) ;
2008-04-24 15:44:10 +00:00
printf ( " layout_version=%d \n " , n - > layout_version ) ;
2013-04-16 23:58:01 -04:00
printf ( " layout_version_original=%d \n " , n - > layout_version_original ) ;
printf ( " layout_version_read_from_disk=%d \n " , n - > layout_version_read_from_disk ) ;
2013-04-16 23:59:36 -04:00
printf ( " build_id=%d \n " , n - > build_id ) ;
2013-04-17 00:00:58 -04:00
printf ( " max_msn_applied_to_node_on_disk=% " PRId64 " (0x% " PRIx64 " ) \n " , n - > max_msn_applied_to_node_on_disk . msn , n - > max_msn_applied_to_node_on_disk . msn ) ;
2013-04-16 23:59:40 -04:00
printf ( " n_children=%d \n " , n - > n_children ) ;
printf ( " total_childkeylens=%u \n " , n - > totalchildkeylens ) ;
2013-04-16 23:59:41 -04:00
2013-04-16 23:59:40 -04:00
printf ( " pivots: \n " ) ;
2013-04-16 23:59:42 -04:00
for ( int i = 0 ; i < n - > n_children - 1 ; i + + ) {
2013-04-17 00:00:29 -04:00
const DBT * piv = & n - > childkeys [ i ] ;
2013-04-16 23:59:42 -04:00
printf ( " pivot %2d: " , i ) ;
2013-04-16 23:59:40 -04:00
assert ( n - > flags = = 0 ) ;
2013-04-17 00:00:29 -04:00
print_item ( piv - > data , piv - > size ) ;
2013-04-16 23:59:40 -04:00
printf ( " \n " ) ;
}
printf ( " children: \n " ) ;
2013-04-16 23:59:42 -04:00
for ( int i = 0 ; i < n - > n_children ; i + + ) {
2013-04-16 23:59:40 -04:00
if ( n - > height > 0 ) {
2013-04-16 23:59:41 -04:00
printf ( " child %d: % " PRId64 " \n " , i , BP_BLOCKNUM ( n , i ) . b ) ;
2013-04-16 23:59:48 -04:00
NONLEAF_CHILDINFO bnc = BNC ( n , i ) ;
unsigned int n_bytes = toku_bnc_nbytesinbuf ( bnc ) ;
int n_entries = toku_bnc_n_entries ( bnc ) ;
2013-04-16 23:59:40 -04:00
if ( n_bytes > 0 | | n_entries > 0 ) {
printf ( " buffer contains %u bytes (%d items) \n " , n_bytes , n_entries ) ;
}
if ( dump_data ) {
2013-04-16 23:59:48 -04:00
FIFO_ITERATE ( bnc - > buffer , key , keylen , data , datalen , typ , msn , xids , UU ( is_fresh ) ,
2013-04-16 23:59:40 -04:00
{
2013-04-17 00:00:58 -04:00
printf ( " msn=% " PRIu64 " (0x% " PRIx64 " ) " , msn . msn , msn . msn ) ;
2013-04-16 23:59:40 -04:00
printf ( " TYPE= " ) ;
2013-04-17 00:00:35 -04:00
switch ( ( enum ft_msg_type ) typ ) {
case FT_NONE : printf ( " NONE " ) ; goto ok ;
case FT_INSERT : printf ( " INSERT " ) ; goto ok ;
case FT_INSERT_NO_OVERWRITE : printf ( " INSERT_NO_OVERWRITE " ) ; goto ok ;
case FT_DELETE_ANY : printf ( " DELETE_ANY " ) ; goto ok ;
case FT_ABORT_ANY : printf ( " ABORT_ANY " ) ; goto ok ;
case FT_COMMIT_ANY : printf ( " COMMIT_ANY " ) ; goto ok ;
case FT_COMMIT_BROADCAST_ALL : printf ( " COMMIT_BROADCAST_ALL " ) ; goto ok ;
case FT_COMMIT_BROADCAST_TXN : printf ( " COMMIT_BROADCAST_TXN " ) ; goto ok ;
case FT_ABORT_BROADCAST_TXN : printf ( " ABORT_BROADCAST_TXN " ) ; goto ok ;
case FT_OPTIMIZE : printf ( " OPTIMIZE " ) ; goto ok ;
case FT_OPTIMIZE_FOR_UPGRADE : printf ( " OPTIMIZE_FOR_UPGRADE " ) ; goto ok ;
case FT_UPDATE : printf ( " UPDATE " ) ; goto ok ;
case FT_UPDATE_BROADCAST_ALL : printf ( " UPDATE_BROADCAST_ALL " ) ; goto ok ;
2013-04-16 23:59:40 -04:00
}
printf ( " HUH? " ) ;
ok :
printf ( " xid= " ) ;
2013-04-16 23:57:58 -04:00
xids_fprintf ( stdout , xids ) ;
printf ( " " ) ;
2008-04-30 13:23:04 +00:00
print_item ( key , keylen ) ;
if ( datalen > 0 ) {
printf ( " " ) ;
print_item ( data , datalen ) ;
}
printf ( " \n " ) ;
2013-04-16 23:57:21 -04:00
}
2008-04-30 13:23:04 +00:00
) ;
}
2013-04-16 23:59:40 -04:00
} else {
2013-04-16 23:59:42 -04:00
printf ( " n_bytes_in_buffer=%u " , BLB_NBYTESINBUF ( n , i ) ) ;
printf ( " items_in_buffer=%u \n " , toku_omt_size ( BLB_BUFFER ( n , i ) ) ) ;
2013-04-16 23:59:41 -04:00
if ( dump_data ) toku_omt_iterate ( BLB_BUFFER ( n , i ) , print_le , 0 ) ;
2008-01-22 19:30:02 +00:00
}
2013-04-16 23:59:40 -04:00
}
2013-04-17 00:00:35 -04:00
toku_ftnode_free ( & n ) ;
2013-04-17 00:00:13 -04:00
toku_free ( ndd ) ;
2008-01-18 16:01:25 +00:00
}
2013-04-16 23:57:20 -04:00
static void
2013-04-17 00:01:01 -04:00
dump_block_translation ( FT h , uint64_t offset ) {
2013-04-16 23:57:47 -04:00
toku_blocknum_dump_translation ( h - > blocktable , make_blocknum ( offset ) ) ;
2013-04-16 23:57:20 -04:00
}
2013-04-16 23:57:47 -04:00
typedef struct {
int f ;
2013-04-17 00:00:35 -04:00
FT h ;
2013-04-17 00:01:01 -04:00
uint64_t blocksizes ;
uint64_t leafsizes ;
uint64_t leafblocks ;
2013-04-16 23:57:47 -04:00
} frag_help_extra ;
static int
fragmentation_helper ( BLOCKNUM b , int64_t size , int64_t UU ( address ) , void * extra ) {
2013-04-17 00:00:59 -04:00
frag_help_extra * CAST_FROM_VOIDP ( info , extra ) ;
2013-04-17 00:00:35 -04:00
FTNODE n ;
FTNODE_DISK_DATA ndd = NULL ;
struct ftnode_fetch_extra bfe ;
2013-04-16 23:59:54 -04:00
fill_bfe_for_full_read ( & bfe , info - > h ) ;
2013-04-17 00:00:35 -04:00
int r = toku_deserialize_ftnode_from ( info - > f , b , 0 /*pass zero for hash, it doesn't matter*/ , & n , & ndd , & bfe ) ;
2013-04-16 23:57:47 -04:00
if ( r = = 0 ) {
info - > blocksizes + = size ;
if ( n - > height = = 0 ) {
info - > leafsizes + = size ;
info - > leafblocks + + ;
}
2013-04-17 00:00:35 -04:00
toku_ftnode_free ( & n ) ;
2013-04-17 00:00:13 -04:00
toku_free ( ndd ) ;
2013-04-16 23:57:47 -04:00
}
2013-04-16 23:57:20 -04:00
return 0 ;
}
static void
2013-04-17 00:00:35 -04:00
dump_fragmentation ( int f , FT h ) {
2013-04-16 23:57:47 -04:00
frag_help_extra info ;
memset ( & info , 0 , sizeof ( info ) ) ;
info . f = f ;
info . h = h ;
toku_blocktable_iterate ( h - > blocktable , TRANSLATION_CHECKPOINTED ,
2013-04-17 00:01:01 -04:00
fragmentation_helper , & info , true , true ) ;
2013-04-16 23:57:47 -04:00
int64_t used_space ;
int64_t total_space ;
toku_blocktable_internal_fragmentation ( h - > blocktable , & total_space , & used_space ) ;
int64_t fragsizes = total_space - used_space ;
2013-04-16 23:57:41 -04:00
2013-04-16 23:57:47 -04:00
printf ( " leafblocks: % " PRIu64 " \n " , info . leafblocks ) ;
printf ( " blocksizes: % " PRIu64 " \n " , info . blocksizes ) ;
printf ( " used size: % " PRId64 " \n " , used_space ) ;
printf ( " total size: % " PRId64 " \n " , total_space ) ;
printf ( " leafsizes: % " PRIu64 " \n " , info . leafsizes ) ;
printf ( " fragsizes: % " PRId64 " \n " , fragsizes ) ;
printf ( " fragmentation: %.1f%% \n " , 100. * ( ( double ) fragsizes / ( double ) ( total_space ) ) ) ;
2013-04-16 23:57:20 -04:00
}
2013-04-17 00:00:12 -04:00
typedef struct {
int f ;
2013-04-17 00:00:35 -04:00
FT h ;
2013-04-17 00:00:12 -04:00
size_t total_space ;
size_t used_space ;
} garbage_help_extra ;
static int
2013-04-17 00:01:01 -04:00
garbage_leafentry_helper ( OMTVALUE v , uint32_t UU ( idx ) , void * extra ) {
2013-04-17 00:00:59 -04:00
garbage_help_extra * CAST_FROM_VOIDP ( info , extra ) ;
LEAFENTRY CAST_FROM_VOIDP ( le , v ) ;
2013-04-17 00:00:12 -04:00
info - > total_space + = leafentry_disksize ( le ) ;
info - > used_space + = LE_CLEAN_MEMSIZE ( le_latest_keylen ( le ) , le_latest_vallen ( le ) ) ;
return 0 ;
}
static int
garbage_helper ( BLOCKNUM b , int64_t UU ( size ) , int64_t UU ( address ) , void * extra ) {
2013-04-17 00:00:59 -04:00
garbage_help_extra * CAST_FROM_VOIDP ( info , extra ) ;
2013-04-17 00:00:35 -04:00
FTNODE n ;
FTNODE_DISK_DATA ndd = NULL ;
struct ftnode_fetch_extra bfe ;
2013-04-17 00:00:12 -04:00
fill_bfe_for_full_read ( & bfe , info - > h ) ;
2013-04-17 00:00:35 -04:00
int r = toku_deserialize_ftnode_from ( info - > f , b , 0 , & n , & ndd , & bfe ) ;
2013-04-17 00:00:12 -04:00
if ( r ! = 0 ) {
2013-04-17 00:00:13 -04:00
goto no_node ;
2013-04-17 00:00:12 -04:00
}
if ( n - > height > 0 ) {
goto exit ;
}
for ( int i = 0 ; i < n - > n_children ; + + i ) {
BASEMENTNODE bn = BLB ( n , i ) ;
r = toku_omt_iterate ( bn - > buffer , garbage_leafentry_helper , info ) ;
if ( r ! = 0 ) {
goto exit ;
}
}
exit :
2013-04-17 00:00:35 -04:00
toku_ftnode_free ( & n ) ;
2013-04-17 00:00:13 -04:00
toku_free ( ndd ) ;
2013-04-17 00:00:13 -04:00
no_node :
2013-04-17 00:00:12 -04:00
return r ;
}
static void
2013-04-17 00:00:35 -04:00
dump_garbage_stats ( int f , FT h ) {
2013-04-17 00:00:12 -04:00
garbage_help_extra info ;
memset ( & info , 0 , sizeof info ) ;
info . f = f ;
info . h = h ;
toku_blocktable_iterate ( h - > blocktable , TRANSLATION_CHECKPOINTED ,
2013-04-17 00:01:01 -04:00
garbage_helper , & info , true , true ) ;
2013-04-17 00:00:12 -04:00
printf ( " total_size: %zu \n " , info . total_space ) ;
printf ( " used_size: %zu \n " , info . used_space ) ;
}
2013-04-17 00:01:01 -04:00
static uint32_t
2013-04-16 23:59:05 -04:00
get_unaligned_uint32 ( unsigned char * p ) {
2013-04-17 00:01:01 -04:00
return * ( uint32_t * ) p ;
2013-04-16 23:59:05 -04:00
}
2013-04-16 23:59:41 -04:00
struct dump_sub_block {
2013-04-17 00:01:01 -04:00
uint32_t compressed_size ;
uint32_t uncompressed_size ;
uint32_t xsum ;
2013-04-16 23:59:05 -04:00
} ;
static void
2013-04-16 23:59:41 -04:00
sub_block_deserialize ( struct dump_sub_block * sb , unsigned char * sub_block_header ) {
2013-04-16 23:59:05 -04:00
sb - > compressed_size = toku_dtoh32 ( get_unaligned_uint32 ( sub_block_header + 0 ) ) ;
sb - > uncompressed_size = toku_dtoh32 ( get_unaligned_uint32 ( sub_block_header + 4 ) ) ;
sb - > xsum = toku_dtoh32 ( get_unaligned_uint32 ( sub_block_header + 8 ) ) ;
}
static void
2013-04-17 00:01:01 -04:00
verify_block ( unsigned char * cp , uint64_t file_offset , uint64_t size ) {
2013-04-16 23:59:05 -04:00
// verify the header checksum
2013-04-17 00:01:01 -04:00
const size_t node_header = 8 + sizeof ( uint32_t ) + sizeof ( uint32_t ) + sizeof ( uint32_t ) ;
2013-04-16 23:59:39 -04:00
printf ( " %.8s layout_version=%u %u build=%d \n " , cp , get_unaligned_uint32 ( cp + 8 ) , get_unaligned_uint32 ( cp + 12 ) , get_unaligned_uint32 ( cp + 16 ) ) ;
2013-04-16 23:59:05 -04:00
unsigned char * sub_block_header = & cp [ node_header ] ;
2013-04-17 00:01:01 -04:00
uint32_t n_sub_blocks = toku_dtoh32 ( get_unaligned_uint32 ( & sub_block_header [ 0 ] ) ) ;
uint32_t header_length = node_header + n_sub_blocks * sizeof ( struct dump_sub_block ) ;
header_length + = sizeof ( uint32_t ) ; // CRC
2013-04-16 23:59:05 -04:00
if ( header_length > size ) {
printf ( " header length too big: %u \n " , header_length ) ;
return ;
}
2013-04-17 00:01:01 -04:00
uint32_t header_xsum = x1764_memory ( cp , header_length ) ;
uint32_t expected_xsum = toku_dtoh32 ( get_unaligned_uint32 ( & cp [ header_length ] ) ) ;
2013-04-16 23:59:05 -04:00
if ( header_xsum ! = expected_xsum ) {
printf ( " header checksum failed: %u %u \n " , header_xsum , expected_xsum ) ;
return ;
}
// deserialize the sub block header
2013-04-16 23:59:41 -04:00
struct dump_sub_block sub_block [ n_sub_blocks ] ;
2013-04-17 00:01:01 -04:00
sub_block_header + = sizeof ( uint32_t ) ;
for ( uint32_t i = 0 ; i < n_sub_blocks ; i + + ) {
2013-04-16 23:59:05 -04:00
sub_block_deserialize ( & sub_block [ i ] , sub_block_header ) ;
2013-04-16 23:59:41 -04:00
sub_block_header + = sizeof ( struct dump_sub_block ) ;
2013-04-16 23:59:05 -04:00
}
// verify the sub block header
2013-04-17 00:01:01 -04:00
uint32_t offset = header_length + 4 ;
for ( uint32_t i = 0 ; i < n_sub_blocks ; i + + ) {
uint32_t xsum = x1764_memory ( cp + offset , sub_block [ i ] . compressed_size ) ;
2013-04-16 23:59:05 -04:00
printf ( " %u: %u %u %u " , i , sub_block [ i ] . compressed_size , sub_block [ i ] . uncompressed_size , sub_block [ i ] . xsum ) ;
if ( xsum ! = sub_block [ i ] . xsum )
2013-04-17 00:00:58 -04:00
printf ( " fail %u offset % " PRIu64 , xsum , file_offset + offset ) ;
2013-04-16 23:59:05 -04:00
printf ( " \n " ) ;
offset + = sub_block [ i ] . compressed_size ;
}
if ( offset ! = size )
2013-04-17 00:00:58 -04:00
printf ( " offset %u expected % " PRIu64 " \n " , offset , size ) ;
2013-04-16 23:59:05 -04:00
}
static void
2013-04-17 00:00:35 -04:00
dump_block ( int f , BLOCKNUM blocknum , FT h ) {
2013-04-16 23:59:05 -04:00
DISKOFF offset , size ;
toku_translate_blocknum_to_offset_size ( h - > blocktable , blocknum , & offset , & size ) ;
2013-04-17 00:00:58 -04:00
printf ( " % " PRId64 " at % " PRId64 " size % " PRId64 " \n " , blocknum . b , offset , size ) ;
2013-04-16 23:59:05 -04:00
2013-04-17 00:00:59 -04:00
unsigned char * CAST_FROM_VOIDP ( vp , toku_malloc ( size ) ) ;
2013-04-17 00:01:01 -04:00
uint64_t r = pread ( f , vp , size , offset ) ;
if ( r = = ( uint64_t ) size ) {
2013-04-16 23:59:34 -04:00
verify_block ( vp , offset , size ) ;
2013-04-16 23:59:05 -04:00
}
2013-04-16 23:59:05 -04:00
toku_free ( vp ) ;
}
2013-04-16 23:59:05 -04:00
static void
2013-04-17 00:01:01 -04:00
dump_file ( int f , uint64_t offset , uint64_t size , FILE * outfp ) {
2013-04-17 00:00:58 -04:00
unsigned char * XMALLOC_N ( size , vp ) ;
2013-04-17 00:01:01 -04:00
uint64_t r = pread ( f , vp , size , offset ) ;
2013-04-16 23:59:34 -04:00
if ( r = = size ) {
if ( outfp = = stdout )
hex_dump ( vp , offset , size ) ;
else
fwrite ( vp , size , 1 , outfp ) ;
}
2013-04-16 23:57:46 -04:00
toku_free ( vp ) ;
}
2013-04-16 23:59:34 -04:00
static void
2013-04-17 00:01:01 -04:00
set_file ( int f , uint64_t offset , unsigned char newc ) {
2013-04-16 23:59:34 -04:00
toku_os_pwrite ( f , & newc , sizeof newc , offset ) ;
}
2013-04-16 23:59:40 -04:00
static int
2013-04-16 23:57:20 -04:00
readline ( char * line , int maxline ) {
2013-04-16 23:57:18 -04:00
int i = 0 ;
int c ;
while ( ( c = getchar ( ) ) ! = EOF & & c ! = ' \n ' & & i < maxline ) {
2013-04-16 23:57:20 -04:00
line [ i + + ] = ( char ) c ;
2013-04-16 23:57:18 -04:00
}
line [ i + + ] = 0 ;
2013-04-16 23:59:40 -04:00
return c = = EOF ? EOF : i ;
2013-04-16 23:57:18 -04:00
}
2013-04-16 23:57:20 -04:00
static int
split_fields ( char * line , char * fields [ ] , int maxfields ) {
2013-04-16 23:57:18 -04:00
int i ;
2013-04-17 00:01:14 -04:00
for ( i = 0 ; i < maxfields ; i + + )
fields [ i ] = NULL ;
2013-04-16 23:57:18 -04:00
for ( i = 0 ; i < maxfields ; i + + , line = NULL ) {
fields [ i ] = strtok ( line , " " ) ;
2013-04-17 00:00:58 -04:00
if ( fields [ i ] = = NULL ) {
break ;
}
2013-04-16 23:57:18 -04:00
}
return i ;
}
2013-04-16 23:57:39 -04:00
static int
usage ( const char * arg0 ) {
2013-04-17 00:00:35 -04:00
printf ( " Usage: %s [--nodata] [--interactive] [--rootnode] ftfilename \n " , arg0 ) ;
2013-04-16 23:57:39 -04:00
return 1 ;
}
2013-04-16 23:57:47 -04:00
typedef struct __dump_node_extra {
int f ;
2013-04-17 00:00:35 -04:00
FT h ;
2013-04-16 23:57:47 -04:00
} dump_node_extra ;
static int
dump_node_wrapper ( BLOCKNUM b , int64_t UU ( size ) , int64_t UU ( address ) , void * extra ) {
2013-04-17 00:00:59 -04:00
dump_node_extra * CAST_FROM_VOIDP ( info , extra ) ;
2013-04-16 23:57:47 -04:00
dump_node ( info - > f , b , info - > h ) ;
return 0 ;
}
2013-04-16 23:57:51 -04:00
static void
interactive_help ( void ) {
fprintf ( stderr , " help \n " ) ;
fprintf ( stderr , " header \n " ) ;
fprintf ( stderr , " node NUMBER \n " ) ;
2013-04-16 23:57:54 -04:00
fprintf ( stderr , " bx OFFSET | block_translation OFFSET \n " ) ;
fprintf ( stderr , " dumpdata 0|1 \n " ) ;
2013-04-16 23:57:51 -04:00
fprintf ( stderr , " fragmentation \n " ) ;
2013-04-17 00:00:12 -04:00
fprintf ( stderr , " garbage \n " ) ;
2013-04-16 23:59:34 -04:00
fprintf ( stderr , " file OFFSET SIZE [outfilename] \n " ) ;
2013-04-16 23:57:51 -04:00
fprintf ( stderr , " quit \n " ) ;
}
2013-04-16 23:57:54 -04:00
static uint64_t
getuint64 ( const char * f ) {
if ( strncmp ( f , " 0x " , 2 ) = = 0 | | strncmp ( f , " 0X " , 2 ) = = 0 )
return strtoull ( f , 0 , 16 ) ;
else if ( strncmp ( f , " 0 " , 1 ) = = 0 )
return strtoull ( f , 0 , 8 ) ;
else
return strtoull ( f , 0 , 10 ) ;
}
2013-04-16 23:57:22 -04:00
int
2013-04-16 23:59:01 -04:00
main ( int argc , const char * const argv [ ] ) {
2013-04-16 23:57:39 -04:00
int interactive = 0 ;
2013-04-16 23:59:40 -04:00
int rootnode = 0 ;
const char * arg0 = argv [ 0 ] ;
2008-04-30 13:23:04 +00:00
argc - - ; argv + + ;
2013-04-16 23:57:39 -04:00
while ( argc > 0 ) {
if ( strcmp ( argv [ 0 ] , " --nodata " ) = = 0 ) {
2008-04-30 13:23:04 +00:00
dump_data = 0 ;
2013-04-16 23:59:40 -04:00
} else if ( strcmp ( argv [ 0 ] , " --interactive " ) = = 0 | | strcmp ( argv [ 0 ] , " --i " ) = = 0 ) {
interactive = 1 ;
} else if ( strcmp ( argv [ 0 ] , " --rootnode " ) = = 0 ) {
rootnode = 1 ;
} else if ( strcmp ( argv [ 0 ] , " --help " ) = = 0 ) {
return usage ( arg0 ) ;
2013-04-16 23:57:39 -04:00
} else
break ;
2008-04-30 13:23:04 +00:00
argc - - ; argv + + ;
}
2013-04-16 23:57:39 -04:00
if ( argc ! = 1 ) return usage ( arg0 ) ;
2013-04-17 00:01:12 -04:00
int r = toku_ft_layer_init ( ) ;
invariant_zero ( r ) ;
2008-04-30 13:23:04 +00:00
const char * n = argv [ 0 ] ;
2013-04-16 23:59:34 -04:00
int f = open ( n , O_RDWR + O_BINARY ) ; assert ( f > = 0 ) ;
2013-04-17 00:00:37 -04:00
FT ft ;
2013-04-16 23:58:54 -04:00
// create a cachefile for the header
2013-04-17 00:01:08 -04:00
toku_cachetable_create ( & ct , 1 < < 25 , ( LSN ) { 0 } , 0 ) ;
2013-04-17 00:00:58 -04:00
CACHEFILE cf = NULL ;
2013-04-17 00:01:12 -04:00
r = toku_cachetable_openfd ( & cf , ct , f , n ) ;
2013-04-16 23:58:54 -04:00
assert ( r = = 0 ) ;
2013-04-17 00:00:37 -04:00
dump_header ( f , & ft , cf ) ;
2013-04-16 23:57:18 -04:00
if ( interactive ) {
while ( 1 ) {
2013-04-17 00:00:35 -04:00
printf ( " ftdump> " ) ; fflush ( stdout ) ;
2013-04-16 23:57:20 -04:00
enum { maxline = 64 } ;
2013-04-16 23:59:40 -04:00
char line [ maxline + 1 ] ;
r = readline ( line , maxline ) ;
if ( r = = EOF )
break ;
const int maxfields = 4 ;
char * fields [ maxfields ] ;
int nfields = split_fields ( line , fields , maxfields ) ;
if ( nfields = = 0 )
continue ;
if ( strcmp ( fields [ 0 ] , " help " ) = = 0 ) {
interactive_help ( ) ;
} else if ( strcmp ( fields [ 0 ] , " header " ) = = 0 ) {
2013-04-17 00:00:37 -04:00
toku_ft_free ( ft ) ;
dump_header ( f , & ft , cf ) ;
2013-04-16 23:59:40 -04:00
} else if ( strcmp ( fields [ 0 ] , " block " ) = = 0 & & nfields = = 2 ) {
BLOCKNUM blocknum = make_blocknum ( getuint64 ( fields [ 1 ] ) ) ;
2013-04-17 00:00:37 -04:00
dump_block ( f , blocknum , ft ) ;
2013-04-16 23:59:40 -04:00
} else if ( strcmp ( fields [ 0 ] , " node " ) = = 0 & & nfields = = 2 ) {
BLOCKNUM off = make_blocknum ( getuint64 ( fields [ 1 ] ) ) ;
2013-04-17 00:00:37 -04:00
dump_node ( f , off , ft ) ;
2013-04-16 23:59:40 -04:00
} else if ( strcmp ( fields [ 0 ] , " dumpdata " ) = = 0 & & nfields = = 2 ) {
dump_data = strtol ( fields [ 1 ] , NULL , 10 ) ;
2013-04-16 23:57:20 -04:00
} else if ( strcmp ( fields [ 0 ] , " block_translation " ) = = 0 | | strcmp ( fields [ 0 ] , " bx " ) = = 0 ) {
2013-04-17 00:01:01 -04:00
uint64_t offset = 0 ;
2013-04-16 23:59:40 -04:00
if ( nfields = = 2 )
2013-04-16 23:57:54 -04:00
offset = getuint64 ( fields [ 1 ] ) ;
2013-04-17 00:00:37 -04:00
dump_block_translation ( ft , offset ) ;
2013-04-16 23:57:20 -04:00
} else if ( strcmp ( fields [ 0 ] , " fragmentation " ) = = 0 ) {
2013-04-17 00:00:37 -04:00
dump_fragmentation ( f , ft ) ;
2013-04-17 00:00:12 -04:00
} else if ( strcmp ( fields [ 0 ] , " garbage " ) = = 0 ) {
2013-04-17 00:00:37 -04:00
dump_garbage_stats ( f , ft ) ;
2013-04-16 23:59:40 -04:00
} else if ( strcmp ( fields [ 0 ] , " file " ) = = 0 & & nfields > = 3 ) {
2013-04-17 00:01:01 -04:00
uint64_t offset = getuint64 ( fields [ 1 ] ) ;
uint64_t size = getuint64 ( fields [ 2 ] ) ;
2013-04-16 23:59:40 -04:00
FILE * outfp = stdout ;
if ( nfields > = 4 )
outfp = fopen ( fields [ 3 ] , " w " ) ;
dump_file ( f , offset , size , outfp ) ;
} else if ( strcmp ( fields [ 0 ] , " setfile " ) = = 0 & & nfields = = 3 ) {
2013-04-17 00:01:01 -04:00
uint64_t offset = getuint64 ( fields [ 1 ] ) ;
2013-04-16 23:59:40 -04:00
unsigned char newc = getuint64 ( fields [ 2 ] ) ;
set_file ( f , offset , newc ) ;
} else if ( strcmp ( fields [ 0 ] , " quit " ) = = 0 | | strcmp ( fields [ 0 ] , " q " ) = = 0 ) {
break ;
}
}
2013-04-16 23:59:40 -04:00
} else if ( rootnode ) {
2013-04-17 00:00:37 -04:00
dump_node ( f , ft - > h - > root_blocknum , ft ) ;
2013-04-16 23:57:18 -04:00
} else {
2013-04-16 23:57:25 -04:00
printf ( " Block translation: " ) ;
2013-04-16 23:57:41 -04:00
2013-04-17 00:00:37 -04:00
toku_dump_translation_table ( stdout , ft - > blocktable ) ;
2013-04-16 23:57:41 -04:00
2013-04-16 23:59:40 -04:00
struct __dump_node_extra info ;
info . f = f ;
2013-04-17 00:00:37 -04:00
info . h = ft ;
toku_blocktable_iterate ( ft - > blocktable , TRANSLATION_CHECKPOINTED ,
2013-04-17 00:01:01 -04:00
dump_node_wrapper , & info , true , true ) ;
2008-01-22 19:30:02 +00:00
}
2013-04-17 00:01:12 -04:00
toku_cachefile_close ( & cf , false , ZERO_LSN ) ;
toku_cachetable_close ( & ct ) ;
2013-04-17 00:00:37 -04:00
toku_ft_free ( ft ) ;
2013-04-17 00:01:12 -04:00
toku_ft_layer_destroy ( ) ;
2008-01-18 16:01:25 +00:00
return 0 ;
}