|
| 1 | +/* |
| 2 | + Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + or more contributor license agreements. See the NOTICE file |
| 4 | + distributed with this work for additional information |
| 5 | + regarding copyright ownership. The ASF licenses this file |
| 6 | + to you under the Apache License, Version 2.0 (the |
| 7 | + "License"); you may not use this file except in compliance |
| 8 | + with the License. You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, software |
| 13 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + See the License for the specific language governing permissions and |
| 16 | + limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package resources |
| 20 | + |
| 21 | +import ( |
| 22 | + "fmt" |
| 23 | + "strings" |
| 24 | + "sync" |
| 25 | + "time" |
| 26 | +) |
| 27 | + |
| 28 | +// TrackedResource is a utility struct to keep track of application resource usage. |
| 29 | +type TrackedResource struct { |
| 30 | + // TrackedResourceMap is a two-level map for aggregated resource usage. |
| 31 | + // The top-level key is the instance type, and the value is a map: |
| 32 | + // resource type (CPU, memory, etc.) -> aggregated used time (in seconds) of the resource type. |
| 33 | + TrackedResourceMap map[string]map[string]int64 |
| 34 | + |
| 35 | + sync.RWMutex |
| 36 | +} |
| 37 | + |
| 38 | +// NewTrackedResource creates a new instance of TrackedResource. |
| 39 | +func NewTrackedResource() *TrackedResource { |
| 40 | + return &TrackedResource{TrackedResourceMap: make(map[string]map[string]int64)} |
| 41 | +} |
| 42 | + |
| 43 | +// NewTrackedResourceFromMap creates NewTrackedResource from the given map. |
| 44 | +// Using for Testing purpose only. |
| 45 | +func NewTrackedResourceFromMap(m map[string]map[string]int64) *TrackedResource { |
| 46 | + if m == nil { |
| 47 | + return NewTrackedResource() |
| 48 | + } |
| 49 | + return &TrackedResource{TrackedResourceMap: m} |
| 50 | +} |
| 51 | + |
| 52 | +func (tr *TrackedResource) String() string { |
| 53 | + tr.RLock() |
| 54 | + defer tr.RUnlock() |
| 55 | + |
| 56 | + var resourceUsage []string |
| 57 | + for instanceType, resourceTypeMap := range tr.TrackedResourceMap { |
| 58 | + for resourceType, usageTime := range resourceTypeMap { |
| 59 | + resourceUsage = append(resourceUsage, fmt.Sprintf("%s:%s=%d", instanceType, resourceType, usageTime)) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return fmt.Sprintf("TrackedResource{%s}", strings.Join(resourceUsage, ",")) |
| 64 | +} |
| 65 | + |
| 66 | +// Clone creates a deep copy of TrackedResource. |
| 67 | +func (tr *TrackedResource) Clone() *TrackedResource { |
| 68 | + if tr == nil { |
| 69 | + return nil |
| 70 | + } |
| 71 | + ret := NewTrackedResource() |
| 72 | + tr.RLock() |
| 73 | + defer tr.RUnlock() |
| 74 | + for k, v := range tr.TrackedResourceMap { |
| 75 | + dest := make(map[string]int64) |
| 76 | + for key, element := range v { |
| 77 | + dest[key] = element |
| 78 | + } |
| 79 | + ret.TrackedResourceMap[k] = dest |
| 80 | + } |
| 81 | + return ret |
| 82 | +} |
| 83 | + |
| 84 | +// AggregateTrackedResource aggregates resource usage to TrackedResourceMap[instType]. |
| 85 | +// The time the given resource used is the delta between the resource createTime and currentTime. |
| 86 | +func (tr *TrackedResource) AggregateTrackedResource(instType string, |
| 87 | + resource *Resource, bindTime time.Time) { |
| 88 | + if resource == nil { |
| 89 | + return |
| 90 | + } |
| 91 | + tr.Lock() |
| 92 | + defer tr.Unlock() |
| 93 | + |
| 94 | + releaseTime := time.Now() |
| 95 | + timeDiff := int64(releaseTime.Sub(bindTime).Seconds()) |
| 96 | + aggregatedResourceTime, ok := tr.TrackedResourceMap[instType] |
| 97 | + if !ok { |
| 98 | + aggregatedResourceTime = map[string]int64{} |
| 99 | + } |
| 100 | + for key, element := range resource.Resources { |
| 101 | + curUsage, ok := aggregatedResourceTime[key] |
| 102 | + if !ok { |
| 103 | + curUsage = 0 |
| 104 | + } |
| 105 | + curUsage += int64(element) * timeDiff // resource size times timeDiff |
| 106 | + aggregatedResourceTime[key] = curUsage |
| 107 | + } |
| 108 | + tr.TrackedResourceMap[instType] = aggregatedResourceTime |
| 109 | +} |
0 commit comments