-
Notifications
You must be signed in to change notification settings - Fork 712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
resource_group_client: statistic sql cpu cost #6003
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
1ab14fe
statistic cpu
HuSharp c2bfebd
address comment
HuSharp b93667e
Merge branch 'master' into sqllayer_cpu_cost
HuSharp 5a8fdcc
as ms
HuSharp 36447fe
update trikcle and remove redundant code
HuSharp 193019f
add serverless check
HuSharp ee3d3f2
Merge branch 'master' into sqllayer_cpu_cost
HuSharp 362b757
make ci happy
HuSharp d57851e
Merge branch 'master' into sqllayer_cpu_cost
HuSharp b820e50
update Config
HuSharp 71ef3b6
update option
HuSharp 93b9b11
update metric
HuSharp 655f0d4
address comment
HuSharp 5aa8459
address comment
HuSharp 31712e7
Merge branch 'master' into sqllayer_cpu_cost
HuSharp c02bd25
pass race ci
HuSharp f82633a
merge master
HuSharp 95f651a
address comment
HuSharp f8d978a
init val
HuSharp 85bde35
init val
HuSharp 869e8e2
update metrics
HuSharp 3225252
update metrics
HuSharp 91923a0
Merge branch 'master' into sqllayer_cpu_cost
HuSharp d56342f
address comment
HuSharp File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,11 @@ | |
package controller | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/elastic/gosigar" | ||
"github.com/pingcap/log" | ||
"go.uber.org/zap" | ||
|
||
rmpb "github.com/pingcap/kvproto/pkg/resource_manager" | ||
) | ||
|
@@ -26,14 +30,14 @@ import ( | |
type RequestUnit float64 | ||
|
||
// RequestInfo is the interface of the request information provider. A request should be | ||
// able tell whether it's a write request and if so, the written bytes would also be provided. | ||
// able to tell whether it's a write request and if so, the written bytes would also be provided. | ||
type RequestInfo interface { | ||
IsWrite() bool | ||
WriteBytes() uint64 | ||
} | ||
|
||
// ResponseInfo is the interface of the response information provider. A response should be | ||
// able tell how many bytes it read and KV CPU cost in milliseconds. | ||
// able to tell how many bytes it read and KV CPU cost in milliseconds. | ||
type ResponseInfo interface { | ||
ReadBytes() uint64 | ||
KVCPUMs() uint64 | ||
|
@@ -46,7 +50,8 @@ type ResponseInfo interface { | |
type ResourceCalculator interface { | ||
// Trickle is used to calculate the resource consumption periodically rather than on the request path. | ||
// It's mainly used to calculate like the SQL CPU cost. | ||
Trickle(context.Context, *rmpb.Consumption) | ||
// Need to check if it is a serverless environment | ||
Trickle(*rmpb.Consumption) | ||
// BeforeKVRequest is used to calculate the resource consumption before the KV request. | ||
// It's mainly used to calculate the base and write request cost. | ||
BeforeKVRequest(*rmpb.Consumption, RequestInfo) | ||
|
@@ -67,7 +72,7 @@ func newKVCalculator(cfg *Config) *KVCalculator { | |
} | ||
|
||
// Trickle ... | ||
func (kc *KVCalculator) Trickle(ctx context.Context, consumption *rmpb.Consumption) { | ||
func (kc *KVCalculator) Trickle(*rmpb.Consumption) { | ||
} | ||
|
||
// BeforeKVRequest ... | ||
|
@@ -133,9 +138,11 @@ func newSQLCalculator(cfg *Config) *SQLCalculator { | |
return &SQLCalculator{Config: cfg} | ||
} | ||
|
||
// Trickle ... | ||
// TODO: calculate the SQL CPU cost and related resource consumption. | ||
func (dsc *SQLCalculator) Trickle(ctx context.Context, consumption *rmpb.Consumption) { | ||
// Trickle update sql layer CPU consumption. | ||
func (dsc *SQLCalculator) Trickle(consumption *rmpb.Consumption) { | ||
delta := getSQLProcessCPUTime(dsc.isSingleGroupByKeyspace) - consumption.SqlLayerCpuTimeMs | ||
consumption.TotalCpuTimeMs += delta | ||
consumption.SqlLayerCpuTimeMs += delta | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need make it start from 0. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can assign the sql time at the time of initialization? |
||
} | ||
|
||
// BeforeKVRequest ... | ||
|
@@ -205,3 +212,26 @@ func sub(custom1 *rmpb.Consumption, custom2 *rmpb.Consumption) { | |
custom1.KvReadRpcCount -= custom2.KvReadRpcCount | ||
custom1.KvWriteRpcCount -= custom2.KvWriteRpcCount | ||
} | ||
|
||
// getSQLProcessCPUTime returns the cumulative user+system time (in ms) since the process start. | ||
func getSQLProcessCPUTime(isSingleGroupByKeyspace bool) float64 { | ||
if isSingleGroupByKeyspace { | ||
return getSysProcessCPUTime() | ||
} | ||
return getGroupProcessCPUTime() | ||
} | ||
|
||
func getSysProcessCPUTime() float64 { | ||
pid := os.Getpid() | ||
cpuTime := gosigar.ProcTime{} | ||
if err := cpuTime.Get(pid); err != nil { | ||
log.Error("getCPUTime get pid failed", zap.Error(err)) | ||
} | ||
|
||
return float64(cpuTime.User + cpuTime.Sys) | ||
} | ||
|
||
// TODO: Need a way to calculate in the case of multiple groups. | ||
func getGroupProcessCPUTime() float64 { | ||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If IsSingleGroupByKeyspace is false, do we need to add SQL CPU to RU consumption as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean we can keep this code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this time pr is mainly for the IsSingleGroupByKeyspace case.
maybe better add this code when implementing multiple groups.