Commit fbe22b60 authored by unknown's avatar unknown

The pagae cache added.


mysys/Makefile.am:
  the page cache and test proms for it added
include/pagecache.h:
  New BitKeeper file ``include/pagecache.h''
mysys/mf_pagecache.c:
  New BitKeeper file ``mysys/mf_pagecache.c''
mysys/test_file.c:
  New BitKeeper file ``mysys/test_file.c''
mysys/test_file.h:
  New BitKeeper file ``mysys/test_file.h''
mysys/test_pagecache_consist.c:
  New BitKeeper file ``mysys/test_pagecache_consist.c''
mysys/test_pagecache_single.c:
  New BitKeeper file ``mysys/test_pagecache_single.c''
parent c262b880
/* Copyright (C) 2006 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/* Page cache variable structures */
#ifndef _pagecache_h
#define _pagecache_h
C_MODE_START
/* Type of the page */
enum pagecache_page_type
{
#ifndef DBUG_OFF
/* used only for control page type chenging during debugging */
PAGECACHE_EMPTY_PAGE,
#endif
/* the page does not contain LSN */
PAGECACHE_PLAIN_PAGE,
/* the page contain LSN (maria tablespace page) */
PAGECACHE_LSN_PAGE
};
/*
This enum describe lock status changing. every typr of page cache will
interpret WRITE/READ lock as it need.
*/
enum pagecache_page_lock
{
PAGECACHE_LOCK_LEFT_UNLOCKED, /* free -> free */
PAGECACHE_LOCK_LEFT_READLOCKED, /* read -> read */
PAGECACHE_LOCK_LEFT_WRITELOCKED, /* write -> write */
PAGECACHE_LOCK_READ, /* free -> read */
PAGECACHE_LOCK_WRITE, /* free -> write */
PAGECACHE_LOCK_READ_UNLOCK, /* read -> free */
PAGECACHE_LOCK_WRITE_UNLOCK, /* write -> free */
PAGECACHE_LOCK_WRITE_TO_READ /* write -> read */
};
/*
This enum describe pin status changing
*/
enum pagecache_page_pin
{
PAGECACHE_PIN_LEFT_PINNED, /* pinned -> pinned */
PAGECACHE_PIN_LEFT_UNPINNED, /* unpinned -> unpinned */
PAGECACHE_PIN, /* unpinned -> pinned */
PAGECACHE_UNPIN /* pinned -> unpinned */
};
/* How to write the page */
enum pagecache_write_mode
{
/* do not write immediately, i.e. it will be dirty page */
PAGECACHE_WRITE_DELAY,
/* write page to the file and put it to the cache */
PAGECACHE_WRITE_NOW,
/* page already is in the file. (key cache insert analogue) */
PAGECACHE_WRITE_DONE
};
typedef void *PAGECACHE_PAGE_LINK;
/* TODO: move to loghandler emulator */
typedef void LOG_HANDLER;
typedef void *LSN;
/* file descriptor for Maria */
typedef struct st_pagecache_file
{
int file; /* it is for debugging purposes then it will be uint32 file_no */
} PAGECACHE_FILE;
/* page number for maria */
typedef uint32 maria_page_no_t;
/* declare structures that is used by st_pagecache */
struct st_pagecache_block_link;
typedef struct st_pagecache_block_link PAGECACHE_BLOCK_LINK;
struct st_pagecache_page;
typedef struct st_pagecache_page PAGECACHE_PAGE;
struct st_pagecache_hash_link;
typedef struct st_pagecache_hash_link PAGECACHE_HASH_LINK;
/* info about requests in a waiting queue */
typedef struct st_pagecache_wqueue
{
struct st_my_thread_var *last_thread; /* circular list of waiting threads */
} PAGECACHE_WQUEUE;
#define PAGECACHE_CHANGED_BLOCKS_HASH 128 /* must be power of 2 */
/*
The page cache structure
It also contains read-only statistics parameters.
*/
typedef struct st_pagecache
{
my_bool inited;
my_bool resize_in_flush; /* true during flush of resize operation */
my_bool can_be_used; /* usage of cache for read/write is allowed */
uint shift; /* block size = 2 ^ shift */
my_size_t mem_size; /* specified size of the cache memory */
uint32 block_size; /* size of the page buffer of a cache block */
ulong min_warm_blocks; /* min number of warm blocks; */
ulong age_threshold; /* age threshold for hot blocks */
ulonglong time; /* total number of block link operations */
uint hash_entries; /* max number of entries in the hash table */
int hash_links; /* max number of hash links */
int hash_links_used; /* number of hash links currently used */
int disk_blocks; /* max number of blocks in the cache */
ulong blocks_used; /* maximum number of concurrently used blocks */
ulong blocks_unused; /* number of currently unused blocks */
ulong blocks_changed; /* number of currently dirty blocks */
ulong warm_blocks; /* number of blocks in warm sub-chain */
ulong cnt_for_resize_op; /* counter to block resize operation */
long blocks_available; /* number of blocks available in the LRU chain */
PAGECACHE_HASH_LINK **hash_root;/* arr. of entries into hash table buckets */
PAGECACHE_HASH_LINK *hash_link_root;/* memory for hash table links */
PAGECACHE_HASH_LINK *free_hash_list;/* list of free hash links */
PAGECACHE_BLOCK_LINK *free_block_list;/* list of free blocks */
PAGECACHE_BLOCK_LINK *block_root;/* memory for block links */
byte HUGE_PTR *block_mem; /* memory for block buffers */
PAGECACHE_BLOCK_LINK *used_last;/* ptr to the last block of the LRU chain */
PAGECACHE_BLOCK_LINK *used_ins;/* ptr to the insertion block in LRU chain */
pthread_mutex_t cache_lock; /* to lock access to the cache structure */
PAGECACHE_WQUEUE resize_queue; /* threads waiting during resize operation */
PAGECACHE_WQUEUE waiting_for_hash_link;/* waiting for a free hash link */
PAGECACHE_WQUEUE waiting_for_block; /* requests waiting for a free block */
/* hash for dirty file bl.*/
PAGECACHE_BLOCK_LINK *changed_blocks[PAGECACHE_CHANGED_BLOCKS_HASH];
/* hash for other file bl.*/
PAGECACHE_BLOCK_LINK *file_blocks[PAGECACHE_CHANGED_BLOCKS_HASH];
LOG_HANDLER *loghandler; /* loghandler structure */
/*
The following variables are and variables used to hold parameters for
initializing the key cache.
*/
ulonglong param_buff_size; /* size the memory allocated for the cache */
ulong param_block_size; /* size of the blocks in the key cache */
ulong param_division_limit; /* min. percentage of warm blocks */
ulong param_age_threshold; /* determines when hot block is downgraded */
/* Statistics variables. These are reset in reset_key_cache_counters(). */
ulong global_blocks_changed; /* number of currently dirty blocks */
ulonglong global_cache_w_requests;/* number of write requests (write hits) */
ulonglong global_cache_write; /* number of writes from cache to files */
ulonglong global_cache_r_requests;/* number of read requests (read hits) */
ulonglong global_cache_read; /* number of reads from files to cache */
int blocks; /* max number of blocks in the cache */
my_bool in_init; /* Set to 1 in MySQL during init/resize */
} PAGECACHE;
extern int init_pagecache(PAGECACHE *pagecache, my_size_t use_mem,
uint division_limit, uint age_threshold,
uint block_size,
LOG_HANDLER *loghandler);
extern int resize_pagecache(PAGECACHE *pagecache,
my_size_t use_mem, uint division_limit,
uint age_threshold);
extern void change_pagecache_param(PAGECACHE *pagecache, uint division_limit,
uint age_threshold);
extern byte *pagecache_read(PAGECACHE *pagecache,
PAGECACHE_FILE *file,
maria_page_no_t pageno,
uint level,
byte *buff,
enum pagecache_page_type type,
enum pagecache_page_lock lock,
PAGECACHE_PAGE_LINK *link);
extern my_bool pagecache_write(PAGECACHE *pagecache,
PAGECACHE_FILE *file,
maria_page_no_t pageno,
uint level,
byte *buff,
enum pagecache_page_type type,
enum pagecache_page_lock lock,
enum pagecache_page_pin pin,
enum pagecache_write_mode write_mode,
PAGECACHE_PAGE_LINK *link);
void pagecache_unlock_page(PAGECACHE *pagecache,
PAGECACHE_FILE *file,
maria_page_no_t pageno,
enum pagecache_page_lock lock,
enum pagecache_page_pin pin,
my_bool stamp_this_page,
LSN first_REDO_LSN_for_page);
void pagecache_unlock(PAGECACHE *pagecache,
PAGECACHE_PAGE_LINK *link,
enum pagecache_page_lock lock,
enum pagecache_page_pin pin,
my_bool stamp_this_page,
LSN first_REDO_LSN_for_page);
void pagecache_unpin_page(PAGECACHE *pagecache,
PAGECACHE_FILE *file,
maria_page_no_t pageno);
void pagecache_unpin(PAGECACHE *pagecache,
PAGECACHE_PAGE_LINK *link);
extern int flush_pagecache_blocks(PAGECACHE *keycache,
PAGECACHE_FILE *file,
enum flush_type type);
my_bool pagecache_delete_page(PAGECACHE *pagecache,
PAGECACHE_FILE *file,
maria_page_no_t pageno,
enum pagecache_page_lock lock,
my_bool flush);
extern void end_pagecache(PAGECACHE *keycache, my_bool cleanup);
C_MODE_END
#endif /* _keycache_h */
......@@ -54,7 +54,7 @@ libmysys_a_SOURCES = my_init.c my_getwd.c mf_getdate.c my_mmap.c \
my_gethostbyname.c rijndael.c my_aes.c sha1.c \
my_handler.c my_netware.c my_largepage.c \
my_memmem.c \
my_windac.c my_access.c base64.c
my_windac.c my_access.c base64.c mf_pagecache.c
EXTRA_DIST = thr_alarm.c thr_lock.c my_pthread.c my_thr_init.c \
thr_mutex.c thr_rwlock.c \
CMakeLists.txt mf_soundex.c \
......@@ -129,5 +129,61 @@ test_base64$(EXEEXT): base64.c $(LIBRARIES)
$(LINK) $(FLAGS) -DMAIN ./test_base64.c $(LDADD) $(LIBS)
$(RM) -f ./test_base64.c
test_mf_pagecache.o: mf_pagecache.c ../include/pagecache.h $(LIBRARIES)
$(CP) $(srcdir)/mf_pagecache.c test_mf_pagecache.c
$(COMPILE) $(FLAGS) -DPAGECACHE_DEBUG -DEXTRA_DEBUG -c test_mf_pagecache.c
test_file.o: test_file.c test_file.h
$(COMPILE) $(FLAGS) -DPAGECACHE_DEBUG -DEXTRA_DEBUG -c test_file.c
test_pagecache_single1k$(EXEEXT): test_pagecache_single.c test_mf_pagecache.o ../unittest/mytap/tap.o test_file.o $(LIBRARIES)
$(LINK) $(FLAGS) -DMAIN -DPAGECACHE_DEBUG -DPAGE_SIZE=1024 -DEXTRA_DEBUG $(srcdir)/test_pagecache_single.c test_mf_pagecache.o ../unittest/mytap/tap.o test_file.o $(LDADD) $(LIBS)
test_pagecache_single8k$(EXEEXT): test_pagecache_single.c test_mf_pagecache.o ../unittest/mytap/tap.o test_file.o $(LIBRARIES)
$(LINK) $(FLAGS) -DMAIN -DPAGECACHE_DEBUG -DPAGE_SIZE=8192 -DEXTRA_DEBUG $(srcdir)/test_pagecache_single.c test_mf_pagecache.o ../unittest/mytap/tap.o test_file.o $(LDADD) $(LIBS)
test_pagecache_single64k$(EXEEXT): test_pagecache_single.c test_mf_pagecache.o ../unittest/mytap/tap.o test_file.o $(LIBRARIES)
$(LINK) $(FLAGS) -DMAIN -DPAGECACHE_DEBUG -DPAGE_SIZE=65536 -DEXTRA_DEBUG $(srcdir)/test_pagecache_single.c test_mf_pagecache.o ../unittest/mytap/tap.o test_file.o $(LDADD) $(LIBS)
test_pagecache_single: test_pagecache_single1k$(EXEEXT) test_pagecache_single8k$(EXEEXT) test_pagecache_single64k$(EXEEXT)
./test_pagecache_single64k$(EXEEXT)
./test_pagecache_single8k$(EXEEXT)
./test_pagecache_single1k$(EXEEXT)
test_pagecache_consist1k$(EXEEXT): test_pagecache_consist.c test_mf_pagecache.o ../unittest/mytap/tap.o $(LIBRARIES)
$(LINK) $(FLAGS) -DMAIN -DPAGECACHE_DEBUG -DPAGE_SIZE=1024 -DEXTRA_DEBUG $(srcdir)/test_pagecache_consist.c test_mf_pagecache.o ../unittest/mytap/tap.o $(LDADD) $(LIBS)
test_pagecache_consist64k$(EXEEXT): test_pagecache_consist.c test_mf_pagecache.o ../unittest/mytap/tap.o $(LIBRARIES)
$(LINK) $(FLAGS) -DMAIN -DPAGECACHE_DEBUG -DPAGE_SIZE=65536 -DEXTRA_DEBUG $(srcdir)/test_pagecache_consist.c test_mf_pagecache.o ../unittest/mytap/tap.o $(LDADD) $(LIBS)
test_pagecache_consist1kHC$(EXEEXT): test_pagecache_consist.c test_mf_pagecache.o ../unittest/mytap/tap.o $(LIBRARIES)
$(LINK) $(FLAGS) -DMAIN -DPAGECACHE_DEBUG -DTEST_HIGH_CONCURENCY -DPAGE_SIZE=1024 -DEXTRA_DEBUG $(srcdir)/test_pagecache_consist.c test_mf_pagecache.o ../unittest/mytap/tap.o $(LDADD) $(LIBS)
test_pagecache_consist64kHC$(EXEEXT): test_pagecache_consist.c test_mf_pagecache.o ../unittest/mytap/tap.o $(LIBRARIES)
$(LINK) $(FLAGS) -DMAIN -DPAGECACHE_DEBUG -DTEST_HIGH_CONCURENCY -DPAGE_SIZE=65536 -DEXTRA_DEBUG $(srcdir)/test_pagecache_consist.c test_mf_pagecache.o ../unittest/mytap/tap.o $(LDADD) $(LIBS)
test_pagecache_consist1kRD$(EXEEXT): test_pagecache_consist.c test_mf_pagecache.o ../unittest/mytap/tap.o $(LIBRARIES)
$(LINK) $(FLAGS) -DMAIN -DPAGECACHE_DEBUG -DTEST_READERS -DPAGE_SIZE=1024 -DEXTRA_DEBUG $(srcdir)/test_pagecache_consist.c test_mf_pagecache.o ../unittest/mytap/tap.o $(LDADD) $(LIBS)
test_pagecache_consist64kRD$(EXEEXT): test_pagecache_consist.c test_mf_pagecache.o ../unittest/mytap/tap.o $(LIBRARIES)
$(LINK) $(FLAGS) -DMAIN -DPAGECACHE_DEBUG -DTEST_READERS -DPAGE_SIZE=65536 -DEXTRA_DEBUG $(srcdir)/test_pagecache_consist.c test_mf_pagecache.o ../unittest/mytap/tap.o $(LDADD) $(LIBS)
test_pagecache_consist1kWR$(EXEEXT): test_pagecache_consist.c test_mf_pagecache.o ../unittest/mytap/tap.o $(LIBRARIES)
$(LINK) $(FLAGS) -DMAIN -DPAGECACHE_DEBUG -DTEST_WRITERS -DPAGE_SIZE=1024 -DEXTRA_DEBUG $(srcdir)/test_pagecache_consist.c test_mf_pagecache.o ../unittest/mytap/tap.o $(LDADD) $(LIBS)
test_pagecache_consist64kWR$(EXEEXT): test_pagecache_consist.c test_mf_pagecache.o ../unittest/mytap/tap.o $(LIBRARIES)
$(LINK) $(FLAGS) -DMAIN -DPAGECACHE_DEBUG -DTEST_WRITERS -DPAGE_SIZE=65536 -DEXTRA_DEBUG $(srcdir)/test_pagecache_consist.c test_mf_pagecache.o ../unittest/mytap/tap.o $(LDADD) $(LIBS)
test_pagecache_consist: test_pagecache_consist1k$(EXEEXT) test_pagecache_consist64k$(EXEEXT) test_pagecache_consist1kHC$(EXEEXT) test_pagecache_consist64kHC$(EXEEXT) test_pagecache_consist1kRD$(EXEEXT) test_pagecache_consist64kRD$(EXEEXT) test_pagecache_consist1kWR$(EXEEXT) test_pagecache_consist64kWR$(EXEEXT)
./test_pagecache_consist1k$(EXEEXT)
./test_pagecache_consist64k$(EXEEXT)
./test_pagecache_consist1kHC$(EXEEXT)
./test_pagecache_consist64kHC$(EXEEXT)
./test_pagecache_consist1kRD$(EXEEXT)
./test_pagecache_consist64kRD$(EXEEXT)
./test_pagecache_consist1kWR$(EXEEXT)
./test_pagecache_consist64kWR$(EXEEXT)
# Don't update the files from bitkeeper
%::SCCS/s.%
This source diff could not be displayed because it is too large. You can view the blob instead.
#include "mysys_priv.h"
#include "my_dir.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <pagecache.h>
#include "test_file.h"
/*
Check that file contance correspond to descriptor
SYNOPSIS
test_file()
file File to test
file_name Path (and name) of file which is tested
size size of file
buff_size size of buffer which is enought to check the file
desc file descriptor to check with
RETURN
1 file if OK
0 error
*/
int test_file(PAGECACHE_FILE file, char *file_name,
off_t size, size_t buff_size, struct file_desc *desc)
{
MY_STAT stat_buff, *stat;
unsigned char *buffr= malloc(buff_size);
off_t pos= 0;
size_t byte;
int step= 0;
if ((stat= my_stat(file_name, &stat_buff, MYF(0))) == NULL)
{
diag("Can't stat() %s (errno: %d)\n", file_name, errno);
return 0;
}
if (stat->st_size != size)
{
diag("file %s size is %lu (should be %lu)\n",
file_name, (ulong) stat->st_size, (ulong) size);
return 0;
}
/* check content */
my_seek(file.file, 0, SEEK_SET, MYF(0));
while (desc[step].length != 0)
{
if (my_read(file.file, (char*)buffr, desc[step].length, MYF(0)) !=
desc[step].length)
{
diag("Can't read %u bytes from %s (errno: %d)\n",
(uint)desc[step].length, file_name, errno);
return 0;
}
for (byte= 0; byte < desc[step].length; byte++)
{
if (buffr[byte] != desc[step].content)
{
diag("content of %s mismatch 0x%x in position %lu instead of 0x%x\n",
file_name, (uint) buffr[byte], (ulong) (pos + byte),
desc[step].content);
return 0;
}
}
pos+= desc[step].length;
step++;
}
return 1;
}
#include <pagecache.h>
/*
File content descriptor
*/
struct file_desc
{
unsigned int length;
unsigned char content;
};
int test_file(PAGECACHE_FILE file, char *file_name,
off_t size, size_t buff_size, struct file_desc *desc);
#include "mysys_priv.h"
#include "../include/my_pthread.h"
#include "../include/pagecache.h"
#include <string.h>
#include "my_dir.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include "../unittest/mytap/tap.h"
#include <my_pthread.h>
/*#define PAGE_SIZE 65536*/
#define PCACHE_SIZE (PAGE_SIZE*1024*20)
#ifndef DBUG_OFF
static const char* default_dbug_option;
#endif
static char *file1_name= (char*)"page_cache_test_file_1";
static PAGECACHE_FILE file1;
static pthread_cond_t COND_thread_count;
static pthread_mutex_t LOCK_thread_count;
static uint thread_count;
static PAGECACHE pagecache;
#ifdef TEST_HIGH_CONCURENCY
static uint number_of_readers= 10;
static uint number_of_writers= 20;
static uint number_of_tests= 30000;
static uint record_length_limit= PAGE_SIZE/200;
static uint number_of_pages= 20;
static uint flush_divider= 1000;
#else /*TEST_HIGH_CONCURENCY*/
#ifdef TEST_READERS
static uint number_of_readers= 10;
static uint number_of_writers= 1;
static uint number_of_tests= 30000;
static uint record_length_limit= PAGE_SIZE/200;
static uint number_of_pages= 20;
static uint flush_divider= 1000;
#else /*TEST_READERS*/
#ifdef TEST_WRITERS
static uint number_of_readers= 0;
static uint number_of_writers= 10;
static uint number_of_tests= 30000;
static uint record_length_limit= PAGE_SIZE/200;
static uint number_of_pages= 20;
static uint flush_divider= 1000;
#else /*TEST_WRITERS*/
static uint number_of_readers= 10;
static uint number_of_writers= 10;
static uint number_of_tests= 50000;
static uint record_length_limit= PAGE_SIZE/200;
static uint number_of_pages= 20000;
static uint flush_divider= 1000;
#endif /*TEST_WRITERS*/
#endif /*TEST_READERS*/
#endif /*TEST_HIGH_CONCURENCY*/
/* check page consistemcy */
uint check_page(uchar *buff, ulong offset, int page_locked, int page_no,
int tag)
{
uint end= sizeof(uint);
uint num= *((uint *)buff);
uint i;
DBUG_ENTER("check_page");
for (i= 0; i < num; i++)
{
uint len= *((uint *)(buff + end));
uint j;
end+= sizeof(uint)+ sizeof(uint);
if (len + end > PAGE_SIZE)
{
diag("incorrect field header #%u by offset %lu\n", i, offset + end + j);
goto err;
}
for(j= 0; j < len; j++)
{
if (buff[end + j] != (uchar)((i+1) % 256))
{
diag("incorrect %lu byte\n", offset + end + j);
goto err;
}
}
end+= len;
}
for(i= end; i < PAGE_SIZE; i++)
{
if (buff[i] != 0)
{
int h;
DBUG_PRINT("err",
("byte %lu (%lu + %u), page %u (%s, end: %u, recs: %u, tag: %d) should be 0\n",
offset + i, offset, i, page_no,
(page_locked ? "locked" : "unlocked"),
end, num, tag));
diag("byte %lu (%lu + %u), page %u (%s, end: %u, recs: %u, tag: %d) should be 0\n",
offset + i, offset, i, page_no,
(page_locked ? "locked" : "unlocked"),
end, num, tag);
h= my_open("wrong_page", O_CREAT | O_TRUNC | O_RDWR, MYF(0));
my_pwrite(h, buff, PAGE_SIZE, 0, MYF(0));
my_close(h, MYF(0));
goto err;
}
}
DBUG_RETURN(end);
err:
DBUG_PRINT("err", ("try to flush"));
if (page_locked)
{
pagecache_delete_page(&pagecache, &file1, page_no,
PAGECACHE_LOCK_LEFT_WRITELOCKED, 1);
}
else
{
flush_pagecache_blocks(&pagecache, &file1, FLUSH_RELEASE);
}
exit(1);
}
void put_rec(uchar *buff, uint end, uint len, uint tag)
{
uint i;
uint num= *((uint *)buff);
if (!len)
len= 1;
if (end + sizeof(uint)*2 + len > PAGE_SIZE)
return;
*((uint *)(buff + end))= len;
end+= sizeof(uint);
*((uint *)(buff + end))= tag;
end+= sizeof(uint);
num++;
*((uint *)buff)= num;
*((uint*)(buff + end))= len;
for (i= end; i < (len + end); i++)
{
buff[i]= (uchar) num % 256;
}
}
/*
Recreate and reopen a file for test
SYNOPSIS
reset_file()
file File to reset
file_name Path (and name) of file which should be reset
*/
void reset_file(PAGECACHE_FILE file, char *file_name)
{
flush_pagecache_blocks(&pagecache, &file1, FLUSH_RELEASE);
if (my_close(file1.file, MYF(0)) != 0)
{
diag("Got error during %s closing from close() (errno: %d)\n",
file_name, errno);
exit(1);
}
my_delete(file_name, MYF(0));
if ((file.file= my_open(file_name,
O_CREAT | O_TRUNC | O_RDWR, MYF(0))) == -1)
{
diag("Got error during %s creation from open() (errno: %d)\n",
file_name, errno);
exit(1);
}
}
void reader(int num)
{
unsigned char *buffr= malloc(PAGE_SIZE);
uint i;
for (i= 0; i < number_of_tests; i++)
{
uint page= rand()/(RAND_MAX/number_of_pages);
pagecache_read(&pagecache, &file1, page, 3, (char*)buffr,
PAGECACHE_PLAIN_PAGE,
PAGECACHE_LOCK_LEFT_UNLOCKED,
0);
check_page(buffr, page * PAGE_SIZE, 0, page, -num);
if (i % 500 == 0)
printf("reader%d: %d\n", num, i);
}
printf("reader%d: done\n", num);
free(buffr);
}
void writer(int num)
{
unsigned char *buffr= malloc(PAGE_SIZE);
uint i;
for (i= 0; i < number_of_tests; i++)
{
uint end;
uint page= rand()/(RAND_MAX/number_of_pages);
pagecache_read(&pagecache, &file1, page, 3, (char*)buffr,
PAGECACHE_PLAIN_PAGE,
PAGECACHE_LOCK_WRITE,
0);
end= check_page(buffr, page * PAGE_SIZE, 1, page, num);
put_rec(buffr, end, rand()/(RAND_MAX/record_length_limit), num);
pagecache_write(&pagecache, &file1, page, 3, (char*)buffr,
PAGECACHE_PLAIN_PAGE,
PAGECACHE_LOCK_WRITE_UNLOCK,
PAGECACHE_UNPIN,
PAGECACHE_WRITE_DELAY,
0);
if (i % flush_divider == 0)
flush_pagecache_blocks(&pagecache, &file1, FLUSH_FORCE_WRITE);
if (i % 500 == 0)
printf("writer%d: %d\n", num, i);
}
printf("writer%d: done\n", num);
free(buffr);
}
static void *test_thread_reader(void *arg)
{
int param=*((int*) arg);
my_thread_init();
DBUG_ENTER("test_reader");
DBUG_PRINT("enter", ("param: %d", param));
reader(param);
DBUG_PRINT("info", ("Thread %s ended\n", my_thread_name()));
pthread_mutex_lock(&LOCK_thread_count);
thread_count--;
VOID(pthread_cond_signal(&COND_thread_count)); /* Tell main we are ready */
pthread_mutex_unlock(&LOCK_thread_count);
free((gptr) arg);
my_thread_end();
DBUG_RETURN(0);
}
static void *test_thread_writer(void *arg)
{
int param=*((int*) arg);
my_thread_init();
DBUG_ENTER("test_writer");
DBUG_PRINT("enter", ("param: %d", param));
writer(param);
DBUG_PRINT("info", ("Thread %s ended\n", my_thread_name()));
pthread_mutex_lock(&LOCK_thread_count);
thread_count--;
VOID(pthread_cond_signal(&COND_thread_count)); /* Tell main we are ready */
pthread_mutex_unlock(&LOCK_thread_count);
free((gptr) arg);
my_thread_end();
DBUG_RETURN(0);
}
int main(int argc, char **argv __attribute__((unused)))
{
pthread_t tid;
pthread_attr_t thr_attr;
int *param, error, pagen;
MY_INIT(argv[0]);
#ifndef DBUG_OFF
#if defined(__WIN__)
default_dbug_option= "d:t:i:O,\\test_pagecache_consist.trace";
#else
default_dbug_option= "d:t:i:o,/tmp/test_pagecache_consist.trace";
#endif
if (argc > 1)
{
DBUG_SET(default_dbug_option);
DBUG_SET_INITIAL(default_dbug_option);
}
#endif
DBUG_ENTER("main");
DBUG_PRINT("info", ("Main thread: %s\n", my_thread_name()));
if ((file1.file= my_open(file1_name,
O_CREAT | O_TRUNC | O_RDWR, MYF(0))) == -1)
{
fprintf(stderr, "Got error during file1 creation from open() (errno: %d)\n",
errno);
exit(1);
}
DBUG_PRINT("info", ("file1: %d", file1.file));
if (chmod(file1_name, S_IRWXU | S_IRWXG | S_IRWXO) != 0)
{
fprintf(stderr, "Got error during file1 chmod() (errno: %d)\n",
errno);
exit(1);
}
my_pwrite(file1.file, "test file", 9, 0, MYF(0));
if ((error= pthread_cond_init(&COND_thread_count, NULL)))
{
fprintf(stderr, "COND_thread_count: %d from pthread_cond_init (errno: %d)\n",
error, errno);
exit(1);
}
if ((error= pthread_mutex_init(&LOCK_thread_count, MY_MUTEX_INIT_FAST)))
{
fprintf(stderr, "LOCK_thread_count: %d from pthread_cond_init (errno: %d)\n",
error, errno);
exit(1);
}
if ((error= pthread_attr_init(&thr_attr)))
{
fprintf(stderr,"Got error: %d from pthread_attr_init (errno: %d)\n",
error,errno);
exit(1);
}
if ((error= pthread_attr_setdetachstate(&thr_attr, PTHREAD_CREATE_DETACHED)))
{
fprintf(stderr,
"Got error: %d from pthread_attr_setdetachstate (errno: %d)\n",
error,errno);
exit(1);
}
#ifndef pthread_attr_setstacksize /* void return value */
if ((error= pthread_attr_setstacksize(&thr_attr, 65536L)))
{
fprintf(stderr,"Got error: %d from pthread_attr_setstacksize (errno: %d)\n",
error,errno);
exit(1);
}
#endif
#ifdef HAVE_THR_SETCONCURRENCY
VOID(thr_setconcurrency(2));
#endif
my_thread_global_init();
if ((pagen= init_pagecache(&pagecache, PCACHE_SIZE, 0, 0,
PAGE_SIZE, 0)) == 0)
{
fprintf(stderr,"Got error: init_pagecache() (errno: %d)\n",
errno);
exit(1);
}
DBUG_PRINT("info", ("Page cache %d pages", pagen));
{
unsigned char *buffr= malloc(PAGE_SIZE);
uint i;
memset(buffr, '\0', PAGE_SIZE);
for (i= 0; i < number_of_pages; i++)
{
pagecache_write(&pagecache, &file1, i, 3, (char*)buffr,
PAGECACHE_PLAIN_PAGE,
PAGECACHE_LOCK_LEFT_UNLOCKED,
PAGECACHE_PIN_LEFT_UNPINNED,
PAGECACHE_WRITE_DELAY,
0);
}
flush_pagecache_blocks(&pagecache, &file1, FLUSH_FORCE_WRITE);
free(buffr);
}
if ((error= pthread_mutex_lock(&LOCK_thread_count)))
{
fprintf(stderr,"LOCK_thread_count: %d from pthread_mutex_lock (errno: %d)\n",
error,errno);
exit(1);
}
while (number_of_readers != 0 || number_of_writers != 0)
{
if (number_of_readers != 0)
{
param=(int*) malloc(sizeof(int));
*param= number_of_readers;
if ((error= pthread_create(&tid, &thr_attr, test_thread_reader,
(void*) param)))
{
fprintf(stderr,"Got error: %d from pthread_create (errno: %d)\n",
error,errno);
exit(1);
}
thread_count++;
number_of_readers--;
}
if (number_of_writers != 0)
{
param=(int*) malloc(sizeof(int));
*param= number_of_writers;
if ((error= pthread_create(&tid, &thr_attr, test_thread_writer,
(void*) param)))
{
fprintf(stderr,"Got error: %d from pthread_create (errno: %d)\n",
error,errno);
exit(1);
}
thread_count++;
number_of_writers--;
}
}
DBUG_PRINT("info", ("Thread started"));
pthread_mutex_unlock(&LOCK_thread_count);
pthread_attr_destroy(&thr_attr);
/* wait finishing */
if ((error= pthread_mutex_lock(&LOCK_thread_count)))
fprintf(stderr,"LOCK_thread_count: %d from pthread_mutex_lock\n",error);
while (thread_count)
{
if ((error= pthread_cond_wait(&COND_thread_count,&LOCK_thread_count)))
fprintf(stderr,"COND_thread_count: %d from pthread_cond_wait\n",error);
}
if ((error= pthread_mutex_unlock(&LOCK_thread_count)))
fprintf(stderr,"LOCK_thread_count: %d from pthread_mutex_unlock\n",error);
DBUG_PRINT("info", ("thread ended"));
end_pagecache(&pagecache, 1);
DBUG_PRINT("info", ("Page cache ended"));
if (my_close(file1.file, MYF(0)) != 0)
{
fprintf(stderr, "Got error during file1 closing from close() (errno: %d)\n",
errno);
exit(1);
}
/*my_delete(file1_name, MYF(0));*/
my_thread_global_end();
DBUG_PRINT("info", ("file1 (%d) closed", file1.file));
DBUG_PRINT("info", ("Program end"));
DBUG_RETURN(0);
}
#include "mysys_priv.h"
#include "../include/my_pthread.h"
#include "../include/pagecache.h"
#include "my_dir.h"
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include "../unittest/mytap/tap.h"
#include "test_file.h"
/* #define PAGE_SIZE 1024 */
#define PCACHE_SIZE (PAGE_SIZE*1024*10)
#ifndef DBUG_OFF
static const char* default_dbug_option;
#endif
static char *file1_name= (char*)"page_cache_test_file_1";
static PAGECACHE_FILE file1;
static pthread_cond_t COND_thread_count;
static pthread_mutex_t LOCK_thread_count;
static uint thread_count;
static PAGECACHE pagecache;
/*
File contance descriptors
*/
static struct file_desc simple_read_write_test_file[]=
{
{PAGE_SIZE, '\1'},
{ 0, 0}
};
static struct file_desc simple_read_change_write_read_test_file[]=
{
{PAGE_SIZE/2, '\65'},
{PAGE_SIZE/2, '\1'},
{ 0, 0}
};
static struct file_desc simple_pin_test_file1[]=
{
{PAGE_SIZE*2, '\1'},
{ 0, 0}
};
static struct file_desc simple_pin_test_file2[]=
{
{PAGE_SIZE/2, '\1'},
{PAGE_SIZE/2, (unsigned char)129},
{PAGE_SIZE, '\1'},
{ 0, 0}
};
static struct file_desc simple_delete_forget_test_file[]=
{
{PAGE_SIZE, '\1'},
{ 0, 0}
};
static struct file_desc simple_delete_flush_test_file[]=
{
{PAGE_SIZE, '\2'},
{ 0, 0}
};
/*
Recreate and reopen a file for test
SYNOPSIS
reset_file()
file File to reset
file_name Path (and name) of file which should be reset
*/
void reset_file(PAGECACHE_FILE file, char *file_name)
{
flush_pagecache_blocks(&pagecache, &file1, FLUSH_RELEASE);
if (my_close(file1.file, MYF(0)) != 0)
{
diag("Got error during %s closing from close() (errno: %d)\n",
file_name, errno);
exit(1);
}
my_delete(file_name, MYF(0));
if ((file.file= my_open(file_name,
O_CREAT | O_TRUNC | O_RDWR, MYF(0))) == -1)
{
diag("Got error during %s creation from open() (errno: %d)\n",
file_name, errno);
exit(1);
}
}
/*
Write then read page, check file on disk
*/
int simple_read_write_test()
{
unsigned char *buffw= malloc(PAGE_SIZE);
unsigned char *buffr= malloc(PAGE_SIZE);
int res;
DBUG_ENTER("simple_read_write_test");
memset(buffw, '\1', PAGE_SIZE);
pagecache_write(&pagecache, &file1, 0, 3, (char*)buffw,
PAGECACHE_PLAIN_PAGE,
PAGECACHE_LOCK_LEFT_UNLOCKED,
PAGECACHE_PIN_LEFT_UNPINNED,
PAGECACHE_WRITE_DELAY,
0);
pagecache_read(&pagecache, &file1, 0, 3, (char*)buffr,
PAGECACHE_PLAIN_PAGE,
PAGECACHE_LOCK_LEFT_UNLOCKED,
0);
ok((res= test(memcmp(buffr, buffw, PAGE_SIZE) == 0)),
"Simple write-read page ");
flush_pagecache_blocks(&pagecache, &file1, FLUSH_FORCE_WRITE);
ok((res&= test(test_file(file1, file1_name, PAGE_SIZE, PAGE_SIZE,
simple_read_write_test_file))),
"Simple write-read page file");
if (res)
reset_file(file1, file1_name);
free(buffw);
free(buffr);
DBUG_RETURN(res);
}
/*
Prepare page, then read (and lock), change (write new value and unlock),
then check the page in the cache and on the disk
*/
int simple_read_change_write_read_test()
{
unsigned char *buffw= malloc(PAGE_SIZE);
unsigned char *buffr= malloc(PAGE_SIZE);
int res;
DBUG_ENTER("simple_read_change_write_read_test");
/* prepare the file */
memset(buffw, '\1', PAGE_SIZE);
pagecache_write(&pagecache, &file1, 0, 3, (char*)buffw,
PAGECACHE_PLAIN_PAGE,
PAGECACHE_LOCK_LEFT_UNLOCKED,
PAGECACHE_PIN_LEFT_UNPINNED,
PAGECACHE_WRITE_DELAY,
0);
flush_pagecache_blocks(&pagecache, &file1, FLUSH_FORCE_WRITE);
/* test */
pagecache_read(&pagecache, &file1, 0, 3, (char*)buffw,
PAGECACHE_PLAIN_PAGE,
PAGECACHE_LOCK_WRITE,
0);
memset(buffw, '\65', PAGE_SIZE/2);
pagecache_write(&pagecache, &file1, 0, 3, (char*)buffw,
PAGECACHE_PLAIN_PAGE,
PAGECACHE_LOCK_WRITE_UNLOCK,
PAGECACHE_UNPIN,
PAGECACHE_WRITE_DELAY,
0);
pagecache_read(&pagecache, &file1, 0, 3, (char*)buffr,
PAGECACHE_PLAIN_PAGE,
PAGECACHE_LOCK_LEFT_UNLOCKED,
0);
ok((res= test(memcmp(buffr, buffw, PAGE_SIZE) == 0)),
"Simple read-change-write-read page ");
flush_pagecache_blocks(&pagecache, &file1, FLUSH_FORCE_WRITE);
ok((res&= test(test_file(file1, file1_name, PAGE_SIZE, PAGE_SIZE,
simple_read_change_write_read_test_file))),
"Simple read-change-write-read page file");
if (res)
reset_file(file1, file1_name);
free(buffw);
free(buffr);
DBUG_RETURN(res);
}
/*
Prepare page, read page 0 (and pin) then write page 1 and page 0.
Flush the file (shold flush only page 1 and return 1 (page 0 is
still pinned).
Check file on the disk.
Unpin and flush.
Check file on the disk.
*/
int simple_pin_test()
{
unsigned char *buffw= malloc(PAGE_SIZE);
unsigned char *buffr= malloc(PAGE_SIZE);
int res;
DBUG_ENTER("simple_pin_test");
/* prepare the file */
memset(buffw, '\1', PAGE_SIZE);
pagecache_write(&pagecache, &file1, 0, 3, (char*)buffw,
PAGECACHE_PLAIN_PAGE,
PAGECACHE_LOCK_LEFT_UNLOCKED,
PAGECACHE_PIN_LEFT_UNPINNED,
PAGECACHE_WRITE_DELAY,
0);
/* test */
if (flush_pagecache_blocks(&pagecache, &file1, FLUSH_FORCE_WRITE))
{
diag("error in flush_pagecache_blocks\n");
exit(1);
}
pagecache_read(&pagecache, &file1, 0, 3, (char*)buffw,
PAGECACHE_PLAIN_PAGE,
PAGECACHE_LOCK_WRITE,
0);
pagecache_write(&pagecache, &file1, 1, 3, (char*)buffw,
PAGECACHE_PLAIN_PAGE,
PAGECACHE_LOCK_LEFT_UNLOCKED,
PAGECACHE_PIN_LEFT_UNPINNED,
PAGECACHE_WRITE_DELAY,
0);
memset(buffw + PAGE_SIZE/2, ((unsigned char) 129), PAGE_SIZE/2);
pagecache_write(&pagecache, &file1, 0, 3, (char*)buffw,
PAGECACHE_PLAIN_PAGE,
PAGECACHE_LOCK_WRITE_TO_READ,
PAGECACHE_PIN_LEFT_PINNED,
PAGECACHE_WRITE_DELAY,
0);
/*
We have to get error because one page of the file is pinned,
other page should be flushed
*/
if (!flush_pagecache_blocks(&pagecache, &file1, FLUSH_FORCE_WRITE))
{
diag("Did not get error in flush_pagecache_blocks\n");
res= 0;
goto err;
}
ok((res= test(test_file(file1, file1_name, PAGE_SIZE*2, PAGE_SIZE*2,
simple_pin_test_file1))),
"Simple pin page file with pin");
pagecache_unlock_page(&pagecache,
&file1,
0,
PAGECACHE_LOCK_READ_UNLOCK,
PAGECACHE_UNPIN,
0, 0);
if (flush_pagecache_blocks(&pagecache, &file1, FLUSH_FORCE_WRITE))
{
diag("Got error in flush_pagecache_blocks\n");
res= 0;
goto err;
}
ok((res&= test(test_file(file1, file1_name, PAGE_SIZE*2, PAGE_SIZE,
simple_pin_test_file2))),
"Simple pin page result file");
if (res)
reset_file(file1, file1_name);
err:
free(buffw);
free(buffr);
DBUG_RETURN(res);
}
/*
Prepare page, write new value, then delete page from cache without flush,
on the disk should be page with old content written during preparation
*/
int simple_delete_forget_test()
{
unsigned char *buffw= malloc(PAGE_SIZE);
unsigned char *buffr= malloc(PAGE_SIZE);
int res;
DBUG_ENTER("simple_delete_forget_test");
/* prepare the file */
memset(buffw, '\1', PAGE_SIZE);
pagecache_write(&pagecache, &file1, 0, 3, (char*)buffw,
PAGECACHE_PLAIN_PAGE,
PAGECACHE_LOCK_LEFT_UNLOCKED,
PAGECACHE_PIN_LEFT_UNPINNED,
PAGECACHE_WRITE_DELAY,
0);
flush_pagecache_blocks(&pagecache, &file1, FLUSH_FORCE_WRITE);
/* test */
memset(buffw, '\2', PAGE_SIZE);
pagecache_write(&pagecache, &file1, 0, 3, (char*)buffw,
PAGECACHE_PLAIN_PAGE,
PAGECACHE_LOCK_LEFT_UNLOCKED,
PAGECACHE_PIN_LEFT_UNPINNED,
PAGECACHE_WRITE_DELAY,
0);
pagecache_delete_page(&pagecache, &file1, 0,
PAGECACHE_LOCK_WRITE, 0);
flush_pagecache_blocks(&pagecache, &file1, FLUSH_FORCE_WRITE);
ok((res= test(test_file(file1, file1_name, PAGE_SIZE, PAGE_SIZE,
simple_delete_forget_test_file))),
"Simple delete-forget page file");
if (res)
reset_file(file1, file1_name);
free(buffw);
free(buffr);
DBUG_RETURN(res);
}
/*
Prepare page with locking, write new content to the page,
delete page with flush and on existing lock,
check that page on disk contain new value.
*/
int simple_delete_flush_test()
{
unsigned char *buffw= malloc(PAGE_SIZE);
unsigned char *buffr= malloc(PAGE_SIZE);
int res;
DBUG_ENTER("simple_delete_flush_test");
/* prepare the file */
memset(buffw, '\1', PAGE_SIZE);
pagecache_write(&pagecache, &file1, 0, 3, (char*)buffw,
PAGECACHE_PLAIN_PAGE,
PAGECACHE_LOCK_WRITE,
PAGECACHE_PIN,
PAGECACHE_WRITE_DELAY,
0);
flush_pagecache_blocks(&pagecache, &file1, FLUSH_FORCE_WRITE);
/* test */
memset(buffw, '\2', PAGE_SIZE);
pagecache_write(&pagecache, &file1, 0, 3, (char*)buffw,
PAGECACHE_PLAIN_PAGE,
PAGECACHE_LOCK_LEFT_WRITELOCKED,
PAGECACHE_PIN_LEFT_PINNED,
PAGECACHE_WRITE_DELAY,
0);
pagecache_delete_page(&pagecache, &file1, 0,
PAGECACHE_LOCK_LEFT_WRITELOCKED, 1);
flush_pagecache_blocks(&pagecache, &file1, FLUSH_FORCE_WRITE);
ok((res= test(test_file(file1, file1_name, PAGE_SIZE, PAGE_SIZE,
simple_delete_flush_test_file))),
"Simple delete-forget page file");
if (res)
reset_file(file1, file1_name);
free(buffw);
free(buffr);
DBUG_RETURN(res);
}
/*
write then read file bigger then cache
*/
int simple_big_test()
{
unsigned char *buffw= (unsigned char *)malloc(PAGE_SIZE);
unsigned char *buffr= (unsigned char *)malloc(PAGE_SIZE);
struct file_desc *desc=
(struct file_desc *)malloc((PCACHE_SIZE/(PAGE_SIZE/2)) *
sizeof(struct file_desc));
int res, i;
DBUG_ENTER("simple_big_test");
/* prepare the file twice larger then cache */
for (i= 0; i < PCACHE_SIZE/(PAGE_SIZE/2); i++)
{
memset(buffw, (unsigned char) (i & 0xff), PAGE_SIZE);
desc[i].length= PAGE_SIZE;
desc[i].content= (i & 0xff);
pagecache_write(&pagecache, &file1, i, 3, (char*)buffw,
PAGECACHE_PLAIN_PAGE,
PAGECACHE_LOCK_LEFT_UNLOCKED,
PAGECACHE_PIN_LEFT_UNPINNED,
PAGECACHE_WRITE_DELAY,
0);
}
ok(1, "Simple big file write");
/* check written pages sequentally read */
for (i= 0; i < PCACHE_SIZE/(PAGE_SIZE/2); i++)
{
int j;
pagecache_read(&pagecache, &file1, i, 3, (char*)buffr,
PAGECACHE_PLAIN_PAGE,
PAGECACHE_LOCK_LEFT_UNLOCKED,
0);
for(j= 0; j < PAGE_SIZE; j++)
{
if (buffr[j] != (i & 0xff))
{
diag("simple_big_test seq: page %u byte %u mismatch\n", i, j);
return 0;
}
}
}
ok(1, "simple big file sequentally read");
/* chack random reads */
for (i= 0; i < PCACHE_SIZE/(PAGE_SIZE); i++)
{
int j, page;
page= rand() % (PCACHE_SIZE/(PAGE_SIZE/2));
pagecache_read(&pagecache, &file1, page, 3, (char*)buffr,
PAGECACHE_PLAIN_PAGE,
PAGECACHE_LOCK_LEFT_UNLOCKED,
0);
for(j= 0; j < PAGE_SIZE; j++)
{
if (buffr[j] != (page & 0xff))
{
diag("simple_big_test rnd: page %u byte %u mismatch\n", page, j);
return 0;
}
}
}
ok(1, "simple big file random read");
flush_pagecache_blocks(&pagecache, &file1, FLUSH_FORCE_WRITE);
ok((res= test(test_file(file1, file1_name, PCACHE_SIZE*2, PAGE_SIZE,
desc))),
"Simple big file");
if (res)
reset_file(file1, file1_name);
free(buffw);
free(buffr);
DBUG_RETURN(res);
}
/*
Thread function
*/
static void *test_thread(void *arg)
{
int param=*((int*) arg);
my_thread_init();
DBUG_ENTER("test_thread");
DBUG_PRINT("enter", ("param: %d", param));
if (!simple_read_write_test() ||
!simple_read_change_write_read_test() ||
!simple_pin_test() ||
!simple_delete_forget_test() ||
!simple_delete_flush_test() ||
!simple_big_test())
exit(1);
DBUG_PRINT("info", ("Thread %s ended\n", my_thread_name()));
pthread_mutex_lock(&LOCK_thread_count);
thread_count--;
VOID(pthread_cond_signal(&COND_thread_count)); /* Tell main we are ready */
pthread_mutex_unlock(&LOCK_thread_count);
free((gptr) arg);
my_thread_end();
DBUG_RETURN(0);
}
int main(int argc, char **argv __attribute__((unused)))
{
pthread_t tid;
pthread_attr_t thr_attr;
int *param, error, pagen;
MY_INIT(argv[0]);
#ifndef DBUG_OFF
#if defined(__WIN__)
default_dbug_option= "d:t:i:O,\\test_pagecache_single.trace";
#else
default_dbug_option= "d:t:i:o,/tmp/test_pagecache_single.trace";
#endif
if (argc > 1)
{
DBUG_SET(default_dbug_option);
DBUG_SET_INITIAL(default_dbug_option);
}
#endif
DBUG_ENTER("main");
DBUG_PRINT("info", ("Main thread: %s\n", my_thread_name()));
if ((file1.file= my_open(file1_name,
O_CREAT | O_TRUNC | O_RDWR, MYF(0))) == -1)
{
fprintf(stderr, "Got error during file1 creation from open() (errno: %d)\n",
errno);
exit(1);
}
DBUG_PRINT("info", ("file1: %d", file1.file));
if (chmod(file1_name, S_IRWXU | S_IRWXG | S_IRWXO) != 0)
{
fprintf(stderr, "Got error during file1 chmod() (errno: %d)\n",
errno);
exit(1);
}
my_pwrite(file1.file, "test file", 9, 0, MYF(0));
if ((error= pthread_cond_init(&COND_thread_count, NULL)))
{
fprintf(stderr, "Got error: %d from pthread_cond_init (errno: %d)\n",
error, errno);
exit(1);
}
if ((error= pthread_mutex_init(&LOCK_thread_count, MY_MUTEX_INIT_FAST)))
{
fprintf(stderr, "Got error: %d from pthread_cond_init (errno: %d)\n",
error, errno);
exit(1);
}
if ((error= pthread_attr_init(&thr_attr)))
{
fprintf(stderr,"Got error: %d from pthread_attr_init (errno: %d)\n",
error,errno);
exit(1);
}
if ((error= pthread_attr_setdetachstate(&thr_attr, PTHREAD_CREATE_DETACHED)))
{
fprintf(stderr,
"Got error: %d from pthread_attr_setdetachstate (errno: %d)\n",
error,errno);
exit(1);
}
#ifndef pthread_attr_setstacksize /* void return value */
if ((error= pthread_attr_setstacksize(&thr_attr, 65536L)))
{
fprintf(stderr,"Got error: %d from pthread_attr_setstacksize (errno: %d)\n",
error,errno);
exit(1);
}
#endif
#ifdef HAVE_THR_SETCONCURRENCY
VOID(thr_setconcurrency(2));
#endif
my_thread_global_init();
if ((pagen= init_pagecache(&pagecache, PCACHE_SIZE, 0, 0,
PAGE_SIZE, 0)) == 0)
{
fprintf(stderr,"Got error: init_pagecache() (errno: %d)\n",
errno);
exit(1);
}
DBUG_PRINT("info", ("Page cache %d pages", pagen));
if ((error=pthread_mutex_lock(&LOCK_thread_count)))
{
fprintf(stderr,"Got error: %d from pthread_mutex_lock (errno: %d)\n",
error,errno);
exit(1);
}
param=(int*) malloc(sizeof(int));
*param= 1;
if ((error= pthread_create(&tid, &thr_attr, test_thread, (void*) param)))
{
fprintf(stderr,"Got error: %d from pthread_create (errno: %d)\n",
error,errno);
exit(1);
}
thread_count++;
DBUG_PRINT("info", ("Thread started"));
pthread_mutex_unlock(&LOCK_thread_count);
pthread_attr_destroy(&thr_attr);
if ((error= pthread_mutex_lock(&LOCK_thread_count)))
fprintf(stderr,"Got error: %d from pthread_mutex_lock\n",error);
while (thread_count)
{
if ((error= pthread_cond_wait(&COND_thread_count,&LOCK_thread_count)))
fprintf(stderr,"Got error: %d from pthread_cond_wait\n",error);
}
if ((error= pthread_mutex_unlock(&LOCK_thread_count)))
fprintf(stderr,"Got error: %d from pthread_mutex_unlock\n",error);
DBUG_PRINT("info", ("thread ended"));
end_pagecache(&pagecache, 1);
DBUG_PRINT("info", ("Page cache ended"));
if (my_close(file1.file, MYF(0)) != 0)
{
fprintf(stderr, "Got error during file1 closing from close() (errno: %d)\n",
errno);
exit(1);
}
/*my_delete(file1_name, MYF(0));*/
my_thread_global_end();
DBUG_PRINT("info", ("file1 (%d) closed", file1.file));
DBUG_PRINT("info", ("Program end"));
DBUG_RETURN(0);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment