diff options
author | unknown <vtkachenko@quadxeon.mysql.com> | 2005-12-02 12:02:38 +0100 |
---|---|---|
committer | unknown <vtkachenko@quadxeon.mysql.com> | 2005-12-02 12:02:38 +0100 |
commit | 5c025e3f8b2e1dd9d07c0c1e1f6bdb545c1b84b1 (patch) | |
tree | e7e8613dc8266db4c61158b43df33fbf62632c72 /storage | |
parent | ca34f415e9d6493706131f769d0f56d8017e3f41 (diff) | |
parent | 1ad9ba57822404bdf99c5791187a29d626942084 (diff) | |
download | mariadb-git-5c025e3f8b2e1dd9d07c0c1e1f6bdb545c1b84b1.tar.gz |
Merge bk-internal:/home/bk/mysql-5.1-new
into quadxeon.mysql.com:/benchmarks/ext3/BUILDS/mysql-5.1-new
Diffstat (limited to 'storage')
-rw-r--r-- | storage/myisam/mi_close.c | 1 | ||||
-rw-r--r-- | storage/myisam/mi_delete_all.c | 7 | ||||
-rw-r--r-- | storage/myisam/mi_dynrec.c | 182 | ||||
-rw-r--r-- | storage/myisam/mi_extra.c | 22 | ||||
-rw-r--r-- | storage/myisam/mi_locking.c | 10 | ||||
-rw-r--r-- | storage/myisam/mi_open.c | 5 | ||||
-rw-r--r-- | storage/myisam/mi_packrec.c | 21 | ||||
-rw-r--r-- | storage/myisam/mi_statrec.c | 23 | ||||
-rw-r--r-- | storage/myisam/myisamdef.h | 12 |
9 files changed, 247 insertions, 36 deletions
diff --git a/storage/myisam/mi_close.c b/storage/myisam/mi_close.c index 62f5617de1a..39a91371323 100644 --- a/storage/myisam/mi_close.c +++ b/storage/myisam/mi_close.c @@ -96,6 +96,7 @@ int mi_close(register MI_INFO *info) { int i,keys; keys = share->state.header.keys; + VOID(rwlock_destroy(&share->mmap_lock)); for(i=0; i<keys; i++) { VOID(rwlock_destroy(&share->key_root_lock[i])); } diff --git a/storage/myisam/mi_delete_all.c b/storage/myisam/mi_delete_all.c index a30abb95070..51f1e44d6d2 100644 --- a/storage/myisam/mi_delete_all.c +++ b/storage/myisam/mi_delete_all.c @@ -22,6 +22,7 @@ int mi_delete_all_rows(MI_INFO *info) { uint i; + char buf[22]; MYISAM_SHARE *share=info->s; MI_STATE_INFO *state=&share->state; DBUG_ENTER("mi_delete_all_rows"); @@ -58,6 +59,12 @@ int mi_delete_all_rows(MI_INFO *info) my_chsize(share->kfile, share->base.keystart, 0, MYF(MY_WME)) ) goto err; VOID(_mi_writeinfo(info,WRITEINFO_UPDATE_KEYFILE)); +#ifdef HAVE_MMAP + /* Resize mmaped area */ + rw_wrlock(&info->s->mmap_lock); + mi_remap_file(info, (my_off_t)0); + rw_unlock(&info->s->mmap_lock); +#endif allow_break(); /* Allow SIGHUP & SIGINT */ DBUG_RETURN(0); diff --git a/storage/myisam/mi_dynrec.c b/storage/myisam/mi_dynrec.c index 8de500a7351..cdcd7b95ce1 100644 --- a/storage/myisam/mi_dynrec.c +++ b/storage/myisam/mi_dynrec.c @@ -50,6 +50,174 @@ static int _mi_cmp_buffer(File file, const byte *buff, my_off_t filepos, /* Interface function from MI_INFO */ +#ifdef HAVE_MMAP + +/* + Create mmaped area for MyISAM handler + + SYNOPSIS + mi_dynmap_file() + info MyISAM handler + + RETURN + 0 ok + 1 error. +*/ + +my_bool mi_dynmap_file(MI_INFO *info, my_off_t size) +{ + DBUG_ENTER("mi_dynmap_file"); + info->s->file_map= (byte*) + my_mmap(0, (size_t)(size + MEMMAP_EXTRA_MARGIN), + info->s->mode==O_RDONLY ? PROT_READ : + PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_NORESERVE, + info->dfile, 0L); + if (info->s->file_map == (byte*) MAP_FAILED) + { + info->s->file_map= NULL; + DBUG_RETURN(1); + } +#if defined(HAVE_MADVISE) + madvise(info->s->file_map, size, MADV_RANDOM); +#endif + info->s->mmaped_length= size; + DBUG_RETURN(0); +} + + +/* + Resize mmaped area for MyISAM handler + + SYNOPSIS + mi_remap_file() + info MyISAM handler + + RETURN +*/ + +void mi_remap_file(MI_INFO *info, my_off_t size) +{ + if (info->s->file_map) + { + VOID(my_munmap(info->s->file_map, + (size_t) info->s->mmaped_length + MEMMAP_EXTRA_MARGIN)); + mi_dynmap_file(info, size); + } +} +#endif + + +/* + Read bytes from MySAM handler, using mmap or pread + + SYNOPSIS + mi_mmap_pread() + info MyISAM handler + Buffer Input buffer + Count Count of bytes for read + offset Start position + MyFlags + + RETURN + 0 ok +*/ + +uint mi_mmap_pread(MI_INFO *info, byte *Buffer, + uint Count, my_off_t offset, myf MyFlags) +{ + DBUG_PRINT("info", ("mi_read with mmap %d\n", info->dfile)); + if (info->s->concurrent_insert) + rw_rdlock(&info->s->mmap_lock); + + /* + The following test may fail in the following cases: + - We failed to remap a memory area (fragmented memory?) + - This thread has done some writes, but not yet extended the + memory mapped area. + */ + + if (info->s->mmaped_length >= offset + Count) + { + memcpy(Buffer, info->s->file_map + offset, Count); + if (info->s->concurrent_insert) + rw_unlock(&info->s->mmap_lock); + return 0; + } + else + { + if (info->s->concurrent_insert) + rw_unlock(&info->s->mmap_lock); + return my_pread(info->dfile, Buffer, Count, offset, MyFlags); + } +} + + + /* wrapper for my_pread in case if mmap isn't used */ + +uint mi_nommap_pread(MI_INFO *info, byte *Buffer, + uint Count, my_off_t offset, myf MyFlags) +{ + return my_pread(info->dfile, Buffer, Count, offset, MyFlags); +} + + +/* + Write bytes to MySAM handler, using mmap or pwrite + + SYNOPSIS + mi_mmap_pwrite() + info MyISAM handler + Buffer Output buffer + Count Count of bytes for write + offset Start position + MyFlags + + RETURN + 0 ok + !=0 error. In this case return error from pwrite +*/ + +uint mi_mmap_pwrite(MI_INFO *info, byte *Buffer, + uint Count, my_off_t offset, myf MyFlags) +{ + DBUG_PRINT("info", ("mi_write with mmap %d\n", info->dfile)); + if (info->s->concurrent_insert) + rw_rdlock(&info->s->mmap_lock); + + /* + The following test may fail in the following cases: + - We failed to remap a memory area (fragmented memory?) + - This thread has done some writes, but not yet extended the + memory mapped area. + */ + + if (info->s->mmaped_length >= offset + Count) + { + memcpy(info->s->file_map + offset, Buffer, Count); + if (info->s->concurrent_insert) + rw_unlock(&info->s->mmap_lock); + return 0; + } + else + { + if (info->s->concurrent_insert) + rw_unlock(&info->s->mmap_lock); + return my_pwrite(info->dfile, Buffer, Count, offset, MyFlags); + } + +} + + + /* wrapper for my_pwrite in case if mmap isn't used */ + +uint mi_nommap_pwrite(MI_INFO *info, byte *Buffer, + uint Count, my_off_t offset, myf MyFlags) +{ + return my_pwrite(info->dfile, Buffer, Count, offset, MyFlags); +} + + int _mi_write_dynamic_record(MI_INFO *info, const byte *record) { ulong reclength=_mi_rec_pack(info,info->rec_buff,record); @@ -243,7 +411,7 @@ static bool unlink_deleted_block(MI_INFO *info, MI_BLOCK_INFO *block_info) & BLOCK_DELETED)) DBUG_RETURN(1); /* Something is wrong */ mi_sizestore(tmp.header+4,block_info->next_filepos); - if (my_pwrite(info->dfile,(char*) tmp.header+4,8, + if (info->s->file_write(info,(char*) tmp.header+4,8, block_info->prev_filepos+4, MYF(MY_NABP))) DBUG_RETURN(1); /* Unlink block from next block */ @@ -253,7 +421,7 @@ static bool unlink_deleted_block(MI_INFO *info, MI_BLOCK_INFO *block_info) & BLOCK_DELETED)) DBUG_RETURN(1); /* Something is wrong */ mi_sizestore(tmp.header+12,block_info->prev_filepos); - if (my_pwrite(info->dfile,(char*) tmp.header+12,8, + if (info->s->file_write(info,(char*) tmp.header+12,8, block_info->next_filepos+12, MYF(MY_NABP))) DBUG_RETURN(1); @@ -304,7 +472,7 @@ static int update_backward_delete_link(MI_INFO *info, my_off_t delete_block, { char buff[8]; mi_sizestore(buff,filepos); - if (my_pwrite(info->dfile,buff, 8, delete_block+12, MYF(MY_NABP))) + if (info->s->file_write(info,buff, 8, delete_block+12, MYF(MY_NABP))) DBUG_RETURN(1); /* Error on write */ } else @@ -362,7 +530,7 @@ static int delete_dynamic_record(MI_INFO *info, my_off_t filepos, bfill(block_info.header+12,8,255); else mi_sizestore(block_info.header+12,block_info.next_filepos); - if (my_pwrite(info->dfile,(byte*) block_info.header,20,filepos, + if (info->s->file_write(info,(byte*) block_info.header,20,filepos, MYF(MY_NABP))) DBUG_RETURN(1); info->s->state.dellink = filepos; @@ -545,7 +713,7 @@ int _mi_write_part_record(MI_INFO *info, else { info->rec_cache.seek_not_done=1; - if (my_pwrite(info->dfile,(byte*) *record-head_length,length+extra_length+ + if (info->s->file_write(info,(byte*) *record-head_length,length+extra_length+ del_length,filepos,info->s->write_flag)) goto err; } @@ -655,7 +823,7 @@ static int update_dynamic_record(MI_INFO *info, my_off_t filepos, byte *record, mi_int3store(del_block.header+1, rest_length); mi_sizestore(del_block.header+4,info->s->state.dellink); bfill(del_block.header+12,8,255); - if (my_pwrite(info->dfile,(byte*) del_block.header,20, next_pos, + if (info->s->file_write(info,(byte*) del_block.header,20, next_pos, MYF(MY_NABP))) DBUG_RETURN(1); info->s->state.dellink= next_pos; @@ -1182,7 +1350,7 @@ int _mi_read_dynamic_record(MI_INFO *info, my_off_t filepos, byte *buf) } if (left_length < block_info.data_len || ! block_info.data_len) goto panic; /* Wrong linked record */ - if (my_pread(file,(byte*) to,block_info.data_len,block_info.filepos, + if (info->s->file_read(info,(byte*) to,block_info.data_len,block_info.filepos, MYF(MY_NABP))) goto panic; left_length-=block_info.data_len; diff --git a/storage/myisam/mi_extra.c b/storage/myisam/mi_extra.c index 7c0dd13b870..04beb36bb47 100644 --- a/storage/myisam/mi_extra.c +++ b/storage/myisam/mi_extra.c @@ -93,6 +93,8 @@ int mi_extra(MI_INFO *info, enum ha_extra_function function, void *extra_arg) my_errno=EACCES; break; } + if (info->s->file_map) /* Don't use cache if mmap */ + break; #if defined(HAVE_MMAP) && defined(HAVE_MADVISE) if ((share->options & HA_OPTION_COMPRESS_RECORD)) { @@ -150,6 +152,7 @@ int mi_extra(MI_INFO *info, enum ha_extra_function function, void *extra_arg) error=1; /* Not possibly if not locked */ break; } + cache_size= (extra_arg ? *(ulong*) extra_arg : my_default_record_cache_size); if (!(info->opt_flag & @@ -367,6 +370,25 @@ int mi_extra(MI_INFO *info, enum ha_extra_function function, void *extra_arg) case HA_EXTRA_CHANGE_KEY_TO_DUP: mi_extra_keyflag(info, function); break; + case HA_EXTRA_MMAP: +#ifdef HAVE_MMAP + pthread_mutex_lock(&share->intern_lock); + if (!share->file_map) + { + if (mi_dynmap_file(info, share->state.state.data_file_length)) + { + DBUG_PRINT("warning",("mmap failed: errno: %d",errno)); + error= my_errno= errno; + } + else + { + share->file_read= mi_mmap_pread; + share->file_write= mi_mmap_pwrite; + } + } + pthread_mutex_unlock(&share->intern_lock); +#endif + break; case HA_EXTRA_KEY_CACHE: case HA_EXTRA_NO_KEY_CACHE: default: diff --git a/storage/myisam/mi_locking.c b/storage/myisam/mi_locking.c index 8d48c5242e5..5d871aa1e38 100644 --- a/storage/myisam/mi_locking.c +++ b/storage/myisam/mi_locking.c @@ -38,7 +38,6 @@ int mi_lock_database(MI_INFO *info, int lock_type) share->w_locks, share->global_changed, share->state.open_count, share->index_file_name)); - if (share->options & HA_OPTION_READ_ONLY_DATA || info->lock_type == lock_type) DBUG_RETURN(0); @@ -84,6 +83,14 @@ int mi_lock_database(MI_INFO *info, int lock_type) (uint) share->changed, share->w_locks)); if (share->changed && !share->w_locks) { + if (info->s->mmaped_length != info->s->state.state.data_file_length) + { + if (info->s->concurrent_insert) + rw_wrlock(&info->s->mmap_lock); + mi_remap_file(info, info->s->state.state.data_file_length); + if (info->s->concurrent_insert) + rw_unlock(&info->s->mmap_lock); + } share->state.process= share->last_process=share->this_process; share->state.unique= info->last_unique= info->this_unique; share->state.update_count= info->last_loop= ++info->this_loop; @@ -215,6 +222,7 @@ int mi_lock_database(MI_INFO *info, int lock_type) } } VOID(_mi_test_if_changed(info)); + info->lock_type=lock_type; info->invalidator=info->s->invalidator; share->w_locks++; diff --git a/storage/myisam/mi_open.c b/storage/myisam/mi_open.c index 91136bbeef5..a09e4514d63 100644 --- a/storage/myisam/mi_open.c +++ b/storage/myisam/mi_open.c @@ -288,6 +288,7 @@ MI_INFO *mi_open(const char *name, int mode, uint open_flags) #ifdef THREAD &share->key_root_lock,sizeof(rw_lock_t)*keys, #endif + &share->mmap_lock,sizeof(rw_lock_t), NullS)) goto err; errpos=4; @@ -459,7 +460,6 @@ MI_INFO *mi_open(const char *name, int mode, uint open_flags) (keys ? MI_INDEX_BLOCK_MARGIN * share->blocksize * keys : 0)); share->blocksize=min(IO_SIZE,myisam_block_size); - share->data_file_type=STATIC_RECORD; if (share->options & HA_OPTION_COMPRESS_RECORD) { @@ -482,6 +482,7 @@ MI_INFO *mi_open(const char *name, int mode, uint open_flags) VOID(pthread_mutex_init(&share->intern_lock,MY_MUTEX_INIT_FAST)); for (i=0; i<keys; i++) VOID(my_rwlock_init(&share->key_root_lock[i], NULL)); + VOID(my_rwlock_init(&share->mmap_lock, NULL)); if (!thr_lock_inited) { /* Probably a single threaded program; Don't use concurrent inserts */ @@ -736,6 +737,8 @@ void mi_setup_functions(register MYISAM_SHARE *share) share->compare_unique=_mi_cmp_static_unique; share->calc_checksum= mi_static_checksum; } + share->file_read= mi_nommap_pread; + share->file_write= mi_nommap_pwrite; if (!(share->options & HA_OPTION_CHECKSUM)) share->calc_checksum=0; return; diff --git a/storage/myisam/mi_packrec.c b/storage/myisam/mi_packrec.c index e242e9d506d..6a1fca9ede9 100644 --- a/storage/myisam/mi_packrec.c +++ b/storage/myisam/mi_packrec.c @@ -1192,34 +1192,25 @@ my_bool _mi_memmap_file(MI_INFO *info) if (!info->s->file_map) { if (my_seek(info->dfile,0L,MY_SEEK_END,MYF(0)) < - share->state.state.data_file_length+MEMMAP_EXTRA_MARGIN) + share->state.state.data_file_length+MEMMAP_EXTRA_MARGIN) { DBUG_PRINT("warning",("File isn't extended for memmap")); DBUG_RETURN(0); } - file_map=(byte*) - my_mmap(0,(size_t)(share->state.state.data_file_length+MEMMAP_EXTRA_MARGIN),PROT_READ, - MAP_SHARED | MAP_NORESERVE,info->dfile,0L); - if (file_map == (byte*) MAP_FAILED) - { - DBUG_PRINT("warning",("mmap failed: errno: %d",errno)); - my_errno=errno; + if (mi_dynmap_file(info, share->state.state.data_file_length)) DBUG_RETURN(0); - } - info->s->file_map=file_map; } info->opt_flag|= MEMMAP_USED; - info->read_record=share->read_record=_mi_read_mempack_record; - share->read_rnd=_mi_read_rnd_mempack_record; + info->read_record= share->read_record= _mi_read_mempack_record; + share->read_rnd= _mi_read_rnd_mempack_record; DBUG_RETURN(1); } void _mi_unmap_file(MI_INFO *info) { - VOID(my_munmap(info->s->file_map, - (size_t) info->s->state.state.data_file_length+ - MEMMAP_EXTRA_MARGIN)); + VOID(my_munmap(info->s->file_map, + (size_t) info->s->mmaped_length + MEMMAP_EXTRA_MARGIN)); } diff --git a/storage/myisam/mi_statrec.c b/storage/myisam/mi_statrec.c index 42352f63c66..70e63ef8ce1 100644 --- a/storage/myisam/mi_statrec.c +++ b/storage/myisam/mi_statrec.c @@ -22,20 +22,19 @@ int _mi_write_static_record(MI_INFO *info, const byte *record) { uchar temp[8]; /* max pointer length */ - if (info->s->state.dellink != HA_OFFSET_ERROR && !info->append_insert_at_end) { my_off_t filepos=info->s->state.dellink; info->rec_cache.seek_not_done=1; /* We have done a seek */ - if (my_pread(info->dfile,(char*) &temp[0],info->s->base.rec_reflength, + if (info->s->file_read(info,(char*) &temp[0],info->s->base.rec_reflength, info->s->state.dellink+1, MYF(MY_NABP))) goto err; info->s->state.dellink= _mi_rec_pos(info->s,temp); info->state->del--; info->state->empty-=info->s->base.pack_reclength; - if (my_pwrite(info->dfile, (char*) record, info->s->base.reclength, + if (info->s->file_write(info, (char*) record, info->s->base.reclength, filepos, MYF(MY_NABP))) goto err; @@ -64,19 +63,19 @@ int _mi_write_static_record(MI_INFO *info, const byte *record) else { info->rec_cache.seek_not_done=1; /* We have done a seek */ - if (my_pwrite(info->dfile,(char*) record,info->s->base.reclength, + if (info->s->file_write(info,(char*) record,info->s->base.reclength, info->state->data_file_length, info->s->write_flag)) - goto err; + goto err; if (info->s->base.pack_reclength != info->s->base.reclength) { uint length=info->s->base.pack_reclength - info->s->base.reclength; bzero((char*) temp,length); - if (my_pwrite(info->dfile, (byte*) temp,length, + if (info->s->file_write(info, (byte*) temp,length, info->state->data_file_length+ info->s->base.reclength, info->s->write_flag)) - goto err; + goto err; } } info->state->data_file_length+=info->s->base.pack_reclength; @@ -90,7 +89,7 @@ int _mi_write_static_record(MI_INFO *info, const byte *record) int _mi_update_static_record(MI_INFO *info, my_off_t pos, const byte *record) { info->rec_cache.seek_not_done=1; /* We have done a seek */ - return (my_pwrite(info->dfile, + return (info->s->file_write(info, (char*) record,info->s->base.reclength, pos, MYF(MY_NABP)) != 0); @@ -107,7 +106,7 @@ int _mi_delete_static_record(MI_INFO *info) _mi_dpointer(info,temp+1,info->s->state.dellink); info->s->state.dellink = info->lastpos; info->rec_cache.seek_not_done=1; - return (my_pwrite(info->dfile,(byte*) temp, 1+info->s->rec_reflength, + return (info->s->file_write(info,(byte*) temp, 1+info->s->rec_reflength, info->lastpos, MYF(MY_NABP)) != 0); } @@ -131,7 +130,7 @@ int _mi_cmp_static_record(register MI_INFO *info, register const byte *old) if ((info->opt_flag & READ_CHECK_USED)) { /* If check isn't disabled */ info->rec_cache.seek_not_done=1; /* We have done a seek */ - if (my_pread(info->dfile, (char*) info->rec_buff, info->s->base.reclength, + if (info->s->file_read(info, (char*) info->rec_buff, info->s->base.reclength, info->lastpos, MYF(MY_NABP))) DBUG_RETURN(-1); @@ -154,7 +153,7 @@ int _mi_cmp_static_unique(MI_INFO *info, MI_UNIQUEDEF *def, DBUG_ENTER("_mi_cmp_static_unique"); info->rec_cache.seek_not_done=1; /* We have done a seek */ - if (my_pread(info->dfile, (char*) info->rec_buff, info->s->base.reclength, + if (info->s->file_read(info, (char*) info->rec_buff, info->s->base.reclength, pos, MYF(MY_NABP))) DBUG_RETURN(-1); DBUG_RETURN(mi_unique_comp(def, record, info->rec_buff, @@ -180,7 +179,7 @@ int _mi_read_static_record(register MI_INFO *info, register my_off_t pos, return(-1); info->rec_cache.seek_not_done=1; /* We have done a seek */ - error=my_pread(info->dfile,(char*) record,info->s->base.reclength, + error=info->s->file_read(info,(char*) record,info->s->base.reclength, pos,MYF(MY_NABP)) != 0; fast_mi_writeinfo(info); if (! error) diff --git a/storage/myisam/myisamdef.h b/storage/myisam/myisamdef.h index 6ccb52aff22..564acb82e69 100644 --- a/storage/myisam/myisamdef.h +++ b/storage/myisam/myisamdef.h @@ -179,6 +179,8 @@ typedef struct st_mi_isam_share { /* Shared between opens */ ha_checksum (*calc_checksum)(struct st_myisam_info*, const byte *); int (*compare_unique)(struct st_myisam_info*, MI_UNIQUEDEF *, const byte *record, my_off_t pos); + uint (*file_read)(MI_INFO *, byte *, uint, my_off_t, myf); + uint (*file_write)(MI_INFO *, byte *, uint, my_off_t, myf); invalidator_by_filename invalidator; /* query cache invalidator */ ulong this_process; /* processid */ ulong last_process; /* For table-change-check */ @@ -207,6 +209,8 @@ typedef struct st_mi_isam_share { /* Shared between opens */ pthread_mutex_t intern_lock; /* Locking for use with _locking */ rw_lock_t *key_root_lock; #endif + my_off_t mmaped_length; + rw_lock_t mmap_lock; } MYISAM_SHARE; @@ -685,6 +689,14 @@ extern void _mi_unmap_file(MI_INFO *info); extern uint save_pack_length(uint version, byte *block_buff, ulong length); extern uint read_pack_length(uint version, const uchar *buf, ulong *length); extern uint calc_pack_length(uint version, ulong length); +extern uint mi_mmap_pread(MI_INFO *info, byte *Buffer, + uint Count, my_off_t offset, myf MyFlags); +extern uint mi_mmap_pwrite(MI_INFO *info, byte *Buffer, + uint Count, my_off_t offset, myf MyFlags); +extern uint mi_nommap_pread(MI_INFO *info, byte *Buffer, + uint Count, my_off_t offset, myf MyFlags); +extern uint mi_nommap_pwrite(MI_INFO *info, byte *Buffer, + uint Count, my_off_t offset, myf MyFlags); uint mi_state_info_write(File file, MI_STATE_INFO *state, uint pWrite); uchar *mi_state_info_read(uchar *ptr, MI_STATE_INFO *state); |