1
/* Copyright (C) 2000-2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
/* write whats in isam.log */
22
#include "myisamdef.h"
26
#include <sys/resource.h>
29
#define FILENAME(A) (A ? A->show_name : "Unknown")
35
char *name, *show_name;
42
struct test_if_open_param {
47
struct st_access_param
50
struct file_info *found;
53
#define NO_FILEPOS (ulong) ~0L
55
extern int main(int argc,char * *argv);
56
static void get_options(int *argc,char ***argv);
57
static int examine_log(char * file_name,char **table_names);
58
static int read_string(IO_CACHE *file,uchar* *to,uint length);
59
static int file_info_compare(void *cmp_arg, void *a,void *b);
60
static int test_if_open(struct file_info *key,element_count count,
61
struct test_if_open_param *param);
62
static void fix_blob_pointers(MI_INFO *isam,uchar *record);
63
static int test_when_accessed(struct file_info *key,element_count count,
64
struct st_access_param *access_param);
65
static void file_info_free(struct file_info *info);
66
static int close_some_file(TREE *tree);
67
static int reopen_closed_file(TREE *tree,struct file_info *file_info);
68
static int find_record_with_key(struct file_info *file_info,uchar *record);
69
static void printf_log(const char *str,...);
70
static my_bool cmp_filename(struct file_info *file_info,char * name);
72
static uint verbose=0,update=0,test_info=0,max_files=0,re_open_count=0,
73
recover=0,prefix_remove=0,opt_processes=0;
74
static char *log_filename=0, *filepath=0, *write_filename=0;
75
static char *record_pos_file= 0;
76
static ulong com_count[10][3],number_of_commands=(ulong) ~0L,
78
static my_off_t isamlog_filepos,start_offset=0,record_pos= HA_OFFSET_ERROR;
79
static const char *command_name[]=
80
{"open","write","update","delete","close","extra","lock","re-open",
84
int main(int argc, char **argv)
87
ulong total_count,total_error,total_recover;
90
log_filename=myisam_log_filename;
91
get_options(&argc,&argv);
92
/* Number of MyISAM files we can have open at one time */
93
max_files= (my_set_max_open_files(min(max_files,8))-6)/2;
95
printf("Trying to %s MyISAM files according to log '%s'\n",
96
(recover ? "recover" : "update"),log_filename);
97
error= examine_log(log_filename,argv);
98
if (update && ! error)
99
puts("Tables updated successfully");
100
total_count=total_error=total_recover=0;
101
for (i=first=0 ; command_name[i] ; i++)
107
if (verbose || update)
109
puts("Commands Used count Errors Recover errors");
111
printf("%-12s%9ld%10ld%17ld\n",command_name[i],com_count[i][0],
112
com_count[i][1],com_count[i][2]);
113
total_count+=com_count[i][0];
114
total_error+=com_count[i][1];
115
total_recover+=com_count[i][2];
119
printf("%-12s%9ld%10ld%17ld\n","Total",total_count,total_error,
122
printf("Had to do %d re-open because of too few possibly open files\n",
124
VOID(mi_panic(HA_PANIC_CLOSE));
125
my_free_open_file_info();
126
my_end(test_info ? MY_CHECK_ERROR | MY_GIVE_INFO : MY_CHECK_ERROR);
128
return 0; /* No compiler warning */
132
static void get_options(register int *argc, register char ***argv)
135
const char *pos,*usage;
139
usage="Usage: %s [-?iruvDIV] [-c #] [-f #] [-F filepath/] [-o #] [-R file recordpos] [-w write_file] [log-filename [table ...]] \n";
142
while (--*argc > 0 && *(pos = *(++*argv)) == '-' ) {
146
switch((option=*pos)) {
155
number_of_commands=(ulong) atol(pos);
169
max_files=(uint) atoi(pos);
183
start_offset=(my_off_t) strtoll(pos,NULL,10);
194
prefix_remove=atoi(pos);
211
record_pos_file=(char*) pos;
214
record_pos=(my_off_t) strtoll(*(++*argv),NULL,10);
228
write_filename=(char*) pos;
239
filepath= (char*) pos;
247
#include <help_start.h>
248
printf("%s Ver 1.4 for %s at %s\n",my_progname,SYSTEM_TYPE,
250
puts("By Monty, for your professional use\n");
253
puts("Write info about whats in a MyISAM log file.");
254
printf("If no file name is given %s is used\n",log_filename);
256
printf(usage,my_progname);
258
puts("Options: -? or -I \"Info\" -V \"version\" -c \"do only # commands\"");
259
puts(" -f \"max open files\" -F \"filepath\" -i \"extra info\"");
260
puts(" -o \"offset\" -p # \"remove # components from path\"");
261
puts(" -r \"recover\" -R \"file recordposition\"");
262
puts(" -u \"update\" -v \"verbose\" -w \"write file\"");
263
puts("\nOne can give a second and a third '-v' for more verbose.");
264
puts("Normaly one does a update (-u).");
265
puts("If a recover is done all writes and all possibly updates and deletes is done\nand errors are only counted.");
266
puts("If one gives table names as arguments only these tables will be updated\n");
268
#include <help_end.h>
271
printf("illegal option: \"-%c\"\n",*pos);
284
log_filename=(char*) pos;
290
VOID(fprintf(stderr,"option \"%c\" used without or with wrong argument\n",
296
static int examine_log(char * file_name, char **table_names)
298
uint command,result,files_open;
299
ulong access_time,length;
301
int lock_command,mi_result;
302
char isam_file_name[FN_REFLEN],llbuff[21],llbuff2[21];
305
struct test_if_open_param open_param;
309
enum ha_extra_function extra_command;
311
struct file_info file_info,*curr_file_info;
313
if ((file=my_open(file_name,O_RDONLY,MYF(MY_WME))) < 0)
318
if (!(write_file=my_fopen(write_filename,O_WRONLY,MYF(MY_WME))))
320
my_close(file,MYF(0));
325
init_io_cache(&cache,file,0,READ_CACHE,start_offset,0,MYF(0));
326
bzero((uchar*) com_count,sizeof(com_count));
327
init_tree(&tree,0,0,sizeof(file_info),(qsort_cmp2) file_info_compare,1,
328
(tree_element_free) file_info_free, NULL);
329
VOID(init_key_cache(dflt_key_cache,KEY_CACHE_BLOCK_SIZE,KEY_CACHE_SIZE,
332
files_open=0; access_time=0;
333
while (access_time++ != number_of_commands &&
334
!my_b_read(&cache,(uchar*) head,9))
336
isamlog_filepos=my_b_tell(&cache)-9L;
337
file_info.filenr= mi_uint2korr(head+1);
338
isamlog_process=file_info.process=(long) mi_uint4korr(head+3);
341
result= mi_uint2korr(head+7);
342
if ((curr_file_info=(struct file_info*) tree_search(&tree, &file_info,
345
curr_file_info->accessed=access_time;
346
if (update && curr_file_info->used && curr_file_info->closed)
348
if (reopen_closed_file(&tree,curr_file_info))
350
command=sizeof(com_count)/sizeof(com_count[0][0])/3;
356
command=(uint) head[0];
357
if (command < sizeof(com_count)/sizeof(com_count[0][0])/3 &&
358
(!table_names[0] || (curr_file_info && curr_file_info->used)))
360
com_count[command][0]++;
362
com_count[command][1]++;
364
switch ((enum myisam_log_commands) command) {
368
com_count[command][0]--; /* Must be counted explicite */
370
com_count[command][1]--;
374
printf("\nWarning: %s is opened with same process and filenumber\n"
375
"Maybe you should use the -P option ?\n",
376
curr_file_info->show_name);
377
if (my_b_read(&cache,(uchar*) head,2))
380
file_info.show_name=0;
382
if (read_string(&cache,(uchar**) &file_info.name,
383
(uint) mi_uint2korr(head)))
389
/* Fix if old DOS files to new format */
390
for (pos=file_info.name; (pos=strchr(pos,'\\')) ; pos++)
394
for (i=0 ; i < prefix_remove ; i++)
397
if (!(next=strchr(pos,'/')))
403
to=convert_dirname(isam_file_name,filepath,NullS);
405
fn_ext(isam_file_name)[0]=0; /* Remove extension */
407
open_param.name=file_info.name;
409
VOID(tree_walk(&tree,(tree_walk_action) test_if_open,(void*) &open_param,
411
file_info.id=open_param.max_id+1;
413
* In the line below +10 is added to accomodate '<' and '>' chars
414
* plus '\0' at the end, so that there is place for 7 digits.
415
* It is improbable that same table can have that many entries in
417
* The additional space is needed for the sprintf commands two lines
420
file_info.show_name=my_memdup(isam_file_name,
421
(uint) strlen(isam_file_name)+10,
423
if (file_info.id > 1)
424
sprintf(strend(file_info.show_name),"<%d>",file_info.id);
426
file_info.accessed=access_time;
432
for (name=table_names ; *name ; name++)
434
if (!strcmp(*name,isam_file_name))
435
file_info.used=1; /* Update/log only this */
438
if (update && file_info.used)
440
if (files_open >= max_files)
442
if (close_some_file(&tree))
446
if (!(file_info.isam= mi_open(isam_file_name,O_RDWR,
447
HA_OPEN_WAIT_IF_LOCKED)))
449
if (!(file_info.record=my_malloc(file_info.isam->s->base.reclength,
455
VOID(tree_insert(&tree, (uchar*) &file_info, 0, tree.custom_arg));
458
if (verbose && !record_pos_file)
459
printf_log("%s: open -> %d",file_info.show_name, file_info.filenr);
460
com_count[command][0]++;
462
com_count[command][1]++;
466
if (verbose && !record_pos_file &&
467
(!table_names[0] || (curr_file_info && curr_file_info->used)))
468
printf_log("%s: %s -> %d",FILENAME(curr_file_info),
469
command_name[command],result);
472
if (!curr_file_info->closed)
474
VOID(tree_delete(&tree, (uchar*) curr_file_info, 0, tree.custom_arg));
478
if (my_b_read(&cache,(uchar*) head,1))
480
extra_command=(enum ha_extra_function) head[0];
481
if (verbose && !record_pos_file &&
482
(!table_names[0] || (curr_file_info && curr_file_info->used)))
483
printf_log("%s: %s(%d) -> %d",FILENAME(curr_file_info),
484
command_name[command], (int) extra_command,result);
485
if (update && curr_file_info && !curr_file_info->closed)
487
if (mi_extra(curr_file_info->isam, extra_command, 0) != (int) result)
491
"Warning: error %d, expected %d on command %s at %s\n",
492
my_errno,result,command_name[command],
493
llstr(isamlog_filepos,llbuff)));
499
if (my_b_read(&cache,(uchar*) head,8))
501
filepos=mi_sizekorr(head);
502
if (verbose && (!record_pos_file ||
503
((record_pos == filepos || record_pos == NO_FILEPOS) &&
504
!cmp_filename(curr_file_info,record_pos_file))) &&
505
(!table_names[0] || (curr_file_info && curr_file_info->used)))
506
printf_log("%s: %s at %ld -> %d",FILENAME(curr_file_info),
507
command_name[command],(long) filepos,result);
508
if (update && curr_file_info && !curr_file_info->closed)
510
if (mi_rrnd(curr_file_info->isam,curr_file_info->record,filepos))
515
printf_log("error: Didn't find row to delete with mi_rrnd");
516
com_count[command][2]++; /* Mark error */
518
mi_result=mi_delete(curr_file_info->isam,curr_file_info->record);
519
if ((mi_result == 0 && result) ||
520
(mi_result && (uint) my_errno != result))
525
com_count[command][2]++; /* Mark error */
527
printf_log("error: Got result %d from mi_delete instead of %d",
534
if (my_b_read(&cache,(uchar*) head,12))
536
filepos=mi_sizekorr(head);
537
length=mi_uint4korr(head+8);
539
if (read_string(&cache,&buff,(uint) length))
541
if ((!record_pos_file ||
542
((record_pos == filepos || record_pos == NO_FILEPOS) &&
543
!cmp_filename(curr_file_info,record_pos_file))) &&
544
(!table_names[0] || (curr_file_info && curr_file_info->used)))
547
(my_fwrite(write_file,buff,length,MYF(MY_WAIT_IF_FULL | MY_NABP))))
550
printf_log("%s: %s at %ld, length=%ld -> %d",
551
FILENAME(curr_file_info),
552
command_name[command], filepos,length,result);
554
if (update && curr_file_info && !curr_file_info->closed)
556
if (curr_file_info->isam->s->base.blobs)
557
fix_blob_pointers(curr_file_info->isam,buff);
558
if ((enum myisam_log_commands) command == MI_LOG_UPDATE)
560
if (mi_rrnd(curr_file_info->isam,curr_file_info->record,filepos))
568
printf_log("error: Didn't find row to update with mi_rrnd");
569
if (recover == 1 || result ||
570
find_record_with_key(curr_file_info,buff))
572
com_count[command][2]++; /* Mark error */
576
mi_result=mi_update(curr_file_info->isam,curr_file_info->record,
578
if ((mi_result == 0 && result) ||
579
(mi_result && (uint) my_errno != result))
584
printf_log("error: Got result %d from mi_update instead of %d",
587
com_count[command][2]++; /* Mark error */
592
mi_result=mi_write(curr_file_info->isam,buff);
593
if ((mi_result == 0 && result) ||
594
(mi_result && (uint) my_errno != result))
599
printf_log("error: Got result %d from mi_write instead of %d",
602
com_count[command][2]++; /* Mark error */
604
if (!recover && filepos != curr_file_info->isam->lastpos)
606
printf("error: Wrote at position: %s, should have been %s",
607
llstr(curr_file_info->isam->lastpos,llbuff),
608
llstr(filepos,llbuff2));
613
my_free(buff,MYF(0));
616
if (my_b_read(&cache,(uchar*) head,sizeof(lock_command)))
618
memcpy_fixed(&lock_command,head,sizeof(lock_command));
619
if (verbose && !record_pos_file &&
620
(!table_names[0] || (curr_file_info && curr_file_info->used)))
621
printf_log("%s: %s(%d) -> %d\n",FILENAME(curr_file_info),
622
command_name[command],lock_command,result);
623
if (update && curr_file_info && !curr_file_info->closed)
625
if (mi_lock_database(curr_file_info->isam,lock_command) !=
630
case MI_LOG_DELETE_ALL:
631
if (verbose && !record_pos_file &&
632
(!table_names[0] || (curr_file_info && curr_file_info->used)))
633
printf_log("%s: %s -> %d\n",FILENAME(curr_file_info),
634
command_name[command],result);
639
"Error: found unknown command %d in logfile, aborted\n",
645
end_key_cache(dflt_key_cache,1);
647
VOID(end_io_cache(&cache));
648
VOID(my_close(file,MYF(0)));
649
if (write_file && my_fclose(write_file,MYF(MY_WME)))
655
VOID(fprintf(stderr,"Got error %d when reading from logfile\n",my_errno));
660
VOID(fprintf(stderr,"Got error %d, expected %d on command %s at %s\n",
661
my_errno,result,command_name[command],
662
llstr(isamlog_filepos,llbuff)));
665
end_key_cache(dflt_key_cache, 1);
667
VOID(end_io_cache(&cache));
668
VOID(my_close(file,MYF(0)));
670
VOID(my_fclose(write_file,MYF(MY_WME)));
675
static int read_string(IO_CACHE *file, register uchar* *to, register uint length)
678
my_free((uchar*) *to,MYF(0));
679
if (!(*to= (uchar*) my_malloc(length+1,MYF(MY_WME))) ||
680
my_b_read(file,(uchar*) *to,length))
687
*((char*) *to+length)= '\0';
692
static int file_info_compare(void* cmp_arg __attribute__((unused)),
697
if ((lint=((struct file_info*) a)->process -
698
((struct file_info*) b)->process))
699
return lint < 0L ? -1 : 1;
700
return ((struct file_info*) a)->filenr - ((struct file_info*) b)->filenr;
705
static int test_if_open (struct file_info *key,
706
element_count count __attribute__((unused)),
707
struct test_if_open_param *param)
709
if (!strcmp(key->name,param->name) && key->id > param->max_id)
710
param->max_id=key->id;
715
static void fix_blob_pointers(MI_INFO *info, uchar *record)
720
pos=record+info->s->base.reclength;
721
for (end=info->blobs+info->s->base.blobs, blob= info->blobs;
725
memcpy_fixed(record+blob->offset+blob->pack_length,&pos,sizeof(char*));
726
pos+=_mi_calc_blob_length(blob->pack_length,record+blob->offset);
730
/* close the file with hasn't been accessed for the longest time */
733
static int test_when_accessed (struct file_info *key,
734
element_count count __attribute__((unused)),
735
struct st_access_param *access_param)
737
if (key->accessed < access_param->min_accessed && ! key->closed)
739
access_param->min_accessed=key->accessed;
740
access_param->found=key;
746
static void file_info_free(struct file_info *fileinfo)
750
if (!fileinfo->closed)
751
VOID(mi_close(fileinfo->isam));
752
if (fileinfo->record)
753
my_free(fileinfo->record,MYF(0));
755
my_free(fileinfo->name,MYF(0));
756
my_free(fileinfo->show_name,MYF(0));
762
static int close_some_file(TREE *tree)
764
struct st_access_param access_param;
766
access_param.min_accessed=LONG_MAX;
767
access_param.found=0;
769
VOID(tree_walk(tree,(tree_walk_action) test_when_accessed,
770
(void*) &access_param,left_root_right));
771
if (!access_param.found)
772
return 1; /* No open file that is possibly to close */
773
if (mi_close(access_param.found->isam))
775
access_param.found->closed=1;
780
static int reopen_closed_file(TREE *tree, struct file_info *fileinfo)
782
char name[FN_REFLEN];
783
if (close_some_file(tree))
784
return 1; /* No file to close */
785
strmov(name,fileinfo->show_name);
786
if (fileinfo->id > 1)
787
*strrchr(name,'<')='\0'; /* Remove "<id>" */
789
if (!(fileinfo->isam= mi_open(name,O_RDWR,HA_OPEN_WAIT_IF_LOCKED)))
796
/* Try to find record with uniq key */
798
static int find_record_with_key(struct file_info *file_info, uchar *record)
801
MI_INFO *info=file_info->isam;
802
uchar tmp_key[MI_MAX_KEY_BUFF];
804
for (key=0 ; key < info->s->base.keys ; key++)
806
if (mi_is_key_active(info->s->state.key_map, key) &&
807
info->s->keyinfo[key].flag & HA_NOSAME)
809
VOID(_mi_make_key(info,key,tmp_key,record,0L));
810
return mi_rkey(info,file_info->record,(int) key,tmp_key,0,
818
static void printf_log(const char *format,...)
822
va_start(args,format);
824
printf("%9s:",llstr(isamlog_filepos,llbuff));
826
printf("%5ld ",isamlog_process); /* Write process number */
827
(void) vprintf((char*) format,args);
833
static my_bool cmp_filename(struct file_info *file_info, char * name)
837
return strcmp(file_info->name,name) ? 1 : 0;