Skip to content

Commit

Permalink
MDEV-21225 Remove ut_align() and use aligned_malloc()
Browse files Browse the repository at this point in the history
Before commit 90c52e5 introduced
aligned_malloc(), InnoDB always used a pattern of over-allocating
memory and invoking ut_align() to guarantee the desired alignment.

It is cleaner to invoke aligned_malloc() and aligned_free() directly.

ut_align(): Remove. In assertions, ut_align_down() can be used instead.
  • Loading branch information
dr-m committed Dec 5, 2019
1 parent 504202b commit 42a4ae5
Show file tree
Hide file tree
Showing 21 changed files with 97 additions and 236 deletions.
27 changes: 8 additions & 19 deletions extra/innochecksum.cc
Expand Up @@ -1551,8 +1551,6 @@ int main(
/* our input filename. */
char* filename;
/* Buffer to store pages read. */
byte* buf_ptr = NULL;
byte* xdes_ptr = NULL;
byte* buf = NULL;
byte* xdes = NULL;
/* bytes read count */
Expand Down Expand Up @@ -1632,10 +1630,10 @@ int main(
}


buf_ptr = (byte*) malloc(UNIV_PAGE_SIZE_MAX * 2);
xdes_ptr = (byte*)malloc(UNIV_PAGE_SIZE_MAX * 2);
buf = (byte *) ut_align(buf_ptr, UNIV_PAGE_SIZE_MAX);
xdes = (byte *) ut_align(xdes_ptr, UNIV_PAGE_SIZE_MAX);
buf = static_cast<byte*>(aligned_malloc(UNIV_PAGE_SIZE_MAX,
UNIV_PAGE_SIZE_MAX));
xdes = static_cast<byte*>(aligned_malloc(UNIV_PAGE_SIZE_MAX,
UNIV_PAGE_SIZE_MAX));

/* The file name is not optional. */
for (int i = 0; i < argc; ++i) {
Expand Down Expand Up @@ -2014,21 +2012,9 @@ int main(
fclose(log_file);
}

free(buf_ptr);
free(xdes_ptr);

my_end(exit_status);
DBUG_RETURN(exit_status);
goto common_exit;

my_exit:
if (buf_ptr) {
free(buf_ptr);
}

if (xdes_ptr) {
free(xdes_ptr);
}

if (!read_from_stdin && fil_in) {
fclose(fil_in);
}
Expand All @@ -2037,6 +2023,9 @@ int main(
fclose(log_file);
}

common_exit:
aligned_free(buf);
aligned_free(xdes);
my_end(exit_status);
DBUG_RETURN(exit_status);
}
3 changes: 1 addition & 2 deletions extra/mariabackup/backup_copy.cc
Expand Up @@ -466,14 +466,13 @@ struct datafile_cur_t {
char abs_path[FN_REFLEN];
MY_STAT statinfo;
uint thread_n;
byte* orig_buf;
byte* buf;
size_t buf_size;
size_t buf_read;
size_t buf_offset;

explicit datafile_cur_t(const char* filename = NULL) :
file(), thread_n(0), orig_buf(NULL), buf(NULL), buf_size(0),
file(), thread_n(0), buf(NULL), buf_size(0),
buf_read(0), buf_offset(0)
{
memset(rel_path, 0, sizeof rel_path);
Expand Down
11 changes: 5 additions & 6 deletions extra/mariabackup/fil_cur.cc
Expand Up @@ -142,7 +142,7 @@ xb_fil_cur_open(
int err;
/* Initialize these first so xb_fil_cur_close() handles them correctly
in case of error */
cursor->orig_buf = NULL;
cursor->buf = NULL;
cursor->node = NULL;

cursor->space_id = node->space->id;
Expand Down Expand Up @@ -238,10 +238,8 @@ xb_fil_cur_open(

/* Allocate read buffer */
cursor->buf_size = XB_FIL_CUR_PAGES * cursor->page_size;
cursor->orig_buf = static_cast<byte *>
(malloc(cursor->buf_size + srv_page_size));
cursor->buf = static_cast<byte *>
(ut_align(cursor->orig_buf, srv_page_size));
cursor->buf = static_cast<byte*>(aligned_malloc(cursor->buf_size,
srv_page_size));

cursor->buf_read = 0;
cursor->buf_npages = 0;
Expand Down Expand Up @@ -494,7 +492,8 @@ xb_fil_cur_close(
cursor->read_filter->deinit(&cursor->read_filter_ctxt);
}

free(cursor->orig_buf);
aligned_free(cursor->buf);
cursor->buf = NULL;

if (cursor->node != NULL) {
xb_fil_node_close_file(cursor->node);
Expand Down
3 changes: 1 addition & 2 deletions extra/mariabackup/fil_cur.h
Expand Up @@ -44,8 +44,7 @@ struct xb_fil_cur_t {
xb_read_filt_t* read_filter; /*!< read filter */
xb_read_filt_ctxt_t read_filter_ctxt;
/*!< read filter context */
byte* orig_buf; /*!< read buffer */
byte* buf; /*!< aligned pointer for orig_buf */
byte* buf; /*!< read buffer */
size_t buf_size; /*!< buffer size in bytes */
size_t buf_read; /*!< number of read bytes in buffer
after the last cursor read */
Expand Down
28 changes: 10 additions & 18 deletions extra/mariabackup/xtrabackup.cc
Expand Up @@ -3269,8 +3269,6 @@ static dberr_t xb_assign_undo_space_start()
{

pfs_os_file_t file;
byte* buf;
byte* page;
bool ret;
dberr_t error = DB_SUCCESS;
ulint space;
Expand All @@ -3289,8 +3287,8 @@ static dberr_t xb_assign_undo_space_start()
return DB_ERROR;
}

buf = static_cast<byte*>(ut_malloc_nokey(2U << srv_page_size_shift));
page = static_cast<byte*>(ut_align(buf, srv_page_size));
byte* page = static_cast<byte*>
(aligned_malloc(srv_page_size, srv_page_size));

if (os_file_read(IORequestRead, file, page, 0, srv_page_size)
!= DB_SUCCESS) {
Expand Down Expand Up @@ -3337,7 +3335,7 @@ static dberr_t xb_assign_undo_space_start()
srv_undo_space_id_start = space;

func_exit:
ut_free(buf);
aligned_free(page);
ret = os_file_close(file);
ut_a(ret);

Expand Down Expand Up @@ -4552,8 +4550,6 @@ xb_space_create_file(
pfs_os_file_t* file) /*!<out: file handle */
{
bool ret;
byte* buf;
byte* page;

*file = os_file_create_simple_no_error_handling(
0, path, OS_FILE_CREATE, OS_FILE_READ_WRITE, false, &ret);
Expand All @@ -4572,9 +4568,9 @@ xb_space_create_file(
return ret;
}

buf = static_cast<byte *>(malloc(3U << srv_page_size_shift));
/* Align the memory for file i/o if we might have O_DIRECT set */
page = static_cast<byte *>(ut_align(buf, srv_page_size));
byte* page = static_cast<byte*>(aligned_malloc(2 * srv_page_size,
srv_page_size));

memset(page, '\0', srv_page_size);

Expand Down Expand Up @@ -4608,7 +4604,7 @@ xb_space_create_file(
page_zip.data, 0, zip_size);
}

free(buf);
aligned_free(page);

if (ret != DB_SUCCESS) {
msg("mariabackup: could not write the first page to %s",
Expand Down Expand Up @@ -4821,8 +4817,7 @@ xtrabackup_apply_delta(
xb_delta_info_t info(srv_page_size, 0, SRV_TMP_SPACE_ID);
ulint page_size;
ulint page_size_shift;
byte* incremental_buffer_base = NULL;
byte* incremental_buffer;
byte* incremental_buffer = NULL;

size_t offset;

Expand Down Expand Up @@ -4890,11 +4885,8 @@ xtrabackup_apply_delta(
posix_fadvise(dst_file, 0, 0, POSIX_FADV_DONTNEED);

/* allocate buffer for incremental backup (4096 pages) */
incremental_buffer_base = static_cast<byte *>
(malloc((page_size / 4 + 1) * page_size));
incremental_buffer = static_cast<byte *>
(ut_align(incremental_buffer_base,
page_size));
(aligned_malloc(page_size / 4 * page_size, page_size));

msg("Applying %s to %s...", src_path, dst_path);

Expand Down Expand Up @@ -5003,7 +4995,7 @@ xtrabackup_apply_delta(
incremental_buffers++;
}

free(incremental_buffer_base);
aligned_free(incremental_buffer);
if (src_file != OS_FILE_CLOSED) {
os_file_close(src_file);
os_file_delete(0,src_path);
Expand All @@ -5013,7 +5005,7 @@ xtrabackup_apply_delta(
return TRUE;

error:
free(incremental_buffer_base);
aligned_free(incremental_buffer);
if (src_file != OS_FILE_CLOSED)
os_file_close(src_file);
if (dst_file != OS_FILE_CLOSED)
Expand Down
5 changes: 4 additions & 1 deletion storage/innobase/buf/buf0buf.cc
Expand Up @@ -1601,8 +1601,11 @@ buf_chunk_init(
opt_large_page_size is smaller than srv_page_size,
we may allocate one fewer block than requested. When
it is bigger, we may allocate more blocks than requested. */
static_assert(sizeof(byte*) == sizeof(ulint), "pointer size");

frame = (byte*) ut_align(chunk->mem, srv_page_size);
frame = reinterpret_cast<byte*>((reinterpret_cast<ulint>(chunk->mem)
+ srv_page_size - 1)
& ~(srv_page_size - 1));
chunk->size = (chunk->mem_pfx.m_size >> srv_page_size_shift)
- (frame != chunk->mem);

Expand Down
65 changes: 17 additions & 48 deletions storage/innobase/buf/buf0dblwr.cc
Expand Up @@ -126,12 +126,9 @@ static void buf_dblwr_init(const byte *doublewrite)
buf_dblwr->in_use = static_cast<bool*>(
ut_zalloc_nokey(buf_size * sizeof(bool)));

buf_dblwr->write_buf_unaligned = static_cast<byte*>(
ut_malloc_nokey((1 + buf_size) << srv_page_size_shift));

buf_dblwr->write_buf = static_cast<byte*>(
ut_align(buf_dblwr->write_buf_unaligned,
srv_page_size));
aligned_malloc(buf_size << srv_page_size_shift,
srv_page_size));

buf_dblwr->buf_block_arr = static_cast<buf_page_t**>(
ut_zalloc_nokey(buf_size * sizeof(void*)));
Expand Down Expand Up @@ -355,23 +352,18 @@ buf_dblwr_init_or_load_pages(
ulint space_id;
byte* read_buf;
byte* doublewrite;
byte* unaligned_read_buf;
ibool reset_space_ids = FALSE;
recv_dblwr_t& recv_dblwr = recv_sys.dblwr;

/* We do the file i/o past the buffer pool */

unaligned_read_buf = static_cast<byte*>(
ut_malloc_nokey(3U << srv_page_size_shift));

read_buf = static_cast<byte*>(
ut_align(unaligned_read_buf, srv_page_size));
aligned_malloc(2 * srv_page_size, srv_page_size));

/* Read the trx sys header to check if we are using the doublewrite
buffer */
dberr_t err;

IORequest read_request(IORequest::READ);
IORequest read_request(IORequest::READ);

err = os_file_read(
read_request,
Expand All @@ -382,9 +374,8 @@ buf_dblwr_init_or_load_pages(

ib::error()
<< "Failed to read the system tablespace header page";

ut_free(unaligned_read_buf);

func_exit:
aligned_free(read_buf);
return(err);
}

Expand All @@ -403,8 +394,8 @@ buf_dblwr_init_or_load_pages(

buf = buf_dblwr->write_buf;
} else {
ut_free(unaligned_read_buf);
return(DB_SUCCESS);
err = DB_SUCCESS;
goto func_exit;
}

if (mach_read_from_4(doublewrite + TRX_SYS_DOUBLEWRITE_SPACE_ID_STORED)
Expand Down Expand Up @@ -432,10 +423,7 @@ buf_dblwr_init_or_load_pages(
ib::error()
<< "Failed to read the first double write buffer "
"extent";

ut_free(unaligned_read_buf);

return(err);
goto func_exit;
}

err = os_file_read(
Expand All @@ -450,10 +438,7 @@ buf_dblwr_init_or_load_pages(
ib::error()
<< "Failed to read the second double write buffer "
"extent";

ut_free(unaligned_read_buf);

return(err);
goto func_exit;
}

/* Check if any of these pages is half-written in data files, in the
Expand All @@ -480,21 +465,16 @@ buf_dblwr_init_or_load_pages(
+ i - TRX_SYS_DOUBLEWRITE_BLOCK_SIZE;
}

IORequest write_request(IORequest::WRITE);

err = os_file_write(
write_request, path, file, page,
IORequestWrite, path, file, page,
source_page_no << srv_page_size_shift,
srv_page_size);
if (err != DB_SUCCESS) {

ib::error()
<< "Failed to write to the double write"
" buffer";

ut_free(unaligned_read_buf);

return(err);
goto func_exit;
}
} else if (mach_read_from_8(page + FIL_PAGE_LSN)) {
/* Each valid page header must contain
Expand All @@ -509,9 +489,8 @@ buf_dblwr_init_or_load_pages(
os_file_flush(file);
}

ut_free(unaligned_read_buf);

return(DB_SUCCESS);
err = DB_SUCCESS;
goto func_exit;
}

/** Process and remove the double write buffer pages for all tablespaces. */
Expand All @@ -520,18 +499,14 @@ buf_dblwr_process()
{
ulint page_no_dblwr = 0;
byte* read_buf;
byte* unaligned_read_buf;
recv_dblwr_t& recv_dblwr = recv_sys.dblwr;

if (!buf_dblwr) {
return;
}

unaligned_read_buf = static_cast<byte*>(
ut_malloc_nokey(3U << srv_page_size_shift));

read_buf = static_cast<byte*>(
ut_align(unaligned_read_buf, srv_page_size));
aligned_malloc(2 * srv_page_size, srv_page_size));
byte* const buf = read_buf + srv_page_size;

for (recv_dblwr_t::list::iterator i = recv_dblwr.pages.begin();
Expand Down Expand Up @@ -687,7 +662,7 @@ buf_dblwr_process()
recv_dblwr.pages.clear();

fil_flush_file_spaces(FIL_TYPE_TABLESPACE);
ut_free(unaligned_read_buf);
aligned_free(read_buf);
}

/****************************************************************//**
Expand All @@ -702,15 +677,9 @@ buf_dblwr_free()

os_event_destroy(buf_dblwr->b_event);
os_event_destroy(buf_dblwr->s_event);
ut_free(buf_dblwr->write_buf_unaligned);
buf_dblwr->write_buf_unaligned = NULL;

aligned_free(buf_dblwr->write_buf);
ut_free(buf_dblwr->buf_block_arr);
buf_dblwr->buf_block_arr = NULL;

ut_free(buf_dblwr->in_use);
buf_dblwr->in_use = NULL;

mutex_free(&buf_dblwr->mutex);
ut_free(buf_dblwr);
buf_dblwr = NULL;
Expand Down

0 comments on commit 42a4ae5

Please sign in to comment.