blob: 702f295657812edd9bdaf991d063773670a919c2 [file] [log] [blame]
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Stephane Barbariedc5022d2018-11-19 15:21:44 -050016
Stephane Barbarieec0919b2018-09-05 14:14:29 -040017package model
18
19import (
khenaidoob9203542018-09-17 22:56:37 -040020 "sync"
npujar9a30c702019-11-14 17:06:39 +053021
22 "github.com/opencord/voltha-lib-go/v2/pkg/log"
Stephane Barbarieec0919b2018-09-05 14:14:29 -040023)
24
Stephane Barbariedc5022d2018-11-19 15:21:44 -050025// Profiling is used to store performance details collected at runtime
npujar9a30c702019-11-14 17:06:39 +053026type Profiling struct {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050027 sync.RWMutex
Stephane Barbarieec0919b2018-09-05 14:14:29 -040028 DatabaseRetrieveTime float64
29 DatabaseRetrieveCount int
30 InMemoryModelTime float64
31 InMemoryModelCount int
32 InMemoryProcessTime float64
33 DatabaseStoreTime float64
Stephane Barbariea188d942018-10-16 16:43:04 -040034 InMemoryLockTime float64
35 InMemoryLockCount int
Stephane Barbarieec0919b2018-09-05 14:14:29 -040036}
37
npujar9a30c702019-11-14 17:06:39 +053038var profilingInstance *Profiling
Stephane Barbariedc5022d2018-11-19 15:21:44 -050039var profilingOnce sync.Once
Stephane Barbarieec0919b2018-09-05 14:14:29 -040040
Stephane Barbariedc5022d2018-11-19 15:21:44 -050041// GetProfiling returns a singleton instance of the Profiling structure
npujar9a30c702019-11-14 17:06:39 +053042func GetProfiling() *Profiling {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050043 profilingOnce.Do(func() {
npujar9a30c702019-11-14 17:06:39 +053044 profilingInstance = &Profiling{}
Stephane Barbarieec0919b2018-09-05 14:14:29 -040045 })
Stephane Barbariedc5022d2018-11-19 15:21:44 -050046 return profilingInstance
Stephane Barbarieec0919b2018-09-05 14:14:29 -040047}
48
Stephane Barbariedc5022d2018-11-19 15:21:44 -050049// AddToDatabaseRetrieveTime appends a time period to retrieve data from the database
npujar9a30c702019-11-14 17:06:39 +053050func (p *Profiling) AddToDatabaseRetrieveTime(period float64) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050051 p.Lock()
52 defer p.Unlock()
53
Stephane Barbarieec0919b2018-09-05 14:14:29 -040054 p.DatabaseRetrieveTime += period
Stephane Barbariedc5022d2018-11-19 15:21:44 -050055 p.DatabaseRetrieveCount++
Stephane Barbarieec0919b2018-09-05 14:14:29 -040056}
Stephane Barbariedc5022d2018-11-19 15:21:44 -050057
58// AddToInMemoryModelTime appends a time period to construct/deconstruct data in memory
npujar9a30c702019-11-14 17:06:39 +053059func (p *Profiling) AddToInMemoryModelTime(period float64) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050060 p.Lock()
61 defer p.Unlock()
62
Stephane Barbarieec0919b2018-09-05 14:14:29 -040063 p.InMemoryModelTime += period
Stephane Barbariedc5022d2018-11-19 15:21:44 -050064 p.InMemoryModelCount++
Stephane Barbarieec0919b2018-09-05 14:14:29 -040065}
Stephane Barbariedc5022d2018-11-19 15:21:44 -050066
67// AddToInMemoryProcessTime appends a time period to process data
npujar9a30c702019-11-14 17:06:39 +053068func (p *Profiling) AddToInMemoryProcessTime(period float64) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050069 p.Lock()
70 defer p.Unlock()
71
Stephane Barbarieec0919b2018-09-05 14:14:29 -040072 p.InMemoryProcessTime += period
73}
Stephane Barbariedc5022d2018-11-19 15:21:44 -050074
75// AddToDatabaseStoreTime appends a time period to store data in the database
npujar9a30c702019-11-14 17:06:39 +053076func (p *Profiling) AddToDatabaseStoreTime(period float64) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050077 p.Lock()
78 defer p.Unlock()
79
Stephane Barbarieec0919b2018-09-05 14:14:29 -040080 p.DatabaseStoreTime += period
81}
82
Stephane Barbariedc5022d2018-11-19 15:21:44 -050083// AddToInMemoryLockTime appends a time period when a code block was locked
npujar9a30c702019-11-14 17:06:39 +053084func (p *Profiling) AddToInMemoryLockTime(period float64) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050085 p.Lock()
86 defer p.Unlock()
87
Stephane Barbariea188d942018-10-16 16:43:04 -040088 p.InMemoryLockTime += period
Stephane Barbariedc5022d2018-11-19 15:21:44 -050089 p.InMemoryLockCount++
Stephane Barbariea188d942018-10-16 16:43:04 -040090}
91
Stephane Barbariedc5022d2018-11-19 15:21:44 -050092// Reset initializes the profile counters
npujar9a30c702019-11-14 17:06:39 +053093func (p *Profiling) Reset() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050094 p.Lock()
95 defer p.Unlock()
96
Stephane Barbariea188d942018-10-16 16:43:04 -040097 p.DatabaseRetrieveTime = 0
98 p.DatabaseRetrieveCount = 0
99 p.InMemoryModelTime = 0
100 p.InMemoryModelCount = 0
101 p.InMemoryProcessTime = 0
102 p.DatabaseStoreTime = 0
103 p.InMemoryLockTime = 0
104 p.InMemoryLockCount = 0
105}
106
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500107// Report will provide the current profile counter status
npujar9a30c702019-11-14 17:06:39 +0530108func (p *Profiling) Report() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500109 p.Lock()
110 defer p.Unlock()
111
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400112 log.Infof("[ Profiling Report ]")
113 log.Infof("Database Retrieval : %f", p.DatabaseRetrieveTime)
114 log.Infof("Database Retrieval Count : %d", p.DatabaseRetrieveCount)
115 log.Infof("Avg Database Retrieval : %f", p.DatabaseRetrieveTime/float64(p.DatabaseRetrieveCount))
116 log.Infof("In-Memory Modeling : %f", p.InMemoryModelTime)
117 log.Infof("In-Memory Modeling Count: %d", p.InMemoryModelCount)
118 log.Infof("Avg In-Memory Modeling : %f", p.InMemoryModelTime/float64(p.InMemoryModelCount))
Stephane Barbariea188d942018-10-16 16:43:04 -0400119 log.Infof("In-Memory Locking : %f", p.InMemoryLockTime)
120 log.Infof("In-Memory Locking Count: %d", p.InMemoryLockCount)
121 log.Infof("Avg In-Memory Locking : %f", p.InMemoryLockTime/float64(p.InMemoryLockCount))
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400122
123}