From 6befaeecc9554be09177e699544d9573e699f1c8 Mon Sep 17 00:00:00 2001 From: Simon J Mudd Date: Sat, 22 Nov 2014 00:20:21 +0100 Subject: [PATCH] Add regexp caching to reduce load --- .../file_summary_by_instance/cache.go | 36 ++++++++++++++++++++++ .../file_summary_by_instance.go | 14 ++++++++- .../file_summary_by_instance_row.go | 32 ++++++++++--------- 3 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 performance_schema/file_summary_by_instance/cache.go diff --git a/performance_schema/file_summary_by_instance/cache.go b/performance_schema/file_summary_by_instance/cache.go new file mode 100644 index 0000000..6b2b147 --- /dev/null +++ b/performance_schema/file_summary_by_instance/cache.go @@ -0,0 +1,36 @@ +package file_summary_by_instance + +import ( + "errors" + + // "github.com/sjmudd/pstop/lib" +) + +// provide a mapping from filename to table.schema etc + +var ( + mapped_name map[string]string + total, matched int +) + +func init() { + // setup on startup + mapped_name = make(map[string]string) +} + +func get_from_cache(key string) (result string, err error) { + total++ + if result, ok := mapped_name[key]; ok { + matched++ + // lib.Logger.Println("matched/total:", matched, total) + return result, nil + } else { + // lib.Logger.Println("matched/total:", matched, total) + return "", errors.New("Not found") + } +} + +func save_to_cache(key, value string) string { + mapped_name[key] = value + return value +} diff --git a/performance_schema/file_summary_by_instance/file_summary_by_instance.go b/performance_schema/file_summary_by_instance/file_summary_by_instance.go index 10ef5f4..737caa8 100644 --- a/performance_schema/file_summary_by_instance/file_summary_by_instance.go +++ b/performance_schema/file_summary_by_instance/file_summary_by_instance.go @@ -6,6 +6,7 @@ package file_summary_by_instance import ( "database/sql" + "fmt" "time" "github.com/sjmudd/pstop/lib" @@ -139,7 +140,8 @@ func (t File_summary_by_instance) EmptyRowContent() string { } func (t File_summary_by_instance) Description() string { - return "File I/O by filename (file_summary_by_instance)" + count := t.count_rows() + return fmt.Sprintf("File I/O by filename (file_summary_by_instance) %4d row(s) ", count) } // create a new structure and include various variable values: @@ -152,3 +154,13 @@ func NewFileSummaryByInstance(global_variables map[string]string) *File_summary_ return n } + +func (t File_summary_by_instance) count_rows() int { + var count int + for row := range t.results { + if t.results[row].SUM_TIMER_WAIT > 0 { + count++ + } + } + return count +} diff --git a/performance_schema/file_summary_by_instance/file_summary_by_instance_row.go b/performance_schema/file_summary_by_instance/file_summary_by_instance_row.go index 20bd606..6c10ef4 100644 --- a/performance_schema/file_summary_by_instance/file_summary_by_instance_row.go +++ b/performance_schema/file_summary_by_instance/file_summary_by_instance_row.go @@ -209,6 +209,10 @@ func (t file_summary_by_instance_row) simple_name(global_variables map[string]st path := t.FILE_NAME + if cached_result, err := get_from_cache(path); err == nil { + return cached_result + } + // FIXME and make this work. // re4 := regexp.MustCompile(re_encoded) // if m4 := re4.FindStringSubmatch(path); m4 != nil { @@ -222,33 +226,33 @@ func (t file_summary_by_instance_row) simple_name(global_variables map[string]st if m1 := re_table_file.FindStringSubmatch(path); m1 != nil { // we may match temporary tables so check for them if m2 := re_temp_table.FindStringSubmatch(m1[2]); m2 != nil { - return "" + return save_to_cache(path, "") } // we may match partitioned tables so check for them if m3 := re_part_table.FindStringSubmatch(m1[2]); m3 != nil { - return m1[1] + "." + m3[1] // . (less partition info) + return save_to_cache(path, m1[1]+"."+m3[1]) // .
(less partition info) } - return m1[1] + "." + m1[2] // .
+ return save_to_cache(path, m1[1]+"."+m1[2]) // .
} if re_ibdata.MatchString(path) == true { - return "" + return save_to_cache(path, "") } if re_redo_log.MatchString(path) == true { - return "" + return save_to_cache(path, "") } if re_binlog.MatchString(path) == true { - return "" + return save_to_cache(path, "") } if re_db_opt.MatchString(path) == true { - return "" + return save_to_cache(path, "") } if re_slowlog.MatchString(path) == true { - return "" + return save_to_cache(path, "") } if re_auto_cnf.MatchString(path) == true { - return "" + return save_to_cache(path, "") } // relay logs are a bit complicated. If a full path then easy to // identify,but if a relative path we may need to add $datadir, @@ -261,19 +265,19 @@ func (t file_summary_by_instance_row) simple_name(global_variables map[string]st } re_relay_log := relay_log + `\.(\d{6}|index)$` if regexp.MustCompile(re_relay_log).MatchString(path) == true { - return "" + return save_to_cache(path, "") } } if re_pid_file.MatchString(path) == true { - return "" + return save_to_cache(path, "") } if re_error_msg.MatchString(path) == true { - return "" + return save_to_cache(path, "") } if re_charset.MatchString(path) == true { - return "" + return save_to_cache(path, "") } - return path + return save_to_cache(path, path) } // Convert the imported "table" to a merged one with merged data. -- 2.7.4