Skip to content

Commit 0177a9c

Browse files
montywiAlexander Barkov
authored andcommitted
Simple binary cache optimizations
- Don't call my_chsize() for small (less than 64K) binary log tmp files - Don't flush cache to disk on reset.
1 parent 1bcfa14 commit 0177a9c

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

sql/log.cc

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
#define MAX_LOG_BUFFER_SIZE 1024
6262
#define MAX_TIME_SIZE 32
6363
#define MY_OFF_T_UNDEF (~(my_off_t)0UL)
64+
/* Truncate cache log files bigger than this */
65+
#define CACHE_FILE_TRUNC_SIZE 65536
6466

6567
#define FLAGSTR(V,F) ((V)&(F)?#F" ":"")
6668

@@ -313,24 +315,19 @@ class binlog_cache_data
313315

314316
void reset()
315317
{
316-
compute_statistics();
317-
truncate(0);
318-
if(cache_log.file != -1)
318+
bool cache_was_empty= empty();
319+
bool truncate_file= (cache_log.file != -1 &&
320+
my_b_write_tell(&cache_log) > CACHE_FILE_TRUNC_SIZE);
321+
truncate(0,1); // Forget what's in cache
322+
if (!cache_was_empty)
323+
compute_statistics();
324+
if (truncate_file)
319325
my_chsize(cache_log.file, 0, 0, MYF(MY_WME));
320326

321327
changes_to_non_trans_temp_table_flag= FALSE;
322328
status= 0;
323329
incident= FALSE;
324330
before_stmt_pos= MY_OFF_T_UNDEF;
325-
/*
326-
The truncate function calls reinit_io_cache that calls
327-
my_b_flush_io_cache which may increase disk_writes. This breaks
328-
the disk_writes use by the binary log which aims to compute the
329-
ratio between in-memory cache usage and disk cache usage. To
330-
avoid this undesirable behavior, we reset the variable after
331-
truncating the cache.
332-
*/
333-
cache_log.disk_writes= 0;
334331
DBUG_ASSERT(empty());
335332
}
336333

@@ -437,11 +434,16 @@ class binlog_cache_data
437434
*/
438435
void compute_statistics()
439436
{
440-
if (!empty())
437+
statistic_increment(*ptr_binlog_cache_use, &LOCK_status);
438+
if (cache_log.disk_writes != 0)
441439
{
442-
statistic_increment(*ptr_binlog_cache_use, &LOCK_status);
443-
if (cache_log.disk_writes != 0)
444-
statistic_increment(*ptr_binlog_cache_disk_use, &LOCK_status);
440+
#ifdef REAL_STATISTICS
441+
statistic_add(*ptr_binlog_cache_disk_use,
442+
cache_log.disk_writes, &LOCK_status);
443+
#else
444+
statistic_increment(*ptr_binlog_cache_disk_use, &LOCK_status);
445+
#endif
446+
cache_log.disk_writes= 0;
445447
}
446448
}
447449

@@ -470,15 +472,15 @@ class binlog_cache_data
470472
It truncates the cache to a certain position. This includes deleting the
471473
pending event.
472474
*/
473-
void truncate(my_off_t pos)
475+
void truncate(my_off_t pos, bool reset_cache=0)
474476
{
475477
DBUG_PRINT("info", ("truncating to position %lu", (ulong) pos));
476478
if (pending())
477479
{
478480
delete pending();
479481
set_pending(0);
480482
}
481-
reinit_io_cache(&cache_log, WRITE_CACHE, pos, 0, 0);
483+
reinit_io_cache(&cache_log, WRITE_CACHE, pos, 0, reset_cache);
482484
cache_log.end_of_file= saved_max_binlog_cache_size;
483485
}
484486

0 commit comments

Comments
 (0)