Fix bug 1234029 (buf_flush_LRU_list_batch() accounts flushed pages
multiple times).

Fix by not having a separate variable for the last batch flushed page
count at all, and passing the running total variable n->flushed to
buf_flush_page_and_try_neigbors() to add to.

At the same time fix the buf_flush_LRU_list_batch() return type to
void to match its use.
Automerge lp:~laurynas-biveinis/percona-server/bug1234029
Fix bug 1234513 (buf_flush_LRU_tail(): incorrect buf_flush_LRU()
return value check and srv_cleaner_eviction_factor handling).

Fixed trivially by removing the else branch from if
(buf_flush_LRU(...)) statement and negating the
(srv_cleaner_eviction_factor ? n.evicted : n.flushed) expression in
the instance flushing stop condition.
Implement relative thread scheduling priorities for XtraDB
(https://blueprints.launchpad.net/percona-server/+spec/xtradb-thread-nice).

Add new command line option innodb_sched_priority_cleaner=0..39,
corresponding to Linux nice values of -20..19.  Add other options
innodb_sched_priority_purge, innodb_sched_priority_io,
innodb_sched_priority_master, hidden under UNIV_PERF_DEBUG.

On setting the new values, silently do nothing if a corresponding
thread or threads are not running.  If they are running, attempt to
set the thread nice priority for the specified thread type and return
a warning with an actual priority if the attempt failed.

As Linux setpriority() accepts as system-specific pid_t thread id,
define a new type os_tid_t.  Implement new functions
os_thread_get_tid() and os_thread_set_priority() that use it.

Introduce a new macro SRV_MAX_N_PURGE_THREADS for the maximum allowed
number of purge threads.

Add new sys_vars test for these variables.
Add new LSN age factor formula for page cleaner adaptive flushing,
implementing
https://blueprints.launchpad.net/percona-server/+spec/af-lsn-age-tuning-5.6.

The formula choice is provided through a new variable
innodb_cleaner_lsn_age_factor, defaulting to HIGH_CHECKPOINT, which
chooses the new formula.  The old formula is available through value
LEGACY.  Other values might be added in the future.

Add a sys_vars test for the new variable.
Automerge lp:~laurynas-biveinis/percona-server/bug1234513
Fix InnoDB server activity accounting for page cleaner heuristics,
implementing
https://blueprints.launchpad.net/percona-server/+spec/xtradb-cleaner-srv-activity-fixes
and fixing bug 1234562 / http://bugs.mysql.com/bug.php?id=70500.

The LRU side of the issue (bug 1234562) is caused by the fact that
page cleaner will not perform LRU flushes for background tasks that
need free pages because they are not bumping server activity.  Fixed
by performing the flushes unconditionally as there appears to be no
link between free page availability and server activity in the first
place.  If the server is truly idle, the LRU flush request will find
full free lists and will not do anything, matching the intended
previous behavior.

The flush list side of the issue is the page cleaner performing
innodb_io_capacity-sized flushes regardless of the background tasks
potentially doing heavy buffer pool I/O at the same time.  In this
case innodb_io_capacity batch size is too large and should be reduced
as if with query threads present.  Fix by adding server activity
counter bumps to background tasks that do a non-trivial amount of
buffer pool writing: purge and change buffer merge.
Automerge lp:~laurynas-biveinis/percona-server/xtradb-thread-nice
Automerge lp:~laurynas-biveinis/percona-server/xtradb-af-lsn-age-tuning-5.6
Implement query thread waits at the sync preflush point, blueprint
https://blueprints.launchpad.net/percona-server/+spec/xtradb-preflush-backoff.

Whenever a query thread needs to perform a sync preflush to proceed,
two options are now available:
1) as previously, the query thread may issue a flush list batch itself
and wait for it to complete.  This is also used whenever the page
cleaner thread is not running.
2) alternatively the query thread may wait until the flush list flush
is performed by the page cleaner thread.  The wait is implemented by
maintaining a wait iteration counter, and a random duration sleep of up
to two to power of loop counter value is performed.  To prevent
runaway sleeps, the counter is reset every 16 iterations.

The behavior is controlled by a new system variable
innodb_foreground_preflush with two possible values "sync_preflush"
and "exponential_backoff".  Add a sys_vars test for this variable.
Automerge lp:~laurynas-biveinis/percona-server/xtradb-srv-activity-fixes
Automerge lp:~laurynas-biveinis/percona-server/xtradb-preflush-backoff
Fix bug 1234848 (sync_array_cell_print() misdirects some output to
stderr) by replacing the misused stderr with file.
Automerge lp:~laurynas-biveinis/percona-server/bug1234848
Fix bug 1224432 (Buffer pool watch sentinel page buf_fix_count race
condition | InnoDB: Failing assertion: buf_fix_count > 0 in file
buf0buf.cc line 3690 | abort (sig=6) in buf_page_init_for_read).

The issue was that LRU list mutex was always released in one of the
code paths unconditionally, but the later watch_page state re-check
only happened if buf_buddy_alloc() had to release the LRU mutex in the
same code path too.  Fix by setting the LRU list mutex release flag in
the affected code path unconditionally.
Implement backoff for free list consumers in the case of empty free
lists, blueprint
https://blueprints.launchpad.net/percona-server/+spec/xtradb-empty-free-list-backoff.

Currently, if buf_LRU_get_free_block() does not find a free page on
the free list, it loops with some wait while trying progressively
harder to get a free page: evict and eventually flush from the LRU
list, still checking the free list on each iteration. Even with the
implemented low-priority free list mutex acquisition for
buf_LRU_get_free_block(), such looping results in free list mutex
contention and LRU list mutex contention.

Fix by making buf_LRU_get_free_block() to skip all the other actions
it does except for the free list checking with a
progressively-increasing wait duration between the checks.  Keep the
old algorithm accessible as an option and also for the code paths
where the cleaner thread is not running.

This is controlled through a new system variable
innodb_empty_free_list_algorithm=legacy|backoff, with backoff being
default.  Add a sys_vars test for this variable.

At the same time, since now all the flushes are done by the cleaner
thread, the following situation is made possible and is thus
addressed.  A query thread may take a page latch and wait for the
cleaner to perform an LRU flush.  Meanwhile the cleaner might be
performing a flush list flush, and attempt to flush the page whose
page latch is held by that query thread.  Before this situation would
have resulted in a single page flush by the query thread, and a latch
release which unblocks the page cleaner.  With all flushing happenning
in page cleaner, this becomes a deadlock.  Fixed by adjusting page
flush readiness condition in buf_flush_read_for_flush() for
BUF_FLUSH_LIST to match that of BUF_FLUSH_LRU, where flushing has been
already avoiding latched pages.

At the same time fix srv_cleaner_flush_chunk_size variable
initializater to match the system variable default for
innodb_cleaner_flush_chunk_size.  The mismatch was visible in regular
vs. UNIV_PERF_DEBUG-compiled binaries.
Automerge lp:~laurynas-biveinis/percona-server/bug1224432
Fix bug 1235114 by tweak LRU flushing timeout: performance experiments
show that the timeout condition should be checked at the topmost
buf_flush_LRU_tail() loop only, that is, the current pass over all
buffer pool instances still must complete.

At the same time fix typo for innodb_cleaner_flush_chunk_size system
variable default value.
Automerge lp:~laurynas-biveinis/percona-server/xtradb-empty-free-list-backoff
Automerge lp:~laurynas-biveinis/percona-server/bug1235114