@@ -31,7 +31,6 @@ import (
3131 runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
3232
3333 containerstore "github.com/containerd/containerd/pkg/cri/store/container"
34- "github.com/containerd/containerd/pkg/cri/store/stats"
3534)
3635
3736func (c * criService ) containerMetrics (
@@ -95,72 +94,6 @@ func (c *criService) containerMetrics(
9594 return & cs , nil
9695}
9796
98- func (c * criService ) getUsageNanoCores (containerID string , isSandbox bool , currentUsageCoreNanoSeconds uint64 , currentTimestamp time.Time ) (uint64 , error ) {
99- var oldStats * stats.ContainerStats
100-
101- if isSandbox {
102- sandbox , err := c .sandboxStore .Get (containerID )
103- if err != nil {
104- return 0 , fmt .Errorf ("failed to get sandbox container: %s: %w" , containerID , err )
105- }
106- oldStats = sandbox .Stats
107- } else {
108- container , err := c .containerStore .Get (containerID )
109- if err != nil {
110- return 0 , fmt .Errorf ("failed to get container ID: %s: %w" , containerID , err )
111- }
112- oldStats = container .Stats
113- }
114-
115- if oldStats == nil {
116- newStats := & stats.ContainerStats {
117- UsageCoreNanoSeconds : currentUsageCoreNanoSeconds ,
118- Timestamp : currentTimestamp ,
119- }
120- if isSandbox {
121- err := c .sandboxStore .UpdateContainerStats (containerID , newStats )
122- if err != nil {
123- return 0 , fmt .Errorf ("failed to update sandbox stats container ID: %s: %w" , containerID , err )
124- }
125- } else {
126- err := c .containerStore .UpdateContainerStats (containerID , newStats )
127- if err != nil {
128- return 0 , fmt .Errorf ("failed to update container stats ID: %s: %w" , containerID , err )
129- }
130- }
131- return 0 , nil
132- }
133-
134- nanoSeconds := currentTimestamp .UnixNano () - oldStats .Timestamp .UnixNano ()
135-
136- // zero or negative interval
137- if nanoSeconds <= 0 {
138- return 0 , nil
139- }
140-
141- newUsageNanoCores := uint64 (float64 (currentUsageCoreNanoSeconds - oldStats .UsageCoreNanoSeconds ) /
142- float64 (nanoSeconds ) * float64 (time .Second / time .Nanosecond ))
143-
144- newStats := & stats.ContainerStats {
145- UsageCoreNanoSeconds : currentUsageCoreNanoSeconds ,
146- Timestamp : currentTimestamp ,
147- }
148- if isSandbox {
149- err := c .sandboxStore .UpdateContainerStats (containerID , newStats )
150- if err != nil {
151- return 0 , fmt .Errorf ("failed to update sandbox container stats: %s: %w" , containerID , err )
152- }
153-
154- } else {
155- err := c .containerStore .UpdateContainerStats (containerID , newStats )
156- if err != nil {
157- return 0 , fmt .Errorf ("failed to update container stats ID: %s: %w" , containerID , err )
158- }
159- }
160-
161- return newUsageNanoCores , nil
162- }
163-
16497// getWorkingSet calculates workingset memory from cgroup memory stats.
16598// The caller should make sure memory is not nil.
16699// workingset = usage - total_inactive_file
@@ -216,32 +149,19 @@ func (c *criService) cpuContainerStats(ID string, isSandbox bool, stats interfac
216149 switch metrics := stats .(type ) {
217150 case * v1.Metrics :
218151 if metrics .CPU != nil && metrics .CPU .Usage != nil {
219-
220- usageNanoCores , err := c .getUsageNanoCores (ID , isSandbox , metrics .CPU .Usage .Total , timestamp )
221- if err != nil {
222- return nil , fmt .Errorf ("failed to get usage nano cores, containerID: %s: %w" , ID , err )
223- }
224-
225152 return & runtime.CpuUsage {
226153 Timestamp : timestamp .UnixNano (),
227154 UsageCoreNanoSeconds : & runtime.UInt64Value {Value : metrics .CPU .Usage .Total },
228- UsageNanoCores : & runtime.UInt64Value {Value : usageNanoCores },
229155 }, nil
230156 }
231157 case * v2.Metrics :
232158 if metrics .CPU != nil {
233159 // convert to nano seconds
234160 usageCoreNanoSeconds := metrics .CPU .UsageUsec * 1000
235161
236- usageNanoCores , err := c .getUsageNanoCores (ID , isSandbox , usageCoreNanoSeconds , timestamp )
237- if err != nil {
238- return nil , fmt .Errorf ("failed to get usage nano cores, containerID: %s: %w" , ID , err )
239- }
240-
241162 return & runtime.CpuUsage {
242163 Timestamp : timestamp .UnixNano (),
243164 UsageCoreNanoSeconds : & runtime.UInt64Value {Value : usageCoreNanoSeconds },
244- UsageNanoCores : & runtime.UInt64Value {Value : usageNanoCores },
245165 }, nil
246166 }
247167 default :
0 commit comments